ALSA: scarlett2: Remove scarlett2_config_sets array
[linux-2.6-block.git] / sound / usb / mixer_scarlett2.c
CommitLineData
9e4d5c1b
GB
1// SPDX-License-Identifier: GPL-2.0
2/*
efc3d7d2
GB
3 * Focusrite Scarlett 2 Protocol Driver for ALSA
4 * (including Scarlett 2nd Gen, 3rd Gen, Clarett USB, and Clarett+
5 * series products)
9e4d5c1b 6 *
4be47798
GB
7 * Supported models:
8 * - 6i6/18i8/18i20 Gen 2
2fa96277 9 * - Solo/2i2/4i4/8i6/18i8/18i20 Gen 3
2b17b489 10 * - Clarett 2Pre/4Pre/8Pre USB
b61a3aca 11 * - Clarett+ 2Pre/4Pre/8Pre
4be47798 12 *
b9a98cdd 13 * Copyright (c) 2018-2023 by Geoffrey D. Bennett <g at b4.vu>
2fa96277 14 * Copyright (c) 2020-2021 by Vladimir Sadovnikov <sadko4u@gmail.com>
f71c70df 15 * Copyright (c) 2022 by Christian Colglazier <christian@cacolglazier.com>
9e4d5c1b
GB
16 *
17 * Based on the Scarlett (Gen 1) Driver for ALSA:
18 *
19 * Copyright (c) 2013 by Tobias Hoffmann
20 * Copyright (c) 2013 by Robin Gareus <robin at gareus.org>
21 * Copyright (c) 2002 by Takashi Iwai <tiwai at suse.de>
22 * Copyright (c) 2014 by Chris J Arges <chris.j.arges at canonical.com>
23 *
24 * Many codes borrowed from audio.c by
25 * Alan Cox (alan at lxorguk.ukuu.org.uk)
26 * Thomas Sailer (sailer at ife.ee.ethz.ch)
27 *
28 * Code cleanup:
29 * David Henningsson <david.henningsson at canonical.com>
30 */
31
9e4d5c1b
GB
32/* The protocol was reverse engineered by looking at the communication
33 * between Focusrite Control 2.3.4 and the Focusrite(R) Scarlett 18i20
34 * (firmware 1083) using usbmon in July-August 2018.
35 *
36 * Scarlett 18i8 support added in April 2019.
37 *
38 * Scarlett 6i6 support added in June 2019 (thanks to Martin Wittmann
39 * for providing usbmon output and testing).
40 *
4be47798
GB
41 * Scarlett 4i4/8i6 Gen 3 support added in May 2020 (thanks to Laurent
42 * Debricon for donating a 4i4 and to Fredrik Unger for providing 8i6
43 * usbmon output and testing).
44 *
45 * Scarlett 18i8/18i20 Gen 3 support added in June 2020 (thanks to
46 * Darren Jaeckel, Alex Sedlack, and Clovis Lunel for providing usbmon
47 * output, protocol traces and testing).
48 *
d6f9afe9
GB
49 * Support for loading mixer volume and mux configuration from the
50 * interface during driver initialisation added in May 2021 (thanks to
51 * Vladimir Sadovnikov for figuring out how).
52 *
2fa96277
GB
53 * Support for Solo/2i2 Gen 3 added in May 2021 (thanks to Alexander
54 * Vorona for 2i2 protocol traces).
55 *
d5bda7e0
GB
56 * Support for phantom power, direct monitoring, speaker switching,
57 * and talkback added in May-June 2021.
ae58a1a1 58 *
f71c70df
CC
59 * Support for Clarett+ 8Pre added in Aug 2022 by Christian
60 * Colglazier.
61 *
b9a98cdd
GB
62 * Support for Clarett 8Pre USB added in Sep 2023 (thanks to Philippe
63 * Perrot for confirmation).
64 *
b61a3aca
GB
65 * Support for Clarett+ 4Pre and 2Pre added in Sep 2023 (thanks to
66 * Gregory Rozzo for donating a 4Pre, and David Sherwood and Patrice
67 * Peterson for usbmon output).
68 *
2b17b489
GB
69 * Support for Clarett 2Pre and 4Pre USB added in Oct 2023.
70 *
303f204e 71 * This ALSA mixer gives access to (model-dependent):
9e4d5c1b 72 * - input, output, mixer-matrix muxes
4be47798 73 * - mixer-matrix gain stages
0c88f9db 74 * - gain/volume/mute controls
9e4d5c1b 75 * - level meters
dbbd4f9e 76 * - line/inst level, pad, and air controls
d5bda7e0
GB
77 * - phantom power, direct monitor, speaker switching, and talkback
78 * controls
303f204e 79 * - disable/enable MSD mode
604b3884 80 * - disable/enable standalone mode
9e4d5c1b
GB
81 *
82 * <ditaa>
83 * /--------------\ 18chn 20chn /--------------\
84 * | Hardware in +--+------\ /-------------+--+ ALSA PCM out |
85 * \--------------/ | | | | \--------------/
86 * | | | /-----\ |
87 * | | | | | |
88 * | v v v | |
89 * | +---------------+ | |
90 * | \ Matrix Mux / | |
91 * | +-----+-----+ | |
92 * | | | |
93 * | |18chn | |
94 * | | | |
95 * | | 10chn| |
96 * | v | |
97 * | +------------+ | |
98 * | | Mixer | | |
99 * | | Matrix | | |
100 * | | | | |
101 * | | 18x10 Gain | | |
102 * | | stages | | |
103 * | +-----+------+ | |
104 * | | | |
105 * |18chn |10chn | |20chn
106 * | | | |
107 * | +----------/ |
108 * | | |
109 * v v v
110 * ===========================
111 * +---------------+ +--—------------+
112 * \ Output Mux / \ Capture Mux /
113 * +---+---+---+ +-----+-----+
114 * | | |
115 * 10chn| | |18chn
116 * | | |
117 * /--------------\ | | | /--------------\
118 * | S/PDIF, ADAT |<--/ |10chn \-->| ALSA PCM in |
119 * | Hardware out | | \--------------/
120 * \--------------/ |
121 * v
122 * +-------------+ Software gain per channel.
123 * | Master Gain |<-- 18i20 only: Switch per channel
124 * +------+------+ to select HW or SW gain control.
125 * |
126 * |10chn
127 * /--------------\ |
128 * | Analogue |<------/
129 * | Hardware out |
130 * \--------------/
131 * </ditaa>
132 *
303f204e
GB
133 * Gen 3 devices have a Mass Storage Device (MSD) mode where a small
134 * disk with registration and driver download information is presented
135 * to the host. To access the full functionality of the device without
136 * proprietary software, MSD mode can be disabled by:
137 * - holding down the 48V button for five seconds while powering on
138 * the device, or
139 * - using this driver and alsamixer to change the "MSD Mode" setting
140 * to Off and power-cycling the device
9e4d5c1b
GB
141 */
142
143#include <linux/slab.h>
144#include <linux/usb.h>
145#include <linux/moduleparam.h>
146
147#include <sound/control.h>
148#include <sound/tlv.h>
337b2f0e
GB
149#include <sound/hwdep.h>
150
151#include <uapi/sound/scarlett2.h>
9e4d5c1b
GB
152
153#include "usbaudio.h"
154#include "mixer.h"
155#include "helper.h"
156
efc3d7d2 157#include "mixer_scarlett2.h"
9e4d5c1b 158
303f204e
GB
159/* device_setup value to allow turning MSD mode back on */
160#define SCARLETT2_MSD_ENABLE 0x02
161
bc83058f
GB
162/* device_setup value to disable this mixer driver */
163#define SCARLETT2_DISABLE 0x04
164
9e4d5c1b
GB
165/* some gui mixers can't handle negative ctl values */
166#define SCARLETT2_VOLUME_BIAS 127
167
168/* mixer range from -80dB to +6dB in 0.5dB steps */
169#define SCARLETT2_MIXER_MIN_DB -80
170#define SCARLETT2_MIXER_BIAS (-SCARLETT2_MIXER_MIN_DB * 2)
171#define SCARLETT2_MIXER_MAX_DB 6
172#define SCARLETT2_MIXER_MAX_VALUE \
173 ((SCARLETT2_MIXER_MAX_DB - SCARLETT2_MIXER_MIN_DB) * 2)
2661f033 174#define SCARLETT2_MIXER_VALUE_COUNT (SCARLETT2_MIXER_MAX_VALUE + 1)
9e4d5c1b
GB
175
176/* map from (dB + 80) * 2 to mixer value
177 * for dB in 0 .. 172: int(8192 * pow(10, ((dB - 160) / 2 / 20)))
178 */
2661f033 179static const u16 scarlett2_mixer_values[SCARLETT2_MIXER_VALUE_COUNT] = {
9e4d5c1b
GB
180 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2,
181 2, 2, 3, 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 6, 6, 6, 7, 7, 8, 8,
182 9, 9, 10, 10, 11, 12, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21,
183 23, 24, 25, 27, 29, 30, 32, 34, 36, 38, 41, 43, 46, 48, 51,
184 54, 57, 61, 65, 68, 73, 77, 81, 86, 91, 97, 103, 109, 115,
185 122, 129, 137, 145, 154, 163, 173, 183, 194, 205, 217, 230,
186 244, 259, 274, 290, 307, 326, 345, 365, 387, 410, 434, 460,
187 487, 516, 547, 579, 614, 650, 689, 730, 773, 819, 867, 919,
188 973, 1031, 1092, 1157, 1225, 1298, 1375, 1456, 1543, 1634,
189 1731, 1833, 1942, 2057, 2179, 2308, 2445, 2590, 2744, 2906,
190 3078, 3261, 3454, 3659, 3876, 4105, 4349, 4606, 4879, 5168,
191 5475, 5799, 6143, 6507, 6892, 7301, 7733, 8192, 8677, 9191,
192 9736, 10313, 10924, 11571, 12257, 12983, 13752, 14567, 15430,
193 16345
194};
195
196/* Maximum number of analogue outputs */
197#define SCARLETT2_ANALOGUE_MAX 10
198
199/* Maximum number of level and pad switches */
200#define SCARLETT2_LEVEL_SWITCH_MAX 2
4be47798 201#define SCARLETT2_PAD_SWITCH_MAX 8
dbbd4f9e 202#define SCARLETT2_AIR_SWITCH_MAX 8
ae58a1a1 203#define SCARLETT2_PHANTOM_SWITCH_MAX 2
9e4d5c1b
GB
204
205/* Maximum number of inputs to the mixer */
4be47798 206#define SCARLETT2_INPUT_MIX_MAX 25
9e4d5c1b
GB
207
208/* Maximum number of outputs from the mixer */
4be47798 209#define SCARLETT2_OUTPUT_MIX_MAX 12
9e4d5c1b
GB
210
211/* Maximum size of the data in the USB mux assignment message:
4be47798 212 * 20 inputs, 20 outputs, 25 matrix inputs, 12 spare
9e4d5c1b 213 */
4be47798 214#define SCARLETT2_MUX_MAX 77
9e4d5c1b 215
3473185f
GB
216/* Maximum number of sources (sum of input port counts) */
217#define SCARLETT2_MAX_SRCS 52
218
b126bbac 219/* Maximum number of meters (sum of output port counts) */
4be47798 220#define SCARLETT2_MAX_METERS 65
9e4d5c1b
GB
221
222/* Hardware port types:
223 * - None (no input to mux)
224 * - Analogue I/O
225 * - S/PDIF I/O
226 * - ADAT I/O
227 * - Mixer I/O
228 * - PCM I/O
229 */
230enum {
a2bb6c7d
GB
231 SCARLETT2_PORT_TYPE_NONE,
232 SCARLETT2_PORT_TYPE_ANALOGUE,
233 SCARLETT2_PORT_TYPE_SPDIF,
234 SCARLETT2_PORT_TYPE_ADAT,
235 SCARLETT2_PORT_TYPE_MIX,
236 SCARLETT2_PORT_TYPE_PCM,
237 SCARLETT2_PORT_TYPE_COUNT
9e4d5c1b
GB
238};
239
6522c364 240/* I/O count of each port type kept in struct scarlett2_ports */
9e4d5c1b 241enum {
a2bb6c7d
GB
242 SCARLETT2_PORT_IN,
243 SCARLETT2_PORT_OUT,
244 SCARLETT2_PORT_DIRNS
9e4d5c1b
GB
245};
246
dbd82c05 247/* Dim/Mute buttons on the 18i20 */
0c88f9db 248enum {
a2bb6c7d
GB
249 SCARLETT2_BUTTON_MUTE,
250 SCARLETT2_BUTTON_DIM,
251 SCARLETT2_DIM_MUTE_COUNT
0c88f9db 252};
9e4d5c1b 253
6a7508e6
GB
254/* Flash Write State */
255enum {
a2bb6c7d
GB
256 SCARLETT2_FLASH_WRITE_STATE_IDLE,
257 SCARLETT2_FLASH_WRITE_STATE_SELECTED,
258 SCARLETT2_FLASH_WRITE_STATE_ERASING,
259 SCARLETT2_FLASH_WRITE_STATE_WRITE
6a7508e6
GB
260};
261
dbd82c05 262static const char *const scarlett2_dim_mute_names[SCARLETT2_DIM_MUTE_COUNT] = {
cdf72837 263 "Mute Playback Switch", "Dim Playback Switch"
9e4d5c1b
GB
264};
265
cbd6f148
GB
266/* Configuration parameters that can be read and written */
267enum {
268 SCARLETT2_CONFIG_DIM_MUTE,
269 SCARLETT2_CONFIG_LINE_OUT_VOLUME,
270 SCARLETT2_CONFIG_MUTE_SWITCH,
271 SCARLETT2_CONFIG_SW_HW_SWITCH,
272 SCARLETT2_CONFIG_LEVEL_SWITCH,
273 SCARLETT2_CONFIG_PAD_SWITCH,
274 SCARLETT2_CONFIG_MSD_SWITCH,
275 SCARLETT2_CONFIG_AIR_SWITCH,
276 SCARLETT2_CONFIG_STANDALONE_SWITCH,
277 SCARLETT2_CONFIG_PHANTOM_SWITCH,
278 SCARLETT2_CONFIG_PHANTOM_PERSISTENCE,
279 SCARLETT2_CONFIG_DIRECT_MONITOR,
280 SCARLETT2_CONFIG_MONITOR_OTHER_SWITCH,
281 SCARLETT2_CONFIG_MONITOR_OTHER_ENABLE,
282 SCARLETT2_CONFIG_TALKBACK_MAP,
283 SCARLETT2_CONFIG_COUNT
284};
285
286/* Location, size, and activation command number for the configuration
287 * parameters. Size is in bits and may be 1, 8, or 16.
288 */
289struct scarlett2_config {
290 u8 offset;
291 u8 size;
292 u8 activate;
293};
294
295struct scarlett2_config_set {
296 const struct scarlett2_config items[SCARLETT2_CONFIG_COUNT];
297};
298
299/* Gen 2 devices: 6i6, 18i8, 18i20 */
300static const struct scarlett2_config_set scarlett2_config_set_gen2 = {
301 .items = {
302 [SCARLETT2_CONFIG_DIM_MUTE] = {
303 .offset = 0x31, .size = 8, .activate = 2 },
304
305 [SCARLETT2_CONFIG_LINE_OUT_VOLUME] = {
306 .offset = 0x34, .size = 16, .activate = 1 },
307
308 [SCARLETT2_CONFIG_MUTE_SWITCH] = {
309 .offset = 0x5c, .size = 8, .activate = 1 },
310
311 [SCARLETT2_CONFIG_SW_HW_SWITCH] = {
312 .offset = 0x66, .size = 8, .activate = 3 },
313
314 [SCARLETT2_CONFIG_LEVEL_SWITCH] = {
315 .offset = 0x7c, .size = 8, .activate = 7 },
316
317 [SCARLETT2_CONFIG_PAD_SWITCH] = {
318 .offset = 0x84, .size = 8, .activate = 8 },
319
320 [SCARLETT2_CONFIG_STANDALONE_SWITCH] = {
321 .offset = 0x8d, .size = 8, .activate = 6 },
322 }
323};
324
325/* Gen 3 devices without a mixer (Solo and 2i2) */
326static const struct scarlett2_config_set scarlett2_config_set_gen3a = {
327 .items = {
328 [SCARLETT2_CONFIG_MSD_SWITCH] = {
329 .offset = 0x04, .size = 8, .activate = 6 },
330
331 [SCARLETT2_CONFIG_PHANTOM_PERSISTENCE] = {
332 .offset = 0x05, .size = 8, .activate = 6 },
333
334 [SCARLETT2_CONFIG_PHANTOM_SWITCH] = {
335 .offset = 0x06, .size = 8, .activate = 3 },
336
337 [SCARLETT2_CONFIG_DIRECT_MONITOR] = {
338 .offset = 0x07, .size = 8, .activate = 4 },
339
340 [SCARLETT2_CONFIG_LEVEL_SWITCH] = {
341 .offset = 0x08, .size = 1, .activate = 7 },
342
343 [SCARLETT2_CONFIG_AIR_SWITCH] = {
344 .offset = 0x09, .size = 1, .activate = 8 },
345 }
346};
347
348/* Gen 3 devices: 4i4, 8i6, 18i8, 18i20 */
349static const struct scarlett2_config_set scarlett2_config_set_gen3b = {
350 .items = {
351 [SCARLETT2_CONFIG_DIM_MUTE] = {
352 .offset = 0x31, .size = 8, .activate = 2 },
353
354 [SCARLETT2_CONFIG_LINE_OUT_VOLUME] = {
355 .offset = 0x34, .size = 16, .activate = 1 },
356
357 [SCARLETT2_CONFIG_MUTE_SWITCH] = {
358 .offset = 0x5c, .size = 8, .activate = 1 },
359
360 [SCARLETT2_CONFIG_SW_HW_SWITCH] = {
361 .offset = 0x66, .size = 8, .activate = 3 },
362
363 [SCARLETT2_CONFIG_LEVEL_SWITCH] = {
364 .offset = 0x7c, .size = 8, .activate = 7 },
365
366 [SCARLETT2_CONFIG_PAD_SWITCH] = {
367 .offset = 0x84, .size = 8, .activate = 8 },
368
369 [SCARLETT2_CONFIG_AIR_SWITCH] = {
370 .offset = 0x8c, .size = 8, .activate = 8 },
371
372 [SCARLETT2_CONFIG_STANDALONE_SWITCH] = {
373 .offset = 0x95, .size = 8, .activate = 6 },
374
375 [SCARLETT2_CONFIG_PHANTOM_SWITCH] = {
376 .offset = 0x9c, .size = 1, .activate = 8 },
377
378 [SCARLETT2_CONFIG_MSD_SWITCH] = {
379 .offset = 0x9d, .size = 8, .activate = 6 },
380
381 [SCARLETT2_CONFIG_PHANTOM_PERSISTENCE] = {
382 .offset = 0x9e, .size = 8, .activate = 6 },
383
384 [SCARLETT2_CONFIG_MONITOR_OTHER_SWITCH] = {
385 .offset = 0x9f, .size = 1, .activate = 10 },
386
387 [SCARLETT2_CONFIG_MONITOR_OTHER_ENABLE] = {
388 .offset = 0xa0, .size = 1, .activate = 10 },
389
390 [SCARLETT2_CONFIG_TALKBACK_MAP] = {
391 .offset = 0xb0, .size = 16, .activate = 10 },
392 }
393};
394
395/* Clarett USB and Clarett+ devices: 2Pre, 4Pre, 8Pre */
396static const struct scarlett2_config_set scarlett2_config_set_clarett = {
397 .items = {
398 [SCARLETT2_CONFIG_DIM_MUTE] = {
399 .offset = 0x31, .size = 8, .activate = 2 },
400
401 [SCARLETT2_CONFIG_LINE_OUT_VOLUME] = {
402 .offset = 0x34, .size = 16, .activate = 1 },
403
404 [SCARLETT2_CONFIG_MUTE_SWITCH] = {
405 .offset = 0x5c, .size = 8, .activate = 1 },
406
407 [SCARLETT2_CONFIG_SW_HW_SWITCH] = {
408 .offset = 0x66, .size = 8, .activate = 3 },
409
410 [SCARLETT2_CONFIG_LEVEL_SWITCH] = {
411 .offset = 0x7c, .size = 8, .activate = 7 },
412
413 [SCARLETT2_CONFIG_AIR_SWITCH] = {
414 .offset = 0x95, .size = 8, .activate = 8 },
415
416 [SCARLETT2_CONFIG_STANDALONE_SWITCH] = {
417 .offset = 0x8d, .size = 8, .activate = 6 },
418 }
419};
420
9e4d5c1b 421/* Description of each hardware port type:
e2cc91ac 422 * - id: hardware ID of this port type
9e4d5c1b
GB
423 * - src_descr: printf format string for mux input selections
424 * - src_num_offset: added to channel number for the fprintf
425 * - dst_descr: printf format string for mixer controls
426 */
e2cc91ac 427struct scarlett2_port {
9e4d5c1b 428 u16 id;
9e4d5c1b
GB
429 const char * const src_descr;
430 int src_num_offset;
431 const char * const dst_descr;
432};
433
e2cc91ac
GB
434static const struct scarlett2_port scarlett2_ports[SCARLETT2_PORT_TYPE_COUNT] = {
435 [SCARLETT2_PORT_TYPE_NONE] = {
436 .id = 0x000,
437 .src_descr = "Off"
438 },
439 [SCARLETT2_PORT_TYPE_ANALOGUE] = {
440 .id = 0x080,
441 .src_descr = "Analogue %d",
442 .src_num_offset = 1,
443 .dst_descr = "Analogue Output %02d Playback"
444 },
445 [SCARLETT2_PORT_TYPE_SPDIF] = {
446 .id = 0x180,
447 .src_descr = "S/PDIF %d",
448 .src_num_offset = 1,
449 .dst_descr = "S/PDIF Output %d Playback"
450 },
451 [SCARLETT2_PORT_TYPE_ADAT] = {
452 .id = 0x200,
453 .src_descr = "ADAT %d",
454 .src_num_offset = 1,
455 .dst_descr = "ADAT Output %d Playback"
456 },
457 [SCARLETT2_PORT_TYPE_MIX] = {
458 .id = 0x300,
459 .src_descr = "Mix %c",
460 .src_num_offset = 'A',
461 .dst_descr = "Mixer Input %02d Capture"
462 },
463 [SCARLETT2_PORT_TYPE_PCM] = {
464 .id = 0x600,
465 .src_descr = "PCM %d",
466 .src_num_offset = 1,
467 .dst_descr = "PCM %02d Capture"
468 },
469};
470
6522c364
GB
471/* Number of mux tables: one for each band of sample rates
472 * (44.1/48kHz, 88.2/96kHz, and 176.4/176kHz)
473 */
474#define SCARLETT2_MUX_TABLES 3
475
476/* Maximum number of entries in a mux table */
4be47798 477#define SCARLETT2_MAX_MUX_ENTRIES 10
6522c364
GB
478
479/* One entry within mux_assignment defines the port type and range of
480 * ports to add to the set_mux message. The end of the list is marked
481 * with count == 0.
482 */
483struct scarlett2_mux_entry {
484 u8 port_type;
485 u8 start;
486 u8 count;
487};
488
3473185f
GB
489/* Maximum number of entries in a mux table */
490#define SCARLETT2_MAX_METER_ENTRIES 9
491
492/* One entry within meter_assignment defines the range of mux outputs
493 * that consecutive meter entries are mapped to. The end of the list
494 * is marked with count == 0.
495 */
496struct scarlett2_meter_entry {
497 u8 start;
498 u8 count;
499};
500
9e4d5c1b 501struct scarlett2_device_info {
441d1e10 502 /* which set of configuration parameters the device uses */
cbd6f148 503 const struct scarlett2_config_set *config_set;
2fa96277 504
904e6da1
GB
505 /* line out hw volume is sw controlled */
506 u8 line_out_hw_vol;
507
e914d843
GB
508 /* support for main/alt speaker switching */
509 u8 has_speaker_switching;
510
d5bda7e0
GB
511 /* support for talkback microphone */
512 u8 has_talkback;
513
904e6da1
GB
514 /* the number of analogue inputs with a software switchable
515 * level control that can be set to line or instrument
516 */
517 u8 level_input_count;
518
2fa96277
GB
519 /* the first input with a level control (0-based) */
520 u8 level_input_first;
521
904e6da1
GB
522 /* the number of analogue inputs with a software switchable
523 * 10dB pad control
524 */
525 u8 pad_input_count;
526
dbbd4f9e
GB
527 /* the number of analogue inputs with a software switchable
528 * "air" control
529 */
530 u8 air_input_count;
531
ae58a1a1
GB
532 /* the number of phantom (48V) software switchable controls */
533 u8 phantom_count;
534
535 /* the number of inputs each phantom switch controls */
536 u8 inputs_per_phantom;
537
6ef9fa4a
GB
538 /* the number of direct monitor options
539 * (0 = none, 1 = mono only, 2 = mono/stereo)
540 */
541 u8 direct_monitor;
542
9cfe1276
GB
543 /* remap analogue outputs; 18i8 Gen 3 has "line 3/4" connected
544 * internally to the analogue 7/8 outputs
545 */
546 u8 line_out_remap_enable;
547 u8 line_out_remap[SCARLETT2_ANALOGUE_MAX];
3473185f 548 u8 line_out_unmap[SCARLETT2_ANALOGUE_MAX];
9cfe1276 549
904e6da1 550 /* additional description for the line out volume controls */
9e4d5c1b 551 const char * const line_out_descrs[SCARLETT2_ANALOGUE_MAX];
904e6da1 552
e2cc91ac
GB
553 /* number of sources/destinations of each port type */
554 const int port_count[SCARLETT2_PORT_TYPE_COUNT][SCARLETT2_PORT_DIRNS];
6522c364
GB
555
556 /* layout/order of the entries in the set_mux message */
557 struct scarlett2_mux_entry mux_assignment[SCARLETT2_MUX_TABLES]
558 [SCARLETT2_MAX_MUX_ENTRIES];
3473185f
GB
559
560 /* map from meter level order returned by
561 * SCARLETT2_USB_GET_METER to index into mux[] entries (same
562 * as the order returned by scarlett2_meter_ctl_get())
563 */
564 struct scarlett2_meter_entry meter_map[SCARLETT2_MAX_METER_ENTRIES];
9e4d5c1b
GB
565};
566
e46f2195 567struct scarlett2_data {
9e4d5c1b
GB
568 struct usb_mixer_interface *mixer;
569 struct mutex usb_mutex; /* prevent sending concurrent USB requests */
570 struct mutex data_mutex; /* lock access to this data */
6a7508e6
GB
571 u8 hwdep_in_use;
572 u8 selected_flash_segment_id;
573 u8 flash_write_state;
9e4d5c1b
GB
574 struct delayed_work work;
575 const struct scarlett2_device_info *info;
cbd6f148 576 const struct scarlett2_config_set *config_set;
6e743781 577 const char *series_name;
6c0a2078
GB
578 __u8 bInterfaceNumber;
579 __u8 bEndpointAddress;
580 __u16 wMaxPacketSize;
581 __u8 bInterval;
9e4d5c1b 582 int num_mux_srcs;
d6f9afe9 583 int num_mux_dsts;
701949cc 584 u32 firmware_version;
34101a0f
GB
585 u8 flash_segment_nums[SCARLETT2_SEGMENT_ID_COUNT];
586 u8 flash_segment_blocks[SCARLETT2_SEGMENT_ID_COUNT];
9e4d5c1b 587 u16 scarlett2_seq;
f3c61043 588 u8 sync_updated;
9e4d5c1b 589 u8 vol_updated;
a5b36123 590 u8 input_other_updated;
6ef9fa4a 591 u8 monitor_other_updated;
8df25eb0 592 u8 mux_updated;
e914d843 593 u8 speaker_switching_switched;
f3c61043 594 u8 sync;
9e4d5c1b
GB
595 u8 master_vol;
596 u8 vol[SCARLETT2_ANALOGUE_MAX];
597 u8 vol_sw_hw_switch[SCARLETT2_ANALOGUE_MAX];
0c88f9db 598 u8 mute_switch[SCARLETT2_ANALOGUE_MAX];
9e4d5c1b
GB
599 u8 level_switch[SCARLETT2_LEVEL_SWITCH_MAX];
600 u8 pad_switch[SCARLETT2_PAD_SWITCH_MAX];
dbd82c05 601 u8 dim_mute[SCARLETT2_DIM_MUTE_COUNT];
dbbd4f9e 602 u8 air_switch[SCARLETT2_AIR_SWITCH_MAX];
ae58a1a1
GB
603 u8 phantom_switch[SCARLETT2_PHANTOM_SWITCH_MAX];
604 u8 phantom_persistence;
6ef9fa4a 605 u8 direct_monitor_switch;
e914d843 606 u8 speaker_switching_switch;
d5bda7e0
GB
607 u8 talkback_switch;
608 u8 talkback_map[SCARLETT2_OUTPUT_MIX_MAX];
303f204e 609 u8 msd_switch;
604b3884 610 u8 standalone_switch;
3473185f 611 u8 meter_level_map[SCARLETT2_MAX_METERS];
f3c61043 612 struct snd_kcontrol *sync_ctl;
9e4d5c1b
GB
613 struct snd_kcontrol *master_vol_ctl;
614 struct snd_kcontrol *vol_ctls[SCARLETT2_ANALOGUE_MAX];
f02da653 615 struct snd_kcontrol *sw_hw_ctls[SCARLETT2_ANALOGUE_MAX];
0c88f9db 616 struct snd_kcontrol *mute_ctls[SCARLETT2_ANALOGUE_MAX];
dbd82c05 617 struct snd_kcontrol *dim_mute_ctls[SCARLETT2_DIM_MUTE_COUNT];
a5b36123
GB
618 struct snd_kcontrol *level_ctls[SCARLETT2_LEVEL_SWITCH_MAX];
619 struct snd_kcontrol *pad_ctls[SCARLETT2_PAD_SWITCH_MAX];
dbbd4f9e 620 struct snd_kcontrol *air_ctls[SCARLETT2_AIR_SWITCH_MAX];
ae58a1a1 621 struct snd_kcontrol *phantom_ctls[SCARLETT2_PHANTOM_SWITCH_MAX];
f02da653 622 struct snd_kcontrol *mux_ctls[SCARLETT2_MUX_MAX];
6ef9fa4a 623 struct snd_kcontrol *direct_monitor_ctl;
e914d843 624 struct snd_kcontrol *speaker_switching_ctl;
d5bda7e0 625 struct snd_kcontrol *talkback_ctl;
9e4d5c1b
GB
626 u8 mux[SCARLETT2_MUX_MAX];
627 u8 mix[SCARLETT2_INPUT_MIX_MAX * SCARLETT2_OUTPUT_MIX_MAX];
628};
629
630/*** Model-specific data ***/
631
632static const struct scarlett2_device_info s6i6_gen2_info = {
cbd6f148 633 .config_set = &scarlett2_config_set_gen2,
9e4d5c1b 634 .level_input_count = 2,
9e4d5c1b
GB
635 .pad_input_count = 2,
636
637 .line_out_descrs = {
c712c6c0
GB
638 "Headphones 1 L",
639 "Headphones 1 R",
640 "Headphones 2 L",
641 "Headphones 2 R",
9e4d5c1b
GB
642 },
643
e2cc91ac
GB
644 .port_count = {
645 [SCARLETT2_PORT_TYPE_NONE] = { 1, 0 },
646 [SCARLETT2_PORT_TYPE_ANALOGUE] = { 4, 4 },
647 [SCARLETT2_PORT_TYPE_SPDIF] = { 2, 2 },
648 [SCARLETT2_PORT_TYPE_MIX] = { 10, 18 },
649 [SCARLETT2_PORT_TYPE_PCM] = { 6, 6 },
9e4d5c1b 650 },
6522c364
GB
651
652 .mux_assignment = { {
653 { SCARLETT2_PORT_TYPE_PCM, 0, 6 },
654 { SCARLETT2_PORT_TYPE_ANALOGUE, 0, 4 },
655 { SCARLETT2_PORT_TYPE_SPDIF, 0, 2 },
656 { SCARLETT2_PORT_TYPE_MIX, 0, 18 },
657 { SCARLETT2_PORT_TYPE_NONE, 0, 8 },
658 { 0, 0, 0 },
659 }, {
660 { SCARLETT2_PORT_TYPE_PCM, 0, 6 },
661 { SCARLETT2_PORT_TYPE_ANALOGUE, 0, 4 },
662 { SCARLETT2_PORT_TYPE_SPDIF, 0, 2 },
663 { SCARLETT2_PORT_TYPE_MIX, 0, 18 },
664 { SCARLETT2_PORT_TYPE_NONE, 0, 8 },
665 { 0, 0, 0 },
666 }, {
667 { SCARLETT2_PORT_TYPE_PCM, 0, 6 },
668 { SCARLETT2_PORT_TYPE_ANALOGUE, 0, 4 },
669 { SCARLETT2_PORT_TYPE_SPDIF, 0, 2 },
670 { SCARLETT2_PORT_TYPE_MIX, 0, 18 },
671 { SCARLETT2_PORT_TYPE_NONE, 0, 8 },
672 { 0, 0, 0 },
673 } },
3473185f
GB
674
675 .meter_map = {
676 { 24, 6 },
677 { 0, 24 },
678 { 0, 0 },
679 }
9e4d5c1b
GB
680};
681
682static const struct scarlett2_device_info s18i8_gen2_info = {
cbd6f148 683 .config_set = &scarlett2_config_set_gen2,
9e4d5c1b 684 .level_input_count = 2,
9e4d5c1b
GB
685 .pad_input_count = 4,
686
687 .line_out_descrs = {
688 "Monitor L",
689 "Monitor R",
690 "Headphones 1 L",
691 "Headphones 1 R",
692 "Headphones 2 L",
693 "Headphones 2 R",
694 },
695
e2cc91ac
GB
696 .port_count = {
697 [SCARLETT2_PORT_TYPE_NONE] = { 1, 0 },
698 [SCARLETT2_PORT_TYPE_ANALOGUE] = { 8, 6 },
699 [SCARLETT2_PORT_TYPE_SPDIF] = { 2, 2 },
700 [SCARLETT2_PORT_TYPE_ADAT] = { 8, 0 },
701 [SCARLETT2_PORT_TYPE_MIX] = { 10, 18 },
702 [SCARLETT2_PORT_TYPE_PCM] = { 8, 18 },
9e4d5c1b 703 },
6522c364
GB
704
705 .mux_assignment = { {
706 { SCARLETT2_PORT_TYPE_PCM, 0, 18 },
707 { SCARLETT2_PORT_TYPE_ANALOGUE, 0, 6 },
708 { SCARLETT2_PORT_TYPE_SPDIF, 0, 2 },
709 { SCARLETT2_PORT_TYPE_MIX, 0, 18 },
710 { SCARLETT2_PORT_TYPE_NONE, 0, 8 },
711 { 0, 0, 0 },
712 }, {
713 { SCARLETT2_PORT_TYPE_PCM, 0, 14 },
714 { SCARLETT2_PORT_TYPE_ANALOGUE, 0, 6 },
715 { SCARLETT2_PORT_TYPE_SPDIF, 0, 2 },
716 { SCARLETT2_PORT_TYPE_MIX, 0, 18 },
717 { SCARLETT2_PORT_TYPE_NONE, 0, 8 },
718 { 0, 0, 0 },
719 }, {
720 { SCARLETT2_PORT_TYPE_PCM, 0, 10 },
721 { SCARLETT2_PORT_TYPE_ANALOGUE, 0, 6 },
722 { SCARLETT2_PORT_TYPE_SPDIF, 0, 2 },
723 { SCARLETT2_PORT_TYPE_MIX, 0, 18 },
724 { SCARLETT2_PORT_TYPE_NONE, 0, 4 },
725 { 0, 0, 0 },
726 } },
3473185f
GB
727
728 .meter_map = {
729 { 26, 18 },
730 { 0, 26 },
731 { 0, 0 },
732 }
9e4d5c1b
GB
733};
734
735static const struct scarlett2_device_info s18i20_gen2_info = {
cbd6f148 736 .config_set = &scarlett2_config_set_gen2,
9e4d5c1b
GB
737 .line_out_hw_vol = 1,
738
9e4d5c1b
GB
739 .line_out_descrs = {
740 "Monitor L",
741 "Monitor R",
742 NULL,
743 NULL,
744 NULL,
745 NULL,
746 "Headphones 1 L",
747 "Headphones 1 R",
748 "Headphones 2 L",
749 "Headphones 2 R",
750 },
751
e2cc91ac
GB
752 .port_count = {
753 [SCARLETT2_PORT_TYPE_NONE] = { 1, 0 },
754 [SCARLETT2_PORT_TYPE_ANALOGUE] = { 8, 10 },
755 [SCARLETT2_PORT_TYPE_SPDIF] = { 2, 2 },
756 [SCARLETT2_PORT_TYPE_ADAT] = { 8, 8 },
757 [SCARLETT2_PORT_TYPE_MIX] = { 10, 18 },
758 [SCARLETT2_PORT_TYPE_PCM] = { 20, 18 },
9e4d5c1b 759 },
6522c364
GB
760
761 .mux_assignment = { {
762 { SCARLETT2_PORT_TYPE_PCM, 0, 18 },
763 { SCARLETT2_PORT_TYPE_ANALOGUE, 0, 10 },
764 { SCARLETT2_PORT_TYPE_SPDIF, 0, 2 },
765 { SCARLETT2_PORT_TYPE_ADAT, 0, 8 },
766 { SCARLETT2_PORT_TYPE_MIX, 0, 18 },
767 { SCARLETT2_PORT_TYPE_NONE, 0, 8 },
768 { 0, 0, 0 },
769 }, {
770 { SCARLETT2_PORT_TYPE_PCM, 0, 14 },
771 { SCARLETT2_PORT_TYPE_ANALOGUE, 0, 10 },
772 { SCARLETT2_PORT_TYPE_SPDIF, 0, 2 },
773 { SCARLETT2_PORT_TYPE_ADAT, 0, 4 },
774 { SCARLETT2_PORT_TYPE_MIX, 0, 18 },
775 { SCARLETT2_PORT_TYPE_NONE, 0, 8 },
776 { 0, 0, 0 },
777 }, {
778 { SCARLETT2_PORT_TYPE_PCM, 0, 10 },
779 { SCARLETT2_PORT_TYPE_ANALOGUE, 0, 10 },
780 { SCARLETT2_PORT_TYPE_SPDIF, 0, 2 },
781 { SCARLETT2_PORT_TYPE_MIX, 0, 18 },
782 { SCARLETT2_PORT_TYPE_NONE, 0, 6 },
783 { 0, 0, 0 },
784 } },
3473185f
GB
785
786 .meter_map = {
787 { 38, 18 },
788 { 0, 38 },
789 { 0, 0 },
790 }
9e4d5c1b
GB
791};
792
2fa96277 793static const struct scarlett2_device_info solo_gen3_info = {
cbd6f148 794 .config_set = &scarlett2_config_set_gen3a,
2fa96277
GB
795 .level_input_count = 1,
796 .level_input_first = 1,
dbbd4f9e 797 .air_input_count = 1,
ae58a1a1
GB
798 .phantom_count = 1,
799 .inputs_per_phantom = 1,
6ef9fa4a 800 .direct_monitor = 1,
2fa96277
GB
801};
802
803static const struct scarlett2_device_info s2i2_gen3_info = {
cbd6f148 804 .config_set = &scarlett2_config_set_gen3a,
2fa96277 805 .level_input_count = 2,
dbbd4f9e 806 .air_input_count = 2,
ae58a1a1
GB
807 .phantom_count = 1,
808 .inputs_per_phantom = 2,
6ef9fa4a 809 .direct_monitor = 2,
2fa96277
GB
810};
811
4be47798 812static const struct scarlett2_device_info s4i4_gen3_info = {
cbd6f148 813 .config_set = &scarlett2_config_set_gen3b,
4be47798
GB
814 .level_input_count = 2,
815 .pad_input_count = 2,
dbbd4f9e 816 .air_input_count = 2,
ae58a1a1
GB
817 .phantom_count = 1,
818 .inputs_per_phantom = 2,
4be47798
GB
819
820 .line_out_descrs = {
821 "Monitor L",
822 "Monitor R",
823 "Headphones L",
824 "Headphones R",
825 },
826
827 .port_count = {
828 [SCARLETT2_PORT_TYPE_NONE] = { 1, 0 },
829 [SCARLETT2_PORT_TYPE_ANALOGUE] = { 4, 4 },
830 [SCARLETT2_PORT_TYPE_MIX] = { 6, 8 },
831 [SCARLETT2_PORT_TYPE_PCM] = { 4, 6 },
832 },
833
834 .mux_assignment = { {
835 { SCARLETT2_PORT_TYPE_PCM, 0, 6 },
836 { SCARLETT2_PORT_TYPE_ANALOGUE, 0, 4 },
837 { SCARLETT2_PORT_TYPE_MIX, 0, 8 },
838 { SCARLETT2_PORT_TYPE_NONE, 0, 16 },
839 { 0, 0, 0 },
840 }, {
841 { SCARLETT2_PORT_TYPE_PCM, 0, 6 },
842 { SCARLETT2_PORT_TYPE_ANALOGUE, 0, 4 },
843 { SCARLETT2_PORT_TYPE_MIX, 0, 8 },
844 { SCARLETT2_PORT_TYPE_NONE, 0, 16 },
845 { 0, 0, 0 },
846 }, {
847 { SCARLETT2_PORT_TYPE_PCM, 0, 6 },
848 { SCARLETT2_PORT_TYPE_ANALOGUE, 0, 4 },
849 { SCARLETT2_PORT_TYPE_MIX, 0, 8 },
850 { SCARLETT2_PORT_TYPE_NONE, 0, 16 },
851 { 0, 0, 0 },
852 } },
3473185f
GB
853
854 .meter_map = {
855 { 12, 6 },
856 { 0, 12 },
857 { 0, 0 },
858 }
4be47798
GB
859};
860
861static const struct scarlett2_device_info s8i6_gen3_info = {
cbd6f148 862 .config_set = &scarlett2_config_set_gen3b,
4be47798
GB
863 .level_input_count = 2,
864 .pad_input_count = 2,
dbbd4f9e 865 .air_input_count = 2,
ae58a1a1
GB
866 .phantom_count = 1,
867 .inputs_per_phantom = 2,
4be47798
GB
868
869 .line_out_descrs = {
870 "Headphones 1 L",
871 "Headphones 1 R",
872 "Headphones 2 L",
873 "Headphones 2 R",
874 },
875
876 .port_count = {
877 [SCARLETT2_PORT_TYPE_NONE] = { 1, 0 },
878 [SCARLETT2_PORT_TYPE_ANALOGUE] = { 6, 4 },
879 [SCARLETT2_PORT_TYPE_SPDIF] = { 2, 2 },
880 [SCARLETT2_PORT_TYPE_MIX] = { 8, 8 },
881 [SCARLETT2_PORT_TYPE_PCM] = { 6, 10 },
882 },
883
884 .mux_assignment = { {
885 { SCARLETT2_PORT_TYPE_PCM, 0, 8 },
886 { SCARLETT2_PORT_TYPE_ANALOGUE, 0, 4 },
887 { SCARLETT2_PORT_TYPE_SPDIF, 0, 2 },
888 { SCARLETT2_PORT_TYPE_PCM, 8, 2 },
889 { SCARLETT2_PORT_TYPE_MIX, 0, 8 },
890 { SCARLETT2_PORT_TYPE_NONE, 0, 18 },
891 { 0, 0, 0 },
892 }, {
893 { SCARLETT2_PORT_TYPE_PCM, 0, 8 },
894 { SCARLETT2_PORT_TYPE_ANALOGUE, 0, 4 },
895 { SCARLETT2_PORT_TYPE_SPDIF, 0, 2 },
896 { SCARLETT2_PORT_TYPE_PCM, 8, 2 },
897 { SCARLETT2_PORT_TYPE_MIX, 0, 8 },
898 { SCARLETT2_PORT_TYPE_NONE, 0, 18 },
899 { 0, 0, 0 },
900 }, {
901 { SCARLETT2_PORT_TYPE_PCM, 0, 8 },
902 { SCARLETT2_PORT_TYPE_ANALOGUE, 0, 4 },
903 { SCARLETT2_PORT_TYPE_SPDIF, 0, 2 },
904 { SCARLETT2_PORT_TYPE_PCM, 8, 2 },
905 { SCARLETT2_PORT_TYPE_MIX, 0, 8 },
906 { SCARLETT2_PORT_TYPE_NONE, 0, 18 },
907 { 0, 0, 0 },
908 } },
3473185f
GB
909
910 .meter_map = {
911 { 14, 8 },
912 { 0, 6 },
913 { 22, 2 },
914 { 6, 8 },
915 { 0, 0 },
916 }
4be47798
GB
917};
918
919static const struct scarlett2_device_info s18i8_gen3_info = {
cbd6f148 920 .config_set = &scarlett2_config_set_gen3b,
4be47798 921 .line_out_hw_vol = 1,
e914d843 922 .has_speaker_switching = 1,
4be47798 923 .level_input_count = 2,
fe9a23a6 924 .pad_input_count = 4,
dbbd4f9e 925 .air_input_count = 4,
ae58a1a1
GB
926 .phantom_count = 2,
927 .inputs_per_phantom = 2,
4be47798 928
9cfe1276
GB
929 .line_out_remap_enable = 1,
930 .line_out_remap = { 0, 1, 6, 7, 2, 3, 4, 5 },
3473185f 931 .line_out_unmap = { 0, 1, 4, 5, 6, 7, 2, 3 },
9cfe1276 932
4be47798
GB
933 .line_out_descrs = {
934 "Monitor L",
935 "Monitor R",
9cfe1276
GB
936 "Alt Monitor L",
937 "Alt Monitor R",
4be47798
GB
938 "Headphones 1 L",
939 "Headphones 1 R",
940 "Headphones 2 L",
941 "Headphones 2 R",
4be47798
GB
942 },
943
944 .port_count = {
945 [SCARLETT2_PORT_TYPE_NONE] = { 1, 0 },
946 [SCARLETT2_PORT_TYPE_ANALOGUE] = { 8, 8 },
947 [SCARLETT2_PORT_TYPE_SPDIF] = { 2, 2 },
948 [SCARLETT2_PORT_TYPE_ADAT] = { 8, 0 },
949 [SCARLETT2_PORT_TYPE_MIX] = { 10, 20 },
950 [SCARLETT2_PORT_TYPE_PCM] = { 8, 20 },
951 },
952
953 .mux_assignment = { {
954 { SCARLETT2_PORT_TYPE_PCM, 0, 10 },
955 { SCARLETT2_PORT_TYPE_PCM, 12, 8 },
956 { SCARLETT2_PORT_TYPE_ANALOGUE, 0, 2 },
957 { SCARLETT2_PORT_TYPE_ANALOGUE, 6, 2 },
958 { SCARLETT2_PORT_TYPE_ANALOGUE, 2, 4 },
959 { SCARLETT2_PORT_TYPE_SPDIF, 0, 2 },
960 { SCARLETT2_PORT_TYPE_PCM, 10, 2 },
961 { SCARLETT2_PORT_TYPE_MIX, 0, 20 },
962 { SCARLETT2_PORT_TYPE_NONE, 0, 10 },
963 { 0, 0, 0 },
964 }, {
965 { SCARLETT2_PORT_TYPE_PCM, 0, 10 },
966 { SCARLETT2_PORT_TYPE_PCM, 12, 4 },
967 { SCARLETT2_PORT_TYPE_ANALOGUE, 0, 2 },
968 { SCARLETT2_PORT_TYPE_ANALOGUE, 6, 2 },
969 { SCARLETT2_PORT_TYPE_ANALOGUE, 2, 4 },
970 { SCARLETT2_PORT_TYPE_SPDIF, 0, 2 },
971 { SCARLETT2_PORT_TYPE_PCM, 10, 2 },
972 { SCARLETT2_PORT_TYPE_MIX, 0, 20 },
973 { SCARLETT2_PORT_TYPE_NONE, 0, 10 },
974 { 0, 0, 0 },
975 }, {
976 { SCARLETT2_PORT_TYPE_PCM, 0, 10 },
977 { SCARLETT2_PORT_TYPE_ANALOGUE, 0, 2 },
978 { SCARLETT2_PORT_TYPE_ANALOGUE, 6, 2 },
979 { SCARLETT2_PORT_TYPE_ANALOGUE, 2, 4 },
980 { SCARLETT2_PORT_TYPE_SPDIF, 0, 2 },
981 { SCARLETT2_PORT_TYPE_MIX, 0, 20 },
982 { SCARLETT2_PORT_TYPE_NONE, 0, 10 },
983 { 0, 0, 0 },
984 } },
3473185f
GB
985
986 .meter_map = {
987 { 30, 10 },
988 { 42, 8 },
989 { 0, 2 },
990 { 6, 2 },
991 { 2, 4 },
992 { 8, 2 },
993 { 40, 2 },
994 { 10, 20 },
995 { 0, 0 }
996 }
4be47798
GB
997};
998
999static const struct scarlett2_device_info s18i20_gen3_info = {
cbd6f148 1000 .config_set = &scarlett2_config_set_gen3b,
4be47798 1001 .line_out_hw_vol = 1,
e914d843 1002 .has_speaker_switching = 1,
d5bda7e0 1003 .has_talkback = 1,
4be47798
GB
1004 .level_input_count = 2,
1005 .pad_input_count = 8,
dbbd4f9e 1006 .air_input_count = 8,
ae58a1a1
GB
1007 .phantom_count = 2,
1008 .inputs_per_phantom = 4,
4be47798
GB
1009
1010 .line_out_descrs = {
1011 "Monitor 1 L",
1012 "Monitor 1 R",
1013 "Monitor 2 L",
1014 "Monitor 2 R",
1015 NULL,
1016 NULL,
1017 "Headphones 1 L",
1018 "Headphones 1 R",
1019 "Headphones 2 L",
1020 "Headphones 2 R",
1021 },
1022
1023 .port_count = {
1024 [SCARLETT2_PORT_TYPE_NONE] = { 1, 0 },
1025 [SCARLETT2_PORT_TYPE_ANALOGUE] = { 9, 10 },
1026 [SCARLETT2_PORT_TYPE_SPDIF] = { 2, 2 },
1027 [SCARLETT2_PORT_TYPE_ADAT] = { 8, 8 },
1028 [SCARLETT2_PORT_TYPE_MIX] = { 12, 25 },
1029 [SCARLETT2_PORT_TYPE_PCM] = { 20, 20 },
1030 },
1031
1032 .mux_assignment = { {
1033 { SCARLETT2_PORT_TYPE_PCM, 0, 8 },
1034 { SCARLETT2_PORT_TYPE_PCM, 10, 10 },
1035 { SCARLETT2_PORT_TYPE_ANALOGUE, 0, 10 },
1036 { SCARLETT2_PORT_TYPE_SPDIF, 0, 2 },
1037 { SCARLETT2_PORT_TYPE_ADAT, 0, 8 },
1038 { SCARLETT2_PORT_TYPE_PCM, 8, 2 },
1039 { SCARLETT2_PORT_TYPE_MIX, 0, 25 },
1040 { SCARLETT2_PORT_TYPE_NONE, 0, 12 },
1041 { 0, 0, 0 },
1042 }, {
1043 { SCARLETT2_PORT_TYPE_PCM, 0, 8 },
1044 { SCARLETT2_PORT_TYPE_PCM, 10, 8 },
1045 { SCARLETT2_PORT_TYPE_ANALOGUE, 0, 10 },
1046 { SCARLETT2_PORT_TYPE_SPDIF, 0, 2 },
1047 { SCARLETT2_PORT_TYPE_ADAT, 0, 8 },
1048 { SCARLETT2_PORT_TYPE_PCM, 8, 2 },
1049 { SCARLETT2_PORT_TYPE_MIX, 0, 25 },
1050 { SCARLETT2_PORT_TYPE_NONE, 0, 10 },
1051 { 0, 0, 0 },
1052 }, {
1053 { SCARLETT2_PORT_TYPE_PCM, 0, 10 },
1054 { SCARLETT2_PORT_TYPE_ANALOGUE, 0, 10 },
1055 { SCARLETT2_PORT_TYPE_SPDIF, 0, 2 },
1056 { SCARLETT2_PORT_TYPE_NONE, 0, 24 },
1057 { 0, 0, 0 },
1058 } },
3473185f
GB
1059
1060 .meter_map = {
1061 { 45, 8 },
1062 { 55, 10 },
1063 { 0, 20 },
1064 { 53, 2 },
1065 { 20, 25 },
1066 { 0, 0 },
1067 }
4be47798
GB
1068};
1069
b61a3aca 1070static const struct scarlett2_device_info clarett_2pre_info = {
cbd6f148 1071 .config_set = &scarlett2_config_set_clarett,
b61a3aca
GB
1072 .line_out_hw_vol = 1,
1073 .level_input_count = 2,
1074 .air_input_count = 2,
1075
1076 .line_out_descrs = {
1077 "Monitor L",
1078 "Monitor R",
1079 "Headphones L",
1080 "Headphones R",
1081 },
1082
1083 .port_count = {
1084 [SCARLETT2_PORT_TYPE_NONE] = { 1, 0 },
1085 [SCARLETT2_PORT_TYPE_ANALOGUE] = { 2, 4 },
1086 [SCARLETT2_PORT_TYPE_SPDIF] = { 2, 0 },
1087 [SCARLETT2_PORT_TYPE_ADAT] = { 8, 0 },
1088 [SCARLETT2_PORT_TYPE_MIX] = { 10, 18 },
1089 [SCARLETT2_PORT_TYPE_PCM] = { 4, 12 },
1090 },
1091
1092 .mux_assignment = { {
1093 { SCARLETT2_PORT_TYPE_PCM, 0, 12 },
1094 { SCARLETT2_PORT_TYPE_ANALOGUE, 0, 4 },
1095 { SCARLETT2_PORT_TYPE_MIX, 0, 18 },
1096 { SCARLETT2_PORT_TYPE_NONE, 0, 8 },
1097 { 0, 0, 0 },
1098 }, {
1099 { SCARLETT2_PORT_TYPE_PCM, 0, 8 },
1100 { SCARLETT2_PORT_TYPE_ANALOGUE, 0, 4 },
1101 { SCARLETT2_PORT_TYPE_MIX, 0, 18 },
1102 { SCARLETT2_PORT_TYPE_NONE, 0, 8 },
1103 { 0, 0, 0 },
1104 }, {
1105 { SCARLETT2_PORT_TYPE_PCM, 0, 2 },
1106 { SCARLETT2_PORT_TYPE_ANALOGUE, 0, 4 },
1107 { SCARLETT2_PORT_TYPE_NONE, 0, 26 },
1108 { 0, 0, 0 },
1109 } },
3473185f
GB
1110
1111 .meter_map = {
1112 { 22, 12 },
1113 { 0, 22 },
1114 { 0, 0 }
1115 }
b61a3aca
GB
1116};
1117
1118static const struct scarlett2_device_info clarett_4pre_info = {
cbd6f148 1119 .config_set = &scarlett2_config_set_clarett,
b61a3aca
GB
1120 .line_out_hw_vol = 1,
1121 .level_input_count = 2,
1122 .air_input_count = 4,
1123
1124 .line_out_descrs = {
1125 "Monitor L",
1126 "Monitor R",
1127 "Headphones 1 L",
1128 "Headphones 1 R",
1129 "Headphones 2 L",
1130 "Headphones 2 R",
1131 },
1132
1133 .port_count = {
1134 [SCARLETT2_PORT_TYPE_NONE] = { 1, 0 },
1135 [SCARLETT2_PORT_TYPE_ANALOGUE] = { 8, 6 },
1136 [SCARLETT2_PORT_TYPE_SPDIF] = { 2, 2 },
1137 [SCARLETT2_PORT_TYPE_ADAT] = { 8, 0 },
1138 [SCARLETT2_PORT_TYPE_MIX] = { 10, 18 },
1139 [SCARLETT2_PORT_TYPE_PCM] = { 8, 18 },
1140 },
1141
1142 .mux_assignment = { {
1143 { SCARLETT2_PORT_TYPE_PCM, 0, 18 },
1144 { SCARLETT2_PORT_TYPE_ANALOGUE, 0, 6 },
1145 { SCARLETT2_PORT_TYPE_SPDIF, 0, 2 },
1146 { SCARLETT2_PORT_TYPE_MIX, 0, 18 },
1147 { SCARLETT2_PORT_TYPE_NONE, 0, 8 },
1148 { 0, 0, 0 },
1149 }, {
1150 { SCARLETT2_PORT_TYPE_PCM, 0, 14 },
1151 { SCARLETT2_PORT_TYPE_ANALOGUE, 0, 6 },
1152 { SCARLETT2_PORT_TYPE_SPDIF, 0, 2 },
1153 { SCARLETT2_PORT_TYPE_MIX, 0, 18 },
1154 { SCARLETT2_PORT_TYPE_NONE, 0, 8 },
1155 { 0, 0, 0 },
1156 }, {
1157 { SCARLETT2_PORT_TYPE_PCM, 0, 12 },
1158 { SCARLETT2_PORT_TYPE_ANALOGUE, 0, 6 },
1159 { SCARLETT2_PORT_TYPE_SPDIF, 0, 2 },
1160 { SCARLETT2_PORT_TYPE_NONE, 0, 24 },
1161 { 0, 0, 0 },
1162 } },
3473185f
GB
1163
1164 .meter_map = {
1165 { 26, 18 },
1166 { 0, 26 },
1167 { 0, 0 }
1168 }
b61a3aca
GB
1169};
1170
f71c70df 1171static const struct scarlett2_device_info clarett_8pre_info = {
cbd6f148 1172 .config_set = &scarlett2_config_set_clarett,
f71c70df
CC
1173 .line_out_hw_vol = 1,
1174 .level_input_count = 2,
1175 .air_input_count = 8,
1176
1177 .line_out_descrs = {
1178 "Monitor L",
1179 "Monitor R",
1180 NULL,
1181 NULL,
1182 NULL,
1183 NULL,
1184 "Headphones 1 L",
1185 "Headphones 1 R",
1186 "Headphones 2 L",
1187 "Headphones 2 R",
1188 },
1189
1190 .port_count = {
1191 [SCARLETT2_PORT_TYPE_NONE] = { 1, 0 },
1192 [SCARLETT2_PORT_TYPE_ANALOGUE] = { 8, 10 },
1193 [SCARLETT2_PORT_TYPE_SPDIF] = { 2, 2 },
1194 [SCARLETT2_PORT_TYPE_ADAT] = { 8, 8 },
1195 [SCARLETT2_PORT_TYPE_MIX] = { 10, 18 },
1196 [SCARLETT2_PORT_TYPE_PCM] = { 20, 18 },
1197 },
1198
1199 .mux_assignment = { {
1200 { SCARLETT2_PORT_TYPE_PCM, 0, 18 },
1201 { SCARLETT2_PORT_TYPE_ANALOGUE, 0, 10 },
1202 { SCARLETT2_PORT_TYPE_SPDIF, 0, 2 },
1203 { SCARLETT2_PORT_TYPE_ADAT, 0, 8 },
1204 { SCARLETT2_PORT_TYPE_MIX, 0, 18 },
1205 { SCARLETT2_PORT_TYPE_NONE, 0, 8 },
1206 { 0, 0, 0 },
1207 }, {
1208 { SCARLETT2_PORT_TYPE_PCM, 0, 14 },
1209 { SCARLETT2_PORT_TYPE_ANALOGUE, 0, 10 },
1210 { SCARLETT2_PORT_TYPE_SPDIF, 0, 2 },
1211 { SCARLETT2_PORT_TYPE_ADAT, 0, 4 },
1212 { SCARLETT2_PORT_TYPE_MIX, 0, 18 },
1213 { SCARLETT2_PORT_TYPE_NONE, 0, 8 },
1214 { 0, 0, 0 },
1215 }, {
1216 { SCARLETT2_PORT_TYPE_PCM, 0, 12 },
1217 { SCARLETT2_PORT_TYPE_ANALOGUE, 0, 10 },
1218 { SCARLETT2_PORT_TYPE_SPDIF, 0, 2 },
1219 { SCARLETT2_PORT_TYPE_NONE, 0, 22 },
1220 { 0, 0, 0 },
1221 } },
3473185f
GB
1222
1223 .meter_map = {
1224 { 38, 18 },
1225 { 0, 38 },
1226 { 0, 0 }
1227 }
f71c70df
CC
1228};
1229
d98cc489
GB
1230struct scarlett2_device_entry {
1231 const u32 usb_id; /* USB device identifier */
1232 const struct scarlett2_device_info *info;
6e743781 1233 const char *series_name;
d98cc489
GB
1234};
1235
1236static const struct scarlett2_device_entry scarlett2_devices[] = {
d92b9157 1237 /* Supported Gen 2 devices */
6e743781
GB
1238 { USB_ID(0x1235, 0x8203), &s6i6_gen2_info, "Scarlett Gen 2" },
1239 { USB_ID(0x1235, 0x8204), &s18i8_gen2_info, "Scarlett Gen 2" },
1240 { USB_ID(0x1235, 0x8201), &s18i20_gen2_info, "Scarlett Gen 2" },
d92b9157 1241
4be47798 1242 /* Supported Gen 3 devices */
6e743781
GB
1243 { USB_ID(0x1235, 0x8211), &solo_gen3_info, "Scarlett Gen 3" },
1244 { USB_ID(0x1235, 0x8210), &s2i2_gen3_info, "Scarlett Gen 3" },
1245 { USB_ID(0x1235, 0x8212), &s4i4_gen3_info, "Scarlett Gen 3" },
1246 { USB_ID(0x1235, 0x8213), &s8i6_gen3_info, "Scarlett Gen 3" },
1247 { USB_ID(0x1235, 0x8214), &s18i8_gen3_info, "Scarlett Gen 3" },
1248 { USB_ID(0x1235, 0x8215), &s18i20_gen3_info, "Scarlett Gen 3" },
4be47798 1249
b9a98cdd 1250 /* Supported Clarett USB/Clarett+ devices */
2b17b489
GB
1251 { USB_ID(0x1235, 0x8206), &clarett_2pre_info, "Clarett USB" },
1252 { USB_ID(0x1235, 0x8207), &clarett_4pre_info, "Clarett USB" },
6e743781 1253 { USB_ID(0x1235, 0x8208), &clarett_8pre_info, "Clarett USB" },
b61a3aca
GB
1254 { USB_ID(0x1235, 0x820a), &clarett_2pre_info, "Clarett+" },
1255 { USB_ID(0x1235, 0x820b), &clarett_4pre_info, "Clarett+" },
6e743781 1256 { USB_ID(0x1235, 0x820c), &clarett_8pre_info, "Clarett+" },
f71c70df 1257
d92b9157 1258 /* End of list */
d98cc489 1259 { 0, NULL },
d92b9157
GB
1260};
1261
9e4d5c1b 1262/* get the starting port index number for a given port type/direction */
e2cc91ac
GB
1263static int scarlett2_get_port_start_num(
1264 const int port_count[][SCARLETT2_PORT_DIRNS],
1265 int direction, int port_type)
9e4d5c1b
GB
1266{
1267 int i, num = 0;
1268
1269 for (i = 0; i < port_type; i++)
e2cc91ac 1270 num += port_count[i][direction];
9e4d5c1b
GB
1271
1272 return num;
1273}
1274
1275/*** USB Interactions ***/
1276
f3c61043 1277/* Notifications from the interface */
6ef9fa4a
GB
1278#define SCARLETT2_USB_NOTIFY_SYNC 0x00000008
1279#define SCARLETT2_USB_NOTIFY_DIM_MUTE 0x00200000
1280#define SCARLETT2_USB_NOTIFY_MONITOR 0x00400000
1281#define SCARLETT2_USB_NOTIFY_INPUT_OTHER 0x00800000
1282#define SCARLETT2_USB_NOTIFY_MONITOR_OTHER 0x01000000
9e4d5c1b
GB
1283
1284/* Commands for sending/receiving requests/responses */
acf91b81 1285#define SCARLETT2_USB_CMD_INIT 0
1f7fa6e5
GB
1286#define SCARLETT2_USB_CMD_REQ 2
1287#define SCARLETT2_USB_CMD_RESP 3
9e4d5c1b 1288
103c23cc
GB
1289#define SCARLETT2_USB_INIT_1 0x00000000
1290#define SCARLETT2_USB_INIT_2 0x00000002
1291#define SCARLETT2_USB_REBOOT 0x00000003
1292#define SCARLETT2_USB_GET_METER 0x00001001
1293#define SCARLETT2_USB_GET_MIX 0x00002001
1294#define SCARLETT2_USB_SET_MIX 0x00002002
1295#define SCARLETT2_USB_GET_MUX 0x00003001
1296#define SCARLETT2_USB_SET_MUX 0x00003002
1297#define SCARLETT2_USB_INFO_FLASH 0x00004000
1298#define SCARLETT2_USB_INFO_SEGMENT 0x00004001
1299#define SCARLETT2_USB_ERASE_SEGMENT 0x00004002
1300#define SCARLETT2_USB_GET_ERASE 0x00004003
1301#define SCARLETT2_USB_WRITE_SEGMENT 0x00004004
1302#define SCARLETT2_USB_GET_SYNC 0x00006004
1303#define SCARLETT2_USB_GET_DATA 0x00800000
1304#define SCARLETT2_USB_SET_DATA 0x00800001
1305#define SCARLETT2_USB_DATA_CMD 0x00800002
06a21621 1306
9e4d5c1b
GB
1307#define SCARLETT2_USB_CONFIG_SAVE 6
1308
1309#define SCARLETT2_USB_VOLUME_STATUS_OFFSET 0x31
1310#define SCARLETT2_USB_METER_LEVELS_GET_MAGIC 1
1311
34101a0f 1312#define SCARLETT2_FLASH_BLOCK_SIZE 4096
1abfbd3c 1313#define SCARLETT2_FLASH_WRITE_MAX 1024
34101a0f
GB
1314#define SCARLETT2_SEGMENT_NUM_MIN 1
1315#define SCARLETT2_SEGMENT_NUM_MAX 4
1316
1317#define SCARLETT2_SEGMENT_SETTINGS_NAME "App_Settings"
1318#define SCARLETT2_SEGMENT_FIRMWARE_NAME "App_Upgrade"
1319
2fa96277 1320/* volume status is read together (matches scarlett2_config_items[1]) */
9e4d5c1b 1321struct scarlett2_usb_volume_status {
dbd82c05
GB
1322 /* dim/mute buttons */
1323 u8 dim_mute[SCARLETT2_DIM_MUTE_COUNT];
9e4d5c1b
GB
1324
1325 u8 pad1;
1326
1327 /* software volume setting */
1328 s16 sw_vol[SCARLETT2_ANALOGUE_MAX];
1329
1330 /* actual volume of output inc. dim (-18dB) */
1331 s16 hw_vol[SCARLETT2_ANALOGUE_MAX];
1332
0c88f9db
GB
1333 /* internal mute buttons */
1334 u8 mute_switch[SCARLETT2_ANALOGUE_MAX];
9e4d5c1b
GB
1335
1336 /* sw (0) or hw (1) controlled */
1337 u8 sw_hw_switch[SCARLETT2_ANALOGUE_MAX];
1338
1339 u8 pad3[6];
1340
1341 /* front panel volume knob */
1342 s16 master_vol;
1343} __packed;
1344
9e4d5c1b
GB
1345/* proprietary request/response format */
1346struct scarlett2_usb_packet {
d8f48935
TI
1347 __le32 cmd;
1348 __le16 size;
1349 __le16 seq;
1350 __le32 error;
1351 __le32 pad;
9e4d5c1b
GB
1352 u8 data[];
1353};
1354
e46f2195 1355static void scarlett2_fill_request_header(struct scarlett2_data *private,
9e4d5c1b
GB
1356 struct scarlett2_usb_packet *req,
1357 u32 cmd, u16 req_size)
1358{
1359 /* sequence must go up by 1 for each request */
1360 u16 seq = private->scarlett2_seq++;
1361
1362 req->cmd = cpu_to_le32(cmd);
1363 req->size = cpu_to_le16(req_size);
1364 req->seq = cpu_to_le16(seq);
1365 req->error = 0;
1366 req->pad = 0;
1367}
1368
1f7fa6e5
GB
1369static int scarlett2_usb_tx(struct usb_device *dev, int interface,
1370 void *buf, u16 size)
1371{
1372 return snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0),
1373 SCARLETT2_USB_CMD_REQ,
1374 USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_OUT,
1375 0, interface, buf, size);
1376}
1377
1378static int scarlett2_usb_rx(struct usb_device *dev, int interface,
1379 u32 usb_req, void *buf, u16 size)
1380{
1381 return snd_usb_ctl_msg(dev, usb_rcvctrlpipe(dev, 0),
1382 usb_req,
1383 USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_IN,
1384 0, interface, buf, size);
1385}
1386
9e4d5c1b
GB
1387/* Send a proprietary format request to the Scarlett interface */
1388static int scarlett2_usb(
1389 struct usb_mixer_interface *mixer, u32 cmd,
1390 void *req_data, u16 req_size, void *resp_data, u16 resp_size)
1391{
e46f2195 1392 struct scarlett2_data *private = mixer->private_data;
1f7fa6e5 1393 struct usb_device *dev = mixer->chip->dev;
b677b6c6 1394 struct scarlett2_usb_packet *req, *resp = NULL;
2b101256
GS
1395 size_t req_buf_size = struct_size(req, data, req_size);
1396 size_t resp_buf_size = struct_size(resp, data, resp_size);
b677b6c6 1397 int err;
9e4d5c1b
GB
1398
1399 req = kmalloc(req_buf_size, GFP_KERNEL);
1400 if (!req) {
1401 err = -ENOMEM;
1402 goto error;
1403 }
1404
1405 resp = kmalloc(resp_buf_size, GFP_KERNEL);
1406 if (!resp) {
1407 err = -ENOMEM;
1408 goto error;
1409 }
1410
1411 mutex_lock(&private->usb_mutex);
1412
1413 /* build request message and send it */
1414
1415 scarlett2_fill_request_header(private, req, cmd, req_size);
1416
1417 if (req_size)
1418 memcpy(req->data, req_data, req_size);
1419
1f7fa6e5
GB
1420 err = scarlett2_usb_tx(dev, private->bInterfaceNumber,
1421 req, req_buf_size);
9e4d5c1b
GB
1422
1423 if (err != req_buf_size) {
1424 usb_audio_err(
1425 mixer->chip,
6e743781
GB
1426 "%s USB request result cmd %x was %d\n",
1427 private->series_name, cmd, err);
9e4d5c1b
GB
1428 err = -EINVAL;
1429 goto unlock;
1430 }
1431
1432 /* send a second message to get the response */
1433
1f7fa6e5
GB
1434 err = scarlett2_usb_rx(dev, private->bInterfaceNumber,
1435 SCARLETT2_USB_CMD_RESP,
1436 resp, resp_buf_size);
9e4d5c1b
GB
1437
1438 /* validate the response */
1439
1440 if (err != resp_buf_size) {
337b2f0e
GB
1441
1442 /* ESHUTDOWN and EPROTO are valid responses to a
1443 * reboot request
1444 */
1445 if (cmd == SCARLETT2_USB_REBOOT &&
1446 (err == -ESHUTDOWN || err == -EPROTO)) {
1447 err = 0;
1448 goto unlock;
1449 }
1450
9e4d5c1b
GB
1451 usb_audio_err(
1452 mixer->chip,
6e743781
GB
1453 "%s USB response result cmd %x was %d expected %zu\n",
1454 private->series_name, cmd, err, resp_buf_size);
9e4d5c1b
GB
1455 err = -EINVAL;
1456 goto unlock;
1457 }
1458
acf91b81
GB
1459 /* cmd/seq/size should match except when initialising
1460 * seq sent = 1, response = 0
1461 */
9e4d5c1b 1462 if (resp->cmd != req->cmd ||
acf91b81
GB
1463 (resp->seq != req->seq &&
1464 (le16_to_cpu(req->seq) != 1 || resp->seq != 0)) ||
9e4d5c1b
GB
1465 resp_size != le16_to_cpu(resp->size) ||
1466 resp->error ||
1467 resp->pad) {
1468 usb_audio_err(
1469 mixer->chip,
6e743781 1470 "%s USB invalid response; "
9e4d5c1b
GB
1471 "cmd tx/rx %d/%d seq %d/%d size %d/%d "
1472 "error %d pad %d\n",
6e743781 1473 private->series_name,
d8f48935 1474 le32_to_cpu(req->cmd), le32_to_cpu(resp->cmd),
9e4d5c1b
GB
1475 le16_to_cpu(req->seq), le16_to_cpu(resp->seq),
1476 resp_size, le16_to_cpu(resp->size),
d8f48935
TI
1477 le32_to_cpu(resp->error),
1478 le32_to_cpu(resp->pad));
9e4d5c1b
GB
1479 err = -EINVAL;
1480 goto unlock;
1481 }
1482
acf91b81 1483 if (resp_data && resp_size > 0)
9e4d5c1b
GB
1484 memcpy(resp_data, resp->data, resp_size);
1485
1486unlock:
1487 mutex_unlock(&private->usb_mutex);
1488error:
1489 kfree(req);
1490 kfree(resp);
1491 return err;
1492}
1493
8aea2e32
GB
1494/* Send a USB message to get data; result placed in *buf */
1495static int scarlett2_usb_get(
1496 struct usb_mixer_interface *mixer,
1497 int offset, void *buf, int size)
1498{
1499 struct {
1500 __le32 offset;
1501 __le32 size;
1502 } __packed req;
1503
1504 req.offset = cpu_to_le32(offset);
1505 req.size = cpu_to_le32(size);
1506 return scarlett2_usb(mixer, SCARLETT2_USB_GET_DATA,
1507 &req, sizeof(req), buf, size);
1508}
1509
3a4e1afe
GB
1510/* Return true if the given configuration item is present in the
1511 * configuration set used by this device.
1512 */
1513static int scarlett2_has_config_item(
1514 struct scarlett2_data *private, int config_item_num)
1515{
cbd6f148 1516 return !!private->config_set->items[config_item_num].offset;
3a4e1afe
GB
1517}
1518
8aea2e32
GB
1519/* Send a USB message to get configuration parameters; result placed in *buf */
1520static int scarlett2_usb_get_config(
1521 struct usb_mixer_interface *mixer,
1522 int config_item_num, int count, void *buf)
1523{
2fa96277 1524 struct scarlett2_data *private = mixer->private_data;
8aea2e32 1525 const struct scarlett2_config *config_item =
cbd6f148 1526 &private->config_set->items[config_item_num];
9e15fae6 1527 int size, err, i;
9a7b7ec3 1528 u8 *buf_8;
9e15fae6 1529 u8 value;
8aea2e32 1530
9e15fae6
GB
1531 /* For byte-sized parameters, retrieve directly into buf */
1532 if (config_item->size >= 8) {
1533 size = config_item->size / 8 * count;
ac34df73
GB
1534 err = scarlett2_usb_get(mixer, config_item->offset, buf, size);
1535 if (err < 0)
1536 return err;
9a7b7ec3
NC
1537 if (size == 2) {
1538 u16 *buf_16 = buf;
1539
1540 for (i = 0; i < count; i++, buf_16++)
1541 *buf_16 = le16_to_cpu(*(__le16 *)buf_16);
1542 }
ac34df73 1543 return 0;
9e15fae6
GB
1544 }
1545
1546 /* For bit-sized parameters, retrieve into value */
1547 err = scarlett2_usb_get(mixer, config_item->offset, &value, 1);
1548 if (err < 0)
1549 return err;
1550
1551 /* then unpack from value into buf[] */
9a7b7ec3 1552 buf_8 = buf;
9e15fae6 1553 for (i = 0; i < 8 && i < count; i++, value >>= 1)
9a7b7ec3 1554 *buf_8++ = value & 1;
9e15fae6
GB
1555
1556 return 0;
8aea2e32
GB
1557}
1558
9e4d5c1b
GB
1559/* Send SCARLETT2_USB_DATA_CMD SCARLETT2_USB_CONFIG_SAVE */
1560static void scarlett2_config_save(struct usb_mixer_interface *mixer)
1561{
d8f48935 1562 __le32 req = cpu_to_le32(SCARLETT2_USB_CONFIG_SAVE);
9e4d5c1b 1563
5f6ff693
GB
1564 int err = scarlett2_usb(mixer, SCARLETT2_USB_DATA_CMD,
1565 &req, sizeof(u32),
1566 NULL, 0);
1567 if (err < 0)
1568 usb_audio_err(mixer->chip, "config save failed: %d\n", err);
9e4d5c1b
GB
1569}
1570
1571/* Delayed work to save config */
1572static void scarlett2_config_save_work(struct work_struct *work)
1573{
e46f2195
GB
1574 struct scarlett2_data *private =
1575 container_of(work, struct scarlett2_data, work.work);
9e4d5c1b
GB
1576
1577 scarlett2_config_save(private->mixer);
1578}
1579
dbbd4f9e 1580/* Send a USB message to set a SCARLETT2_CONFIG_* parameter */
9e4d5c1b
GB
1581static int scarlett2_usb_set_config(
1582 struct usb_mixer_interface *mixer,
1583 int config_item_num, int index, int value)
1584{
2fa96277 1585 struct scarlett2_data *private = mixer->private_data;
29672631 1586 const struct scarlett2_config *config_item =
cbd6f148 1587 &private->config_set->items[config_item_num];
9e4d5c1b 1588 struct {
d8f48935
TI
1589 __le32 offset;
1590 __le32 bytes;
1591 __le32 value;
9e4d5c1b 1592 } __packed req;
d8f48935 1593 __le32 req2;
9e15fae6 1594 int offset, size;
9e4d5c1b 1595 int err;
9e4d5c1b
GB
1596
1597 /* Cancel any pending NVRAM save */
1598 cancel_delayed_work_sync(&private->work);
1599
9e15fae6
GB
1600 /* Convert config_item->size in bits to size in bytes and
1601 * calculate offset
1602 */
1603 if (config_item->size >= 8) {
1604 size = config_item->size / 8;
1605 offset = config_item->offset + index * size;
1606
1607 /* If updating a bit, retrieve the old value, set/clear the
1608 * bit as needed, and update value
1609 */
1610 } else {
1611 u8 tmp;
1612
1613 size = 1;
1614 offset = config_item->offset;
1615
ca459dfa
GB
1616 err = scarlett2_usb_get(mixer, offset, &tmp, 1);
1617 if (err < 0)
1618 return err;
1619
9e15fae6
GB
1620 if (value)
1621 tmp |= (1 << index);
1622 else
1623 tmp &= ~(1 << index);
1624
1625 value = tmp;
1626 }
1627
9e4d5c1b 1628 /* Send the configuration parameter data */
9e15fae6
GB
1629 req.offset = cpu_to_le32(offset);
1630 req.bytes = cpu_to_le32(size);
9e4d5c1b
GB
1631 req.value = cpu_to_le32(value);
1632 err = scarlett2_usb(mixer, SCARLETT2_USB_SET_DATA,
9e15fae6 1633 &req, sizeof(u32) * 2 + size,
9e4d5c1b
GB
1634 NULL, 0);
1635 if (err < 0)
1636 return err;
1637
1638 /* Activate the change */
29672631 1639 req2 = cpu_to_le32(config_item->activate);
9e4d5c1b
GB
1640 err = scarlett2_usb(mixer, SCARLETT2_USB_DATA_CMD,
1641 &req2, sizeof(req2), NULL, 0);
1642 if (err < 0)
1643 return err;
1644
1645 /* Schedule the change to be written to NVRAM */
303f204e
GB
1646 if (config_item->activate != SCARLETT2_USB_CONFIG_SAVE)
1647 schedule_delayed_work(&private->work, msecs_to_jiffies(2000));
9e4d5c1b
GB
1648
1649 return 0;
1650}
1651
f3c61043
GB
1652/* Send a USB message to get sync status; result placed in *sync */
1653static int scarlett2_usb_get_sync_status(
1654 struct usb_mixer_interface *mixer,
1655 u8 *sync)
1656{
1657 __le32 data;
1658 int err;
1659
1660 err = scarlett2_usb(mixer, SCARLETT2_USB_GET_SYNC,
1661 NULL, 0, &data, sizeof(data));
1662 if (err < 0)
1663 return err;
1664
1665 *sync = !!data;
1666 return 0;
1667}
1668
9e4d5c1b
GB
1669/* Send a USB message to get volume status; result placed in *buf */
1670static int scarlett2_usb_get_volume_status(
1671 struct usb_mixer_interface *mixer,
1672 struct scarlett2_usb_volume_status *buf)
1673{
1674 return scarlett2_usb_get(mixer, SCARLETT2_USB_VOLUME_STATUS_OFFSET,
1675 buf, sizeof(*buf));
1676}
1677
c13d43a8
GB
1678/* Return true if the device has a mixer that we can control */
1679static int scarlett2_has_mixer(struct scarlett2_data *private)
1680{
1681 return !!private->info->mux_assignment[0][0].count;
1682}
1683
2661f033
GB
1684/* Send a USB message to get the volumes for all inputs of one mix
1685 * and put the values into private->mix[]
1686 */
1687static int scarlett2_usb_get_mix(struct usb_mixer_interface *mixer,
1688 int mix_num)
1689{
e46f2195 1690 struct scarlett2_data *private = mixer->private_data;
2661f033
GB
1691 const struct scarlett2_device_info *info = private->info;
1692
1693 int num_mixer_in =
e2cc91ac 1694 info->port_count[SCARLETT2_PORT_TYPE_MIX][SCARLETT2_PORT_OUT];
2661f033
GB
1695 int err, i, j, k;
1696
1697 struct {
1698 __le16 mix_num;
1699 __le16 count;
1700 } __packed req;
1701
1702 __le16 data[SCARLETT2_INPUT_MIX_MAX];
1703
1704 req.mix_num = cpu_to_le16(mix_num);
1705 req.count = cpu_to_le16(num_mixer_in);
1706
1707 err = scarlett2_usb(mixer, SCARLETT2_USB_GET_MIX,
1708 &req, sizeof(req),
1709 data, num_mixer_in * sizeof(u16));
1710 if (err < 0)
1711 return err;
1712
1713 for (i = 0, j = mix_num * num_mixer_in; i < num_mixer_in; i++, j++) {
1714 u16 mixer_value = le16_to_cpu(data[i]);
1715
1716 for (k = 0; k < SCARLETT2_MIXER_VALUE_COUNT; k++)
1717 if (scarlett2_mixer_values[k] >= mixer_value)
1718 break;
1719 if (k == SCARLETT2_MIXER_VALUE_COUNT)
1720 k = SCARLETT2_MIXER_MAX_VALUE;
1721 private->mix[j] = k;
1722 }
1723
1724 return 0;
1725}
1726
9e4d5c1b
GB
1727/* Send a USB message to set the volumes for all inputs of one mix
1728 * (values obtained from private->mix[])
1729 */
1730static int scarlett2_usb_set_mix(struct usb_mixer_interface *mixer,
06a21621 1731 int mix_num)
9e4d5c1b 1732{
e46f2195 1733 struct scarlett2_data *private = mixer->private_data;
9e4d5c1b
GB
1734 const struct scarlett2_device_info *info = private->info;
1735
1736 struct {
d8f48935
TI
1737 __le16 mix_num;
1738 __le16 data[SCARLETT2_INPUT_MIX_MAX];
9e4d5c1b
GB
1739 } __packed req;
1740
1741 int i, j;
1742 int num_mixer_in =
e2cc91ac 1743 info->port_count[SCARLETT2_PORT_TYPE_MIX][SCARLETT2_PORT_OUT];
9e4d5c1b
GB
1744
1745 req.mix_num = cpu_to_le16(mix_num);
1746
1747 for (i = 0, j = mix_num * num_mixer_in; i < num_mixer_in; i++, j++)
1748 req.data[i] = cpu_to_le16(
1749 scarlett2_mixer_values[private->mix[j]]
1750 );
1751
1752 return scarlett2_usb(mixer, SCARLETT2_USB_SET_MIX,
1753 &req, (num_mixer_in + 1) * sizeof(u16),
1754 NULL, 0);
1755}
1756
e2cc91ac
GB
1757/* Convert a port number index (per info->port_count) to a hardware ID */
1758static u32 scarlett2_mux_src_num_to_id(
1759 const int port_count[][SCARLETT2_PORT_DIRNS], int num)
9e4d5c1b
GB
1760{
1761 int port_type;
1762
1763 for (port_type = 0;
1764 port_type < SCARLETT2_PORT_TYPE_COUNT;
1765 port_type++) {
e2cc91ac
GB
1766 if (num < port_count[port_type][SCARLETT2_PORT_IN])
1767 return scarlett2_ports[port_type].id | num;
1768 num -= port_count[port_type][SCARLETT2_PORT_IN];
9e4d5c1b
GB
1769 }
1770
1771 /* Oops */
1772 return 0;
1773}
1774
d6f9afe9 1775/* Convert a hardware ID to a port number index */
e2cc91ac
GB
1776static u32 scarlett2_mux_id_to_num(
1777 const int port_count[][SCARLETT2_PORT_DIRNS], int direction, u32 id)
d6f9afe9
GB
1778{
1779 int port_type;
1780 int port_num = 0;
1781
1782 for (port_type = 0;
1783 port_type < SCARLETT2_PORT_TYPE_COUNT;
1784 port_type++) {
e2cc91ac
GB
1785 int base = scarlett2_ports[port_type].id;
1786 int count = port_count[port_type][direction];
d6f9afe9 1787
e2cc91ac
GB
1788 if (id >= base && id < base + count)
1789 return port_num + id - base;
d6f9afe9
GB
1790 port_num += count;
1791 }
1792
1793 /* Oops */
1794 return -1;
1795}
1796
1797/* Convert one mux entry from the interface and load into private->mux[] */
e46f2195 1798static void scarlett2_usb_populate_mux(struct scarlett2_data *private,
d6f9afe9
GB
1799 u32 mux_entry)
1800{
1801 const struct scarlett2_device_info *info = private->info;
e2cc91ac 1802 const int (*port_count)[SCARLETT2_PORT_DIRNS] = info->port_count;
d6f9afe9
GB
1803
1804 int dst_idx, src_idx;
1805
e2cc91ac 1806 dst_idx = scarlett2_mux_id_to_num(port_count, SCARLETT2_PORT_OUT,
d6f9afe9
GB
1807 mux_entry & 0xFFF);
1808 if (dst_idx < 0)
1809 return;
1810
1811 if (dst_idx >= private->num_mux_dsts) {
1812 usb_audio_err(private->mixer->chip,
1813 "BUG: scarlett2_mux_id_to_num(%06x, OUT): %d >= %d",
1814 mux_entry, dst_idx, private->num_mux_dsts);
1815 return;
1816 }
1817
e2cc91ac 1818 src_idx = scarlett2_mux_id_to_num(port_count, SCARLETT2_PORT_IN,
d6f9afe9
GB
1819 mux_entry >> 12);
1820 if (src_idx < 0)
1821 return;
1822
1823 if (src_idx >= private->num_mux_srcs) {
1824 usb_audio_err(private->mixer->chip,
1825 "BUG: scarlett2_mux_id_to_num(%06x, IN): %d >= %d",
1826 mux_entry, src_idx, private->num_mux_srcs);
1827 return;
1828 }
1829
1830 private->mux[dst_idx] = src_idx;
1831}
1832
3473185f
GB
1833/* Update the meter level map
1834 *
1835 * The meter level data from the interface (SCARLETT2_USB_GET_METER
1836 * request) is returned in mux_assignment order, but to avoid exposing
1837 * that to userspace, scarlett2_meter_ctl_get() rearranges the data
1838 * into scarlett2_ports order using the meter_level_map[] array which
1839 * is set up by this function.
1840 *
1841 * In addition, the meter level data values returned from the
1842 * interface are invalid for destinations where:
1843 *
1844 * - the source is "Off"; therefore we set those values to zero (map
1845 * value of 255)
1846 *
1847 * - the source is assigned to a previous (with respect to the
1848 * mux_assignment order) destination; therefore we set those values
1849 * to the value previously reported for that source
1850 */
1851static void scarlett2_update_meter_level_map(struct scarlett2_data *private)
1852{
1853 const struct scarlett2_device_info *info = private->info;
1854 const int (*port_count)[SCARLETT2_PORT_DIRNS] = info->port_count;
1855 int line_out_count =
1856 port_count[SCARLETT2_PORT_TYPE_ANALOGUE][SCARLETT2_PORT_OUT];
1857 const struct scarlett2_meter_entry *entry;
1858
1859 /* sources already assigned to a destination
1860 * value is 255 for None, otherwise the value of i
1861 * (index into array returned by
1862 * scarlett2_usb_get_meter_levels())
1863 */
1864 u8 seen_src[SCARLETT2_MAX_SRCS] = { 1 };
1865 u8 seen_src_value[SCARLETT2_MAX_SRCS] = { 255 };
1866
1867 /* index in meter_map[] order */
1868 int i = 0;
1869
1870 /* go through the meter_map[] entries */
1871 for (entry = info->meter_map;
1872 entry->count;
1873 entry++) {
1874
1875 /* fill in each meter_level_map[] entry */
1876 int j, mux_idx;
1877
1878 for (j = 0, mux_idx = entry->start;
1879 j < entry->count;
1880 i++, j++, mux_idx++) {
1881
1882 /* convert mux_idx using line_out_unmap[] */
1883 int map_mux_idx = (
1884 info->line_out_remap_enable &&
1885 mux_idx < line_out_count
1886 ) ? info->line_out_unmap[mux_idx]
1887 : mux_idx;
1888
1889 /* check which source is connected, and if
1890 * that source is already connected elsewhere,
1891 * use that existing connection's destination
1892 * for this meter entry instead
1893 */
1894 int mux_src = private->mux[mux_idx];
1895
1896 if (!seen_src[mux_src]) {
1897 seen_src[mux_src] = 1;
1898 seen_src_value[mux_src] = i;
1899 }
1900 private->meter_level_map[map_mux_idx] =
1901 seen_src_value[mux_src];
1902 }
1903 }
1904}
1905
d6f9afe9
GB
1906/* Send USB message to get mux inputs and then populate private->mux[] */
1907static int scarlett2_usb_get_mux(struct usb_mixer_interface *mixer)
1908{
e46f2195 1909 struct scarlett2_data *private = mixer->private_data;
d6f9afe9
GB
1910 int count = private->num_mux_dsts;
1911 int err, i;
1912
1913 struct {
1914 __le16 num;
1915 __le16 count;
1916 } __packed req;
1917
1918 __le32 data[SCARLETT2_MUX_MAX];
1919
8df25eb0
GB
1920 private->mux_updated = 0;
1921
d6f9afe9
GB
1922 req.num = 0;
1923 req.count = cpu_to_le16(count);
1924
1925 err = scarlett2_usb(mixer, SCARLETT2_USB_GET_MUX,
1926 &req, sizeof(req),
1927 data, count * sizeof(u32));
1928 if (err < 0)
1929 return err;
1930
1931 for (i = 0; i < count; i++)
1932 scarlett2_usb_populate_mux(private, le32_to_cpu(data[i]));
1933
3473185f
GB
1934 scarlett2_update_meter_level_map(private);
1935
d6f9afe9
GB
1936 return 0;
1937}
1938
9e4d5c1b
GB
1939/* Send USB messages to set mux inputs */
1940static int scarlett2_usb_set_mux(struct usb_mixer_interface *mixer)
1941{
e46f2195 1942 struct scarlett2_data *private = mixer->private_data;
9e4d5c1b 1943 const struct scarlett2_device_info *info = private->info;
e2cc91ac 1944 const int (*port_count)[SCARLETT2_PORT_DIRNS] = info->port_count;
6522c364 1945 int table;
9e4d5c1b
GB
1946
1947 struct {
d8f48935
TI
1948 __le16 pad;
1949 __le16 num;
1950 __le32 data[SCARLETT2_MUX_MAX];
9e4d5c1b
GB
1951 } __packed req;
1952
1953 req.pad = 0;
1954
6522c364
GB
1955 /* set mux settings for each rate */
1956 for (table = 0; table < SCARLETT2_MUX_TABLES; table++) {
1957 const struct scarlett2_mux_entry *entry;
1958
1959 /* i counts over the output array */
1960 int i = 0, err;
1961
1962 req.num = cpu_to_le16(table);
1963
1964 /* loop through each entry */
1965 for (entry = info->mux_assignment[table];
1966 entry->count;
1967 entry++) {
1968 int j;
1969 int port_type = entry->port_type;
1970 int port_idx = entry->start;
e2cc91ac 1971 int mux_idx = scarlett2_get_port_start_num(port_count,
6522c364 1972 SCARLETT2_PORT_OUT, port_type) + port_idx;
e2cc91ac 1973 int dst_id = scarlett2_ports[port_type].id + port_idx;
6522c364
GB
1974
1975 /* Empty slots */
1976 if (!dst_id) {
1977 for (j = 0; j < entry->count; j++)
1978 req.data[i++] = 0;
1979 continue;
1980 }
1981
1982 /* Non-empty mux slots use the lower 12 bits
1983 * for the destination and next 12 bits for
1984 * the source
1985 */
1986 for (j = 0; j < entry->count; j++) {
1987 int src_id = scarlett2_mux_src_num_to_id(
e2cc91ac 1988 port_count, private->mux[mux_idx++]);
6522c364
GB
1989 req.data[i++] = cpu_to_le32(dst_id |
1990 src_id << 12);
1991 dst_id++;
1992 }
9e4d5c1b
GB
1993 }
1994
1995 err = scarlett2_usb(mixer, SCARLETT2_USB_SET_MUX,
1996 &req, (i + 1) * sizeof(u32),
1997 NULL, 0);
1998 if (err < 0)
1999 return err;
2000 }
2001
3473185f
GB
2002 scarlett2_update_meter_level_map(private);
2003
9e4d5c1b
GB
2004 return 0;
2005}
2006
2007/* Send USB message to get meter levels */
2008static int scarlett2_usb_get_meter_levels(struct usb_mixer_interface *mixer,
b126bbac 2009 u16 num_meters, u16 *levels)
9e4d5c1b
GB
2010{
2011 struct {
d8f48935
TI
2012 __le16 pad;
2013 __le16 num_meters;
2014 __le32 magic;
9e4d5c1b 2015 } __packed req;
0b2dca55 2016 __le32 resp[SCARLETT2_MAX_METERS];
9e4d5c1b
GB
2017 int i, err;
2018
2019 req.pad = 0;
b126bbac 2020 req.num_meters = cpu_to_le16(num_meters);
9e4d5c1b 2021 req.magic = cpu_to_le32(SCARLETT2_USB_METER_LEVELS_GET_MAGIC);
06a21621 2022 err = scarlett2_usb(mixer, SCARLETT2_USB_GET_METER,
b126bbac 2023 &req, sizeof(req), resp, num_meters * sizeof(u32));
9e4d5c1b
GB
2024 if (err < 0)
2025 return err;
2026
2027 /* copy, convert to u16 */
b126bbac 2028 for (i = 0; i < num_meters; i++)
0b2dca55 2029 levels[i] = le32_to_cpu(resp[i]);
9e4d5c1b
GB
2030
2031 return 0;
2032}
2033
2034/*** Control Functions ***/
2035
2036/* helper function to create a new control */
2037static int scarlett2_add_new_ctl(struct usb_mixer_interface *mixer,
2038 const struct snd_kcontrol_new *ncontrol,
2039 int index, int channels, const char *name,
2040 struct snd_kcontrol **kctl_return)
2041{
2042 struct snd_kcontrol *kctl;
2043 struct usb_mixer_elem_info *elem;
2044 int err;
2045
2046 elem = kzalloc(sizeof(*elem), GFP_KERNEL);
2047 if (!elem)
2048 return -ENOMEM;
2049
785b6f29
TI
2050 /* We set USB_MIXER_BESPOKEN type, so that the core USB mixer code
2051 * ignores them for resume and other operations.
2052 * Also, the head.id field is set to 0, as we don't use this field.
2053 */
9e4d5c1b
GB
2054 elem->head.mixer = mixer;
2055 elem->control = index;
785b6f29 2056 elem->head.id = 0;
9e4d5c1b 2057 elem->channels = channels;
785b6f29 2058 elem->val_type = USB_MIXER_BESPOKEN;
9e4d5c1b
GB
2059
2060 kctl = snd_ctl_new1(ncontrol, elem);
2061 if (!kctl) {
2062 kfree(elem);
2063 return -ENOMEM;
2064 }
2065 kctl->private_free = snd_usb_mixer_elem_free;
2066
75b1a8f9 2067 strscpy(kctl->id.name, name, sizeof(kctl->id.name));
9e4d5c1b
GB
2068
2069 err = snd_usb_mixer_add_control(&elem->head, kctl);
2070 if (err < 0)
2071 return err;
2072
2073 if (kctl_return)
2074 *kctl_return = kctl;
2075
2076 return 0;
2077}
2078
701949cc
GB
2079/*** Firmware Version Control ***/
2080
2081static int scarlett2_firmware_version_ctl_get(
2082 struct snd_kcontrol *kctl,
2083 struct snd_ctl_elem_value *ucontrol)
2084{
2085 struct usb_mixer_elem_info *elem = kctl->private_data;
2086 struct scarlett2_data *private = elem->head.mixer->private_data;
2087
2088 ucontrol->value.integer.value[0] = private->firmware_version;
2089
2090 return 0;
2091}
2092
2093static int scarlett2_firmware_version_ctl_info(
2094 struct snd_kcontrol *kctl,
2095 struct snd_ctl_elem_info *uinfo)
2096{
2097 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2098 uinfo->count = 1;
2099
2100 return 0;
2101}
2102
2103static const struct snd_kcontrol_new scarlett2_firmware_version_ctl = {
2104 .iface = SNDRV_CTL_ELEM_IFACE_CARD,
2105 .access = SNDRV_CTL_ELEM_ACCESS_READ,
2106 .name = "",
2107 .info = scarlett2_firmware_version_ctl_info,
2108 .get = scarlett2_firmware_version_ctl_get
2109};
2110
2111static int scarlett2_add_firmware_version_ctl(
2112 struct usb_mixer_interface *mixer)
2113{
2114 return scarlett2_add_new_ctl(mixer, &scarlett2_firmware_version_ctl,
2115 0, 0, "Firmware Version", NULL);
2116}
f3c61043
GB
2117/*** Sync Control ***/
2118
2119/* Update sync control after receiving notification that the status
2120 * has changed
2121 */
2122static int scarlett2_update_sync(struct usb_mixer_interface *mixer)
2123{
2124 struct scarlett2_data *private = mixer->private_data;
2125
2126 private->sync_updated = 0;
2127 return scarlett2_usb_get_sync_status(mixer, &private->sync);
2128}
2129
2130static int scarlett2_sync_ctl_info(struct snd_kcontrol *kctl,
2131 struct snd_ctl_elem_info *uinfo)
2132{
2133 static const char *texts[2] = {
2134 "Unlocked", "Locked"
2135 };
2136 return snd_ctl_enum_info(uinfo, 1, 2, texts);
2137}
2138
2139static int scarlett2_sync_ctl_get(struct snd_kcontrol *kctl,
2140 struct snd_ctl_elem_value *ucontrol)
2141{
2142 struct usb_mixer_elem_info *elem = kctl->private_data;
2143 struct usb_mixer_interface *mixer = elem->head.mixer;
2144 struct scarlett2_data *private = mixer->private_data;
50603a67 2145 int err = 0;
f3c61043
GB
2146
2147 mutex_lock(&private->data_mutex);
50603a67 2148
6a7508e6
GB
2149 if (private->hwdep_in_use) {
2150 err = -EBUSY;
2151 goto unlock;
2152 }
2153
50603a67
GB
2154 if (private->sync_updated) {
2155 err = scarlett2_update_sync(mixer);
2156 if (err < 0)
2157 goto unlock;
2158 }
f3c61043 2159 ucontrol->value.enumerated.item[0] = private->sync;
f3c61043 2160
50603a67
GB
2161unlock:
2162 mutex_unlock(&private->data_mutex);
2163 return err;
f3c61043
GB
2164}
2165
2166static const struct snd_kcontrol_new scarlett2_sync_ctl = {
2167 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2168 .access = SNDRV_CTL_ELEM_ACCESS_READ,
2169 .name = "",
2170 .info = scarlett2_sync_ctl_info,
2171 .get = scarlett2_sync_ctl_get
2172};
2173
2174static int scarlett2_add_sync_ctl(struct usb_mixer_interface *mixer)
2175{
2176 struct scarlett2_data *private = mixer->private_data;
2177
2fa96277 2178 /* devices without a mixer also don't support reporting sync status */
c13d43a8 2179 if (!scarlett2_has_mixer(private))
2fa96277
GB
2180 return 0;
2181
f3c61043
GB
2182 return scarlett2_add_new_ctl(mixer, &scarlett2_sync_ctl,
2183 0, 1, "Sync Status", &private->sync_ctl);
2184}
2185
9e4d5c1b
GB
2186/*** Analogue Line Out Volume Controls ***/
2187
2188/* Update hardware volume controls after receiving notification that
2189 * they have changed
2190 */
2191static int scarlett2_update_volumes(struct usb_mixer_interface *mixer)
2192{
e46f2195 2193 struct scarlett2_data *private = mixer->private_data;
03bdbcf0 2194 const struct scarlett2_device_info *info = private->info;
e2cc91ac 2195 const int (*port_count)[SCARLETT2_PORT_DIRNS] = info->port_count;
9e4d5c1b
GB
2196 struct scarlett2_usb_volume_status volume_status;
2197 int num_line_out =
e2cc91ac 2198 port_count[SCARLETT2_PORT_TYPE_ANALOGUE][SCARLETT2_PORT_OUT];
9e4d5c1b 2199 int err, i;
0c88f9db 2200 int mute;
9e4d5c1b
GB
2201
2202 private->vol_updated = 0;
2203
2204 err = scarlett2_usb_get_volume_status(mixer, &volume_status);
2205 if (err < 0)
2206 return err;
2207
2208 private->master_vol = clamp(
2209 volume_status.master_vol + SCARLETT2_VOLUME_BIAS,
2210 0, SCARLETT2_VOLUME_BIAS);
2211
03bdbcf0 2212 if (info->line_out_hw_vol)
dbd82c05
GB
2213 for (i = 0; i < SCARLETT2_DIM_MUTE_COUNT; i++)
2214 private->dim_mute[i] = !!volume_status.dim_mute[i];
9e4d5c1b 2215
0c88f9db
GB
2216 mute = private->dim_mute[SCARLETT2_BUTTON_MUTE];
2217
2218 for (i = 0; i < num_line_out; i++)
2219 if (private->vol_sw_hw_switch[i]) {
2220 private->vol[i] = private->master_vol;
2221 private->mute_switch[i] = mute;
2222 }
2223
9e4d5c1b
GB
2224 return 0;
2225}
2226
2227static int scarlett2_volume_ctl_info(struct snd_kcontrol *kctl,
2228 struct snd_ctl_elem_info *uinfo)
2229{
2230 struct usb_mixer_elem_info *elem = kctl->private_data;
2231
2232 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2233 uinfo->count = elem->channels;
2234 uinfo->value.integer.min = 0;
2235 uinfo->value.integer.max = SCARLETT2_VOLUME_BIAS;
2236 uinfo->value.integer.step = 1;
2237 return 0;
2238}
2239
2240static int scarlett2_master_volume_ctl_get(struct snd_kcontrol *kctl,
2241 struct snd_ctl_elem_value *ucontrol)
2242{
2243 struct usb_mixer_elem_info *elem = kctl->private_data;
2244 struct usb_mixer_interface *mixer = elem->head.mixer;
e46f2195 2245 struct scarlett2_data *private = mixer->private_data;
50603a67 2246 int err = 0;
9e4d5c1b 2247
9b5ddea9 2248 mutex_lock(&private->data_mutex);
9e4d5c1b 2249
6a7508e6
GB
2250 if (private->hwdep_in_use) {
2251 err = -EBUSY;
2252 goto unlock;
2253 }
2254
50603a67
GB
2255 if (private->vol_updated) {
2256 err = scarlett2_update_volumes(mixer);
2257 if (err < 0)
2258 goto unlock;
2259 }
9e4d5c1b 2260 ucontrol->value.integer.value[0] = private->master_vol;
50603a67
GB
2261
2262unlock:
2263 mutex_unlock(&private->data_mutex);
2264 return err;
9e4d5c1b
GB
2265}
2266
9cfe1276
GB
2267static int line_out_remap(struct scarlett2_data *private, int index)
2268{
2269 const struct scarlett2_device_info *info = private->info;
2190b9ae
GB
2270 const int (*port_count)[SCARLETT2_PORT_DIRNS] = info->port_count;
2271 int line_out_count =
2272 port_count[SCARLETT2_PORT_TYPE_ANALOGUE][SCARLETT2_PORT_OUT];
9cfe1276
GB
2273
2274 if (!info->line_out_remap_enable)
2275 return index;
2190b9ae
GB
2276
2277 if (index >= line_out_count)
2278 return index;
2279
9cfe1276
GB
2280 return info->line_out_remap[index];
2281}
2282
9e4d5c1b
GB
2283static int scarlett2_volume_ctl_get(struct snd_kcontrol *kctl,
2284 struct snd_ctl_elem_value *ucontrol)
2285{
2286 struct usb_mixer_elem_info *elem = kctl->private_data;
2287 struct usb_mixer_interface *mixer = elem->head.mixer;
e46f2195 2288 struct scarlett2_data *private = mixer->private_data;
9cfe1276 2289 int index = line_out_remap(private, elem->control);
50603a67 2290 int err = 0;
9e4d5c1b 2291
9b5ddea9 2292 mutex_lock(&private->data_mutex);
9e4d5c1b 2293
6a7508e6
GB
2294 if (private->hwdep_in_use) {
2295 err = -EBUSY;
2296 goto unlock;
2297 }
2298
50603a67
GB
2299 if (private->vol_updated) {
2300 err = scarlett2_update_volumes(mixer);
2301 if (err < 0)
2302 goto unlock;
2303 }
9e4d5c1b 2304 ucontrol->value.integer.value[0] = private->vol[index];
50603a67
GB
2305
2306unlock:
2307 mutex_unlock(&private->data_mutex);
2308 return err;
9e4d5c1b
GB
2309}
2310
2311static int scarlett2_volume_ctl_put(struct snd_kcontrol *kctl,
2312 struct snd_ctl_elem_value *ucontrol)
2313{
2314 struct usb_mixer_elem_info *elem = kctl->private_data;
2315 struct usb_mixer_interface *mixer = elem->head.mixer;
e46f2195 2316 struct scarlett2_data *private = mixer->private_data;
9cfe1276 2317 int index = line_out_remap(private, elem->control);
9e4d5c1b
GB
2318 int oval, val, err = 0;
2319
2320 mutex_lock(&private->data_mutex);
2321
6a7508e6
GB
2322 if (private->hwdep_in_use) {
2323 err = -EBUSY;
2324 goto unlock;
2325 }
2326
9e4d5c1b
GB
2327 oval = private->vol[index];
2328 val = ucontrol->value.integer.value[0];
2329
2330 if (oval == val)
2331 goto unlock;
2332
2333 private->vol[index] = val;
2334 err = scarlett2_usb_set_config(mixer, SCARLETT2_CONFIG_LINE_OUT_VOLUME,
2335 index, val - SCARLETT2_VOLUME_BIAS);
2336 if (err == 0)
2337 err = 1;
2338
2339unlock:
2340 mutex_unlock(&private->data_mutex);
2341 return err;
2342}
2343
2344static const DECLARE_TLV_DB_MINMAX(
2345 db_scale_scarlett2_gain, -SCARLETT2_VOLUME_BIAS * 100, 0
2346);
2347
2348static const struct snd_kcontrol_new scarlett2_master_volume_ctl = {
2349 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2350 .access = SNDRV_CTL_ELEM_ACCESS_READ |
2351 SNDRV_CTL_ELEM_ACCESS_TLV_READ,
2352 .name = "",
2353 .info = scarlett2_volume_ctl_info,
2354 .get = scarlett2_master_volume_ctl_get,
2355 .private_value = 0, /* max value */
2356 .tlv = { .p = db_scale_scarlett2_gain }
2357};
2358
2359static const struct snd_kcontrol_new scarlett2_line_out_volume_ctl = {
2360 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2361 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE |
2362 SNDRV_CTL_ELEM_ACCESS_TLV_READ,
2363 .name = "",
2364 .info = scarlett2_volume_ctl_info,
2365 .get = scarlett2_volume_ctl_get,
2366 .put = scarlett2_volume_ctl_put,
2367 .private_value = 0, /* max value */
2368 .tlv = { .p = db_scale_scarlett2_gain }
2369};
2370
0c88f9db
GB
2371/*** Mute Switch Controls ***/
2372
2373static int scarlett2_mute_ctl_get(struct snd_kcontrol *kctl,
2374 struct snd_ctl_elem_value *ucontrol)
2375{
2376 struct usb_mixer_elem_info *elem = kctl->private_data;
9ee0fc83
GB
2377 struct usb_mixer_interface *mixer = elem->head.mixer;
2378 struct scarlett2_data *private = mixer->private_data;
9cfe1276 2379 int index = line_out_remap(private, elem->control);
50603a67 2380 int err = 0;
0c88f9db 2381
9ee0fc83 2382 mutex_lock(&private->data_mutex);
9ee0fc83 2383
6a7508e6
GB
2384 if (private->hwdep_in_use) {
2385 err = -EBUSY;
2386 goto unlock;
2387 }
2388
50603a67
GB
2389 if (private->vol_updated) {
2390 err = scarlett2_update_volumes(mixer);
2391 if (err < 0)
2392 goto unlock;
2393 }
0c88f9db 2394 ucontrol->value.integer.value[0] = private->mute_switch[index];
50603a67
GB
2395
2396unlock:
2397 mutex_unlock(&private->data_mutex);
2398 return err;
0c88f9db
GB
2399}
2400
2401static int scarlett2_mute_ctl_put(struct snd_kcontrol *kctl,
2402 struct snd_ctl_elem_value *ucontrol)
2403{
2404 struct usb_mixer_elem_info *elem = kctl->private_data;
2405 struct usb_mixer_interface *mixer = elem->head.mixer;
2406 struct scarlett2_data *private = mixer->private_data;
9cfe1276 2407 int index = line_out_remap(private, elem->control);
0c88f9db
GB
2408 int oval, val, err = 0;
2409
2410 mutex_lock(&private->data_mutex);
2411
6a7508e6
GB
2412 if (private->hwdep_in_use) {
2413 err = -EBUSY;
2414 goto unlock;
2415 }
2416
0c88f9db
GB
2417 oval = private->mute_switch[index];
2418 val = !!ucontrol->value.integer.value[0];
2419
2420 if (oval == val)
2421 goto unlock;
2422
2423 private->mute_switch[index] = val;
2424
2425 /* Send mute change to the device */
2426 err = scarlett2_usb_set_config(mixer, SCARLETT2_CONFIG_MUTE_SWITCH,
2427 index, val);
459d2320
GB
2428 if (err == 0)
2429 err = 1;
0c88f9db
GB
2430
2431unlock:
2432 mutex_unlock(&private->data_mutex);
2433 return err;
2434}
2435
2436static const struct snd_kcontrol_new scarlett2_mute_ctl = {
2437 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2438 .name = "",
2439 .info = snd_ctl_boolean_mono_info,
2440 .get = scarlett2_mute_ctl_get,
2441 .put = scarlett2_mute_ctl_put,
2442};
2443
9e4d5c1b
GB
2444/*** HW/SW Volume Switch Controls ***/
2445
e914d843
GB
2446static void scarlett2_sw_hw_ctl_ro(struct scarlett2_data *private, int index)
2447{
2448 private->sw_hw_ctls[index]->vd[0].access &=
2449 ~SNDRV_CTL_ELEM_ACCESS_WRITE;
2450}
2451
2452static void scarlett2_sw_hw_ctl_rw(struct scarlett2_data *private, int index)
2453{
2454 private->sw_hw_ctls[index]->vd[0].access |=
2455 SNDRV_CTL_ELEM_ACCESS_WRITE;
2456}
2457
9e4d5c1b
GB
2458static int scarlett2_sw_hw_enum_ctl_info(struct snd_kcontrol *kctl,
2459 struct snd_ctl_elem_info *uinfo)
2460{
2461 static const char *const values[2] = {
2462 "SW", "HW"
2463 };
2464
2465 return snd_ctl_enum_info(uinfo, 1, 2, values);
2466}
2467
2468static int scarlett2_sw_hw_enum_ctl_get(struct snd_kcontrol *kctl,
2469 struct snd_ctl_elem_value *ucontrol)
2470{
2471 struct usb_mixer_elem_info *elem = kctl->private_data;
e46f2195 2472 struct scarlett2_data *private = elem->head.mixer->private_data;
9cfe1276 2473 int index = line_out_remap(private, elem->control);
9e4d5c1b 2474
9cfe1276 2475 ucontrol->value.enumerated.item[0] = private->vol_sw_hw_switch[index];
9e4d5c1b
GB
2476 return 0;
2477}
2478
06250c89
GB
2479static void scarlett2_vol_ctl_set_writable(struct usb_mixer_interface *mixer,
2480 int index, int value)
2481{
2482 struct scarlett2_data *private = mixer->private_data;
0c88f9db 2483 struct snd_card *card = mixer->chip->card;
06250c89 2484
0c88f9db
GB
2485 /* Set/Clear write bits */
2486 if (value) {
06250c89
GB
2487 private->vol_ctls[index]->vd[0].access |=
2488 SNDRV_CTL_ELEM_ACCESS_WRITE;
0c88f9db
GB
2489 private->mute_ctls[index]->vd[0].access |=
2490 SNDRV_CTL_ELEM_ACCESS_WRITE;
2491 } else {
06250c89
GB
2492 private->vol_ctls[index]->vd[0].access &=
2493 ~SNDRV_CTL_ELEM_ACCESS_WRITE;
0c88f9db
GB
2494 private->mute_ctls[index]->vd[0].access &=
2495 ~SNDRV_CTL_ELEM_ACCESS_WRITE;
2496 }
06250c89 2497
2b8b12be
GB
2498 /* Notify of write bit and possible value change */
2499 snd_ctl_notify(card,
2500 SNDRV_CTL_EVENT_MASK_VALUE | SNDRV_CTL_EVENT_MASK_INFO,
06250c89 2501 &private->vol_ctls[index]->id);
2b8b12be
GB
2502 snd_ctl_notify(card,
2503 SNDRV_CTL_EVENT_MASK_VALUE | SNDRV_CTL_EVENT_MASK_INFO,
0c88f9db 2504 &private->mute_ctls[index]->id);
06250c89
GB
2505}
2506
3b9e3720
GB
2507static int scarlett2_sw_hw_change(struct usb_mixer_interface *mixer,
2508 int ctl_index, int val)
9e4d5c1b 2509{
e46f2195 2510 struct scarlett2_data *private = mixer->private_data;
9cfe1276 2511 int index = line_out_remap(private, ctl_index);
3b9e3720 2512 int err;
9e4d5c1b
GB
2513
2514 private->vol_sw_hw_switch[index] = val;
2515
2516 /* Change access mode to RO (hardware controlled volume)
2517 * or RW (software controlled volume)
2518 */
9cfe1276 2519 scarlett2_vol_ctl_set_writable(mixer, ctl_index, !val);
9e4d5c1b 2520
0c88f9db 2521 /* Reset volume/mute to master volume/mute */
9e4d5c1b 2522 private->vol[index] = private->master_vol;
0c88f9db 2523 private->mute_switch[index] = private->dim_mute[SCARLETT2_BUTTON_MUTE];
9e4d5c1b
GB
2524
2525 /* Set SW volume to current HW volume */
2526 err = scarlett2_usb_set_config(
2527 mixer, SCARLETT2_CONFIG_LINE_OUT_VOLUME,
2528 index, private->master_vol - SCARLETT2_VOLUME_BIAS);
2529 if (err < 0)
3b9e3720 2530 return err;
9e4d5c1b 2531
0c88f9db
GB
2532 /* Set SW mute to current HW mute */
2533 err = scarlett2_usb_set_config(
2534 mixer, SCARLETT2_CONFIG_MUTE_SWITCH,
2535 index, private->dim_mute[SCARLETT2_BUTTON_MUTE]);
2536 if (err < 0)
3b9e3720 2537 return err;
0c88f9db 2538
9e4d5c1b 2539 /* Send SW/HW switch change to the device */
3b9e3720
GB
2540 return scarlett2_usb_set_config(mixer, SCARLETT2_CONFIG_SW_HW_SWITCH,
2541 index, val);
2542}
2543
2544static int scarlett2_sw_hw_enum_ctl_put(struct snd_kcontrol *kctl,
2545 struct snd_ctl_elem_value *ucontrol)
2546{
2547 struct usb_mixer_elem_info *elem = kctl->private_data;
2548 struct usb_mixer_interface *mixer = elem->head.mixer;
2549 struct scarlett2_data *private = mixer->private_data;
2550 int ctl_index = elem->control;
2551 int index = line_out_remap(private, ctl_index);
2552 int oval, val, err = 0;
2553
2554 mutex_lock(&private->data_mutex);
2555
6a7508e6
GB
2556 if (private->hwdep_in_use) {
2557 err = -EBUSY;
2558 goto unlock;
2559 }
2560
3b9e3720
GB
2561 oval = private->vol_sw_hw_switch[index];
2562 val = !!ucontrol->value.enumerated.item[0];
2563
2564 if (oval == val)
2565 goto unlock;
2566
2567 err = scarlett2_sw_hw_change(mixer, ctl_index, val);
c5d8e008
GB
2568 if (err == 0)
2569 err = 1;
9e4d5c1b
GB
2570
2571unlock:
2572 mutex_unlock(&private->data_mutex);
2573 return err;
2574}
2575
2576static const struct snd_kcontrol_new scarlett2_sw_hw_enum_ctl = {
2577 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2578 .name = "",
2579 .info = scarlett2_sw_hw_enum_ctl_info,
2580 .get = scarlett2_sw_hw_enum_ctl_get,
2581 .put = scarlett2_sw_hw_enum_ctl_put,
2582};
2583
2584/*** Line Level/Instrument Level Switch Controls ***/
2585
a5b36123
GB
2586static int scarlett2_update_input_other(struct usb_mixer_interface *mixer)
2587{
2588 struct scarlett2_data *private = mixer->private_data;
2589 const struct scarlett2_device_info *info = private->info;
2590
2591 private->input_other_updated = 0;
2592
2593 if (info->level_input_count) {
2594 int err = scarlett2_usb_get_config(
2595 mixer, SCARLETT2_CONFIG_LEVEL_SWITCH,
2fa96277
GB
2596 info->level_input_count + info->level_input_first,
2597 private->level_switch);
a5b36123
GB
2598 if (err < 0)
2599 return err;
2600 }
2601
2602 if (info->pad_input_count) {
2603 int err = scarlett2_usb_get_config(
2604 mixer, SCARLETT2_CONFIG_PAD_SWITCH,
2605 info->pad_input_count, private->pad_switch);
2606 if (err < 0)
2607 return err;
2608 }
2609
dbbd4f9e
GB
2610 if (info->air_input_count) {
2611 int err = scarlett2_usb_get_config(
2612 mixer, SCARLETT2_CONFIG_AIR_SWITCH,
2613 info->air_input_count, private->air_switch);
2614 if (err < 0)
2615 return err;
2616 }
2617
ae58a1a1
GB
2618 if (info->phantom_count) {
2619 int err = scarlett2_usb_get_config(
2620 mixer, SCARLETT2_CONFIG_PHANTOM_SWITCH,
2621 info->phantom_count, private->phantom_switch);
2622 if (err < 0)
2623 return err;
2624
2edc76dd
GB
2625 if (scarlett2_has_config_item(
2626 private,
2627 SCARLETT2_CONFIG_PHANTOM_PERSISTENCE)) {
2628 err = scarlett2_usb_get_config(
2629 mixer, SCARLETT2_CONFIG_PHANTOM_PERSISTENCE,
2630 1, &private->phantom_persistence);
2631 if (err < 0)
2632 return err;
2633 }
ae58a1a1
GB
2634 }
2635
a5b36123
GB
2636 return 0;
2637}
2638
9e4d5c1b
GB
2639static int scarlett2_level_enum_ctl_info(struct snd_kcontrol *kctl,
2640 struct snd_ctl_elem_info *uinfo)
2641{
2642 static const char *const values[2] = {
2643 "Line", "Inst"
2644 };
2645
2646 return snd_ctl_enum_info(uinfo, 1, 2, values);
2647}
2648
2649static int scarlett2_level_enum_ctl_get(struct snd_kcontrol *kctl,
2650 struct snd_ctl_elem_value *ucontrol)
2651{
2652 struct usb_mixer_elem_info *elem = kctl->private_data;
a5b36123
GB
2653 struct usb_mixer_interface *mixer = elem->head.mixer;
2654 struct scarlett2_data *private = mixer->private_data;
2fa96277
GB
2655 const struct scarlett2_device_info *info = private->info;
2656
2657 int index = elem->control + info->level_input_first;
50603a67 2658 int err = 0;
9e4d5c1b 2659
a5b36123 2660 mutex_lock(&private->data_mutex);
50603a67 2661
6a7508e6
GB
2662 if (private->hwdep_in_use) {
2663 err = -EBUSY;
2664 goto unlock;
2665 }
2666
50603a67
GB
2667 if (private->input_other_updated) {
2668 err = scarlett2_update_input_other(mixer);
2669 if (err < 0)
2670 goto unlock;
2671 }
2fa96277 2672 ucontrol->value.enumerated.item[0] = private->level_switch[index];
a5b36123 2673
50603a67
GB
2674unlock:
2675 mutex_unlock(&private->data_mutex);
2676 return err;
9e4d5c1b
GB
2677}
2678
2679static int scarlett2_level_enum_ctl_put(struct snd_kcontrol *kctl,
2680 struct snd_ctl_elem_value *ucontrol)
2681{
2682 struct usb_mixer_elem_info *elem = kctl->private_data;
2683 struct usb_mixer_interface *mixer = elem->head.mixer;
e46f2195 2684 struct scarlett2_data *private = mixer->private_data;
2fa96277 2685 const struct scarlett2_device_info *info = private->info;
9e4d5c1b 2686
2fa96277 2687 int index = elem->control + info->level_input_first;
9e4d5c1b
GB
2688 int oval, val, err = 0;
2689
2690 mutex_lock(&private->data_mutex);
2691
6a7508e6
GB
2692 if (private->hwdep_in_use) {
2693 err = -EBUSY;
2694 goto unlock;
2695 }
2696
9e4d5c1b 2697 oval = private->level_switch[index];
64c02a9d 2698 val = !!ucontrol->value.enumerated.item[0];
9e4d5c1b
GB
2699
2700 if (oval == val)
2701 goto unlock;
2702
2703 private->level_switch[index] = val;
2704
2705 /* Send switch change to the device */
2706 err = scarlett2_usb_set_config(mixer, SCARLETT2_CONFIG_LEVEL_SWITCH,
2707 index, val);
c5d8e008
GB
2708 if (err == 0)
2709 err = 1;
9e4d5c1b
GB
2710
2711unlock:
2712 mutex_unlock(&private->data_mutex);
2713 return err;
2714}
2715
2716static const struct snd_kcontrol_new scarlett2_level_enum_ctl = {
2717 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2718 .name = "",
2719 .info = scarlett2_level_enum_ctl_info,
2720 .get = scarlett2_level_enum_ctl_get,
2721 .put = scarlett2_level_enum_ctl_put,
2722};
2723
2724/*** Pad Switch Controls ***/
2725
2726static int scarlett2_pad_ctl_get(struct snd_kcontrol *kctl,
2727 struct snd_ctl_elem_value *ucontrol)
2728{
2729 struct usb_mixer_elem_info *elem = kctl->private_data;
a5b36123
GB
2730 struct usb_mixer_interface *mixer = elem->head.mixer;
2731 struct scarlett2_data *private = mixer->private_data;
50603a67 2732 int err = 0;
9e4d5c1b 2733
a5b36123 2734 mutex_lock(&private->data_mutex);
50603a67 2735
6a7508e6
GB
2736 if (private->hwdep_in_use) {
2737 err = -EBUSY;
2738 goto unlock;
2739 }
2740
50603a67
GB
2741 if (private->input_other_updated) {
2742 err = scarlett2_update_input_other(mixer);
2743 if (err < 0)
2744 goto unlock;
2745 }
64c02a9d 2746 ucontrol->value.integer.value[0] =
9e4d5c1b 2747 private->pad_switch[elem->control];
a5b36123 2748
50603a67
GB
2749unlock:
2750 mutex_unlock(&private->data_mutex);
2751 return err;
9e4d5c1b
GB
2752}
2753
2754static int scarlett2_pad_ctl_put(struct snd_kcontrol *kctl,
2755 struct snd_ctl_elem_value *ucontrol)
2756{
2757 struct usb_mixer_elem_info *elem = kctl->private_data;
2758 struct usb_mixer_interface *mixer = elem->head.mixer;
e46f2195 2759 struct scarlett2_data *private = mixer->private_data;
9e4d5c1b
GB
2760
2761 int index = elem->control;
2762 int oval, val, err = 0;
2763
2764 mutex_lock(&private->data_mutex);
2765
6a7508e6
GB
2766 if (private->hwdep_in_use) {
2767 err = -EBUSY;
2768 goto unlock;
2769 }
2770
9e4d5c1b
GB
2771 oval = private->pad_switch[index];
2772 val = !!ucontrol->value.integer.value[0];
2773
2774 if (oval == val)
2775 goto unlock;
2776
2777 private->pad_switch[index] = val;
2778
2779 /* Send switch change to the device */
2780 err = scarlett2_usb_set_config(mixer, SCARLETT2_CONFIG_PAD_SWITCH,
2781 index, val);
c5d8e008
GB
2782 if (err == 0)
2783 err = 1;
9e4d5c1b
GB
2784
2785unlock:
2786 mutex_unlock(&private->data_mutex);
2787 return err;
2788}
2789
2790static const struct snd_kcontrol_new scarlett2_pad_ctl = {
2791 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2792 .name = "",
2793 .info = snd_ctl_boolean_mono_info,
2794 .get = scarlett2_pad_ctl_get,
2795 .put = scarlett2_pad_ctl_put,
2796};
2797
dbbd4f9e
GB
2798/*** Air Switch Controls ***/
2799
2800static int scarlett2_air_ctl_get(struct snd_kcontrol *kctl,
2801 struct snd_ctl_elem_value *ucontrol)
2802{
2803 struct usb_mixer_elem_info *elem = kctl->private_data;
2804 struct usb_mixer_interface *mixer = elem->head.mixer;
2805 struct scarlett2_data *private = mixer->private_data;
50603a67 2806 int err = 0;
dbbd4f9e
GB
2807
2808 mutex_lock(&private->data_mutex);
50603a67 2809
6a7508e6
GB
2810 if (private->hwdep_in_use) {
2811 err = -EBUSY;
2812 goto unlock;
2813 }
2814
50603a67
GB
2815 if (private->input_other_updated) {
2816 err = scarlett2_update_input_other(mixer);
2817 if (err < 0)
2818 goto unlock;
2819 }
dbbd4f9e 2820 ucontrol->value.integer.value[0] = private->air_switch[elem->control];
dbbd4f9e 2821
50603a67
GB
2822unlock:
2823 mutex_unlock(&private->data_mutex);
2824 return err;
dbbd4f9e
GB
2825}
2826
2827static int scarlett2_air_ctl_put(struct snd_kcontrol *kctl,
2828 struct snd_ctl_elem_value *ucontrol)
2829{
2830 struct usb_mixer_elem_info *elem = kctl->private_data;
2831 struct usb_mixer_interface *mixer = elem->head.mixer;
2832 struct scarlett2_data *private = mixer->private_data;
2833
2834 int index = elem->control;
2835 int oval, val, err = 0;
2836
2837 mutex_lock(&private->data_mutex);
2838
6a7508e6
GB
2839 if (private->hwdep_in_use) {
2840 err = -EBUSY;
2841 goto unlock;
2842 }
2843
dbbd4f9e
GB
2844 oval = private->air_switch[index];
2845 val = !!ucontrol->value.integer.value[0];
2846
2847 if (oval == val)
2848 goto unlock;
2849
2850 private->air_switch[index] = val;
2851
2852 /* Send switch change to the device */
2853 err = scarlett2_usb_set_config(mixer, SCARLETT2_CONFIG_AIR_SWITCH,
2854 index, val);
459d2320
GB
2855 if (err == 0)
2856 err = 1;
dbbd4f9e
GB
2857
2858unlock:
2859 mutex_unlock(&private->data_mutex);
2860 return err;
2861}
2862
2863static const struct snd_kcontrol_new scarlett2_air_ctl = {
2864 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2865 .name = "",
2866 .info = snd_ctl_boolean_mono_info,
2867 .get = scarlett2_air_ctl_get,
2868 .put = scarlett2_air_ctl_put,
2869};
2870
ae58a1a1
GB
2871/*** Phantom Switch Controls ***/
2872
2873static int scarlett2_phantom_ctl_get(struct snd_kcontrol *kctl,
2874 struct snd_ctl_elem_value *ucontrol)
2875{
2876 struct usb_mixer_elem_info *elem = kctl->private_data;
2877 struct usb_mixer_interface *mixer = elem->head.mixer;
2878 struct scarlett2_data *private = mixer->private_data;
50603a67 2879 int err = 0;
ae58a1a1
GB
2880
2881 mutex_lock(&private->data_mutex);
50603a67 2882
6a7508e6
GB
2883 if (private->hwdep_in_use) {
2884 err = -EBUSY;
2885 goto unlock;
2886 }
2887
50603a67
GB
2888 if (private->input_other_updated) {
2889 err = scarlett2_update_input_other(mixer);
2890 if (err < 0)
2891 goto unlock;
2892 }
ae58a1a1
GB
2893 ucontrol->value.integer.value[0] =
2894 private->phantom_switch[elem->control];
ae58a1a1 2895
50603a67
GB
2896unlock:
2897 mutex_unlock(&private->data_mutex);
2898 return err;
ae58a1a1
GB
2899}
2900
2901static int scarlett2_phantom_ctl_put(struct snd_kcontrol *kctl,
2902 struct snd_ctl_elem_value *ucontrol)
2903{
2904 struct usb_mixer_elem_info *elem = kctl->private_data;
2905 struct usb_mixer_interface *mixer = elem->head.mixer;
2906 struct scarlett2_data *private = mixer->private_data;
2907
2908 int index = elem->control;
2909 int oval, val, err = 0;
2910
2911 mutex_lock(&private->data_mutex);
2912
6a7508e6
GB
2913 if (private->hwdep_in_use) {
2914 err = -EBUSY;
2915 goto unlock;
2916 }
2917
ae58a1a1
GB
2918 oval = private->phantom_switch[index];
2919 val = !!ucontrol->value.integer.value[0];
2920
2921 if (oval == val)
2922 goto unlock;
2923
2924 private->phantom_switch[index] = val;
2925
2926 /* Send switch change to the device */
2927 err = scarlett2_usb_set_config(mixer, SCARLETT2_CONFIG_PHANTOM_SWITCH,
2928 index, val);
459d2320
GB
2929 if (err == 0)
2930 err = 1;
ae58a1a1
GB
2931
2932unlock:
2933 mutex_unlock(&private->data_mutex);
2934 return err;
2935}
2936
2937static const struct snd_kcontrol_new scarlett2_phantom_ctl = {
2938 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2939 .name = "",
2940 .info = snd_ctl_boolean_mono_info,
2941 .get = scarlett2_phantom_ctl_get,
2942 .put = scarlett2_phantom_ctl_put,
2943};
2944
2945/*** Phantom Persistence Control ***/
2946
2947static int scarlett2_phantom_persistence_ctl_get(
2948 struct snd_kcontrol *kctl, struct snd_ctl_elem_value *ucontrol)
2949{
2950 struct usb_mixer_elem_info *elem = kctl->private_data;
2951 struct scarlett2_data *private = elem->head.mixer->private_data;
2952
2953 ucontrol->value.integer.value[0] = private->phantom_persistence;
2954 return 0;
2955}
2956
2957static int scarlett2_phantom_persistence_ctl_put(
2958 struct snd_kcontrol *kctl, struct snd_ctl_elem_value *ucontrol)
2959{
2960 struct usb_mixer_elem_info *elem = kctl->private_data;
2961 struct usb_mixer_interface *mixer = elem->head.mixer;
2962 struct scarlett2_data *private = mixer->private_data;
2963
2964 int index = elem->control;
2965 int oval, val, err = 0;
2966
2967 mutex_lock(&private->data_mutex);
2968
6a7508e6
GB
2969 if (private->hwdep_in_use) {
2970 err = -EBUSY;
2971 goto unlock;
2972 }
2973
ae58a1a1
GB
2974 oval = private->phantom_persistence;
2975 val = !!ucontrol->value.integer.value[0];
2976
2977 if (oval == val)
2978 goto unlock;
2979
2980 private->phantom_persistence = val;
2981
2982 /* Send switch change to the device */
2983 err = scarlett2_usb_set_config(
2984 mixer, SCARLETT2_CONFIG_PHANTOM_PERSISTENCE, index, val);
459d2320
GB
2985 if (err == 0)
2986 err = 1;
ae58a1a1
GB
2987
2988unlock:
2989 mutex_unlock(&private->data_mutex);
2990 return err;
2991}
2992
2993static const struct snd_kcontrol_new scarlett2_phantom_persistence_ctl = {
2994 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2995 .name = "",
2996 .info = snd_ctl_boolean_mono_info,
2997 .get = scarlett2_phantom_persistence_ctl_get,
2998 .put = scarlett2_phantom_persistence_ctl_put,
2999};
3000
6ef9fa4a
GB
3001/*** Direct Monitor Control ***/
3002
3003static int scarlett2_update_monitor_other(struct usb_mixer_interface *mixer)
3004{
3005 struct scarlett2_data *private = mixer->private_data;
3006 const struct scarlett2_device_info *info = private->info;
e914d843
GB
3007 int err;
3008
d5bda7e0
GB
3009 /* monitor_other_enable[0] enables speaker switching
3010 * monitor_other_enable[1] enables talkback
3011 */
e914d843
GB
3012 u8 monitor_other_enable[2];
3013
d5bda7e0
GB
3014 /* monitor_other_switch[0] activates the alternate speakers
3015 * monitor_other_switch[1] activates talkback
3016 */
e914d843 3017 u8 monitor_other_switch[2];
6ef9fa4a
GB
3018
3019 private->monitor_other_updated = 0;
3020
3021 if (info->direct_monitor)
3022 return scarlett2_usb_get_config(
3023 mixer, SCARLETT2_CONFIG_DIRECT_MONITOR,
3024 1, &private->direct_monitor_switch);
3025
d5bda7e0
GB
3026 /* if it doesn't do speaker switching then it also doesn't do
3027 * talkback
3028 */
e914d843
GB
3029 if (!info->has_speaker_switching)
3030 return 0;
3031
3032 err = scarlett2_usb_get_config(
3033 mixer, SCARLETT2_CONFIG_MONITOR_OTHER_ENABLE,
3034 2, monitor_other_enable);
3035 if (err < 0)
3036 return err;
3037
3038 err = scarlett2_usb_get_config(
3039 mixer, SCARLETT2_CONFIG_MONITOR_OTHER_SWITCH,
3040 2, monitor_other_switch);
3041 if (err < 0)
3042 return err;
3043
3044 if (!monitor_other_enable[0])
3045 private->speaker_switching_switch = 0;
3046 else
3047 private->speaker_switching_switch = monitor_other_switch[0] + 1;
3048
d5bda7e0
GB
3049 if (info->has_talkback) {
3050 const int (*port_count)[SCARLETT2_PORT_DIRNS] =
3051 info->port_count;
3052 int num_mixes =
3053 port_count[SCARLETT2_PORT_TYPE_MIX][SCARLETT2_PORT_IN];
3054 u16 bitmap;
3055 int i;
3056
3057 if (!monitor_other_enable[1])
3058 private->talkback_switch = 0;
3059 else
3060 private->talkback_switch = monitor_other_switch[1] + 1;
3061
3062 err = scarlett2_usb_get_config(mixer,
3063 SCARLETT2_CONFIG_TALKBACK_MAP,
3064 1, &bitmap);
8ec59ac3
TI
3065 if (err < 0)
3066 return err;
d5bda7e0
GB
3067 for (i = 0; i < num_mixes; i++, bitmap >>= 1)
3068 private->talkback_map[i] = bitmap & 1;
3069 }
3070
6ef9fa4a
GB
3071 return 0;
3072}
3073
3074static int scarlett2_direct_monitor_ctl_get(
3075 struct snd_kcontrol *kctl, struct snd_ctl_elem_value *ucontrol)
3076{
3077 struct usb_mixer_elem_info *elem = kctl->private_data;
3078 struct usb_mixer_interface *mixer = elem->head.mixer;
3079 struct scarlett2_data *private = elem->head.mixer->private_data;
50603a67 3080 int err = 0;
6ef9fa4a
GB
3081
3082 mutex_lock(&private->data_mutex);
50603a67 3083
6a7508e6
GB
3084 if (private->hwdep_in_use) {
3085 err = -EBUSY;
3086 goto unlock;
3087 }
3088
50603a67
GB
3089 if (private->monitor_other_updated) {
3090 err = scarlett2_update_monitor_other(mixer);
3091 if (err < 0)
3092 goto unlock;
3093 }
6ef9fa4a 3094 ucontrol->value.enumerated.item[0] = private->direct_monitor_switch;
6ef9fa4a 3095
50603a67
GB
3096unlock:
3097 mutex_unlock(&private->data_mutex);
3098 return err;
6ef9fa4a
GB
3099}
3100
3101static int scarlett2_direct_monitor_ctl_put(
3102 struct snd_kcontrol *kctl, struct snd_ctl_elem_value *ucontrol)
3103{
3104 struct usb_mixer_elem_info *elem = kctl->private_data;
3105 struct usb_mixer_interface *mixer = elem->head.mixer;
3106 struct scarlett2_data *private = mixer->private_data;
3107
3108 int index = elem->control;
3109 int oval, val, err = 0;
3110
3111 mutex_lock(&private->data_mutex);
3112
6a7508e6
GB
3113 if (private->hwdep_in_use) {
3114 err = -EBUSY;
3115 goto unlock;
3116 }
3117
6ef9fa4a
GB
3118 oval = private->direct_monitor_switch;
3119 val = min(ucontrol->value.enumerated.item[0], 2U);
3120
3121 if (oval == val)
3122 goto unlock;
3123
3124 private->direct_monitor_switch = val;
3125
3126 /* Send switch change to the device */
3127 err = scarlett2_usb_set_config(
3128 mixer, SCARLETT2_CONFIG_DIRECT_MONITOR, index, val);
459d2320
GB
3129 if (err == 0)
3130 err = 1;
6ef9fa4a
GB
3131
3132unlock:
3133 mutex_unlock(&private->data_mutex);
3134 return err;
3135}
3136
3137static int scarlett2_direct_monitor_stereo_enum_ctl_info(
3138 struct snd_kcontrol *kctl, struct snd_ctl_elem_info *uinfo)
3139{
3140 static const char *const values[3] = {
3141 "Off", "Mono", "Stereo"
3142 };
3143
3144 return snd_ctl_enum_info(uinfo, 1, 3, values);
3145}
3146
3147/* Direct Monitor for Solo is mono-only and only needs a boolean control
3148 * Direct Monitor for 2i2 is selectable between Off/Mono/Stereo
3149 */
3150static const struct snd_kcontrol_new scarlett2_direct_monitor_ctl[2] = {
3151 {
3152 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3153 .name = "",
3154 .info = snd_ctl_boolean_mono_info,
3155 .get = scarlett2_direct_monitor_ctl_get,
3156 .put = scarlett2_direct_monitor_ctl_put,
3157 },
3158 {
3159 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3160 .name = "",
3161 .info = scarlett2_direct_monitor_stereo_enum_ctl_info,
3162 .get = scarlett2_direct_monitor_ctl_get,
3163 .put = scarlett2_direct_monitor_ctl_put,
3164 }
3165};
3166
3167static int scarlett2_add_direct_monitor_ctl(struct usb_mixer_interface *mixer)
3168{
3169 struct scarlett2_data *private = mixer->private_data;
3170 const struct scarlett2_device_info *info = private->info;
d3a4f784 3171 const char *s;
6ef9fa4a
GB
3172
3173 if (!info->direct_monitor)
3174 return 0;
3175
d3a4f784
GB
3176 s = info->direct_monitor == 1
3177 ? "Direct Monitor Playback Switch"
3178 : "Direct Monitor Playback Enum";
3179
6ef9fa4a
GB
3180 return scarlett2_add_new_ctl(
3181 mixer, &scarlett2_direct_monitor_ctl[info->direct_monitor - 1],
d3a4f784 3182 0, 1, s, &private->direct_monitor_ctl);
6ef9fa4a
GB
3183}
3184
e914d843
GB
3185/*** Speaker Switching Control ***/
3186
3187static int scarlett2_speaker_switch_enum_ctl_info(
3188 struct snd_kcontrol *kctl, struct snd_ctl_elem_info *uinfo)
3189{
3190 static const char *const values[3] = {
3191 "Off", "Main", "Alt"
3192 };
3193
3194 return snd_ctl_enum_info(uinfo, 1, 3, values);
3195}
3196
3197static int scarlett2_speaker_switch_enum_ctl_get(
3198 struct snd_kcontrol *kctl, struct snd_ctl_elem_value *ucontrol)
3199{
3200 struct usb_mixer_elem_info *elem = kctl->private_data;
3201 struct usb_mixer_interface *mixer = elem->head.mixer;
3202 struct scarlett2_data *private = mixer->private_data;
50603a67 3203 int err = 0;
e914d843
GB
3204
3205 mutex_lock(&private->data_mutex);
50603a67 3206
6a7508e6
GB
3207 if (private->hwdep_in_use) {
3208 err = -EBUSY;
3209 goto unlock;
3210 }
3211
50603a67
GB
3212 if (private->monitor_other_updated) {
3213 err = scarlett2_update_monitor_other(mixer);
3214 if (err < 0)
3215 goto unlock;
3216 }
e914d843 3217 ucontrol->value.enumerated.item[0] = private->speaker_switching_switch;
e914d843 3218
50603a67
GB
3219unlock:
3220 mutex_unlock(&private->data_mutex);
3221 return err;
e914d843
GB
3222}
3223
3224/* when speaker switching gets enabled, switch the main/alt speakers
3225 * to HW volume and disable those controls
3226 */
459d2320 3227static int scarlett2_speaker_switch_enable(struct usb_mixer_interface *mixer)
e914d843
GB
3228{
3229 struct snd_card *card = mixer->chip->card;
3230 struct scarlett2_data *private = mixer->private_data;
459d2320 3231 int i, err;
e914d843
GB
3232
3233 for (i = 0; i < 4; i++) {
3234 int index = line_out_remap(private, i);
3235
3236 /* switch the main/alt speakers to HW volume */
459d2320
GB
3237 if (!private->vol_sw_hw_switch[index]) {
3238 err = scarlett2_sw_hw_change(private->mixer, i, 1);
3239 if (err < 0)
3240 return err;
3241 }
e914d843
GB
3242
3243 /* disable the line out SW/HW switch */
3244 scarlett2_sw_hw_ctl_ro(private, i);
2b8b12be
GB
3245 snd_ctl_notify(card,
3246 SNDRV_CTL_EVENT_MASK_VALUE |
3247 SNDRV_CTL_EVENT_MASK_INFO,
e914d843
GB
3248 &private->sw_hw_ctls[i]->id);
3249 }
3250
3251 /* when the next monitor-other notify comes in, update the mux
3252 * configuration
3253 */
3254 private->speaker_switching_switched = 1;
459d2320
GB
3255
3256 return 0;
e914d843
GB
3257}
3258
3259/* when speaker switching gets disabled, reenable the hw/sw controls
3260 * and invalidate the routing
3261 */
3262static void scarlett2_speaker_switch_disable(struct usb_mixer_interface *mixer)
3263{
3264 struct snd_card *card = mixer->chip->card;
3265 struct scarlett2_data *private = mixer->private_data;
3266 int i;
3267
3268 /* enable the line out SW/HW switch */
3269 for (i = 0; i < 4; i++) {
3270 scarlett2_sw_hw_ctl_rw(private, i);
3271 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_INFO,
3272 &private->sw_hw_ctls[i]->id);
3273 }
3274
3275 /* when the next monitor-other notify comes in, update the mux
3276 * configuration
3277 */
3278 private->speaker_switching_switched = 1;
3279}
3280
3281static int scarlett2_speaker_switch_enum_ctl_put(
3282 struct snd_kcontrol *kctl, struct snd_ctl_elem_value *ucontrol)
3283{
3284 struct usb_mixer_elem_info *elem = kctl->private_data;
3285 struct usb_mixer_interface *mixer = elem->head.mixer;
3286 struct scarlett2_data *private = mixer->private_data;
3287
3288 int oval, val, err = 0;
3289
3290 mutex_lock(&private->data_mutex);
3291
6a7508e6
GB
3292 if (private->hwdep_in_use) {
3293 err = -EBUSY;
3294 goto unlock;
3295 }
3296
e914d843
GB
3297 oval = private->speaker_switching_switch;
3298 val = min(ucontrol->value.enumerated.item[0], 2U);
3299
3300 if (oval == val)
3301 goto unlock;
3302
3303 private->speaker_switching_switch = val;
3304
3305 /* enable/disable speaker switching */
3306 err = scarlett2_usb_set_config(
3307 mixer, SCARLETT2_CONFIG_MONITOR_OTHER_ENABLE,
3308 0, !!val);
3309 if (err < 0)
3310 goto unlock;
3311
3312 /* if speaker switching is enabled, select main or alt */
3313 err = scarlett2_usb_set_config(
3314 mixer, SCARLETT2_CONFIG_MONITOR_OTHER_SWITCH,
3315 0, val == 2);
3316 if (err < 0)
3317 goto unlock;
3318
3319 /* update controls if speaker switching gets enabled or disabled */
3320 if (!oval && val)
459d2320 3321 err = scarlett2_speaker_switch_enable(mixer);
e914d843
GB
3322 else if (oval && !val)
3323 scarlett2_speaker_switch_disable(mixer);
3324
459d2320
GB
3325 if (err == 0)
3326 err = 1;
3327
e914d843
GB
3328unlock:
3329 mutex_unlock(&private->data_mutex);
3330 return err;
3331}
3332
3333static const struct snd_kcontrol_new scarlett2_speaker_switch_enum_ctl = {
3334 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3335 .name = "",
3336 .info = scarlett2_speaker_switch_enum_ctl_info,
3337 .get = scarlett2_speaker_switch_enum_ctl_get,
3338 .put = scarlett2_speaker_switch_enum_ctl_put,
3339};
3340
3341static int scarlett2_add_speaker_switch_ctl(
3342 struct usb_mixer_interface *mixer)
3343{
3344 struct scarlett2_data *private = mixer->private_data;
3345 const struct scarlett2_device_info *info = private->info;
3346
3347 if (!info->has_speaker_switching)
3348 return 0;
3349
3350 return scarlett2_add_new_ctl(
3351 mixer, &scarlett2_speaker_switch_enum_ctl,
3352 0, 1, "Speaker Switching Playback Enum",
3353 &private->speaker_switching_ctl);
3354}
3355
d5bda7e0
GB
3356/*** Talkback and Talkback Map Controls ***/
3357
3358static int scarlett2_talkback_enum_ctl_info(
3359 struct snd_kcontrol *kctl, struct snd_ctl_elem_info *uinfo)
3360{
3361 static const char *const values[3] = {
3362 "Disabled", "Off", "On"
3363 };
3364
3365 return snd_ctl_enum_info(uinfo, 1, 3, values);
3366}
3367
3368static int scarlett2_talkback_enum_ctl_get(
3369 struct snd_kcontrol *kctl, struct snd_ctl_elem_value *ucontrol)
3370{
3371 struct usb_mixer_elem_info *elem = kctl->private_data;
3372 struct usb_mixer_interface *mixer = elem->head.mixer;
3373 struct scarlett2_data *private = mixer->private_data;
50603a67 3374 int err = 0;
d5bda7e0
GB
3375
3376 mutex_lock(&private->data_mutex);
50603a67 3377
6a7508e6
GB
3378 if (private->hwdep_in_use) {
3379 err = -EBUSY;
3380 goto unlock;
3381 }
3382
50603a67
GB
3383 if (private->monitor_other_updated) {
3384 err = scarlett2_update_monitor_other(mixer);
3385 if (err < 0)
3386 goto unlock;
3387 }
d5bda7e0 3388 ucontrol->value.enumerated.item[0] = private->talkback_switch;
d5bda7e0 3389
50603a67
GB
3390unlock:
3391 mutex_unlock(&private->data_mutex);
3392 return err;
d5bda7e0
GB
3393}
3394
3395static int scarlett2_talkback_enum_ctl_put(
3396 struct snd_kcontrol *kctl, struct snd_ctl_elem_value *ucontrol)
3397{
3398 struct usb_mixer_elem_info *elem = kctl->private_data;
3399 struct usb_mixer_interface *mixer = elem->head.mixer;
3400 struct scarlett2_data *private = mixer->private_data;
3401
3402 int oval, val, err = 0;
3403
3404 mutex_lock(&private->data_mutex);
3405
6a7508e6
GB
3406 if (private->hwdep_in_use) {
3407 err = -EBUSY;
3408 goto unlock;
3409 }
3410
d5bda7e0
GB
3411 oval = private->talkback_switch;
3412 val = min(ucontrol->value.enumerated.item[0], 2U);
3413
3414 if (oval == val)
3415 goto unlock;
3416
3417 private->talkback_switch = val;
3418
3419 /* enable/disable talkback */
3420 err = scarlett2_usb_set_config(
3421 mixer, SCARLETT2_CONFIG_MONITOR_OTHER_ENABLE,
3422 1, !!val);
3423 if (err < 0)
3424 goto unlock;
3425
3426 /* if talkback is enabled, select main or alt */
3427 err = scarlett2_usb_set_config(
3428 mixer, SCARLETT2_CONFIG_MONITOR_OTHER_SWITCH,
3429 1, val == 2);
459d2320
GB
3430 if (err == 0)
3431 err = 1;
d5bda7e0
GB
3432
3433unlock:
3434 mutex_unlock(&private->data_mutex);
3435 return err;
3436}
3437
3438static const struct snd_kcontrol_new scarlett2_talkback_enum_ctl = {
3439 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3440 .name = "",
3441 .info = scarlett2_talkback_enum_ctl_info,
3442 .get = scarlett2_talkback_enum_ctl_get,
3443 .put = scarlett2_talkback_enum_ctl_put,
3444};
3445
3446static int scarlett2_talkback_map_ctl_get(
3447 struct snd_kcontrol *kctl, struct snd_ctl_elem_value *ucontrol)
3448{
3449 struct usb_mixer_elem_info *elem = kctl->private_data;
3450 struct usb_mixer_interface *mixer = elem->head.mixer;
3451 struct scarlett2_data *private = mixer->private_data;
3452 int index = elem->control;
3453
3454 ucontrol->value.integer.value[0] = private->talkback_map[index];
3455
3456 return 0;
3457}
3458
3459static int scarlett2_talkback_map_ctl_put(
3460 struct snd_kcontrol *kctl, struct snd_ctl_elem_value *ucontrol)
3461{
3462 struct usb_mixer_elem_info *elem = kctl->private_data;
3463 struct usb_mixer_interface *mixer = elem->head.mixer;
3464 struct scarlett2_data *private = mixer->private_data;
3465 const int (*port_count)[SCARLETT2_PORT_DIRNS] =
3466 private->info->port_count;
3467 int num_mixes = port_count[SCARLETT2_PORT_TYPE_MIX][SCARLETT2_PORT_IN];
3468
3469 int index = elem->control;
3470 int oval, val, err = 0, i;
3471 u16 bitmap = 0;
3472
3473 mutex_lock(&private->data_mutex);
3474
6a7508e6
GB
3475 if (private->hwdep_in_use) {
3476 err = -EBUSY;
3477 goto unlock;
3478 }
3479
d5bda7e0
GB
3480 oval = private->talkback_map[index];
3481 val = !!ucontrol->value.integer.value[0];
3482
3483 if (oval == val)
3484 goto unlock;
3485
3486 private->talkback_map[index] = val;
3487
3488 for (i = 0; i < num_mixes; i++)
3489 bitmap |= private->talkback_map[i] << i;
3490
3491 /* Send updated bitmap to the device */
3492 err = scarlett2_usb_set_config(mixer, SCARLETT2_CONFIG_TALKBACK_MAP,
3493 0, bitmap);
459d2320
GB
3494 if (err == 0)
3495 err = 1;
d5bda7e0
GB
3496
3497unlock:
3498 mutex_unlock(&private->data_mutex);
3499 return err;
3500}
3501
3502static const struct snd_kcontrol_new scarlett2_talkback_map_ctl = {
3503 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3504 .name = "",
3505 .info = snd_ctl_boolean_mono_info,
3506 .get = scarlett2_talkback_map_ctl_get,
3507 .put = scarlett2_talkback_map_ctl_put,
3508};
3509
3510static int scarlett2_add_talkback_ctls(
3511 struct usb_mixer_interface *mixer)
3512{
3513 struct scarlett2_data *private = mixer->private_data;
3514 const struct scarlett2_device_info *info = private->info;
3515 const int (*port_count)[SCARLETT2_PORT_DIRNS] = info->port_count;
3516 int num_mixes = port_count[SCARLETT2_PORT_TYPE_MIX][SCARLETT2_PORT_IN];
3517 int err, i;
3518 char s[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
3519
3520 if (!info->has_talkback)
3521 return 0;
3522
3523 err = scarlett2_add_new_ctl(
3524 mixer, &scarlett2_talkback_enum_ctl,
3525 0, 1, "Talkback Playback Enum",
3526 &private->talkback_ctl);
3527 if (err < 0)
3528 return err;
3529
3530 for (i = 0; i < num_mixes; i++) {
3531 snprintf(s, sizeof(s),
3532 "Talkback Mix %c Playback Switch", i + 'A');
3533 err = scarlett2_add_new_ctl(mixer, &scarlett2_talkback_map_ctl,
3534 i, 1, s, NULL);
3535 if (err < 0)
3536 return err;
3537 }
3538
3539 return 0;
3540}
3541
dbd82c05 3542/*** Dim/Mute Controls ***/
9e4d5c1b 3543
dbd82c05
GB
3544static int scarlett2_dim_mute_ctl_get(struct snd_kcontrol *kctl,
3545 struct snd_ctl_elem_value *ucontrol)
9e4d5c1b
GB
3546{
3547 struct usb_mixer_elem_info *elem = kctl->private_data;
3548 struct usb_mixer_interface *mixer = elem->head.mixer;
e46f2195 3549 struct scarlett2_data *private = mixer->private_data;
50603a67 3550 int err = 0;
9e4d5c1b 3551
9b5ddea9 3552 mutex_lock(&private->data_mutex);
9e4d5c1b 3553
6a7508e6
GB
3554 if (private->hwdep_in_use) {
3555 err = -EBUSY;
3556 goto unlock;
3557 }
3558
50603a67
GB
3559 if (private->vol_updated) {
3560 err = scarlett2_update_volumes(mixer);
3561 if (err < 0)
3562 goto unlock;
3563 }
64c02a9d 3564 ucontrol->value.integer.value[0] = private->dim_mute[elem->control];
50603a67
GB
3565
3566unlock:
3567 mutex_unlock(&private->data_mutex);
3568 return err;
9e4d5c1b
GB
3569}
3570
dbd82c05
GB
3571static int scarlett2_dim_mute_ctl_put(struct snd_kcontrol *kctl,
3572 struct snd_ctl_elem_value *ucontrol)
9e4d5c1b
GB
3573{
3574 struct usb_mixer_elem_info *elem = kctl->private_data;
3575 struct usb_mixer_interface *mixer = elem->head.mixer;
e46f2195 3576 struct scarlett2_data *private = mixer->private_data;
0c88f9db 3577 const struct scarlett2_device_info *info = private->info;
e2cc91ac 3578 const int (*port_count)[SCARLETT2_PORT_DIRNS] = info->port_count;
0c88f9db 3579 int num_line_out =
e2cc91ac 3580 port_count[SCARLETT2_PORT_TYPE_ANALOGUE][SCARLETT2_PORT_OUT];
9e4d5c1b
GB
3581
3582 int index = elem->control;
0c88f9db 3583 int oval, val, err = 0, i;
9e4d5c1b
GB
3584
3585 mutex_lock(&private->data_mutex);
3586
6a7508e6
GB
3587 if (private->hwdep_in_use) {
3588 err = -EBUSY;
3589 goto unlock;
3590 }
3591
dbd82c05 3592 oval = private->dim_mute[index];
9e4d5c1b
GB
3593 val = !!ucontrol->value.integer.value[0];
3594
3595 if (oval == val)
3596 goto unlock;
3597
dbd82c05 3598 private->dim_mute[index] = val;
9e4d5c1b
GB
3599
3600 /* Send switch change to the device */
dbd82c05 3601 err = scarlett2_usb_set_config(mixer, SCARLETT2_CONFIG_DIM_MUTE,
9e4d5c1b 3602 index, val);
c5d8e008
GB
3603 if (err == 0)
3604 err = 1;
9e4d5c1b 3605
0c88f9db 3606 if (index == SCARLETT2_BUTTON_MUTE)
9cfe1276
GB
3607 for (i = 0; i < num_line_out; i++) {
3608 int line_index = line_out_remap(private, i);
3609
3610 if (private->vol_sw_hw_switch[line_index]) {
3611 private->mute_switch[line_index] = val;
0c88f9db 3612 snd_ctl_notify(mixer->chip->card,
2b8b12be 3613 SNDRV_CTL_EVENT_MASK_VALUE,
0c88f9db
GB
3614 &private->mute_ctls[i]->id);
3615 }
9cfe1276 3616 }
0c88f9db 3617
9e4d5c1b
GB
3618unlock:
3619 mutex_unlock(&private->data_mutex);
3620 return err;
3621}
3622
dbd82c05 3623static const struct snd_kcontrol_new scarlett2_dim_mute_ctl = {
9e4d5c1b
GB
3624 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3625 .name = "",
3626 .info = snd_ctl_boolean_mono_info,
dbd82c05
GB
3627 .get = scarlett2_dim_mute_ctl_get,
3628 .put = scarlett2_dim_mute_ctl_put
9e4d5c1b
GB
3629};
3630
3631/*** Create the analogue output controls ***/
3632
3633static int scarlett2_add_line_out_ctls(struct usb_mixer_interface *mixer)
3634{
e46f2195 3635 struct scarlett2_data *private = mixer->private_data;
9e4d5c1b 3636 const struct scarlett2_device_info *info = private->info;
e2cc91ac 3637 const int (*port_count)[SCARLETT2_PORT_DIRNS] = info->port_count;
9e4d5c1b 3638 int num_line_out =
e2cc91ac 3639 port_count[SCARLETT2_PORT_TYPE_ANALOGUE][SCARLETT2_PORT_OUT];
9e4d5c1b
GB
3640 int err, i;
3641 char s[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
3642
3643 /* Add R/O HW volume control */
3644 if (info->line_out_hw_vol) {
3645 snprintf(s, sizeof(s), "Master HW Playback Volume");
3646 err = scarlett2_add_new_ctl(mixer,
3647 &scarlett2_master_volume_ctl,
3648 0, 1, s, &private->master_vol_ctl);
3649 if (err < 0)
3650 return err;
3651 }
3652
3653 /* Add volume controls */
3654 for (i = 0; i < num_line_out; i++) {
9cfe1276 3655 int index = line_out_remap(private, i);
9e4d5c1b
GB
3656
3657 /* Fader */
3658 if (info->line_out_descrs[i])
3659 snprintf(s, sizeof(s),
3660 "Line %02d (%s) Playback Volume",
3661 i + 1, info->line_out_descrs[i]);
3662 else
3663 snprintf(s, sizeof(s),
3664 "Line %02d Playback Volume",
3665 i + 1);
3666 err = scarlett2_add_new_ctl(mixer,
3667 &scarlett2_line_out_volume_ctl,
3668 i, 1, s, &private->vol_ctls[i]);
3669 if (err < 0)
3670 return err;
3671
0c88f9db
GB
3672 /* Mute Switch */
3673 snprintf(s, sizeof(s),
3674 "Line %02d Mute Playback Switch",
3675 i + 1);
3676 err = scarlett2_add_new_ctl(mixer,
3677 &scarlett2_mute_ctl,
3678 i, 1, s,
3679 &private->mute_ctls[i]);
3680 if (err < 0)
3681 return err;
3682
3683 /* Make the fader and mute controls read-only if the
3684 * SW/HW switch is set to HW
3685 */
9cfe1276 3686 if (private->vol_sw_hw_switch[index])
06250c89 3687 scarlett2_vol_ctl_set_writable(mixer, i, 0);
9e4d5c1b
GB
3688
3689 /* SW/HW Switch */
3690 if (info->line_out_hw_vol) {
3691 snprintf(s, sizeof(s),
3692 "Line Out %02d Volume Control Playback Enum",
3693 i + 1);
3694 err = scarlett2_add_new_ctl(mixer,
3695 &scarlett2_sw_hw_enum_ctl,
f02da653
GB
3696 i, 1, s,
3697 &private->sw_hw_ctls[i]);
9e4d5c1b
GB
3698 if (err < 0)
3699 return err;
e914d843
GB
3700
3701 /* Make the switch read-only if the line is
3702 * involved in speaker switching
3703 */
3704 if (private->speaker_switching_switch && i < 4)
3705 scarlett2_sw_hw_ctl_ro(private, i);
9e4d5c1b
GB
3706 }
3707 }
3708
dbd82c05 3709 /* Add dim/mute controls */
03bdbcf0 3710 if (info->line_out_hw_vol)
dbd82c05 3711 for (i = 0; i < SCARLETT2_DIM_MUTE_COUNT; i++) {
03bdbcf0 3712 err = scarlett2_add_new_ctl(
dbd82c05
GB
3713 mixer, &scarlett2_dim_mute_ctl,
3714 i, 1, scarlett2_dim_mute_names[i],
3715 &private->dim_mute_ctls[i]);
03bdbcf0
GB
3716 if (err < 0)
3717 return err;
3718 }
9e4d5c1b
GB
3719
3720 return 0;
3721}
3722
3723/*** Create the analogue input controls ***/
3724
3725static int scarlett2_add_line_in_ctls(struct usb_mixer_interface *mixer)
3726{
e46f2195 3727 struct scarlett2_data *private = mixer->private_data;
9e4d5c1b
GB
3728 const struct scarlett2_device_info *info = private->info;
3729 int err, i;
3730 char s[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
077e104e 3731 const char *fmt = "Line In %d %s Capture %s";
ae58a1a1 3732 const char *fmt2 = "Line In %d-%d %s Capture %s";
9e4d5c1b
GB
3733
3734 /* Add input level (line/inst) controls */
3735 for (i = 0; i < info->level_input_count; i++) {
2fa96277
GB
3736 snprintf(s, sizeof(s), fmt, i + 1 + info->level_input_first,
3737 "Level", "Enum");
9e4d5c1b 3738 err = scarlett2_add_new_ctl(mixer, &scarlett2_level_enum_ctl,
a5b36123 3739 i, 1, s, &private->level_ctls[i]);
9e4d5c1b
GB
3740 if (err < 0)
3741 return err;
3742 }
3743
3744 /* Add input pad controls */
3745 for (i = 0; i < info->pad_input_count; i++) {
077e104e 3746 snprintf(s, sizeof(s), fmt, i + 1, "Pad", "Switch");
9e4d5c1b 3747 err = scarlett2_add_new_ctl(mixer, &scarlett2_pad_ctl,
a5b36123 3748 i, 1, s, &private->pad_ctls[i]);
9e4d5c1b
GB
3749 if (err < 0)
3750 return err;
3751 }
3752
dbbd4f9e
GB
3753 /* Add input air controls */
3754 for (i = 0; i < info->air_input_count; i++) {
3755 snprintf(s, sizeof(s), fmt, i + 1, "Air", "Switch");
3756 err = scarlett2_add_new_ctl(mixer, &scarlett2_air_ctl,
3757 i, 1, s, &private->air_ctls[i]);
3758 if (err < 0)
3759 return err;
3760 }
3761
ae58a1a1
GB
3762 /* Add input phantom controls */
3763 if (info->inputs_per_phantom == 1) {
3764 for (i = 0; i < info->phantom_count; i++) {
aadb0330
PU
3765 scnprintf(s, sizeof(s), fmt, i + 1,
3766 "Phantom Power", "Switch");
ae58a1a1
GB
3767 err = scarlett2_add_new_ctl(
3768 mixer, &scarlett2_phantom_ctl,
3769 i, 1, s, &private->phantom_ctls[i]);
3770 if (err < 0)
3771 return err;
3772 }
3773 } else if (info->inputs_per_phantom > 1) {
3774 for (i = 0; i < info->phantom_count; i++) {
3775 int from = i * info->inputs_per_phantom + 1;
3776 int to = (i + 1) * info->inputs_per_phantom;
3777
78bd8f51
TI
3778 scnprintf(s, sizeof(s), fmt2, from, to,
3779 "Phantom Power", "Switch");
ae58a1a1
GB
3780 err = scarlett2_add_new_ctl(
3781 mixer, &scarlett2_phantom_ctl,
3782 i, 1, s, &private->phantom_ctls[i]);
3783 if (err < 0)
3784 return err;
3785 }
3786 }
2edc76dd
GB
3787 if (info->phantom_count &&
3788 scarlett2_has_config_item(private,
3789 SCARLETT2_CONFIG_PHANTOM_PERSISTENCE)) {
ae58a1a1
GB
3790 err = scarlett2_add_new_ctl(
3791 mixer, &scarlett2_phantom_persistence_ctl, 0, 1,
3792 "Phantom Power Persistence Capture Switch", NULL);
3793 if (err < 0)
3794 return err;
3795 }
3796
9e4d5c1b
GB
3797 return 0;
3798}
3799
3800/*** Mixer Volume Controls ***/
3801
3802static int scarlett2_mixer_ctl_info(struct snd_kcontrol *kctl,
3803 struct snd_ctl_elem_info *uinfo)
3804{
3805 struct usb_mixer_elem_info *elem = kctl->private_data;
3806
3807 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
3808 uinfo->count = elem->channels;
3809 uinfo->value.integer.min = 0;
3810 uinfo->value.integer.max = SCARLETT2_MIXER_MAX_VALUE;
3811 uinfo->value.integer.step = 1;
3812 return 0;
3813}
3814
3815static int scarlett2_mixer_ctl_get(struct snd_kcontrol *kctl,
3816 struct snd_ctl_elem_value *ucontrol)
3817{
3818 struct usb_mixer_elem_info *elem = kctl->private_data;
e46f2195 3819 struct scarlett2_data *private = elem->head.mixer->private_data;
9e4d5c1b
GB
3820
3821 ucontrol->value.integer.value[0] = private->mix[elem->control];
3822 return 0;
3823}
3824
3825static int scarlett2_mixer_ctl_put(struct snd_kcontrol *kctl,
3826 struct snd_ctl_elem_value *ucontrol)
3827{
3828 struct usb_mixer_elem_info *elem = kctl->private_data;
3829 struct usb_mixer_interface *mixer = elem->head.mixer;
e46f2195 3830 struct scarlett2_data *private = mixer->private_data;
9e4d5c1b 3831 const struct scarlett2_device_info *info = private->info;
e2cc91ac 3832 const int (*port_count)[SCARLETT2_PORT_DIRNS] = info->port_count;
9e4d5c1b 3833 int oval, val, num_mixer_in, mix_num, err = 0;
3eeb2a19 3834 int index = elem->control;
9e4d5c1b
GB
3835
3836 mutex_lock(&private->data_mutex);
3837
6a7508e6
GB
3838 if (private->hwdep_in_use) {
3839 err = -EBUSY;
3840 goto unlock;
3841 }
3842
3eeb2a19 3843 oval = private->mix[index];
04f8f053
GB
3844 val = clamp(ucontrol->value.integer.value[0],
3845 0L, (long)SCARLETT2_MIXER_MAX_VALUE);
e2cc91ac 3846 num_mixer_in = port_count[SCARLETT2_PORT_TYPE_MIX][SCARLETT2_PORT_OUT];
3eeb2a19 3847 mix_num = index / num_mixer_in;
9e4d5c1b
GB
3848
3849 if (oval == val)
3850 goto unlock;
3851
3eeb2a19 3852 private->mix[index] = val;
9e4d5c1b
GB
3853 err = scarlett2_usb_set_mix(mixer, mix_num);
3854 if (err == 0)
3855 err = 1;
3856
3857unlock:
3858 mutex_unlock(&private->data_mutex);
3859 return err;
3860}
3861
3862static const DECLARE_TLV_DB_MINMAX(
3863 db_scale_scarlett2_mixer,
3864 SCARLETT2_MIXER_MIN_DB * 100,
3865 SCARLETT2_MIXER_MAX_DB * 100
3866);
3867
3868static const struct snd_kcontrol_new scarlett2_mixer_ctl = {
3869 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3870 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE |
3871 SNDRV_CTL_ELEM_ACCESS_TLV_READ,
3872 .name = "",
3873 .info = scarlett2_mixer_ctl_info,
3874 .get = scarlett2_mixer_ctl_get,
3875 .put = scarlett2_mixer_ctl_put,
3876 .private_value = SCARLETT2_MIXER_MAX_DB, /* max value */
3877 .tlv = { .p = db_scale_scarlett2_mixer }
3878};
3879
3880static int scarlett2_add_mixer_ctls(struct usb_mixer_interface *mixer)
3881{
e46f2195 3882 struct scarlett2_data *private = mixer->private_data;
e2cc91ac
GB
3883 const struct scarlett2_device_info *info = private->info;
3884 const int (*port_count)[SCARLETT2_PORT_DIRNS] = info->port_count;
9e4d5c1b
GB
3885 int err, i, j;
3886 int index;
3887 char s[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
3888
e2cc91ac
GB
3889 int num_inputs =
3890 port_count[SCARLETT2_PORT_TYPE_MIX][SCARLETT2_PORT_OUT];
3891 int num_outputs =
3892 port_count[SCARLETT2_PORT_TYPE_MIX][SCARLETT2_PORT_IN];
9e4d5c1b 3893
b677b6c6 3894 for (i = 0, index = 0; i < num_outputs; i++)
9e4d5c1b
GB
3895 for (j = 0; j < num_inputs; j++, index++) {
3896 snprintf(s, sizeof(s),
3897 "Mix %c Input %02d Playback Volume",
3898 'A' + i, j + 1);
3899 err = scarlett2_add_new_ctl(mixer, &scarlett2_mixer_ctl,
3900 index, 1, s, NULL);
3901 if (err < 0)
3902 return err;
3903 }
9e4d5c1b
GB
3904
3905 return 0;
3906}
3907
3908/*** Mux Source Selection Controls ***/
3909
3910static int scarlett2_mux_src_enum_ctl_info(struct snd_kcontrol *kctl,
3911 struct snd_ctl_elem_info *uinfo)
3912{
3913 struct usb_mixer_elem_info *elem = kctl->private_data;
e46f2195 3914 struct scarlett2_data *private = elem->head.mixer->private_data;
e2cc91ac
GB
3915 const struct scarlett2_device_info *info = private->info;
3916 const int (*port_count)[SCARLETT2_PORT_DIRNS] = info->port_count;
9e4d5c1b
GB
3917 unsigned int item = uinfo->value.enumerated.item;
3918 int items = private->num_mux_srcs;
3919 int port_type;
3920
3921 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
3922 uinfo->count = elem->channels;
3923 uinfo->value.enumerated.items = items;
3924
3925 if (item >= items)
3926 item = uinfo->value.enumerated.item = items - 1;
3927
3928 for (port_type = 0;
3929 port_type < SCARLETT2_PORT_TYPE_COUNT;
3930 port_type++) {
e2cc91ac
GB
3931 if (item < port_count[port_type][SCARLETT2_PORT_IN]) {
3932 const struct scarlett2_port *port =
3933 &scarlett2_ports[port_type];
3934
9e4d5c1b 3935 sprintf(uinfo->value.enumerated.name,
e2cc91ac 3936 port->src_descr, item + port->src_num_offset);
9e4d5c1b
GB
3937 return 0;
3938 }
e2cc91ac 3939 item -= port_count[port_type][SCARLETT2_PORT_IN];
9e4d5c1b
GB
3940 }
3941
3942 return -EINVAL;
3943}
3944
3945static int scarlett2_mux_src_enum_ctl_get(struct snd_kcontrol *kctl,
3946 struct snd_ctl_elem_value *ucontrol)
3947{
3948 struct usb_mixer_elem_info *elem = kctl->private_data;
8df25eb0
GB
3949 struct usb_mixer_interface *mixer = elem->head.mixer;
3950 struct scarlett2_data *private = mixer->private_data;
2190b9ae 3951 int index = line_out_remap(private, elem->control);
50603a67 3952 int err = 0;
9e4d5c1b 3953
8df25eb0 3954 mutex_lock(&private->data_mutex);
50603a67 3955
6a7508e6
GB
3956 if (private->hwdep_in_use) {
3957 err = -EBUSY;
3958 goto unlock;
3959 }
3960
50603a67
GB
3961 if (private->mux_updated) {
3962 err = scarlett2_usb_get_mux(mixer);
3963 if (err < 0)
3964 goto unlock;
3965 }
9cfe1276 3966 ucontrol->value.enumerated.item[0] = private->mux[index];
8df25eb0 3967
50603a67
GB
3968unlock:
3969 mutex_unlock(&private->data_mutex);
3970 return err;
9e4d5c1b
GB
3971}
3972
3973static int scarlett2_mux_src_enum_ctl_put(struct snd_kcontrol *kctl,
3974 struct snd_ctl_elem_value *ucontrol)
3975{
3976 struct usb_mixer_elem_info *elem = kctl->private_data;
3977 struct usb_mixer_interface *mixer = elem->head.mixer;
e46f2195 3978 struct scarlett2_data *private = mixer->private_data;
2190b9ae 3979 int index = line_out_remap(private, elem->control);
9e4d5c1b
GB
3980 int oval, val, err = 0;
3981
3982 mutex_lock(&private->data_mutex);
3983
6a7508e6
GB
3984 if (private->hwdep_in_use) {
3985 err = -EBUSY;
3986 goto unlock;
3987 }
3988
9e4d5c1b 3989 oval = private->mux[index];
64c02a9d
GB
3990 val = min(ucontrol->value.enumerated.item[0],
3991 private->num_mux_srcs - 1U);
9e4d5c1b
GB
3992
3993 if (oval == val)
3994 goto unlock;
3995
3996 private->mux[index] = val;
3997 err = scarlett2_usb_set_mux(mixer);
3998 if (err == 0)
3999 err = 1;
4000
4001unlock:
4002 mutex_unlock(&private->data_mutex);
4003 return err;
4004}
4005
4006static const struct snd_kcontrol_new scarlett2_mux_src_enum_ctl = {
4007 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
4008 .name = "",
4009 .info = scarlett2_mux_src_enum_ctl_info,
4010 .get = scarlett2_mux_src_enum_ctl_get,
4011 .put = scarlett2_mux_src_enum_ctl_put,
4012};
4013
4014static int scarlett2_add_mux_enums(struct usb_mixer_interface *mixer)
4015{
e46f2195 4016 struct scarlett2_data *private = mixer->private_data;
e2cc91ac
GB
4017 const struct scarlett2_device_info *info = private->info;
4018 const int (*port_count)[SCARLETT2_PORT_DIRNS] = info->port_count;
9e4d5c1b
GB
4019 int port_type, channel, i;
4020
4021 for (i = 0, port_type = 0;
4022 port_type < SCARLETT2_PORT_TYPE_COUNT;
4023 port_type++) {
4024 for (channel = 0;
e2cc91ac 4025 channel < port_count[port_type][SCARLETT2_PORT_OUT];
9e4d5c1b
GB
4026 channel++, i++) {
4027 int err;
4028 char s[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
e2cc91ac
GB
4029 const char *const descr =
4030 scarlett2_ports[port_type].dst_descr;
9e4d5c1b
GB
4031
4032 snprintf(s, sizeof(s) - 5, descr, channel + 1);
4033 strcat(s, " Enum");
4034
4035 err = scarlett2_add_new_ctl(mixer,
4036 &scarlett2_mux_src_enum_ctl,
f02da653
GB
4037 i, 1, s,
4038 &private->mux_ctls[i]);
9e4d5c1b
GB
4039 if (err < 0)
4040 return err;
4041 }
4042 }
4043
4044 return 0;
4045}
4046
4047/*** Meter Controls ***/
4048
4049static int scarlett2_meter_ctl_info(struct snd_kcontrol *kctl,
4050 struct snd_ctl_elem_info *uinfo)
4051{
4052 struct usb_mixer_elem_info *elem = kctl->private_data;
4053
4054 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
4055 uinfo->count = elem->channels;
4056 uinfo->value.integer.min = 0;
4057 uinfo->value.integer.max = 4095;
4058 uinfo->value.integer.step = 1;
4059 return 0;
4060}
4061
4062static int scarlett2_meter_ctl_get(struct snd_kcontrol *kctl,
4063 struct snd_ctl_elem_value *ucontrol)
4064{
4065 struct usb_mixer_elem_info *elem = kctl->private_data;
3473185f
GB
4066 struct scarlett2_data *private = elem->head.mixer->private_data;
4067 u8 *meter_level_map = private->meter_level_map;
b126bbac 4068 u16 meter_levels[SCARLETT2_MAX_METERS];
9e4d5c1b
GB
4069 int i, err;
4070
993f7b42
GB
4071 mutex_lock(&private->data_mutex);
4072
6a7508e6
GB
4073 if (private->hwdep_in_use) {
4074 err = -EBUSY;
4075 goto unlock;
4076 }
4077
b126bbac
GB
4078 err = scarlett2_usb_get_meter_levels(elem->head.mixer, elem->channels,
4079 meter_levels);
9e4d5c1b 4080 if (err < 0)
993f7b42 4081 goto unlock;
9e4d5c1b 4082
3473185f
GB
4083 /* copy & translate from meter_levels[] using meter_level_map[] */
4084 for (i = 0; i < elem->channels; i++) {
4085 int idx = meter_level_map[i];
4086 int value;
4087
4088 if (idx == 255)
4089 value = 0;
4090 else
4091 value = meter_levels[idx];
4092
4093 ucontrol->value.integer.value[i] = value;
4094 }
9e4d5c1b 4095
993f7b42
GB
4096unlock:
4097 mutex_unlock(&private->data_mutex);
4098
4099 return err;
9e4d5c1b
GB
4100}
4101
4102static const struct snd_kcontrol_new scarlett2_meter_ctl = {
4103 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
4104 .access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE,
4105 .name = "",
4106 .info = scarlett2_meter_ctl_info,
4107 .get = scarlett2_meter_ctl_get
4108};
4109
4110static int scarlett2_add_meter_ctl(struct usb_mixer_interface *mixer)
4111{
b126bbac
GB
4112 struct scarlett2_data *private = mixer->private_data;
4113
2fa96277 4114 /* devices without a mixer also don't support reporting levels */
c13d43a8 4115 if (!scarlett2_has_mixer(private))
2fa96277
GB
4116 return 0;
4117
9e4d5c1b 4118 return scarlett2_add_new_ctl(mixer, &scarlett2_meter_ctl,
b126bbac 4119 0, private->num_mux_dsts,
9e4d5c1b
GB
4120 "Level Meter", NULL);
4121}
4122
303f204e
GB
4123/*** MSD Controls ***/
4124
4125static int scarlett2_msd_ctl_get(struct snd_kcontrol *kctl,
4126 struct snd_ctl_elem_value *ucontrol)
4127{
4128 struct usb_mixer_elem_info *elem = kctl->private_data;
4129 struct scarlett2_data *private = elem->head.mixer->private_data;
4130
4131 ucontrol->value.integer.value[0] = private->msd_switch;
4132 return 0;
4133}
4134
4135static int scarlett2_msd_ctl_put(struct snd_kcontrol *kctl,
4136 struct snd_ctl_elem_value *ucontrol)
4137{
4138 struct usb_mixer_elem_info *elem = kctl->private_data;
4139 struct usb_mixer_interface *mixer = elem->head.mixer;
4140 struct scarlett2_data *private = mixer->private_data;
4141
4142 int oval, val, err = 0;
4143
4144 mutex_lock(&private->data_mutex);
4145
6a7508e6
GB
4146 if (private->hwdep_in_use) {
4147 err = -EBUSY;
4148 goto unlock;
4149 }
4150
303f204e
GB
4151 oval = private->msd_switch;
4152 val = !!ucontrol->value.integer.value[0];
4153
4154 if (oval == val)
4155 goto unlock;
4156
4157 private->msd_switch = val;
4158
4159 /* Send switch change to the device */
4160 err = scarlett2_usb_set_config(mixer, SCARLETT2_CONFIG_MSD_SWITCH,
4161 0, val);
459d2320
GB
4162 if (err == 0)
4163 err = 1;
303f204e
GB
4164
4165unlock:
4166 mutex_unlock(&private->data_mutex);
4167 return err;
4168}
4169
4170static const struct snd_kcontrol_new scarlett2_msd_ctl = {
4171 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
4172 .name = "",
4173 .info = snd_ctl_boolean_mono_info,
4174 .get = scarlett2_msd_ctl_get,
4175 .put = scarlett2_msd_ctl_put,
4176};
4177
4178static int scarlett2_add_msd_ctl(struct usb_mixer_interface *mixer)
4179{
4180 struct scarlett2_data *private = mixer->private_data;
303f204e 4181
3a4e1afe 4182 if (!scarlett2_has_config_item(private, SCARLETT2_CONFIG_MSD_SWITCH))
303f204e
GB
4183 return 0;
4184
4185 /* If MSD mode is off, hide the switch by default */
4186 if (!private->msd_switch && !(mixer->chip->setup & SCARLETT2_MSD_ENABLE))
4187 return 0;
4188
4189 /* Add MSD control */
4190 return scarlett2_add_new_ctl(mixer, &scarlett2_msd_ctl,
cdf72837 4191 0, 1, "MSD Mode Switch", NULL);
303f204e
GB
4192}
4193
604b3884
GB
4194/*** Standalone Control ***/
4195
4196static int scarlett2_standalone_ctl_get(struct snd_kcontrol *kctl,
4197 struct snd_ctl_elem_value *ucontrol)
4198{
4199 struct usb_mixer_elem_info *elem = kctl->private_data;
4200 struct scarlett2_data *private = elem->head.mixer->private_data;
4201
4202 ucontrol->value.integer.value[0] = private->standalone_switch;
4203 return 0;
4204}
4205
4206static int scarlett2_standalone_ctl_put(struct snd_kcontrol *kctl,
4207 struct snd_ctl_elem_value *ucontrol)
4208{
4209 struct usb_mixer_elem_info *elem = kctl->private_data;
4210 struct usb_mixer_interface *mixer = elem->head.mixer;
4211 struct scarlett2_data *private = mixer->private_data;
4212
4213 int oval, val, err = 0;
4214
4215 mutex_lock(&private->data_mutex);
4216
6a7508e6
GB
4217 if (private->hwdep_in_use) {
4218 err = -EBUSY;
4219 goto unlock;
4220 }
4221
604b3884
GB
4222 oval = private->standalone_switch;
4223 val = !!ucontrol->value.integer.value[0];
4224
4225 if (oval == val)
4226 goto unlock;
4227
4228 private->standalone_switch = val;
4229
4230 /* Send switch change to the device */
4231 err = scarlett2_usb_set_config(mixer,
4232 SCARLETT2_CONFIG_STANDALONE_SWITCH,
4233 0, val);
4234 if (err == 0)
4235 err = 1;
4236
4237unlock:
4238 mutex_unlock(&private->data_mutex);
4239 return err;
4240}
4241
4242static const struct snd_kcontrol_new scarlett2_standalone_ctl = {
4243 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
4244 .name = "",
4245 .info = snd_ctl_boolean_mono_info,
4246 .get = scarlett2_standalone_ctl_get,
4247 .put = scarlett2_standalone_ctl_put,
4248};
4249
4250static int scarlett2_add_standalone_ctl(struct usb_mixer_interface *mixer)
4251{
4252 struct scarlett2_data *private = mixer->private_data;
4253
3978fefd
GB
4254 if (!scarlett2_has_config_item(private,
4255 SCARLETT2_CONFIG_STANDALONE_SWITCH))
604b3884
GB
4256 return 0;
4257
4258 /* Add standalone control */
4259 return scarlett2_add_new_ctl(mixer, &scarlett2_standalone_ctl,
4260 0, 1, "Standalone Switch", NULL);
4261}
4262
9e4d5c1b
GB
4263/*** Cleanup/Suspend Callbacks ***/
4264
4265static void scarlett2_private_free(struct usb_mixer_interface *mixer)
4266{
e46f2195 4267 struct scarlett2_data *private = mixer->private_data;
9e4d5c1b
GB
4268
4269 cancel_delayed_work_sync(&private->work);
4270 kfree(private);
4271 mixer->private_data = NULL;
4272}
4273
4274static void scarlett2_private_suspend(struct usb_mixer_interface *mixer)
4275{
e46f2195 4276 struct scarlett2_data *private = mixer->private_data;
9e4d5c1b
GB
4277
4278 if (cancel_delayed_work_sync(&private->work))
4279 scarlett2_config_save(private->mixer);
4280}
4281
4282/*** Initialisation ***/
4283
e46f2195 4284static void scarlett2_count_mux_io(struct scarlett2_data *private)
9e4d5c1b 4285{
e2cc91ac
GB
4286 const struct scarlett2_device_info *info = private->info;
4287 const int (*port_count)[SCARLETT2_PORT_DIRNS] = info->port_count;
d6f9afe9 4288 int port_type, srcs = 0, dsts = 0;
9e4d5c1b
GB
4289
4290 for (port_type = 0;
4291 port_type < SCARLETT2_PORT_TYPE_COUNT;
d6f9afe9 4292 port_type++) {
e2cc91ac
GB
4293 srcs += port_count[port_type][SCARLETT2_PORT_IN];
4294 dsts += port_count[port_type][SCARLETT2_PORT_OUT];
9e4d5c1b
GB
4295 }
4296
d6f9afe9
GB
4297 private->num_mux_srcs = srcs;
4298 private->num_mux_dsts = dsts;
9e4d5c1b
GB
4299}
4300
6c0a2078
GB
4301/* Look through the interface descriptors for the Focusrite Control
4302 * interface (bInterfaceClass = 255 Vendor Specific Class) and set
4303 * bInterfaceNumber, bEndpointAddress, wMaxPacketSize, and bInterval
4304 * in private
4305 */
4306static int scarlett2_find_fc_interface(struct usb_device *dev,
4307 struct scarlett2_data *private)
4308{
4309 struct usb_host_config *config = dev->actconfig;
4310 int i;
4311
4312 for (i = 0; i < config->desc.bNumInterfaces; i++) {
4313 struct usb_interface *intf = config->interface[i];
4314 struct usb_interface_descriptor *desc =
4315 &intf->altsetting[0].desc;
4316 struct usb_endpoint_descriptor *epd;
4317
4318 if (desc->bInterfaceClass != 255)
4319 continue;
4320
4321 epd = get_endpoint(intf->altsetting, 0);
4322 private->bInterfaceNumber = desc->bInterfaceNumber;
4323 private->bEndpointAddress = epd->bEndpointAddress &
4324 USB_ENDPOINT_NUMBER_MASK;
4325 private->wMaxPacketSize = le16_to_cpu(epd->wMaxPacketSize);
4326 private->bInterval = epd->bInterval;
4327 return 0;
4328 }
4329
4330 return -EINVAL;
4331}
4332
acf91b81 4333/* Initialise private data */
9e4d5c1b 4334static int scarlett2_init_private(struct usb_mixer_interface *mixer,
6e743781 4335 const struct scarlett2_device_entry *entry)
9e4d5c1b 4336{
e46f2195
GB
4337 struct scarlett2_data *private =
4338 kzalloc(sizeof(struct scarlett2_data), GFP_KERNEL);
9e4d5c1b
GB
4339
4340 if (!private)
4341 return -ENOMEM;
4342
4343 mutex_init(&private->usb_mutex);
4344 mutex_init(&private->data_mutex);
4345 INIT_DELAYED_WORK(&private->work, scarlett2_config_save_work);
6c0a2078
GB
4346
4347 mixer->private_data = private;
4348 mixer->private_free = scarlett2_private_free;
4349 mixer->private_suspend = scarlett2_private_suspend;
4350
6e743781 4351 private->info = entry->info;
cbd6f148 4352 private->config_set = entry->info->config_set;
6e743781 4353 private->series_name = entry->series_name;
d6f9afe9 4354 scarlett2_count_mux_io(private);
9e4d5c1b
GB
4355 private->scarlett2_seq = 0;
4356 private->mixer = mixer;
6c0a2078 4357
acf91b81
GB
4358 return scarlett2_find_fc_interface(mixer->chip->dev, private);
4359}
4360
4361/* Cargo cult proprietary initialisation sequence */
4362static int scarlett2_usb_init(struct usb_mixer_interface *mixer)
4363{
4364 struct usb_device *dev = mixer->chip->dev;
4365 struct scarlett2_data *private = mixer->private_data;
701949cc
GB
4366 u8 step0_buf[24];
4367 u8 step2_buf[84];
acf91b81
GB
4368 int err;
4369
4370 if (usb_pipe_type_check(dev, usb_sndctrlpipe(dev, 0)))
4371 return -EINVAL;
4372
4373 /* step 0 */
4374 err = scarlett2_usb_rx(dev, private->bInterfaceNumber,
701949cc
GB
4375 SCARLETT2_USB_CMD_INIT,
4376 step0_buf, sizeof(step0_buf));
6c0a2078
GB
4377 if (err < 0)
4378 return err;
9e4d5c1b 4379
acf91b81
GB
4380 /* step 1 */
4381 private->scarlett2_seq = 1;
4382 err = scarlett2_usb(mixer, SCARLETT2_USB_INIT_1, NULL, 0, NULL, 0);
4383 if (err < 0)
4384 return err;
4385
4386 /* step 2 */
4387 private->scarlett2_seq = 1;
701949cc
GB
4388 err = scarlett2_usb(mixer, SCARLETT2_USB_INIT_2,
4389 NULL, 0,
4390 step2_buf, sizeof(step2_buf));
4391 if (err < 0)
4392 return err;
4393
4394 /* extract 4-byte firmware version from step2_buf[8] */
4395 private->firmware_version = le32_to_cpu(*(__le32 *)(step2_buf + 8));
4396 usb_audio_info(mixer->chip,
4397 "Firmware version %d\n",
4398 private->firmware_version);
4399
4400 return 0;
9e4d5c1b
GB
4401}
4402
34101a0f
GB
4403/* Get the flash segment numbers for the App_Settings and App_Upgrade
4404 * segments and put them in the private data
4405 */
4406static int scarlett2_get_flash_segment_nums(struct usb_mixer_interface *mixer)
4407{
4408 struct scarlett2_data *private = mixer->private_data;
4409 int err, count, i;
4410
4411 struct {
4412 __le32 size;
4413 __le32 count;
4414 u8 unknown[8];
4415 } __packed flash_info;
4416
4417 struct {
4418 __le32 size;
4419 __le32 flags;
4420 char name[16];
4421 } __packed segment_info;
4422
4423 err = scarlett2_usb(mixer, SCARLETT2_USB_INFO_FLASH,
4424 NULL, 0,
4425 &flash_info, sizeof(flash_info));
4426 if (err < 0)
4427 return err;
4428
4429 count = le32_to_cpu(flash_info.count);
4430
4431 /* sanity check count */
4432 if (count < SCARLETT2_SEGMENT_NUM_MIN ||
4433 count > SCARLETT2_SEGMENT_NUM_MAX + 1) {
4434 usb_audio_err(mixer->chip,
4435 "invalid flash segment count: %d\n", count);
4436 return -EINVAL;
4437 }
4438
4439 for (i = 0; i < count; i++) {
4440 __le32 segment_num_req = cpu_to_le32(i);
4441 int flash_segment_id;
4442
4443 err = scarlett2_usb(mixer, SCARLETT2_USB_INFO_SEGMENT,
4444 &segment_num_req, sizeof(segment_num_req),
4445 &segment_info, sizeof(segment_info));
4446 if (err < 0) {
4447 usb_audio_err(mixer->chip,
4448 "failed to get flash segment info %d: %d\n",
4449 i, err);
4450 return err;
4451 }
4452
4453 if (!strncmp(segment_info.name,
4454 SCARLETT2_SEGMENT_SETTINGS_NAME, 16))
4455 flash_segment_id = SCARLETT2_SEGMENT_ID_SETTINGS;
4456 else if (!strncmp(segment_info.name,
4457 SCARLETT2_SEGMENT_FIRMWARE_NAME, 16))
4458 flash_segment_id = SCARLETT2_SEGMENT_ID_FIRMWARE;
4459 else
4460 continue;
4461
4462 private->flash_segment_nums[flash_segment_id] = i;
4463 private->flash_segment_blocks[flash_segment_id] =
4464 le32_to_cpu(segment_info.size) /
4465 SCARLETT2_FLASH_BLOCK_SIZE;
4466 }
4467
4468 /* segment 0 is App_Gold and we never want to touch that, so
4469 * use 0 as the "not-found" value
4470 */
4471 if (!private->flash_segment_nums[SCARLETT2_SEGMENT_ID_SETTINGS]) {
4472 usb_audio_err(mixer->chip,
4473 "failed to find flash segment %s\n",
4474 SCARLETT2_SEGMENT_SETTINGS_NAME);
4475 return -EINVAL;
4476 }
4477 if (!private->flash_segment_nums[SCARLETT2_SEGMENT_ID_FIRMWARE]) {
4478 usb_audio_err(mixer->chip,
4479 "failed to find flash segment %s\n",
4480 SCARLETT2_SEGMENT_FIRMWARE_NAME);
4481 return -EINVAL;
4482 }
4483
4484 return 0;
4485}
4486
2661f033 4487/* Read configuration from the interface on start */
9e4d5c1b
GB
4488static int scarlett2_read_configs(struct usb_mixer_interface *mixer)
4489{
e46f2195 4490 struct scarlett2_data *private = mixer->private_data;
9e4d5c1b 4491 const struct scarlett2_device_info *info = private->info;
e2cc91ac 4492 const int (*port_count)[SCARLETT2_PORT_DIRNS] = info->port_count;
9e4d5c1b 4493 int num_line_out =
e2cc91ac 4494 port_count[SCARLETT2_PORT_TYPE_ANALOGUE][SCARLETT2_PORT_OUT];
2661f033 4495 int num_mixer_out =
e2cc91ac 4496 port_count[SCARLETT2_PORT_TYPE_MIX][SCARLETT2_PORT_IN];
9e4d5c1b
GB
4497 struct scarlett2_usb_volume_status volume_status;
4498 int err, i;
4499
3a4e1afe 4500 if (scarlett2_has_config_item(private, SCARLETT2_CONFIG_MSD_SWITCH)) {
303f204e
GB
4501 err = scarlett2_usb_get_config(
4502 mixer, SCARLETT2_CONFIG_MSD_SWITCH,
4503 1, &private->msd_switch);
4504 if (err < 0)
4505 return err;
4506
4507 /* no other controls are created if MSD mode is on */
4508 if (private->msd_switch)
4509 return 0;
4510 }
4511
a5b36123
GB
4512 err = scarlett2_update_input_other(mixer);
4513 if (err < 0)
4514 return err;
9e4d5c1b 4515
6ef9fa4a
GB
4516 err = scarlett2_update_monitor_other(mixer);
4517 if (err < 0)
4518 return err;
4519
2fa96277 4520 /* the rest of the configuration is for devices with a mixer */
c13d43a8 4521 if (!scarlett2_has_mixer(private))
2fa96277
GB
4522 return 0;
4523
3978fefd
GB
4524 if (scarlett2_has_config_item(private,
4525 SCARLETT2_CONFIG_STANDALONE_SWITCH)) {
4526 err = scarlett2_usb_get_config(
4527 mixer, SCARLETT2_CONFIG_STANDALONE_SWITCH,
4528 1, &private->standalone_switch);
4529 if (err < 0)
4530 return err;
4531 }
604b3884 4532
f3c61043
GB
4533 err = scarlett2_update_sync(mixer);
4534 if (err < 0)
4535 return err;
4536
9e4d5c1b
GB
4537 err = scarlett2_usb_get_volume_status(mixer, &volume_status);
4538 if (err < 0)
4539 return err;
4540
0c88f9db
GB
4541 if (info->line_out_hw_vol)
4542 for (i = 0; i < SCARLETT2_DIM_MUTE_COUNT; i++)
4543 private->dim_mute[i] = !!volume_status.dim_mute[i];
4544
9e4d5c1b
GB
4545 private->master_vol = clamp(
4546 volume_status.master_vol + SCARLETT2_VOLUME_BIAS,
4547 0, SCARLETT2_VOLUME_BIAS);
4548
4549 for (i = 0; i < num_line_out; i++) {
0c88f9db 4550 int volume, mute;
9e4d5c1b
GB
4551
4552 private->vol_sw_hw_switch[i] =
4553 info->line_out_hw_vol
4554 && volume_status.sw_hw_switch[i];
4555
4556 volume = private->vol_sw_hw_switch[i]
4557 ? volume_status.master_vol
4558 : volume_status.sw_vol[i];
4559 volume = clamp(volume + SCARLETT2_VOLUME_BIAS,
4560 0, SCARLETT2_VOLUME_BIAS);
4561 private->vol[i] = volume;
9e4d5c1b 4562
0c88f9db
GB
4563 mute = private->vol_sw_hw_switch[i]
4564 ? private->dim_mute[SCARLETT2_BUTTON_MUTE]
4565 : volume_status.mute_switch[i];
4566 private->mute_switch[i] = mute;
4567 }
9e4d5c1b 4568
2661f033
GB
4569 for (i = 0; i < num_mixer_out; i++) {
4570 err = scarlett2_usb_get_mix(mixer, i);
4571 if (err < 0)
4572 return err;
4573 }
4574
d6f9afe9 4575 return scarlett2_usb_get_mux(mixer);
9e4d5c1b
GB
4576}
4577
f3c61043
GB
4578/* Notify on sync change */
4579static void scarlett2_notify_sync(
4580 struct usb_mixer_interface *mixer)
4581{
4582 struct scarlett2_data *private = mixer->private_data;
4583
4584 private->sync_updated = 1;
4585
4586 snd_ctl_notify(mixer->chip->card, SNDRV_CTL_EVENT_MASK_VALUE,
4587 &private->sync_ctl->id);
4588}
4589
dbd82c05
GB
4590/* Notify on monitor change */
4591static void scarlett2_notify_monitor(
9e4d5c1b
GB
4592 struct usb_mixer_interface *mixer)
4593{
9cfe1276 4594 struct snd_card *card = mixer->chip->card;
e46f2195 4595 struct scarlett2_data *private = mixer->private_data;
e840ee30 4596 const struct scarlett2_device_info *info = private->info;
e2cc91ac 4597 const int (*port_count)[SCARLETT2_PORT_DIRNS] = info->port_count;
9e4d5c1b 4598 int num_line_out =
e2cc91ac 4599 port_count[SCARLETT2_PORT_TYPE_ANALOGUE][SCARLETT2_PORT_OUT];
9e4d5c1b
GB
4600 int i;
4601
e840ee30
GB
4602 /* if line_out_hw_vol is 0, there are no controls to update */
4603 if (!info->line_out_hw_vol)
4604 return;
4605
9e4d5c1b
GB
4606 private->vol_updated = 1;
4607
4608 snd_ctl_notify(mixer->chip->card, SNDRV_CTL_EVENT_MASK_VALUE,
4609 &private->master_vol_ctl->id);
4610
9cfe1276
GB
4611 for (i = 0; i < num_line_out; i++)
4612 if (private->vol_sw_hw_switch[line_out_remap(private, i)])
4613 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE,
4614 &private->vol_ctls[i]->id);
9e4d5c1b
GB
4615}
4616
dbd82c05
GB
4617/* Notify on dim/mute change */
4618static void scarlett2_notify_dim_mute(
9e4d5c1b
GB
4619 struct usb_mixer_interface *mixer)
4620{
0c88f9db 4621 struct snd_card *card = mixer->chip->card;
e46f2195 4622 struct scarlett2_data *private = mixer->private_data;
03bdbcf0 4623 const struct scarlett2_device_info *info = private->info;
e2cc91ac 4624 const int (*port_count)[SCARLETT2_PORT_DIRNS] = info->port_count;
0c88f9db 4625 int num_line_out =
e2cc91ac 4626 port_count[SCARLETT2_PORT_TYPE_ANALOGUE][SCARLETT2_PORT_OUT];
9e4d5c1b
GB
4627 int i;
4628
4629 private->vol_updated = 1;
4630
03bdbcf0
GB
4631 if (!info->line_out_hw_vol)
4632 return;
4633
dbd82c05 4634 for (i = 0; i < SCARLETT2_DIM_MUTE_COUNT; i++)
0c88f9db 4635 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE,
dbd82c05 4636 &private->dim_mute_ctls[i]->id);
0c88f9db
GB
4637
4638 for (i = 0; i < num_line_out; i++)
9cfe1276 4639 if (private->vol_sw_hw_switch[line_out_remap(private, i)])
0c88f9db
GB
4640 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE,
4641 &private->mute_ctls[i]->id);
9e4d5c1b
GB
4642}
4643
dbbd4f9e 4644/* Notify on "input other" change (level/pad/air) */
a5b36123
GB
4645static void scarlett2_notify_input_other(
4646 struct usb_mixer_interface *mixer)
4647{
4648 struct snd_card *card = mixer->chip->card;
4649 struct scarlett2_data *private = mixer->private_data;
4650 const struct scarlett2_device_info *info = private->info;
4651 int i;
4652
4653 private->input_other_updated = 1;
4654
4655 for (i = 0; i < info->level_input_count; i++)
4656 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE,
4657 &private->level_ctls[i]->id);
4658 for (i = 0; i < info->pad_input_count; i++)
4659 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE,
4660 &private->pad_ctls[i]->id);
dbbd4f9e
GB
4661 for (i = 0; i < info->air_input_count; i++)
4662 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE,
4663 &private->air_ctls[i]->id);
ae58a1a1
GB
4664 for (i = 0; i < info->phantom_count; i++)
4665 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE,
4666 &private->phantom_ctls[i]->id);
a5b36123
GB
4667}
4668
d5bda7e0
GB
4669/* Notify on "monitor other" change (direct monitor, speaker
4670 * switching, talkback)
4671 */
6ef9fa4a
GB
4672static void scarlett2_notify_monitor_other(
4673 struct usb_mixer_interface *mixer)
4674{
6ef9fa4a 4675 struct snd_card *card = mixer->chip->card;
e914d843
GB
4676 struct scarlett2_data *private = mixer->private_data;
4677 const struct scarlett2_device_info *info = private->info;
6ef9fa4a
GB
4678
4679 private->monitor_other_updated = 1;
4680
e914d843 4681 if (info->direct_monitor) {
6ef9fa4a
GB
4682 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE,
4683 &private->direct_monitor_ctl->id);
e914d843
GB
4684 return;
4685 }
4686
4687 if (info->has_speaker_switching)
4688 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE,
4689 &private->speaker_switching_ctl->id);
4690
d5bda7e0
GB
4691 if (info->has_talkback)
4692 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE,
4693 &private->talkback_ctl->id);
4694
e914d843
GB
4695 /* if speaker switching was recently enabled or disabled,
4696 * invalidate the dim/mute and mux enum controls
4697 */
4698 if (private->speaker_switching_switched) {
4699 int i;
4700
4701 scarlett2_notify_dim_mute(mixer);
4702
4703 private->speaker_switching_switched = 0;
4704 private->mux_updated = 1;
4705
4706 for (i = 0; i < private->num_mux_dsts; i++)
4707 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE,
4708 &private->mux_ctls[i]->id);
4709 }
6ef9fa4a
GB
4710}
4711
9e4d5c1b 4712/* Interrupt callback */
dbd82c05 4713static void scarlett2_notify(struct urb *urb)
9e4d5c1b
GB
4714{
4715 struct usb_mixer_interface *mixer = urb->context;
4716 int len = urb->actual_length;
4717 int ustatus = urb->status;
4718 u32 data;
4719
411b22ed 4720 if (ustatus != 0 || len != 8)
9e4d5c1b
GB
4721 goto requeue;
4722
411b22ed 4723 data = le32_to_cpu(*(__le32 *)urb->transfer_buffer);
f3c61043
GB
4724 if (data & SCARLETT2_USB_NOTIFY_SYNC)
4725 scarlett2_notify_sync(mixer);
dbd82c05
GB
4726 if (data & SCARLETT2_USB_NOTIFY_MONITOR)
4727 scarlett2_notify_monitor(mixer);
4728 if (data & SCARLETT2_USB_NOTIFY_DIM_MUTE)
4729 scarlett2_notify_dim_mute(mixer);
a5b36123
GB
4730 if (data & SCARLETT2_USB_NOTIFY_INPUT_OTHER)
4731 scarlett2_notify_input_other(mixer);
6ef9fa4a
GB
4732 if (data & SCARLETT2_USB_NOTIFY_MONITOR_OTHER)
4733 scarlett2_notify_monitor_other(mixer);
9e4d5c1b
GB
4734
4735requeue:
4736 if (ustatus != -ENOENT &&
4737 ustatus != -ECONNRESET &&
4738 ustatus != -ESHUTDOWN) {
4739 urb->dev = mixer->chip->dev;
4740 usb_submit_urb(urb, GFP_ATOMIC);
4741 }
4742}
4743
dbd82c05 4744static int scarlett2_init_notify(struct usb_mixer_interface *mixer)
9e4d5c1b
GB
4745{
4746 struct usb_device *dev = mixer->chip->dev;
6c0a2078
GB
4747 struct scarlett2_data *private = mixer->private_data;
4748 unsigned int pipe = usb_rcvintpipe(dev, private->bEndpointAddress);
9e4d5c1b
GB
4749 void *transfer_buffer;
4750
4751 if (mixer->urb) {
4752 usb_audio_err(mixer->chip,
4753 "%s: mixer urb already in use!\n", __func__);
4754 return 0;
4755 }
4756
fcc2cc1f 4757 if (usb_pipe_type_check(dev, pipe))
9e4d5c1b
GB
4758 return -EINVAL;
4759
4760 mixer->urb = usb_alloc_urb(0, GFP_KERNEL);
4761 if (!mixer->urb)
4762 return -ENOMEM;
4763
6c0a2078 4764 transfer_buffer = kmalloc(private->wMaxPacketSize, GFP_KERNEL);
9e4d5c1b
GB
4765 if (!transfer_buffer)
4766 return -ENOMEM;
4767
4768 usb_fill_int_urb(mixer->urb, dev, pipe,
6c0a2078
GB
4769 transfer_buffer, private->wMaxPacketSize,
4770 scarlett2_notify, mixer, private->bInterval);
9e4d5c1b
GB
4771
4772 return usb_submit_urb(mixer->urb, GFP_KERNEL);
4773}
4774
6e743781
GB
4775static const struct scarlett2_device_entry *get_scarlett2_device_entry(
4776 struct usb_mixer_interface *mixer)
9e4d5c1b 4777{
d98cc489 4778 const struct scarlett2_device_entry *entry = scarlett2_devices;
9e4d5c1b 4779
d98cc489
GB
4780 /* Find entry in scarlett2_devices */
4781 while (entry->usb_id && entry->usb_id != mixer->chip->usb_id)
4782 entry++;
4783 if (!entry->usb_id)
6e743781
GB
4784 return NULL;
4785
4786 return entry;
4787}
4788
efc3d7d2 4789static int snd_scarlett2_controls_create(
6e743781
GB
4790 struct usb_mixer_interface *mixer,
4791 const struct scarlett2_device_entry *entry)
4792{
4793 int err;
6fd9d695 4794
acf91b81 4795 /* Initialise private data */
6e743781 4796 err = scarlett2_init_private(mixer, entry);
9e4d5c1b 4797 if (err < 0)
acf91b81
GB
4798 return err;
4799
4800 /* Send proprietary USB initialisation sequence */
4801 err = scarlett2_usb_init(mixer);
4802 if (err < 0)
9e4d5c1b
GB
4803 return err;
4804
34101a0f
GB
4805 /* Get the upgrade & settings flash segment numbers */
4806 err = scarlett2_get_flash_segment_nums(mixer);
4807 if (err < 0)
4808 return err;
4809
701949cc
GB
4810 /* Add firmware version control */
4811 err = scarlett2_add_firmware_version_ctl(mixer);
a5901f27
GB
4812 if (err < 0)
4813 return err;
701949cc 4814
9e4d5c1b
GB
4815 /* Read volume levels and controls from the interface */
4816 err = scarlett2_read_configs(mixer);
4817 if (err < 0)
4818 return err;
4819
303f204e
GB
4820 /* Create the MSD control */
4821 err = scarlett2_add_msd_ctl(mixer);
4822 if (err < 0)
4823 return err;
4824
4825 /* If MSD mode is enabled, don't create any other controls */
4826 if (((struct scarlett2_data *)mixer->private_data)->msd_switch)
4827 return 0;
4828
9e4d5c1b
GB
4829 /* Create the analogue output controls */
4830 err = scarlett2_add_line_out_ctls(mixer);
4831 if (err < 0)
4832 return err;
4833
4834 /* Create the analogue input controls */
4835 err = scarlett2_add_line_in_ctls(mixer);
4836 if (err < 0)
4837 return err;
4838
4839 /* Create the input, output, and mixer mux input selections */
4840 err = scarlett2_add_mux_enums(mixer);
4841 if (err < 0)
4842 return err;
4843
4844 /* Create the matrix mixer controls */
4845 err = scarlett2_add_mixer_ctls(mixer);
4846 if (err < 0)
4847 return err;
4848
4849 /* Create the level meter controls */
4850 err = scarlett2_add_meter_ctl(mixer);
4851 if (err < 0)
4852 return err;
4853
f3c61043
GB
4854 /* Create the sync control */
4855 err = scarlett2_add_sync_ctl(mixer);
4856 if (err < 0)
4857 return err;
4858
6ef9fa4a
GB
4859 /* Create the direct monitor control */
4860 err = scarlett2_add_direct_monitor_ctl(mixer);
4861 if (err < 0)
4862 return err;
4863
e914d843
GB
4864 /* Create the speaker switching control */
4865 err = scarlett2_add_speaker_switch_ctl(mixer);
4866 if (err < 0)
4867 return err;
4868
d5bda7e0
GB
4869 /* Create the talkback controls */
4870 err = scarlett2_add_talkback_ctls(mixer);
4871 if (err < 0)
4872 return err;
4873
604b3884
GB
4874 /* Create the standalone control */
4875 err = scarlett2_add_standalone_ctl(mixer);
4876 if (err < 0)
4877 return err;
4878
e840ee30
GB
4879 /* Set up the interrupt polling */
4880 err = scarlett2_init_notify(mixer);
4881 if (err < 0)
4882 return err;
9e4d5c1b
GB
4883
4884 return 0;
4885}
265d1a90 4886
337b2f0e
GB
4887/*** hwdep interface ***/
4888
6a7508e6
GB
4889/* Set private->hwdep_in_use; prevents access to the ALSA controls
4890 * while doing a config erase/firmware upgrade.
4891 */
4892static void scarlett2_lock(struct scarlett2_data *private)
4893{
4894 mutex_lock(&private->data_mutex);
4895 private->hwdep_in_use = 1;
4896 mutex_unlock(&private->data_mutex);
4897}
4898
4899/* Call SCARLETT2_USB_GET_ERASE to get the erase progress */
4900static int scarlett2_get_erase_progress(struct usb_mixer_interface *mixer)
4901{
4902 struct scarlett2_data *private = mixer->private_data;
4903 int segment_id, segment_num, err;
4904 u8 erase_resp;
4905
4906 struct {
4907 __le32 segment_num;
4908 __le32 pad;
4909 } __packed erase_req;
4910
4911 segment_id = private->selected_flash_segment_id;
4912 segment_num = private->flash_segment_nums[segment_id];
4913
4914 if (segment_num < SCARLETT2_SEGMENT_NUM_MIN ||
4915 segment_num > SCARLETT2_SEGMENT_NUM_MAX)
4916 return -EFAULT;
4917
4918 /* Send the erase progress request */
4919 erase_req.segment_num = cpu_to_le32(segment_num);
4920 erase_req.pad = 0;
4921
4922 err = scarlett2_usb(mixer, SCARLETT2_USB_GET_ERASE,
4923 &erase_req, sizeof(erase_req),
4924 &erase_resp, sizeof(erase_resp));
4925 if (err < 0)
4926 return err;
4927
4928 return erase_resp;
4929}
4930
4931/* Repeatedly call scarlett2_get_erase_progress() until it returns
4932 * 0xff (erase complete) or we've waited 10 seconds (it usually takes
4933 * <3 seconds).
4934 */
4935static int scarlett2_wait_for_erase(struct usb_mixer_interface *mixer)
4936{
4937 int i, err;
4938
4939 for (i = 0; i < 100; i++) {
4940 err = scarlett2_get_erase_progress(mixer);
4941 if (err < 0)
4942 return err;
4943
4944 if (err == 0xff)
4945 return 0;
4946
4947 msleep(100);
4948 }
4949
4950 return -ETIMEDOUT;
4951}
4952
4953/* Reboot the device; wait for the erase to complete if one is in
4954 * progress.
4955 */
337b2f0e
GB
4956static int scarlett2_reboot(struct usb_mixer_interface *mixer)
4957{
6a7508e6
GB
4958 struct scarlett2_data *private = mixer->private_data;
4959
4960 if (private->flash_write_state ==
4961 SCARLETT2_FLASH_WRITE_STATE_ERASING) {
4962 int err = scarlett2_wait_for_erase(mixer);
4963
4964 if (err < 0)
4965 return err;
4966 }
4967
337b2f0e
GB
4968 return scarlett2_usb(mixer, SCARLETT2_USB_REBOOT, NULL, 0, NULL, 0);
4969}
4970
6a7508e6
GB
4971/* Select a flash segment for erasing (and possibly writing to) */
4972static int scarlett2_ioctl_select_flash_segment(
4973 struct usb_mixer_interface *mixer,
4974 unsigned long arg)
4975{
4976 struct scarlett2_data *private = mixer->private_data;
4977 int segment_id, segment_num;
4978
4979 if (get_user(segment_id, (int __user *)arg))
4980 return -EFAULT;
4981
4982 /* Check the segment ID and segment number */
4983 if (segment_id < 0 || segment_id >= SCARLETT2_SEGMENT_ID_COUNT)
4984 return -EINVAL;
4985
4986 segment_num = private->flash_segment_nums[segment_id];
4987 if (segment_num < SCARLETT2_SEGMENT_NUM_MIN ||
4988 segment_num > SCARLETT2_SEGMENT_NUM_MAX) {
4989 usb_audio_err(mixer->chip,
4990 "%s: invalid segment number %d\n",
4991 __func__, segment_id);
4992 return -EFAULT;
4993 }
4994
4995 /* If erasing, wait for it to complete */
4996 if (private->flash_write_state == SCARLETT2_FLASH_WRITE_STATE_ERASING) {
4997 int err = scarlett2_wait_for_erase(mixer);
4998
4999 if (err < 0)
5000 return err;
5001 }
5002
5003 /* Save the selected segment ID and set the state to SELECTED */
5004 private->selected_flash_segment_id = segment_id;
5005 private->flash_write_state = SCARLETT2_FLASH_WRITE_STATE_SELECTED;
5006
5007 return 0;
5008}
5009
5010/* Erase the previously-selected flash segment */
5011static int scarlett2_ioctl_erase_flash_segment(
5012 struct usb_mixer_interface *mixer)
5013{
5014 struct scarlett2_data *private = mixer->private_data;
5015 int segment_id, segment_num, err;
5016
5017 struct {
5018 __le32 segment_num;
5019 __le32 pad;
5020 } __packed erase_req;
5021
5022 if (private->flash_write_state != SCARLETT2_FLASH_WRITE_STATE_SELECTED)
5023 return -EINVAL;
5024
5025 segment_id = private->selected_flash_segment_id;
5026 segment_num = private->flash_segment_nums[segment_id];
5027
5028 if (segment_num < SCARLETT2_SEGMENT_NUM_MIN ||
5029 segment_num > SCARLETT2_SEGMENT_NUM_MAX)
5030 return -EFAULT;
5031
5032 /* Prevent access to ALSA controls that access the device from
5033 * here on
5034 */
5035 scarlett2_lock(private);
5036
5037 /* Send the erase request */
5038 erase_req.segment_num = cpu_to_le32(segment_num);
5039 erase_req.pad = 0;
5040
5041 err = scarlett2_usb(mixer, SCARLETT2_USB_ERASE_SEGMENT,
5042 &erase_req, sizeof(erase_req),
5043 NULL, 0);
5044 if (err < 0)
5045 return err;
5046
5047 /* On success, change the state from SELECTED to ERASING */
5048 private->flash_write_state = SCARLETT2_FLASH_WRITE_STATE_ERASING;
5049
5050 return 0;
5051}
5052
5053/* Get the erase progress from the device */
5054static int scarlett2_ioctl_get_erase_progress(
5055 struct usb_mixer_interface *mixer,
5056 unsigned long arg)
5057{
5058 struct scarlett2_data *private = mixer->private_data;
5059 struct scarlett2_flash_segment_erase_progress progress;
5060 int segment_id, segment_num, err;
5061 u8 erase_resp;
5062
5063 struct {
5064 __le32 segment_num;
5065 __le32 pad;
5066 } __packed erase_req;
5067
5068 /* Check that we're erasing */
5069 if (private->flash_write_state != SCARLETT2_FLASH_WRITE_STATE_ERASING)
5070 return -EINVAL;
5071
5072 segment_id = private->selected_flash_segment_id;
5073 segment_num = private->flash_segment_nums[segment_id];
5074
5075 if (segment_num < SCARLETT2_SEGMENT_NUM_MIN ||
5076 segment_num > SCARLETT2_SEGMENT_NUM_MAX)
5077 return -EFAULT;
5078
5079 /* Send the erase progress request */
5080 erase_req.segment_num = cpu_to_le32(segment_num);
5081 erase_req.pad = 0;
5082
5083 err = scarlett2_usb(mixer, SCARLETT2_USB_GET_ERASE,
5084 &erase_req, sizeof(erase_req),
5085 &erase_resp, sizeof(erase_resp));
5086 if (err < 0)
5087 return err;
5088
5089 progress.progress = erase_resp;
5090 progress.num_blocks = private->flash_segment_blocks[segment_id];
5091
5092 if (copy_to_user((void __user *)arg, &progress, sizeof(progress)))
5093 return -EFAULT;
5094
5095 /* If the erase is complete, change the state from ERASING to
1abfbd3c 5096 * WRITE.
6a7508e6
GB
5097 */
5098 if (progress.progress == 0xff)
1abfbd3c 5099 private->flash_write_state = SCARLETT2_FLASH_WRITE_STATE_WRITE;
6a7508e6
GB
5100
5101 return 0;
5102}
5103
5104static int scarlett2_hwdep_open(struct snd_hwdep *hw, struct file *file)
5105{
5106 struct usb_mixer_interface *mixer = hw->private_data;
5107 struct scarlett2_data *private = mixer->private_data;
5108
5109 /* If erasing, wait for it to complete */
5110 if (private->flash_write_state ==
5111 SCARLETT2_FLASH_WRITE_STATE_ERASING) {
5112 int err = scarlett2_wait_for_erase(mixer);
5113
5114 if (err < 0)
5115 return err;
5116 }
5117
5118 /* Set the state to IDLE */
5119 private->flash_write_state = SCARLETT2_FLASH_WRITE_STATE_IDLE;
5120
5121 return 0;
5122}
5123
337b2f0e
GB
5124static int scarlett2_hwdep_ioctl(struct snd_hwdep *hw, struct file *file,
5125 unsigned int cmd, unsigned long arg)
5126{
5127 struct usb_mixer_interface *mixer = hw->private_data;
5128
5129 switch (cmd) {
5130
5131 case SCARLETT2_IOCTL_PVERSION:
5132 return put_user(SCARLETT2_HWDEP_VERSION,
5133 (int __user *)arg) ? -EFAULT : 0;
5134
5135 case SCARLETT2_IOCTL_REBOOT:
5136 return scarlett2_reboot(mixer);
5137
6a7508e6
GB
5138 case SCARLETT2_IOCTL_SELECT_FLASH_SEGMENT:
5139 return scarlett2_ioctl_select_flash_segment(mixer, arg);
5140
5141 case SCARLETT2_IOCTL_ERASE_FLASH_SEGMENT:
5142 return scarlett2_ioctl_erase_flash_segment(mixer);
5143
5144 case SCARLETT2_IOCTL_GET_ERASE_PROGRESS:
5145 return scarlett2_ioctl_get_erase_progress(mixer, arg);
5146
337b2f0e
GB
5147 default:
5148 return -ENOIOCTLCMD;
5149 }
5150}
5151
1abfbd3c
GB
5152static long scarlett2_hwdep_write(struct snd_hwdep *hw,
5153 const char __user *buf,
5154 long count, loff_t *offset)
5155{
5156 struct usb_mixer_interface *mixer = hw->private_data;
5157 struct scarlett2_data *private = mixer->private_data;
5158 int segment_id, segment_num, err, len;
5159 int flash_size;
5160
5161 /* SCARLETT2_USB_WRITE_SEGMENT request data */
5162 struct {
5163 __le32 segment_num;
5164 __le32 offset;
5165 __le32 pad;
5166 u8 data[];
5167 } __packed *req;
5168
5169 /* Calculate the maximum permitted in data[] */
5170 const size_t max_data_size = SCARLETT2_FLASH_WRITE_MAX -
5171 offsetof(typeof(*req), data);
5172
5173 /* If erasing, wait for it to complete */
5174 if (private->flash_write_state ==
5175 SCARLETT2_FLASH_WRITE_STATE_ERASING) {
5176 err = scarlett2_wait_for_erase(mixer);
5177 if (err < 0)
5178 return err;
5179 private->flash_write_state = SCARLETT2_FLASH_WRITE_STATE_WRITE;
5180
5181 /* Check that an erase has been done & completed */
5182 } else if (private->flash_write_state !=
5183 SCARLETT2_FLASH_WRITE_STATE_WRITE) {
5184 return -EINVAL;
5185 }
5186
5187 /* Check that we're writing to the upgrade firmware */
5188 segment_id = private->selected_flash_segment_id;
5189 if (segment_id != SCARLETT2_SEGMENT_ID_FIRMWARE)
5190 return -EINVAL;
5191
5192 segment_num = private->flash_segment_nums[segment_id];
5193 if (segment_num < SCARLETT2_SEGMENT_NUM_MIN ||
5194 segment_num > SCARLETT2_SEGMENT_NUM_MAX)
5195 return -EFAULT;
5196
5197 /* Validate the offset and count */
5198 flash_size = private->flash_segment_blocks[segment_id] *
5199 SCARLETT2_FLASH_BLOCK_SIZE;
5200
5201 if (count < 0 || *offset < 0 || *offset + count >= flash_size)
5202 return -EINVAL;
5203
5204 if (!count)
5205 return 0;
5206
5207 /* Limit the *req size to SCARLETT2_FLASH_WRITE_MAX */
5208 if (count > max_data_size)
5209 count = max_data_size;
5210
5211 /* Create and send the request */
5212 len = struct_size(req, data, count);
5213 req = kzalloc(len, GFP_KERNEL);
5214 if (!req)
5215 return -ENOMEM;
5216
5217 req->segment_num = cpu_to_le32(segment_num);
5218 req->offset = cpu_to_le32(*offset);
5219 req->pad = 0;
5220
5221 if (copy_from_user(req->data, buf, count)) {
5222 err = -EFAULT;
5223 goto error;
5224 }
5225
5226 err = scarlett2_usb(mixer, SCARLETT2_USB_WRITE_SEGMENT,
5227 req, len, NULL, 0);
5228 if (err < 0)
5229 goto error;
5230
5231 *offset += count;
5232 err = count;
5233
5234error:
5235 kfree(req);
5236 return err;
5237}
5238
6a7508e6
GB
5239static int scarlett2_hwdep_release(struct snd_hwdep *hw, struct file *file)
5240{
5241 struct usb_mixer_interface *mixer = hw->private_data;
5242 struct scarlett2_data *private = mixer->private_data;
5243
5244 /* Return from the SELECTED or WRITE state to IDLE.
5245 * The ERASING state is left as-is, and checked on next open.
5246 */
5247 if (private &&
5248 private->hwdep_in_use &&
5249 private->flash_write_state != SCARLETT2_FLASH_WRITE_STATE_ERASING)
5250 private->flash_write_state = SCARLETT2_FLASH_WRITE_STATE_IDLE;
5251
5252 return 0;
5253}
5254
337b2f0e
GB
5255static int scarlett2_hwdep_init(struct usb_mixer_interface *mixer)
5256{
5257 struct snd_hwdep *hw;
5258 int err;
5259
5260 err = snd_hwdep_new(mixer->chip->card, "Focusrite Control", 0, &hw);
5261 if (err < 0)
5262 return err;
5263
5264 hw->private_data = mixer;
5265 hw->exclusive = 1;
6a7508e6 5266 hw->ops.open = scarlett2_hwdep_open;
337b2f0e 5267 hw->ops.ioctl = scarlett2_hwdep_ioctl;
1abfbd3c 5268 hw->ops.write = scarlett2_hwdep_write;
6a7508e6 5269 hw->ops.release = scarlett2_hwdep_release;
337b2f0e
GB
5270
5271 return 0;
5272}
5273
efc3d7d2 5274int snd_scarlett2_init(struct usb_mixer_interface *mixer)
265d1a90
GB
5275{
5276 struct snd_usb_audio *chip = mixer->chip;
6e743781 5277 const struct scarlett2_device_entry *entry;
265d1a90
GB
5278 int err;
5279
5280 /* only use UAC_VERSION_2 */
5281 if (!mixer->protocol)
5282 return 0;
5283
6e743781
GB
5284 /* find entry in scarlett2_devices */
5285 entry = get_scarlett2_device_entry(mixer);
5286 if (!entry) {
5287 usb_audio_err(mixer->chip,
5288 "%s: missing device entry for %04x:%04x\n",
5289 __func__,
5290 USB_ID_VENDOR(chip->usb_id),
5291 USB_ID_PRODUCT(chip->usb_id));
5292 return 0;
5293 }
5294
bc83058f 5295 if (chip->setup & SCARLETT2_DISABLE) {
265d1a90 5296 usb_audio_info(chip,
6e743781 5297 "Focusrite %s Mixer Driver disabled "
bc83058f
GB
5298 "by modprobe options (snd_usb_audio "
5299 "vid=0x%04x pid=0x%04x device_setup=%d)\n",
6e743781 5300 entry->series_name,
265d1a90 5301 USB_ID_VENDOR(chip->usb_id),
bc83058f
GB
5302 USB_ID_PRODUCT(chip->usb_id),
5303 SCARLETT2_DISABLE);
265d1a90
GB
5304 return 0;
5305 }
5306
5307 usb_audio_info(chip,
6e743781 5308 "Focusrite %s Mixer Driver enabled (pid=0x%04x); "
649cc9e5
GB
5309 "report any issues to "
5310 "https://github.com/geoffreybennett/scarlett-gen2/issues",
6e743781 5311 entry->series_name,
265d1a90
GB
5312 USB_ID_PRODUCT(chip->usb_id));
5313
efc3d7d2 5314 err = snd_scarlett2_controls_create(mixer, entry);
337b2f0e 5315 if (err < 0) {
265d1a90 5316 usb_audio_err(mixer->chip,
6e743781
GB
5317 "Error initialising %s Mixer Driver: %d",
5318 entry->series_name,
265d1a90 5319 err);
337b2f0e
GB
5320 return err;
5321 }
5322
5323 err = scarlett2_hwdep_init(mixer);
5324 if (err < 0)
5325 usb_audio_err(mixer->chip,
5326 "Error creating %s hwdep device: %d",
5327 entry->series_name,
5328 err);
265d1a90
GB
5329
5330 return err;
5331}