Loading...
1// SPDX-License-Identifier: GPL-2.0
2/*
3 * Focusrite Scarlett Gen 2/3 Driver for ALSA
4 *
5 * Supported models:
6 * - 6i6/18i8/18i20 Gen 2
7 * - Solo/2i2/4i4/8i6/18i8/18i20 Gen 3
8 *
9 * Copyright (c) 2018-2021 by Geoffrey D. Bennett <g at b4.vu>
10 * Copyright (c) 2020-2021 by Vladimir Sadovnikov <sadko4u@gmail.com>
11 *
12 * Based on the Scarlett (Gen 1) Driver for ALSA:
13 *
14 * Copyright (c) 2013 by Tobias Hoffmann
15 * Copyright (c) 2013 by Robin Gareus <robin at gareus.org>
16 * Copyright (c) 2002 by Takashi Iwai <tiwai at suse.de>
17 * Copyright (c) 2014 by Chris J Arges <chris.j.arges at canonical.com>
18 *
19 * Many codes borrowed from audio.c by
20 * Alan Cox (alan at lxorguk.ukuu.org.uk)
21 * Thomas Sailer (sailer at ife.ee.ethz.ch)
22 *
23 * Code cleanup:
24 * David Henningsson <david.henningsson at canonical.com>
25 */
26
27/* The protocol was reverse engineered by looking at the communication
28 * between Focusrite Control 2.3.4 and the Focusrite(R) Scarlett 18i20
29 * (firmware 1083) using usbmon in July-August 2018.
30 *
31 * Scarlett 18i8 support added in April 2019.
32 *
33 * Scarlett 6i6 support added in June 2019 (thanks to Martin Wittmann
34 * for providing usbmon output and testing).
35 *
36 * Scarlett 4i4/8i6 Gen 3 support added in May 2020 (thanks to Laurent
37 * Debricon for donating a 4i4 and to Fredrik Unger for providing 8i6
38 * usbmon output and testing).
39 *
40 * Scarlett 18i8/18i20 Gen 3 support added in June 2020 (thanks to
41 * Darren Jaeckel, Alex Sedlack, and Clovis Lunel for providing usbmon
42 * output, protocol traces and testing).
43 *
44 * Support for loading mixer volume and mux configuration from the
45 * interface during driver initialisation added in May 2021 (thanks to
46 * Vladimir Sadovnikov for figuring out how).
47 *
48 * Support for Solo/2i2 Gen 3 added in May 2021 (thanks to Alexander
49 * Vorona for 2i2 protocol traces).
50 *
51 * Support for phantom power, direct monitoring, speaker switching,
52 * and talkback added in May-June 2021.
53 *
54 * This ALSA mixer gives access to (model-dependent):
55 * - input, output, mixer-matrix muxes
56 * - mixer-matrix gain stages
57 * - gain/volume/mute controls
58 * - level meters
59 * - line/inst level, pad, and air controls
60 * - phantom power, direct monitor, speaker switching, and talkback
61 * controls
62 * - disable/enable MSD mode
63 *
64 * <ditaa>
65 * /--------------\ 18chn 20chn /--------------\
66 * | Hardware in +--+------\ /-------------+--+ ALSA PCM out |
67 * \--------------/ | | | | \--------------/
68 * | | | /-----\ |
69 * | | | | | |
70 * | v v v | |
71 * | +---------------+ | |
72 * | \ Matrix Mux / | |
73 * | +-----+-----+ | |
74 * | | | |
75 * | |18chn | |
76 * | | | |
77 * | | 10chn| |
78 * | v | |
79 * | +------------+ | |
80 * | | Mixer | | |
81 * | | Matrix | | |
82 * | | | | |
83 * | | 18x10 Gain | | |
84 * | | stages | | |
85 * | +-----+------+ | |
86 * | | | |
87 * |18chn |10chn | |20chn
88 * | | | |
89 * | +----------/ |
90 * | | |
91 * v v v
92 * ===========================
93 * +---------------+ +--—------------+
94 * \ Output Mux / \ Capture Mux /
95 * +---+---+---+ +-----+-----+
96 * | | |
97 * 10chn| | |18chn
98 * | | |
99 * /--------------\ | | | /--------------\
100 * | S/PDIF, ADAT |<--/ |10chn \-->| ALSA PCM in |
101 * | Hardware out | | \--------------/
102 * \--------------/ |
103 * v
104 * +-------------+ Software gain per channel.
105 * | Master Gain |<-- 18i20 only: Switch per channel
106 * +------+------+ to select HW or SW gain control.
107 * |
108 * |10chn
109 * /--------------\ |
110 * | Analogue |<------/
111 * | Hardware out |
112 * \--------------/
113 * </ditaa>
114 *
115 * Gen 3 devices have a Mass Storage Device (MSD) mode where a small
116 * disk with registration and driver download information is presented
117 * to the host. To access the full functionality of the device without
118 * proprietary software, MSD mode can be disabled by:
119 * - holding down the 48V button for five seconds while powering on
120 * the device, or
121 * - using this driver and alsamixer to change the "MSD Mode" setting
122 * to Off and power-cycling the device
123 */
124
125#include <linux/slab.h>
126#include <linux/usb.h>
127#include <linux/moduleparam.h>
128
129#include <sound/control.h>
130#include <sound/tlv.h>
131
132#include "usbaudio.h"
133#include "mixer.h"
134#include "helper.h"
135
136#include "mixer_scarlett_gen2.h"
137
138/* device_setup value to enable */
139#define SCARLETT2_ENABLE 0x01
140
141/* device_setup value to allow turning MSD mode back on */
142#define SCARLETT2_MSD_ENABLE 0x02
143
144/* some gui mixers can't handle negative ctl values */
145#define SCARLETT2_VOLUME_BIAS 127
146
147/* mixer range from -80dB to +6dB in 0.5dB steps */
148#define SCARLETT2_MIXER_MIN_DB -80
149#define SCARLETT2_MIXER_BIAS (-SCARLETT2_MIXER_MIN_DB * 2)
150#define SCARLETT2_MIXER_MAX_DB 6
151#define SCARLETT2_MIXER_MAX_VALUE \
152 ((SCARLETT2_MIXER_MAX_DB - SCARLETT2_MIXER_MIN_DB) * 2)
153#define SCARLETT2_MIXER_VALUE_COUNT (SCARLETT2_MIXER_MAX_VALUE + 1)
154
155/* map from (dB + 80) * 2 to mixer value
156 * for dB in 0 .. 172: int(8192 * pow(10, ((dB - 160) / 2 / 20)))
157 */
158static const u16 scarlett2_mixer_values[SCARLETT2_MIXER_VALUE_COUNT] = {
159 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2,
160 2, 2, 3, 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 6, 6, 6, 7, 7, 8, 8,
161 9, 9, 10, 10, 11, 12, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21,
162 23, 24, 25, 27, 29, 30, 32, 34, 36, 38, 41, 43, 46, 48, 51,
163 54, 57, 61, 65, 68, 73, 77, 81, 86, 91, 97, 103, 109, 115,
164 122, 129, 137, 145, 154, 163, 173, 183, 194, 205, 217, 230,
165 244, 259, 274, 290, 307, 326, 345, 365, 387, 410, 434, 460,
166 487, 516, 547, 579, 614, 650, 689, 730, 773, 819, 867, 919,
167 973, 1031, 1092, 1157, 1225, 1298, 1375, 1456, 1543, 1634,
168 1731, 1833, 1942, 2057, 2179, 2308, 2445, 2590, 2744, 2906,
169 3078, 3261, 3454, 3659, 3876, 4105, 4349, 4606, 4879, 5168,
170 5475, 5799, 6143, 6507, 6892, 7301, 7733, 8192, 8677, 9191,
171 9736, 10313, 10924, 11571, 12257, 12983, 13752, 14567, 15430,
172 16345
173};
174
175/* Maximum number of analogue outputs */
176#define SCARLETT2_ANALOGUE_MAX 10
177
178/* Maximum number of level and pad switches */
179#define SCARLETT2_LEVEL_SWITCH_MAX 2
180#define SCARLETT2_PAD_SWITCH_MAX 8
181#define SCARLETT2_AIR_SWITCH_MAX 8
182#define SCARLETT2_PHANTOM_SWITCH_MAX 2
183
184/* Maximum number of inputs to the mixer */
185#define SCARLETT2_INPUT_MIX_MAX 25
186
187/* Maximum number of outputs from the mixer */
188#define SCARLETT2_OUTPUT_MIX_MAX 12
189
190/* Maximum size of the data in the USB mux assignment message:
191 * 20 inputs, 20 outputs, 25 matrix inputs, 12 spare
192 */
193#define SCARLETT2_MUX_MAX 77
194
195/* Maximum number of meters (sum of output port counts) */
196#define SCARLETT2_MAX_METERS 65
197
198/* Hardware port types:
199 * - None (no input to mux)
200 * - Analogue I/O
201 * - S/PDIF I/O
202 * - ADAT I/O
203 * - Mixer I/O
204 * - PCM I/O
205 */
206enum {
207 SCARLETT2_PORT_TYPE_NONE = 0,
208 SCARLETT2_PORT_TYPE_ANALOGUE = 1,
209 SCARLETT2_PORT_TYPE_SPDIF = 2,
210 SCARLETT2_PORT_TYPE_ADAT = 3,
211 SCARLETT2_PORT_TYPE_MIX = 4,
212 SCARLETT2_PORT_TYPE_PCM = 5,
213 SCARLETT2_PORT_TYPE_COUNT = 6,
214};
215
216/* I/O count of each port type kept in struct scarlett2_ports */
217enum {
218 SCARLETT2_PORT_IN = 0,
219 SCARLETT2_PORT_OUT = 1,
220 SCARLETT2_PORT_DIRNS = 2,
221};
222
223/* Dim/Mute buttons on the 18i20 */
224enum {
225 SCARLETT2_BUTTON_MUTE = 0,
226 SCARLETT2_BUTTON_DIM = 1,
227 SCARLETT2_DIM_MUTE_COUNT = 2,
228};
229
230static const char *const scarlett2_dim_mute_names[SCARLETT2_DIM_MUTE_COUNT] = {
231 "Mute Playback Switch", "Dim Playback Switch"
232};
233
234/* Description of each hardware port type:
235 * - id: hardware ID of this port type
236 * - src_descr: printf format string for mux input selections
237 * - src_num_offset: added to channel number for the fprintf
238 * - dst_descr: printf format string for mixer controls
239 */
240struct scarlett2_port {
241 u16 id;
242 const char * const src_descr;
243 int src_num_offset;
244 const char * const dst_descr;
245};
246
247static const struct scarlett2_port scarlett2_ports[SCARLETT2_PORT_TYPE_COUNT] = {
248 [SCARLETT2_PORT_TYPE_NONE] = {
249 .id = 0x000,
250 .src_descr = "Off"
251 },
252 [SCARLETT2_PORT_TYPE_ANALOGUE] = {
253 .id = 0x080,
254 .src_descr = "Analogue %d",
255 .src_num_offset = 1,
256 .dst_descr = "Analogue Output %02d Playback"
257 },
258 [SCARLETT2_PORT_TYPE_SPDIF] = {
259 .id = 0x180,
260 .src_descr = "S/PDIF %d",
261 .src_num_offset = 1,
262 .dst_descr = "S/PDIF Output %d Playback"
263 },
264 [SCARLETT2_PORT_TYPE_ADAT] = {
265 .id = 0x200,
266 .src_descr = "ADAT %d",
267 .src_num_offset = 1,
268 .dst_descr = "ADAT Output %d Playback"
269 },
270 [SCARLETT2_PORT_TYPE_MIX] = {
271 .id = 0x300,
272 .src_descr = "Mix %c",
273 .src_num_offset = 'A',
274 .dst_descr = "Mixer Input %02d Capture"
275 },
276 [SCARLETT2_PORT_TYPE_PCM] = {
277 .id = 0x600,
278 .src_descr = "PCM %d",
279 .src_num_offset = 1,
280 .dst_descr = "PCM %02d Capture"
281 },
282};
283
284/* Number of mux tables: one for each band of sample rates
285 * (44.1/48kHz, 88.2/96kHz, and 176.4/176kHz)
286 */
287#define SCARLETT2_MUX_TABLES 3
288
289/* Maximum number of entries in a mux table */
290#define SCARLETT2_MAX_MUX_ENTRIES 10
291
292/* One entry within mux_assignment defines the port type and range of
293 * ports to add to the set_mux message. The end of the list is marked
294 * with count == 0.
295 */
296struct scarlett2_mux_entry {
297 u8 port_type;
298 u8 start;
299 u8 count;
300};
301
302struct scarlett2_device_info {
303 u32 usb_id; /* USB device identifier */
304
305 /* Gen 3 devices have an internal MSD mode switch that needs
306 * to be disabled in order to access the full functionality of
307 * the device.
308 */
309 u8 has_msd_mode;
310
311 /* Gen 3 devices without a mixer have a different
312 * configuration set
313 */
314 u8 has_mixer;
315
316 /* line out hw volume is sw controlled */
317 u8 line_out_hw_vol;
318
319 /* support for main/alt speaker switching */
320 u8 has_speaker_switching;
321
322 /* support for talkback microphone */
323 u8 has_talkback;
324
325 /* the number of analogue inputs with a software switchable
326 * level control that can be set to line or instrument
327 */
328 u8 level_input_count;
329
330 /* the first input with a level control (0-based) */
331 u8 level_input_first;
332
333 /* the number of analogue inputs with a software switchable
334 * 10dB pad control
335 */
336 u8 pad_input_count;
337
338 /* the number of analogue inputs with a software switchable
339 * "air" control
340 */
341 u8 air_input_count;
342
343 /* the number of phantom (48V) software switchable controls */
344 u8 phantom_count;
345
346 /* the number of inputs each phantom switch controls */
347 u8 inputs_per_phantom;
348
349 /* the number of direct monitor options
350 * (0 = none, 1 = mono only, 2 = mono/stereo)
351 */
352 u8 direct_monitor;
353
354 /* remap analogue outputs; 18i8 Gen 3 has "line 3/4" connected
355 * internally to the analogue 7/8 outputs
356 */
357 u8 line_out_remap_enable;
358 u8 line_out_remap[SCARLETT2_ANALOGUE_MAX];
359
360 /* additional description for the line out volume controls */
361 const char * const line_out_descrs[SCARLETT2_ANALOGUE_MAX];
362
363 /* number of sources/destinations of each port type */
364 const int port_count[SCARLETT2_PORT_TYPE_COUNT][SCARLETT2_PORT_DIRNS];
365
366 /* layout/order of the entries in the set_mux message */
367 struct scarlett2_mux_entry mux_assignment[SCARLETT2_MUX_TABLES]
368 [SCARLETT2_MAX_MUX_ENTRIES];
369};
370
371struct scarlett2_data {
372 struct usb_mixer_interface *mixer;
373 struct mutex usb_mutex; /* prevent sending concurrent USB requests */
374 struct mutex data_mutex; /* lock access to this data */
375 struct delayed_work work;
376 const struct scarlett2_device_info *info;
377 __u8 bInterfaceNumber;
378 __u8 bEndpointAddress;
379 __u16 wMaxPacketSize;
380 __u8 bInterval;
381 int num_mux_srcs;
382 int num_mux_dsts;
383 u16 scarlett2_seq;
384 u8 sync_updated;
385 u8 vol_updated;
386 u8 input_other_updated;
387 u8 monitor_other_updated;
388 u8 mux_updated;
389 u8 speaker_switching_switched;
390 u8 sync;
391 u8 master_vol;
392 u8 vol[SCARLETT2_ANALOGUE_MAX];
393 u8 vol_sw_hw_switch[SCARLETT2_ANALOGUE_MAX];
394 u8 mute_switch[SCARLETT2_ANALOGUE_MAX];
395 u8 level_switch[SCARLETT2_LEVEL_SWITCH_MAX];
396 u8 pad_switch[SCARLETT2_PAD_SWITCH_MAX];
397 u8 dim_mute[SCARLETT2_DIM_MUTE_COUNT];
398 u8 air_switch[SCARLETT2_AIR_SWITCH_MAX];
399 u8 phantom_switch[SCARLETT2_PHANTOM_SWITCH_MAX];
400 u8 phantom_persistence;
401 u8 direct_monitor_switch;
402 u8 speaker_switching_switch;
403 u8 talkback_switch;
404 u8 talkback_map[SCARLETT2_OUTPUT_MIX_MAX];
405 u8 msd_switch;
406 struct snd_kcontrol *sync_ctl;
407 struct snd_kcontrol *master_vol_ctl;
408 struct snd_kcontrol *vol_ctls[SCARLETT2_ANALOGUE_MAX];
409 struct snd_kcontrol *sw_hw_ctls[SCARLETT2_ANALOGUE_MAX];
410 struct snd_kcontrol *mute_ctls[SCARLETT2_ANALOGUE_MAX];
411 struct snd_kcontrol *dim_mute_ctls[SCARLETT2_DIM_MUTE_COUNT];
412 struct snd_kcontrol *level_ctls[SCARLETT2_LEVEL_SWITCH_MAX];
413 struct snd_kcontrol *pad_ctls[SCARLETT2_PAD_SWITCH_MAX];
414 struct snd_kcontrol *air_ctls[SCARLETT2_AIR_SWITCH_MAX];
415 struct snd_kcontrol *phantom_ctls[SCARLETT2_PHANTOM_SWITCH_MAX];
416 struct snd_kcontrol *mux_ctls[SCARLETT2_MUX_MAX];
417 struct snd_kcontrol *direct_monitor_ctl;
418 struct snd_kcontrol *speaker_switching_ctl;
419 struct snd_kcontrol *talkback_ctl;
420 u8 mux[SCARLETT2_MUX_MAX];
421 u8 mix[SCARLETT2_INPUT_MIX_MAX * SCARLETT2_OUTPUT_MIX_MAX];
422};
423
424/*** Model-specific data ***/
425
426static const struct scarlett2_device_info s6i6_gen2_info = {
427 .usb_id = USB_ID(0x1235, 0x8203),
428
429 .has_mixer = 1,
430 .level_input_count = 2,
431 .pad_input_count = 2,
432
433 .line_out_descrs = {
434 "Headphones 1 L",
435 "Headphones 1 R",
436 "Headphones 2 L",
437 "Headphones 2 R",
438 },
439
440 .port_count = {
441 [SCARLETT2_PORT_TYPE_NONE] = { 1, 0 },
442 [SCARLETT2_PORT_TYPE_ANALOGUE] = { 4, 4 },
443 [SCARLETT2_PORT_TYPE_SPDIF] = { 2, 2 },
444 [SCARLETT2_PORT_TYPE_MIX] = { 10, 18 },
445 [SCARLETT2_PORT_TYPE_PCM] = { 6, 6 },
446 },
447
448 .mux_assignment = { {
449 { SCARLETT2_PORT_TYPE_PCM, 0, 6 },
450 { SCARLETT2_PORT_TYPE_ANALOGUE, 0, 4 },
451 { SCARLETT2_PORT_TYPE_SPDIF, 0, 2 },
452 { SCARLETT2_PORT_TYPE_MIX, 0, 18 },
453 { SCARLETT2_PORT_TYPE_NONE, 0, 8 },
454 { 0, 0, 0 },
455 }, {
456 { SCARLETT2_PORT_TYPE_PCM, 0, 6 },
457 { SCARLETT2_PORT_TYPE_ANALOGUE, 0, 4 },
458 { SCARLETT2_PORT_TYPE_SPDIF, 0, 2 },
459 { SCARLETT2_PORT_TYPE_MIX, 0, 18 },
460 { SCARLETT2_PORT_TYPE_NONE, 0, 8 },
461 { 0, 0, 0 },
462 }, {
463 { SCARLETT2_PORT_TYPE_PCM, 0, 6 },
464 { SCARLETT2_PORT_TYPE_ANALOGUE, 0, 4 },
465 { SCARLETT2_PORT_TYPE_SPDIF, 0, 2 },
466 { SCARLETT2_PORT_TYPE_MIX, 0, 18 },
467 { SCARLETT2_PORT_TYPE_NONE, 0, 8 },
468 { 0, 0, 0 },
469 } },
470};
471
472static const struct scarlett2_device_info s18i8_gen2_info = {
473 .usb_id = USB_ID(0x1235, 0x8204),
474
475 .has_mixer = 1,
476 .level_input_count = 2,
477 .pad_input_count = 4,
478
479 .line_out_descrs = {
480 "Monitor L",
481 "Monitor R",
482 "Headphones 1 L",
483 "Headphones 1 R",
484 "Headphones 2 L",
485 "Headphones 2 R",
486 },
487
488 .port_count = {
489 [SCARLETT2_PORT_TYPE_NONE] = { 1, 0 },
490 [SCARLETT2_PORT_TYPE_ANALOGUE] = { 8, 6 },
491 [SCARLETT2_PORT_TYPE_SPDIF] = { 2, 2 },
492 [SCARLETT2_PORT_TYPE_ADAT] = { 8, 0 },
493 [SCARLETT2_PORT_TYPE_MIX] = { 10, 18 },
494 [SCARLETT2_PORT_TYPE_PCM] = { 8, 18 },
495 },
496
497 .mux_assignment = { {
498 { SCARLETT2_PORT_TYPE_PCM, 0, 18 },
499 { SCARLETT2_PORT_TYPE_ANALOGUE, 0, 6 },
500 { SCARLETT2_PORT_TYPE_SPDIF, 0, 2 },
501 { SCARLETT2_PORT_TYPE_MIX, 0, 18 },
502 { SCARLETT2_PORT_TYPE_NONE, 0, 8 },
503 { 0, 0, 0 },
504 }, {
505 { SCARLETT2_PORT_TYPE_PCM, 0, 14 },
506 { SCARLETT2_PORT_TYPE_ANALOGUE, 0, 6 },
507 { SCARLETT2_PORT_TYPE_SPDIF, 0, 2 },
508 { SCARLETT2_PORT_TYPE_MIX, 0, 18 },
509 { SCARLETT2_PORT_TYPE_NONE, 0, 8 },
510 { 0, 0, 0 },
511 }, {
512 { SCARLETT2_PORT_TYPE_PCM, 0, 10 },
513 { SCARLETT2_PORT_TYPE_ANALOGUE, 0, 6 },
514 { SCARLETT2_PORT_TYPE_SPDIF, 0, 2 },
515 { SCARLETT2_PORT_TYPE_MIX, 0, 18 },
516 { SCARLETT2_PORT_TYPE_NONE, 0, 4 },
517 { 0, 0, 0 },
518 } },
519};
520
521static const struct scarlett2_device_info s18i20_gen2_info = {
522 .usb_id = USB_ID(0x1235, 0x8201),
523
524 .has_mixer = 1,
525 .line_out_hw_vol = 1,
526
527 .line_out_descrs = {
528 "Monitor L",
529 "Monitor R",
530 NULL,
531 NULL,
532 NULL,
533 NULL,
534 "Headphones 1 L",
535 "Headphones 1 R",
536 "Headphones 2 L",
537 "Headphones 2 R",
538 },
539
540 .port_count = {
541 [SCARLETT2_PORT_TYPE_NONE] = { 1, 0 },
542 [SCARLETT2_PORT_TYPE_ANALOGUE] = { 8, 10 },
543 [SCARLETT2_PORT_TYPE_SPDIF] = { 2, 2 },
544 [SCARLETT2_PORT_TYPE_ADAT] = { 8, 8 },
545 [SCARLETT2_PORT_TYPE_MIX] = { 10, 18 },
546 [SCARLETT2_PORT_TYPE_PCM] = { 20, 18 },
547 },
548
549 .mux_assignment = { {
550 { SCARLETT2_PORT_TYPE_PCM, 0, 18 },
551 { SCARLETT2_PORT_TYPE_ANALOGUE, 0, 10 },
552 { SCARLETT2_PORT_TYPE_SPDIF, 0, 2 },
553 { SCARLETT2_PORT_TYPE_ADAT, 0, 8 },
554 { SCARLETT2_PORT_TYPE_MIX, 0, 18 },
555 { SCARLETT2_PORT_TYPE_NONE, 0, 8 },
556 { 0, 0, 0 },
557 }, {
558 { SCARLETT2_PORT_TYPE_PCM, 0, 14 },
559 { SCARLETT2_PORT_TYPE_ANALOGUE, 0, 10 },
560 { SCARLETT2_PORT_TYPE_SPDIF, 0, 2 },
561 { SCARLETT2_PORT_TYPE_ADAT, 0, 4 },
562 { SCARLETT2_PORT_TYPE_MIX, 0, 18 },
563 { SCARLETT2_PORT_TYPE_NONE, 0, 8 },
564 { 0, 0, 0 },
565 }, {
566 { SCARLETT2_PORT_TYPE_PCM, 0, 10 },
567 { SCARLETT2_PORT_TYPE_ANALOGUE, 0, 10 },
568 { SCARLETT2_PORT_TYPE_SPDIF, 0, 2 },
569 { SCARLETT2_PORT_TYPE_MIX, 0, 18 },
570 { SCARLETT2_PORT_TYPE_NONE, 0, 6 },
571 { 0, 0, 0 },
572 } },
573};
574
575static const struct scarlett2_device_info solo_gen3_info = {
576 .usb_id = USB_ID(0x1235, 0x8211),
577
578 .has_msd_mode = 1,
579 .level_input_count = 1,
580 .level_input_first = 1,
581 .air_input_count = 1,
582 .phantom_count = 1,
583 .inputs_per_phantom = 1,
584 .direct_monitor = 1,
585};
586
587static const struct scarlett2_device_info s2i2_gen3_info = {
588 .usb_id = USB_ID(0x1235, 0x8210),
589
590 .has_msd_mode = 1,
591 .level_input_count = 2,
592 .air_input_count = 2,
593 .phantom_count = 1,
594 .inputs_per_phantom = 2,
595 .direct_monitor = 2,
596};
597
598static const struct scarlett2_device_info s4i4_gen3_info = {
599 .usb_id = USB_ID(0x1235, 0x8212),
600
601 .has_msd_mode = 1,
602 .has_mixer = 1,
603 .level_input_count = 2,
604 .pad_input_count = 2,
605 .air_input_count = 2,
606 .phantom_count = 1,
607 .inputs_per_phantom = 2,
608
609 .line_out_descrs = {
610 "Monitor L",
611 "Monitor R",
612 "Headphones L",
613 "Headphones R",
614 },
615
616 .port_count = {
617 [SCARLETT2_PORT_TYPE_NONE] = { 1, 0 },
618 [SCARLETT2_PORT_TYPE_ANALOGUE] = { 4, 4 },
619 [SCARLETT2_PORT_TYPE_MIX] = { 6, 8 },
620 [SCARLETT2_PORT_TYPE_PCM] = { 4, 6 },
621 },
622
623 .mux_assignment = { {
624 { SCARLETT2_PORT_TYPE_PCM, 0, 6 },
625 { SCARLETT2_PORT_TYPE_ANALOGUE, 0, 4 },
626 { SCARLETT2_PORT_TYPE_MIX, 0, 8 },
627 { SCARLETT2_PORT_TYPE_NONE, 0, 16 },
628 { 0, 0, 0 },
629 }, {
630 { SCARLETT2_PORT_TYPE_PCM, 0, 6 },
631 { SCARLETT2_PORT_TYPE_ANALOGUE, 0, 4 },
632 { SCARLETT2_PORT_TYPE_MIX, 0, 8 },
633 { SCARLETT2_PORT_TYPE_NONE, 0, 16 },
634 { 0, 0, 0 },
635 }, {
636 { SCARLETT2_PORT_TYPE_PCM, 0, 6 },
637 { SCARLETT2_PORT_TYPE_ANALOGUE, 0, 4 },
638 { SCARLETT2_PORT_TYPE_MIX, 0, 8 },
639 { SCARLETT2_PORT_TYPE_NONE, 0, 16 },
640 { 0, 0, 0 },
641 } },
642};
643
644static const struct scarlett2_device_info s8i6_gen3_info = {
645 .usb_id = USB_ID(0x1235, 0x8213),
646
647 .has_msd_mode = 1,
648 .has_mixer = 1,
649 .level_input_count = 2,
650 .pad_input_count = 2,
651 .air_input_count = 2,
652 .phantom_count = 1,
653 .inputs_per_phantom = 2,
654
655 .line_out_descrs = {
656 "Headphones 1 L",
657 "Headphones 1 R",
658 "Headphones 2 L",
659 "Headphones 2 R",
660 },
661
662 .port_count = {
663 [SCARLETT2_PORT_TYPE_NONE] = { 1, 0 },
664 [SCARLETT2_PORT_TYPE_ANALOGUE] = { 6, 4 },
665 [SCARLETT2_PORT_TYPE_SPDIF] = { 2, 2 },
666 [SCARLETT2_PORT_TYPE_MIX] = { 8, 8 },
667 [SCARLETT2_PORT_TYPE_PCM] = { 6, 10 },
668 },
669
670 .mux_assignment = { {
671 { SCARLETT2_PORT_TYPE_PCM, 0, 8 },
672 { SCARLETT2_PORT_TYPE_ANALOGUE, 0, 4 },
673 { SCARLETT2_PORT_TYPE_SPDIF, 0, 2 },
674 { SCARLETT2_PORT_TYPE_PCM, 8, 2 },
675 { SCARLETT2_PORT_TYPE_MIX, 0, 8 },
676 { SCARLETT2_PORT_TYPE_NONE, 0, 18 },
677 { 0, 0, 0 },
678 }, {
679 { SCARLETT2_PORT_TYPE_PCM, 0, 8 },
680 { SCARLETT2_PORT_TYPE_ANALOGUE, 0, 4 },
681 { SCARLETT2_PORT_TYPE_SPDIF, 0, 2 },
682 { SCARLETT2_PORT_TYPE_PCM, 8, 2 },
683 { SCARLETT2_PORT_TYPE_MIX, 0, 8 },
684 { SCARLETT2_PORT_TYPE_NONE, 0, 18 },
685 { 0, 0, 0 },
686 }, {
687 { SCARLETT2_PORT_TYPE_PCM, 0, 8 },
688 { SCARLETT2_PORT_TYPE_ANALOGUE, 0, 4 },
689 { SCARLETT2_PORT_TYPE_SPDIF, 0, 2 },
690 { SCARLETT2_PORT_TYPE_PCM, 8, 2 },
691 { SCARLETT2_PORT_TYPE_MIX, 0, 8 },
692 { SCARLETT2_PORT_TYPE_NONE, 0, 18 },
693 { 0, 0, 0 },
694 } },
695};
696
697static const struct scarlett2_device_info s18i8_gen3_info = {
698 .usb_id = USB_ID(0x1235, 0x8214),
699
700 .has_msd_mode = 1,
701 .has_mixer = 1,
702 .line_out_hw_vol = 1,
703 .has_speaker_switching = 1,
704 .level_input_count = 2,
705 .pad_input_count = 4,
706 .air_input_count = 4,
707 .phantom_count = 2,
708 .inputs_per_phantom = 2,
709
710 .line_out_remap_enable = 1,
711 .line_out_remap = { 0, 1, 6, 7, 2, 3, 4, 5 },
712
713 .line_out_descrs = {
714 "Monitor L",
715 "Monitor R",
716 "Alt Monitor L",
717 "Alt Monitor R",
718 "Headphones 1 L",
719 "Headphones 1 R",
720 "Headphones 2 L",
721 "Headphones 2 R",
722 },
723
724 .port_count = {
725 [SCARLETT2_PORT_TYPE_NONE] = { 1, 0 },
726 [SCARLETT2_PORT_TYPE_ANALOGUE] = { 8, 8 },
727 [SCARLETT2_PORT_TYPE_SPDIF] = { 2, 2 },
728 [SCARLETT2_PORT_TYPE_ADAT] = { 8, 0 },
729 [SCARLETT2_PORT_TYPE_MIX] = { 10, 20 },
730 [SCARLETT2_PORT_TYPE_PCM] = { 8, 20 },
731 },
732
733 .mux_assignment = { {
734 { SCARLETT2_PORT_TYPE_PCM, 0, 10 },
735 { SCARLETT2_PORT_TYPE_PCM, 12, 8 },
736 { SCARLETT2_PORT_TYPE_ANALOGUE, 0, 2 },
737 { SCARLETT2_PORT_TYPE_ANALOGUE, 6, 2 },
738 { SCARLETT2_PORT_TYPE_ANALOGUE, 2, 4 },
739 { SCARLETT2_PORT_TYPE_SPDIF, 0, 2 },
740 { SCARLETT2_PORT_TYPE_PCM, 10, 2 },
741 { SCARLETT2_PORT_TYPE_MIX, 0, 20 },
742 { SCARLETT2_PORT_TYPE_NONE, 0, 10 },
743 { 0, 0, 0 },
744 }, {
745 { SCARLETT2_PORT_TYPE_PCM, 0, 10 },
746 { SCARLETT2_PORT_TYPE_PCM, 12, 4 },
747 { SCARLETT2_PORT_TYPE_ANALOGUE, 0, 2 },
748 { SCARLETT2_PORT_TYPE_ANALOGUE, 6, 2 },
749 { SCARLETT2_PORT_TYPE_ANALOGUE, 2, 4 },
750 { SCARLETT2_PORT_TYPE_SPDIF, 0, 2 },
751 { SCARLETT2_PORT_TYPE_PCM, 10, 2 },
752 { SCARLETT2_PORT_TYPE_MIX, 0, 20 },
753 { SCARLETT2_PORT_TYPE_NONE, 0, 10 },
754 { 0, 0, 0 },
755 }, {
756 { SCARLETT2_PORT_TYPE_PCM, 0, 10 },
757 { SCARLETT2_PORT_TYPE_ANALOGUE, 0, 2 },
758 { SCARLETT2_PORT_TYPE_ANALOGUE, 6, 2 },
759 { SCARLETT2_PORT_TYPE_ANALOGUE, 2, 4 },
760 { SCARLETT2_PORT_TYPE_SPDIF, 0, 2 },
761 { SCARLETT2_PORT_TYPE_MIX, 0, 20 },
762 { SCARLETT2_PORT_TYPE_NONE, 0, 10 },
763 { 0, 0, 0 },
764 } },
765};
766
767static const struct scarlett2_device_info s18i20_gen3_info = {
768 .usb_id = USB_ID(0x1235, 0x8215),
769
770 .has_msd_mode = 1,
771 .has_mixer = 1,
772 .line_out_hw_vol = 1,
773 .has_speaker_switching = 1,
774 .has_talkback = 1,
775 .level_input_count = 2,
776 .pad_input_count = 8,
777 .air_input_count = 8,
778 .phantom_count = 2,
779 .inputs_per_phantom = 4,
780
781 .line_out_descrs = {
782 "Monitor 1 L",
783 "Monitor 1 R",
784 "Monitor 2 L",
785 "Monitor 2 R",
786 NULL,
787 NULL,
788 "Headphones 1 L",
789 "Headphones 1 R",
790 "Headphones 2 L",
791 "Headphones 2 R",
792 },
793
794 .port_count = {
795 [SCARLETT2_PORT_TYPE_NONE] = { 1, 0 },
796 [SCARLETT2_PORT_TYPE_ANALOGUE] = { 9, 10 },
797 [SCARLETT2_PORT_TYPE_SPDIF] = { 2, 2 },
798 [SCARLETT2_PORT_TYPE_ADAT] = { 8, 8 },
799 [SCARLETT2_PORT_TYPE_MIX] = { 12, 25 },
800 [SCARLETT2_PORT_TYPE_PCM] = { 20, 20 },
801 },
802
803 .mux_assignment = { {
804 { SCARLETT2_PORT_TYPE_PCM, 0, 8 },
805 { SCARLETT2_PORT_TYPE_PCM, 10, 10 },
806 { SCARLETT2_PORT_TYPE_ANALOGUE, 0, 10 },
807 { SCARLETT2_PORT_TYPE_SPDIF, 0, 2 },
808 { SCARLETT2_PORT_TYPE_ADAT, 0, 8 },
809 { SCARLETT2_PORT_TYPE_PCM, 8, 2 },
810 { SCARLETT2_PORT_TYPE_MIX, 0, 25 },
811 { SCARLETT2_PORT_TYPE_NONE, 0, 12 },
812 { 0, 0, 0 },
813 }, {
814 { SCARLETT2_PORT_TYPE_PCM, 0, 8 },
815 { SCARLETT2_PORT_TYPE_PCM, 10, 8 },
816 { SCARLETT2_PORT_TYPE_ANALOGUE, 0, 10 },
817 { SCARLETT2_PORT_TYPE_SPDIF, 0, 2 },
818 { SCARLETT2_PORT_TYPE_ADAT, 0, 8 },
819 { SCARLETT2_PORT_TYPE_PCM, 8, 2 },
820 { SCARLETT2_PORT_TYPE_MIX, 0, 25 },
821 { SCARLETT2_PORT_TYPE_NONE, 0, 10 },
822 { 0, 0, 0 },
823 }, {
824 { SCARLETT2_PORT_TYPE_PCM, 0, 10 },
825 { SCARLETT2_PORT_TYPE_ANALOGUE, 0, 10 },
826 { SCARLETT2_PORT_TYPE_SPDIF, 0, 2 },
827 { SCARLETT2_PORT_TYPE_NONE, 0, 24 },
828 { 0, 0, 0 },
829 } },
830};
831
832static const struct scarlett2_device_info *scarlett2_devices[] = {
833 /* Supported Gen 2 devices */
834 &s6i6_gen2_info,
835 &s18i8_gen2_info,
836 &s18i20_gen2_info,
837
838 /* Supported Gen 3 devices */
839 &solo_gen3_info,
840 &s2i2_gen3_info,
841 &s4i4_gen3_info,
842 &s8i6_gen3_info,
843 &s18i8_gen3_info,
844 &s18i20_gen3_info,
845
846 /* End of list */
847 NULL
848};
849
850/* get the starting port index number for a given port type/direction */
851static int scarlett2_get_port_start_num(
852 const int port_count[][SCARLETT2_PORT_DIRNS],
853 int direction, int port_type)
854{
855 int i, num = 0;
856
857 for (i = 0; i < port_type; i++)
858 num += port_count[i][direction];
859
860 return num;
861}
862
863/*** USB Interactions ***/
864
865/* Notifications from the interface */
866#define SCARLETT2_USB_NOTIFY_SYNC 0x00000008
867#define SCARLETT2_USB_NOTIFY_DIM_MUTE 0x00200000
868#define SCARLETT2_USB_NOTIFY_MONITOR 0x00400000
869#define SCARLETT2_USB_NOTIFY_INPUT_OTHER 0x00800000
870#define SCARLETT2_USB_NOTIFY_MONITOR_OTHER 0x01000000
871
872/* Commands for sending/receiving requests/responses */
873#define SCARLETT2_USB_CMD_INIT 0
874#define SCARLETT2_USB_CMD_REQ 2
875#define SCARLETT2_USB_CMD_RESP 3
876
877#define SCARLETT2_USB_INIT_1 0x00000000
878#define SCARLETT2_USB_INIT_2 0x00000002
879#define SCARLETT2_USB_GET_METER 0x00001001
880#define SCARLETT2_USB_GET_MIX 0x00002001
881#define SCARLETT2_USB_SET_MIX 0x00002002
882#define SCARLETT2_USB_GET_MUX 0x00003001
883#define SCARLETT2_USB_SET_MUX 0x00003002
884#define SCARLETT2_USB_GET_SYNC 0x00006004
885#define SCARLETT2_USB_GET_DATA 0x00800000
886#define SCARLETT2_USB_SET_DATA 0x00800001
887#define SCARLETT2_USB_DATA_CMD 0x00800002
888
889#define SCARLETT2_USB_CONFIG_SAVE 6
890
891#define SCARLETT2_USB_VOLUME_STATUS_OFFSET 0x31
892#define SCARLETT2_USB_METER_LEVELS_GET_MAGIC 1
893
894/* volume status is read together (matches scarlett2_config_items[1]) */
895struct scarlett2_usb_volume_status {
896 /* dim/mute buttons */
897 u8 dim_mute[SCARLETT2_DIM_MUTE_COUNT];
898
899 u8 pad1;
900
901 /* software volume setting */
902 s16 sw_vol[SCARLETT2_ANALOGUE_MAX];
903
904 /* actual volume of output inc. dim (-18dB) */
905 s16 hw_vol[SCARLETT2_ANALOGUE_MAX];
906
907 /* internal mute buttons */
908 u8 mute_switch[SCARLETT2_ANALOGUE_MAX];
909
910 /* sw (0) or hw (1) controlled */
911 u8 sw_hw_switch[SCARLETT2_ANALOGUE_MAX];
912
913 u8 pad3[6];
914
915 /* front panel volume knob */
916 s16 master_vol;
917} __packed;
918
919/* Configuration parameters that can be read and written */
920enum {
921 SCARLETT2_CONFIG_DIM_MUTE = 0,
922 SCARLETT2_CONFIG_LINE_OUT_VOLUME = 1,
923 SCARLETT2_CONFIG_MUTE_SWITCH = 2,
924 SCARLETT2_CONFIG_SW_HW_SWITCH = 3,
925 SCARLETT2_CONFIG_LEVEL_SWITCH = 4,
926 SCARLETT2_CONFIG_PAD_SWITCH = 5,
927 SCARLETT2_CONFIG_MSD_SWITCH = 6,
928 SCARLETT2_CONFIG_AIR_SWITCH = 7,
929 SCARLETT2_CONFIG_PHANTOM_SWITCH = 8,
930 SCARLETT2_CONFIG_PHANTOM_PERSISTENCE = 9,
931 SCARLETT2_CONFIG_DIRECT_MONITOR = 10,
932 SCARLETT2_CONFIG_MONITOR_OTHER_SWITCH = 11,
933 SCARLETT2_CONFIG_MONITOR_OTHER_ENABLE = 12,
934 SCARLETT2_CONFIG_TALKBACK_MAP = 13,
935 SCARLETT2_CONFIG_COUNT = 14
936};
937
938/* Location, size, and activation command number for the configuration
939 * parameters. Size is in bits and may be 1, 8, or 16.
940 */
941struct scarlett2_config {
942 u8 offset;
943 u8 size;
944 u8 activate;
945};
946
947/* scarlett2_config_items[0] is for devices without a mixer
948 * scarlett2_config_items[1] is for devices with a mixer
949 */
950static const struct scarlett2_config
951 scarlett2_config_items[2][SCARLETT2_CONFIG_COUNT] =
952
953/* Devices without a mixer (Solo and 2i2 Gen 3) */
954{ {
955 [SCARLETT2_CONFIG_MSD_SWITCH] = {
956 .offset = 0x04, .size = 8, .activate = 6 },
957
958 [SCARLETT2_CONFIG_PHANTOM_PERSISTENCE] = {
959 .offset = 0x05, .size = 8, .activate = 6 },
960
961 [SCARLETT2_CONFIG_PHANTOM_SWITCH] = {
962 .offset = 0x06, .size = 8, .activate = 3 },
963
964 [SCARLETT2_CONFIG_DIRECT_MONITOR] = {
965 .offset = 0x07, .size = 8, .activate = 4 },
966
967 [SCARLETT2_CONFIG_LEVEL_SWITCH] = {
968 .offset = 0x08, .size = 1, .activate = 7 },
969
970 [SCARLETT2_CONFIG_AIR_SWITCH] = {
971 .offset = 0x09, .size = 1, .activate = 8 },
972
973/* Devices with a mixer (Gen 2 and all other Gen 3) */
974}, {
975 [SCARLETT2_CONFIG_DIM_MUTE] = {
976 .offset = 0x31, .size = 8, .activate = 2 },
977
978 [SCARLETT2_CONFIG_LINE_OUT_VOLUME] = {
979 .offset = 0x34, .size = 16, .activate = 1 },
980
981 [SCARLETT2_CONFIG_MUTE_SWITCH] = {
982 .offset = 0x5c, .size = 8, .activate = 1 },
983
984 [SCARLETT2_CONFIG_SW_HW_SWITCH] = {
985 .offset = 0x66, .size = 8, .activate = 3 },
986
987 [SCARLETT2_CONFIG_LEVEL_SWITCH] = {
988 .offset = 0x7c, .size = 8, .activate = 7 },
989
990 [SCARLETT2_CONFIG_PAD_SWITCH] = {
991 .offset = 0x84, .size = 8, .activate = 8 },
992
993 [SCARLETT2_CONFIG_AIR_SWITCH] = {
994 .offset = 0x8c, .size = 8, .activate = 8 },
995
996 [SCARLETT2_CONFIG_PHANTOM_SWITCH] = {
997 .offset = 0x9c, .size = 1, .activate = 8 },
998
999 [SCARLETT2_CONFIG_MSD_SWITCH] = {
1000 .offset = 0x9d, .size = 8, .activate = 6 },
1001
1002 [SCARLETT2_CONFIG_PHANTOM_PERSISTENCE] = {
1003 .offset = 0x9e, .size = 8, .activate = 6 },
1004
1005 [SCARLETT2_CONFIG_MONITOR_OTHER_SWITCH] = {
1006 .offset = 0x9f, .size = 1, .activate = 10 },
1007
1008 [SCARLETT2_CONFIG_MONITOR_OTHER_ENABLE] = {
1009 .offset = 0xa0, .size = 1, .activate = 10 },
1010
1011 [SCARLETT2_CONFIG_TALKBACK_MAP] = {
1012 .offset = 0xb0, .size = 16, .activate = 10 },
1013} };
1014
1015/* proprietary request/response format */
1016struct scarlett2_usb_packet {
1017 __le32 cmd;
1018 __le16 size;
1019 __le16 seq;
1020 __le32 error;
1021 __le32 pad;
1022 u8 data[];
1023};
1024
1025static void scarlett2_fill_request_header(struct scarlett2_data *private,
1026 struct scarlett2_usb_packet *req,
1027 u32 cmd, u16 req_size)
1028{
1029 /* sequence must go up by 1 for each request */
1030 u16 seq = private->scarlett2_seq++;
1031
1032 req->cmd = cpu_to_le32(cmd);
1033 req->size = cpu_to_le16(req_size);
1034 req->seq = cpu_to_le16(seq);
1035 req->error = 0;
1036 req->pad = 0;
1037}
1038
1039static int scarlett2_usb_tx(struct usb_device *dev, int interface,
1040 void *buf, u16 size)
1041{
1042 return snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0),
1043 SCARLETT2_USB_CMD_REQ,
1044 USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_OUT,
1045 0, interface, buf, size);
1046}
1047
1048static int scarlett2_usb_rx(struct usb_device *dev, int interface,
1049 u32 usb_req, void *buf, u16 size)
1050{
1051 return snd_usb_ctl_msg(dev, usb_rcvctrlpipe(dev, 0),
1052 usb_req,
1053 USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_IN,
1054 0, interface, buf, size);
1055}
1056
1057/* Send a proprietary format request to the Scarlett interface */
1058static int scarlett2_usb(
1059 struct usb_mixer_interface *mixer, u32 cmd,
1060 void *req_data, u16 req_size, void *resp_data, u16 resp_size)
1061{
1062 struct scarlett2_data *private = mixer->private_data;
1063 struct usb_device *dev = mixer->chip->dev;
1064 u16 req_buf_size = sizeof(struct scarlett2_usb_packet) + req_size;
1065 u16 resp_buf_size = sizeof(struct scarlett2_usb_packet) + resp_size;
1066 struct scarlett2_usb_packet *req, *resp = NULL;
1067 int err;
1068
1069 req = kmalloc(req_buf_size, GFP_KERNEL);
1070 if (!req) {
1071 err = -ENOMEM;
1072 goto error;
1073 }
1074
1075 resp = kmalloc(resp_buf_size, GFP_KERNEL);
1076 if (!resp) {
1077 err = -ENOMEM;
1078 goto error;
1079 }
1080
1081 mutex_lock(&private->usb_mutex);
1082
1083 /* build request message and send it */
1084
1085 scarlett2_fill_request_header(private, req, cmd, req_size);
1086
1087 if (req_size)
1088 memcpy(req->data, req_data, req_size);
1089
1090 err = scarlett2_usb_tx(dev, private->bInterfaceNumber,
1091 req, req_buf_size);
1092
1093 if (err != req_buf_size) {
1094 usb_audio_err(
1095 mixer->chip,
1096 "Scarlett Gen 2/3 USB request result cmd %x was %d\n",
1097 cmd, err);
1098 err = -EINVAL;
1099 goto unlock;
1100 }
1101
1102 /* send a second message to get the response */
1103
1104 err = scarlett2_usb_rx(dev, private->bInterfaceNumber,
1105 SCARLETT2_USB_CMD_RESP,
1106 resp, resp_buf_size);
1107
1108 /* validate the response */
1109
1110 if (err != resp_buf_size) {
1111 usb_audio_err(
1112 mixer->chip,
1113 "Scarlett Gen 2/3 USB response result cmd %x was %d "
1114 "expected %d\n",
1115 cmd, err, resp_buf_size);
1116 err = -EINVAL;
1117 goto unlock;
1118 }
1119
1120 /* cmd/seq/size should match except when initialising
1121 * seq sent = 1, response = 0
1122 */
1123 if (resp->cmd != req->cmd ||
1124 (resp->seq != req->seq &&
1125 (le16_to_cpu(req->seq) != 1 || resp->seq != 0)) ||
1126 resp_size != le16_to_cpu(resp->size) ||
1127 resp->error ||
1128 resp->pad) {
1129 usb_audio_err(
1130 mixer->chip,
1131 "Scarlett Gen 2/3 USB invalid response; "
1132 "cmd tx/rx %d/%d seq %d/%d size %d/%d "
1133 "error %d pad %d\n",
1134 le32_to_cpu(req->cmd), le32_to_cpu(resp->cmd),
1135 le16_to_cpu(req->seq), le16_to_cpu(resp->seq),
1136 resp_size, le16_to_cpu(resp->size),
1137 le32_to_cpu(resp->error),
1138 le32_to_cpu(resp->pad));
1139 err = -EINVAL;
1140 goto unlock;
1141 }
1142
1143 if (resp_data && resp_size > 0)
1144 memcpy(resp_data, resp->data, resp_size);
1145
1146unlock:
1147 mutex_unlock(&private->usb_mutex);
1148error:
1149 kfree(req);
1150 kfree(resp);
1151 return err;
1152}
1153
1154/* Send a USB message to get data; result placed in *buf */
1155static int scarlett2_usb_get(
1156 struct usb_mixer_interface *mixer,
1157 int offset, void *buf, int size)
1158{
1159 struct {
1160 __le32 offset;
1161 __le32 size;
1162 } __packed req;
1163
1164 req.offset = cpu_to_le32(offset);
1165 req.size = cpu_to_le32(size);
1166 return scarlett2_usb(mixer, SCARLETT2_USB_GET_DATA,
1167 &req, sizeof(req), buf, size);
1168}
1169
1170/* Send a USB message to get configuration parameters; result placed in *buf */
1171static int scarlett2_usb_get_config(
1172 struct usb_mixer_interface *mixer,
1173 int config_item_num, int count, void *buf)
1174{
1175 struct scarlett2_data *private = mixer->private_data;
1176 const struct scarlett2_device_info *info = private->info;
1177 const struct scarlett2_config *config_item =
1178 &scarlett2_config_items[info->has_mixer][config_item_num];
1179 int size, err, i;
1180 u8 *buf_8;
1181 u8 value;
1182
1183 /* For byte-sized parameters, retrieve directly into buf */
1184 if (config_item->size >= 8) {
1185 size = config_item->size / 8 * count;
1186 err = scarlett2_usb_get(mixer, config_item->offset, buf, size);
1187 if (err < 0)
1188 return err;
1189 if (size == 2) {
1190 u16 *buf_16 = buf;
1191
1192 for (i = 0; i < count; i++, buf_16++)
1193 *buf_16 = le16_to_cpu(*(__le16 *)buf_16);
1194 }
1195 return 0;
1196 }
1197
1198 /* For bit-sized parameters, retrieve into value */
1199 err = scarlett2_usb_get(mixer, config_item->offset, &value, 1);
1200 if (err < 0)
1201 return err;
1202
1203 /* then unpack from value into buf[] */
1204 buf_8 = buf;
1205 for (i = 0; i < 8 && i < count; i++, value >>= 1)
1206 *buf_8++ = value & 1;
1207
1208 return 0;
1209}
1210
1211/* Send SCARLETT2_USB_DATA_CMD SCARLETT2_USB_CONFIG_SAVE */
1212static void scarlett2_config_save(struct usb_mixer_interface *mixer)
1213{
1214 __le32 req = cpu_to_le32(SCARLETT2_USB_CONFIG_SAVE);
1215
1216 scarlett2_usb(mixer, SCARLETT2_USB_DATA_CMD,
1217 &req, sizeof(u32),
1218 NULL, 0);
1219}
1220
1221/* Delayed work to save config */
1222static void scarlett2_config_save_work(struct work_struct *work)
1223{
1224 struct scarlett2_data *private =
1225 container_of(work, struct scarlett2_data, work.work);
1226
1227 scarlett2_config_save(private->mixer);
1228}
1229
1230/* Send a USB message to set a SCARLETT2_CONFIG_* parameter */
1231static int scarlett2_usb_set_config(
1232 struct usb_mixer_interface *mixer,
1233 int config_item_num, int index, int value)
1234{
1235 struct scarlett2_data *private = mixer->private_data;
1236 const struct scarlett2_device_info *info = private->info;
1237 const struct scarlett2_config *config_item =
1238 &scarlett2_config_items[info->has_mixer][config_item_num];
1239 struct {
1240 __le32 offset;
1241 __le32 bytes;
1242 __le32 value;
1243 } __packed req;
1244 __le32 req2;
1245 int offset, size;
1246 int err;
1247
1248 /* Cancel any pending NVRAM save */
1249 cancel_delayed_work_sync(&private->work);
1250
1251 /* Convert config_item->size in bits to size in bytes and
1252 * calculate offset
1253 */
1254 if (config_item->size >= 8) {
1255 size = config_item->size / 8;
1256 offset = config_item->offset + index * size;
1257
1258 /* If updating a bit, retrieve the old value, set/clear the
1259 * bit as needed, and update value
1260 */
1261 } else {
1262 u8 tmp;
1263
1264 size = 1;
1265 offset = config_item->offset;
1266
1267 scarlett2_usb_get(mixer, offset, &tmp, 1);
1268 if (value)
1269 tmp |= (1 << index);
1270 else
1271 tmp &= ~(1 << index);
1272
1273 value = tmp;
1274 }
1275
1276 /* Send the configuration parameter data */
1277 req.offset = cpu_to_le32(offset);
1278 req.bytes = cpu_to_le32(size);
1279 req.value = cpu_to_le32(value);
1280 err = scarlett2_usb(mixer, SCARLETT2_USB_SET_DATA,
1281 &req, sizeof(u32) * 2 + size,
1282 NULL, 0);
1283 if (err < 0)
1284 return err;
1285
1286 /* Activate the change */
1287 req2 = cpu_to_le32(config_item->activate);
1288 err = scarlett2_usb(mixer, SCARLETT2_USB_DATA_CMD,
1289 &req2, sizeof(req2), NULL, 0);
1290 if (err < 0)
1291 return err;
1292
1293 /* Schedule the change to be written to NVRAM */
1294 if (config_item->activate != SCARLETT2_USB_CONFIG_SAVE)
1295 schedule_delayed_work(&private->work, msecs_to_jiffies(2000));
1296
1297 return 0;
1298}
1299
1300/* Send a USB message to get sync status; result placed in *sync */
1301static int scarlett2_usb_get_sync_status(
1302 struct usb_mixer_interface *mixer,
1303 u8 *sync)
1304{
1305 __le32 data;
1306 int err;
1307
1308 err = scarlett2_usb(mixer, SCARLETT2_USB_GET_SYNC,
1309 NULL, 0, &data, sizeof(data));
1310 if (err < 0)
1311 return err;
1312
1313 *sync = !!data;
1314 return 0;
1315}
1316
1317/* Send a USB message to get volume status; result placed in *buf */
1318static int scarlett2_usb_get_volume_status(
1319 struct usb_mixer_interface *mixer,
1320 struct scarlett2_usb_volume_status *buf)
1321{
1322 return scarlett2_usb_get(mixer, SCARLETT2_USB_VOLUME_STATUS_OFFSET,
1323 buf, sizeof(*buf));
1324}
1325
1326/* Send a USB message to get the volumes for all inputs of one mix
1327 * and put the values into private->mix[]
1328 */
1329static int scarlett2_usb_get_mix(struct usb_mixer_interface *mixer,
1330 int mix_num)
1331{
1332 struct scarlett2_data *private = mixer->private_data;
1333 const struct scarlett2_device_info *info = private->info;
1334
1335 int num_mixer_in =
1336 info->port_count[SCARLETT2_PORT_TYPE_MIX][SCARLETT2_PORT_OUT];
1337 int err, i, j, k;
1338
1339 struct {
1340 __le16 mix_num;
1341 __le16 count;
1342 } __packed req;
1343
1344 __le16 data[SCARLETT2_INPUT_MIX_MAX];
1345
1346 req.mix_num = cpu_to_le16(mix_num);
1347 req.count = cpu_to_le16(num_mixer_in);
1348
1349 err = scarlett2_usb(mixer, SCARLETT2_USB_GET_MIX,
1350 &req, sizeof(req),
1351 data, num_mixer_in * sizeof(u16));
1352 if (err < 0)
1353 return err;
1354
1355 for (i = 0, j = mix_num * num_mixer_in; i < num_mixer_in; i++, j++) {
1356 u16 mixer_value = le16_to_cpu(data[i]);
1357
1358 for (k = 0; k < SCARLETT2_MIXER_VALUE_COUNT; k++)
1359 if (scarlett2_mixer_values[k] >= mixer_value)
1360 break;
1361 if (k == SCARLETT2_MIXER_VALUE_COUNT)
1362 k = SCARLETT2_MIXER_MAX_VALUE;
1363 private->mix[j] = k;
1364 }
1365
1366 return 0;
1367}
1368
1369/* Send a USB message to set the volumes for all inputs of one mix
1370 * (values obtained from private->mix[])
1371 */
1372static int scarlett2_usb_set_mix(struct usb_mixer_interface *mixer,
1373 int mix_num)
1374{
1375 struct scarlett2_data *private = mixer->private_data;
1376 const struct scarlett2_device_info *info = private->info;
1377
1378 struct {
1379 __le16 mix_num;
1380 __le16 data[SCARLETT2_INPUT_MIX_MAX];
1381 } __packed req;
1382
1383 int i, j;
1384 int num_mixer_in =
1385 info->port_count[SCARLETT2_PORT_TYPE_MIX][SCARLETT2_PORT_OUT];
1386
1387 req.mix_num = cpu_to_le16(mix_num);
1388
1389 for (i = 0, j = mix_num * num_mixer_in; i < num_mixer_in; i++, j++)
1390 req.data[i] = cpu_to_le16(
1391 scarlett2_mixer_values[private->mix[j]]
1392 );
1393
1394 return scarlett2_usb(mixer, SCARLETT2_USB_SET_MIX,
1395 &req, (num_mixer_in + 1) * sizeof(u16),
1396 NULL, 0);
1397}
1398
1399/* Convert a port number index (per info->port_count) to a hardware ID */
1400static u32 scarlett2_mux_src_num_to_id(
1401 const int port_count[][SCARLETT2_PORT_DIRNS], int num)
1402{
1403 int port_type;
1404
1405 for (port_type = 0;
1406 port_type < SCARLETT2_PORT_TYPE_COUNT;
1407 port_type++) {
1408 if (num < port_count[port_type][SCARLETT2_PORT_IN])
1409 return scarlett2_ports[port_type].id | num;
1410 num -= port_count[port_type][SCARLETT2_PORT_IN];
1411 }
1412
1413 /* Oops */
1414 return 0;
1415}
1416
1417/* Convert a hardware ID to a port number index */
1418static u32 scarlett2_mux_id_to_num(
1419 const int port_count[][SCARLETT2_PORT_DIRNS], int direction, u32 id)
1420{
1421 int port_type;
1422 int port_num = 0;
1423
1424 for (port_type = 0;
1425 port_type < SCARLETT2_PORT_TYPE_COUNT;
1426 port_type++) {
1427 int base = scarlett2_ports[port_type].id;
1428 int count = port_count[port_type][direction];
1429
1430 if (id >= base && id < base + count)
1431 return port_num + id - base;
1432 port_num += count;
1433 }
1434
1435 /* Oops */
1436 return -1;
1437}
1438
1439/* Convert one mux entry from the interface and load into private->mux[] */
1440static void scarlett2_usb_populate_mux(struct scarlett2_data *private,
1441 u32 mux_entry)
1442{
1443 const struct scarlett2_device_info *info = private->info;
1444 const int (*port_count)[SCARLETT2_PORT_DIRNS] = info->port_count;
1445
1446 int dst_idx, src_idx;
1447
1448 dst_idx = scarlett2_mux_id_to_num(port_count, SCARLETT2_PORT_OUT,
1449 mux_entry & 0xFFF);
1450 if (dst_idx < 0)
1451 return;
1452
1453 if (dst_idx >= private->num_mux_dsts) {
1454 usb_audio_err(private->mixer->chip,
1455 "BUG: scarlett2_mux_id_to_num(%06x, OUT): %d >= %d",
1456 mux_entry, dst_idx, private->num_mux_dsts);
1457 return;
1458 }
1459
1460 src_idx = scarlett2_mux_id_to_num(port_count, SCARLETT2_PORT_IN,
1461 mux_entry >> 12);
1462 if (src_idx < 0)
1463 return;
1464
1465 if (src_idx >= private->num_mux_srcs) {
1466 usb_audio_err(private->mixer->chip,
1467 "BUG: scarlett2_mux_id_to_num(%06x, IN): %d >= %d",
1468 mux_entry, src_idx, private->num_mux_srcs);
1469 return;
1470 }
1471
1472 private->mux[dst_idx] = src_idx;
1473}
1474
1475/* Send USB message to get mux inputs and then populate private->mux[] */
1476static int scarlett2_usb_get_mux(struct usb_mixer_interface *mixer)
1477{
1478 struct scarlett2_data *private = mixer->private_data;
1479 int count = private->num_mux_dsts;
1480 int err, i;
1481
1482 struct {
1483 __le16 num;
1484 __le16 count;
1485 } __packed req;
1486
1487 __le32 data[SCARLETT2_MUX_MAX];
1488
1489 private->mux_updated = 0;
1490
1491 req.num = 0;
1492 req.count = cpu_to_le16(count);
1493
1494 err = scarlett2_usb(mixer, SCARLETT2_USB_GET_MUX,
1495 &req, sizeof(req),
1496 data, count * sizeof(u32));
1497 if (err < 0)
1498 return err;
1499
1500 for (i = 0; i < count; i++)
1501 scarlett2_usb_populate_mux(private, le32_to_cpu(data[i]));
1502
1503 return 0;
1504}
1505
1506/* Send USB messages to set mux inputs */
1507static int scarlett2_usb_set_mux(struct usb_mixer_interface *mixer)
1508{
1509 struct scarlett2_data *private = mixer->private_data;
1510 const struct scarlett2_device_info *info = private->info;
1511 const int (*port_count)[SCARLETT2_PORT_DIRNS] = info->port_count;
1512 int table;
1513
1514 struct {
1515 __le16 pad;
1516 __le16 num;
1517 __le32 data[SCARLETT2_MUX_MAX];
1518 } __packed req;
1519
1520 req.pad = 0;
1521
1522 /* set mux settings for each rate */
1523 for (table = 0; table < SCARLETT2_MUX_TABLES; table++) {
1524 const struct scarlett2_mux_entry *entry;
1525
1526 /* i counts over the output array */
1527 int i = 0, err;
1528
1529 req.num = cpu_to_le16(table);
1530
1531 /* loop through each entry */
1532 for (entry = info->mux_assignment[table];
1533 entry->count;
1534 entry++) {
1535 int j;
1536 int port_type = entry->port_type;
1537 int port_idx = entry->start;
1538 int mux_idx = scarlett2_get_port_start_num(port_count,
1539 SCARLETT2_PORT_OUT, port_type) + port_idx;
1540 int dst_id = scarlett2_ports[port_type].id + port_idx;
1541
1542 /* Empty slots */
1543 if (!dst_id) {
1544 for (j = 0; j < entry->count; j++)
1545 req.data[i++] = 0;
1546 continue;
1547 }
1548
1549 /* Non-empty mux slots use the lower 12 bits
1550 * for the destination and next 12 bits for
1551 * the source
1552 */
1553 for (j = 0; j < entry->count; j++) {
1554 int src_id = scarlett2_mux_src_num_to_id(
1555 port_count, private->mux[mux_idx++]);
1556 req.data[i++] = cpu_to_le32(dst_id |
1557 src_id << 12);
1558 dst_id++;
1559 }
1560 }
1561
1562 err = scarlett2_usb(mixer, SCARLETT2_USB_SET_MUX,
1563 &req, (i + 1) * sizeof(u32),
1564 NULL, 0);
1565 if (err < 0)
1566 return err;
1567 }
1568
1569 return 0;
1570}
1571
1572/* Send USB message to get meter levels */
1573static int scarlett2_usb_get_meter_levels(struct usb_mixer_interface *mixer,
1574 u16 num_meters, u16 *levels)
1575{
1576 struct {
1577 __le16 pad;
1578 __le16 num_meters;
1579 __le32 magic;
1580 } __packed req;
1581 u32 resp[SCARLETT2_MAX_METERS];
1582 int i, err;
1583
1584 req.pad = 0;
1585 req.num_meters = cpu_to_le16(num_meters);
1586 req.magic = cpu_to_le32(SCARLETT2_USB_METER_LEVELS_GET_MAGIC);
1587 err = scarlett2_usb(mixer, SCARLETT2_USB_GET_METER,
1588 &req, sizeof(req), resp, num_meters * sizeof(u32));
1589 if (err < 0)
1590 return err;
1591
1592 /* copy, convert to u16 */
1593 for (i = 0; i < num_meters; i++)
1594 levels[i] = resp[i];
1595
1596 return 0;
1597}
1598
1599/*** Control Functions ***/
1600
1601/* helper function to create a new control */
1602static int scarlett2_add_new_ctl(struct usb_mixer_interface *mixer,
1603 const struct snd_kcontrol_new *ncontrol,
1604 int index, int channels, const char *name,
1605 struct snd_kcontrol **kctl_return)
1606{
1607 struct snd_kcontrol *kctl;
1608 struct usb_mixer_elem_info *elem;
1609 int err;
1610
1611 elem = kzalloc(sizeof(*elem), GFP_KERNEL);
1612 if (!elem)
1613 return -ENOMEM;
1614
1615 /* We set USB_MIXER_BESPOKEN type, so that the core USB mixer code
1616 * ignores them for resume and other operations.
1617 * Also, the head.id field is set to 0, as we don't use this field.
1618 */
1619 elem->head.mixer = mixer;
1620 elem->control = index;
1621 elem->head.id = 0;
1622 elem->channels = channels;
1623 elem->val_type = USB_MIXER_BESPOKEN;
1624
1625 kctl = snd_ctl_new1(ncontrol, elem);
1626 if (!kctl) {
1627 kfree(elem);
1628 return -ENOMEM;
1629 }
1630 kctl->private_free = snd_usb_mixer_elem_free;
1631
1632 strscpy(kctl->id.name, name, sizeof(kctl->id.name));
1633
1634 err = snd_usb_mixer_add_control(&elem->head, kctl);
1635 if (err < 0)
1636 return err;
1637
1638 if (kctl_return)
1639 *kctl_return = kctl;
1640
1641 return 0;
1642}
1643
1644/*** Sync Control ***/
1645
1646/* Update sync control after receiving notification that the status
1647 * has changed
1648 */
1649static int scarlett2_update_sync(struct usb_mixer_interface *mixer)
1650{
1651 struct scarlett2_data *private = mixer->private_data;
1652
1653 private->sync_updated = 0;
1654 return scarlett2_usb_get_sync_status(mixer, &private->sync);
1655}
1656
1657static int scarlett2_sync_ctl_info(struct snd_kcontrol *kctl,
1658 struct snd_ctl_elem_info *uinfo)
1659{
1660 static const char *texts[2] = {
1661 "Unlocked", "Locked"
1662 };
1663 return snd_ctl_enum_info(uinfo, 1, 2, texts);
1664}
1665
1666static int scarlett2_sync_ctl_get(struct snd_kcontrol *kctl,
1667 struct snd_ctl_elem_value *ucontrol)
1668{
1669 struct usb_mixer_elem_info *elem = kctl->private_data;
1670 struct usb_mixer_interface *mixer = elem->head.mixer;
1671 struct scarlett2_data *private = mixer->private_data;
1672
1673 mutex_lock(&private->data_mutex);
1674 if (private->sync_updated)
1675 scarlett2_update_sync(mixer);
1676 ucontrol->value.enumerated.item[0] = private->sync;
1677 mutex_unlock(&private->data_mutex);
1678
1679 return 0;
1680}
1681
1682static const struct snd_kcontrol_new scarlett2_sync_ctl = {
1683 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1684 .access = SNDRV_CTL_ELEM_ACCESS_READ,
1685 .name = "",
1686 .info = scarlett2_sync_ctl_info,
1687 .get = scarlett2_sync_ctl_get
1688};
1689
1690static int scarlett2_add_sync_ctl(struct usb_mixer_interface *mixer)
1691{
1692 struct scarlett2_data *private = mixer->private_data;
1693
1694 /* devices without a mixer also don't support reporting sync status */
1695 if (!private->info->has_mixer)
1696 return 0;
1697
1698 return scarlett2_add_new_ctl(mixer, &scarlett2_sync_ctl,
1699 0, 1, "Sync Status", &private->sync_ctl);
1700}
1701
1702/*** Analogue Line Out Volume Controls ***/
1703
1704/* Update hardware volume controls after receiving notification that
1705 * they have changed
1706 */
1707static int scarlett2_update_volumes(struct usb_mixer_interface *mixer)
1708{
1709 struct scarlett2_data *private = mixer->private_data;
1710 const struct scarlett2_device_info *info = private->info;
1711 const int (*port_count)[SCARLETT2_PORT_DIRNS] = info->port_count;
1712 struct scarlett2_usb_volume_status volume_status;
1713 int num_line_out =
1714 port_count[SCARLETT2_PORT_TYPE_ANALOGUE][SCARLETT2_PORT_OUT];
1715 int err, i;
1716 int mute;
1717
1718 private->vol_updated = 0;
1719
1720 err = scarlett2_usb_get_volume_status(mixer, &volume_status);
1721 if (err < 0)
1722 return err;
1723
1724 private->master_vol = clamp(
1725 volume_status.master_vol + SCARLETT2_VOLUME_BIAS,
1726 0, SCARLETT2_VOLUME_BIAS);
1727
1728 if (info->line_out_hw_vol)
1729 for (i = 0; i < SCARLETT2_DIM_MUTE_COUNT; i++)
1730 private->dim_mute[i] = !!volume_status.dim_mute[i];
1731
1732 mute = private->dim_mute[SCARLETT2_BUTTON_MUTE];
1733
1734 for (i = 0; i < num_line_out; i++)
1735 if (private->vol_sw_hw_switch[i]) {
1736 private->vol[i] = private->master_vol;
1737 private->mute_switch[i] = mute;
1738 }
1739
1740 return 0;
1741}
1742
1743static int scarlett2_volume_ctl_info(struct snd_kcontrol *kctl,
1744 struct snd_ctl_elem_info *uinfo)
1745{
1746 struct usb_mixer_elem_info *elem = kctl->private_data;
1747
1748 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1749 uinfo->count = elem->channels;
1750 uinfo->value.integer.min = 0;
1751 uinfo->value.integer.max = SCARLETT2_VOLUME_BIAS;
1752 uinfo->value.integer.step = 1;
1753 return 0;
1754}
1755
1756static int scarlett2_master_volume_ctl_get(struct snd_kcontrol *kctl,
1757 struct snd_ctl_elem_value *ucontrol)
1758{
1759 struct usb_mixer_elem_info *elem = kctl->private_data;
1760 struct usb_mixer_interface *mixer = elem->head.mixer;
1761 struct scarlett2_data *private = mixer->private_data;
1762
1763 mutex_lock(&private->data_mutex);
1764 if (private->vol_updated)
1765 scarlett2_update_volumes(mixer);
1766 mutex_unlock(&private->data_mutex);
1767
1768 ucontrol->value.integer.value[0] = private->master_vol;
1769 return 0;
1770}
1771
1772static int line_out_remap(struct scarlett2_data *private, int index)
1773{
1774 const struct scarlett2_device_info *info = private->info;
1775
1776 if (!info->line_out_remap_enable)
1777 return index;
1778 return info->line_out_remap[index];
1779}
1780
1781static int scarlett2_volume_ctl_get(struct snd_kcontrol *kctl,
1782 struct snd_ctl_elem_value *ucontrol)
1783{
1784 struct usb_mixer_elem_info *elem = kctl->private_data;
1785 struct usb_mixer_interface *mixer = elem->head.mixer;
1786 struct scarlett2_data *private = mixer->private_data;
1787 int index = line_out_remap(private, elem->control);
1788
1789 mutex_lock(&private->data_mutex);
1790 if (private->vol_updated)
1791 scarlett2_update_volumes(mixer);
1792 mutex_unlock(&private->data_mutex);
1793
1794 ucontrol->value.integer.value[0] = private->vol[index];
1795 return 0;
1796}
1797
1798static int scarlett2_volume_ctl_put(struct snd_kcontrol *kctl,
1799 struct snd_ctl_elem_value *ucontrol)
1800{
1801 struct usb_mixer_elem_info *elem = kctl->private_data;
1802 struct usb_mixer_interface *mixer = elem->head.mixer;
1803 struct scarlett2_data *private = mixer->private_data;
1804 int index = line_out_remap(private, elem->control);
1805 int oval, val, err = 0;
1806
1807 mutex_lock(&private->data_mutex);
1808
1809 oval = private->vol[index];
1810 val = ucontrol->value.integer.value[0];
1811
1812 if (oval == val)
1813 goto unlock;
1814
1815 private->vol[index] = val;
1816 err = scarlett2_usb_set_config(mixer, SCARLETT2_CONFIG_LINE_OUT_VOLUME,
1817 index, val - SCARLETT2_VOLUME_BIAS);
1818 if (err == 0)
1819 err = 1;
1820
1821unlock:
1822 mutex_unlock(&private->data_mutex);
1823 return err;
1824}
1825
1826static const DECLARE_TLV_DB_MINMAX(
1827 db_scale_scarlett2_gain, -SCARLETT2_VOLUME_BIAS * 100, 0
1828);
1829
1830static const struct snd_kcontrol_new scarlett2_master_volume_ctl = {
1831 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1832 .access = SNDRV_CTL_ELEM_ACCESS_READ |
1833 SNDRV_CTL_ELEM_ACCESS_TLV_READ,
1834 .name = "",
1835 .info = scarlett2_volume_ctl_info,
1836 .get = scarlett2_master_volume_ctl_get,
1837 .private_value = 0, /* max value */
1838 .tlv = { .p = db_scale_scarlett2_gain }
1839};
1840
1841static const struct snd_kcontrol_new scarlett2_line_out_volume_ctl = {
1842 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1843 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE |
1844 SNDRV_CTL_ELEM_ACCESS_TLV_READ,
1845 .name = "",
1846 .info = scarlett2_volume_ctl_info,
1847 .get = scarlett2_volume_ctl_get,
1848 .put = scarlett2_volume_ctl_put,
1849 .private_value = 0, /* max value */
1850 .tlv = { .p = db_scale_scarlett2_gain }
1851};
1852
1853/*** Mute Switch Controls ***/
1854
1855static int scarlett2_mute_ctl_get(struct snd_kcontrol *kctl,
1856 struct snd_ctl_elem_value *ucontrol)
1857{
1858 struct usb_mixer_elem_info *elem = kctl->private_data;
1859 struct usb_mixer_interface *mixer = elem->head.mixer;
1860 struct scarlett2_data *private = mixer->private_data;
1861 int index = line_out_remap(private, elem->control);
1862
1863 mutex_lock(&private->data_mutex);
1864 if (private->vol_updated)
1865 scarlett2_update_volumes(mixer);
1866 mutex_unlock(&private->data_mutex);
1867
1868 ucontrol->value.integer.value[0] = private->mute_switch[index];
1869 return 0;
1870}
1871
1872static int scarlett2_mute_ctl_put(struct snd_kcontrol *kctl,
1873 struct snd_ctl_elem_value *ucontrol)
1874{
1875 struct usb_mixer_elem_info *elem = kctl->private_data;
1876 struct usb_mixer_interface *mixer = elem->head.mixer;
1877 struct scarlett2_data *private = mixer->private_data;
1878 int index = line_out_remap(private, elem->control);
1879 int oval, val, err = 0;
1880
1881 mutex_lock(&private->data_mutex);
1882
1883 oval = private->mute_switch[index];
1884 val = !!ucontrol->value.integer.value[0];
1885
1886 if (oval == val)
1887 goto unlock;
1888
1889 private->mute_switch[index] = val;
1890
1891 /* Send mute change to the device */
1892 err = scarlett2_usb_set_config(mixer, SCARLETT2_CONFIG_MUTE_SWITCH,
1893 index, val);
1894 if (err == 0)
1895 err = 1;
1896
1897unlock:
1898 mutex_unlock(&private->data_mutex);
1899 return err;
1900}
1901
1902static const struct snd_kcontrol_new scarlett2_mute_ctl = {
1903 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1904 .name = "",
1905 .info = snd_ctl_boolean_mono_info,
1906 .get = scarlett2_mute_ctl_get,
1907 .put = scarlett2_mute_ctl_put,
1908};
1909
1910/*** HW/SW Volume Switch Controls ***/
1911
1912static void scarlett2_sw_hw_ctl_ro(struct scarlett2_data *private, int index)
1913{
1914 private->sw_hw_ctls[index]->vd[0].access &=
1915 ~SNDRV_CTL_ELEM_ACCESS_WRITE;
1916}
1917
1918static void scarlett2_sw_hw_ctl_rw(struct scarlett2_data *private, int index)
1919{
1920 private->sw_hw_ctls[index]->vd[0].access |=
1921 SNDRV_CTL_ELEM_ACCESS_WRITE;
1922}
1923
1924static int scarlett2_sw_hw_enum_ctl_info(struct snd_kcontrol *kctl,
1925 struct snd_ctl_elem_info *uinfo)
1926{
1927 static const char *const values[2] = {
1928 "SW", "HW"
1929 };
1930
1931 return snd_ctl_enum_info(uinfo, 1, 2, values);
1932}
1933
1934static int scarlett2_sw_hw_enum_ctl_get(struct snd_kcontrol *kctl,
1935 struct snd_ctl_elem_value *ucontrol)
1936{
1937 struct usb_mixer_elem_info *elem = kctl->private_data;
1938 struct scarlett2_data *private = elem->head.mixer->private_data;
1939 int index = line_out_remap(private, elem->control);
1940
1941 ucontrol->value.enumerated.item[0] = private->vol_sw_hw_switch[index];
1942 return 0;
1943}
1944
1945static void scarlett2_vol_ctl_set_writable(struct usb_mixer_interface *mixer,
1946 int index, int value)
1947{
1948 struct scarlett2_data *private = mixer->private_data;
1949 struct snd_card *card = mixer->chip->card;
1950
1951 /* Set/Clear write bits */
1952 if (value) {
1953 private->vol_ctls[index]->vd[0].access |=
1954 SNDRV_CTL_ELEM_ACCESS_WRITE;
1955 private->mute_ctls[index]->vd[0].access |=
1956 SNDRV_CTL_ELEM_ACCESS_WRITE;
1957 } else {
1958 private->vol_ctls[index]->vd[0].access &=
1959 ~SNDRV_CTL_ELEM_ACCESS_WRITE;
1960 private->mute_ctls[index]->vd[0].access &=
1961 ~SNDRV_CTL_ELEM_ACCESS_WRITE;
1962 }
1963
1964 /* Notify of write bit and possible value change */
1965 snd_ctl_notify(card,
1966 SNDRV_CTL_EVENT_MASK_VALUE | SNDRV_CTL_EVENT_MASK_INFO,
1967 &private->vol_ctls[index]->id);
1968 snd_ctl_notify(card,
1969 SNDRV_CTL_EVENT_MASK_VALUE | SNDRV_CTL_EVENT_MASK_INFO,
1970 &private->mute_ctls[index]->id);
1971}
1972
1973static int scarlett2_sw_hw_change(struct usb_mixer_interface *mixer,
1974 int ctl_index, int val)
1975{
1976 struct scarlett2_data *private = mixer->private_data;
1977 int index = line_out_remap(private, ctl_index);
1978 int err;
1979
1980 private->vol_sw_hw_switch[index] = val;
1981
1982 /* Change access mode to RO (hardware controlled volume)
1983 * or RW (software controlled volume)
1984 */
1985 scarlett2_vol_ctl_set_writable(mixer, ctl_index, !val);
1986
1987 /* Reset volume/mute to master volume/mute */
1988 private->vol[index] = private->master_vol;
1989 private->mute_switch[index] = private->dim_mute[SCARLETT2_BUTTON_MUTE];
1990
1991 /* Set SW volume to current HW volume */
1992 err = scarlett2_usb_set_config(
1993 mixer, SCARLETT2_CONFIG_LINE_OUT_VOLUME,
1994 index, private->master_vol - SCARLETT2_VOLUME_BIAS);
1995 if (err < 0)
1996 return err;
1997
1998 /* Set SW mute to current HW mute */
1999 err = scarlett2_usb_set_config(
2000 mixer, SCARLETT2_CONFIG_MUTE_SWITCH,
2001 index, private->dim_mute[SCARLETT2_BUTTON_MUTE]);
2002 if (err < 0)
2003 return err;
2004
2005 /* Send SW/HW switch change to the device */
2006 return scarlett2_usb_set_config(mixer, SCARLETT2_CONFIG_SW_HW_SWITCH,
2007 index, val);
2008}
2009
2010static int scarlett2_sw_hw_enum_ctl_put(struct snd_kcontrol *kctl,
2011 struct snd_ctl_elem_value *ucontrol)
2012{
2013 struct usb_mixer_elem_info *elem = kctl->private_data;
2014 struct usb_mixer_interface *mixer = elem->head.mixer;
2015 struct scarlett2_data *private = mixer->private_data;
2016 int ctl_index = elem->control;
2017 int index = line_out_remap(private, ctl_index);
2018 int oval, val, err = 0;
2019
2020 mutex_lock(&private->data_mutex);
2021
2022 oval = private->vol_sw_hw_switch[index];
2023 val = !!ucontrol->value.enumerated.item[0];
2024
2025 if (oval == val)
2026 goto unlock;
2027
2028 err = scarlett2_sw_hw_change(mixer, ctl_index, val);
2029 if (err == 0)
2030 err = 1;
2031
2032unlock:
2033 mutex_unlock(&private->data_mutex);
2034 return err;
2035}
2036
2037static const struct snd_kcontrol_new scarlett2_sw_hw_enum_ctl = {
2038 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2039 .name = "",
2040 .info = scarlett2_sw_hw_enum_ctl_info,
2041 .get = scarlett2_sw_hw_enum_ctl_get,
2042 .put = scarlett2_sw_hw_enum_ctl_put,
2043};
2044
2045/*** Line Level/Instrument Level Switch Controls ***/
2046
2047static int scarlett2_update_input_other(struct usb_mixer_interface *mixer)
2048{
2049 struct scarlett2_data *private = mixer->private_data;
2050 const struct scarlett2_device_info *info = private->info;
2051
2052 private->input_other_updated = 0;
2053
2054 if (info->level_input_count) {
2055 int err = scarlett2_usb_get_config(
2056 mixer, SCARLETT2_CONFIG_LEVEL_SWITCH,
2057 info->level_input_count + info->level_input_first,
2058 private->level_switch);
2059 if (err < 0)
2060 return err;
2061 }
2062
2063 if (info->pad_input_count) {
2064 int err = scarlett2_usb_get_config(
2065 mixer, SCARLETT2_CONFIG_PAD_SWITCH,
2066 info->pad_input_count, private->pad_switch);
2067 if (err < 0)
2068 return err;
2069 }
2070
2071 if (info->air_input_count) {
2072 int err = scarlett2_usb_get_config(
2073 mixer, SCARLETT2_CONFIG_AIR_SWITCH,
2074 info->air_input_count, private->air_switch);
2075 if (err < 0)
2076 return err;
2077 }
2078
2079 if (info->phantom_count) {
2080 int err = scarlett2_usb_get_config(
2081 mixer, SCARLETT2_CONFIG_PHANTOM_SWITCH,
2082 info->phantom_count, private->phantom_switch);
2083 if (err < 0)
2084 return err;
2085
2086 err = scarlett2_usb_get_config(
2087 mixer, SCARLETT2_CONFIG_PHANTOM_PERSISTENCE,
2088 1, &private->phantom_persistence);
2089 if (err < 0)
2090 return err;
2091 }
2092
2093 return 0;
2094}
2095
2096static int scarlett2_level_enum_ctl_info(struct snd_kcontrol *kctl,
2097 struct snd_ctl_elem_info *uinfo)
2098{
2099 static const char *const values[2] = {
2100 "Line", "Inst"
2101 };
2102
2103 return snd_ctl_enum_info(uinfo, 1, 2, values);
2104}
2105
2106static int scarlett2_level_enum_ctl_get(struct snd_kcontrol *kctl,
2107 struct snd_ctl_elem_value *ucontrol)
2108{
2109 struct usb_mixer_elem_info *elem = kctl->private_data;
2110 struct usb_mixer_interface *mixer = elem->head.mixer;
2111 struct scarlett2_data *private = mixer->private_data;
2112 const struct scarlett2_device_info *info = private->info;
2113
2114 int index = elem->control + info->level_input_first;
2115
2116 mutex_lock(&private->data_mutex);
2117 if (private->input_other_updated)
2118 scarlett2_update_input_other(mixer);
2119 ucontrol->value.enumerated.item[0] = private->level_switch[index];
2120 mutex_unlock(&private->data_mutex);
2121
2122 return 0;
2123}
2124
2125static int scarlett2_level_enum_ctl_put(struct snd_kcontrol *kctl,
2126 struct snd_ctl_elem_value *ucontrol)
2127{
2128 struct usb_mixer_elem_info *elem = kctl->private_data;
2129 struct usb_mixer_interface *mixer = elem->head.mixer;
2130 struct scarlett2_data *private = mixer->private_data;
2131 const struct scarlett2_device_info *info = private->info;
2132
2133 int index = elem->control + info->level_input_first;
2134 int oval, val, err = 0;
2135
2136 mutex_lock(&private->data_mutex);
2137
2138 oval = private->level_switch[index];
2139 val = !!ucontrol->value.enumerated.item[0];
2140
2141 if (oval == val)
2142 goto unlock;
2143
2144 private->level_switch[index] = val;
2145
2146 /* Send switch change to the device */
2147 err = scarlett2_usb_set_config(mixer, SCARLETT2_CONFIG_LEVEL_SWITCH,
2148 index, val);
2149 if (err == 0)
2150 err = 1;
2151
2152unlock:
2153 mutex_unlock(&private->data_mutex);
2154 return err;
2155}
2156
2157static const struct snd_kcontrol_new scarlett2_level_enum_ctl = {
2158 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2159 .name = "",
2160 .info = scarlett2_level_enum_ctl_info,
2161 .get = scarlett2_level_enum_ctl_get,
2162 .put = scarlett2_level_enum_ctl_put,
2163};
2164
2165/*** Pad Switch Controls ***/
2166
2167static int scarlett2_pad_ctl_get(struct snd_kcontrol *kctl,
2168 struct snd_ctl_elem_value *ucontrol)
2169{
2170 struct usb_mixer_elem_info *elem = kctl->private_data;
2171 struct usb_mixer_interface *mixer = elem->head.mixer;
2172 struct scarlett2_data *private = mixer->private_data;
2173
2174 mutex_lock(&private->data_mutex);
2175 if (private->input_other_updated)
2176 scarlett2_update_input_other(mixer);
2177 ucontrol->value.integer.value[0] =
2178 private->pad_switch[elem->control];
2179 mutex_unlock(&private->data_mutex);
2180
2181 return 0;
2182}
2183
2184static int scarlett2_pad_ctl_put(struct snd_kcontrol *kctl,
2185 struct snd_ctl_elem_value *ucontrol)
2186{
2187 struct usb_mixer_elem_info *elem = kctl->private_data;
2188 struct usb_mixer_interface *mixer = elem->head.mixer;
2189 struct scarlett2_data *private = mixer->private_data;
2190
2191 int index = elem->control;
2192 int oval, val, err = 0;
2193
2194 mutex_lock(&private->data_mutex);
2195
2196 oval = private->pad_switch[index];
2197 val = !!ucontrol->value.integer.value[0];
2198
2199 if (oval == val)
2200 goto unlock;
2201
2202 private->pad_switch[index] = val;
2203
2204 /* Send switch change to the device */
2205 err = scarlett2_usb_set_config(mixer, SCARLETT2_CONFIG_PAD_SWITCH,
2206 index, val);
2207 if (err == 0)
2208 err = 1;
2209
2210unlock:
2211 mutex_unlock(&private->data_mutex);
2212 return err;
2213}
2214
2215static const struct snd_kcontrol_new scarlett2_pad_ctl = {
2216 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2217 .name = "",
2218 .info = snd_ctl_boolean_mono_info,
2219 .get = scarlett2_pad_ctl_get,
2220 .put = scarlett2_pad_ctl_put,
2221};
2222
2223/*** Air Switch Controls ***/
2224
2225static int scarlett2_air_ctl_get(struct snd_kcontrol *kctl,
2226 struct snd_ctl_elem_value *ucontrol)
2227{
2228 struct usb_mixer_elem_info *elem = kctl->private_data;
2229 struct usb_mixer_interface *mixer = elem->head.mixer;
2230 struct scarlett2_data *private = mixer->private_data;
2231
2232 mutex_lock(&private->data_mutex);
2233 if (private->input_other_updated)
2234 scarlett2_update_input_other(mixer);
2235 ucontrol->value.integer.value[0] = private->air_switch[elem->control];
2236 mutex_unlock(&private->data_mutex);
2237
2238 return 0;
2239}
2240
2241static int scarlett2_air_ctl_put(struct snd_kcontrol *kctl,
2242 struct snd_ctl_elem_value *ucontrol)
2243{
2244 struct usb_mixer_elem_info *elem = kctl->private_data;
2245 struct usb_mixer_interface *mixer = elem->head.mixer;
2246 struct scarlett2_data *private = mixer->private_data;
2247
2248 int index = elem->control;
2249 int oval, val, err = 0;
2250
2251 mutex_lock(&private->data_mutex);
2252
2253 oval = private->air_switch[index];
2254 val = !!ucontrol->value.integer.value[0];
2255
2256 if (oval == val)
2257 goto unlock;
2258
2259 private->air_switch[index] = val;
2260
2261 /* Send switch change to the device */
2262 err = scarlett2_usb_set_config(mixer, SCARLETT2_CONFIG_AIR_SWITCH,
2263 index, val);
2264 if (err == 0)
2265 err = 1;
2266
2267unlock:
2268 mutex_unlock(&private->data_mutex);
2269 return err;
2270}
2271
2272static const struct snd_kcontrol_new scarlett2_air_ctl = {
2273 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2274 .name = "",
2275 .info = snd_ctl_boolean_mono_info,
2276 .get = scarlett2_air_ctl_get,
2277 .put = scarlett2_air_ctl_put,
2278};
2279
2280/*** Phantom Switch Controls ***/
2281
2282static int scarlett2_phantom_ctl_get(struct snd_kcontrol *kctl,
2283 struct snd_ctl_elem_value *ucontrol)
2284{
2285 struct usb_mixer_elem_info *elem = kctl->private_data;
2286 struct usb_mixer_interface *mixer = elem->head.mixer;
2287 struct scarlett2_data *private = mixer->private_data;
2288
2289 mutex_lock(&private->data_mutex);
2290 if (private->input_other_updated)
2291 scarlett2_update_input_other(mixer);
2292 ucontrol->value.integer.value[0] =
2293 private->phantom_switch[elem->control];
2294 mutex_unlock(&private->data_mutex);
2295
2296 return 0;
2297}
2298
2299static int scarlett2_phantom_ctl_put(struct snd_kcontrol *kctl,
2300 struct snd_ctl_elem_value *ucontrol)
2301{
2302 struct usb_mixer_elem_info *elem = kctl->private_data;
2303 struct usb_mixer_interface *mixer = elem->head.mixer;
2304 struct scarlett2_data *private = mixer->private_data;
2305
2306 int index = elem->control;
2307 int oval, val, err = 0;
2308
2309 mutex_lock(&private->data_mutex);
2310
2311 oval = private->phantom_switch[index];
2312 val = !!ucontrol->value.integer.value[0];
2313
2314 if (oval == val)
2315 goto unlock;
2316
2317 private->phantom_switch[index] = val;
2318
2319 /* Send switch change to the device */
2320 err = scarlett2_usb_set_config(mixer, SCARLETT2_CONFIG_PHANTOM_SWITCH,
2321 index, val);
2322 if (err == 0)
2323 err = 1;
2324
2325unlock:
2326 mutex_unlock(&private->data_mutex);
2327 return err;
2328}
2329
2330static const struct snd_kcontrol_new scarlett2_phantom_ctl = {
2331 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2332 .name = "",
2333 .info = snd_ctl_boolean_mono_info,
2334 .get = scarlett2_phantom_ctl_get,
2335 .put = scarlett2_phantom_ctl_put,
2336};
2337
2338/*** Phantom Persistence Control ***/
2339
2340static int scarlett2_phantom_persistence_ctl_get(
2341 struct snd_kcontrol *kctl, struct snd_ctl_elem_value *ucontrol)
2342{
2343 struct usb_mixer_elem_info *elem = kctl->private_data;
2344 struct scarlett2_data *private = elem->head.mixer->private_data;
2345
2346 ucontrol->value.integer.value[0] = private->phantom_persistence;
2347 return 0;
2348}
2349
2350static int scarlett2_phantom_persistence_ctl_put(
2351 struct snd_kcontrol *kctl, struct snd_ctl_elem_value *ucontrol)
2352{
2353 struct usb_mixer_elem_info *elem = kctl->private_data;
2354 struct usb_mixer_interface *mixer = elem->head.mixer;
2355 struct scarlett2_data *private = mixer->private_data;
2356
2357 int index = elem->control;
2358 int oval, val, err = 0;
2359
2360 mutex_lock(&private->data_mutex);
2361
2362 oval = private->phantom_persistence;
2363 val = !!ucontrol->value.integer.value[0];
2364
2365 if (oval == val)
2366 goto unlock;
2367
2368 private->phantom_persistence = val;
2369
2370 /* Send switch change to the device */
2371 err = scarlett2_usb_set_config(
2372 mixer, SCARLETT2_CONFIG_PHANTOM_PERSISTENCE, index, val);
2373 if (err == 0)
2374 err = 1;
2375
2376unlock:
2377 mutex_unlock(&private->data_mutex);
2378 return err;
2379}
2380
2381static const struct snd_kcontrol_new scarlett2_phantom_persistence_ctl = {
2382 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2383 .name = "",
2384 .info = snd_ctl_boolean_mono_info,
2385 .get = scarlett2_phantom_persistence_ctl_get,
2386 .put = scarlett2_phantom_persistence_ctl_put,
2387};
2388
2389/*** Direct Monitor Control ***/
2390
2391static int scarlett2_update_monitor_other(struct usb_mixer_interface *mixer)
2392{
2393 struct scarlett2_data *private = mixer->private_data;
2394 const struct scarlett2_device_info *info = private->info;
2395 int err;
2396
2397 /* monitor_other_enable[0] enables speaker switching
2398 * monitor_other_enable[1] enables talkback
2399 */
2400 u8 monitor_other_enable[2];
2401
2402 /* monitor_other_switch[0] activates the alternate speakers
2403 * monitor_other_switch[1] activates talkback
2404 */
2405 u8 monitor_other_switch[2];
2406
2407 private->monitor_other_updated = 0;
2408
2409 if (info->direct_monitor)
2410 return scarlett2_usb_get_config(
2411 mixer, SCARLETT2_CONFIG_DIRECT_MONITOR,
2412 1, &private->direct_monitor_switch);
2413
2414 /* if it doesn't do speaker switching then it also doesn't do
2415 * talkback
2416 */
2417 if (!info->has_speaker_switching)
2418 return 0;
2419
2420 err = scarlett2_usb_get_config(
2421 mixer, SCARLETT2_CONFIG_MONITOR_OTHER_ENABLE,
2422 2, monitor_other_enable);
2423 if (err < 0)
2424 return err;
2425
2426 err = scarlett2_usb_get_config(
2427 mixer, SCARLETT2_CONFIG_MONITOR_OTHER_SWITCH,
2428 2, monitor_other_switch);
2429 if (err < 0)
2430 return err;
2431
2432 if (!monitor_other_enable[0])
2433 private->speaker_switching_switch = 0;
2434 else
2435 private->speaker_switching_switch = monitor_other_switch[0] + 1;
2436
2437 if (info->has_talkback) {
2438 const int (*port_count)[SCARLETT2_PORT_DIRNS] =
2439 info->port_count;
2440 int num_mixes =
2441 port_count[SCARLETT2_PORT_TYPE_MIX][SCARLETT2_PORT_IN];
2442 u16 bitmap;
2443 int i;
2444
2445 if (!monitor_other_enable[1])
2446 private->talkback_switch = 0;
2447 else
2448 private->talkback_switch = monitor_other_switch[1] + 1;
2449
2450 err = scarlett2_usb_get_config(mixer,
2451 SCARLETT2_CONFIG_TALKBACK_MAP,
2452 1, &bitmap);
2453 if (err < 0)
2454 return err;
2455 for (i = 0; i < num_mixes; i++, bitmap >>= 1)
2456 private->talkback_map[i] = bitmap & 1;
2457 }
2458
2459 return 0;
2460}
2461
2462static int scarlett2_direct_monitor_ctl_get(
2463 struct snd_kcontrol *kctl, struct snd_ctl_elem_value *ucontrol)
2464{
2465 struct usb_mixer_elem_info *elem = kctl->private_data;
2466 struct usb_mixer_interface *mixer = elem->head.mixer;
2467 struct scarlett2_data *private = elem->head.mixer->private_data;
2468
2469 mutex_lock(&private->data_mutex);
2470 if (private->monitor_other_updated)
2471 scarlett2_update_monitor_other(mixer);
2472 ucontrol->value.enumerated.item[0] = private->direct_monitor_switch;
2473 mutex_unlock(&private->data_mutex);
2474
2475 return 0;
2476}
2477
2478static int scarlett2_direct_monitor_ctl_put(
2479 struct snd_kcontrol *kctl, struct snd_ctl_elem_value *ucontrol)
2480{
2481 struct usb_mixer_elem_info *elem = kctl->private_data;
2482 struct usb_mixer_interface *mixer = elem->head.mixer;
2483 struct scarlett2_data *private = mixer->private_data;
2484
2485 int index = elem->control;
2486 int oval, val, err = 0;
2487
2488 mutex_lock(&private->data_mutex);
2489
2490 oval = private->direct_monitor_switch;
2491 val = min(ucontrol->value.enumerated.item[0], 2U);
2492
2493 if (oval == val)
2494 goto unlock;
2495
2496 private->direct_monitor_switch = val;
2497
2498 /* Send switch change to the device */
2499 err = scarlett2_usb_set_config(
2500 mixer, SCARLETT2_CONFIG_DIRECT_MONITOR, index, val);
2501 if (err == 0)
2502 err = 1;
2503
2504unlock:
2505 mutex_unlock(&private->data_mutex);
2506 return err;
2507}
2508
2509static int scarlett2_direct_monitor_stereo_enum_ctl_info(
2510 struct snd_kcontrol *kctl, struct snd_ctl_elem_info *uinfo)
2511{
2512 static const char *const values[3] = {
2513 "Off", "Mono", "Stereo"
2514 };
2515
2516 return snd_ctl_enum_info(uinfo, 1, 3, values);
2517}
2518
2519/* Direct Monitor for Solo is mono-only and only needs a boolean control
2520 * Direct Monitor for 2i2 is selectable between Off/Mono/Stereo
2521 */
2522static const struct snd_kcontrol_new scarlett2_direct_monitor_ctl[2] = {
2523 {
2524 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2525 .name = "",
2526 .info = snd_ctl_boolean_mono_info,
2527 .get = scarlett2_direct_monitor_ctl_get,
2528 .put = scarlett2_direct_monitor_ctl_put,
2529 },
2530 {
2531 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2532 .name = "",
2533 .info = scarlett2_direct_monitor_stereo_enum_ctl_info,
2534 .get = scarlett2_direct_monitor_ctl_get,
2535 .put = scarlett2_direct_monitor_ctl_put,
2536 }
2537};
2538
2539static int scarlett2_add_direct_monitor_ctl(struct usb_mixer_interface *mixer)
2540{
2541 struct scarlett2_data *private = mixer->private_data;
2542 const struct scarlett2_device_info *info = private->info;
2543 const char *s;
2544
2545 if (!info->direct_monitor)
2546 return 0;
2547
2548 s = info->direct_monitor == 1
2549 ? "Direct Monitor Playback Switch"
2550 : "Direct Monitor Playback Enum";
2551
2552 return scarlett2_add_new_ctl(
2553 mixer, &scarlett2_direct_monitor_ctl[info->direct_monitor - 1],
2554 0, 1, s, &private->direct_monitor_ctl);
2555}
2556
2557/*** Speaker Switching Control ***/
2558
2559static int scarlett2_speaker_switch_enum_ctl_info(
2560 struct snd_kcontrol *kctl, struct snd_ctl_elem_info *uinfo)
2561{
2562 static const char *const values[3] = {
2563 "Off", "Main", "Alt"
2564 };
2565
2566 return snd_ctl_enum_info(uinfo, 1, 3, values);
2567}
2568
2569static int scarlett2_speaker_switch_enum_ctl_get(
2570 struct snd_kcontrol *kctl, struct snd_ctl_elem_value *ucontrol)
2571{
2572 struct usb_mixer_elem_info *elem = kctl->private_data;
2573 struct usb_mixer_interface *mixer = elem->head.mixer;
2574 struct scarlett2_data *private = mixer->private_data;
2575
2576 mutex_lock(&private->data_mutex);
2577 if (private->monitor_other_updated)
2578 scarlett2_update_monitor_other(mixer);
2579 ucontrol->value.enumerated.item[0] = private->speaker_switching_switch;
2580 mutex_unlock(&private->data_mutex);
2581
2582 return 0;
2583}
2584
2585/* when speaker switching gets enabled, switch the main/alt speakers
2586 * to HW volume and disable those controls
2587 */
2588static int scarlett2_speaker_switch_enable(struct usb_mixer_interface *mixer)
2589{
2590 struct snd_card *card = mixer->chip->card;
2591 struct scarlett2_data *private = mixer->private_data;
2592 int i, err;
2593
2594 for (i = 0; i < 4; i++) {
2595 int index = line_out_remap(private, i);
2596
2597 /* switch the main/alt speakers to HW volume */
2598 if (!private->vol_sw_hw_switch[index]) {
2599 err = scarlett2_sw_hw_change(private->mixer, i, 1);
2600 if (err < 0)
2601 return err;
2602 }
2603
2604 /* disable the line out SW/HW switch */
2605 scarlett2_sw_hw_ctl_ro(private, i);
2606 snd_ctl_notify(card,
2607 SNDRV_CTL_EVENT_MASK_VALUE |
2608 SNDRV_CTL_EVENT_MASK_INFO,
2609 &private->sw_hw_ctls[i]->id);
2610 }
2611
2612 /* when the next monitor-other notify comes in, update the mux
2613 * configuration
2614 */
2615 private->speaker_switching_switched = 1;
2616
2617 return 0;
2618}
2619
2620/* when speaker switching gets disabled, reenable the hw/sw controls
2621 * and invalidate the routing
2622 */
2623static void scarlett2_speaker_switch_disable(struct usb_mixer_interface *mixer)
2624{
2625 struct snd_card *card = mixer->chip->card;
2626 struct scarlett2_data *private = mixer->private_data;
2627 int i;
2628
2629 /* enable the line out SW/HW switch */
2630 for (i = 0; i < 4; i++) {
2631 scarlett2_sw_hw_ctl_rw(private, i);
2632 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_INFO,
2633 &private->sw_hw_ctls[i]->id);
2634 }
2635
2636 /* when the next monitor-other notify comes in, update the mux
2637 * configuration
2638 */
2639 private->speaker_switching_switched = 1;
2640}
2641
2642static int scarlett2_speaker_switch_enum_ctl_put(
2643 struct snd_kcontrol *kctl, struct snd_ctl_elem_value *ucontrol)
2644{
2645 struct usb_mixer_elem_info *elem = kctl->private_data;
2646 struct usb_mixer_interface *mixer = elem->head.mixer;
2647 struct scarlett2_data *private = mixer->private_data;
2648
2649 int oval, val, err = 0;
2650
2651 mutex_lock(&private->data_mutex);
2652
2653 oval = private->speaker_switching_switch;
2654 val = min(ucontrol->value.enumerated.item[0], 2U);
2655
2656 if (oval == val)
2657 goto unlock;
2658
2659 private->speaker_switching_switch = val;
2660
2661 /* enable/disable speaker switching */
2662 err = scarlett2_usb_set_config(
2663 mixer, SCARLETT2_CONFIG_MONITOR_OTHER_ENABLE,
2664 0, !!val);
2665 if (err < 0)
2666 goto unlock;
2667
2668 /* if speaker switching is enabled, select main or alt */
2669 err = scarlett2_usb_set_config(
2670 mixer, SCARLETT2_CONFIG_MONITOR_OTHER_SWITCH,
2671 0, val == 2);
2672 if (err < 0)
2673 goto unlock;
2674
2675 /* update controls if speaker switching gets enabled or disabled */
2676 if (!oval && val)
2677 err = scarlett2_speaker_switch_enable(mixer);
2678 else if (oval && !val)
2679 scarlett2_speaker_switch_disable(mixer);
2680
2681 if (err == 0)
2682 err = 1;
2683
2684unlock:
2685 mutex_unlock(&private->data_mutex);
2686 return err;
2687}
2688
2689static const struct snd_kcontrol_new scarlett2_speaker_switch_enum_ctl = {
2690 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2691 .name = "",
2692 .info = scarlett2_speaker_switch_enum_ctl_info,
2693 .get = scarlett2_speaker_switch_enum_ctl_get,
2694 .put = scarlett2_speaker_switch_enum_ctl_put,
2695};
2696
2697static int scarlett2_add_speaker_switch_ctl(
2698 struct usb_mixer_interface *mixer)
2699{
2700 struct scarlett2_data *private = mixer->private_data;
2701 const struct scarlett2_device_info *info = private->info;
2702
2703 if (!info->has_speaker_switching)
2704 return 0;
2705
2706 return scarlett2_add_new_ctl(
2707 mixer, &scarlett2_speaker_switch_enum_ctl,
2708 0, 1, "Speaker Switching Playback Enum",
2709 &private->speaker_switching_ctl);
2710}
2711
2712/*** Talkback and Talkback Map Controls ***/
2713
2714static int scarlett2_talkback_enum_ctl_info(
2715 struct snd_kcontrol *kctl, struct snd_ctl_elem_info *uinfo)
2716{
2717 static const char *const values[3] = {
2718 "Disabled", "Off", "On"
2719 };
2720
2721 return snd_ctl_enum_info(uinfo, 1, 3, values);
2722}
2723
2724static int scarlett2_talkback_enum_ctl_get(
2725 struct snd_kcontrol *kctl, struct snd_ctl_elem_value *ucontrol)
2726{
2727 struct usb_mixer_elem_info *elem = kctl->private_data;
2728 struct usb_mixer_interface *mixer = elem->head.mixer;
2729 struct scarlett2_data *private = mixer->private_data;
2730
2731 mutex_lock(&private->data_mutex);
2732 if (private->monitor_other_updated)
2733 scarlett2_update_monitor_other(mixer);
2734 ucontrol->value.enumerated.item[0] = private->talkback_switch;
2735 mutex_unlock(&private->data_mutex);
2736
2737 return 0;
2738}
2739
2740static int scarlett2_talkback_enum_ctl_put(
2741 struct snd_kcontrol *kctl, struct snd_ctl_elem_value *ucontrol)
2742{
2743 struct usb_mixer_elem_info *elem = kctl->private_data;
2744 struct usb_mixer_interface *mixer = elem->head.mixer;
2745 struct scarlett2_data *private = mixer->private_data;
2746
2747 int oval, val, err = 0;
2748
2749 mutex_lock(&private->data_mutex);
2750
2751 oval = private->talkback_switch;
2752 val = min(ucontrol->value.enumerated.item[0], 2U);
2753
2754 if (oval == val)
2755 goto unlock;
2756
2757 private->talkback_switch = val;
2758
2759 /* enable/disable talkback */
2760 err = scarlett2_usb_set_config(
2761 mixer, SCARLETT2_CONFIG_MONITOR_OTHER_ENABLE,
2762 1, !!val);
2763 if (err < 0)
2764 goto unlock;
2765
2766 /* if talkback is enabled, select main or alt */
2767 err = scarlett2_usb_set_config(
2768 mixer, SCARLETT2_CONFIG_MONITOR_OTHER_SWITCH,
2769 1, val == 2);
2770 if (err == 0)
2771 err = 1;
2772
2773unlock:
2774 mutex_unlock(&private->data_mutex);
2775 return err;
2776}
2777
2778static const struct snd_kcontrol_new scarlett2_talkback_enum_ctl = {
2779 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2780 .name = "",
2781 .info = scarlett2_talkback_enum_ctl_info,
2782 .get = scarlett2_talkback_enum_ctl_get,
2783 .put = scarlett2_talkback_enum_ctl_put,
2784};
2785
2786static int scarlett2_talkback_map_ctl_get(
2787 struct snd_kcontrol *kctl, struct snd_ctl_elem_value *ucontrol)
2788{
2789 struct usb_mixer_elem_info *elem = kctl->private_data;
2790 struct usb_mixer_interface *mixer = elem->head.mixer;
2791 struct scarlett2_data *private = mixer->private_data;
2792 int index = elem->control;
2793
2794 ucontrol->value.integer.value[0] = private->talkback_map[index];
2795
2796 return 0;
2797}
2798
2799static int scarlett2_talkback_map_ctl_put(
2800 struct snd_kcontrol *kctl, struct snd_ctl_elem_value *ucontrol)
2801{
2802 struct usb_mixer_elem_info *elem = kctl->private_data;
2803 struct usb_mixer_interface *mixer = elem->head.mixer;
2804 struct scarlett2_data *private = mixer->private_data;
2805 const int (*port_count)[SCARLETT2_PORT_DIRNS] =
2806 private->info->port_count;
2807 int num_mixes = port_count[SCARLETT2_PORT_TYPE_MIX][SCARLETT2_PORT_IN];
2808
2809 int index = elem->control;
2810 int oval, val, err = 0, i;
2811 u16 bitmap = 0;
2812
2813 mutex_lock(&private->data_mutex);
2814
2815 oval = private->talkback_map[index];
2816 val = !!ucontrol->value.integer.value[0];
2817
2818 if (oval == val)
2819 goto unlock;
2820
2821 private->talkback_map[index] = val;
2822
2823 for (i = 0; i < num_mixes; i++)
2824 bitmap |= private->talkback_map[i] << i;
2825
2826 /* Send updated bitmap to the device */
2827 err = scarlett2_usb_set_config(mixer, SCARLETT2_CONFIG_TALKBACK_MAP,
2828 0, bitmap);
2829 if (err == 0)
2830 err = 1;
2831
2832unlock:
2833 mutex_unlock(&private->data_mutex);
2834 return err;
2835}
2836
2837static const struct snd_kcontrol_new scarlett2_talkback_map_ctl = {
2838 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2839 .name = "",
2840 .info = snd_ctl_boolean_mono_info,
2841 .get = scarlett2_talkback_map_ctl_get,
2842 .put = scarlett2_talkback_map_ctl_put,
2843};
2844
2845static int scarlett2_add_talkback_ctls(
2846 struct usb_mixer_interface *mixer)
2847{
2848 struct scarlett2_data *private = mixer->private_data;
2849 const struct scarlett2_device_info *info = private->info;
2850 const int (*port_count)[SCARLETT2_PORT_DIRNS] = info->port_count;
2851 int num_mixes = port_count[SCARLETT2_PORT_TYPE_MIX][SCARLETT2_PORT_IN];
2852 int err, i;
2853 char s[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
2854
2855 if (!info->has_talkback)
2856 return 0;
2857
2858 err = scarlett2_add_new_ctl(
2859 mixer, &scarlett2_talkback_enum_ctl,
2860 0, 1, "Talkback Playback Enum",
2861 &private->talkback_ctl);
2862 if (err < 0)
2863 return err;
2864
2865 for (i = 0; i < num_mixes; i++) {
2866 snprintf(s, sizeof(s),
2867 "Talkback Mix %c Playback Switch", i + 'A');
2868 err = scarlett2_add_new_ctl(mixer, &scarlett2_talkback_map_ctl,
2869 i, 1, s, NULL);
2870 if (err < 0)
2871 return err;
2872 }
2873
2874 return 0;
2875}
2876
2877/*** Dim/Mute Controls ***/
2878
2879static int scarlett2_dim_mute_ctl_get(struct snd_kcontrol *kctl,
2880 struct snd_ctl_elem_value *ucontrol)
2881{
2882 struct usb_mixer_elem_info *elem = kctl->private_data;
2883 struct usb_mixer_interface *mixer = elem->head.mixer;
2884 struct scarlett2_data *private = mixer->private_data;
2885
2886 mutex_lock(&private->data_mutex);
2887 if (private->vol_updated)
2888 scarlett2_update_volumes(mixer);
2889 mutex_unlock(&private->data_mutex);
2890
2891 ucontrol->value.integer.value[0] = private->dim_mute[elem->control];
2892 return 0;
2893}
2894
2895static int scarlett2_dim_mute_ctl_put(struct snd_kcontrol *kctl,
2896 struct snd_ctl_elem_value *ucontrol)
2897{
2898 struct usb_mixer_elem_info *elem = kctl->private_data;
2899 struct usb_mixer_interface *mixer = elem->head.mixer;
2900 struct scarlett2_data *private = mixer->private_data;
2901 const struct scarlett2_device_info *info = private->info;
2902 const int (*port_count)[SCARLETT2_PORT_DIRNS] = info->port_count;
2903 int num_line_out =
2904 port_count[SCARLETT2_PORT_TYPE_ANALOGUE][SCARLETT2_PORT_OUT];
2905
2906 int index = elem->control;
2907 int oval, val, err = 0, i;
2908
2909 mutex_lock(&private->data_mutex);
2910
2911 oval = private->dim_mute[index];
2912 val = !!ucontrol->value.integer.value[0];
2913
2914 if (oval == val)
2915 goto unlock;
2916
2917 private->dim_mute[index] = val;
2918
2919 /* Send switch change to the device */
2920 err = scarlett2_usb_set_config(mixer, SCARLETT2_CONFIG_DIM_MUTE,
2921 index, val);
2922 if (err == 0)
2923 err = 1;
2924
2925 if (index == SCARLETT2_BUTTON_MUTE)
2926 for (i = 0; i < num_line_out; i++) {
2927 int line_index = line_out_remap(private, i);
2928
2929 if (private->vol_sw_hw_switch[line_index]) {
2930 private->mute_switch[line_index] = val;
2931 snd_ctl_notify(mixer->chip->card,
2932 SNDRV_CTL_EVENT_MASK_VALUE,
2933 &private->mute_ctls[i]->id);
2934 }
2935 }
2936
2937unlock:
2938 mutex_unlock(&private->data_mutex);
2939 return err;
2940}
2941
2942static const struct snd_kcontrol_new scarlett2_dim_mute_ctl = {
2943 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2944 .name = "",
2945 .info = snd_ctl_boolean_mono_info,
2946 .get = scarlett2_dim_mute_ctl_get,
2947 .put = scarlett2_dim_mute_ctl_put
2948};
2949
2950/*** Create the analogue output controls ***/
2951
2952static int scarlett2_add_line_out_ctls(struct usb_mixer_interface *mixer)
2953{
2954 struct scarlett2_data *private = mixer->private_data;
2955 const struct scarlett2_device_info *info = private->info;
2956 const int (*port_count)[SCARLETT2_PORT_DIRNS] = info->port_count;
2957 int num_line_out =
2958 port_count[SCARLETT2_PORT_TYPE_ANALOGUE][SCARLETT2_PORT_OUT];
2959 int err, i;
2960 char s[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
2961
2962 /* Add R/O HW volume control */
2963 if (info->line_out_hw_vol) {
2964 snprintf(s, sizeof(s), "Master HW Playback Volume");
2965 err = scarlett2_add_new_ctl(mixer,
2966 &scarlett2_master_volume_ctl,
2967 0, 1, s, &private->master_vol_ctl);
2968 if (err < 0)
2969 return err;
2970 }
2971
2972 /* Add volume controls */
2973 for (i = 0; i < num_line_out; i++) {
2974 int index = line_out_remap(private, i);
2975
2976 /* Fader */
2977 if (info->line_out_descrs[i])
2978 snprintf(s, sizeof(s),
2979 "Line %02d (%s) Playback Volume",
2980 i + 1, info->line_out_descrs[i]);
2981 else
2982 snprintf(s, sizeof(s),
2983 "Line %02d Playback Volume",
2984 i + 1);
2985 err = scarlett2_add_new_ctl(mixer,
2986 &scarlett2_line_out_volume_ctl,
2987 i, 1, s, &private->vol_ctls[i]);
2988 if (err < 0)
2989 return err;
2990
2991 /* Mute Switch */
2992 snprintf(s, sizeof(s),
2993 "Line %02d Mute Playback Switch",
2994 i + 1);
2995 err = scarlett2_add_new_ctl(mixer,
2996 &scarlett2_mute_ctl,
2997 i, 1, s,
2998 &private->mute_ctls[i]);
2999 if (err < 0)
3000 return err;
3001
3002 /* Make the fader and mute controls read-only if the
3003 * SW/HW switch is set to HW
3004 */
3005 if (private->vol_sw_hw_switch[index])
3006 scarlett2_vol_ctl_set_writable(mixer, i, 0);
3007
3008 /* SW/HW Switch */
3009 if (info->line_out_hw_vol) {
3010 snprintf(s, sizeof(s),
3011 "Line Out %02d Volume Control Playback Enum",
3012 i + 1);
3013 err = scarlett2_add_new_ctl(mixer,
3014 &scarlett2_sw_hw_enum_ctl,
3015 i, 1, s,
3016 &private->sw_hw_ctls[i]);
3017 if (err < 0)
3018 return err;
3019
3020 /* Make the switch read-only if the line is
3021 * involved in speaker switching
3022 */
3023 if (private->speaker_switching_switch && i < 4)
3024 scarlett2_sw_hw_ctl_ro(private, i);
3025 }
3026 }
3027
3028 /* Add dim/mute controls */
3029 if (info->line_out_hw_vol)
3030 for (i = 0; i < SCARLETT2_DIM_MUTE_COUNT; i++) {
3031 err = scarlett2_add_new_ctl(
3032 mixer, &scarlett2_dim_mute_ctl,
3033 i, 1, scarlett2_dim_mute_names[i],
3034 &private->dim_mute_ctls[i]);
3035 if (err < 0)
3036 return err;
3037 }
3038
3039 return 0;
3040}
3041
3042/*** Create the analogue input controls ***/
3043
3044static int scarlett2_add_line_in_ctls(struct usb_mixer_interface *mixer)
3045{
3046 struct scarlett2_data *private = mixer->private_data;
3047 const struct scarlett2_device_info *info = private->info;
3048 int err, i;
3049 char s[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
3050 const char *fmt = "Line In %d %s Capture %s";
3051 const char *fmt2 = "Line In %d-%d %s Capture %s";
3052
3053 /* Add input level (line/inst) controls */
3054 for (i = 0; i < info->level_input_count; i++) {
3055 snprintf(s, sizeof(s), fmt, i + 1 + info->level_input_first,
3056 "Level", "Enum");
3057 err = scarlett2_add_new_ctl(mixer, &scarlett2_level_enum_ctl,
3058 i, 1, s, &private->level_ctls[i]);
3059 if (err < 0)
3060 return err;
3061 }
3062
3063 /* Add input pad controls */
3064 for (i = 0; i < info->pad_input_count; i++) {
3065 snprintf(s, sizeof(s), fmt, i + 1, "Pad", "Switch");
3066 err = scarlett2_add_new_ctl(mixer, &scarlett2_pad_ctl,
3067 i, 1, s, &private->pad_ctls[i]);
3068 if (err < 0)
3069 return err;
3070 }
3071
3072 /* Add input air controls */
3073 for (i = 0; i < info->air_input_count; i++) {
3074 snprintf(s, sizeof(s), fmt, i + 1, "Air", "Switch");
3075 err = scarlett2_add_new_ctl(mixer, &scarlett2_air_ctl,
3076 i, 1, s, &private->air_ctls[i]);
3077 if (err < 0)
3078 return err;
3079 }
3080
3081 /* Add input phantom controls */
3082 if (info->inputs_per_phantom == 1) {
3083 for (i = 0; i < info->phantom_count; i++) {
3084 snprintf(s, sizeof(s), fmt, i + 1,
3085 "Phantom Power", "Switch");
3086 err = scarlett2_add_new_ctl(
3087 mixer, &scarlett2_phantom_ctl,
3088 i, 1, s, &private->phantom_ctls[i]);
3089 if (err < 0)
3090 return err;
3091 }
3092 } else if (info->inputs_per_phantom > 1) {
3093 for (i = 0; i < info->phantom_count; i++) {
3094 int from = i * info->inputs_per_phantom + 1;
3095 int to = (i + 1) * info->inputs_per_phantom;
3096
3097 snprintf(s, sizeof(s), fmt2, from, to,
3098 "Phantom Power", "Switch");
3099 err = scarlett2_add_new_ctl(
3100 mixer, &scarlett2_phantom_ctl,
3101 i, 1, s, &private->phantom_ctls[i]);
3102 if (err < 0)
3103 return err;
3104 }
3105 }
3106 if (info->phantom_count) {
3107 err = scarlett2_add_new_ctl(
3108 mixer, &scarlett2_phantom_persistence_ctl, 0, 1,
3109 "Phantom Power Persistence Capture Switch", NULL);
3110 if (err < 0)
3111 return err;
3112 }
3113
3114 return 0;
3115}
3116
3117/*** Mixer Volume Controls ***/
3118
3119static int scarlett2_mixer_ctl_info(struct snd_kcontrol *kctl,
3120 struct snd_ctl_elem_info *uinfo)
3121{
3122 struct usb_mixer_elem_info *elem = kctl->private_data;
3123
3124 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
3125 uinfo->count = elem->channels;
3126 uinfo->value.integer.min = 0;
3127 uinfo->value.integer.max = SCARLETT2_MIXER_MAX_VALUE;
3128 uinfo->value.integer.step = 1;
3129 return 0;
3130}
3131
3132static int scarlett2_mixer_ctl_get(struct snd_kcontrol *kctl,
3133 struct snd_ctl_elem_value *ucontrol)
3134{
3135 struct usb_mixer_elem_info *elem = kctl->private_data;
3136 struct scarlett2_data *private = elem->head.mixer->private_data;
3137
3138 ucontrol->value.integer.value[0] = private->mix[elem->control];
3139 return 0;
3140}
3141
3142static int scarlett2_mixer_ctl_put(struct snd_kcontrol *kctl,
3143 struct snd_ctl_elem_value *ucontrol)
3144{
3145 struct usb_mixer_elem_info *elem = kctl->private_data;
3146 struct usb_mixer_interface *mixer = elem->head.mixer;
3147 struct scarlett2_data *private = mixer->private_data;
3148 const struct scarlett2_device_info *info = private->info;
3149 const int (*port_count)[SCARLETT2_PORT_DIRNS] = info->port_count;
3150 int oval, val, num_mixer_in, mix_num, err = 0;
3151 int index = elem->control;
3152
3153 mutex_lock(&private->data_mutex);
3154
3155 oval = private->mix[index];
3156 val = ucontrol->value.integer.value[0];
3157 num_mixer_in = port_count[SCARLETT2_PORT_TYPE_MIX][SCARLETT2_PORT_OUT];
3158 mix_num = index / num_mixer_in;
3159
3160 if (oval == val)
3161 goto unlock;
3162
3163 private->mix[index] = val;
3164 err = scarlett2_usb_set_mix(mixer, mix_num);
3165 if (err == 0)
3166 err = 1;
3167
3168unlock:
3169 mutex_unlock(&private->data_mutex);
3170 return err;
3171}
3172
3173static const DECLARE_TLV_DB_MINMAX(
3174 db_scale_scarlett2_mixer,
3175 SCARLETT2_MIXER_MIN_DB * 100,
3176 SCARLETT2_MIXER_MAX_DB * 100
3177);
3178
3179static const struct snd_kcontrol_new scarlett2_mixer_ctl = {
3180 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3181 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE |
3182 SNDRV_CTL_ELEM_ACCESS_TLV_READ,
3183 .name = "",
3184 .info = scarlett2_mixer_ctl_info,
3185 .get = scarlett2_mixer_ctl_get,
3186 .put = scarlett2_mixer_ctl_put,
3187 .private_value = SCARLETT2_MIXER_MAX_DB, /* max value */
3188 .tlv = { .p = db_scale_scarlett2_mixer }
3189};
3190
3191static int scarlett2_add_mixer_ctls(struct usb_mixer_interface *mixer)
3192{
3193 struct scarlett2_data *private = mixer->private_data;
3194 const struct scarlett2_device_info *info = private->info;
3195 const int (*port_count)[SCARLETT2_PORT_DIRNS] = info->port_count;
3196 int err, i, j;
3197 int index;
3198 char s[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
3199
3200 int num_inputs =
3201 port_count[SCARLETT2_PORT_TYPE_MIX][SCARLETT2_PORT_OUT];
3202 int num_outputs =
3203 port_count[SCARLETT2_PORT_TYPE_MIX][SCARLETT2_PORT_IN];
3204
3205 for (i = 0, index = 0; i < num_outputs; i++)
3206 for (j = 0; j < num_inputs; j++, index++) {
3207 snprintf(s, sizeof(s),
3208 "Mix %c Input %02d Playback Volume",
3209 'A' + i, j + 1);
3210 err = scarlett2_add_new_ctl(mixer, &scarlett2_mixer_ctl,
3211 index, 1, s, NULL);
3212 if (err < 0)
3213 return err;
3214 }
3215
3216 return 0;
3217}
3218
3219/*** Mux Source Selection Controls ***/
3220
3221static int scarlett2_mux_src_enum_ctl_info(struct snd_kcontrol *kctl,
3222 struct snd_ctl_elem_info *uinfo)
3223{
3224 struct usb_mixer_elem_info *elem = kctl->private_data;
3225 struct scarlett2_data *private = elem->head.mixer->private_data;
3226 const struct scarlett2_device_info *info = private->info;
3227 const int (*port_count)[SCARLETT2_PORT_DIRNS] = info->port_count;
3228 unsigned int item = uinfo->value.enumerated.item;
3229 int items = private->num_mux_srcs;
3230 int port_type;
3231
3232 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
3233 uinfo->count = elem->channels;
3234 uinfo->value.enumerated.items = items;
3235
3236 if (item >= items)
3237 item = uinfo->value.enumerated.item = items - 1;
3238
3239 for (port_type = 0;
3240 port_type < SCARLETT2_PORT_TYPE_COUNT;
3241 port_type++) {
3242 if (item < port_count[port_type][SCARLETT2_PORT_IN]) {
3243 const struct scarlett2_port *port =
3244 &scarlett2_ports[port_type];
3245
3246 sprintf(uinfo->value.enumerated.name,
3247 port->src_descr, item + port->src_num_offset);
3248 return 0;
3249 }
3250 item -= port_count[port_type][SCARLETT2_PORT_IN];
3251 }
3252
3253 return -EINVAL;
3254}
3255
3256static int scarlett2_mux_src_enum_ctl_get(struct snd_kcontrol *kctl,
3257 struct snd_ctl_elem_value *ucontrol)
3258{
3259 struct usb_mixer_elem_info *elem = kctl->private_data;
3260 struct usb_mixer_interface *mixer = elem->head.mixer;
3261 struct scarlett2_data *private = mixer->private_data;
3262 const struct scarlett2_device_info *info = private->info;
3263 const int (*port_count)[SCARLETT2_PORT_DIRNS] = info->port_count;
3264 int line_out_count =
3265 port_count[SCARLETT2_PORT_TYPE_ANALOGUE][SCARLETT2_PORT_OUT];
3266 int index = elem->control;
3267
3268 if (index < line_out_count)
3269 index = line_out_remap(private, index);
3270
3271 mutex_lock(&private->data_mutex);
3272 if (private->mux_updated)
3273 scarlett2_usb_get_mux(mixer);
3274 ucontrol->value.enumerated.item[0] = private->mux[index];
3275 mutex_unlock(&private->data_mutex);
3276
3277 return 0;
3278}
3279
3280static int scarlett2_mux_src_enum_ctl_put(struct snd_kcontrol *kctl,
3281 struct snd_ctl_elem_value *ucontrol)
3282{
3283 struct usb_mixer_elem_info *elem = kctl->private_data;
3284 struct usb_mixer_interface *mixer = elem->head.mixer;
3285 struct scarlett2_data *private = mixer->private_data;
3286 const struct scarlett2_device_info *info = private->info;
3287 const int (*port_count)[SCARLETT2_PORT_DIRNS] = info->port_count;
3288 int line_out_count =
3289 port_count[SCARLETT2_PORT_TYPE_ANALOGUE][SCARLETT2_PORT_OUT];
3290 int index = elem->control;
3291 int oval, val, err = 0;
3292
3293 if (index < line_out_count)
3294 index = line_out_remap(private, index);
3295
3296 mutex_lock(&private->data_mutex);
3297
3298 oval = private->mux[index];
3299 val = min(ucontrol->value.enumerated.item[0],
3300 private->num_mux_srcs - 1U);
3301
3302 if (oval == val)
3303 goto unlock;
3304
3305 private->mux[index] = val;
3306 err = scarlett2_usb_set_mux(mixer);
3307 if (err == 0)
3308 err = 1;
3309
3310unlock:
3311 mutex_unlock(&private->data_mutex);
3312 return err;
3313}
3314
3315static const struct snd_kcontrol_new scarlett2_mux_src_enum_ctl = {
3316 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3317 .name = "",
3318 .info = scarlett2_mux_src_enum_ctl_info,
3319 .get = scarlett2_mux_src_enum_ctl_get,
3320 .put = scarlett2_mux_src_enum_ctl_put,
3321};
3322
3323static int scarlett2_add_mux_enums(struct usb_mixer_interface *mixer)
3324{
3325 struct scarlett2_data *private = mixer->private_data;
3326 const struct scarlett2_device_info *info = private->info;
3327 const int (*port_count)[SCARLETT2_PORT_DIRNS] = info->port_count;
3328 int port_type, channel, i;
3329
3330 for (i = 0, port_type = 0;
3331 port_type < SCARLETT2_PORT_TYPE_COUNT;
3332 port_type++) {
3333 for (channel = 0;
3334 channel < port_count[port_type][SCARLETT2_PORT_OUT];
3335 channel++, i++) {
3336 int err;
3337 char s[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
3338 const char *const descr =
3339 scarlett2_ports[port_type].dst_descr;
3340
3341 snprintf(s, sizeof(s) - 5, descr, channel + 1);
3342 strcat(s, " Enum");
3343
3344 err = scarlett2_add_new_ctl(mixer,
3345 &scarlett2_mux_src_enum_ctl,
3346 i, 1, s,
3347 &private->mux_ctls[i]);
3348 if (err < 0)
3349 return err;
3350 }
3351 }
3352
3353 return 0;
3354}
3355
3356/*** Meter Controls ***/
3357
3358static int scarlett2_meter_ctl_info(struct snd_kcontrol *kctl,
3359 struct snd_ctl_elem_info *uinfo)
3360{
3361 struct usb_mixer_elem_info *elem = kctl->private_data;
3362
3363 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
3364 uinfo->count = elem->channels;
3365 uinfo->value.integer.min = 0;
3366 uinfo->value.integer.max = 4095;
3367 uinfo->value.integer.step = 1;
3368 return 0;
3369}
3370
3371static int scarlett2_meter_ctl_get(struct snd_kcontrol *kctl,
3372 struct snd_ctl_elem_value *ucontrol)
3373{
3374 struct usb_mixer_elem_info *elem = kctl->private_data;
3375 u16 meter_levels[SCARLETT2_MAX_METERS];
3376 int i, err;
3377
3378 err = scarlett2_usb_get_meter_levels(elem->head.mixer, elem->channels,
3379 meter_levels);
3380 if (err < 0)
3381 return err;
3382
3383 for (i = 0; i < elem->channels; i++)
3384 ucontrol->value.integer.value[i] = meter_levels[i];
3385
3386 return 0;
3387}
3388
3389static const struct snd_kcontrol_new scarlett2_meter_ctl = {
3390 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
3391 .access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE,
3392 .name = "",
3393 .info = scarlett2_meter_ctl_info,
3394 .get = scarlett2_meter_ctl_get
3395};
3396
3397static int scarlett2_add_meter_ctl(struct usb_mixer_interface *mixer)
3398{
3399 struct scarlett2_data *private = mixer->private_data;
3400
3401 /* devices without a mixer also don't support reporting levels */
3402 if (!private->info->has_mixer)
3403 return 0;
3404
3405 return scarlett2_add_new_ctl(mixer, &scarlett2_meter_ctl,
3406 0, private->num_mux_dsts,
3407 "Level Meter", NULL);
3408}
3409
3410/*** MSD Controls ***/
3411
3412static int scarlett2_msd_ctl_get(struct snd_kcontrol *kctl,
3413 struct snd_ctl_elem_value *ucontrol)
3414{
3415 struct usb_mixer_elem_info *elem = kctl->private_data;
3416 struct scarlett2_data *private = elem->head.mixer->private_data;
3417
3418 ucontrol->value.integer.value[0] = private->msd_switch;
3419 return 0;
3420}
3421
3422static int scarlett2_msd_ctl_put(struct snd_kcontrol *kctl,
3423 struct snd_ctl_elem_value *ucontrol)
3424{
3425 struct usb_mixer_elem_info *elem = kctl->private_data;
3426 struct usb_mixer_interface *mixer = elem->head.mixer;
3427 struct scarlett2_data *private = mixer->private_data;
3428
3429 int oval, val, err = 0;
3430
3431 mutex_lock(&private->data_mutex);
3432
3433 oval = private->msd_switch;
3434 val = !!ucontrol->value.integer.value[0];
3435
3436 if (oval == val)
3437 goto unlock;
3438
3439 private->msd_switch = val;
3440
3441 /* Send switch change to the device */
3442 err = scarlett2_usb_set_config(mixer, SCARLETT2_CONFIG_MSD_SWITCH,
3443 0, val);
3444 if (err == 0)
3445 err = 1;
3446
3447unlock:
3448 mutex_unlock(&private->data_mutex);
3449 return err;
3450}
3451
3452static const struct snd_kcontrol_new scarlett2_msd_ctl = {
3453 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3454 .name = "",
3455 .info = snd_ctl_boolean_mono_info,
3456 .get = scarlett2_msd_ctl_get,
3457 .put = scarlett2_msd_ctl_put,
3458};
3459
3460static int scarlett2_add_msd_ctl(struct usb_mixer_interface *mixer)
3461{
3462 struct scarlett2_data *private = mixer->private_data;
3463 const struct scarlett2_device_info *info = private->info;
3464
3465 if (!info->has_msd_mode)
3466 return 0;
3467
3468 /* If MSD mode is off, hide the switch by default */
3469 if (!private->msd_switch && !(mixer->chip->setup & SCARLETT2_MSD_ENABLE))
3470 return 0;
3471
3472 /* Add MSD control */
3473 return scarlett2_add_new_ctl(mixer, &scarlett2_msd_ctl,
3474 0, 1, "MSD Mode Switch", NULL);
3475}
3476
3477/*** Cleanup/Suspend Callbacks ***/
3478
3479static void scarlett2_private_free(struct usb_mixer_interface *mixer)
3480{
3481 struct scarlett2_data *private = mixer->private_data;
3482
3483 cancel_delayed_work_sync(&private->work);
3484 kfree(private);
3485 mixer->private_data = NULL;
3486}
3487
3488static void scarlett2_private_suspend(struct usb_mixer_interface *mixer)
3489{
3490 struct scarlett2_data *private = mixer->private_data;
3491
3492 if (cancel_delayed_work_sync(&private->work))
3493 scarlett2_config_save(private->mixer);
3494}
3495
3496/*** Initialisation ***/
3497
3498static void scarlett2_count_mux_io(struct scarlett2_data *private)
3499{
3500 const struct scarlett2_device_info *info = private->info;
3501 const int (*port_count)[SCARLETT2_PORT_DIRNS] = info->port_count;
3502 int port_type, srcs = 0, dsts = 0;
3503
3504 for (port_type = 0;
3505 port_type < SCARLETT2_PORT_TYPE_COUNT;
3506 port_type++) {
3507 srcs += port_count[port_type][SCARLETT2_PORT_IN];
3508 dsts += port_count[port_type][SCARLETT2_PORT_OUT];
3509 }
3510
3511 private->num_mux_srcs = srcs;
3512 private->num_mux_dsts = dsts;
3513}
3514
3515/* Look through the interface descriptors for the Focusrite Control
3516 * interface (bInterfaceClass = 255 Vendor Specific Class) and set
3517 * bInterfaceNumber, bEndpointAddress, wMaxPacketSize, and bInterval
3518 * in private
3519 */
3520static int scarlett2_find_fc_interface(struct usb_device *dev,
3521 struct scarlett2_data *private)
3522{
3523 struct usb_host_config *config = dev->actconfig;
3524 int i;
3525
3526 for (i = 0; i < config->desc.bNumInterfaces; i++) {
3527 struct usb_interface *intf = config->interface[i];
3528 struct usb_interface_descriptor *desc =
3529 &intf->altsetting[0].desc;
3530 struct usb_endpoint_descriptor *epd;
3531
3532 if (desc->bInterfaceClass != 255)
3533 continue;
3534
3535 epd = get_endpoint(intf->altsetting, 0);
3536 private->bInterfaceNumber = desc->bInterfaceNumber;
3537 private->bEndpointAddress = epd->bEndpointAddress &
3538 USB_ENDPOINT_NUMBER_MASK;
3539 private->wMaxPacketSize = le16_to_cpu(epd->wMaxPacketSize);
3540 private->bInterval = epd->bInterval;
3541 return 0;
3542 }
3543
3544 return -EINVAL;
3545}
3546
3547/* Initialise private data */
3548static int scarlett2_init_private(struct usb_mixer_interface *mixer,
3549 const struct scarlett2_device_info *info)
3550{
3551 struct scarlett2_data *private =
3552 kzalloc(sizeof(struct scarlett2_data), GFP_KERNEL);
3553
3554 if (!private)
3555 return -ENOMEM;
3556
3557 mutex_init(&private->usb_mutex);
3558 mutex_init(&private->data_mutex);
3559 INIT_DELAYED_WORK(&private->work, scarlett2_config_save_work);
3560
3561 mixer->private_data = private;
3562 mixer->private_free = scarlett2_private_free;
3563 mixer->private_suspend = scarlett2_private_suspend;
3564
3565 private->info = info;
3566 scarlett2_count_mux_io(private);
3567 private->scarlett2_seq = 0;
3568 private->mixer = mixer;
3569
3570 return scarlett2_find_fc_interface(mixer->chip->dev, private);
3571}
3572
3573/* Cargo cult proprietary initialisation sequence */
3574static int scarlett2_usb_init(struct usb_mixer_interface *mixer)
3575{
3576 struct usb_device *dev = mixer->chip->dev;
3577 struct scarlett2_data *private = mixer->private_data;
3578 u8 buf[24];
3579 int err;
3580
3581 if (usb_pipe_type_check(dev, usb_sndctrlpipe(dev, 0)))
3582 return -EINVAL;
3583
3584 /* step 0 */
3585 err = scarlett2_usb_rx(dev, private->bInterfaceNumber,
3586 SCARLETT2_USB_CMD_INIT, buf, sizeof(buf));
3587 if (err < 0)
3588 return err;
3589
3590 /* step 1 */
3591 private->scarlett2_seq = 1;
3592 err = scarlett2_usb(mixer, SCARLETT2_USB_INIT_1, NULL, 0, NULL, 0);
3593 if (err < 0)
3594 return err;
3595
3596 /* step 2 */
3597 private->scarlett2_seq = 1;
3598 return scarlett2_usb(mixer, SCARLETT2_USB_INIT_2, NULL, 0, NULL, 84);
3599}
3600
3601/* Read configuration from the interface on start */
3602static int scarlett2_read_configs(struct usb_mixer_interface *mixer)
3603{
3604 struct scarlett2_data *private = mixer->private_data;
3605 const struct scarlett2_device_info *info = private->info;
3606 const int (*port_count)[SCARLETT2_PORT_DIRNS] = info->port_count;
3607 int num_line_out =
3608 port_count[SCARLETT2_PORT_TYPE_ANALOGUE][SCARLETT2_PORT_OUT];
3609 int num_mixer_out =
3610 port_count[SCARLETT2_PORT_TYPE_MIX][SCARLETT2_PORT_IN];
3611 struct scarlett2_usb_volume_status volume_status;
3612 int err, i;
3613
3614 if (info->has_msd_mode) {
3615 err = scarlett2_usb_get_config(
3616 mixer, SCARLETT2_CONFIG_MSD_SWITCH,
3617 1, &private->msd_switch);
3618 if (err < 0)
3619 return err;
3620
3621 /* no other controls are created if MSD mode is on */
3622 if (private->msd_switch)
3623 return 0;
3624 }
3625
3626 err = scarlett2_update_input_other(mixer);
3627 if (err < 0)
3628 return err;
3629
3630 err = scarlett2_update_monitor_other(mixer);
3631 if (err < 0)
3632 return err;
3633
3634 /* the rest of the configuration is for devices with a mixer */
3635 if (!info->has_mixer)
3636 return 0;
3637
3638 err = scarlett2_update_sync(mixer);
3639 if (err < 0)
3640 return err;
3641
3642 err = scarlett2_usb_get_volume_status(mixer, &volume_status);
3643 if (err < 0)
3644 return err;
3645
3646 if (info->line_out_hw_vol)
3647 for (i = 0; i < SCARLETT2_DIM_MUTE_COUNT; i++)
3648 private->dim_mute[i] = !!volume_status.dim_mute[i];
3649
3650 private->master_vol = clamp(
3651 volume_status.master_vol + SCARLETT2_VOLUME_BIAS,
3652 0, SCARLETT2_VOLUME_BIAS);
3653
3654 for (i = 0; i < num_line_out; i++) {
3655 int volume, mute;
3656
3657 private->vol_sw_hw_switch[i] =
3658 info->line_out_hw_vol
3659 && volume_status.sw_hw_switch[i];
3660
3661 volume = private->vol_sw_hw_switch[i]
3662 ? volume_status.master_vol
3663 : volume_status.sw_vol[i];
3664 volume = clamp(volume + SCARLETT2_VOLUME_BIAS,
3665 0, SCARLETT2_VOLUME_BIAS);
3666 private->vol[i] = volume;
3667
3668 mute = private->vol_sw_hw_switch[i]
3669 ? private->dim_mute[SCARLETT2_BUTTON_MUTE]
3670 : volume_status.mute_switch[i];
3671 private->mute_switch[i] = mute;
3672 }
3673
3674 for (i = 0; i < num_mixer_out; i++) {
3675 err = scarlett2_usb_get_mix(mixer, i);
3676 if (err < 0)
3677 return err;
3678 }
3679
3680 return scarlett2_usb_get_mux(mixer);
3681}
3682
3683/* Notify on sync change */
3684static void scarlett2_notify_sync(
3685 struct usb_mixer_interface *mixer)
3686{
3687 struct scarlett2_data *private = mixer->private_data;
3688
3689 private->sync_updated = 1;
3690
3691 snd_ctl_notify(mixer->chip->card, SNDRV_CTL_EVENT_MASK_VALUE,
3692 &private->sync_ctl->id);
3693}
3694
3695/* Notify on monitor change */
3696static void scarlett2_notify_monitor(
3697 struct usb_mixer_interface *mixer)
3698{
3699 struct snd_card *card = mixer->chip->card;
3700 struct scarlett2_data *private = mixer->private_data;
3701 const struct scarlett2_device_info *info = private->info;
3702 const int (*port_count)[SCARLETT2_PORT_DIRNS] = info->port_count;
3703 int num_line_out =
3704 port_count[SCARLETT2_PORT_TYPE_ANALOGUE][SCARLETT2_PORT_OUT];
3705 int i;
3706
3707 /* if line_out_hw_vol is 0, there are no controls to update */
3708 if (!info->line_out_hw_vol)
3709 return;
3710
3711 private->vol_updated = 1;
3712
3713 snd_ctl_notify(mixer->chip->card, SNDRV_CTL_EVENT_MASK_VALUE,
3714 &private->master_vol_ctl->id);
3715
3716 for (i = 0; i < num_line_out; i++)
3717 if (private->vol_sw_hw_switch[line_out_remap(private, i)])
3718 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE,
3719 &private->vol_ctls[i]->id);
3720}
3721
3722/* Notify on dim/mute change */
3723static void scarlett2_notify_dim_mute(
3724 struct usb_mixer_interface *mixer)
3725{
3726 struct snd_card *card = mixer->chip->card;
3727 struct scarlett2_data *private = mixer->private_data;
3728 const struct scarlett2_device_info *info = private->info;
3729 const int (*port_count)[SCARLETT2_PORT_DIRNS] = info->port_count;
3730 int num_line_out =
3731 port_count[SCARLETT2_PORT_TYPE_ANALOGUE][SCARLETT2_PORT_OUT];
3732 int i;
3733
3734 private->vol_updated = 1;
3735
3736 if (!info->line_out_hw_vol)
3737 return;
3738
3739 for (i = 0; i < SCARLETT2_DIM_MUTE_COUNT; i++)
3740 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE,
3741 &private->dim_mute_ctls[i]->id);
3742
3743 for (i = 0; i < num_line_out; i++)
3744 if (private->vol_sw_hw_switch[line_out_remap(private, i)])
3745 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE,
3746 &private->mute_ctls[i]->id);
3747}
3748
3749/* Notify on "input other" change (level/pad/air) */
3750static void scarlett2_notify_input_other(
3751 struct usb_mixer_interface *mixer)
3752{
3753 struct snd_card *card = mixer->chip->card;
3754 struct scarlett2_data *private = mixer->private_data;
3755 const struct scarlett2_device_info *info = private->info;
3756 int i;
3757
3758 private->input_other_updated = 1;
3759
3760 for (i = 0; i < info->level_input_count; i++)
3761 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE,
3762 &private->level_ctls[i]->id);
3763 for (i = 0; i < info->pad_input_count; i++)
3764 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE,
3765 &private->pad_ctls[i]->id);
3766 for (i = 0; i < info->air_input_count; i++)
3767 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE,
3768 &private->air_ctls[i]->id);
3769 for (i = 0; i < info->phantom_count; i++)
3770 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE,
3771 &private->phantom_ctls[i]->id);
3772}
3773
3774/* Notify on "monitor other" change (direct monitor, speaker
3775 * switching, talkback)
3776 */
3777static void scarlett2_notify_monitor_other(
3778 struct usb_mixer_interface *mixer)
3779{
3780 struct snd_card *card = mixer->chip->card;
3781 struct scarlett2_data *private = mixer->private_data;
3782 const struct scarlett2_device_info *info = private->info;
3783
3784 private->monitor_other_updated = 1;
3785
3786 if (info->direct_monitor) {
3787 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE,
3788 &private->direct_monitor_ctl->id);
3789 return;
3790 }
3791
3792 if (info->has_speaker_switching)
3793 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE,
3794 &private->speaker_switching_ctl->id);
3795
3796 if (info->has_talkback)
3797 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE,
3798 &private->talkback_ctl->id);
3799
3800 /* if speaker switching was recently enabled or disabled,
3801 * invalidate the dim/mute and mux enum controls
3802 */
3803 if (private->speaker_switching_switched) {
3804 int i;
3805
3806 scarlett2_notify_dim_mute(mixer);
3807
3808 private->speaker_switching_switched = 0;
3809 private->mux_updated = 1;
3810
3811 for (i = 0; i < private->num_mux_dsts; i++)
3812 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE,
3813 &private->mux_ctls[i]->id);
3814 }
3815}
3816
3817/* Interrupt callback */
3818static void scarlett2_notify(struct urb *urb)
3819{
3820 struct usb_mixer_interface *mixer = urb->context;
3821 int len = urb->actual_length;
3822 int ustatus = urb->status;
3823 u32 data;
3824
3825 if (ustatus != 0 || len != 8)
3826 goto requeue;
3827
3828 data = le32_to_cpu(*(__le32 *)urb->transfer_buffer);
3829 if (data & SCARLETT2_USB_NOTIFY_SYNC)
3830 scarlett2_notify_sync(mixer);
3831 if (data & SCARLETT2_USB_NOTIFY_MONITOR)
3832 scarlett2_notify_monitor(mixer);
3833 if (data & SCARLETT2_USB_NOTIFY_DIM_MUTE)
3834 scarlett2_notify_dim_mute(mixer);
3835 if (data & SCARLETT2_USB_NOTIFY_INPUT_OTHER)
3836 scarlett2_notify_input_other(mixer);
3837 if (data & SCARLETT2_USB_NOTIFY_MONITOR_OTHER)
3838 scarlett2_notify_monitor_other(mixer);
3839
3840requeue:
3841 if (ustatus != -ENOENT &&
3842 ustatus != -ECONNRESET &&
3843 ustatus != -ESHUTDOWN) {
3844 urb->dev = mixer->chip->dev;
3845 usb_submit_urb(urb, GFP_ATOMIC);
3846 }
3847}
3848
3849static int scarlett2_init_notify(struct usb_mixer_interface *mixer)
3850{
3851 struct usb_device *dev = mixer->chip->dev;
3852 struct scarlett2_data *private = mixer->private_data;
3853 unsigned int pipe = usb_rcvintpipe(dev, private->bEndpointAddress);
3854 void *transfer_buffer;
3855
3856 if (mixer->urb) {
3857 usb_audio_err(mixer->chip,
3858 "%s: mixer urb already in use!\n", __func__);
3859 return 0;
3860 }
3861
3862 if (usb_pipe_type_check(dev, pipe))
3863 return -EINVAL;
3864
3865 mixer->urb = usb_alloc_urb(0, GFP_KERNEL);
3866 if (!mixer->urb)
3867 return -ENOMEM;
3868
3869 transfer_buffer = kmalloc(private->wMaxPacketSize, GFP_KERNEL);
3870 if (!transfer_buffer)
3871 return -ENOMEM;
3872
3873 usb_fill_int_urb(mixer->urb, dev, pipe,
3874 transfer_buffer, private->wMaxPacketSize,
3875 scarlett2_notify, mixer, private->bInterval);
3876
3877 return usb_submit_urb(mixer->urb, GFP_KERNEL);
3878}
3879
3880static int snd_scarlett_gen2_controls_create(struct usb_mixer_interface *mixer)
3881{
3882 const struct scarlett2_device_info **info = scarlett2_devices;
3883 int err;
3884
3885 /* Find device in scarlett2_devices */
3886 while (*info && (*info)->usb_id != mixer->chip->usb_id)
3887 info++;
3888 if (!*info)
3889 return -EINVAL;
3890
3891 /* Initialise private data */
3892 err = scarlett2_init_private(mixer, *info);
3893 if (err < 0)
3894 return err;
3895
3896 /* Send proprietary USB initialisation sequence */
3897 err = scarlett2_usb_init(mixer);
3898 if (err < 0)
3899 return err;
3900
3901 /* Read volume levels and controls from the interface */
3902 err = scarlett2_read_configs(mixer);
3903 if (err < 0)
3904 return err;
3905
3906 /* Create the MSD control */
3907 err = scarlett2_add_msd_ctl(mixer);
3908 if (err < 0)
3909 return err;
3910
3911 /* If MSD mode is enabled, don't create any other controls */
3912 if (((struct scarlett2_data *)mixer->private_data)->msd_switch)
3913 return 0;
3914
3915 /* Create the analogue output controls */
3916 err = scarlett2_add_line_out_ctls(mixer);
3917 if (err < 0)
3918 return err;
3919
3920 /* Create the analogue input controls */
3921 err = scarlett2_add_line_in_ctls(mixer);
3922 if (err < 0)
3923 return err;
3924
3925 /* Create the input, output, and mixer mux input selections */
3926 err = scarlett2_add_mux_enums(mixer);
3927 if (err < 0)
3928 return err;
3929
3930 /* Create the matrix mixer controls */
3931 err = scarlett2_add_mixer_ctls(mixer);
3932 if (err < 0)
3933 return err;
3934
3935 /* Create the level meter controls */
3936 err = scarlett2_add_meter_ctl(mixer);
3937 if (err < 0)
3938 return err;
3939
3940 /* Create the sync control */
3941 err = scarlett2_add_sync_ctl(mixer);
3942 if (err < 0)
3943 return err;
3944
3945 /* Create the direct monitor control */
3946 err = scarlett2_add_direct_monitor_ctl(mixer);
3947 if (err < 0)
3948 return err;
3949
3950 /* Create the speaker switching control */
3951 err = scarlett2_add_speaker_switch_ctl(mixer);
3952 if (err < 0)
3953 return err;
3954
3955 /* Create the talkback controls */
3956 err = scarlett2_add_talkback_ctls(mixer);
3957 if (err < 0)
3958 return err;
3959
3960 /* Set up the interrupt polling */
3961 err = scarlett2_init_notify(mixer);
3962 if (err < 0)
3963 return err;
3964
3965 return 0;
3966}
3967
3968int snd_scarlett_gen2_init(struct usb_mixer_interface *mixer)
3969{
3970 struct snd_usb_audio *chip = mixer->chip;
3971 int err;
3972
3973 /* only use UAC_VERSION_2 */
3974 if (!mixer->protocol)
3975 return 0;
3976
3977 if (!(chip->setup & SCARLETT2_ENABLE)) {
3978 usb_audio_info(chip,
3979 "Focusrite Scarlett Gen 2/3 Mixer Driver disabled; "
3980 "use options snd_usb_audio vid=0x%04x pid=0x%04x "
3981 "device_setup=1 to enable and report any issues "
3982 "to g@b4.vu",
3983 USB_ID_VENDOR(chip->usb_id),
3984 USB_ID_PRODUCT(chip->usb_id));
3985 return 0;
3986 }
3987
3988 usb_audio_info(chip,
3989 "Focusrite Scarlett Gen 2/3 Mixer Driver enabled pid=0x%04x",
3990 USB_ID_PRODUCT(chip->usb_id));
3991
3992 err = snd_scarlett_gen2_controls_create(mixer);
3993 if (err < 0)
3994 usb_audio_err(mixer->chip,
3995 "Error initialising Scarlett Mixer Driver: %d",
3996 err);
3997
3998 return err;
3999}
1// SPDX-License-Identifier: GPL-2.0
2/*
3 * Focusrite Scarlett Gen 2/3 and Clarett+ Driver for ALSA
4 *
5 * Supported models:
6 * - 6i6/18i8/18i20 Gen 2
7 * - Solo/2i2/4i4/8i6/18i8/18i20 Gen 3
8 * - Clarett+ 8Pre
9 *
10 * Copyright (c) 2018-2022 by Geoffrey D. Bennett <g at b4.vu>
11 * Copyright (c) 2020-2021 by Vladimir Sadovnikov <sadko4u@gmail.com>
12 * Copyright (c) 2022 by Christian Colglazier <christian@cacolglazier.com>
13 *
14 * Based on the Scarlett (Gen 1) Driver for ALSA:
15 *
16 * Copyright (c) 2013 by Tobias Hoffmann
17 * Copyright (c) 2013 by Robin Gareus <robin at gareus.org>
18 * Copyright (c) 2002 by Takashi Iwai <tiwai at suse.de>
19 * Copyright (c) 2014 by Chris J Arges <chris.j.arges at canonical.com>
20 *
21 * Many codes borrowed from audio.c by
22 * Alan Cox (alan at lxorguk.ukuu.org.uk)
23 * Thomas Sailer (sailer at ife.ee.ethz.ch)
24 *
25 * Code cleanup:
26 * David Henningsson <david.henningsson at canonical.com>
27 */
28
29/* The protocol was reverse engineered by looking at the communication
30 * between Focusrite Control 2.3.4 and the Focusrite(R) Scarlett 18i20
31 * (firmware 1083) using usbmon in July-August 2018.
32 *
33 * Scarlett 18i8 support added in April 2019.
34 *
35 * Scarlett 6i6 support added in June 2019 (thanks to Martin Wittmann
36 * for providing usbmon output and testing).
37 *
38 * Scarlett 4i4/8i6 Gen 3 support added in May 2020 (thanks to Laurent
39 * Debricon for donating a 4i4 and to Fredrik Unger for providing 8i6
40 * usbmon output and testing).
41 *
42 * Scarlett 18i8/18i20 Gen 3 support added in June 2020 (thanks to
43 * Darren Jaeckel, Alex Sedlack, and Clovis Lunel for providing usbmon
44 * output, protocol traces and testing).
45 *
46 * Support for loading mixer volume and mux configuration from the
47 * interface during driver initialisation added in May 2021 (thanks to
48 * Vladimir Sadovnikov for figuring out how).
49 *
50 * Support for Solo/2i2 Gen 3 added in May 2021 (thanks to Alexander
51 * Vorona for 2i2 protocol traces).
52 *
53 * Support for phantom power, direct monitoring, speaker switching,
54 * and talkback added in May-June 2021.
55 *
56 * Support for Clarett+ 8Pre added in Aug 2022 by Christian
57 * Colglazier.
58 *
59 * This ALSA mixer gives access to (model-dependent):
60 * - input, output, mixer-matrix muxes
61 * - mixer-matrix gain stages
62 * - gain/volume/mute controls
63 * - level meters
64 * - line/inst level, pad, and air controls
65 * - phantom power, direct monitor, speaker switching, and talkback
66 * controls
67 * - disable/enable MSD mode
68 * - disable/enable standalone mode
69 *
70 * <ditaa>
71 * /--------------\ 18chn 20chn /--------------\
72 * | Hardware in +--+------\ /-------------+--+ ALSA PCM out |
73 * \--------------/ | | | | \--------------/
74 * | | | /-----\ |
75 * | | | | | |
76 * | v v v | |
77 * | +---------------+ | |
78 * | \ Matrix Mux / | |
79 * | +-----+-----+ | |
80 * | | | |
81 * | |18chn | |
82 * | | | |
83 * | | 10chn| |
84 * | v | |
85 * | +------------+ | |
86 * | | Mixer | | |
87 * | | Matrix | | |
88 * | | | | |
89 * | | 18x10 Gain | | |
90 * | | stages | | |
91 * | +-----+------+ | |
92 * | | | |
93 * |18chn |10chn | |20chn
94 * | | | |
95 * | +----------/ |
96 * | | |
97 * v v v
98 * ===========================
99 * +---------------+ +--—------------+
100 * \ Output Mux / \ Capture Mux /
101 * +---+---+---+ +-----+-----+
102 * | | |
103 * 10chn| | |18chn
104 * | | |
105 * /--------------\ | | | /--------------\
106 * | S/PDIF, ADAT |<--/ |10chn \-->| ALSA PCM in |
107 * | Hardware out | | \--------------/
108 * \--------------/ |
109 * v
110 * +-------------+ Software gain per channel.
111 * | Master Gain |<-- 18i20 only: Switch per channel
112 * +------+------+ to select HW or SW gain control.
113 * |
114 * |10chn
115 * /--------------\ |
116 * | Analogue |<------/
117 * | Hardware out |
118 * \--------------/
119 * </ditaa>
120 *
121 * Gen 3 devices have a Mass Storage Device (MSD) mode where a small
122 * disk with registration and driver download information is presented
123 * to the host. To access the full functionality of the device without
124 * proprietary software, MSD mode can be disabled by:
125 * - holding down the 48V button for five seconds while powering on
126 * the device, or
127 * - using this driver and alsamixer to change the "MSD Mode" setting
128 * to Off and power-cycling the device
129 */
130
131#include <linux/slab.h>
132#include <linux/usb.h>
133#include <linux/moduleparam.h>
134
135#include <sound/control.h>
136#include <sound/tlv.h>
137
138#include "usbaudio.h"
139#include "mixer.h"
140#include "helper.h"
141
142#include "mixer_scarlett_gen2.h"
143
144/* device_setup value to enable */
145#define SCARLETT2_ENABLE 0x01
146
147/* device_setup value to allow turning MSD mode back on */
148#define SCARLETT2_MSD_ENABLE 0x02
149
150/* some gui mixers can't handle negative ctl values */
151#define SCARLETT2_VOLUME_BIAS 127
152
153/* mixer range from -80dB to +6dB in 0.5dB steps */
154#define SCARLETT2_MIXER_MIN_DB -80
155#define SCARLETT2_MIXER_BIAS (-SCARLETT2_MIXER_MIN_DB * 2)
156#define SCARLETT2_MIXER_MAX_DB 6
157#define SCARLETT2_MIXER_MAX_VALUE \
158 ((SCARLETT2_MIXER_MAX_DB - SCARLETT2_MIXER_MIN_DB) * 2)
159#define SCARLETT2_MIXER_VALUE_COUNT (SCARLETT2_MIXER_MAX_VALUE + 1)
160
161/* map from (dB + 80) * 2 to mixer value
162 * for dB in 0 .. 172: int(8192 * pow(10, ((dB - 160) / 2 / 20)))
163 */
164static const u16 scarlett2_mixer_values[SCARLETT2_MIXER_VALUE_COUNT] = {
165 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2,
166 2, 2, 3, 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 6, 6, 6, 7, 7, 8, 8,
167 9, 9, 10, 10, 11, 12, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21,
168 23, 24, 25, 27, 29, 30, 32, 34, 36, 38, 41, 43, 46, 48, 51,
169 54, 57, 61, 65, 68, 73, 77, 81, 86, 91, 97, 103, 109, 115,
170 122, 129, 137, 145, 154, 163, 173, 183, 194, 205, 217, 230,
171 244, 259, 274, 290, 307, 326, 345, 365, 387, 410, 434, 460,
172 487, 516, 547, 579, 614, 650, 689, 730, 773, 819, 867, 919,
173 973, 1031, 1092, 1157, 1225, 1298, 1375, 1456, 1543, 1634,
174 1731, 1833, 1942, 2057, 2179, 2308, 2445, 2590, 2744, 2906,
175 3078, 3261, 3454, 3659, 3876, 4105, 4349, 4606, 4879, 5168,
176 5475, 5799, 6143, 6507, 6892, 7301, 7733, 8192, 8677, 9191,
177 9736, 10313, 10924, 11571, 12257, 12983, 13752, 14567, 15430,
178 16345
179};
180
181/* Maximum number of analogue outputs */
182#define SCARLETT2_ANALOGUE_MAX 10
183
184/* Maximum number of level and pad switches */
185#define SCARLETT2_LEVEL_SWITCH_MAX 2
186#define SCARLETT2_PAD_SWITCH_MAX 8
187#define SCARLETT2_AIR_SWITCH_MAX 8
188#define SCARLETT2_PHANTOM_SWITCH_MAX 2
189
190/* Maximum number of inputs to the mixer */
191#define SCARLETT2_INPUT_MIX_MAX 25
192
193/* Maximum number of outputs from the mixer */
194#define SCARLETT2_OUTPUT_MIX_MAX 12
195
196/* Maximum size of the data in the USB mux assignment message:
197 * 20 inputs, 20 outputs, 25 matrix inputs, 12 spare
198 */
199#define SCARLETT2_MUX_MAX 77
200
201/* Maximum number of meters (sum of output port counts) */
202#define SCARLETT2_MAX_METERS 65
203
204/* There are three different sets of configuration parameters across
205 * the devices
206 */
207enum {
208 SCARLETT2_CONFIG_SET_NO_MIXER = 0,
209 SCARLETT2_CONFIG_SET_GEN_2 = 1,
210 SCARLETT2_CONFIG_SET_GEN_3 = 2,
211 SCARLETT2_CONFIG_SET_CLARETT = 3,
212 SCARLETT2_CONFIG_SET_COUNT = 4
213};
214
215/* Hardware port types:
216 * - None (no input to mux)
217 * - Analogue I/O
218 * - S/PDIF I/O
219 * - ADAT I/O
220 * - Mixer I/O
221 * - PCM I/O
222 */
223enum {
224 SCARLETT2_PORT_TYPE_NONE = 0,
225 SCARLETT2_PORT_TYPE_ANALOGUE = 1,
226 SCARLETT2_PORT_TYPE_SPDIF = 2,
227 SCARLETT2_PORT_TYPE_ADAT = 3,
228 SCARLETT2_PORT_TYPE_MIX = 4,
229 SCARLETT2_PORT_TYPE_PCM = 5,
230 SCARLETT2_PORT_TYPE_COUNT = 6,
231};
232
233/* I/O count of each port type kept in struct scarlett2_ports */
234enum {
235 SCARLETT2_PORT_IN = 0,
236 SCARLETT2_PORT_OUT = 1,
237 SCARLETT2_PORT_DIRNS = 2,
238};
239
240/* Dim/Mute buttons on the 18i20 */
241enum {
242 SCARLETT2_BUTTON_MUTE = 0,
243 SCARLETT2_BUTTON_DIM = 1,
244 SCARLETT2_DIM_MUTE_COUNT = 2,
245};
246
247static const char *const scarlett2_dim_mute_names[SCARLETT2_DIM_MUTE_COUNT] = {
248 "Mute Playback Switch", "Dim Playback Switch"
249};
250
251/* Description of each hardware port type:
252 * - id: hardware ID of this port type
253 * - src_descr: printf format string for mux input selections
254 * - src_num_offset: added to channel number for the fprintf
255 * - dst_descr: printf format string for mixer controls
256 */
257struct scarlett2_port {
258 u16 id;
259 const char * const src_descr;
260 int src_num_offset;
261 const char * const dst_descr;
262};
263
264static const struct scarlett2_port scarlett2_ports[SCARLETT2_PORT_TYPE_COUNT] = {
265 [SCARLETT2_PORT_TYPE_NONE] = {
266 .id = 0x000,
267 .src_descr = "Off"
268 },
269 [SCARLETT2_PORT_TYPE_ANALOGUE] = {
270 .id = 0x080,
271 .src_descr = "Analogue %d",
272 .src_num_offset = 1,
273 .dst_descr = "Analogue Output %02d Playback"
274 },
275 [SCARLETT2_PORT_TYPE_SPDIF] = {
276 .id = 0x180,
277 .src_descr = "S/PDIF %d",
278 .src_num_offset = 1,
279 .dst_descr = "S/PDIF Output %d Playback"
280 },
281 [SCARLETT2_PORT_TYPE_ADAT] = {
282 .id = 0x200,
283 .src_descr = "ADAT %d",
284 .src_num_offset = 1,
285 .dst_descr = "ADAT Output %d Playback"
286 },
287 [SCARLETT2_PORT_TYPE_MIX] = {
288 .id = 0x300,
289 .src_descr = "Mix %c",
290 .src_num_offset = 'A',
291 .dst_descr = "Mixer Input %02d Capture"
292 },
293 [SCARLETT2_PORT_TYPE_PCM] = {
294 .id = 0x600,
295 .src_descr = "PCM %d",
296 .src_num_offset = 1,
297 .dst_descr = "PCM %02d Capture"
298 },
299};
300
301/* Number of mux tables: one for each band of sample rates
302 * (44.1/48kHz, 88.2/96kHz, and 176.4/176kHz)
303 */
304#define SCARLETT2_MUX_TABLES 3
305
306/* Maximum number of entries in a mux table */
307#define SCARLETT2_MAX_MUX_ENTRIES 10
308
309/* One entry within mux_assignment defines the port type and range of
310 * ports to add to the set_mux message. The end of the list is marked
311 * with count == 0.
312 */
313struct scarlett2_mux_entry {
314 u8 port_type;
315 u8 start;
316 u8 count;
317};
318
319struct scarlett2_device_info {
320 u32 usb_id; /* USB device identifier */
321
322 /* Gen 3 devices have an internal MSD mode switch that needs
323 * to be disabled in order to access the full functionality of
324 * the device.
325 */
326 u8 has_msd_mode;
327
328 /* which set of configuration parameters the device uses */
329 u8 config_set;
330
331 /* line out hw volume is sw controlled */
332 u8 line_out_hw_vol;
333
334 /* support for main/alt speaker switching */
335 u8 has_speaker_switching;
336
337 /* support for talkback microphone */
338 u8 has_talkback;
339
340 /* the number of analogue inputs with a software switchable
341 * level control that can be set to line or instrument
342 */
343 u8 level_input_count;
344
345 /* the first input with a level control (0-based) */
346 u8 level_input_first;
347
348 /* the number of analogue inputs with a software switchable
349 * 10dB pad control
350 */
351 u8 pad_input_count;
352
353 /* the number of analogue inputs with a software switchable
354 * "air" control
355 */
356 u8 air_input_count;
357
358 /* the number of phantom (48V) software switchable controls */
359 u8 phantom_count;
360
361 /* the number of inputs each phantom switch controls */
362 u8 inputs_per_phantom;
363
364 /* the number of direct monitor options
365 * (0 = none, 1 = mono only, 2 = mono/stereo)
366 */
367 u8 direct_monitor;
368
369 /* remap analogue outputs; 18i8 Gen 3 has "line 3/4" connected
370 * internally to the analogue 7/8 outputs
371 */
372 u8 line_out_remap_enable;
373 u8 line_out_remap[SCARLETT2_ANALOGUE_MAX];
374
375 /* additional description for the line out volume controls */
376 const char * const line_out_descrs[SCARLETT2_ANALOGUE_MAX];
377
378 /* number of sources/destinations of each port type */
379 const int port_count[SCARLETT2_PORT_TYPE_COUNT][SCARLETT2_PORT_DIRNS];
380
381 /* layout/order of the entries in the set_mux message */
382 struct scarlett2_mux_entry mux_assignment[SCARLETT2_MUX_TABLES]
383 [SCARLETT2_MAX_MUX_ENTRIES];
384};
385
386struct scarlett2_data {
387 struct usb_mixer_interface *mixer;
388 struct mutex usb_mutex; /* prevent sending concurrent USB requests */
389 struct mutex data_mutex; /* lock access to this data */
390 struct delayed_work work;
391 const struct scarlett2_device_info *info;
392 __u8 bInterfaceNumber;
393 __u8 bEndpointAddress;
394 __u16 wMaxPacketSize;
395 __u8 bInterval;
396 int num_mux_srcs;
397 int num_mux_dsts;
398 u16 scarlett2_seq;
399 u8 sync_updated;
400 u8 vol_updated;
401 u8 input_other_updated;
402 u8 monitor_other_updated;
403 u8 mux_updated;
404 u8 speaker_switching_switched;
405 u8 sync;
406 u8 master_vol;
407 u8 vol[SCARLETT2_ANALOGUE_MAX];
408 u8 vol_sw_hw_switch[SCARLETT2_ANALOGUE_MAX];
409 u8 mute_switch[SCARLETT2_ANALOGUE_MAX];
410 u8 level_switch[SCARLETT2_LEVEL_SWITCH_MAX];
411 u8 pad_switch[SCARLETT2_PAD_SWITCH_MAX];
412 u8 dim_mute[SCARLETT2_DIM_MUTE_COUNT];
413 u8 air_switch[SCARLETT2_AIR_SWITCH_MAX];
414 u8 phantom_switch[SCARLETT2_PHANTOM_SWITCH_MAX];
415 u8 phantom_persistence;
416 u8 direct_monitor_switch;
417 u8 speaker_switching_switch;
418 u8 talkback_switch;
419 u8 talkback_map[SCARLETT2_OUTPUT_MIX_MAX];
420 u8 msd_switch;
421 u8 standalone_switch;
422 struct snd_kcontrol *sync_ctl;
423 struct snd_kcontrol *master_vol_ctl;
424 struct snd_kcontrol *vol_ctls[SCARLETT2_ANALOGUE_MAX];
425 struct snd_kcontrol *sw_hw_ctls[SCARLETT2_ANALOGUE_MAX];
426 struct snd_kcontrol *mute_ctls[SCARLETT2_ANALOGUE_MAX];
427 struct snd_kcontrol *dim_mute_ctls[SCARLETT2_DIM_MUTE_COUNT];
428 struct snd_kcontrol *level_ctls[SCARLETT2_LEVEL_SWITCH_MAX];
429 struct snd_kcontrol *pad_ctls[SCARLETT2_PAD_SWITCH_MAX];
430 struct snd_kcontrol *air_ctls[SCARLETT2_AIR_SWITCH_MAX];
431 struct snd_kcontrol *phantom_ctls[SCARLETT2_PHANTOM_SWITCH_MAX];
432 struct snd_kcontrol *mux_ctls[SCARLETT2_MUX_MAX];
433 struct snd_kcontrol *direct_monitor_ctl;
434 struct snd_kcontrol *speaker_switching_ctl;
435 struct snd_kcontrol *talkback_ctl;
436 u8 mux[SCARLETT2_MUX_MAX];
437 u8 mix[SCARLETT2_INPUT_MIX_MAX * SCARLETT2_OUTPUT_MIX_MAX];
438};
439
440/*** Model-specific data ***/
441
442static const struct scarlett2_device_info s6i6_gen2_info = {
443 .usb_id = USB_ID(0x1235, 0x8203),
444
445 .config_set = SCARLETT2_CONFIG_SET_GEN_2,
446 .level_input_count = 2,
447 .pad_input_count = 2,
448
449 .line_out_descrs = {
450 "Headphones 1 L",
451 "Headphones 1 R",
452 "Headphones 2 L",
453 "Headphones 2 R",
454 },
455
456 .port_count = {
457 [SCARLETT2_PORT_TYPE_NONE] = { 1, 0 },
458 [SCARLETT2_PORT_TYPE_ANALOGUE] = { 4, 4 },
459 [SCARLETT2_PORT_TYPE_SPDIF] = { 2, 2 },
460 [SCARLETT2_PORT_TYPE_MIX] = { 10, 18 },
461 [SCARLETT2_PORT_TYPE_PCM] = { 6, 6 },
462 },
463
464 .mux_assignment = { {
465 { SCARLETT2_PORT_TYPE_PCM, 0, 6 },
466 { SCARLETT2_PORT_TYPE_ANALOGUE, 0, 4 },
467 { SCARLETT2_PORT_TYPE_SPDIF, 0, 2 },
468 { SCARLETT2_PORT_TYPE_MIX, 0, 18 },
469 { SCARLETT2_PORT_TYPE_NONE, 0, 8 },
470 { 0, 0, 0 },
471 }, {
472 { SCARLETT2_PORT_TYPE_PCM, 0, 6 },
473 { SCARLETT2_PORT_TYPE_ANALOGUE, 0, 4 },
474 { SCARLETT2_PORT_TYPE_SPDIF, 0, 2 },
475 { SCARLETT2_PORT_TYPE_MIX, 0, 18 },
476 { SCARLETT2_PORT_TYPE_NONE, 0, 8 },
477 { 0, 0, 0 },
478 }, {
479 { SCARLETT2_PORT_TYPE_PCM, 0, 6 },
480 { SCARLETT2_PORT_TYPE_ANALOGUE, 0, 4 },
481 { SCARLETT2_PORT_TYPE_SPDIF, 0, 2 },
482 { SCARLETT2_PORT_TYPE_MIX, 0, 18 },
483 { SCARLETT2_PORT_TYPE_NONE, 0, 8 },
484 { 0, 0, 0 },
485 } },
486};
487
488static const struct scarlett2_device_info s18i8_gen2_info = {
489 .usb_id = USB_ID(0x1235, 0x8204),
490
491 .config_set = SCARLETT2_CONFIG_SET_GEN_2,
492 .level_input_count = 2,
493 .pad_input_count = 4,
494
495 .line_out_descrs = {
496 "Monitor L",
497 "Monitor R",
498 "Headphones 1 L",
499 "Headphones 1 R",
500 "Headphones 2 L",
501 "Headphones 2 R",
502 },
503
504 .port_count = {
505 [SCARLETT2_PORT_TYPE_NONE] = { 1, 0 },
506 [SCARLETT2_PORT_TYPE_ANALOGUE] = { 8, 6 },
507 [SCARLETT2_PORT_TYPE_SPDIF] = { 2, 2 },
508 [SCARLETT2_PORT_TYPE_ADAT] = { 8, 0 },
509 [SCARLETT2_PORT_TYPE_MIX] = { 10, 18 },
510 [SCARLETT2_PORT_TYPE_PCM] = { 8, 18 },
511 },
512
513 .mux_assignment = { {
514 { SCARLETT2_PORT_TYPE_PCM, 0, 18 },
515 { SCARLETT2_PORT_TYPE_ANALOGUE, 0, 6 },
516 { SCARLETT2_PORT_TYPE_SPDIF, 0, 2 },
517 { SCARLETT2_PORT_TYPE_MIX, 0, 18 },
518 { SCARLETT2_PORT_TYPE_NONE, 0, 8 },
519 { 0, 0, 0 },
520 }, {
521 { SCARLETT2_PORT_TYPE_PCM, 0, 14 },
522 { SCARLETT2_PORT_TYPE_ANALOGUE, 0, 6 },
523 { SCARLETT2_PORT_TYPE_SPDIF, 0, 2 },
524 { SCARLETT2_PORT_TYPE_MIX, 0, 18 },
525 { SCARLETT2_PORT_TYPE_NONE, 0, 8 },
526 { 0, 0, 0 },
527 }, {
528 { SCARLETT2_PORT_TYPE_PCM, 0, 10 },
529 { SCARLETT2_PORT_TYPE_ANALOGUE, 0, 6 },
530 { SCARLETT2_PORT_TYPE_SPDIF, 0, 2 },
531 { SCARLETT2_PORT_TYPE_MIX, 0, 18 },
532 { SCARLETT2_PORT_TYPE_NONE, 0, 4 },
533 { 0, 0, 0 },
534 } },
535};
536
537static const struct scarlett2_device_info s18i20_gen2_info = {
538 .usb_id = USB_ID(0x1235, 0x8201),
539
540 .config_set = SCARLETT2_CONFIG_SET_GEN_2,
541 .line_out_hw_vol = 1,
542
543 .line_out_descrs = {
544 "Monitor L",
545 "Monitor R",
546 NULL,
547 NULL,
548 NULL,
549 NULL,
550 "Headphones 1 L",
551 "Headphones 1 R",
552 "Headphones 2 L",
553 "Headphones 2 R",
554 },
555
556 .port_count = {
557 [SCARLETT2_PORT_TYPE_NONE] = { 1, 0 },
558 [SCARLETT2_PORT_TYPE_ANALOGUE] = { 8, 10 },
559 [SCARLETT2_PORT_TYPE_SPDIF] = { 2, 2 },
560 [SCARLETT2_PORT_TYPE_ADAT] = { 8, 8 },
561 [SCARLETT2_PORT_TYPE_MIX] = { 10, 18 },
562 [SCARLETT2_PORT_TYPE_PCM] = { 20, 18 },
563 },
564
565 .mux_assignment = { {
566 { SCARLETT2_PORT_TYPE_PCM, 0, 18 },
567 { SCARLETT2_PORT_TYPE_ANALOGUE, 0, 10 },
568 { SCARLETT2_PORT_TYPE_SPDIF, 0, 2 },
569 { SCARLETT2_PORT_TYPE_ADAT, 0, 8 },
570 { SCARLETT2_PORT_TYPE_MIX, 0, 18 },
571 { SCARLETT2_PORT_TYPE_NONE, 0, 8 },
572 { 0, 0, 0 },
573 }, {
574 { SCARLETT2_PORT_TYPE_PCM, 0, 14 },
575 { SCARLETT2_PORT_TYPE_ANALOGUE, 0, 10 },
576 { SCARLETT2_PORT_TYPE_SPDIF, 0, 2 },
577 { SCARLETT2_PORT_TYPE_ADAT, 0, 4 },
578 { SCARLETT2_PORT_TYPE_MIX, 0, 18 },
579 { SCARLETT2_PORT_TYPE_NONE, 0, 8 },
580 { 0, 0, 0 },
581 }, {
582 { SCARLETT2_PORT_TYPE_PCM, 0, 10 },
583 { SCARLETT2_PORT_TYPE_ANALOGUE, 0, 10 },
584 { SCARLETT2_PORT_TYPE_SPDIF, 0, 2 },
585 { SCARLETT2_PORT_TYPE_MIX, 0, 18 },
586 { SCARLETT2_PORT_TYPE_NONE, 0, 6 },
587 { 0, 0, 0 },
588 } },
589};
590
591static const struct scarlett2_device_info solo_gen3_info = {
592 .usb_id = USB_ID(0x1235, 0x8211),
593
594 .has_msd_mode = 1,
595 .config_set = SCARLETT2_CONFIG_SET_NO_MIXER,
596 .level_input_count = 1,
597 .level_input_first = 1,
598 .air_input_count = 1,
599 .phantom_count = 1,
600 .inputs_per_phantom = 1,
601 .direct_monitor = 1,
602};
603
604static const struct scarlett2_device_info s2i2_gen3_info = {
605 .usb_id = USB_ID(0x1235, 0x8210),
606
607 .has_msd_mode = 1,
608 .config_set = SCARLETT2_CONFIG_SET_NO_MIXER,
609 .level_input_count = 2,
610 .air_input_count = 2,
611 .phantom_count = 1,
612 .inputs_per_phantom = 2,
613 .direct_monitor = 2,
614};
615
616static const struct scarlett2_device_info s4i4_gen3_info = {
617 .usb_id = USB_ID(0x1235, 0x8212),
618
619 .has_msd_mode = 1,
620 .config_set = SCARLETT2_CONFIG_SET_GEN_3,
621 .level_input_count = 2,
622 .pad_input_count = 2,
623 .air_input_count = 2,
624 .phantom_count = 1,
625 .inputs_per_phantom = 2,
626
627 .line_out_descrs = {
628 "Monitor L",
629 "Monitor R",
630 "Headphones L",
631 "Headphones R",
632 },
633
634 .port_count = {
635 [SCARLETT2_PORT_TYPE_NONE] = { 1, 0 },
636 [SCARLETT2_PORT_TYPE_ANALOGUE] = { 4, 4 },
637 [SCARLETT2_PORT_TYPE_MIX] = { 6, 8 },
638 [SCARLETT2_PORT_TYPE_PCM] = { 4, 6 },
639 },
640
641 .mux_assignment = { {
642 { SCARLETT2_PORT_TYPE_PCM, 0, 6 },
643 { SCARLETT2_PORT_TYPE_ANALOGUE, 0, 4 },
644 { SCARLETT2_PORT_TYPE_MIX, 0, 8 },
645 { SCARLETT2_PORT_TYPE_NONE, 0, 16 },
646 { 0, 0, 0 },
647 }, {
648 { SCARLETT2_PORT_TYPE_PCM, 0, 6 },
649 { SCARLETT2_PORT_TYPE_ANALOGUE, 0, 4 },
650 { SCARLETT2_PORT_TYPE_MIX, 0, 8 },
651 { SCARLETT2_PORT_TYPE_NONE, 0, 16 },
652 { 0, 0, 0 },
653 }, {
654 { SCARLETT2_PORT_TYPE_PCM, 0, 6 },
655 { SCARLETT2_PORT_TYPE_ANALOGUE, 0, 4 },
656 { SCARLETT2_PORT_TYPE_MIX, 0, 8 },
657 { SCARLETT2_PORT_TYPE_NONE, 0, 16 },
658 { 0, 0, 0 },
659 } },
660};
661
662static const struct scarlett2_device_info s8i6_gen3_info = {
663 .usb_id = USB_ID(0x1235, 0x8213),
664
665 .has_msd_mode = 1,
666 .config_set = SCARLETT2_CONFIG_SET_GEN_3,
667 .level_input_count = 2,
668 .pad_input_count = 2,
669 .air_input_count = 2,
670 .phantom_count = 1,
671 .inputs_per_phantom = 2,
672
673 .line_out_descrs = {
674 "Headphones 1 L",
675 "Headphones 1 R",
676 "Headphones 2 L",
677 "Headphones 2 R",
678 },
679
680 .port_count = {
681 [SCARLETT2_PORT_TYPE_NONE] = { 1, 0 },
682 [SCARLETT2_PORT_TYPE_ANALOGUE] = { 6, 4 },
683 [SCARLETT2_PORT_TYPE_SPDIF] = { 2, 2 },
684 [SCARLETT2_PORT_TYPE_MIX] = { 8, 8 },
685 [SCARLETT2_PORT_TYPE_PCM] = { 6, 10 },
686 },
687
688 .mux_assignment = { {
689 { SCARLETT2_PORT_TYPE_PCM, 0, 8 },
690 { SCARLETT2_PORT_TYPE_ANALOGUE, 0, 4 },
691 { SCARLETT2_PORT_TYPE_SPDIF, 0, 2 },
692 { SCARLETT2_PORT_TYPE_PCM, 8, 2 },
693 { SCARLETT2_PORT_TYPE_MIX, 0, 8 },
694 { SCARLETT2_PORT_TYPE_NONE, 0, 18 },
695 { 0, 0, 0 },
696 }, {
697 { SCARLETT2_PORT_TYPE_PCM, 0, 8 },
698 { SCARLETT2_PORT_TYPE_ANALOGUE, 0, 4 },
699 { SCARLETT2_PORT_TYPE_SPDIF, 0, 2 },
700 { SCARLETT2_PORT_TYPE_PCM, 8, 2 },
701 { SCARLETT2_PORT_TYPE_MIX, 0, 8 },
702 { SCARLETT2_PORT_TYPE_NONE, 0, 18 },
703 { 0, 0, 0 },
704 }, {
705 { SCARLETT2_PORT_TYPE_PCM, 0, 8 },
706 { SCARLETT2_PORT_TYPE_ANALOGUE, 0, 4 },
707 { SCARLETT2_PORT_TYPE_SPDIF, 0, 2 },
708 { SCARLETT2_PORT_TYPE_PCM, 8, 2 },
709 { SCARLETT2_PORT_TYPE_MIX, 0, 8 },
710 { SCARLETT2_PORT_TYPE_NONE, 0, 18 },
711 { 0, 0, 0 },
712 } },
713};
714
715static const struct scarlett2_device_info s18i8_gen3_info = {
716 .usb_id = USB_ID(0x1235, 0x8214),
717
718 .has_msd_mode = 1,
719 .config_set = SCARLETT2_CONFIG_SET_GEN_3,
720 .line_out_hw_vol = 1,
721 .has_speaker_switching = 1,
722 .level_input_count = 2,
723 .pad_input_count = 4,
724 .air_input_count = 4,
725 .phantom_count = 2,
726 .inputs_per_phantom = 2,
727
728 .line_out_remap_enable = 1,
729 .line_out_remap = { 0, 1, 6, 7, 2, 3, 4, 5 },
730
731 .line_out_descrs = {
732 "Monitor L",
733 "Monitor R",
734 "Alt Monitor L",
735 "Alt Monitor R",
736 "Headphones 1 L",
737 "Headphones 1 R",
738 "Headphones 2 L",
739 "Headphones 2 R",
740 },
741
742 .port_count = {
743 [SCARLETT2_PORT_TYPE_NONE] = { 1, 0 },
744 [SCARLETT2_PORT_TYPE_ANALOGUE] = { 8, 8 },
745 [SCARLETT2_PORT_TYPE_SPDIF] = { 2, 2 },
746 [SCARLETT2_PORT_TYPE_ADAT] = { 8, 0 },
747 [SCARLETT2_PORT_TYPE_MIX] = { 10, 20 },
748 [SCARLETT2_PORT_TYPE_PCM] = { 8, 20 },
749 },
750
751 .mux_assignment = { {
752 { SCARLETT2_PORT_TYPE_PCM, 0, 10 },
753 { SCARLETT2_PORT_TYPE_PCM, 12, 8 },
754 { SCARLETT2_PORT_TYPE_ANALOGUE, 0, 2 },
755 { SCARLETT2_PORT_TYPE_ANALOGUE, 6, 2 },
756 { SCARLETT2_PORT_TYPE_ANALOGUE, 2, 4 },
757 { SCARLETT2_PORT_TYPE_SPDIF, 0, 2 },
758 { SCARLETT2_PORT_TYPE_PCM, 10, 2 },
759 { SCARLETT2_PORT_TYPE_MIX, 0, 20 },
760 { SCARLETT2_PORT_TYPE_NONE, 0, 10 },
761 { 0, 0, 0 },
762 }, {
763 { SCARLETT2_PORT_TYPE_PCM, 0, 10 },
764 { SCARLETT2_PORT_TYPE_PCM, 12, 4 },
765 { SCARLETT2_PORT_TYPE_ANALOGUE, 0, 2 },
766 { SCARLETT2_PORT_TYPE_ANALOGUE, 6, 2 },
767 { SCARLETT2_PORT_TYPE_ANALOGUE, 2, 4 },
768 { SCARLETT2_PORT_TYPE_SPDIF, 0, 2 },
769 { SCARLETT2_PORT_TYPE_PCM, 10, 2 },
770 { SCARLETT2_PORT_TYPE_MIX, 0, 20 },
771 { SCARLETT2_PORT_TYPE_NONE, 0, 10 },
772 { 0, 0, 0 },
773 }, {
774 { SCARLETT2_PORT_TYPE_PCM, 0, 10 },
775 { SCARLETT2_PORT_TYPE_ANALOGUE, 0, 2 },
776 { SCARLETT2_PORT_TYPE_ANALOGUE, 6, 2 },
777 { SCARLETT2_PORT_TYPE_ANALOGUE, 2, 4 },
778 { SCARLETT2_PORT_TYPE_SPDIF, 0, 2 },
779 { SCARLETT2_PORT_TYPE_MIX, 0, 20 },
780 { SCARLETT2_PORT_TYPE_NONE, 0, 10 },
781 { 0, 0, 0 },
782 } },
783};
784
785static const struct scarlett2_device_info s18i20_gen3_info = {
786 .usb_id = USB_ID(0x1235, 0x8215),
787
788 .has_msd_mode = 1,
789 .config_set = SCARLETT2_CONFIG_SET_GEN_3,
790 .line_out_hw_vol = 1,
791 .has_speaker_switching = 1,
792 .has_talkback = 1,
793 .level_input_count = 2,
794 .pad_input_count = 8,
795 .air_input_count = 8,
796 .phantom_count = 2,
797 .inputs_per_phantom = 4,
798
799 .line_out_descrs = {
800 "Monitor 1 L",
801 "Monitor 1 R",
802 "Monitor 2 L",
803 "Monitor 2 R",
804 NULL,
805 NULL,
806 "Headphones 1 L",
807 "Headphones 1 R",
808 "Headphones 2 L",
809 "Headphones 2 R",
810 },
811
812 .port_count = {
813 [SCARLETT2_PORT_TYPE_NONE] = { 1, 0 },
814 [SCARLETT2_PORT_TYPE_ANALOGUE] = { 9, 10 },
815 [SCARLETT2_PORT_TYPE_SPDIF] = { 2, 2 },
816 [SCARLETT2_PORT_TYPE_ADAT] = { 8, 8 },
817 [SCARLETT2_PORT_TYPE_MIX] = { 12, 25 },
818 [SCARLETT2_PORT_TYPE_PCM] = { 20, 20 },
819 },
820
821 .mux_assignment = { {
822 { SCARLETT2_PORT_TYPE_PCM, 0, 8 },
823 { SCARLETT2_PORT_TYPE_PCM, 10, 10 },
824 { SCARLETT2_PORT_TYPE_ANALOGUE, 0, 10 },
825 { SCARLETT2_PORT_TYPE_SPDIF, 0, 2 },
826 { SCARLETT2_PORT_TYPE_ADAT, 0, 8 },
827 { SCARLETT2_PORT_TYPE_PCM, 8, 2 },
828 { SCARLETT2_PORT_TYPE_MIX, 0, 25 },
829 { SCARLETT2_PORT_TYPE_NONE, 0, 12 },
830 { 0, 0, 0 },
831 }, {
832 { SCARLETT2_PORT_TYPE_PCM, 0, 8 },
833 { SCARLETT2_PORT_TYPE_PCM, 10, 8 },
834 { SCARLETT2_PORT_TYPE_ANALOGUE, 0, 10 },
835 { SCARLETT2_PORT_TYPE_SPDIF, 0, 2 },
836 { SCARLETT2_PORT_TYPE_ADAT, 0, 8 },
837 { SCARLETT2_PORT_TYPE_PCM, 8, 2 },
838 { SCARLETT2_PORT_TYPE_MIX, 0, 25 },
839 { SCARLETT2_PORT_TYPE_NONE, 0, 10 },
840 { 0, 0, 0 },
841 }, {
842 { SCARLETT2_PORT_TYPE_PCM, 0, 10 },
843 { SCARLETT2_PORT_TYPE_ANALOGUE, 0, 10 },
844 { SCARLETT2_PORT_TYPE_SPDIF, 0, 2 },
845 { SCARLETT2_PORT_TYPE_NONE, 0, 24 },
846 { 0, 0, 0 },
847 } },
848};
849
850static const struct scarlett2_device_info clarett_8pre_info = {
851 .usb_id = USB_ID(0x1235, 0x820c),
852
853 .config_set = SCARLETT2_CONFIG_SET_CLARETT,
854 .line_out_hw_vol = 1,
855 .level_input_count = 2,
856 .air_input_count = 8,
857
858 .line_out_descrs = {
859 "Monitor L",
860 "Monitor R",
861 NULL,
862 NULL,
863 NULL,
864 NULL,
865 "Headphones 1 L",
866 "Headphones 1 R",
867 "Headphones 2 L",
868 "Headphones 2 R",
869 },
870
871 .port_count = {
872 [SCARLETT2_PORT_TYPE_NONE] = { 1, 0 },
873 [SCARLETT2_PORT_TYPE_ANALOGUE] = { 8, 10 },
874 [SCARLETT2_PORT_TYPE_SPDIF] = { 2, 2 },
875 [SCARLETT2_PORT_TYPE_ADAT] = { 8, 8 },
876 [SCARLETT2_PORT_TYPE_MIX] = { 10, 18 },
877 [SCARLETT2_PORT_TYPE_PCM] = { 20, 18 },
878 },
879
880 .mux_assignment = { {
881 { SCARLETT2_PORT_TYPE_PCM, 0, 18 },
882 { SCARLETT2_PORT_TYPE_ANALOGUE, 0, 10 },
883 { SCARLETT2_PORT_TYPE_SPDIF, 0, 2 },
884 { SCARLETT2_PORT_TYPE_ADAT, 0, 8 },
885 { SCARLETT2_PORT_TYPE_MIX, 0, 18 },
886 { SCARLETT2_PORT_TYPE_NONE, 0, 8 },
887 { 0, 0, 0 },
888 }, {
889 { SCARLETT2_PORT_TYPE_PCM, 0, 14 },
890 { SCARLETT2_PORT_TYPE_ANALOGUE, 0, 10 },
891 { SCARLETT2_PORT_TYPE_SPDIF, 0, 2 },
892 { SCARLETT2_PORT_TYPE_ADAT, 0, 4 },
893 { SCARLETT2_PORT_TYPE_MIX, 0, 18 },
894 { SCARLETT2_PORT_TYPE_NONE, 0, 8 },
895 { 0, 0, 0 },
896 }, {
897 { SCARLETT2_PORT_TYPE_PCM, 0, 12 },
898 { SCARLETT2_PORT_TYPE_ANALOGUE, 0, 10 },
899 { SCARLETT2_PORT_TYPE_SPDIF, 0, 2 },
900 { SCARLETT2_PORT_TYPE_NONE, 0, 22 },
901 { 0, 0, 0 },
902 } },
903};
904
905static const struct scarlett2_device_info *scarlett2_devices[] = {
906 /* Supported Gen 2 devices */
907 &s6i6_gen2_info,
908 &s18i8_gen2_info,
909 &s18i20_gen2_info,
910
911 /* Supported Gen 3 devices */
912 &solo_gen3_info,
913 &s2i2_gen3_info,
914 &s4i4_gen3_info,
915 &s8i6_gen3_info,
916 &s18i8_gen3_info,
917 &s18i20_gen3_info,
918
919 /* Supported Clarett+ devices */
920 &clarett_8pre_info,
921
922 /* End of list */
923 NULL
924};
925
926/* get the starting port index number for a given port type/direction */
927static int scarlett2_get_port_start_num(
928 const int port_count[][SCARLETT2_PORT_DIRNS],
929 int direction, int port_type)
930{
931 int i, num = 0;
932
933 for (i = 0; i < port_type; i++)
934 num += port_count[i][direction];
935
936 return num;
937}
938
939/*** USB Interactions ***/
940
941/* Notifications from the interface */
942#define SCARLETT2_USB_NOTIFY_SYNC 0x00000008
943#define SCARLETT2_USB_NOTIFY_DIM_MUTE 0x00200000
944#define SCARLETT2_USB_NOTIFY_MONITOR 0x00400000
945#define SCARLETT2_USB_NOTIFY_INPUT_OTHER 0x00800000
946#define SCARLETT2_USB_NOTIFY_MONITOR_OTHER 0x01000000
947
948/* Commands for sending/receiving requests/responses */
949#define SCARLETT2_USB_CMD_INIT 0
950#define SCARLETT2_USB_CMD_REQ 2
951#define SCARLETT2_USB_CMD_RESP 3
952
953#define SCARLETT2_USB_INIT_1 0x00000000
954#define SCARLETT2_USB_INIT_2 0x00000002
955#define SCARLETT2_USB_GET_METER 0x00001001
956#define SCARLETT2_USB_GET_MIX 0x00002001
957#define SCARLETT2_USB_SET_MIX 0x00002002
958#define SCARLETT2_USB_GET_MUX 0x00003001
959#define SCARLETT2_USB_SET_MUX 0x00003002
960#define SCARLETT2_USB_GET_SYNC 0x00006004
961#define SCARLETT2_USB_GET_DATA 0x00800000
962#define SCARLETT2_USB_SET_DATA 0x00800001
963#define SCARLETT2_USB_DATA_CMD 0x00800002
964
965#define SCARLETT2_USB_CONFIG_SAVE 6
966
967#define SCARLETT2_USB_VOLUME_STATUS_OFFSET 0x31
968#define SCARLETT2_USB_METER_LEVELS_GET_MAGIC 1
969
970/* volume status is read together (matches scarlett2_config_items[1]) */
971struct scarlett2_usb_volume_status {
972 /* dim/mute buttons */
973 u8 dim_mute[SCARLETT2_DIM_MUTE_COUNT];
974
975 u8 pad1;
976
977 /* software volume setting */
978 s16 sw_vol[SCARLETT2_ANALOGUE_MAX];
979
980 /* actual volume of output inc. dim (-18dB) */
981 s16 hw_vol[SCARLETT2_ANALOGUE_MAX];
982
983 /* internal mute buttons */
984 u8 mute_switch[SCARLETT2_ANALOGUE_MAX];
985
986 /* sw (0) or hw (1) controlled */
987 u8 sw_hw_switch[SCARLETT2_ANALOGUE_MAX];
988
989 u8 pad3[6];
990
991 /* front panel volume knob */
992 s16 master_vol;
993} __packed;
994
995/* Configuration parameters that can be read and written */
996enum {
997 SCARLETT2_CONFIG_DIM_MUTE = 0,
998 SCARLETT2_CONFIG_LINE_OUT_VOLUME = 1,
999 SCARLETT2_CONFIG_MUTE_SWITCH = 2,
1000 SCARLETT2_CONFIG_SW_HW_SWITCH = 3,
1001 SCARLETT2_CONFIG_LEVEL_SWITCH = 4,
1002 SCARLETT2_CONFIG_PAD_SWITCH = 5,
1003 SCARLETT2_CONFIG_MSD_SWITCH = 6,
1004 SCARLETT2_CONFIG_AIR_SWITCH = 7,
1005 SCARLETT2_CONFIG_STANDALONE_SWITCH = 8,
1006 SCARLETT2_CONFIG_PHANTOM_SWITCH = 9,
1007 SCARLETT2_CONFIG_PHANTOM_PERSISTENCE = 10,
1008 SCARLETT2_CONFIG_DIRECT_MONITOR = 11,
1009 SCARLETT2_CONFIG_MONITOR_OTHER_SWITCH = 12,
1010 SCARLETT2_CONFIG_MONITOR_OTHER_ENABLE = 13,
1011 SCARLETT2_CONFIG_TALKBACK_MAP = 14,
1012 SCARLETT2_CONFIG_COUNT = 15
1013};
1014
1015/* Location, size, and activation command number for the configuration
1016 * parameters. Size is in bits and may be 1, 8, or 16.
1017 */
1018struct scarlett2_config {
1019 u8 offset;
1020 u8 size;
1021 u8 activate;
1022};
1023
1024static const struct scarlett2_config
1025 scarlett2_config_items[SCARLETT2_CONFIG_SET_COUNT]
1026 [SCARLETT2_CONFIG_COUNT] =
1027
1028/* Devices without a mixer (Gen 3 Solo and 2i2) */
1029{ {
1030 [SCARLETT2_CONFIG_MSD_SWITCH] = {
1031 .offset = 0x04, .size = 8, .activate = 6 },
1032
1033 [SCARLETT2_CONFIG_PHANTOM_PERSISTENCE] = {
1034 .offset = 0x05, .size = 8, .activate = 6 },
1035
1036 [SCARLETT2_CONFIG_PHANTOM_SWITCH] = {
1037 .offset = 0x06, .size = 8, .activate = 3 },
1038
1039 [SCARLETT2_CONFIG_DIRECT_MONITOR] = {
1040 .offset = 0x07, .size = 8, .activate = 4 },
1041
1042 [SCARLETT2_CONFIG_LEVEL_SWITCH] = {
1043 .offset = 0x08, .size = 1, .activate = 7 },
1044
1045 [SCARLETT2_CONFIG_AIR_SWITCH] = {
1046 .offset = 0x09, .size = 1, .activate = 8 },
1047
1048/* Gen 2 devices: 6i6, 18i8, 18i20 */
1049}, {
1050 [SCARLETT2_CONFIG_DIM_MUTE] = {
1051 .offset = 0x31, .size = 8, .activate = 2 },
1052
1053 [SCARLETT2_CONFIG_LINE_OUT_VOLUME] = {
1054 .offset = 0x34, .size = 16, .activate = 1 },
1055
1056 [SCARLETT2_CONFIG_MUTE_SWITCH] = {
1057 .offset = 0x5c, .size = 8, .activate = 1 },
1058
1059 [SCARLETT2_CONFIG_SW_HW_SWITCH] = {
1060 .offset = 0x66, .size = 8, .activate = 3 },
1061
1062 [SCARLETT2_CONFIG_LEVEL_SWITCH] = {
1063 .offset = 0x7c, .size = 8, .activate = 7 },
1064
1065 [SCARLETT2_CONFIG_PAD_SWITCH] = {
1066 .offset = 0x84, .size = 8, .activate = 8 },
1067
1068 [SCARLETT2_CONFIG_STANDALONE_SWITCH] = {
1069 .offset = 0x8d, .size = 8, .activate = 6 },
1070
1071/* Gen 3 devices: 4i4, 8i6, 18i8, 18i20 */
1072}, {
1073 [SCARLETT2_CONFIG_DIM_MUTE] = {
1074 .offset = 0x31, .size = 8, .activate = 2 },
1075
1076 [SCARLETT2_CONFIG_LINE_OUT_VOLUME] = {
1077 .offset = 0x34, .size = 16, .activate = 1 },
1078
1079 [SCARLETT2_CONFIG_MUTE_SWITCH] = {
1080 .offset = 0x5c, .size = 8, .activate = 1 },
1081
1082 [SCARLETT2_CONFIG_SW_HW_SWITCH] = {
1083 .offset = 0x66, .size = 8, .activate = 3 },
1084
1085 [SCARLETT2_CONFIG_LEVEL_SWITCH] = {
1086 .offset = 0x7c, .size = 8, .activate = 7 },
1087
1088 [SCARLETT2_CONFIG_PAD_SWITCH] = {
1089 .offset = 0x84, .size = 8, .activate = 8 },
1090
1091 [SCARLETT2_CONFIG_AIR_SWITCH] = {
1092 .offset = 0x8c, .size = 8, .activate = 8 },
1093
1094 [SCARLETT2_CONFIG_STANDALONE_SWITCH] = {
1095 .offset = 0x95, .size = 8, .activate = 6 },
1096
1097 [SCARLETT2_CONFIG_PHANTOM_SWITCH] = {
1098 .offset = 0x9c, .size = 1, .activate = 8 },
1099
1100 [SCARLETT2_CONFIG_MSD_SWITCH] = {
1101 .offset = 0x9d, .size = 8, .activate = 6 },
1102
1103 [SCARLETT2_CONFIG_PHANTOM_PERSISTENCE] = {
1104 .offset = 0x9e, .size = 8, .activate = 6 },
1105
1106 [SCARLETT2_CONFIG_MONITOR_OTHER_SWITCH] = {
1107 .offset = 0x9f, .size = 1, .activate = 10 },
1108
1109 [SCARLETT2_CONFIG_MONITOR_OTHER_ENABLE] = {
1110 .offset = 0xa0, .size = 1, .activate = 10 },
1111
1112 [SCARLETT2_CONFIG_TALKBACK_MAP] = {
1113 .offset = 0xb0, .size = 16, .activate = 10 },
1114
1115/* Clarett+ 8Pre */
1116}, {
1117 [SCARLETT2_CONFIG_DIM_MUTE] = {
1118 .offset = 0x31, .size = 8, .activate = 2 },
1119
1120 [SCARLETT2_CONFIG_LINE_OUT_VOLUME] = {
1121 .offset = 0x34, .size = 16, .activate = 1 },
1122
1123 [SCARLETT2_CONFIG_MUTE_SWITCH] = {
1124 .offset = 0x5c, .size = 8, .activate = 1 },
1125
1126 [SCARLETT2_CONFIG_SW_HW_SWITCH] = {
1127 .offset = 0x66, .size = 8, .activate = 3 },
1128
1129 [SCARLETT2_CONFIG_LEVEL_SWITCH] = {
1130 .offset = 0x7c, .size = 8, .activate = 7 },
1131
1132 [SCARLETT2_CONFIG_AIR_SWITCH] = {
1133 .offset = 0x95, .size = 8, .activate = 8 },
1134
1135 [SCARLETT2_CONFIG_STANDALONE_SWITCH] = {
1136 .offset = 0x8d, .size = 8, .activate = 6 },
1137} };
1138
1139/* proprietary request/response format */
1140struct scarlett2_usb_packet {
1141 __le32 cmd;
1142 __le16 size;
1143 __le16 seq;
1144 __le32 error;
1145 __le32 pad;
1146 u8 data[];
1147};
1148
1149static void scarlett2_fill_request_header(struct scarlett2_data *private,
1150 struct scarlett2_usb_packet *req,
1151 u32 cmd, u16 req_size)
1152{
1153 /* sequence must go up by 1 for each request */
1154 u16 seq = private->scarlett2_seq++;
1155
1156 req->cmd = cpu_to_le32(cmd);
1157 req->size = cpu_to_le16(req_size);
1158 req->seq = cpu_to_le16(seq);
1159 req->error = 0;
1160 req->pad = 0;
1161}
1162
1163static int scarlett2_usb_tx(struct usb_device *dev, int interface,
1164 void *buf, u16 size)
1165{
1166 return snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0),
1167 SCARLETT2_USB_CMD_REQ,
1168 USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_OUT,
1169 0, interface, buf, size);
1170}
1171
1172static int scarlett2_usb_rx(struct usb_device *dev, int interface,
1173 u32 usb_req, void *buf, u16 size)
1174{
1175 return snd_usb_ctl_msg(dev, usb_rcvctrlpipe(dev, 0),
1176 usb_req,
1177 USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_IN,
1178 0, interface, buf, size);
1179}
1180
1181/* Send a proprietary format request to the Scarlett interface */
1182static int scarlett2_usb(
1183 struct usb_mixer_interface *mixer, u32 cmd,
1184 void *req_data, u16 req_size, void *resp_data, u16 resp_size)
1185{
1186 struct scarlett2_data *private = mixer->private_data;
1187 struct usb_device *dev = mixer->chip->dev;
1188 struct scarlett2_usb_packet *req, *resp = NULL;
1189 size_t req_buf_size = struct_size(req, data, req_size);
1190 size_t resp_buf_size = struct_size(resp, data, resp_size);
1191 int err;
1192
1193 req = kmalloc(req_buf_size, GFP_KERNEL);
1194 if (!req) {
1195 err = -ENOMEM;
1196 goto error;
1197 }
1198
1199 resp = kmalloc(resp_buf_size, GFP_KERNEL);
1200 if (!resp) {
1201 err = -ENOMEM;
1202 goto error;
1203 }
1204
1205 mutex_lock(&private->usb_mutex);
1206
1207 /* build request message and send it */
1208
1209 scarlett2_fill_request_header(private, req, cmd, req_size);
1210
1211 if (req_size)
1212 memcpy(req->data, req_data, req_size);
1213
1214 err = scarlett2_usb_tx(dev, private->bInterfaceNumber,
1215 req, req_buf_size);
1216
1217 if (err != req_buf_size) {
1218 usb_audio_err(
1219 mixer->chip,
1220 "Scarlett Gen 2/3 USB request result cmd %x was %d\n",
1221 cmd, err);
1222 err = -EINVAL;
1223 goto unlock;
1224 }
1225
1226 /* send a second message to get the response */
1227
1228 err = scarlett2_usb_rx(dev, private->bInterfaceNumber,
1229 SCARLETT2_USB_CMD_RESP,
1230 resp, resp_buf_size);
1231
1232 /* validate the response */
1233
1234 if (err != resp_buf_size) {
1235 usb_audio_err(
1236 mixer->chip,
1237 "Scarlett Gen 2/3 USB response result cmd %x was %d "
1238 "expected %zu\n",
1239 cmd, err, resp_buf_size);
1240 err = -EINVAL;
1241 goto unlock;
1242 }
1243
1244 /* cmd/seq/size should match except when initialising
1245 * seq sent = 1, response = 0
1246 */
1247 if (resp->cmd != req->cmd ||
1248 (resp->seq != req->seq &&
1249 (le16_to_cpu(req->seq) != 1 || resp->seq != 0)) ||
1250 resp_size != le16_to_cpu(resp->size) ||
1251 resp->error ||
1252 resp->pad) {
1253 usb_audio_err(
1254 mixer->chip,
1255 "Scarlett Gen 2/3 USB invalid response; "
1256 "cmd tx/rx %d/%d seq %d/%d size %d/%d "
1257 "error %d pad %d\n",
1258 le32_to_cpu(req->cmd), le32_to_cpu(resp->cmd),
1259 le16_to_cpu(req->seq), le16_to_cpu(resp->seq),
1260 resp_size, le16_to_cpu(resp->size),
1261 le32_to_cpu(resp->error),
1262 le32_to_cpu(resp->pad));
1263 err = -EINVAL;
1264 goto unlock;
1265 }
1266
1267 if (resp_data && resp_size > 0)
1268 memcpy(resp_data, resp->data, resp_size);
1269
1270unlock:
1271 mutex_unlock(&private->usb_mutex);
1272error:
1273 kfree(req);
1274 kfree(resp);
1275 return err;
1276}
1277
1278/* Send a USB message to get data; result placed in *buf */
1279static int scarlett2_usb_get(
1280 struct usb_mixer_interface *mixer,
1281 int offset, void *buf, int size)
1282{
1283 struct {
1284 __le32 offset;
1285 __le32 size;
1286 } __packed req;
1287
1288 req.offset = cpu_to_le32(offset);
1289 req.size = cpu_to_le32(size);
1290 return scarlett2_usb(mixer, SCARLETT2_USB_GET_DATA,
1291 &req, sizeof(req), buf, size);
1292}
1293
1294/* Send a USB message to get configuration parameters; result placed in *buf */
1295static int scarlett2_usb_get_config(
1296 struct usb_mixer_interface *mixer,
1297 int config_item_num, int count, void *buf)
1298{
1299 struct scarlett2_data *private = mixer->private_data;
1300 const struct scarlett2_device_info *info = private->info;
1301 const struct scarlett2_config *config_item =
1302 &scarlett2_config_items[info->config_set][config_item_num];
1303 int size, err, i;
1304 u8 *buf_8;
1305 u8 value;
1306
1307 /* For byte-sized parameters, retrieve directly into buf */
1308 if (config_item->size >= 8) {
1309 size = config_item->size / 8 * count;
1310 err = scarlett2_usb_get(mixer, config_item->offset, buf, size);
1311 if (err < 0)
1312 return err;
1313 if (size == 2) {
1314 u16 *buf_16 = buf;
1315
1316 for (i = 0; i < count; i++, buf_16++)
1317 *buf_16 = le16_to_cpu(*(__le16 *)buf_16);
1318 }
1319 return 0;
1320 }
1321
1322 /* For bit-sized parameters, retrieve into value */
1323 err = scarlett2_usb_get(mixer, config_item->offset, &value, 1);
1324 if (err < 0)
1325 return err;
1326
1327 /* then unpack from value into buf[] */
1328 buf_8 = buf;
1329 for (i = 0; i < 8 && i < count; i++, value >>= 1)
1330 *buf_8++ = value & 1;
1331
1332 return 0;
1333}
1334
1335/* Send SCARLETT2_USB_DATA_CMD SCARLETT2_USB_CONFIG_SAVE */
1336static void scarlett2_config_save(struct usb_mixer_interface *mixer)
1337{
1338 __le32 req = cpu_to_le32(SCARLETT2_USB_CONFIG_SAVE);
1339
1340 scarlett2_usb(mixer, SCARLETT2_USB_DATA_CMD,
1341 &req, sizeof(u32),
1342 NULL, 0);
1343}
1344
1345/* Delayed work to save config */
1346static void scarlett2_config_save_work(struct work_struct *work)
1347{
1348 struct scarlett2_data *private =
1349 container_of(work, struct scarlett2_data, work.work);
1350
1351 scarlett2_config_save(private->mixer);
1352}
1353
1354/* Send a USB message to set a SCARLETT2_CONFIG_* parameter */
1355static int scarlett2_usb_set_config(
1356 struct usb_mixer_interface *mixer,
1357 int config_item_num, int index, int value)
1358{
1359 struct scarlett2_data *private = mixer->private_data;
1360 const struct scarlett2_device_info *info = private->info;
1361 const struct scarlett2_config *config_item =
1362 &scarlett2_config_items[info->config_set][config_item_num];
1363 struct {
1364 __le32 offset;
1365 __le32 bytes;
1366 __le32 value;
1367 } __packed req;
1368 __le32 req2;
1369 int offset, size;
1370 int err;
1371
1372 /* Cancel any pending NVRAM save */
1373 cancel_delayed_work_sync(&private->work);
1374
1375 /* Convert config_item->size in bits to size in bytes and
1376 * calculate offset
1377 */
1378 if (config_item->size >= 8) {
1379 size = config_item->size / 8;
1380 offset = config_item->offset + index * size;
1381
1382 /* If updating a bit, retrieve the old value, set/clear the
1383 * bit as needed, and update value
1384 */
1385 } else {
1386 u8 tmp;
1387
1388 size = 1;
1389 offset = config_item->offset;
1390
1391 scarlett2_usb_get(mixer, offset, &tmp, 1);
1392 if (value)
1393 tmp |= (1 << index);
1394 else
1395 tmp &= ~(1 << index);
1396
1397 value = tmp;
1398 }
1399
1400 /* Send the configuration parameter data */
1401 req.offset = cpu_to_le32(offset);
1402 req.bytes = cpu_to_le32(size);
1403 req.value = cpu_to_le32(value);
1404 err = scarlett2_usb(mixer, SCARLETT2_USB_SET_DATA,
1405 &req, sizeof(u32) * 2 + size,
1406 NULL, 0);
1407 if (err < 0)
1408 return err;
1409
1410 /* Activate the change */
1411 req2 = cpu_to_le32(config_item->activate);
1412 err = scarlett2_usb(mixer, SCARLETT2_USB_DATA_CMD,
1413 &req2, sizeof(req2), NULL, 0);
1414 if (err < 0)
1415 return err;
1416
1417 /* Schedule the change to be written to NVRAM */
1418 if (config_item->activate != SCARLETT2_USB_CONFIG_SAVE)
1419 schedule_delayed_work(&private->work, msecs_to_jiffies(2000));
1420
1421 return 0;
1422}
1423
1424/* Send a USB message to get sync status; result placed in *sync */
1425static int scarlett2_usb_get_sync_status(
1426 struct usb_mixer_interface *mixer,
1427 u8 *sync)
1428{
1429 __le32 data;
1430 int err;
1431
1432 err = scarlett2_usb(mixer, SCARLETT2_USB_GET_SYNC,
1433 NULL, 0, &data, sizeof(data));
1434 if (err < 0)
1435 return err;
1436
1437 *sync = !!data;
1438 return 0;
1439}
1440
1441/* Send a USB message to get volume status; result placed in *buf */
1442static int scarlett2_usb_get_volume_status(
1443 struct usb_mixer_interface *mixer,
1444 struct scarlett2_usb_volume_status *buf)
1445{
1446 return scarlett2_usb_get(mixer, SCARLETT2_USB_VOLUME_STATUS_OFFSET,
1447 buf, sizeof(*buf));
1448}
1449
1450/* Send a USB message to get the volumes for all inputs of one mix
1451 * and put the values into private->mix[]
1452 */
1453static int scarlett2_usb_get_mix(struct usb_mixer_interface *mixer,
1454 int mix_num)
1455{
1456 struct scarlett2_data *private = mixer->private_data;
1457 const struct scarlett2_device_info *info = private->info;
1458
1459 int num_mixer_in =
1460 info->port_count[SCARLETT2_PORT_TYPE_MIX][SCARLETT2_PORT_OUT];
1461 int err, i, j, k;
1462
1463 struct {
1464 __le16 mix_num;
1465 __le16 count;
1466 } __packed req;
1467
1468 __le16 data[SCARLETT2_INPUT_MIX_MAX];
1469
1470 req.mix_num = cpu_to_le16(mix_num);
1471 req.count = cpu_to_le16(num_mixer_in);
1472
1473 err = scarlett2_usb(mixer, SCARLETT2_USB_GET_MIX,
1474 &req, sizeof(req),
1475 data, num_mixer_in * sizeof(u16));
1476 if (err < 0)
1477 return err;
1478
1479 for (i = 0, j = mix_num * num_mixer_in; i < num_mixer_in; i++, j++) {
1480 u16 mixer_value = le16_to_cpu(data[i]);
1481
1482 for (k = 0; k < SCARLETT2_MIXER_VALUE_COUNT; k++)
1483 if (scarlett2_mixer_values[k] >= mixer_value)
1484 break;
1485 if (k == SCARLETT2_MIXER_VALUE_COUNT)
1486 k = SCARLETT2_MIXER_MAX_VALUE;
1487 private->mix[j] = k;
1488 }
1489
1490 return 0;
1491}
1492
1493/* Send a USB message to set the volumes for all inputs of one mix
1494 * (values obtained from private->mix[])
1495 */
1496static int scarlett2_usb_set_mix(struct usb_mixer_interface *mixer,
1497 int mix_num)
1498{
1499 struct scarlett2_data *private = mixer->private_data;
1500 const struct scarlett2_device_info *info = private->info;
1501
1502 struct {
1503 __le16 mix_num;
1504 __le16 data[SCARLETT2_INPUT_MIX_MAX];
1505 } __packed req;
1506
1507 int i, j;
1508 int num_mixer_in =
1509 info->port_count[SCARLETT2_PORT_TYPE_MIX][SCARLETT2_PORT_OUT];
1510
1511 req.mix_num = cpu_to_le16(mix_num);
1512
1513 for (i = 0, j = mix_num * num_mixer_in; i < num_mixer_in; i++, j++)
1514 req.data[i] = cpu_to_le16(
1515 scarlett2_mixer_values[private->mix[j]]
1516 );
1517
1518 return scarlett2_usb(mixer, SCARLETT2_USB_SET_MIX,
1519 &req, (num_mixer_in + 1) * sizeof(u16),
1520 NULL, 0);
1521}
1522
1523/* Convert a port number index (per info->port_count) to a hardware ID */
1524static u32 scarlett2_mux_src_num_to_id(
1525 const int port_count[][SCARLETT2_PORT_DIRNS], int num)
1526{
1527 int port_type;
1528
1529 for (port_type = 0;
1530 port_type < SCARLETT2_PORT_TYPE_COUNT;
1531 port_type++) {
1532 if (num < port_count[port_type][SCARLETT2_PORT_IN])
1533 return scarlett2_ports[port_type].id | num;
1534 num -= port_count[port_type][SCARLETT2_PORT_IN];
1535 }
1536
1537 /* Oops */
1538 return 0;
1539}
1540
1541/* Convert a hardware ID to a port number index */
1542static u32 scarlett2_mux_id_to_num(
1543 const int port_count[][SCARLETT2_PORT_DIRNS], int direction, u32 id)
1544{
1545 int port_type;
1546 int port_num = 0;
1547
1548 for (port_type = 0;
1549 port_type < SCARLETT2_PORT_TYPE_COUNT;
1550 port_type++) {
1551 int base = scarlett2_ports[port_type].id;
1552 int count = port_count[port_type][direction];
1553
1554 if (id >= base && id < base + count)
1555 return port_num + id - base;
1556 port_num += count;
1557 }
1558
1559 /* Oops */
1560 return -1;
1561}
1562
1563/* Convert one mux entry from the interface and load into private->mux[] */
1564static void scarlett2_usb_populate_mux(struct scarlett2_data *private,
1565 u32 mux_entry)
1566{
1567 const struct scarlett2_device_info *info = private->info;
1568 const int (*port_count)[SCARLETT2_PORT_DIRNS] = info->port_count;
1569
1570 int dst_idx, src_idx;
1571
1572 dst_idx = scarlett2_mux_id_to_num(port_count, SCARLETT2_PORT_OUT,
1573 mux_entry & 0xFFF);
1574 if (dst_idx < 0)
1575 return;
1576
1577 if (dst_idx >= private->num_mux_dsts) {
1578 usb_audio_err(private->mixer->chip,
1579 "BUG: scarlett2_mux_id_to_num(%06x, OUT): %d >= %d",
1580 mux_entry, dst_idx, private->num_mux_dsts);
1581 return;
1582 }
1583
1584 src_idx = scarlett2_mux_id_to_num(port_count, SCARLETT2_PORT_IN,
1585 mux_entry >> 12);
1586 if (src_idx < 0)
1587 return;
1588
1589 if (src_idx >= private->num_mux_srcs) {
1590 usb_audio_err(private->mixer->chip,
1591 "BUG: scarlett2_mux_id_to_num(%06x, IN): %d >= %d",
1592 mux_entry, src_idx, private->num_mux_srcs);
1593 return;
1594 }
1595
1596 private->mux[dst_idx] = src_idx;
1597}
1598
1599/* Send USB message to get mux inputs and then populate private->mux[] */
1600static int scarlett2_usb_get_mux(struct usb_mixer_interface *mixer)
1601{
1602 struct scarlett2_data *private = mixer->private_data;
1603 int count = private->num_mux_dsts;
1604 int err, i;
1605
1606 struct {
1607 __le16 num;
1608 __le16 count;
1609 } __packed req;
1610
1611 __le32 data[SCARLETT2_MUX_MAX];
1612
1613 private->mux_updated = 0;
1614
1615 req.num = 0;
1616 req.count = cpu_to_le16(count);
1617
1618 err = scarlett2_usb(mixer, SCARLETT2_USB_GET_MUX,
1619 &req, sizeof(req),
1620 data, count * sizeof(u32));
1621 if (err < 0)
1622 return err;
1623
1624 for (i = 0; i < count; i++)
1625 scarlett2_usb_populate_mux(private, le32_to_cpu(data[i]));
1626
1627 return 0;
1628}
1629
1630/* Send USB messages to set mux inputs */
1631static int scarlett2_usb_set_mux(struct usb_mixer_interface *mixer)
1632{
1633 struct scarlett2_data *private = mixer->private_data;
1634 const struct scarlett2_device_info *info = private->info;
1635 const int (*port_count)[SCARLETT2_PORT_DIRNS] = info->port_count;
1636 int table;
1637
1638 struct {
1639 __le16 pad;
1640 __le16 num;
1641 __le32 data[SCARLETT2_MUX_MAX];
1642 } __packed req;
1643
1644 req.pad = 0;
1645
1646 /* set mux settings for each rate */
1647 for (table = 0; table < SCARLETT2_MUX_TABLES; table++) {
1648 const struct scarlett2_mux_entry *entry;
1649
1650 /* i counts over the output array */
1651 int i = 0, err;
1652
1653 req.num = cpu_to_le16(table);
1654
1655 /* loop through each entry */
1656 for (entry = info->mux_assignment[table];
1657 entry->count;
1658 entry++) {
1659 int j;
1660 int port_type = entry->port_type;
1661 int port_idx = entry->start;
1662 int mux_idx = scarlett2_get_port_start_num(port_count,
1663 SCARLETT2_PORT_OUT, port_type) + port_idx;
1664 int dst_id = scarlett2_ports[port_type].id + port_idx;
1665
1666 /* Empty slots */
1667 if (!dst_id) {
1668 for (j = 0; j < entry->count; j++)
1669 req.data[i++] = 0;
1670 continue;
1671 }
1672
1673 /* Non-empty mux slots use the lower 12 bits
1674 * for the destination and next 12 bits for
1675 * the source
1676 */
1677 for (j = 0; j < entry->count; j++) {
1678 int src_id = scarlett2_mux_src_num_to_id(
1679 port_count, private->mux[mux_idx++]);
1680 req.data[i++] = cpu_to_le32(dst_id |
1681 src_id << 12);
1682 dst_id++;
1683 }
1684 }
1685
1686 err = scarlett2_usb(mixer, SCARLETT2_USB_SET_MUX,
1687 &req, (i + 1) * sizeof(u32),
1688 NULL, 0);
1689 if (err < 0)
1690 return err;
1691 }
1692
1693 return 0;
1694}
1695
1696/* Send USB message to get meter levels */
1697static int scarlett2_usb_get_meter_levels(struct usb_mixer_interface *mixer,
1698 u16 num_meters, u16 *levels)
1699{
1700 struct {
1701 __le16 pad;
1702 __le16 num_meters;
1703 __le32 magic;
1704 } __packed req;
1705 u32 resp[SCARLETT2_MAX_METERS];
1706 int i, err;
1707
1708 req.pad = 0;
1709 req.num_meters = cpu_to_le16(num_meters);
1710 req.magic = cpu_to_le32(SCARLETT2_USB_METER_LEVELS_GET_MAGIC);
1711 err = scarlett2_usb(mixer, SCARLETT2_USB_GET_METER,
1712 &req, sizeof(req), resp, num_meters * sizeof(u32));
1713 if (err < 0)
1714 return err;
1715
1716 /* copy, convert to u16 */
1717 for (i = 0; i < num_meters; i++)
1718 levels[i] = resp[i];
1719
1720 return 0;
1721}
1722
1723/*** Control Functions ***/
1724
1725/* helper function to create a new control */
1726static int scarlett2_add_new_ctl(struct usb_mixer_interface *mixer,
1727 const struct snd_kcontrol_new *ncontrol,
1728 int index, int channels, const char *name,
1729 struct snd_kcontrol **kctl_return)
1730{
1731 struct snd_kcontrol *kctl;
1732 struct usb_mixer_elem_info *elem;
1733 int err;
1734
1735 elem = kzalloc(sizeof(*elem), GFP_KERNEL);
1736 if (!elem)
1737 return -ENOMEM;
1738
1739 /* We set USB_MIXER_BESPOKEN type, so that the core USB mixer code
1740 * ignores them for resume and other operations.
1741 * Also, the head.id field is set to 0, as we don't use this field.
1742 */
1743 elem->head.mixer = mixer;
1744 elem->control = index;
1745 elem->head.id = 0;
1746 elem->channels = channels;
1747 elem->val_type = USB_MIXER_BESPOKEN;
1748
1749 kctl = snd_ctl_new1(ncontrol, elem);
1750 if (!kctl) {
1751 kfree(elem);
1752 return -ENOMEM;
1753 }
1754 kctl->private_free = snd_usb_mixer_elem_free;
1755
1756 strscpy(kctl->id.name, name, sizeof(kctl->id.name));
1757
1758 err = snd_usb_mixer_add_control(&elem->head, kctl);
1759 if (err < 0)
1760 return err;
1761
1762 if (kctl_return)
1763 *kctl_return = kctl;
1764
1765 return 0;
1766}
1767
1768/*** Sync Control ***/
1769
1770/* Update sync control after receiving notification that the status
1771 * has changed
1772 */
1773static int scarlett2_update_sync(struct usb_mixer_interface *mixer)
1774{
1775 struct scarlett2_data *private = mixer->private_data;
1776
1777 private->sync_updated = 0;
1778 return scarlett2_usb_get_sync_status(mixer, &private->sync);
1779}
1780
1781static int scarlett2_sync_ctl_info(struct snd_kcontrol *kctl,
1782 struct snd_ctl_elem_info *uinfo)
1783{
1784 static const char *texts[2] = {
1785 "Unlocked", "Locked"
1786 };
1787 return snd_ctl_enum_info(uinfo, 1, 2, texts);
1788}
1789
1790static int scarlett2_sync_ctl_get(struct snd_kcontrol *kctl,
1791 struct snd_ctl_elem_value *ucontrol)
1792{
1793 struct usb_mixer_elem_info *elem = kctl->private_data;
1794 struct usb_mixer_interface *mixer = elem->head.mixer;
1795 struct scarlett2_data *private = mixer->private_data;
1796
1797 mutex_lock(&private->data_mutex);
1798 if (private->sync_updated)
1799 scarlett2_update_sync(mixer);
1800 ucontrol->value.enumerated.item[0] = private->sync;
1801 mutex_unlock(&private->data_mutex);
1802
1803 return 0;
1804}
1805
1806static const struct snd_kcontrol_new scarlett2_sync_ctl = {
1807 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1808 .access = SNDRV_CTL_ELEM_ACCESS_READ,
1809 .name = "",
1810 .info = scarlett2_sync_ctl_info,
1811 .get = scarlett2_sync_ctl_get
1812};
1813
1814static int scarlett2_add_sync_ctl(struct usb_mixer_interface *mixer)
1815{
1816 struct scarlett2_data *private = mixer->private_data;
1817
1818 /* devices without a mixer also don't support reporting sync status */
1819 if (private->info->config_set == SCARLETT2_CONFIG_SET_NO_MIXER)
1820 return 0;
1821
1822 return scarlett2_add_new_ctl(mixer, &scarlett2_sync_ctl,
1823 0, 1, "Sync Status", &private->sync_ctl);
1824}
1825
1826/*** Analogue Line Out Volume Controls ***/
1827
1828/* Update hardware volume controls after receiving notification that
1829 * they have changed
1830 */
1831static int scarlett2_update_volumes(struct usb_mixer_interface *mixer)
1832{
1833 struct scarlett2_data *private = mixer->private_data;
1834 const struct scarlett2_device_info *info = private->info;
1835 const int (*port_count)[SCARLETT2_PORT_DIRNS] = info->port_count;
1836 struct scarlett2_usb_volume_status volume_status;
1837 int num_line_out =
1838 port_count[SCARLETT2_PORT_TYPE_ANALOGUE][SCARLETT2_PORT_OUT];
1839 int err, i;
1840 int mute;
1841
1842 private->vol_updated = 0;
1843
1844 err = scarlett2_usb_get_volume_status(mixer, &volume_status);
1845 if (err < 0)
1846 return err;
1847
1848 private->master_vol = clamp(
1849 volume_status.master_vol + SCARLETT2_VOLUME_BIAS,
1850 0, SCARLETT2_VOLUME_BIAS);
1851
1852 if (info->line_out_hw_vol)
1853 for (i = 0; i < SCARLETT2_DIM_MUTE_COUNT; i++)
1854 private->dim_mute[i] = !!volume_status.dim_mute[i];
1855
1856 mute = private->dim_mute[SCARLETT2_BUTTON_MUTE];
1857
1858 for (i = 0; i < num_line_out; i++)
1859 if (private->vol_sw_hw_switch[i]) {
1860 private->vol[i] = private->master_vol;
1861 private->mute_switch[i] = mute;
1862 }
1863
1864 return 0;
1865}
1866
1867static int scarlett2_volume_ctl_info(struct snd_kcontrol *kctl,
1868 struct snd_ctl_elem_info *uinfo)
1869{
1870 struct usb_mixer_elem_info *elem = kctl->private_data;
1871
1872 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1873 uinfo->count = elem->channels;
1874 uinfo->value.integer.min = 0;
1875 uinfo->value.integer.max = SCARLETT2_VOLUME_BIAS;
1876 uinfo->value.integer.step = 1;
1877 return 0;
1878}
1879
1880static int scarlett2_master_volume_ctl_get(struct snd_kcontrol *kctl,
1881 struct snd_ctl_elem_value *ucontrol)
1882{
1883 struct usb_mixer_elem_info *elem = kctl->private_data;
1884 struct usb_mixer_interface *mixer = elem->head.mixer;
1885 struct scarlett2_data *private = mixer->private_data;
1886
1887 mutex_lock(&private->data_mutex);
1888 if (private->vol_updated)
1889 scarlett2_update_volumes(mixer);
1890 mutex_unlock(&private->data_mutex);
1891
1892 ucontrol->value.integer.value[0] = private->master_vol;
1893 return 0;
1894}
1895
1896static int line_out_remap(struct scarlett2_data *private, int index)
1897{
1898 const struct scarlett2_device_info *info = private->info;
1899
1900 if (!info->line_out_remap_enable)
1901 return index;
1902 return info->line_out_remap[index];
1903}
1904
1905static int scarlett2_volume_ctl_get(struct snd_kcontrol *kctl,
1906 struct snd_ctl_elem_value *ucontrol)
1907{
1908 struct usb_mixer_elem_info *elem = kctl->private_data;
1909 struct usb_mixer_interface *mixer = elem->head.mixer;
1910 struct scarlett2_data *private = mixer->private_data;
1911 int index = line_out_remap(private, elem->control);
1912
1913 mutex_lock(&private->data_mutex);
1914 if (private->vol_updated)
1915 scarlett2_update_volumes(mixer);
1916 mutex_unlock(&private->data_mutex);
1917
1918 ucontrol->value.integer.value[0] = private->vol[index];
1919 return 0;
1920}
1921
1922static int scarlett2_volume_ctl_put(struct snd_kcontrol *kctl,
1923 struct snd_ctl_elem_value *ucontrol)
1924{
1925 struct usb_mixer_elem_info *elem = kctl->private_data;
1926 struct usb_mixer_interface *mixer = elem->head.mixer;
1927 struct scarlett2_data *private = mixer->private_data;
1928 int index = line_out_remap(private, elem->control);
1929 int oval, val, err = 0;
1930
1931 mutex_lock(&private->data_mutex);
1932
1933 oval = private->vol[index];
1934 val = ucontrol->value.integer.value[0];
1935
1936 if (oval == val)
1937 goto unlock;
1938
1939 private->vol[index] = val;
1940 err = scarlett2_usb_set_config(mixer, SCARLETT2_CONFIG_LINE_OUT_VOLUME,
1941 index, val - SCARLETT2_VOLUME_BIAS);
1942 if (err == 0)
1943 err = 1;
1944
1945unlock:
1946 mutex_unlock(&private->data_mutex);
1947 return err;
1948}
1949
1950static const DECLARE_TLV_DB_MINMAX(
1951 db_scale_scarlett2_gain, -SCARLETT2_VOLUME_BIAS * 100, 0
1952);
1953
1954static const struct snd_kcontrol_new scarlett2_master_volume_ctl = {
1955 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1956 .access = SNDRV_CTL_ELEM_ACCESS_READ |
1957 SNDRV_CTL_ELEM_ACCESS_TLV_READ,
1958 .name = "",
1959 .info = scarlett2_volume_ctl_info,
1960 .get = scarlett2_master_volume_ctl_get,
1961 .private_value = 0, /* max value */
1962 .tlv = { .p = db_scale_scarlett2_gain }
1963};
1964
1965static const struct snd_kcontrol_new scarlett2_line_out_volume_ctl = {
1966 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1967 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE |
1968 SNDRV_CTL_ELEM_ACCESS_TLV_READ,
1969 .name = "",
1970 .info = scarlett2_volume_ctl_info,
1971 .get = scarlett2_volume_ctl_get,
1972 .put = scarlett2_volume_ctl_put,
1973 .private_value = 0, /* max value */
1974 .tlv = { .p = db_scale_scarlett2_gain }
1975};
1976
1977/*** Mute Switch Controls ***/
1978
1979static int scarlett2_mute_ctl_get(struct snd_kcontrol *kctl,
1980 struct snd_ctl_elem_value *ucontrol)
1981{
1982 struct usb_mixer_elem_info *elem = kctl->private_data;
1983 struct usb_mixer_interface *mixer = elem->head.mixer;
1984 struct scarlett2_data *private = mixer->private_data;
1985 int index = line_out_remap(private, elem->control);
1986
1987 mutex_lock(&private->data_mutex);
1988 if (private->vol_updated)
1989 scarlett2_update_volumes(mixer);
1990 mutex_unlock(&private->data_mutex);
1991
1992 ucontrol->value.integer.value[0] = private->mute_switch[index];
1993 return 0;
1994}
1995
1996static int scarlett2_mute_ctl_put(struct snd_kcontrol *kctl,
1997 struct snd_ctl_elem_value *ucontrol)
1998{
1999 struct usb_mixer_elem_info *elem = kctl->private_data;
2000 struct usb_mixer_interface *mixer = elem->head.mixer;
2001 struct scarlett2_data *private = mixer->private_data;
2002 int index = line_out_remap(private, elem->control);
2003 int oval, val, err = 0;
2004
2005 mutex_lock(&private->data_mutex);
2006
2007 oval = private->mute_switch[index];
2008 val = !!ucontrol->value.integer.value[0];
2009
2010 if (oval == val)
2011 goto unlock;
2012
2013 private->mute_switch[index] = val;
2014
2015 /* Send mute change to the device */
2016 err = scarlett2_usb_set_config(mixer, SCARLETT2_CONFIG_MUTE_SWITCH,
2017 index, val);
2018 if (err == 0)
2019 err = 1;
2020
2021unlock:
2022 mutex_unlock(&private->data_mutex);
2023 return err;
2024}
2025
2026static const struct snd_kcontrol_new scarlett2_mute_ctl = {
2027 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2028 .name = "",
2029 .info = snd_ctl_boolean_mono_info,
2030 .get = scarlett2_mute_ctl_get,
2031 .put = scarlett2_mute_ctl_put,
2032};
2033
2034/*** HW/SW Volume Switch Controls ***/
2035
2036static void scarlett2_sw_hw_ctl_ro(struct scarlett2_data *private, int index)
2037{
2038 private->sw_hw_ctls[index]->vd[0].access &=
2039 ~SNDRV_CTL_ELEM_ACCESS_WRITE;
2040}
2041
2042static void scarlett2_sw_hw_ctl_rw(struct scarlett2_data *private, int index)
2043{
2044 private->sw_hw_ctls[index]->vd[0].access |=
2045 SNDRV_CTL_ELEM_ACCESS_WRITE;
2046}
2047
2048static int scarlett2_sw_hw_enum_ctl_info(struct snd_kcontrol *kctl,
2049 struct snd_ctl_elem_info *uinfo)
2050{
2051 static const char *const values[2] = {
2052 "SW", "HW"
2053 };
2054
2055 return snd_ctl_enum_info(uinfo, 1, 2, values);
2056}
2057
2058static int scarlett2_sw_hw_enum_ctl_get(struct snd_kcontrol *kctl,
2059 struct snd_ctl_elem_value *ucontrol)
2060{
2061 struct usb_mixer_elem_info *elem = kctl->private_data;
2062 struct scarlett2_data *private = elem->head.mixer->private_data;
2063 int index = line_out_remap(private, elem->control);
2064
2065 ucontrol->value.enumerated.item[0] = private->vol_sw_hw_switch[index];
2066 return 0;
2067}
2068
2069static void scarlett2_vol_ctl_set_writable(struct usb_mixer_interface *mixer,
2070 int index, int value)
2071{
2072 struct scarlett2_data *private = mixer->private_data;
2073 struct snd_card *card = mixer->chip->card;
2074
2075 /* Set/Clear write bits */
2076 if (value) {
2077 private->vol_ctls[index]->vd[0].access |=
2078 SNDRV_CTL_ELEM_ACCESS_WRITE;
2079 private->mute_ctls[index]->vd[0].access |=
2080 SNDRV_CTL_ELEM_ACCESS_WRITE;
2081 } else {
2082 private->vol_ctls[index]->vd[0].access &=
2083 ~SNDRV_CTL_ELEM_ACCESS_WRITE;
2084 private->mute_ctls[index]->vd[0].access &=
2085 ~SNDRV_CTL_ELEM_ACCESS_WRITE;
2086 }
2087
2088 /* Notify of write bit and possible value change */
2089 snd_ctl_notify(card,
2090 SNDRV_CTL_EVENT_MASK_VALUE | SNDRV_CTL_EVENT_MASK_INFO,
2091 &private->vol_ctls[index]->id);
2092 snd_ctl_notify(card,
2093 SNDRV_CTL_EVENT_MASK_VALUE | SNDRV_CTL_EVENT_MASK_INFO,
2094 &private->mute_ctls[index]->id);
2095}
2096
2097static int scarlett2_sw_hw_change(struct usb_mixer_interface *mixer,
2098 int ctl_index, int val)
2099{
2100 struct scarlett2_data *private = mixer->private_data;
2101 int index = line_out_remap(private, ctl_index);
2102 int err;
2103
2104 private->vol_sw_hw_switch[index] = val;
2105
2106 /* Change access mode to RO (hardware controlled volume)
2107 * or RW (software controlled volume)
2108 */
2109 scarlett2_vol_ctl_set_writable(mixer, ctl_index, !val);
2110
2111 /* Reset volume/mute to master volume/mute */
2112 private->vol[index] = private->master_vol;
2113 private->mute_switch[index] = private->dim_mute[SCARLETT2_BUTTON_MUTE];
2114
2115 /* Set SW volume to current HW volume */
2116 err = scarlett2_usb_set_config(
2117 mixer, SCARLETT2_CONFIG_LINE_OUT_VOLUME,
2118 index, private->master_vol - SCARLETT2_VOLUME_BIAS);
2119 if (err < 0)
2120 return err;
2121
2122 /* Set SW mute to current HW mute */
2123 err = scarlett2_usb_set_config(
2124 mixer, SCARLETT2_CONFIG_MUTE_SWITCH,
2125 index, private->dim_mute[SCARLETT2_BUTTON_MUTE]);
2126 if (err < 0)
2127 return err;
2128
2129 /* Send SW/HW switch change to the device */
2130 return scarlett2_usb_set_config(mixer, SCARLETT2_CONFIG_SW_HW_SWITCH,
2131 index, val);
2132}
2133
2134static int scarlett2_sw_hw_enum_ctl_put(struct snd_kcontrol *kctl,
2135 struct snd_ctl_elem_value *ucontrol)
2136{
2137 struct usb_mixer_elem_info *elem = kctl->private_data;
2138 struct usb_mixer_interface *mixer = elem->head.mixer;
2139 struct scarlett2_data *private = mixer->private_data;
2140 int ctl_index = elem->control;
2141 int index = line_out_remap(private, ctl_index);
2142 int oval, val, err = 0;
2143
2144 mutex_lock(&private->data_mutex);
2145
2146 oval = private->vol_sw_hw_switch[index];
2147 val = !!ucontrol->value.enumerated.item[0];
2148
2149 if (oval == val)
2150 goto unlock;
2151
2152 err = scarlett2_sw_hw_change(mixer, ctl_index, val);
2153 if (err == 0)
2154 err = 1;
2155
2156unlock:
2157 mutex_unlock(&private->data_mutex);
2158 return err;
2159}
2160
2161static const struct snd_kcontrol_new scarlett2_sw_hw_enum_ctl = {
2162 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2163 .name = "",
2164 .info = scarlett2_sw_hw_enum_ctl_info,
2165 .get = scarlett2_sw_hw_enum_ctl_get,
2166 .put = scarlett2_sw_hw_enum_ctl_put,
2167};
2168
2169/*** Line Level/Instrument Level Switch Controls ***/
2170
2171static int scarlett2_update_input_other(struct usb_mixer_interface *mixer)
2172{
2173 struct scarlett2_data *private = mixer->private_data;
2174 const struct scarlett2_device_info *info = private->info;
2175
2176 private->input_other_updated = 0;
2177
2178 if (info->level_input_count) {
2179 int err = scarlett2_usb_get_config(
2180 mixer, SCARLETT2_CONFIG_LEVEL_SWITCH,
2181 info->level_input_count + info->level_input_first,
2182 private->level_switch);
2183 if (err < 0)
2184 return err;
2185 }
2186
2187 if (info->pad_input_count) {
2188 int err = scarlett2_usb_get_config(
2189 mixer, SCARLETT2_CONFIG_PAD_SWITCH,
2190 info->pad_input_count, private->pad_switch);
2191 if (err < 0)
2192 return err;
2193 }
2194
2195 if (info->air_input_count) {
2196 int err = scarlett2_usb_get_config(
2197 mixer, SCARLETT2_CONFIG_AIR_SWITCH,
2198 info->air_input_count, private->air_switch);
2199 if (err < 0)
2200 return err;
2201 }
2202
2203 if (info->phantom_count) {
2204 int err = scarlett2_usb_get_config(
2205 mixer, SCARLETT2_CONFIG_PHANTOM_SWITCH,
2206 info->phantom_count, private->phantom_switch);
2207 if (err < 0)
2208 return err;
2209
2210 err = scarlett2_usb_get_config(
2211 mixer, SCARLETT2_CONFIG_PHANTOM_PERSISTENCE,
2212 1, &private->phantom_persistence);
2213 if (err < 0)
2214 return err;
2215 }
2216
2217 return 0;
2218}
2219
2220static int scarlett2_level_enum_ctl_info(struct snd_kcontrol *kctl,
2221 struct snd_ctl_elem_info *uinfo)
2222{
2223 static const char *const values[2] = {
2224 "Line", "Inst"
2225 };
2226
2227 return snd_ctl_enum_info(uinfo, 1, 2, values);
2228}
2229
2230static int scarlett2_level_enum_ctl_get(struct snd_kcontrol *kctl,
2231 struct snd_ctl_elem_value *ucontrol)
2232{
2233 struct usb_mixer_elem_info *elem = kctl->private_data;
2234 struct usb_mixer_interface *mixer = elem->head.mixer;
2235 struct scarlett2_data *private = mixer->private_data;
2236 const struct scarlett2_device_info *info = private->info;
2237
2238 int index = elem->control + info->level_input_first;
2239
2240 mutex_lock(&private->data_mutex);
2241 if (private->input_other_updated)
2242 scarlett2_update_input_other(mixer);
2243 ucontrol->value.enumerated.item[0] = private->level_switch[index];
2244 mutex_unlock(&private->data_mutex);
2245
2246 return 0;
2247}
2248
2249static int scarlett2_level_enum_ctl_put(struct snd_kcontrol *kctl,
2250 struct snd_ctl_elem_value *ucontrol)
2251{
2252 struct usb_mixer_elem_info *elem = kctl->private_data;
2253 struct usb_mixer_interface *mixer = elem->head.mixer;
2254 struct scarlett2_data *private = mixer->private_data;
2255 const struct scarlett2_device_info *info = private->info;
2256
2257 int index = elem->control + info->level_input_first;
2258 int oval, val, err = 0;
2259
2260 mutex_lock(&private->data_mutex);
2261
2262 oval = private->level_switch[index];
2263 val = !!ucontrol->value.enumerated.item[0];
2264
2265 if (oval == val)
2266 goto unlock;
2267
2268 private->level_switch[index] = val;
2269
2270 /* Send switch change to the device */
2271 err = scarlett2_usb_set_config(mixer, SCARLETT2_CONFIG_LEVEL_SWITCH,
2272 index, val);
2273 if (err == 0)
2274 err = 1;
2275
2276unlock:
2277 mutex_unlock(&private->data_mutex);
2278 return err;
2279}
2280
2281static const struct snd_kcontrol_new scarlett2_level_enum_ctl = {
2282 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2283 .name = "",
2284 .info = scarlett2_level_enum_ctl_info,
2285 .get = scarlett2_level_enum_ctl_get,
2286 .put = scarlett2_level_enum_ctl_put,
2287};
2288
2289/*** Pad Switch Controls ***/
2290
2291static int scarlett2_pad_ctl_get(struct snd_kcontrol *kctl,
2292 struct snd_ctl_elem_value *ucontrol)
2293{
2294 struct usb_mixer_elem_info *elem = kctl->private_data;
2295 struct usb_mixer_interface *mixer = elem->head.mixer;
2296 struct scarlett2_data *private = mixer->private_data;
2297
2298 mutex_lock(&private->data_mutex);
2299 if (private->input_other_updated)
2300 scarlett2_update_input_other(mixer);
2301 ucontrol->value.integer.value[0] =
2302 private->pad_switch[elem->control];
2303 mutex_unlock(&private->data_mutex);
2304
2305 return 0;
2306}
2307
2308static int scarlett2_pad_ctl_put(struct snd_kcontrol *kctl,
2309 struct snd_ctl_elem_value *ucontrol)
2310{
2311 struct usb_mixer_elem_info *elem = kctl->private_data;
2312 struct usb_mixer_interface *mixer = elem->head.mixer;
2313 struct scarlett2_data *private = mixer->private_data;
2314
2315 int index = elem->control;
2316 int oval, val, err = 0;
2317
2318 mutex_lock(&private->data_mutex);
2319
2320 oval = private->pad_switch[index];
2321 val = !!ucontrol->value.integer.value[0];
2322
2323 if (oval == val)
2324 goto unlock;
2325
2326 private->pad_switch[index] = val;
2327
2328 /* Send switch change to the device */
2329 err = scarlett2_usb_set_config(mixer, SCARLETT2_CONFIG_PAD_SWITCH,
2330 index, val);
2331 if (err == 0)
2332 err = 1;
2333
2334unlock:
2335 mutex_unlock(&private->data_mutex);
2336 return err;
2337}
2338
2339static const struct snd_kcontrol_new scarlett2_pad_ctl = {
2340 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2341 .name = "",
2342 .info = snd_ctl_boolean_mono_info,
2343 .get = scarlett2_pad_ctl_get,
2344 .put = scarlett2_pad_ctl_put,
2345};
2346
2347/*** Air Switch Controls ***/
2348
2349static int scarlett2_air_ctl_get(struct snd_kcontrol *kctl,
2350 struct snd_ctl_elem_value *ucontrol)
2351{
2352 struct usb_mixer_elem_info *elem = kctl->private_data;
2353 struct usb_mixer_interface *mixer = elem->head.mixer;
2354 struct scarlett2_data *private = mixer->private_data;
2355
2356 mutex_lock(&private->data_mutex);
2357 if (private->input_other_updated)
2358 scarlett2_update_input_other(mixer);
2359 ucontrol->value.integer.value[0] = private->air_switch[elem->control];
2360 mutex_unlock(&private->data_mutex);
2361
2362 return 0;
2363}
2364
2365static int scarlett2_air_ctl_put(struct snd_kcontrol *kctl,
2366 struct snd_ctl_elem_value *ucontrol)
2367{
2368 struct usb_mixer_elem_info *elem = kctl->private_data;
2369 struct usb_mixer_interface *mixer = elem->head.mixer;
2370 struct scarlett2_data *private = mixer->private_data;
2371
2372 int index = elem->control;
2373 int oval, val, err = 0;
2374
2375 mutex_lock(&private->data_mutex);
2376
2377 oval = private->air_switch[index];
2378 val = !!ucontrol->value.integer.value[0];
2379
2380 if (oval == val)
2381 goto unlock;
2382
2383 private->air_switch[index] = val;
2384
2385 /* Send switch change to the device */
2386 err = scarlett2_usb_set_config(mixer, SCARLETT2_CONFIG_AIR_SWITCH,
2387 index, val);
2388 if (err == 0)
2389 err = 1;
2390
2391unlock:
2392 mutex_unlock(&private->data_mutex);
2393 return err;
2394}
2395
2396static const struct snd_kcontrol_new scarlett2_air_ctl = {
2397 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2398 .name = "",
2399 .info = snd_ctl_boolean_mono_info,
2400 .get = scarlett2_air_ctl_get,
2401 .put = scarlett2_air_ctl_put,
2402};
2403
2404/*** Phantom Switch Controls ***/
2405
2406static int scarlett2_phantom_ctl_get(struct snd_kcontrol *kctl,
2407 struct snd_ctl_elem_value *ucontrol)
2408{
2409 struct usb_mixer_elem_info *elem = kctl->private_data;
2410 struct usb_mixer_interface *mixer = elem->head.mixer;
2411 struct scarlett2_data *private = mixer->private_data;
2412
2413 mutex_lock(&private->data_mutex);
2414 if (private->input_other_updated)
2415 scarlett2_update_input_other(mixer);
2416 ucontrol->value.integer.value[0] =
2417 private->phantom_switch[elem->control];
2418 mutex_unlock(&private->data_mutex);
2419
2420 return 0;
2421}
2422
2423static int scarlett2_phantom_ctl_put(struct snd_kcontrol *kctl,
2424 struct snd_ctl_elem_value *ucontrol)
2425{
2426 struct usb_mixer_elem_info *elem = kctl->private_data;
2427 struct usb_mixer_interface *mixer = elem->head.mixer;
2428 struct scarlett2_data *private = mixer->private_data;
2429
2430 int index = elem->control;
2431 int oval, val, err = 0;
2432
2433 mutex_lock(&private->data_mutex);
2434
2435 oval = private->phantom_switch[index];
2436 val = !!ucontrol->value.integer.value[0];
2437
2438 if (oval == val)
2439 goto unlock;
2440
2441 private->phantom_switch[index] = val;
2442
2443 /* Send switch change to the device */
2444 err = scarlett2_usb_set_config(mixer, SCARLETT2_CONFIG_PHANTOM_SWITCH,
2445 index, val);
2446 if (err == 0)
2447 err = 1;
2448
2449unlock:
2450 mutex_unlock(&private->data_mutex);
2451 return err;
2452}
2453
2454static const struct snd_kcontrol_new scarlett2_phantom_ctl = {
2455 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2456 .name = "",
2457 .info = snd_ctl_boolean_mono_info,
2458 .get = scarlett2_phantom_ctl_get,
2459 .put = scarlett2_phantom_ctl_put,
2460};
2461
2462/*** Phantom Persistence Control ***/
2463
2464static int scarlett2_phantom_persistence_ctl_get(
2465 struct snd_kcontrol *kctl, struct snd_ctl_elem_value *ucontrol)
2466{
2467 struct usb_mixer_elem_info *elem = kctl->private_data;
2468 struct scarlett2_data *private = elem->head.mixer->private_data;
2469
2470 ucontrol->value.integer.value[0] = private->phantom_persistence;
2471 return 0;
2472}
2473
2474static int scarlett2_phantom_persistence_ctl_put(
2475 struct snd_kcontrol *kctl, struct snd_ctl_elem_value *ucontrol)
2476{
2477 struct usb_mixer_elem_info *elem = kctl->private_data;
2478 struct usb_mixer_interface *mixer = elem->head.mixer;
2479 struct scarlett2_data *private = mixer->private_data;
2480
2481 int index = elem->control;
2482 int oval, val, err = 0;
2483
2484 mutex_lock(&private->data_mutex);
2485
2486 oval = private->phantom_persistence;
2487 val = !!ucontrol->value.integer.value[0];
2488
2489 if (oval == val)
2490 goto unlock;
2491
2492 private->phantom_persistence = val;
2493
2494 /* Send switch change to the device */
2495 err = scarlett2_usb_set_config(
2496 mixer, SCARLETT2_CONFIG_PHANTOM_PERSISTENCE, index, val);
2497 if (err == 0)
2498 err = 1;
2499
2500unlock:
2501 mutex_unlock(&private->data_mutex);
2502 return err;
2503}
2504
2505static const struct snd_kcontrol_new scarlett2_phantom_persistence_ctl = {
2506 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2507 .name = "",
2508 .info = snd_ctl_boolean_mono_info,
2509 .get = scarlett2_phantom_persistence_ctl_get,
2510 .put = scarlett2_phantom_persistence_ctl_put,
2511};
2512
2513/*** Direct Monitor Control ***/
2514
2515static int scarlett2_update_monitor_other(struct usb_mixer_interface *mixer)
2516{
2517 struct scarlett2_data *private = mixer->private_data;
2518 const struct scarlett2_device_info *info = private->info;
2519 int err;
2520
2521 /* monitor_other_enable[0] enables speaker switching
2522 * monitor_other_enable[1] enables talkback
2523 */
2524 u8 monitor_other_enable[2];
2525
2526 /* monitor_other_switch[0] activates the alternate speakers
2527 * monitor_other_switch[1] activates talkback
2528 */
2529 u8 monitor_other_switch[2];
2530
2531 private->monitor_other_updated = 0;
2532
2533 if (info->direct_monitor)
2534 return scarlett2_usb_get_config(
2535 mixer, SCARLETT2_CONFIG_DIRECT_MONITOR,
2536 1, &private->direct_monitor_switch);
2537
2538 /* if it doesn't do speaker switching then it also doesn't do
2539 * talkback
2540 */
2541 if (!info->has_speaker_switching)
2542 return 0;
2543
2544 err = scarlett2_usb_get_config(
2545 mixer, SCARLETT2_CONFIG_MONITOR_OTHER_ENABLE,
2546 2, monitor_other_enable);
2547 if (err < 0)
2548 return err;
2549
2550 err = scarlett2_usb_get_config(
2551 mixer, SCARLETT2_CONFIG_MONITOR_OTHER_SWITCH,
2552 2, monitor_other_switch);
2553 if (err < 0)
2554 return err;
2555
2556 if (!monitor_other_enable[0])
2557 private->speaker_switching_switch = 0;
2558 else
2559 private->speaker_switching_switch = monitor_other_switch[0] + 1;
2560
2561 if (info->has_talkback) {
2562 const int (*port_count)[SCARLETT2_PORT_DIRNS] =
2563 info->port_count;
2564 int num_mixes =
2565 port_count[SCARLETT2_PORT_TYPE_MIX][SCARLETT2_PORT_IN];
2566 u16 bitmap;
2567 int i;
2568
2569 if (!monitor_other_enable[1])
2570 private->talkback_switch = 0;
2571 else
2572 private->talkback_switch = monitor_other_switch[1] + 1;
2573
2574 err = scarlett2_usb_get_config(mixer,
2575 SCARLETT2_CONFIG_TALKBACK_MAP,
2576 1, &bitmap);
2577 if (err < 0)
2578 return err;
2579 for (i = 0; i < num_mixes; i++, bitmap >>= 1)
2580 private->talkback_map[i] = bitmap & 1;
2581 }
2582
2583 return 0;
2584}
2585
2586static int scarlett2_direct_monitor_ctl_get(
2587 struct snd_kcontrol *kctl, struct snd_ctl_elem_value *ucontrol)
2588{
2589 struct usb_mixer_elem_info *elem = kctl->private_data;
2590 struct usb_mixer_interface *mixer = elem->head.mixer;
2591 struct scarlett2_data *private = elem->head.mixer->private_data;
2592
2593 mutex_lock(&private->data_mutex);
2594 if (private->monitor_other_updated)
2595 scarlett2_update_monitor_other(mixer);
2596 ucontrol->value.enumerated.item[0] = private->direct_monitor_switch;
2597 mutex_unlock(&private->data_mutex);
2598
2599 return 0;
2600}
2601
2602static int scarlett2_direct_monitor_ctl_put(
2603 struct snd_kcontrol *kctl, struct snd_ctl_elem_value *ucontrol)
2604{
2605 struct usb_mixer_elem_info *elem = kctl->private_data;
2606 struct usb_mixer_interface *mixer = elem->head.mixer;
2607 struct scarlett2_data *private = mixer->private_data;
2608
2609 int index = elem->control;
2610 int oval, val, err = 0;
2611
2612 mutex_lock(&private->data_mutex);
2613
2614 oval = private->direct_monitor_switch;
2615 val = min(ucontrol->value.enumerated.item[0], 2U);
2616
2617 if (oval == val)
2618 goto unlock;
2619
2620 private->direct_monitor_switch = val;
2621
2622 /* Send switch change to the device */
2623 err = scarlett2_usb_set_config(
2624 mixer, SCARLETT2_CONFIG_DIRECT_MONITOR, index, val);
2625 if (err == 0)
2626 err = 1;
2627
2628unlock:
2629 mutex_unlock(&private->data_mutex);
2630 return err;
2631}
2632
2633static int scarlett2_direct_monitor_stereo_enum_ctl_info(
2634 struct snd_kcontrol *kctl, struct snd_ctl_elem_info *uinfo)
2635{
2636 static const char *const values[3] = {
2637 "Off", "Mono", "Stereo"
2638 };
2639
2640 return snd_ctl_enum_info(uinfo, 1, 3, values);
2641}
2642
2643/* Direct Monitor for Solo is mono-only and only needs a boolean control
2644 * Direct Monitor for 2i2 is selectable between Off/Mono/Stereo
2645 */
2646static const struct snd_kcontrol_new scarlett2_direct_monitor_ctl[2] = {
2647 {
2648 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2649 .name = "",
2650 .info = snd_ctl_boolean_mono_info,
2651 .get = scarlett2_direct_monitor_ctl_get,
2652 .put = scarlett2_direct_monitor_ctl_put,
2653 },
2654 {
2655 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2656 .name = "",
2657 .info = scarlett2_direct_monitor_stereo_enum_ctl_info,
2658 .get = scarlett2_direct_monitor_ctl_get,
2659 .put = scarlett2_direct_monitor_ctl_put,
2660 }
2661};
2662
2663static int scarlett2_add_direct_monitor_ctl(struct usb_mixer_interface *mixer)
2664{
2665 struct scarlett2_data *private = mixer->private_data;
2666 const struct scarlett2_device_info *info = private->info;
2667 const char *s;
2668
2669 if (!info->direct_monitor)
2670 return 0;
2671
2672 s = info->direct_monitor == 1
2673 ? "Direct Monitor Playback Switch"
2674 : "Direct Monitor Playback Enum";
2675
2676 return scarlett2_add_new_ctl(
2677 mixer, &scarlett2_direct_monitor_ctl[info->direct_monitor - 1],
2678 0, 1, s, &private->direct_monitor_ctl);
2679}
2680
2681/*** Speaker Switching Control ***/
2682
2683static int scarlett2_speaker_switch_enum_ctl_info(
2684 struct snd_kcontrol *kctl, struct snd_ctl_elem_info *uinfo)
2685{
2686 static const char *const values[3] = {
2687 "Off", "Main", "Alt"
2688 };
2689
2690 return snd_ctl_enum_info(uinfo, 1, 3, values);
2691}
2692
2693static int scarlett2_speaker_switch_enum_ctl_get(
2694 struct snd_kcontrol *kctl, struct snd_ctl_elem_value *ucontrol)
2695{
2696 struct usb_mixer_elem_info *elem = kctl->private_data;
2697 struct usb_mixer_interface *mixer = elem->head.mixer;
2698 struct scarlett2_data *private = mixer->private_data;
2699
2700 mutex_lock(&private->data_mutex);
2701 if (private->monitor_other_updated)
2702 scarlett2_update_monitor_other(mixer);
2703 ucontrol->value.enumerated.item[0] = private->speaker_switching_switch;
2704 mutex_unlock(&private->data_mutex);
2705
2706 return 0;
2707}
2708
2709/* when speaker switching gets enabled, switch the main/alt speakers
2710 * to HW volume and disable those controls
2711 */
2712static int scarlett2_speaker_switch_enable(struct usb_mixer_interface *mixer)
2713{
2714 struct snd_card *card = mixer->chip->card;
2715 struct scarlett2_data *private = mixer->private_data;
2716 int i, err;
2717
2718 for (i = 0; i < 4; i++) {
2719 int index = line_out_remap(private, i);
2720
2721 /* switch the main/alt speakers to HW volume */
2722 if (!private->vol_sw_hw_switch[index]) {
2723 err = scarlett2_sw_hw_change(private->mixer, i, 1);
2724 if (err < 0)
2725 return err;
2726 }
2727
2728 /* disable the line out SW/HW switch */
2729 scarlett2_sw_hw_ctl_ro(private, i);
2730 snd_ctl_notify(card,
2731 SNDRV_CTL_EVENT_MASK_VALUE |
2732 SNDRV_CTL_EVENT_MASK_INFO,
2733 &private->sw_hw_ctls[i]->id);
2734 }
2735
2736 /* when the next monitor-other notify comes in, update the mux
2737 * configuration
2738 */
2739 private->speaker_switching_switched = 1;
2740
2741 return 0;
2742}
2743
2744/* when speaker switching gets disabled, reenable the hw/sw controls
2745 * and invalidate the routing
2746 */
2747static void scarlett2_speaker_switch_disable(struct usb_mixer_interface *mixer)
2748{
2749 struct snd_card *card = mixer->chip->card;
2750 struct scarlett2_data *private = mixer->private_data;
2751 int i;
2752
2753 /* enable the line out SW/HW switch */
2754 for (i = 0; i < 4; i++) {
2755 scarlett2_sw_hw_ctl_rw(private, i);
2756 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_INFO,
2757 &private->sw_hw_ctls[i]->id);
2758 }
2759
2760 /* when the next monitor-other notify comes in, update the mux
2761 * configuration
2762 */
2763 private->speaker_switching_switched = 1;
2764}
2765
2766static int scarlett2_speaker_switch_enum_ctl_put(
2767 struct snd_kcontrol *kctl, struct snd_ctl_elem_value *ucontrol)
2768{
2769 struct usb_mixer_elem_info *elem = kctl->private_data;
2770 struct usb_mixer_interface *mixer = elem->head.mixer;
2771 struct scarlett2_data *private = mixer->private_data;
2772
2773 int oval, val, err = 0;
2774
2775 mutex_lock(&private->data_mutex);
2776
2777 oval = private->speaker_switching_switch;
2778 val = min(ucontrol->value.enumerated.item[0], 2U);
2779
2780 if (oval == val)
2781 goto unlock;
2782
2783 private->speaker_switching_switch = val;
2784
2785 /* enable/disable speaker switching */
2786 err = scarlett2_usb_set_config(
2787 mixer, SCARLETT2_CONFIG_MONITOR_OTHER_ENABLE,
2788 0, !!val);
2789 if (err < 0)
2790 goto unlock;
2791
2792 /* if speaker switching is enabled, select main or alt */
2793 err = scarlett2_usb_set_config(
2794 mixer, SCARLETT2_CONFIG_MONITOR_OTHER_SWITCH,
2795 0, val == 2);
2796 if (err < 0)
2797 goto unlock;
2798
2799 /* update controls if speaker switching gets enabled or disabled */
2800 if (!oval && val)
2801 err = scarlett2_speaker_switch_enable(mixer);
2802 else if (oval && !val)
2803 scarlett2_speaker_switch_disable(mixer);
2804
2805 if (err == 0)
2806 err = 1;
2807
2808unlock:
2809 mutex_unlock(&private->data_mutex);
2810 return err;
2811}
2812
2813static const struct snd_kcontrol_new scarlett2_speaker_switch_enum_ctl = {
2814 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2815 .name = "",
2816 .info = scarlett2_speaker_switch_enum_ctl_info,
2817 .get = scarlett2_speaker_switch_enum_ctl_get,
2818 .put = scarlett2_speaker_switch_enum_ctl_put,
2819};
2820
2821static int scarlett2_add_speaker_switch_ctl(
2822 struct usb_mixer_interface *mixer)
2823{
2824 struct scarlett2_data *private = mixer->private_data;
2825 const struct scarlett2_device_info *info = private->info;
2826
2827 if (!info->has_speaker_switching)
2828 return 0;
2829
2830 return scarlett2_add_new_ctl(
2831 mixer, &scarlett2_speaker_switch_enum_ctl,
2832 0, 1, "Speaker Switching Playback Enum",
2833 &private->speaker_switching_ctl);
2834}
2835
2836/*** Talkback and Talkback Map Controls ***/
2837
2838static int scarlett2_talkback_enum_ctl_info(
2839 struct snd_kcontrol *kctl, struct snd_ctl_elem_info *uinfo)
2840{
2841 static const char *const values[3] = {
2842 "Disabled", "Off", "On"
2843 };
2844
2845 return snd_ctl_enum_info(uinfo, 1, 3, values);
2846}
2847
2848static int scarlett2_talkback_enum_ctl_get(
2849 struct snd_kcontrol *kctl, struct snd_ctl_elem_value *ucontrol)
2850{
2851 struct usb_mixer_elem_info *elem = kctl->private_data;
2852 struct usb_mixer_interface *mixer = elem->head.mixer;
2853 struct scarlett2_data *private = mixer->private_data;
2854
2855 mutex_lock(&private->data_mutex);
2856 if (private->monitor_other_updated)
2857 scarlett2_update_monitor_other(mixer);
2858 ucontrol->value.enumerated.item[0] = private->talkback_switch;
2859 mutex_unlock(&private->data_mutex);
2860
2861 return 0;
2862}
2863
2864static int scarlett2_talkback_enum_ctl_put(
2865 struct snd_kcontrol *kctl, struct snd_ctl_elem_value *ucontrol)
2866{
2867 struct usb_mixer_elem_info *elem = kctl->private_data;
2868 struct usb_mixer_interface *mixer = elem->head.mixer;
2869 struct scarlett2_data *private = mixer->private_data;
2870
2871 int oval, val, err = 0;
2872
2873 mutex_lock(&private->data_mutex);
2874
2875 oval = private->talkback_switch;
2876 val = min(ucontrol->value.enumerated.item[0], 2U);
2877
2878 if (oval == val)
2879 goto unlock;
2880
2881 private->talkback_switch = val;
2882
2883 /* enable/disable talkback */
2884 err = scarlett2_usb_set_config(
2885 mixer, SCARLETT2_CONFIG_MONITOR_OTHER_ENABLE,
2886 1, !!val);
2887 if (err < 0)
2888 goto unlock;
2889
2890 /* if talkback is enabled, select main or alt */
2891 err = scarlett2_usb_set_config(
2892 mixer, SCARLETT2_CONFIG_MONITOR_OTHER_SWITCH,
2893 1, val == 2);
2894 if (err == 0)
2895 err = 1;
2896
2897unlock:
2898 mutex_unlock(&private->data_mutex);
2899 return err;
2900}
2901
2902static const struct snd_kcontrol_new scarlett2_talkback_enum_ctl = {
2903 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2904 .name = "",
2905 .info = scarlett2_talkback_enum_ctl_info,
2906 .get = scarlett2_talkback_enum_ctl_get,
2907 .put = scarlett2_talkback_enum_ctl_put,
2908};
2909
2910static int scarlett2_talkback_map_ctl_get(
2911 struct snd_kcontrol *kctl, struct snd_ctl_elem_value *ucontrol)
2912{
2913 struct usb_mixer_elem_info *elem = kctl->private_data;
2914 struct usb_mixer_interface *mixer = elem->head.mixer;
2915 struct scarlett2_data *private = mixer->private_data;
2916 int index = elem->control;
2917
2918 ucontrol->value.integer.value[0] = private->talkback_map[index];
2919
2920 return 0;
2921}
2922
2923static int scarlett2_talkback_map_ctl_put(
2924 struct snd_kcontrol *kctl, struct snd_ctl_elem_value *ucontrol)
2925{
2926 struct usb_mixer_elem_info *elem = kctl->private_data;
2927 struct usb_mixer_interface *mixer = elem->head.mixer;
2928 struct scarlett2_data *private = mixer->private_data;
2929 const int (*port_count)[SCARLETT2_PORT_DIRNS] =
2930 private->info->port_count;
2931 int num_mixes = port_count[SCARLETT2_PORT_TYPE_MIX][SCARLETT2_PORT_IN];
2932
2933 int index = elem->control;
2934 int oval, val, err = 0, i;
2935 u16 bitmap = 0;
2936
2937 mutex_lock(&private->data_mutex);
2938
2939 oval = private->talkback_map[index];
2940 val = !!ucontrol->value.integer.value[0];
2941
2942 if (oval == val)
2943 goto unlock;
2944
2945 private->talkback_map[index] = val;
2946
2947 for (i = 0; i < num_mixes; i++)
2948 bitmap |= private->talkback_map[i] << i;
2949
2950 /* Send updated bitmap to the device */
2951 err = scarlett2_usb_set_config(mixer, SCARLETT2_CONFIG_TALKBACK_MAP,
2952 0, bitmap);
2953 if (err == 0)
2954 err = 1;
2955
2956unlock:
2957 mutex_unlock(&private->data_mutex);
2958 return err;
2959}
2960
2961static const struct snd_kcontrol_new scarlett2_talkback_map_ctl = {
2962 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2963 .name = "",
2964 .info = snd_ctl_boolean_mono_info,
2965 .get = scarlett2_talkback_map_ctl_get,
2966 .put = scarlett2_talkback_map_ctl_put,
2967};
2968
2969static int scarlett2_add_talkback_ctls(
2970 struct usb_mixer_interface *mixer)
2971{
2972 struct scarlett2_data *private = mixer->private_data;
2973 const struct scarlett2_device_info *info = private->info;
2974 const int (*port_count)[SCARLETT2_PORT_DIRNS] = info->port_count;
2975 int num_mixes = port_count[SCARLETT2_PORT_TYPE_MIX][SCARLETT2_PORT_IN];
2976 int err, i;
2977 char s[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
2978
2979 if (!info->has_talkback)
2980 return 0;
2981
2982 err = scarlett2_add_new_ctl(
2983 mixer, &scarlett2_talkback_enum_ctl,
2984 0, 1, "Talkback Playback Enum",
2985 &private->talkback_ctl);
2986 if (err < 0)
2987 return err;
2988
2989 for (i = 0; i < num_mixes; i++) {
2990 snprintf(s, sizeof(s),
2991 "Talkback Mix %c Playback Switch", i + 'A');
2992 err = scarlett2_add_new_ctl(mixer, &scarlett2_talkback_map_ctl,
2993 i, 1, s, NULL);
2994 if (err < 0)
2995 return err;
2996 }
2997
2998 return 0;
2999}
3000
3001/*** Dim/Mute Controls ***/
3002
3003static int scarlett2_dim_mute_ctl_get(struct snd_kcontrol *kctl,
3004 struct snd_ctl_elem_value *ucontrol)
3005{
3006 struct usb_mixer_elem_info *elem = kctl->private_data;
3007 struct usb_mixer_interface *mixer = elem->head.mixer;
3008 struct scarlett2_data *private = mixer->private_data;
3009
3010 mutex_lock(&private->data_mutex);
3011 if (private->vol_updated)
3012 scarlett2_update_volumes(mixer);
3013 mutex_unlock(&private->data_mutex);
3014
3015 ucontrol->value.integer.value[0] = private->dim_mute[elem->control];
3016 return 0;
3017}
3018
3019static int scarlett2_dim_mute_ctl_put(struct snd_kcontrol *kctl,
3020 struct snd_ctl_elem_value *ucontrol)
3021{
3022 struct usb_mixer_elem_info *elem = kctl->private_data;
3023 struct usb_mixer_interface *mixer = elem->head.mixer;
3024 struct scarlett2_data *private = mixer->private_data;
3025 const struct scarlett2_device_info *info = private->info;
3026 const int (*port_count)[SCARLETT2_PORT_DIRNS] = info->port_count;
3027 int num_line_out =
3028 port_count[SCARLETT2_PORT_TYPE_ANALOGUE][SCARLETT2_PORT_OUT];
3029
3030 int index = elem->control;
3031 int oval, val, err = 0, i;
3032
3033 mutex_lock(&private->data_mutex);
3034
3035 oval = private->dim_mute[index];
3036 val = !!ucontrol->value.integer.value[0];
3037
3038 if (oval == val)
3039 goto unlock;
3040
3041 private->dim_mute[index] = val;
3042
3043 /* Send switch change to the device */
3044 err = scarlett2_usb_set_config(mixer, SCARLETT2_CONFIG_DIM_MUTE,
3045 index, val);
3046 if (err == 0)
3047 err = 1;
3048
3049 if (index == SCARLETT2_BUTTON_MUTE)
3050 for (i = 0; i < num_line_out; i++) {
3051 int line_index = line_out_remap(private, i);
3052
3053 if (private->vol_sw_hw_switch[line_index]) {
3054 private->mute_switch[line_index] = val;
3055 snd_ctl_notify(mixer->chip->card,
3056 SNDRV_CTL_EVENT_MASK_VALUE,
3057 &private->mute_ctls[i]->id);
3058 }
3059 }
3060
3061unlock:
3062 mutex_unlock(&private->data_mutex);
3063 return err;
3064}
3065
3066static const struct snd_kcontrol_new scarlett2_dim_mute_ctl = {
3067 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3068 .name = "",
3069 .info = snd_ctl_boolean_mono_info,
3070 .get = scarlett2_dim_mute_ctl_get,
3071 .put = scarlett2_dim_mute_ctl_put
3072};
3073
3074/*** Create the analogue output controls ***/
3075
3076static int scarlett2_add_line_out_ctls(struct usb_mixer_interface *mixer)
3077{
3078 struct scarlett2_data *private = mixer->private_data;
3079 const struct scarlett2_device_info *info = private->info;
3080 const int (*port_count)[SCARLETT2_PORT_DIRNS] = info->port_count;
3081 int num_line_out =
3082 port_count[SCARLETT2_PORT_TYPE_ANALOGUE][SCARLETT2_PORT_OUT];
3083 int err, i;
3084 char s[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
3085
3086 /* Add R/O HW volume control */
3087 if (info->line_out_hw_vol) {
3088 snprintf(s, sizeof(s), "Master HW Playback Volume");
3089 err = scarlett2_add_new_ctl(mixer,
3090 &scarlett2_master_volume_ctl,
3091 0, 1, s, &private->master_vol_ctl);
3092 if (err < 0)
3093 return err;
3094 }
3095
3096 /* Add volume controls */
3097 for (i = 0; i < num_line_out; i++) {
3098 int index = line_out_remap(private, i);
3099
3100 /* Fader */
3101 if (info->line_out_descrs[i])
3102 snprintf(s, sizeof(s),
3103 "Line %02d (%s) Playback Volume",
3104 i + 1, info->line_out_descrs[i]);
3105 else
3106 snprintf(s, sizeof(s),
3107 "Line %02d Playback Volume",
3108 i + 1);
3109 err = scarlett2_add_new_ctl(mixer,
3110 &scarlett2_line_out_volume_ctl,
3111 i, 1, s, &private->vol_ctls[i]);
3112 if (err < 0)
3113 return err;
3114
3115 /* Mute Switch */
3116 snprintf(s, sizeof(s),
3117 "Line %02d Mute Playback Switch",
3118 i + 1);
3119 err = scarlett2_add_new_ctl(mixer,
3120 &scarlett2_mute_ctl,
3121 i, 1, s,
3122 &private->mute_ctls[i]);
3123 if (err < 0)
3124 return err;
3125
3126 /* Make the fader and mute controls read-only if the
3127 * SW/HW switch is set to HW
3128 */
3129 if (private->vol_sw_hw_switch[index])
3130 scarlett2_vol_ctl_set_writable(mixer, i, 0);
3131
3132 /* SW/HW Switch */
3133 if (info->line_out_hw_vol) {
3134 snprintf(s, sizeof(s),
3135 "Line Out %02d Volume Control Playback Enum",
3136 i + 1);
3137 err = scarlett2_add_new_ctl(mixer,
3138 &scarlett2_sw_hw_enum_ctl,
3139 i, 1, s,
3140 &private->sw_hw_ctls[i]);
3141 if (err < 0)
3142 return err;
3143
3144 /* Make the switch read-only if the line is
3145 * involved in speaker switching
3146 */
3147 if (private->speaker_switching_switch && i < 4)
3148 scarlett2_sw_hw_ctl_ro(private, i);
3149 }
3150 }
3151
3152 /* Add dim/mute controls */
3153 if (info->line_out_hw_vol)
3154 for (i = 0; i < SCARLETT2_DIM_MUTE_COUNT; i++) {
3155 err = scarlett2_add_new_ctl(
3156 mixer, &scarlett2_dim_mute_ctl,
3157 i, 1, scarlett2_dim_mute_names[i],
3158 &private->dim_mute_ctls[i]);
3159 if (err < 0)
3160 return err;
3161 }
3162
3163 return 0;
3164}
3165
3166/*** Create the analogue input controls ***/
3167
3168static int scarlett2_add_line_in_ctls(struct usb_mixer_interface *mixer)
3169{
3170 struct scarlett2_data *private = mixer->private_data;
3171 const struct scarlett2_device_info *info = private->info;
3172 int err, i;
3173 char s[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
3174 const char *fmt = "Line In %d %s Capture %s";
3175 const char *fmt2 = "Line In %d-%d %s Capture %s";
3176
3177 /* Add input level (line/inst) controls */
3178 for (i = 0; i < info->level_input_count; i++) {
3179 snprintf(s, sizeof(s), fmt, i + 1 + info->level_input_first,
3180 "Level", "Enum");
3181 err = scarlett2_add_new_ctl(mixer, &scarlett2_level_enum_ctl,
3182 i, 1, s, &private->level_ctls[i]);
3183 if (err < 0)
3184 return err;
3185 }
3186
3187 /* Add input pad controls */
3188 for (i = 0; i < info->pad_input_count; i++) {
3189 snprintf(s, sizeof(s), fmt, i + 1, "Pad", "Switch");
3190 err = scarlett2_add_new_ctl(mixer, &scarlett2_pad_ctl,
3191 i, 1, s, &private->pad_ctls[i]);
3192 if (err < 0)
3193 return err;
3194 }
3195
3196 /* Add input air controls */
3197 for (i = 0; i < info->air_input_count; i++) {
3198 snprintf(s, sizeof(s), fmt, i + 1, "Air", "Switch");
3199 err = scarlett2_add_new_ctl(mixer, &scarlett2_air_ctl,
3200 i, 1, s, &private->air_ctls[i]);
3201 if (err < 0)
3202 return err;
3203 }
3204
3205 /* Add input phantom controls */
3206 if (info->inputs_per_phantom == 1) {
3207 for (i = 0; i < info->phantom_count; i++) {
3208 snprintf(s, sizeof(s), fmt, i + 1,
3209 "Phantom Power", "Switch");
3210 err = scarlett2_add_new_ctl(
3211 mixer, &scarlett2_phantom_ctl,
3212 i, 1, s, &private->phantom_ctls[i]);
3213 if (err < 0)
3214 return err;
3215 }
3216 } else if (info->inputs_per_phantom > 1) {
3217 for (i = 0; i < info->phantom_count; i++) {
3218 int from = i * info->inputs_per_phantom + 1;
3219 int to = (i + 1) * info->inputs_per_phantom;
3220
3221 snprintf(s, sizeof(s), fmt2, from, to,
3222 "Phantom Power", "Switch");
3223 err = scarlett2_add_new_ctl(
3224 mixer, &scarlett2_phantom_ctl,
3225 i, 1, s, &private->phantom_ctls[i]);
3226 if (err < 0)
3227 return err;
3228 }
3229 }
3230 if (info->phantom_count) {
3231 err = scarlett2_add_new_ctl(
3232 mixer, &scarlett2_phantom_persistence_ctl, 0, 1,
3233 "Phantom Power Persistence Capture Switch", NULL);
3234 if (err < 0)
3235 return err;
3236 }
3237
3238 return 0;
3239}
3240
3241/*** Mixer Volume Controls ***/
3242
3243static int scarlett2_mixer_ctl_info(struct snd_kcontrol *kctl,
3244 struct snd_ctl_elem_info *uinfo)
3245{
3246 struct usb_mixer_elem_info *elem = kctl->private_data;
3247
3248 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
3249 uinfo->count = elem->channels;
3250 uinfo->value.integer.min = 0;
3251 uinfo->value.integer.max = SCARLETT2_MIXER_MAX_VALUE;
3252 uinfo->value.integer.step = 1;
3253 return 0;
3254}
3255
3256static int scarlett2_mixer_ctl_get(struct snd_kcontrol *kctl,
3257 struct snd_ctl_elem_value *ucontrol)
3258{
3259 struct usb_mixer_elem_info *elem = kctl->private_data;
3260 struct scarlett2_data *private = elem->head.mixer->private_data;
3261
3262 ucontrol->value.integer.value[0] = private->mix[elem->control];
3263 return 0;
3264}
3265
3266static int scarlett2_mixer_ctl_put(struct snd_kcontrol *kctl,
3267 struct snd_ctl_elem_value *ucontrol)
3268{
3269 struct usb_mixer_elem_info *elem = kctl->private_data;
3270 struct usb_mixer_interface *mixer = elem->head.mixer;
3271 struct scarlett2_data *private = mixer->private_data;
3272 const struct scarlett2_device_info *info = private->info;
3273 const int (*port_count)[SCARLETT2_PORT_DIRNS] = info->port_count;
3274 int oval, val, num_mixer_in, mix_num, err = 0;
3275 int index = elem->control;
3276
3277 mutex_lock(&private->data_mutex);
3278
3279 oval = private->mix[index];
3280 val = ucontrol->value.integer.value[0];
3281 num_mixer_in = port_count[SCARLETT2_PORT_TYPE_MIX][SCARLETT2_PORT_OUT];
3282 mix_num = index / num_mixer_in;
3283
3284 if (oval == val)
3285 goto unlock;
3286
3287 private->mix[index] = val;
3288 err = scarlett2_usb_set_mix(mixer, mix_num);
3289 if (err == 0)
3290 err = 1;
3291
3292unlock:
3293 mutex_unlock(&private->data_mutex);
3294 return err;
3295}
3296
3297static const DECLARE_TLV_DB_MINMAX(
3298 db_scale_scarlett2_mixer,
3299 SCARLETT2_MIXER_MIN_DB * 100,
3300 SCARLETT2_MIXER_MAX_DB * 100
3301);
3302
3303static const struct snd_kcontrol_new scarlett2_mixer_ctl = {
3304 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3305 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE |
3306 SNDRV_CTL_ELEM_ACCESS_TLV_READ,
3307 .name = "",
3308 .info = scarlett2_mixer_ctl_info,
3309 .get = scarlett2_mixer_ctl_get,
3310 .put = scarlett2_mixer_ctl_put,
3311 .private_value = SCARLETT2_MIXER_MAX_DB, /* max value */
3312 .tlv = { .p = db_scale_scarlett2_mixer }
3313};
3314
3315static int scarlett2_add_mixer_ctls(struct usb_mixer_interface *mixer)
3316{
3317 struct scarlett2_data *private = mixer->private_data;
3318 const struct scarlett2_device_info *info = private->info;
3319 const int (*port_count)[SCARLETT2_PORT_DIRNS] = info->port_count;
3320 int err, i, j;
3321 int index;
3322 char s[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
3323
3324 int num_inputs =
3325 port_count[SCARLETT2_PORT_TYPE_MIX][SCARLETT2_PORT_OUT];
3326 int num_outputs =
3327 port_count[SCARLETT2_PORT_TYPE_MIX][SCARLETT2_PORT_IN];
3328
3329 for (i = 0, index = 0; i < num_outputs; i++)
3330 for (j = 0; j < num_inputs; j++, index++) {
3331 snprintf(s, sizeof(s),
3332 "Mix %c Input %02d Playback Volume",
3333 'A' + i, j + 1);
3334 err = scarlett2_add_new_ctl(mixer, &scarlett2_mixer_ctl,
3335 index, 1, s, NULL);
3336 if (err < 0)
3337 return err;
3338 }
3339
3340 return 0;
3341}
3342
3343/*** Mux Source Selection Controls ***/
3344
3345static int scarlett2_mux_src_enum_ctl_info(struct snd_kcontrol *kctl,
3346 struct snd_ctl_elem_info *uinfo)
3347{
3348 struct usb_mixer_elem_info *elem = kctl->private_data;
3349 struct scarlett2_data *private = elem->head.mixer->private_data;
3350 const struct scarlett2_device_info *info = private->info;
3351 const int (*port_count)[SCARLETT2_PORT_DIRNS] = info->port_count;
3352 unsigned int item = uinfo->value.enumerated.item;
3353 int items = private->num_mux_srcs;
3354 int port_type;
3355
3356 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
3357 uinfo->count = elem->channels;
3358 uinfo->value.enumerated.items = items;
3359
3360 if (item >= items)
3361 item = uinfo->value.enumerated.item = items - 1;
3362
3363 for (port_type = 0;
3364 port_type < SCARLETT2_PORT_TYPE_COUNT;
3365 port_type++) {
3366 if (item < port_count[port_type][SCARLETT2_PORT_IN]) {
3367 const struct scarlett2_port *port =
3368 &scarlett2_ports[port_type];
3369
3370 sprintf(uinfo->value.enumerated.name,
3371 port->src_descr, item + port->src_num_offset);
3372 return 0;
3373 }
3374 item -= port_count[port_type][SCARLETT2_PORT_IN];
3375 }
3376
3377 return -EINVAL;
3378}
3379
3380static int scarlett2_mux_src_enum_ctl_get(struct snd_kcontrol *kctl,
3381 struct snd_ctl_elem_value *ucontrol)
3382{
3383 struct usb_mixer_elem_info *elem = kctl->private_data;
3384 struct usb_mixer_interface *mixer = elem->head.mixer;
3385 struct scarlett2_data *private = mixer->private_data;
3386 const struct scarlett2_device_info *info = private->info;
3387 const int (*port_count)[SCARLETT2_PORT_DIRNS] = info->port_count;
3388 int line_out_count =
3389 port_count[SCARLETT2_PORT_TYPE_ANALOGUE][SCARLETT2_PORT_OUT];
3390 int index = elem->control;
3391
3392 if (index < line_out_count)
3393 index = line_out_remap(private, index);
3394
3395 mutex_lock(&private->data_mutex);
3396 if (private->mux_updated)
3397 scarlett2_usb_get_mux(mixer);
3398 ucontrol->value.enumerated.item[0] = private->mux[index];
3399 mutex_unlock(&private->data_mutex);
3400
3401 return 0;
3402}
3403
3404static int scarlett2_mux_src_enum_ctl_put(struct snd_kcontrol *kctl,
3405 struct snd_ctl_elem_value *ucontrol)
3406{
3407 struct usb_mixer_elem_info *elem = kctl->private_data;
3408 struct usb_mixer_interface *mixer = elem->head.mixer;
3409 struct scarlett2_data *private = mixer->private_data;
3410 const struct scarlett2_device_info *info = private->info;
3411 const int (*port_count)[SCARLETT2_PORT_DIRNS] = info->port_count;
3412 int line_out_count =
3413 port_count[SCARLETT2_PORT_TYPE_ANALOGUE][SCARLETT2_PORT_OUT];
3414 int index = elem->control;
3415 int oval, val, err = 0;
3416
3417 if (index < line_out_count)
3418 index = line_out_remap(private, index);
3419
3420 mutex_lock(&private->data_mutex);
3421
3422 oval = private->mux[index];
3423 val = min(ucontrol->value.enumerated.item[0],
3424 private->num_mux_srcs - 1U);
3425
3426 if (oval == val)
3427 goto unlock;
3428
3429 private->mux[index] = val;
3430 err = scarlett2_usb_set_mux(mixer);
3431 if (err == 0)
3432 err = 1;
3433
3434unlock:
3435 mutex_unlock(&private->data_mutex);
3436 return err;
3437}
3438
3439static const struct snd_kcontrol_new scarlett2_mux_src_enum_ctl = {
3440 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3441 .name = "",
3442 .info = scarlett2_mux_src_enum_ctl_info,
3443 .get = scarlett2_mux_src_enum_ctl_get,
3444 .put = scarlett2_mux_src_enum_ctl_put,
3445};
3446
3447static int scarlett2_add_mux_enums(struct usb_mixer_interface *mixer)
3448{
3449 struct scarlett2_data *private = mixer->private_data;
3450 const struct scarlett2_device_info *info = private->info;
3451 const int (*port_count)[SCARLETT2_PORT_DIRNS] = info->port_count;
3452 int port_type, channel, i;
3453
3454 for (i = 0, port_type = 0;
3455 port_type < SCARLETT2_PORT_TYPE_COUNT;
3456 port_type++) {
3457 for (channel = 0;
3458 channel < port_count[port_type][SCARLETT2_PORT_OUT];
3459 channel++, i++) {
3460 int err;
3461 char s[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
3462 const char *const descr =
3463 scarlett2_ports[port_type].dst_descr;
3464
3465 snprintf(s, sizeof(s) - 5, descr, channel + 1);
3466 strcat(s, " Enum");
3467
3468 err = scarlett2_add_new_ctl(mixer,
3469 &scarlett2_mux_src_enum_ctl,
3470 i, 1, s,
3471 &private->mux_ctls[i]);
3472 if (err < 0)
3473 return err;
3474 }
3475 }
3476
3477 return 0;
3478}
3479
3480/*** Meter Controls ***/
3481
3482static int scarlett2_meter_ctl_info(struct snd_kcontrol *kctl,
3483 struct snd_ctl_elem_info *uinfo)
3484{
3485 struct usb_mixer_elem_info *elem = kctl->private_data;
3486
3487 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
3488 uinfo->count = elem->channels;
3489 uinfo->value.integer.min = 0;
3490 uinfo->value.integer.max = 4095;
3491 uinfo->value.integer.step = 1;
3492 return 0;
3493}
3494
3495static int scarlett2_meter_ctl_get(struct snd_kcontrol *kctl,
3496 struct snd_ctl_elem_value *ucontrol)
3497{
3498 struct usb_mixer_elem_info *elem = kctl->private_data;
3499 u16 meter_levels[SCARLETT2_MAX_METERS];
3500 int i, err;
3501
3502 err = scarlett2_usb_get_meter_levels(elem->head.mixer, elem->channels,
3503 meter_levels);
3504 if (err < 0)
3505 return err;
3506
3507 for (i = 0; i < elem->channels; i++)
3508 ucontrol->value.integer.value[i] = meter_levels[i];
3509
3510 return 0;
3511}
3512
3513static const struct snd_kcontrol_new scarlett2_meter_ctl = {
3514 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
3515 .access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE,
3516 .name = "",
3517 .info = scarlett2_meter_ctl_info,
3518 .get = scarlett2_meter_ctl_get
3519};
3520
3521static int scarlett2_add_meter_ctl(struct usb_mixer_interface *mixer)
3522{
3523 struct scarlett2_data *private = mixer->private_data;
3524
3525 /* devices without a mixer also don't support reporting levels */
3526 if (private->info->config_set == SCARLETT2_CONFIG_SET_NO_MIXER)
3527 return 0;
3528
3529 return scarlett2_add_new_ctl(mixer, &scarlett2_meter_ctl,
3530 0, private->num_mux_dsts,
3531 "Level Meter", NULL);
3532}
3533
3534/*** MSD Controls ***/
3535
3536static int scarlett2_msd_ctl_get(struct snd_kcontrol *kctl,
3537 struct snd_ctl_elem_value *ucontrol)
3538{
3539 struct usb_mixer_elem_info *elem = kctl->private_data;
3540 struct scarlett2_data *private = elem->head.mixer->private_data;
3541
3542 ucontrol->value.integer.value[0] = private->msd_switch;
3543 return 0;
3544}
3545
3546static int scarlett2_msd_ctl_put(struct snd_kcontrol *kctl,
3547 struct snd_ctl_elem_value *ucontrol)
3548{
3549 struct usb_mixer_elem_info *elem = kctl->private_data;
3550 struct usb_mixer_interface *mixer = elem->head.mixer;
3551 struct scarlett2_data *private = mixer->private_data;
3552
3553 int oval, val, err = 0;
3554
3555 mutex_lock(&private->data_mutex);
3556
3557 oval = private->msd_switch;
3558 val = !!ucontrol->value.integer.value[0];
3559
3560 if (oval == val)
3561 goto unlock;
3562
3563 private->msd_switch = val;
3564
3565 /* Send switch change to the device */
3566 err = scarlett2_usb_set_config(mixer, SCARLETT2_CONFIG_MSD_SWITCH,
3567 0, val);
3568 if (err == 0)
3569 err = 1;
3570
3571unlock:
3572 mutex_unlock(&private->data_mutex);
3573 return err;
3574}
3575
3576static const struct snd_kcontrol_new scarlett2_msd_ctl = {
3577 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3578 .name = "",
3579 .info = snd_ctl_boolean_mono_info,
3580 .get = scarlett2_msd_ctl_get,
3581 .put = scarlett2_msd_ctl_put,
3582};
3583
3584static int scarlett2_add_msd_ctl(struct usb_mixer_interface *mixer)
3585{
3586 struct scarlett2_data *private = mixer->private_data;
3587 const struct scarlett2_device_info *info = private->info;
3588
3589 if (!info->has_msd_mode)
3590 return 0;
3591
3592 /* If MSD mode is off, hide the switch by default */
3593 if (!private->msd_switch && !(mixer->chip->setup & SCARLETT2_MSD_ENABLE))
3594 return 0;
3595
3596 /* Add MSD control */
3597 return scarlett2_add_new_ctl(mixer, &scarlett2_msd_ctl,
3598 0, 1, "MSD Mode Switch", NULL);
3599}
3600
3601/*** Standalone Control ***/
3602
3603static int scarlett2_standalone_ctl_get(struct snd_kcontrol *kctl,
3604 struct snd_ctl_elem_value *ucontrol)
3605{
3606 struct usb_mixer_elem_info *elem = kctl->private_data;
3607 struct scarlett2_data *private = elem->head.mixer->private_data;
3608
3609 ucontrol->value.integer.value[0] = private->standalone_switch;
3610 return 0;
3611}
3612
3613static int scarlett2_standalone_ctl_put(struct snd_kcontrol *kctl,
3614 struct snd_ctl_elem_value *ucontrol)
3615{
3616 struct usb_mixer_elem_info *elem = kctl->private_data;
3617 struct usb_mixer_interface *mixer = elem->head.mixer;
3618 struct scarlett2_data *private = mixer->private_data;
3619
3620 int oval, val, err = 0;
3621
3622 mutex_lock(&private->data_mutex);
3623
3624 oval = private->standalone_switch;
3625 val = !!ucontrol->value.integer.value[0];
3626
3627 if (oval == val)
3628 goto unlock;
3629
3630 private->standalone_switch = val;
3631
3632 /* Send switch change to the device */
3633 err = scarlett2_usb_set_config(mixer,
3634 SCARLETT2_CONFIG_STANDALONE_SWITCH,
3635 0, val);
3636 if (err == 0)
3637 err = 1;
3638
3639unlock:
3640 mutex_unlock(&private->data_mutex);
3641 return err;
3642}
3643
3644static const struct snd_kcontrol_new scarlett2_standalone_ctl = {
3645 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3646 .name = "",
3647 .info = snd_ctl_boolean_mono_info,
3648 .get = scarlett2_standalone_ctl_get,
3649 .put = scarlett2_standalone_ctl_put,
3650};
3651
3652static int scarlett2_add_standalone_ctl(struct usb_mixer_interface *mixer)
3653{
3654 struct scarlett2_data *private = mixer->private_data;
3655
3656 if (private->info->config_set == SCARLETT2_CONFIG_SET_NO_MIXER)
3657 return 0;
3658
3659 /* Add standalone control */
3660 return scarlett2_add_new_ctl(mixer, &scarlett2_standalone_ctl,
3661 0, 1, "Standalone Switch", NULL);
3662}
3663
3664/*** Cleanup/Suspend Callbacks ***/
3665
3666static void scarlett2_private_free(struct usb_mixer_interface *mixer)
3667{
3668 struct scarlett2_data *private = mixer->private_data;
3669
3670 cancel_delayed_work_sync(&private->work);
3671 kfree(private);
3672 mixer->private_data = NULL;
3673}
3674
3675static void scarlett2_private_suspend(struct usb_mixer_interface *mixer)
3676{
3677 struct scarlett2_data *private = mixer->private_data;
3678
3679 if (cancel_delayed_work_sync(&private->work))
3680 scarlett2_config_save(private->mixer);
3681}
3682
3683/*** Initialisation ***/
3684
3685static void scarlett2_count_mux_io(struct scarlett2_data *private)
3686{
3687 const struct scarlett2_device_info *info = private->info;
3688 const int (*port_count)[SCARLETT2_PORT_DIRNS] = info->port_count;
3689 int port_type, srcs = 0, dsts = 0;
3690
3691 for (port_type = 0;
3692 port_type < SCARLETT2_PORT_TYPE_COUNT;
3693 port_type++) {
3694 srcs += port_count[port_type][SCARLETT2_PORT_IN];
3695 dsts += port_count[port_type][SCARLETT2_PORT_OUT];
3696 }
3697
3698 private->num_mux_srcs = srcs;
3699 private->num_mux_dsts = dsts;
3700}
3701
3702/* Look through the interface descriptors for the Focusrite Control
3703 * interface (bInterfaceClass = 255 Vendor Specific Class) and set
3704 * bInterfaceNumber, bEndpointAddress, wMaxPacketSize, and bInterval
3705 * in private
3706 */
3707static int scarlett2_find_fc_interface(struct usb_device *dev,
3708 struct scarlett2_data *private)
3709{
3710 struct usb_host_config *config = dev->actconfig;
3711 int i;
3712
3713 for (i = 0; i < config->desc.bNumInterfaces; i++) {
3714 struct usb_interface *intf = config->interface[i];
3715 struct usb_interface_descriptor *desc =
3716 &intf->altsetting[0].desc;
3717 struct usb_endpoint_descriptor *epd;
3718
3719 if (desc->bInterfaceClass != 255)
3720 continue;
3721
3722 epd = get_endpoint(intf->altsetting, 0);
3723 private->bInterfaceNumber = desc->bInterfaceNumber;
3724 private->bEndpointAddress = epd->bEndpointAddress &
3725 USB_ENDPOINT_NUMBER_MASK;
3726 private->wMaxPacketSize = le16_to_cpu(epd->wMaxPacketSize);
3727 private->bInterval = epd->bInterval;
3728 return 0;
3729 }
3730
3731 return -EINVAL;
3732}
3733
3734/* Initialise private data */
3735static int scarlett2_init_private(struct usb_mixer_interface *mixer,
3736 const struct scarlett2_device_info *info)
3737{
3738 struct scarlett2_data *private =
3739 kzalloc(sizeof(struct scarlett2_data), GFP_KERNEL);
3740
3741 if (!private)
3742 return -ENOMEM;
3743
3744 mutex_init(&private->usb_mutex);
3745 mutex_init(&private->data_mutex);
3746 INIT_DELAYED_WORK(&private->work, scarlett2_config_save_work);
3747
3748 mixer->private_data = private;
3749 mixer->private_free = scarlett2_private_free;
3750 mixer->private_suspend = scarlett2_private_suspend;
3751
3752 private->info = info;
3753 scarlett2_count_mux_io(private);
3754 private->scarlett2_seq = 0;
3755 private->mixer = mixer;
3756
3757 return scarlett2_find_fc_interface(mixer->chip->dev, private);
3758}
3759
3760/* Cargo cult proprietary initialisation sequence */
3761static int scarlett2_usb_init(struct usb_mixer_interface *mixer)
3762{
3763 struct usb_device *dev = mixer->chip->dev;
3764 struct scarlett2_data *private = mixer->private_data;
3765 u8 buf[24];
3766 int err;
3767
3768 if (usb_pipe_type_check(dev, usb_sndctrlpipe(dev, 0)))
3769 return -EINVAL;
3770
3771 /* step 0 */
3772 err = scarlett2_usb_rx(dev, private->bInterfaceNumber,
3773 SCARLETT2_USB_CMD_INIT, buf, sizeof(buf));
3774 if (err < 0)
3775 return err;
3776
3777 /* step 1 */
3778 private->scarlett2_seq = 1;
3779 err = scarlett2_usb(mixer, SCARLETT2_USB_INIT_1, NULL, 0, NULL, 0);
3780 if (err < 0)
3781 return err;
3782
3783 /* step 2 */
3784 private->scarlett2_seq = 1;
3785 return scarlett2_usb(mixer, SCARLETT2_USB_INIT_2, NULL, 0, NULL, 84);
3786}
3787
3788/* Read configuration from the interface on start */
3789static int scarlett2_read_configs(struct usb_mixer_interface *mixer)
3790{
3791 struct scarlett2_data *private = mixer->private_data;
3792 const struct scarlett2_device_info *info = private->info;
3793 const int (*port_count)[SCARLETT2_PORT_DIRNS] = info->port_count;
3794 int num_line_out =
3795 port_count[SCARLETT2_PORT_TYPE_ANALOGUE][SCARLETT2_PORT_OUT];
3796 int num_mixer_out =
3797 port_count[SCARLETT2_PORT_TYPE_MIX][SCARLETT2_PORT_IN];
3798 struct scarlett2_usb_volume_status volume_status;
3799 int err, i;
3800
3801 if (info->has_msd_mode) {
3802 err = scarlett2_usb_get_config(
3803 mixer, SCARLETT2_CONFIG_MSD_SWITCH,
3804 1, &private->msd_switch);
3805 if (err < 0)
3806 return err;
3807
3808 /* no other controls are created if MSD mode is on */
3809 if (private->msd_switch)
3810 return 0;
3811 }
3812
3813 err = scarlett2_update_input_other(mixer);
3814 if (err < 0)
3815 return err;
3816
3817 err = scarlett2_update_monitor_other(mixer);
3818 if (err < 0)
3819 return err;
3820
3821 /* the rest of the configuration is for devices with a mixer */
3822 if (info->config_set == SCARLETT2_CONFIG_SET_NO_MIXER)
3823 return 0;
3824
3825 err = scarlett2_usb_get_config(
3826 mixer, SCARLETT2_CONFIG_STANDALONE_SWITCH,
3827 1, &private->standalone_switch);
3828 if (err < 0)
3829 return err;
3830
3831 err = scarlett2_update_sync(mixer);
3832 if (err < 0)
3833 return err;
3834
3835 err = scarlett2_usb_get_volume_status(mixer, &volume_status);
3836 if (err < 0)
3837 return err;
3838
3839 if (info->line_out_hw_vol)
3840 for (i = 0; i < SCARLETT2_DIM_MUTE_COUNT; i++)
3841 private->dim_mute[i] = !!volume_status.dim_mute[i];
3842
3843 private->master_vol = clamp(
3844 volume_status.master_vol + SCARLETT2_VOLUME_BIAS,
3845 0, SCARLETT2_VOLUME_BIAS);
3846
3847 for (i = 0; i < num_line_out; i++) {
3848 int volume, mute;
3849
3850 private->vol_sw_hw_switch[i] =
3851 info->line_out_hw_vol
3852 && volume_status.sw_hw_switch[i];
3853
3854 volume = private->vol_sw_hw_switch[i]
3855 ? volume_status.master_vol
3856 : volume_status.sw_vol[i];
3857 volume = clamp(volume + SCARLETT2_VOLUME_BIAS,
3858 0, SCARLETT2_VOLUME_BIAS);
3859 private->vol[i] = volume;
3860
3861 mute = private->vol_sw_hw_switch[i]
3862 ? private->dim_mute[SCARLETT2_BUTTON_MUTE]
3863 : volume_status.mute_switch[i];
3864 private->mute_switch[i] = mute;
3865 }
3866
3867 for (i = 0; i < num_mixer_out; i++) {
3868 err = scarlett2_usb_get_mix(mixer, i);
3869 if (err < 0)
3870 return err;
3871 }
3872
3873 return scarlett2_usb_get_mux(mixer);
3874}
3875
3876/* Notify on sync change */
3877static void scarlett2_notify_sync(
3878 struct usb_mixer_interface *mixer)
3879{
3880 struct scarlett2_data *private = mixer->private_data;
3881
3882 private->sync_updated = 1;
3883
3884 snd_ctl_notify(mixer->chip->card, SNDRV_CTL_EVENT_MASK_VALUE,
3885 &private->sync_ctl->id);
3886}
3887
3888/* Notify on monitor change */
3889static void scarlett2_notify_monitor(
3890 struct usb_mixer_interface *mixer)
3891{
3892 struct snd_card *card = mixer->chip->card;
3893 struct scarlett2_data *private = mixer->private_data;
3894 const struct scarlett2_device_info *info = private->info;
3895 const int (*port_count)[SCARLETT2_PORT_DIRNS] = info->port_count;
3896 int num_line_out =
3897 port_count[SCARLETT2_PORT_TYPE_ANALOGUE][SCARLETT2_PORT_OUT];
3898 int i;
3899
3900 /* if line_out_hw_vol is 0, there are no controls to update */
3901 if (!info->line_out_hw_vol)
3902 return;
3903
3904 private->vol_updated = 1;
3905
3906 snd_ctl_notify(mixer->chip->card, SNDRV_CTL_EVENT_MASK_VALUE,
3907 &private->master_vol_ctl->id);
3908
3909 for (i = 0; i < num_line_out; i++)
3910 if (private->vol_sw_hw_switch[line_out_remap(private, i)])
3911 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE,
3912 &private->vol_ctls[i]->id);
3913}
3914
3915/* Notify on dim/mute change */
3916static void scarlett2_notify_dim_mute(
3917 struct usb_mixer_interface *mixer)
3918{
3919 struct snd_card *card = mixer->chip->card;
3920 struct scarlett2_data *private = mixer->private_data;
3921 const struct scarlett2_device_info *info = private->info;
3922 const int (*port_count)[SCARLETT2_PORT_DIRNS] = info->port_count;
3923 int num_line_out =
3924 port_count[SCARLETT2_PORT_TYPE_ANALOGUE][SCARLETT2_PORT_OUT];
3925 int i;
3926
3927 private->vol_updated = 1;
3928
3929 if (!info->line_out_hw_vol)
3930 return;
3931
3932 for (i = 0; i < SCARLETT2_DIM_MUTE_COUNT; i++)
3933 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE,
3934 &private->dim_mute_ctls[i]->id);
3935
3936 for (i = 0; i < num_line_out; i++)
3937 if (private->vol_sw_hw_switch[line_out_remap(private, i)])
3938 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE,
3939 &private->mute_ctls[i]->id);
3940}
3941
3942/* Notify on "input other" change (level/pad/air) */
3943static void scarlett2_notify_input_other(
3944 struct usb_mixer_interface *mixer)
3945{
3946 struct snd_card *card = mixer->chip->card;
3947 struct scarlett2_data *private = mixer->private_data;
3948 const struct scarlett2_device_info *info = private->info;
3949 int i;
3950
3951 private->input_other_updated = 1;
3952
3953 for (i = 0; i < info->level_input_count; i++)
3954 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE,
3955 &private->level_ctls[i]->id);
3956 for (i = 0; i < info->pad_input_count; i++)
3957 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE,
3958 &private->pad_ctls[i]->id);
3959 for (i = 0; i < info->air_input_count; i++)
3960 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE,
3961 &private->air_ctls[i]->id);
3962 for (i = 0; i < info->phantom_count; i++)
3963 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE,
3964 &private->phantom_ctls[i]->id);
3965}
3966
3967/* Notify on "monitor other" change (direct monitor, speaker
3968 * switching, talkback)
3969 */
3970static void scarlett2_notify_monitor_other(
3971 struct usb_mixer_interface *mixer)
3972{
3973 struct snd_card *card = mixer->chip->card;
3974 struct scarlett2_data *private = mixer->private_data;
3975 const struct scarlett2_device_info *info = private->info;
3976
3977 private->monitor_other_updated = 1;
3978
3979 if (info->direct_monitor) {
3980 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE,
3981 &private->direct_monitor_ctl->id);
3982 return;
3983 }
3984
3985 if (info->has_speaker_switching)
3986 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE,
3987 &private->speaker_switching_ctl->id);
3988
3989 if (info->has_talkback)
3990 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE,
3991 &private->talkback_ctl->id);
3992
3993 /* if speaker switching was recently enabled or disabled,
3994 * invalidate the dim/mute and mux enum controls
3995 */
3996 if (private->speaker_switching_switched) {
3997 int i;
3998
3999 scarlett2_notify_dim_mute(mixer);
4000
4001 private->speaker_switching_switched = 0;
4002 private->mux_updated = 1;
4003
4004 for (i = 0; i < private->num_mux_dsts; i++)
4005 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE,
4006 &private->mux_ctls[i]->id);
4007 }
4008}
4009
4010/* Interrupt callback */
4011static void scarlett2_notify(struct urb *urb)
4012{
4013 struct usb_mixer_interface *mixer = urb->context;
4014 int len = urb->actual_length;
4015 int ustatus = urb->status;
4016 u32 data;
4017
4018 if (ustatus != 0 || len != 8)
4019 goto requeue;
4020
4021 data = le32_to_cpu(*(__le32 *)urb->transfer_buffer);
4022 if (data & SCARLETT2_USB_NOTIFY_SYNC)
4023 scarlett2_notify_sync(mixer);
4024 if (data & SCARLETT2_USB_NOTIFY_MONITOR)
4025 scarlett2_notify_monitor(mixer);
4026 if (data & SCARLETT2_USB_NOTIFY_DIM_MUTE)
4027 scarlett2_notify_dim_mute(mixer);
4028 if (data & SCARLETT2_USB_NOTIFY_INPUT_OTHER)
4029 scarlett2_notify_input_other(mixer);
4030 if (data & SCARLETT2_USB_NOTIFY_MONITOR_OTHER)
4031 scarlett2_notify_monitor_other(mixer);
4032
4033requeue:
4034 if (ustatus != -ENOENT &&
4035 ustatus != -ECONNRESET &&
4036 ustatus != -ESHUTDOWN) {
4037 urb->dev = mixer->chip->dev;
4038 usb_submit_urb(urb, GFP_ATOMIC);
4039 }
4040}
4041
4042static int scarlett2_init_notify(struct usb_mixer_interface *mixer)
4043{
4044 struct usb_device *dev = mixer->chip->dev;
4045 struct scarlett2_data *private = mixer->private_data;
4046 unsigned int pipe = usb_rcvintpipe(dev, private->bEndpointAddress);
4047 void *transfer_buffer;
4048
4049 if (mixer->urb) {
4050 usb_audio_err(mixer->chip,
4051 "%s: mixer urb already in use!\n", __func__);
4052 return 0;
4053 }
4054
4055 if (usb_pipe_type_check(dev, pipe))
4056 return -EINVAL;
4057
4058 mixer->urb = usb_alloc_urb(0, GFP_KERNEL);
4059 if (!mixer->urb)
4060 return -ENOMEM;
4061
4062 transfer_buffer = kmalloc(private->wMaxPacketSize, GFP_KERNEL);
4063 if (!transfer_buffer)
4064 return -ENOMEM;
4065
4066 usb_fill_int_urb(mixer->urb, dev, pipe,
4067 transfer_buffer, private->wMaxPacketSize,
4068 scarlett2_notify, mixer, private->bInterval);
4069
4070 return usb_submit_urb(mixer->urb, GFP_KERNEL);
4071}
4072
4073static int snd_scarlett_gen2_controls_create(struct usb_mixer_interface *mixer)
4074{
4075 const struct scarlett2_device_info **info = scarlett2_devices;
4076 int err;
4077
4078 /* Find device in scarlett2_devices */
4079 while (*info && (*info)->usb_id != mixer->chip->usb_id)
4080 info++;
4081 if (!*info)
4082 return -EINVAL;
4083
4084 /* Initialise private data */
4085 err = scarlett2_init_private(mixer, *info);
4086 if (err < 0)
4087 return err;
4088
4089 /* Send proprietary USB initialisation sequence */
4090 err = scarlett2_usb_init(mixer);
4091 if (err < 0)
4092 return err;
4093
4094 /* Read volume levels and controls from the interface */
4095 err = scarlett2_read_configs(mixer);
4096 if (err < 0)
4097 return err;
4098
4099 /* Create the MSD control */
4100 err = scarlett2_add_msd_ctl(mixer);
4101 if (err < 0)
4102 return err;
4103
4104 /* If MSD mode is enabled, don't create any other controls */
4105 if (((struct scarlett2_data *)mixer->private_data)->msd_switch)
4106 return 0;
4107
4108 /* Create the analogue output controls */
4109 err = scarlett2_add_line_out_ctls(mixer);
4110 if (err < 0)
4111 return err;
4112
4113 /* Create the analogue input controls */
4114 err = scarlett2_add_line_in_ctls(mixer);
4115 if (err < 0)
4116 return err;
4117
4118 /* Create the input, output, and mixer mux input selections */
4119 err = scarlett2_add_mux_enums(mixer);
4120 if (err < 0)
4121 return err;
4122
4123 /* Create the matrix mixer controls */
4124 err = scarlett2_add_mixer_ctls(mixer);
4125 if (err < 0)
4126 return err;
4127
4128 /* Create the level meter controls */
4129 err = scarlett2_add_meter_ctl(mixer);
4130 if (err < 0)
4131 return err;
4132
4133 /* Create the sync control */
4134 err = scarlett2_add_sync_ctl(mixer);
4135 if (err < 0)
4136 return err;
4137
4138 /* Create the direct monitor control */
4139 err = scarlett2_add_direct_monitor_ctl(mixer);
4140 if (err < 0)
4141 return err;
4142
4143 /* Create the speaker switching control */
4144 err = scarlett2_add_speaker_switch_ctl(mixer);
4145 if (err < 0)
4146 return err;
4147
4148 /* Create the talkback controls */
4149 err = scarlett2_add_talkback_ctls(mixer);
4150 if (err < 0)
4151 return err;
4152
4153 /* Create the standalone control */
4154 err = scarlett2_add_standalone_ctl(mixer);
4155 if (err < 0)
4156 return err;
4157
4158 /* Set up the interrupt polling */
4159 err = scarlett2_init_notify(mixer);
4160 if (err < 0)
4161 return err;
4162
4163 return 0;
4164}
4165
4166int snd_scarlett_gen2_init(struct usb_mixer_interface *mixer)
4167{
4168 struct snd_usb_audio *chip = mixer->chip;
4169 int err;
4170
4171 /* only use UAC_VERSION_2 */
4172 if (!mixer->protocol)
4173 return 0;
4174
4175 if (!(chip->setup & SCARLETT2_ENABLE)) {
4176 usb_audio_info(chip,
4177 "Focusrite Scarlett Gen 2/3 Mixer Driver disabled; "
4178 "use options snd_usb_audio vid=0x%04x pid=0x%04x "
4179 "device_setup=1 to enable and report any issues "
4180 "to g@b4.vu",
4181 USB_ID_VENDOR(chip->usb_id),
4182 USB_ID_PRODUCT(chip->usb_id));
4183 return 0;
4184 }
4185
4186 usb_audio_info(chip,
4187 "Focusrite Scarlett Gen 2/3 Mixer Driver enabled pid=0x%04x",
4188 USB_ID_PRODUCT(chip->usb_id));
4189
4190 err = snd_scarlett_gen2_controls_create(mixer);
4191 if (err < 0)
4192 usb_audio_err(mixer->chip,
4193 "Error initialising Scarlett Mixer Driver: %d",
4194 err);
4195
4196 return err;
4197}