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