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