1 // SPDX-License-Identifier: GPL-2.0
3 * Focusrite Scarlett Gen 2/3 and Clarett+ Driver for ALSA
6 * - 6i6/18i8/18i20 Gen 2
7 * - Solo/2i2/4i4/8i6/18i8/18i20 Gen 3
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>
14 * Based on the Scarlett (Gen 1) Driver for ALSA:
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>
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)
26 * David Henningsson <david.henningsson at canonical.com>
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.
33 * Scarlett 18i8 support added in April 2019.
35 * Scarlett 6i6 support added in June 2019 (thanks to Martin Wittmann
36 * for providing usbmon output and testing).
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).
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).
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).
50 * Support for Solo/2i2 Gen 3 added in May 2021 (thanks to Alexander
51 * Vorona for 2i2 protocol traces).
53 * Support for phantom power, direct monitoring, speaker switching,
54 * and talkback added in May-June 2021.
56 * Support for Clarett+ 8Pre added in Aug 2022 by Christian
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
64 * - line/inst level, pad, and air controls
65 * - phantom power, direct monitor, speaker switching, and talkback
67 * - disable/enable MSD mode
68 * - disable/enable standalone mode
71 * /--------------\ 18chn 20chn /--------------\
72 * | Hardware in +--+------\ /-------------+--+ ALSA PCM out |
73 * \--------------/ | | | | \--------------/
77 * | +---------------+ | |
78 * | \ Matrix Mux / | |
85 * | +------------+ | |
89 * | | 18x10 Gain | | |
91 * | +-----+------+ | |
93 * |18chn |10chn | |20chn
98 * ===========================
99 * +---------------+ +--—------------+
100 * \ Output Mux / \ Capture Mux /
101 * +---+---+---+ +-----+-----+
105 * /--------------\ | | | /--------------\
106 * | S/PDIF, ADAT |<--/ |10chn \-->| ALSA PCM in |
107 * | Hardware out | | \--------------/
110 * +-------------+ Software gain per channel.
111 * | Master Gain |<-- 18i20 only: Switch per channel
112 * +------+------+ to select HW or SW gain control.
116 * | Analogue |<------/
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
127 * - using this driver and alsamixer to change the "MSD Mode" setting
128 * to Off and power-cycling the device
131 #include <linux/slab.h>
132 #include <linux/usb.h>
133 #include <linux/moduleparam.h>
135 #include <sound/control.h>
136 #include <sound/tlv.h>
138 #include "usbaudio.h"
142 #include "mixer_scarlett_gen2.h"
144 /* device_setup value to enable */
145 #define SCARLETT2_ENABLE 0x01
147 /* device_setup value to allow turning MSD mode back on */
148 #define SCARLETT2_MSD_ENABLE 0x02
150 /* some gui mixers can't handle negative ctl values */
151 #define SCARLETT2_VOLUME_BIAS 127
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)
161 /* map from (dB + 80) * 2 to mixer value
162 * for dB in 0 .. 172: int(8192 * pow(10, ((dB - 160) / 2 / 20)))
164 static 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,
181 /* Maximum number of analogue outputs */
182 #define SCARLETT2_ANALOGUE_MAX 10
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
190 /* Maximum number of inputs to the mixer */
191 #define SCARLETT2_INPUT_MIX_MAX 25
193 /* Maximum number of outputs from the mixer */
194 #define SCARLETT2_OUTPUT_MIX_MAX 12
196 /* Maximum size of the data in the USB mux assignment message:
197 * 20 inputs, 20 outputs, 25 matrix inputs, 12 spare
199 #define SCARLETT2_MUX_MAX 77
201 /* Maximum number of meters (sum of output port counts) */
202 #define SCARLETT2_MAX_METERS 65
204 /* There are three different sets of configuration parameters across
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
215 /* Hardware port types:
216 * - None (no input to mux)
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,
233 /* I/O count of each port type kept in struct scarlett2_ports */
235 SCARLETT2_PORT_IN = 0,
236 SCARLETT2_PORT_OUT = 1,
237 SCARLETT2_PORT_DIRNS = 2,
240 /* Dim/Mute buttons on the 18i20 */
242 SCARLETT2_BUTTON_MUTE = 0,
243 SCARLETT2_BUTTON_DIM = 1,
244 SCARLETT2_DIM_MUTE_COUNT = 2,
247 static const char *const scarlett2_dim_mute_names[SCARLETT2_DIM_MUTE_COUNT] = {
248 "Mute Playback Switch", "Dim Playback Switch"
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
257 struct scarlett2_port {
259 const char * const src_descr;
261 const char * const dst_descr;
264 static const struct scarlett2_port scarlett2_ports[SCARLETT2_PORT_TYPE_COUNT] = {
265 [SCARLETT2_PORT_TYPE_NONE] = {
269 [SCARLETT2_PORT_TYPE_ANALOGUE] = {
271 .src_descr = "Analogue %d",
273 .dst_descr = "Analogue Output %02d Playback"
275 [SCARLETT2_PORT_TYPE_SPDIF] = {
277 .src_descr = "S/PDIF %d",
279 .dst_descr = "S/PDIF Output %d Playback"
281 [SCARLETT2_PORT_TYPE_ADAT] = {
283 .src_descr = "ADAT %d",
285 .dst_descr = "ADAT Output %d Playback"
287 [SCARLETT2_PORT_TYPE_MIX] = {
289 .src_descr = "Mix %c",
290 .src_num_offset = 'A',
291 .dst_descr = "Mixer Input %02d Capture"
293 [SCARLETT2_PORT_TYPE_PCM] = {
295 .src_descr = "PCM %d",
297 .dst_descr = "PCM %02d Capture"
301 /* Number of mux tables: one for each band of sample rates
302 * (44.1/48kHz, 88.2/96kHz, and 176.4/176kHz)
304 #define SCARLETT2_MUX_TABLES 3
306 /* Maximum number of entries in a mux table */
307 #define SCARLETT2_MAX_MUX_ENTRIES 10
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
313 struct scarlett2_mux_entry {
319 struct scarlett2_device_info {
320 u32 usb_id; /* USB device identifier */
322 /* Gen 3 devices have an internal MSD mode switch that needs
323 * to be disabled in order to access the full functionality of
328 /* which set of configuration parameters the device uses */
331 /* line out hw volume is sw controlled */
334 /* support for main/alt speaker switching */
335 u8 has_speaker_switching;
337 /* support for talkback microphone */
340 /* the number of analogue inputs with a software switchable
341 * level control that can be set to line or instrument
343 u8 level_input_count;
345 /* the first input with a level control (0-based) */
346 u8 level_input_first;
348 /* the number of analogue inputs with a software switchable
353 /* the number of analogue inputs with a software switchable
358 /* the number of phantom (48V) software switchable controls */
361 /* the number of inputs each phantom switch controls */
362 u8 inputs_per_phantom;
364 /* the number of direct monitor options
365 * (0 = none, 1 = mono only, 2 = mono/stereo)
369 /* remap analogue outputs; 18i8 Gen 3 has "line 3/4" connected
370 * internally to the analogue 7/8 outputs
372 u8 line_out_remap_enable;
373 u8 line_out_remap[SCARLETT2_ANALOGUE_MAX];
375 /* additional description for the line out volume controls */
376 const char * const line_out_descrs[SCARLETT2_ANALOGUE_MAX];
378 /* number of sources/destinations of each port type */
379 const int port_count[SCARLETT2_PORT_TYPE_COUNT][SCARLETT2_PORT_DIRNS];
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];
386 struct 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;
401 u8 input_other_updated;
402 u8 monitor_other_updated;
404 u8 speaker_switching_switched;
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;
419 u8 talkback_map[SCARLETT2_OUTPUT_MIX_MAX];
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];
440 /*** Model-specific data ***/
442 static const struct scarlett2_device_info s6i6_gen2_info = {
443 .usb_id = USB_ID(0x1235, 0x8203),
445 .config_set = SCARLETT2_CONFIG_SET_GEN_2,
446 .level_input_count = 2,
447 .pad_input_count = 2,
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 },
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 },
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 },
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 },
488 static const struct scarlett2_device_info s18i8_gen2_info = {
489 .usb_id = USB_ID(0x1235, 0x8204),
491 .config_set = SCARLETT2_CONFIG_SET_GEN_2,
492 .level_input_count = 2,
493 .pad_input_count = 4,
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 },
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 },
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 },
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 },
537 static const struct scarlett2_device_info s18i20_gen2_info = {
538 .usb_id = USB_ID(0x1235, 0x8201),
540 .config_set = SCARLETT2_CONFIG_SET_GEN_2,
541 .line_out_hw_vol = 1,
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 },
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 },
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 },
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 },
591 static const struct scarlett2_device_info solo_gen3_info = {
592 .usb_id = USB_ID(0x1235, 0x8211),
595 .config_set = SCARLETT2_CONFIG_SET_NO_MIXER,
596 .level_input_count = 1,
597 .level_input_first = 1,
598 .air_input_count = 1,
600 .inputs_per_phantom = 1,
604 static const struct scarlett2_device_info s2i2_gen3_info = {
605 .usb_id = USB_ID(0x1235, 0x8210),
608 .config_set = SCARLETT2_CONFIG_SET_NO_MIXER,
609 .level_input_count = 2,
610 .air_input_count = 2,
612 .inputs_per_phantom = 2,
616 static const struct scarlett2_device_info s4i4_gen3_info = {
617 .usb_id = USB_ID(0x1235, 0x8212),
620 .config_set = SCARLETT2_CONFIG_SET_GEN_3,
621 .level_input_count = 2,
622 .pad_input_count = 2,
623 .air_input_count = 2,
625 .inputs_per_phantom = 2,
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 },
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 },
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 },
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 },
662 static const struct scarlett2_device_info s8i6_gen3_info = {
663 .usb_id = USB_ID(0x1235, 0x8213),
666 .config_set = SCARLETT2_CONFIG_SET_GEN_3,
667 .level_input_count = 2,
668 .pad_input_count = 2,
669 .air_input_count = 2,
671 .inputs_per_phantom = 2,
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 },
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 },
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 },
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 },
715 static const struct scarlett2_device_info s18i8_gen3_info = {
716 .usb_id = USB_ID(0x1235, 0x8214),
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,
726 .inputs_per_phantom = 2,
728 .line_out_remap_enable = 1,
729 .line_out_remap = { 0, 1, 6, 7, 2, 3, 4, 5 },
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 },
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 },
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 },
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 },
785 static const struct scarlett2_device_info s18i20_gen3_info = {
786 .usb_id = USB_ID(0x1235, 0x8215),
789 .config_set = SCARLETT2_CONFIG_SET_GEN_3,
790 .line_out_hw_vol = 1,
791 .has_speaker_switching = 1,
793 .level_input_count = 2,
794 .pad_input_count = 8,
795 .air_input_count = 8,
797 .inputs_per_phantom = 4,
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 },
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 },
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 },
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 },
850 static const struct scarlett2_device_info clarett_8pre_info = {
851 .usb_id = USB_ID(0x1235, 0x820c),
853 .config_set = SCARLETT2_CONFIG_SET_CLARETT,
854 .line_out_hw_vol = 1,
855 .level_input_count = 2,
856 .air_input_count = 8,
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 },
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 },
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 },
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 },
905 static const struct scarlett2_device_info *scarlett2_devices[] = {
906 /* Supported Gen 2 devices */
911 /* Supported Gen 3 devices */
919 /* Supported Clarett+ devices */
926 /* get the starting port index number for a given port type/direction */
927 static int scarlett2_get_port_start_num(
928 const int port_count[][SCARLETT2_PORT_DIRNS],
929 int direction, int port_type)
933 for (i = 0; i < port_type; i++)
934 num += port_count[i][direction];
939 /*** USB Interactions ***/
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
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
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
965 #define SCARLETT2_USB_CONFIG_SAVE 6
967 #define SCARLETT2_USB_VOLUME_STATUS_OFFSET 0x31
968 #define SCARLETT2_USB_METER_LEVELS_GET_MAGIC 1
970 /* volume status is read together (matches scarlett2_config_items[1]) */
971 struct scarlett2_usb_volume_status {
972 /* dim/mute buttons */
973 u8 dim_mute[SCARLETT2_DIM_MUTE_COUNT];
977 /* software volume setting */
978 s16 sw_vol[SCARLETT2_ANALOGUE_MAX];
980 /* actual volume of output inc. dim (-18dB) */
981 s16 hw_vol[SCARLETT2_ANALOGUE_MAX];
983 /* internal mute buttons */
984 u8 mute_switch[SCARLETT2_ANALOGUE_MAX];
986 /* sw (0) or hw (1) controlled */
987 u8 sw_hw_switch[SCARLETT2_ANALOGUE_MAX];
991 /* front panel volume knob */
995 /* Configuration parameters that can be read and written */
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
1015 /* Location, size, and activation command number for the configuration
1016 * parameters. Size is in bits and may be 1, 8, or 16.
1018 struct scarlett2_config {
1024 static const struct scarlett2_config
1025 scarlett2_config_items[SCARLETT2_CONFIG_SET_COUNT]
1026 [SCARLETT2_CONFIG_COUNT] =
1028 /* Devices without a mixer (Gen 3 Solo and 2i2) */
1030 [SCARLETT2_CONFIG_MSD_SWITCH] = {
1031 .offset = 0x04, .size = 8, .activate = 6 },
1033 [SCARLETT2_CONFIG_PHANTOM_PERSISTENCE] = {
1034 .offset = 0x05, .size = 8, .activate = 6 },
1036 [SCARLETT2_CONFIG_PHANTOM_SWITCH] = {
1037 .offset = 0x06, .size = 8, .activate = 3 },
1039 [SCARLETT2_CONFIG_DIRECT_MONITOR] = {
1040 .offset = 0x07, .size = 8, .activate = 4 },
1042 [SCARLETT2_CONFIG_LEVEL_SWITCH] = {
1043 .offset = 0x08, .size = 1, .activate = 7 },
1045 [SCARLETT2_CONFIG_AIR_SWITCH] = {
1046 .offset = 0x09, .size = 1, .activate = 8 },
1048 /* Gen 2 devices: 6i6, 18i8, 18i20 */
1050 [SCARLETT2_CONFIG_DIM_MUTE] = {
1051 .offset = 0x31, .size = 8, .activate = 2 },
1053 [SCARLETT2_CONFIG_LINE_OUT_VOLUME] = {
1054 .offset = 0x34, .size = 16, .activate = 1 },
1056 [SCARLETT2_CONFIG_MUTE_SWITCH] = {
1057 .offset = 0x5c, .size = 8, .activate = 1 },
1059 [SCARLETT2_CONFIG_SW_HW_SWITCH] = {
1060 .offset = 0x66, .size = 8, .activate = 3 },
1062 [SCARLETT2_CONFIG_LEVEL_SWITCH] = {
1063 .offset = 0x7c, .size = 8, .activate = 7 },
1065 [SCARLETT2_CONFIG_PAD_SWITCH] = {
1066 .offset = 0x84, .size = 8, .activate = 8 },
1068 [SCARLETT2_CONFIG_STANDALONE_SWITCH] = {
1069 .offset = 0x8d, .size = 8, .activate = 6 },
1071 /* Gen 3 devices: 4i4, 8i6, 18i8, 18i20 */
1073 [SCARLETT2_CONFIG_DIM_MUTE] = {
1074 .offset = 0x31, .size = 8, .activate = 2 },
1076 [SCARLETT2_CONFIG_LINE_OUT_VOLUME] = {
1077 .offset = 0x34, .size = 16, .activate = 1 },
1079 [SCARLETT2_CONFIG_MUTE_SWITCH] = {
1080 .offset = 0x5c, .size = 8, .activate = 1 },
1082 [SCARLETT2_CONFIG_SW_HW_SWITCH] = {
1083 .offset = 0x66, .size = 8, .activate = 3 },
1085 [SCARLETT2_CONFIG_LEVEL_SWITCH] = {
1086 .offset = 0x7c, .size = 8, .activate = 7 },
1088 [SCARLETT2_CONFIG_PAD_SWITCH] = {
1089 .offset = 0x84, .size = 8, .activate = 8 },
1091 [SCARLETT2_CONFIG_AIR_SWITCH] = {
1092 .offset = 0x8c, .size = 8, .activate = 8 },
1094 [SCARLETT2_CONFIG_STANDALONE_SWITCH] = {
1095 .offset = 0x95, .size = 8, .activate = 6 },
1097 [SCARLETT2_CONFIG_PHANTOM_SWITCH] = {
1098 .offset = 0x9c, .size = 1, .activate = 8 },
1100 [SCARLETT2_CONFIG_MSD_SWITCH] = {
1101 .offset = 0x9d, .size = 8, .activate = 6 },
1103 [SCARLETT2_CONFIG_PHANTOM_PERSISTENCE] = {
1104 .offset = 0x9e, .size = 8, .activate = 6 },
1106 [SCARLETT2_CONFIG_MONITOR_OTHER_SWITCH] = {
1107 .offset = 0x9f, .size = 1, .activate = 10 },
1109 [SCARLETT2_CONFIG_MONITOR_OTHER_ENABLE] = {
1110 .offset = 0xa0, .size = 1, .activate = 10 },
1112 [SCARLETT2_CONFIG_TALKBACK_MAP] = {
1113 .offset = 0xb0, .size = 16, .activate = 10 },
1117 [SCARLETT2_CONFIG_DIM_MUTE] = {
1118 .offset = 0x31, .size = 8, .activate = 2 },
1120 [SCARLETT2_CONFIG_LINE_OUT_VOLUME] = {
1121 .offset = 0x34, .size = 16, .activate = 1 },
1123 [SCARLETT2_CONFIG_MUTE_SWITCH] = {
1124 .offset = 0x5c, .size = 8, .activate = 1 },
1126 [SCARLETT2_CONFIG_SW_HW_SWITCH] = {
1127 .offset = 0x66, .size = 8, .activate = 3 },
1129 [SCARLETT2_CONFIG_LEVEL_SWITCH] = {
1130 .offset = 0x7c, .size = 8, .activate = 7 },
1132 [SCARLETT2_CONFIG_AIR_SWITCH] = {
1133 .offset = 0x95, .size = 8, .activate = 8 },
1135 [SCARLETT2_CONFIG_STANDALONE_SWITCH] = {
1136 .offset = 0x8d, .size = 8, .activate = 6 },
1139 /* proprietary request/response format */
1140 struct scarlett2_usb_packet {
1149 static void scarlett2_fill_request_header(struct scarlett2_data *private,
1150 struct scarlett2_usb_packet *req,
1151 u32 cmd, u16 req_size)
1153 /* sequence must go up by 1 for each request */
1154 u16 seq = private->scarlett2_seq++;
1156 req->cmd = cpu_to_le32(cmd);
1157 req->size = cpu_to_le16(req_size);
1158 req->seq = cpu_to_le16(seq);
1163 static int scarlett2_usb_tx(struct usb_device *dev, int interface,
1164 void *buf, u16 size)
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);
1172 static int scarlett2_usb_rx(struct usb_device *dev, int interface,
1173 u32 usb_req, void *buf, u16 size)
1175 return snd_usb_ctl_msg(dev, usb_rcvctrlpipe(dev, 0),
1177 USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_IN,
1178 0, interface, buf, size);
1181 /* Send a proprietary format request to the Scarlett interface */
1182 static int scarlett2_usb(
1183 struct usb_mixer_interface *mixer, u32 cmd,
1184 void *req_data, u16 req_size, void *resp_data, u16 resp_size)
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);
1193 req = kmalloc(req_buf_size, GFP_KERNEL);
1199 resp = kmalloc(resp_buf_size, GFP_KERNEL);
1205 mutex_lock(&private->usb_mutex);
1207 /* build request message and send it */
1209 scarlett2_fill_request_header(private, req, cmd, req_size);
1212 memcpy(req->data, req_data, req_size);
1214 err = scarlett2_usb_tx(dev, private->bInterfaceNumber,
1217 if (err != req_buf_size) {
1220 "Scarlett Gen 2/3 USB request result cmd %x was %d\n",
1226 /* send a second message to get the response */
1228 err = scarlett2_usb_rx(dev, private->bInterfaceNumber,
1229 SCARLETT2_USB_CMD_RESP,
1230 resp, resp_buf_size);
1232 /* validate the response */
1234 if (err != resp_buf_size) {
1237 "Scarlett Gen 2/3 USB response result cmd %x was %d "
1239 cmd, err, resp_buf_size);
1244 /* cmd/seq/size should match except when initialising
1245 * seq sent = 1, response = 0
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) ||
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));
1267 if (resp_data && resp_size > 0)
1268 memcpy(resp_data, resp->data, resp_size);
1271 mutex_unlock(&private->usb_mutex);
1278 /* Send a USB message to get data; result placed in *buf */
1279 static int scarlett2_usb_get(
1280 struct usb_mixer_interface *mixer,
1281 int offset, void *buf, int size)
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);
1294 /* Send a USB message to get configuration parameters; result placed in *buf */
1295 static int scarlett2_usb_get_config(
1296 struct usb_mixer_interface *mixer,
1297 int config_item_num, int count, void *buf)
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];
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);
1316 for (i = 0; i < count; i++, buf_16++)
1317 *buf_16 = le16_to_cpu(*(__le16 *)buf_16);
1322 /* For bit-sized parameters, retrieve into value */
1323 err = scarlett2_usb_get(mixer, config_item->offset, &value, 1);
1327 /* then unpack from value into buf[] */
1329 for (i = 0; i < 8 && i < count; i++, value >>= 1)
1330 *buf_8++ = value & 1;
1335 /* Send SCARLETT2_USB_DATA_CMD SCARLETT2_USB_CONFIG_SAVE */
1336 static void scarlett2_config_save(struct usb_mixer_interface *mixer)
1338 __le32 req = cpu_to_le32(SCARLETT2_USB_CONFIG_SAVE);
1340 scarlett2_usb(mixer, SCARLETT2_USB_DATA_CMD,
1345 /* Delayed work to save config */
1346 static void scarlett2_config_save_work(struct work_struct *work)
1348 struct scarlett2_data *private =
1349 container_of(work, struct scarlett2_data, work.work);
1351 scarlett2_config_save(private->mixer);
1354 /* Send a USB message to set a SCARLETT2_CONFIG_* parameter */
1355 static int scarlett2_usb_set_config(
1356 struct usb_mixer_interface *mixer,
1357 int config_item_num, int index, int value)
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];
1372 /* Cancel any pending NVRAM save */
1373 cancel_delayed_work_sync(&private->work);
1375 /* Convert config_item->size in bits to size in bytes and
1378 if (config_item->size >= 8) {
1379 size = config_item->size / 8;
1380 offset = config_item->offset + index * size;
1382 /* If updating a bit, retrieve the old value, set/clear the
1383 * bit as needed, and update value
1389 offset = config_item->offset;
1391 scarlett2_usb_get(mixer, offset, &tmp, 1);
1393 tmp |= (1 << index);
1395 tmp &= ~(1 << index);
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,
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);
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));
1424 /* Send a USB message to get sync status; result placed in *sync */
1425 static int scarlett2_usb_get_sync_status(
1426 struct usb_mixer_interface *mixer,
1432 err = scarlett2_usb(mixer, SCARLETT2_USB_GET_SYNC,
1433 NULL, 0, &data, sizeof(data));
1441 /* Send a USB message to get volume status; result placed in *buf */
1442 static int scarlett2_usb_get_volume_status(
1443 struct usb_mixer_interface *mixer,
1444 struct scarlett2_usb_volume_status *buf)
1446 return scarlett2_usb_get(mixer, SCARLETT2_USB_VOLUME_STATUS_OFFSET,
1450 /* Send a USB message to get the volumes for all inputs of one mix
1451 * and put the values into private->mix[]
1453 static int scarlett2_usb_get_mix(struct usb_mixer_interface *mixer,
1456 struct scarlett2_data *private = mixer->private_data;
1457 const struct scarlett2_device_info *info = private->info;
1460 info->port_count[SCARLETT2_PORT_TYPE_MIX][SCARLETT2_PORT_OUT];
1468 __le16 data[SCARLETT2_INPUT_MIX_MAX];
1470 req.mix_num = cpu_to_le16(mix_num);
1471 req.count = cpu_to_le16(num_mixer_in);
1473 err = scarlett2_usb(mixer, SCARLETT2_USB_GET_MIX,
1475 data, num_mixer_in * sizeof(u16));
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]);
1482 for (k = 0; k < SCARLETT2_MIXER_VALUE_COUNT; k++)
1483 if (scarlett2_mixer_values[k] >= mixer_value)
1485 if (k == SCARLETT2_MIXER_VALUE_COUNT)
1486 k = SCARLETT2_MIXER_MAX_VALUE;
1487 private->mix[j] = k;
1493 /* Send a USB message to set the volumes for all inputs of one mix
1494 * (values obtained from private->mix[])
1496 static int scarlett2_usb_set_mix(struct usb_mixer_interface *mixer,
1499 struct scarlett2_data *private = mixer->private_data;
1500 const struct scarlett2_device_info *info = private->info;
1504 __le16 data[SCARLETT2_INPUT_MIX_MAX];
1509 info->port_count[SCARLETT2_PORT_TYPE_MIX][SCARLETT2_PORT_OUT];
1511 req.mix_num = cpu_to_le16(mix_num);
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]]
1518 return scarlett2_usb(mixer, SCARLETT2_USB_SET_MIX,
1519 &req, (num_mixer_in + 1) * sizeof(u16),
1523 /* Convert a port number index (per info->port_count) to a hardware ID */
1524 static u32 scarlett2_mux_src_num_to_id(
1525 const int port_count[][SCARLETT2_PORT_DIRNS], int num)
1530 port_type < SCARLETT2_PORT_TYPE_COUNT;
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];
1541 /* Convert a hardware ID to a port number index */
1542 static u32 scarlett2_mux_id_to_num(
1543 const int port_count[][SCARLETT2_PORT_DIRNS], int direction, u32 id)
1549 port_type < SCARLETT2_PORT_TYPE_COUNT;
1551 int base = scarlett2_ports[port_type].id;
1552 int count = port_count[port_type][direction];
1554 if (id >= base && id < base + count)
1555 return port_num + id - base;
1563 /* Convert one mux entry from the interface and load into private->mux[] */
1564 static void scarlett2_usb_populate_mux(struct scarlett2_data *private,
1567 const struct scarlett2_device_info *info = private->info;
1568 const int (*port_count)[SCARLETT2_PORT_DIRNS] = info->port_count;
1570 int dst_idx, src_idx;
1572 dst_idx = scarlett2_mux_id_to_num(port_count, SCARLETT2_PORT_OUT,
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);
1584 src_idx = scarlett2_mux_id_to_num(port_count, SCARLETT2_PORT_IN,
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);
1596 private->mux[dst_idx] = src_idx;
1599 /* Send USB message to get mux inputs and then populate private->mux[] */
1600 static int scarlett2_usb_get_mux(struct usb_mixer_interface *mixer)
1602 struct scarlett2_data *private = mixer->private_data;
1603 int count = private->num_mux_dsts;
1611 __le32 data[SCARLETT2_MUX_MAX];
1613 private->mux_updated = 0;
1616 req.count = cpu_to_le16(count);
1618 err = scarlett2_usb(mixer, SCARLETT2_USB_GET_MUX,
1620 data, count * sizeof(u32));
1624 for (i = 0; i < count; i++)
1625 scarlett2_usb_populate_mux(private, le32_to_cpu(data[i]));
1630 /* Send USB messages to set mux inputs */
1631 static int scarlett2_usb_set_mux(struct usb_mixer_interface *mixer)
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;
1641 __le32 data[SCARLETT2_MUX_MAX];
1646 /* set mux settings for each rate */
1647 for (table = 0; table < SCARLETT2_MUX_TABLES; table++) {
1648 const struct scarlett2_mux_entry *entry;
1650 /* i counts over the output array */
1653 req.num = cpu_to_le16(table);
1655 /* loop through each entry */
1656 for (entry = info->mux_assignment[table];
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;
1668 for (j = 0; j < entry->count; j++)
1673 /* Non-empty mux slots use the lower 12 bits
1674 * for the destination and next 12 bits for
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 |
1686 err = scarlett2_usb(mixer, SCARLETT2_USB_SET_MUX,
1687 &req, (i + 1) * sizeof(u32),
1696 /* Send USB message to get meter levels */
1697 static int scarlett2_usb_get_meter_levels(struct usb_mixer_interface *mixer,
1698 u16 num_meters, u16 *levels)
1705 u32 resp[SCARLETT2_MAX_METERS];
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));
1716 /* copy, convert to u16 */
1717 for (i = 0; i < num_meters; i++)
1718 levels[i] = resp[i];
1723 /*** Control Functions ***/
1725 /* helper function to create a new control */
1726 static 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)
1731 struct snd_kcontrol *kctl;
1732 struct usb_mixer_elem_info *elem;
1735 elem = kzalloc(sizeof(*elem), GFP_KERNEL);
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.
1743 elem->head.mixer = mixer;
1744 elem->control = index;
1746 elem->channels = channels;
1747 elem->val_type = USB_MIXER_BESPOKEN;
1749 kctl = snd_ctl_new1(ncontrol, elem);
1754 kctl->private_free = snd_usb_mixer_elem_free;
1756 strscpy(kctl->id.name, name, sizeof(kctl->id.name));
1758 err = snd_usb_mixer_add_control(&elem->head, kctl);
1763 *kctl_return = kctl;
1768 /*** Sync Control ***/
1770 /* Update sync control after receiving notification that the status
1773 static int scarlett2_update_sync(struct usb_mixer_interface *mixer)
1775 struct scarlett2_data *private = mixer->private_data;
1777 private->sync_updated = 0;
1778 return scarlett2_usb_get_sync_status(mixer, &private->sync);
1781 static int scarlett2_sync_ctl_info(struct snd_kcontrol *kctl,
1782 struct snd_ctl_elem_info *uinfo)
1784 static const char *texts[2] = {
1785 "Unlocked", "Locked"
1787 return snd_ctl_enum_info(uinfo, 1, 2, texts);
1790 static int scarlett2_sync_ctl_get(struct snd_kcontrol *kctl,
1791 struct snd_ctl_elem_value *ucontrol)
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;
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);
1806 static const struct snd_kcontrol_new scarlett2_sync_ctl = {
1807 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1808 .access = SNDRV_CTL_ELEM_ACCESS_READ,
1810 .info = scarlett2_sync_ctl_info,
1811 .get = scarlett2_sync_ctl_get
1814 static int scarlett2_add_sync_ctl(struct usb_mixer_interface *mixer)
1816 struct scarlett2_data *private = mixer->private_data;
1818 /* devices without a mixer also don't support reporting sync status */
1819 if (private->info->config_set == SCARLETT2_CONFIG_SET_NO_MIXER)
1822 return scarlett2_add_new_ctl(mixer, &scarlett2_sync_ctl,
1823 0, 1, "Sync Status", &private->sync_ctl);
1826 /*** Analogue Line Out Volume Controls ***/
1828 /* Update hardware volume controls after receiving notification that
1831 static int scarlett2_update_volumes(struct usb_mixer_interface *mixer)
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;
1838 port_count[SCARLETT2_PORT_TYPE_ANALOGUE][SCARLETT2_PORT_OUT];
1842 private->vol_updated = 0;
1844 err = scarlett2_usb_get_volume_status(mixer, &volume_status);
1848 private->master_vol = clamp(
1849 volume_status.master_vol + SCARLETT2_VOLUME_BIAS,
1850 0, SCARLETT2_VOLUME_BIAS);
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];
1856 mute = private->dim_mute[SCARLETT2_BUTTON_MUTE];
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;
1867 static int scarlett2_volume_ctl_info(struct snd_kcontrol *kctl,
1868 struct snd_ctl_elem_info *uinfo)
1870 struct usb_mixer_elem_info *elem = kctl->private_data;
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;
1880 static int scarlett2_master_volume_ctl_get(struct snd_kcontrol *kctl,
1881 struct snd_ctl_elem_value *ucontrol)
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;
1887 mutex_lock(&private->data_mutex);
1888 if (private->vol_updated)
1889 scarlett2_update_volumes(mixer);
1890 mutex_unlock(&private->data_mutex);
1892 ucontrol->value.integer.value[0] = private->master_vol;
1896 static int line_out_remap(struct scarlett2_data *private, int index)
1898 const struct scarlett2_device_info *info = private->info;
1900 if (!info->line_out_remap_enable)
1902 return info->line_out_remap[index];
1905 static int scarlett2_volume_ctl_get(struct snd_kcontrol *kctl,
1906 struct snd_ctl_elem_value *ucontrol)
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);
1913 mutex_lock(&private->data_mutex);
1914 if (private->vol_updated)
1915 scarlett2_update_volumes(mixer);
1916 mutex_unlock(&private->data_mutex);
1918 ucontrol->value.integer.value[0] = private->vol[index];
1922 static int scarlett2_volume_ctl_put(struct snd_kcontrol *kctl,
1923 struct snd_ctl_elem_value *ucontrol)
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;
1931 mutex_lock(&private->data_mutex);
1933 oval = private->vol[index];
1934 val = ucontrol->value.integer.value[0];
1939 private->vol[index] = val;
1940 err = scarlett2_usb_set_config(mixer, SCARLETT2_CONFIG_LINE_OUT_VOLUME,
1941 index, val - SCARLETT2_VOLUME_BIAS);
1946 mutex_unlock(&private->data_mutex);
1950 static const DECLARE_TLV_DB_MINMAX(
1951 db_scale_scarlett2_gain, -SCARLETT2_VOLUME_BIAS * 100, 0
1954 static 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,
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 }
1965 static 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,
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 }
1977 /*** Mute Switch Controls ***/
1979 static int scarlett2_mute_ctl_get(struct snd_kcontrol *kctl,
1980 struct snd_ctl_elem_value *ucontrol)
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);
1987 mutex_lock(&private->data_mutex);
1988 if (private->vol_updated)
1989 scarlett2_update_volumes(mixer);
1990 mutex_unlock(&private->data_mutex);
1992 ucontrol->value.integer.value[0] = private->mute_switch[index];
1996 static int scarlett2_mute_ctl_put(struct snd_kcontrol *kctl,
1997 struct snd_ctl_elem_value *ucontrol)
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;
2005 mutex_lock(&private->data_mutex);
2007 oval = private->mute_switch[index];
2008 val = !!ucontrol->value.integer.value[0];
2013 private->mute_switch[index] = val;
2015 /* Send mute change to the device */
2016 err = scarlett2_usb_set_config(mixer, SCARLETT2_CONFIG_MUTE_SWITCH,
2022 mutex_unlock(&private->data_mutex);
2026 static const struct snd_kcontrol_new scarlett2_mute_ctl = {
2027 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2029 .info = snd_ctl_boolean_mono_info,
2030 .get = scarlett2_mute_ctl_get,
2031 .put = scarlett2_mute_ctl_put,
2034 /*** HW/SW Volume Switch Controls ***/
2036 static void scarlett2_sw_hw_ctl_ro(struct scarlett2_data *private, int index)
2038 private->sw_hw_ctls[index]->vd[0].access &=
2039 ~SNDRV_CTL_ELEM_ACCESS_WRITE;
2042 static void scarlett2_sw_hw_ctl_rw(struct scarlett2_data *private, int index)
2044 private->sw_hw_ctls[index]->vd[0].access |=
2045 SNDRV_CTL_ELEM_ACCESS_WRITE;
2048 static int scarlett2_sw_hw_enum_ctl_info(struct snd_kcontrol *kctl,
2049 struct snd_ctl_elem_info *uinfo)
2051 static const char *const values[2] = {
2055 return snd_ctl_enum_info(uinfo, 1, 2, values);
2058 static int scarlett2_sw_hw_enum_ctl_get(struct snd_kcontrol *kctl,
2059 struct snd_ctl_elem_value *ucontrol)
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);
2065 ucontrol->value.enumerated.item[0] = private->vol_sw_hw_switch[index];
2069 static void scarlett2_vol_ctl_set_writable(struct usb_mixer_interface *mixer,
2070 int index, int value)
2072 struct scarlett2_data *private = mixer->private_data;
2073 struct snd_card *card = mixer->chip->card;
2075 /* Set/Clear write bits */
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;
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;
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);
2097 static int scarlett2_sw_hw_change(struct usb_mixer_interface *mixer,
2098 int ctl_index, int val)
2100 struct scarlett2_data *private = mixer->private_data;
2101 int index = line_out_remap(private, ctl_index);
2104 private->vol_sw_hw_switch[index] = val;
2106 /* Change access mode to RO (hardware controlled volume)
2107 * or RW (software controlled volume)
2109 scarlett2_vol_ctl_set_writable(mixer, ctl_index, !val);
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];
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);
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]);
2129 /* Send SW/HW switch change to the device */
2130 return scarlett2_usb_set_config(mixer, SCARLETT2_CONFIG_SW_HW_SWITCH,
2134 static int scarlett2_sw_hw_enum_ctl_put(struct snd_kcontrol *kctl,
2135 struct snd_ctl_elem_value *ucontrol)
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;
2144 mutex_lock(&private->data_mutex);
2146 oval = private->vol_sw_hw_switch[index];
2147 val = !!ucontrol->value.enumerated.item[0];
2152 err = scarlett2_sw_hw_change(mixer, ctl_index, val);
2157 mutex_unlock(&private->data_mutex);
2161 static const struct snd_kcontrol_new scarlett2_sw_hw_enum_ctl = {
2162 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2164 .info = scarlett2_sw_hw_enum_ctl_info,
2165 .get = scarlett2_sw_hw_enum_ctl_get,
2166 .put = scarlett2_sw_hw_enum_ctl_put,
2169 /*** Line Level/Instrument Level Switch Controls ***/
2171 static int scarlett2_update_input_other(struct usb_mixer_interface *mixer)
2173 struct scarlett2_data *private = mixer->private_data;
2174 const struct scarlett2_device_info *info = private->info;
2176 private->input_other_updated = 0;
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);
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);
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);
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);
2210 err = scarlett2_usb_get_config(
2211 mixer, SCARLETT2_CONFIG_PHANTOM_PERSISTENCE,
2212 1, &private->phantom_persistence);
2220 static int scarlett2_level_enum_ctl_info(struct snd_kcontrol *kctl,
2221 struct snd_ctl_elem_info *uinfo)
2223 static const char *const values[2] = {
2227 return snd_ctl_enum_info(uinfo, 1, 2, values);
2230 static int scarlett2_level_enum_ctl_get(struct snd_kcontrol *kctl,
2231 struct snd_ctl_elem_value *ucontrol)
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;
2238 int index = elem->control + info->level_input_first;
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);
2249 static int scarlett2_level_enum_ctl_put(struct snd_kcontrol *kctl,
2250 struct snd_ctl_elem_value *ucontrol)
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;
2257 int index = elem->control + info->level_input_first;
2258 int oval, val, err = 0;
2260 mutex_lock(&private->data_mutex);
2262 oval = private->level_switch[index];
2263 val = !!ucontrol->value.enumerated.item[0];
2268 private->level_switch[index] = val;
2270 /* Send switch change to the device */
2271 err = scarlett2_usb_set_config(mixer, SCARLETT2_CONFIG_LEVEL_SWITCH,
2277 mutex_unlock(&private->data_mutex);
2281 static const struct snd_kcontrol_new scarlett2_level_enum_ctl = {
2282 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2284 .info = scarlett2_level_enum_ctl_info,
2285 .get = scarlett2_level_enum_ctl_get,
2286 .put = scarlett2_level_enum_ctl_put,
2289 /*** Pad Switch Controls ***/
2291 static int scarlett2_pad_ctl_get(struct snd_kcontrol *kctl,
2292 struct snd_ctl_elem_value *ucontrol)
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;
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);
2308 static int scarlett2_pad_ctl_put(struct snd_kcontrol *kctl,
2309 struct snd_ctl_elem_value *ucontrol)
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;
2315 int index = elem->control;
2316 int oval, val, err = 0;
2318 mutex_lock(&private->data_mutex);
2320 oval = private->pad_switch[index];
2321 val = !!ucontrol->value.integer.value[0];
2326 private->pad_switch[index] = val;
2328 /* Send switch change to the device */
2329 err = scarlett2_usb_set_config(mixer, SCARLETT2_CONFIG_PAD_SWITCH,
2335 mutex_unlock(&private->data_mutex);
2339 static const struct snd_kcontrol_new scarlett2_pad_ctl = {
2340 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2342 .info = snd_ctl_boolean_mono_info,
2343 .get = scarlett2_pad_ctl_get,
2344 .put = scarlett2_pad_ctl_put,
2347 /*** Air Switch Controls ***/
2349 static int scarlett2_air_ctl_get(struct snd_kcontrol *kctl,
2350 struct snd_ctl_elem_value *ucontrol)
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;
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);
2365 static int scarlett2_air_ctl_put(struct snd_kcontrol *kctl,
2366 struct snd_ctl_elem_value *ucontrol)
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;
2372 int index = elem->control;
2373 int oval, val, err = 0;
2375 mutex_lock(&private->data_mutex);
2377 oval = private->air_switch[index];
2378 val = !!ucontrol->value.integer.value[0];
2383 private->air_switch[index] = val;
2385 /* Send switch change to the device */
2386 err = scarlett2_usb_set_config(mixer, SCARLETT2_CONFIG_AIR_SWITCH,
2392 mutex_unlock(&private->data_mutex);
2396 static const struct snd_kcontrol_new scarlett2_air_ctl = {
2397 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2399 .info = snd_ctl_boolean_mono_info,
2400 .get = scarlett2_air_ctl_get,
2401 .put = scarlett2_air_ctl_put,
2404 /*** Phantom Switch Controls ***/
2406 static int scarlett2_phantom_ctl_get(struct snd_kcontrol *kctl,
2407 struct snd_ctl_elem_value *ucontrol)
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;
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);
2423 static int scarlett2_phantom_ctl_put(struct snd_kcontrol *kctl,
2424 struct snd_ctl_elem_value *ucontrol)
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;
2430 int index = elem->control;
2431 int oval, val, err = 0;
2433 mutex_lock(&private->data_mutex);
2435 oval = private->phantom_switch[index];
2436 val = !!ucontrol->value.integer.value[0];
2441 private->phantom_switch[index] = val;
2443 /* Send switch change to the device */
2444 err = scarlett2_usb_set_config(mixer, SCARLETT2_CONFIG_PHANTOM_SWITCH,
2450 mutex_unlock(&private->data_mutex);
2454 static const struct snd_kcontrol_new scarlett2_phantom_ctl = {
2455 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2457 .info = snd_ctl_boolean_mono_info,
2458 .get = scarlett2_phantom_ctl_get,
2459 .put = scarlett2_phantom_ctl_put,
2462 /*** Phantom Persistence Control ***/
2464 static int scarlett2_phantom_persistence_ctl_get(
2465 struct snd_kcontrol *kctl, struct snd_ctl_elem_value *ucontrol)
2467 struct usb_mixer_elem_info *elem = kctl->private_data;
2468 struct scarlett2_data *private = elem->head.mixer->private_data;
2470 ucontrol->value.integer.value[0] = private->phantom_persistence;
2474 static int scarlett2_phantom_persistence_ctl_put(
2475 struct snd_kcontrol *kctl, struct snd_ctl_elem_value *ucontrol)
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;
2481 int index = elem->control;
2482 int oval, val, err = 0;
2484 mutex_lock(&private->data_mutex);
2486 oval = private->phantom_persistence;
2487 val = !!ucontrol->value.integer.value[0];
2492 private->phantom_persistence = val;
2494 /* Send switch change to the device */
2495 err = scarlett2_usb_set_config(
2496 mixer, SCARLETT2_CONFIG_PHANTOM_PERSISTENCE, index, val);
2501 mutex_unlock(&private->data_mutex);
2505 static const struct snd_kcontrol_new scarlett2_phantom_persistence_ctl = {
2506 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2508 .info = snd_ctl_boolean_mono_info,
2509 .get = scarlett2_phantom_persistence_ctl_get,
2510 .put = scarlett2_phantom_persistence_ctl_put,
2513 /*** Direct Monitor Control ***/
2515 static int scarlett2_update_monitor_other(struct usb_mixer_interface *mixer)
2517 struct scarlett2_data *private = mixer->private_data;
2518 const struct scarlett2_device_info *info = private->info;
2521 /* monitor_other_enable[0] enables speaker switching
2522 * monitor_other_enable[1] enables talkback
2524 u8 monitor_other_enable[2];
2526 /* monitor_other_switch[0] activates the alternate speakers
2527 * monitor_other_switch[1] activates talkback
2529 u8 monitor_other_switch[2];
2531 private->monitor_other_updated = 0;
2533 if (info->direct_monitor)
2534 return scarlett2_usb_get_config(
2535 mixer, SCARLETT2_CONFIG_DIRECT_MONITOR,
2536 1, &private->direct_monitor_switch);
2538 /* if it doesn't do speaker switching then it also doesn't do
2541 if (!info->has_speaker_switching)
2544 err = scarlett2_usb_get_config(
2545 mixer, SCARLETT2_CONFIG_MONITOR_OTHER_ENABLE,
2546 2, monitor_other_enable);
2550 err = scarlett2_usb_get_config(
2551 mixer, SCARLETT2_CONFIG_MONITOR_OTHER_SWITCH,
2552 2, monitor_other_switch);
2556 if (!monitor_other_enable[0])
2557 private->speaker_switching_switch = 0;
2559 private->speaker_switching_switch = monitor_other_switch[0] + 1;
2561 if (info->has_talkback) {
2562 const int (*port_count)[SCARLETT2_PORT_DIRNS] =
2565 port_count[SCARLETT2_PORT_TYPE_MIX][SCARLETT2_PORT_IN];
2569 if (!monitor_other_enable[1])
2570 private->talkback_switch = 0;
2572 private->talkback_switch = monitor_other_switch[1] + 1;
2574 err = scarlett2_usb_get_config(mixer,
2575 SCARLETT2_CONFIG_TALKBACK_MAP,
2579 for (i = 0; i < num_mixes; i++, bitmap >>= 1)
2580 private->talkback_map[i] = bitmap & 1;
2586 static int scarlett2_direct_monitor_ctl_get(
2587 struct snd_kcontrol *kctl, struct snd_ctl_elem_value *ucontrol)
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;
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);
2602 static int scarlett2_direct_monitor_ctl_put(
2603 struct snd_kcontrol *kctl, struct snd_ctl_elem_value *ucontrol)
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;
2609 int index = elem->control;
2610 int oval, val, err = 0;
2612 mutex_lock(&private->data_mutex);
2614 oval = private->direct_monitor_switch;
2615 val = min(ucontrol->value.enumerated.item[0], 2U);
2620 private->direct_monitor_switch = val;
2622 /* Send switch change to the device */
2623 err = scarlett2_usb_set_config(
2624 mixer, SCARLETT2_CONFIG_DIRECT_MONITOR, index, val);
2629 mutex_unlock(&private->data_mutex);
2633 static int scarlett2_direct_monitor_stereo_enum_ctl_info(
2634 struct snd_kcontrol *kctl, struct snd_ctl_elem_info *uinfo)
2636 static const char *const values[3] = {
2637 "Off", "Mono", "Stereo"
2640 return snd_ctl_enum_info(uinfo, 1, 3, values);
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
2646 static const struct snd_kcontrol_new scarlett2_direct_monitor_ctl[2] = {
2648 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2650 .info = snd_ctl_boolean_mono_info,
2651 .get = scarlett2_direct_monitor_ctl_get,
2652 .put = scarlett2_direct_monitor_ctl_put,
2655 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2657 .info = scarlett2_direct_monitor_stereo_enum_ctl_info,
2658 .get = scarlett2_direct_monitor_ctl_get,
2659 .put = scarlett2_direct_monitor_ctl_put,
2663 static int scarlett2_add_direct_monitor_ctl(struct usb_mixer_interface *mixer)
2665 struct scarlett2_data *private = mixer->private_data;
2666 const struct scarlett2_device_info *info = private->info;
2669 if (!info->direct_monitor)
2672 s = info->direct_monitor == 1
2673 ? "Direct Monitor Playback Switch"
2674 : "Direct Monitor Playback Enum";
2676 return scarlett2_add_new_ctl(
2677 mixer, &scarlett2_direct_monitor_ctl[info->direct_monitor - 1],
2678 0, 1, s, &private->direct_monitor_ctl);
2681 /*** Speaker Switching Control ***/
2683 static int scarlett2_speaker_switch_enum_ctl_info(
2684 struct snd_kcontrol *kctl, struct snd_ctl_elem_info *uinfo)
2686 static const char *const values[3] = {
2687 "Off", "Main", "Alt"
2690 return snd_ctl_enum_info(uinfo, 1, 3, values);
2693 static int scarlett2_speaker_switch_enum_ctl_get(
2694 struct snd_kcontrol *kctl, struct snd_ctl_elem_value *ucontrol)
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;
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);
2709 /* when speaker switching gets enabled, switch the main/alt speakers
2710 * to HW volume and disable those controls
2712 static int scarlett2_speaker_switch_enable(struct usb_mixer_interface *mixer)
2714 struct snd_card *card = mixer->chip->card;
2715 struct scarlett2_data *private = mixer->private_data;
2718 for (i = 0; i < 4; i++) {
2719 int index = line_out_remap(private, i);
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);
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);
2736 /* when the next monitor-other notify comes in, update the mux
2739 private->speaker_switching_switched = 1;
2744 /* when speaker switching gets disabled, reenable the hw/sw controls
2745 * and invalidate the routing
2747 static void scarlett2_speaker_switch_disable(struct usb_mixer_interface *mixer)
2749 struct snd_card *card = mixer->chip->card;
2750 struct scarlett2_data *private = mixer->private_data;
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);
2760 /* when the next monitor-other notify comes in, update the mux
2763 private->speaker_switching_switched = 1;
2766 static int scarlett2_speaker_switch_enum_ctl_put(
2767 struct snd_kcontrol *kctl, struct snd_ctl_elem_value *ucontrol)
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;
2773 int oval, val, err = 0;
2775 mutex_lock(&private->data_mutex);
2777 oval = private->speaker_switching_switch;
2778 val = min(ucontrol->value.enumerated.item[0], 2U);
2783 private->speaker_switching_switch = val;
2785 /* enable/disable speaker switching */
2786 err = scarlett2_usb_set_config(
2787 mixer, SCARLETT2_CONFIG_MONITOR_OTHER_ENABLE,
2792 /* if speaker switching is enabled, select main or alt */
2793 err = scarlett2_usb_set_config(
2794 mixer, SCARLETT2_CONFIG_MONITOR_OTHER_SWITCH,
2799 /* update controls if speaker switching gets enabled or disabled */
2801 err = scarlett2_speaker_switch_enable(mixer);
2802 else if (oval && !val)
2803 scarlett2_speaker_switch_disable(mixer);
2809 mutex_unlock(&private->data_mutex);
2813 static const struct snd_kcontrol_new scarlett2_speaker_switch_enum_ctl = {
2814 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2816 .info = scarlett2_speaker_switch_enum_ctl_info,
2817 .get = scarlett2_speaker_switch_enum_ctl_get,
2818 .put = scarlett2_speaker_switch_enum_ctl_put,
2821 static int scarlett2_add_speaker_switch_ctl(
2822 struct usb_mixer_interface *mixer)
2824 struct scarlett2_data *private = mixer->private_data;
2825 const struct scarlett2_device_info *info = private->info;
2827 if (!info->has_speaker_switching)
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);
2836 /*** Talkback and Talkback Map Controls ***/
2838 static int scarlett2_talkback_enum_ctl_info(
2839 struct snd_kcontrol *kctl, struct snd_ctl_elem_info *uinfo)
2841 static const char *const values[3] = {
2842 "Disabled", "Off", "On"
2845 return snd_ctl_enum_info(uinfo, 1, 3, values);
2848 static int scarlett2_talkback_enum_ctl_get(
2849 struct snd_kcontrol *kctl, struct snd_ctl_elem_value *ucontrol)
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;
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);
2864 static int scarlett2_talkback_enum_ctl_put(
2865 struct snd_kcontrol *kctl, struct snd_ctl_elem_value *ucontrol)
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;
2871 int oval, val, err = 0;
2873 mutex_lock(&private->data_mutex);
2875 oval = private->talkback_switch;
2876 val = min(ucontrol->value.enumerated.item[0], 2U);
2881 private->talkback_switch = val;
2883 /* enable/disable talkback */
2884 err = scarlett2_usb_set_config(
2885 mixer, SCARLETT2_CONFIG_MONITOR_OTHER_ENABLE,
2890 /* if talkback is enabled, select main or alt */
2891 err = scarlett2_usb_set_config(
2892 mixer, SCARLETT2_CONFIG_MONITOR_OTHER_SWITCH,
2898 mutex_unlock(&private->data_mutex);
2902 static const struct snd_kcontrol_new scarlett2_talkback_enum_ctl = {
2903 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2905 .info = scarlett2_talkback_enum_ctl_info,
2906 .get = scarlett2_talkback_enum_ctl_get,
2907 .put = scarlett2_talkback_enum_ctl_put,
2910 static int scarlett2_talkback_map_ctl_get(
2911 struct snd_kcontrol *kctl, struct snd_ctl_elem_value *ucontrol)
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;
2918 ucontrol->value.integer.value[0] = private->talkback_map[index];
2923 static int scarlett2_talkback_map_ctl_put(
2924 struct snd_kcontrol *kctl, struct snd_ctl_elem_value *ucontrol)
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];
2933 int index = elem->control;
2934 int oval, val, err = 0, i;
2937 mutex_lock(&private->data_mutex);
2939 oval = private->talkback_map[index];
2940 val = !!ucontrol->value.integer.value[0];
2945 private->talkback_map[index] = val;
2947 for (i = 0; i < num_mixes; i++)
2948 bitmap |= private->talkback_map[i] << i;
2950 /* Send updated bitmap to the device */
2951 err = scarlett2_usb_set_config(mixer, SCARLETT2_CONFIG_TALKBACK_MAP,
2957 mutex_unlock(&private->data_mutex);
2961 static const struct snd_kcontrol_new scarlett2_talkback_map_ctl = {
2962 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2964 .info = snd_ctl_boolean_mono_info,
2965 .get = scarlett2_talkback_map_ctl_get,
2966 .put = scarlett2_talkback_map_ctl_put,
2969 static int scarlett2_add_talkback_ctls(
2970 struct usb_mixer_interface *mixer)
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];
2977 char s[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
2979 if (!info->has_talkback)
2982 err = scarlett2_add_new_ctl(
2983 mixer, &scarlett2_talkback_enum_ctl,
2984 0, 1, "Talkback Playback Enum",
2985 &private->talkback_ctl);
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,
3001 /*** Dim/Mute Controls ***/
3003 static int scarlett2_dim_mute_ctl_get(struct snd_kcontrol *kctl,
3004 struct snd_ctl_elem_value *ucontrol)
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;
3010 mutex_lock(&private->data_mutex);
3011 if (private->vol_updated)
3012 scarlett2_update_volumes(mixer);
3013 mutex_unlock(&private->data_mutex);
3015 ucontrol->value.integer.value[0] = private->dim_mute[elem->control];
3019 static int scarlett2_dim_mute_ctl_put(struct snd_kcontrol *kctl,
3020 struct snd_ctl_elem_value *ucontrol)
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;
3028 port_count[SCARLETT2_PORT_TYPE_ANALOGUE][SCARLETT2_PORT_OUT];
3030 int index = elem->control;
3031 int oval, val, err = 0, i;
3033 mutex_lock(&private->data_mutex);
3035 oval = private->dim_mute[index];
3036 val = !!ucontrol->value.integer.value[0];
3041 private->dim_mute[index] = val;
3043 /* Send switch change to the device */
3044 err = scarlett2_usb_set_config(mixer, SCARLETT2_CONFIG_DIM_MUTE,
3049 if (index == SCARLETT2_BUTTON_MUTE)
3050 for (i = 0; i < num_line_out; i++) {
3051 int line_index = line_out_remap(private, i);
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);
3062 mutex_unlock(&private->data_mutex);
3066 static const struct snd_kcontrol_new scarlett2_dim_mute_ctl = {
3067 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3069 .info = snd_ctl_boolean_mono_info,
3070 .get = scarlett2_dim_mute_ctl_get,
3071 .put = scarlett2_dim_mute_ctl_put
3074 /*** Create the analogue output controls ***/
3076 static int scarlett2_add_line_out_ctls(struct usb_mixer_interface *mixer)
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;
3082 port_count[SCARLETT2_PORT_TYPE_ANALOGUE][SCARLETT2_PORT_OUT];
3084 char s[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
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);
3096 /* Add volume controls */
3097 for (i = 0; i < num_line_out; i++) {
3098 int index = line_out_remap(private, i);
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]);
3106 snprintf(s, sizeof(s),
3107 "Line %02d Playback Volume",
3109 err = scarlett2_add_new_ctl(mixer,
3110 &scarlett2_line_out_volume_ctl,
3111 i, 1, s, &private->vol_ctls[i]);
3116 snprintf(s, sizeof(s),
3117 "Line %02d Mute Playback Switch",
3119 err = scarlett2_add_new_ctl(mixer,
3120 &scarlett2_mute_ctl,
3122 &private->mute_ctls[i]);
3126 /* Make the fader and mute controls read-only if the
3127 * SW/HW switch is set to HW
3129 if (private->vol_sw_hw_switch[index])
3130 scarlett2_vol_ctl_set_writable(mixer, i, 0);
3133 if (info->line_out_hw_vol) {
3134 snprintf(s, sizeof(s),
3135 "Line Out %02d Volume Control Playback Enum",
3137 err = scarlett2_add_new_ctl(mixer,
3138 &scarlett2_sw_hw_enum_ctl,
3140 &private->sw_hw_ctls[i]);
3144 /* Make the switch read-only if the line is
3145 * involved in speaker switching
3147 if (private->speaker_switching_switch && i < 4)
3148 scarlett2_sw_hw_ctl_ro(private, i);
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]);
3166 /*** Create the analogue input controls ***/
3168 static int scarlett2_add_line_in_ctls(struct usb_mixer_interface *mixer)
3170 struct scarlett2_data *private = mixer->private_data;
3171 const struct scarlett2_device_info *info = private->info;
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";
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,
3181 err = scarlett2_add_new_ctl(mixer, &scarlett2_level_enum_ctl,
3182 i, 1, s, &private->level_ctls[i]);
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]);
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]);
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]);
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;
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]);
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);
3241 /*** Mixer Volume Controls ***/
3243 static int scarlett2_mixer_ctl_info(struct snd_kcontrol *kctl,
3244 struct snd_ctl_elem_info *uinfo)
3246 struct usb_mixer_elem_info *elem = kctl->private_data;
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;
3256 static int scarlett2_mixer_ctl_get(struct snd_kcontrol *kctl,
3257 struct snd_ctl_elem_value *ucontrol)
3259 struct usb_mixer_elem_info *elem = kctl->private_data;
3260 struct scarlett2_data *private = elem->head.mixer->private_data;
3262 ucontrol->value.integer.value[0] = private->mix[elem->control];
3266 static int scarlett2_mixer_ctl_put(struct snd_kcontrol *kctl,
3267 struct snd_ctl_elem_value *ucontrol)
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;
3277 mutex_lock(&private->data_mutex);
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;
3287 private->mix[index] = val;
3288 err = scarlett2_usb_set_mix(mixer, mix_num);
3293 mutex_unlock(&private->data_mutex);
3297 static const DECLARE_TLV_DB_MINMAX(
3298 db_scale_scarlett2_mixer,
3299 SCARLETT2_MIXER_MIN_DB * 100,
3300 SCARLETT2_MIXER_MAX_DB * 100
3303 static 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,
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 }
3315 static int scarlett2_add_mixer_ctls(struct usb_mixer_interface *mixer)
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;
3322 char s[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
3325 port_count[SCARLETT2_PORT_TYPE_MIX][SCARLETT2_PORT_OUT];
3327 port_count[SCARLETT2_PORT_TYPE_MIX][SCARLETT2_PORT_IN];
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",
3334 err = scarlett2_add_new_ctl(mixer, &scarlett2_mixer_ctl,
3343 /*** Mux Source Selection Controls ***/
3345 static int scarlett2_mux_src_enum_ctl_info(struct snd_kcontrol *kctl,
3346 struct snd_ctl_elem_info *uinfo)
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;
3356 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
3357 uinfo->count = elem->channels;
3358 uinfo->value.enumerated.items = items;
3361 item = uinfo->value.enumerated.item = items - 1;
3364 port_type < SCARLETT2_PORT_TYPE_COUNT;
3366 if (item < port_count[port_type][SCARLETT2_PORT_IN]) {
3367 const struct scarlett2_port *port =
3368 &scarlett2_ports[port_type];
3370 sprintf(uinfo->value.enumerated.name,
3371 port->src_descr, item + port->src_num_offset);
3374 item -= port_count[port_type][SCARLETT2_PORT_IN];
3380 static int scarlett2_mux_src_enum_ctl_get(struct snd_kcontrol *kctl,
3381 struct snd_ctl_elem_value *ucontrol)
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;
3392 if (index < line_out_count)
3393 index = line_out_remap(private, index);
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);
3404 static int scarlett2_mux_src_enum_ctl_put(struct snd_kcontrol *kctl,
3405 struct snd_ctl_elem_value *ucontrol)
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;
3417 if (index < line_out_count)
3418 index = line_out_remap(private, index);
3420 mutex_lock(&private->data_mutex);
3422 oval = private->mux[index];
3423 val = min(ucontrol->value.enumerated.item[0],
3424 private->num_mux_srcs - 1U);
3429 private->mux[index] = val;
3430 err = scarlett2_usb_set_mux(mixer);
3435 mutex_unlock(&private->data_mutex);
3439 static const struct snd_kcontrol_new scarlett2_mux_src_enum_ctl = {
3440 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3442 .info = scarlett2_mux_src_enum_ctl_info,
3443 .get = scarlett2_mux_src_enum_ctl_get,
3444 .put = scarlett2_mux_src_enum_ctl_put,
3447 static int scarlett2_add_mux_enums(struct usb_mixer_interface *mixer)
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;
3454 for (i = 0, port_type = 0;
3455 port_type < SCARLETT2_PORT_TYPE_COUNT;
3458 channel < port_count[port_type][SCARLETT2_PORT_OUT];
3461 char s[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
3462 const char *const descr =
3463 scarlett2_ports[port_type].dst_descr;
3465 snprintf(s, sizeof(s) - 5, descr, channel + 1);
3468 err = scarlett2_add_new_ctl(mixer,
3469 &scarlett2_mux_src_enum_ctl,
3471 &private->mux_ctls[i]);
3480 /*** Meter Controls ***/
3482 static int scarlett2_meter_ctl_info(struct snd_kcontrol *kctl,
3483 struct snd_ctl_elem_info *uinfo)
3485 struct usb_mixer_elem_info *elem = kctl->private_data;
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;
3495 static int scarlett2_meter_ctl_get(struct snd_kcontrol *kctl,
3496 struct snd_ctl_elem_value *ucontrol)
3498 struct usb_mixer_elem_info *elem = kctl->private_data;
3499 u16 meter_levels[SCARLETT2_MAX_METERS];
3502 err = scarlett2_usb_get_meter_levels(elem->head.mixer, elem->channels,
3507 for (i = 0; i < elem->channels; i++)
3508 ucontrol->value.integer.value[i] = meter_levels[i];
3513 static 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,
3517 .info = scarlett2_meter_ctl_info,
3518 .get = scarlett2_meter_ctl_get
3521 static int scarlett2_add_meter_ctl(struct usb_mixer_interface *mixer)
3523 struct scarlett2_data *private = mixer->private_data;
3525 /* devices without a mixer also don't support reporting levels */
3526 if (private->info->config_set == SCARLETT2_CONFIG_SET_NO_MIXER)
3529 return scarlett2_add_new_ctl(mixer, &scarlett2_meter_ctl,
3530 0, private->num_mux_dsts,
3531 "Level Meter", NULL);
3534 /*** MSD Controls ***/
3536 static int scarlett2_msd_ctl_get(struct snd_kcontrol *kctl,
3537 struct snd_ctl_elem_value *ucontrol)
3539 struct usb_mixer_elem_info *elem = kctl->private_data;
3540 struct scarlett2_data *private = elem->head.mixer->private_data;
3542 ucontrol->value.integer.value[0] = private->msd_switch;
3546 static int scarlett2_msd_ctl_put(struct snd_kcontrol *kctl,
3547 struct snd_ctl_elem_value *ucontrol)
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;
3553 int oval, val, err = 0;
3555 mutex_lock(&private->data_mutex);
3557 oval = private->msd_switch;
3558 val = !!ucontrol->value.integer.value[0];
3563 private->msd_switch = val;
3565 /* Send switch change to the device */
3566 err = scarlett2_usb_set_config(mixer, SCARLETT2_CONFIG_MSD_SWITCH,
3572 mutex_unlock(&private->data_mutex);
3576 static const struct snd_kcontrol_new scarlett2_msd_ctl = {
3577 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3579 .info = snd_ctl_boolean_mono_info,
3580 .get = scarlett2_msd_ctl_get,
3581 .put = scarlett2_msd_ctl_put,
3584 static int scarlett2_add_msd_ctl(struct usb_mixer_interface *mixer)
3586 struct scarlett2_data *private = mixer->private_data;
3587 const struct scarlett2_device_info *info = private->info;
3589 if (!info->has_msd_mode)
3592 /* If MSD mode is off, hide the switch by default */
3593 if (!private->msd_switch && !(mixer->chip->setup & SCARLETT2_MSD_ENABLE))
3596 /* Add MSD control */
3597 return scarlett2_add_new_ctl(mixer, &scarlett2_msd_ctl,
3598 0, 1, "MSD Mode Switch", NULL);
3601 /*** Standalone Control ***/
3603 static int scarlett2_standalone_ctl_get(struct snd_kcontrol *kctl,
3604 struct snd_ctl_elem_value *ucontrol)
3606 struct usb_mixer_elem_info *elem = kctl->private_data;
3607 struct scarlett2_data *private = elem->head.mixer->private_data;
3609 ucontrol->value.integer.value[0] = private->standalone_switch;
3613 static int scarlett2_standalone_ctl_put(struct snd_kcontrol *kctl,
3614 struct snd_ctl_elem_value *ucontrol)
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;
3620 int oval, val, err = 0;
3622 mutex_lock(&private->data_mutex);
3624 oval = private->standalone_switch;
3625 val = !!ucontrol->value.integer.value[0];
3630 private->standalone_switch = val;
3632 /* Send switch change to the device */
3633 err = scarlett2_usb_set_config(mixer,
3634 SCARLETT2_CONFIG_STANDALONE_SWITCH,
3640 mutex_unlock(&private->data_mutex);
3644 static const struct snd_kcontrol_new scarlett2_standalone_ctl = {
3645 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3647 .info = snd_ctl_boolean_mono_info,
3648 .get = scarlett2_standalone_ctl_get,
3649 .put = scarlett2_standalone_ctl_put,
3652 static int scarlett2_add_standalone_ctl(struct usb_mixer_interface *mixer)
3654 struct scarlett2_data *private = mixer->private_data;
3656 if (private->info->config_set == SCARLETT2_CONFIG_SET_NO_MIXER)
3659 /* Add standalone control */
3660 return scarlett2_add_new_ctl(mixer, &scarlett2_standalone_ctl,
3661 0, 1, "Standalone Switch", NULL);
3664 /*** Cleanup/Suspend Callbacks ***/
3666 static void scarlett2_private_free(struct usb_mixer_interface *mixer)
3668 struct scarlett2_data *private = mixer->private_data;
3670 cancel_delayed_work_sync(&private->work);
3672 mixer->private_data = NULL;
3675 static void scarlett2_private_suspend(struct usb_mixer_interface *mixer)
3677 struct scarlett2_data *private = mixer->private_data;
3679 if (cancel_delayed_work_sync(&private->work))
3680 scarlett2_config_save(private->mixer);
3683 /*** Initialisation ***/
3685 static void scarlett2_count_mux_io(struct scarlett2_data *private)
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;
3692 port_type < SCARLETT2_PORT_TYPE_COUNT;
3694 srcs += port_count[port_type][SCARLETT2_PORT_IN];
3695 dsts += port_count[port_type][SCARLETT2_PORT_OUT];
3698 private->num_mux_srcs = srcs;
3699 private->num_mux_dsts = dsts;
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
3707 static int scarlett2_find_fc_interface(struct usb_device *dev,
3708 struct scarlett2_data *private)
3710 struct usb_host_config *config = dev->actconfig;
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;
3719 if (desc->bInterfaceClass != 255)
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;
3734 /* Initialise private data */
3735 static int scarlett2_init_private(struct usb_mixer_interface *mixer,
3736 const struct scarlett2_device_info *info)
3738 struct scarlett2_data *private =
3739 kzalloc(sizeof(struct scarlett2_data), GFP_KERNEL);
3744 mutex_init(&private->usb_mutex);
3745 mutex_init(&private->data_mutex);
3746 INIT_DELAYED_WORK(&private->work, scarlett2_config_save_work);
3748 mixer->private_data = private;
3749 mixer->private_free = scarlett2_private_free;
3750 mixer->private_suspend = scarlett2_private_suspend;
3752 private->info = info;
3753 scarlett2_count_mux_io(private);
3754 private->scarlett2_seq = 0;
3755 private->mixer = mixer;
3757 return scarlett2_find_fc_interface(mixer->chip->dev, private);
3760 /* Cargo cult proprietary initialisation sequence */
3761 static int scarlett2_usb_init(struct usb_mixer_interface *mixer)
3763 struct usb_device *dev = mixer->chip->dev;
3764 struct scarlett2_data *private = mixer->private_data;
3768 if (usb_pipe_type_check(dev, usb_sndctrlpipe(dev, 0)))
3772 err = scarlett2_usb_rx(dev, private->bInterfaceNumber,
3773 SCARLETT2_USB_CMD_INIT, buf, sizeof(buf));
3778 private->scarlett2_seq = 1;
3779 err = scarlett2_usb(mixer, SCARLETT2_USB_INIT_1, NULL, 0, NULL, 0);
3784 private->scarlett2_seq = 1;
3785 return scarlett2_usb(mixer, SCARLETT2_USB_INIT_2, NULL, 0, NULL, 84);
3788 /* Read configuration from the interface on start */
3789 static int scarlett2_read_configs(struct usb_mixer_interface *mixer)
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;
3795 port_count[SCARLETT2_PORT_TYPE_ANALOGUE][SCARLETT2_PORT_OUT];
3797 port_count[SCARLETT2_PORT_TYPE_MIX][SCARLETT2_PORT_IN];
3798 struct scarlett2_usb_volume_status volume_status;
3801 if (info->has_msd_mode) {
3802 err = scarlett2_usb_get_config(
3803 mixer, SCARLETT2_CONFIG_MSD_SWITCH,
3804 1, &private->msd_switch);
3808 /* no other controls are created if MSD mode is on */
3809 if (private->msd_switch)
3813 err = scarlett2_update_input_other(mixer);
3817 err = scarlett2_update_monitor_other(mixer);
3821 /* the rest of the configuration is for devices with a mixer */
3822 if (info->config_set == SCARLETT2_CONFIG_SET_NO_MIXER)
3825 err = scarlett2_usb_get_config(
3826 mixer, SCARLETT2_CONFIG_STANDALONE_SWITCH,
3827 1, &private->standalone_switch);
3831 err = scarlett2_update_sync(mixer);
3835 err = scarlett2_usb_get_volume_status(mixer, &volume_status);
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];
3843 private->master_vol = clamp(
3844 volume_status.master_vol + SCARLETT2_VOLUME_BIAS,
3845 0, SCARLETT2_VOLUME_BIAS);
3847 for (i = 0; i < num_line_out; i++) {
3850 private->vol_sw_hw_switch[i] =
3851 info->line_out_hw_vol
3852 && volume_status.sw_hw_switch[i];
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;
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;
3867 for (i = 0; i < num_mixer_out; i++) {
3868 err = scarlett2_usb_get_mix(mixer, i);
3873 return scarlett2_usb_get_mux(mixer);
3876 /* Notify on sync change */
3877 static void scarlett2_notify_sync(
3878 struct usb_mixer_interface *mixer)
3880 struct scarlett2_data *private = mixer->private_data;
3882 private->sync_updated = 1;
3884 snd_ctl_notify(mixer->chip->card, SNDRV_CTL_EVENT_MASK_VALUE,
3885 &private->sync_ctl->id);
3888 /* Notify on monitor change */
3889 static void scarlett2_notify_monitor(
3890 struct usb_mixer_interface *mixer)
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;
3897 port_count[SCARLETT2_PORT_TYPE_ANALOGUE][SCARLETT2_PORT_OUT];
3900 /* if line_out_hw_vol is 0, there are no controls to update */
3901 if (!info->line_out_hw_vol)
3904 private->vol_updated = 1;
3906 snd_ctl_notify(mixer->chip->card, SNDRV_CTL_EVENT_MASK_VALUE,
3907 &private->master_vol_ctl->id);
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);
3915 /* Notify on dim/mute change */
3916 static void scarlett2_notify_dim_mute(
3917 struct usb_mixer_interface *mixer)
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;
3924 port_count[SCARLETT2_PORT_TYPE_ANALOGUE][SCARLETT2_PORT_OUT];
3927 private->vol_updated = 1;
3929 if (!info->line_out_hw_vol)
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);
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);
3942 /* Notify on "input other" change (level/pad/air) */
3943 static void scarlett2_notify_input_other(
3944 struct usb_mixer_interface *mixer)
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;
3951 private->input_other_updated = 1;
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);
3967 /* Notify on "monitor other" change (direct monitor, speaker
3968 * switching, talkback)
3970 static void scarlett2_notify_monitor_other(
3971 struct usb_mixer_interface *mixer)
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;
3977 private->monitor_other_updated = 1;
3979 if (info->direct_monitor) {
3980 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE,
3981 &private->direct_monitor_ctl->id);
3985 if (info->has_speaker_switching)
3986 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE,
3987 &private->speaker_switching_ctl->id);
3989 if (info->has_talkback)
3990 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE,
3991 &private->talkback_ctl->id);
3993 /* if speaker switching was recently enabled or disabled,
3994 * invalidate the dim/mute and mux enum controls
3996 if (private->speaker_switching_switched) {
3999 scarlett2_notify_dim_mute(mixer);
4001 private->speaker_switching_switched = 0;
4002 private->mux_updated = 1;
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);
4010 /* Interrupt callback */
4011 static void scarlett2_notify(struct urb *urb)
4013 struct usb_mixer_interface *mixer = urb->context;
4014 int len = urb->actual_length;
4015 int ustatus = urb->status;
4018 if (ustatus != 0 || len != 8)
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);
4034 if (ustatus != -ENOENT &&
4035 ustatus != -ECONNRESET &&
4036 ustatus != -ESHUTDOWN) {
4037 urb->dev = mixer->chip->dev;
4038 usb_submit_urb(urb, GFP_ATOMIC);
4042 static int scarlett2_init_notify(struct usb_mixer_interface *mixer)
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;
4050 usb_audio_err(mixer->chip,
4051 "%s: mixer urb already in use!\n", __func__);
4055 if (usb_pipe_type_check(dev, pipe))
4058 mixer->urb = usb_alloc_urb(0, GFP_KERNEL);
4062 transfer_buffer = kmalloc(private->wMaxPacketSize, GFP_KERNEL);
4063 if (!transfer_buffer)
4066 usb_fill_int_urb(mixer->urb, dev, pipe,
4067 transfer_buffer, private->wMaxPacketSize,
4068 scarlett2_notify, mixer, private->bInterval);
4070 return usb_submit_urb(mixer->urb, GFP_KERNEL);
4073 static int snd_scarlett_gen2_controls_create(struct usb_mixer_interface *mixer)
4075 const struct scarlett2_device_info **info = scarlett2_devices;
4078 /* Find device in scarlett2_devices */
4079 while (*info && (*info)->usb_id != mixer->chip->usb_id)
4084 /* Initialise private data */
4085 err = scarlett2_init_private(mixer, *info);
4089 /* Send proprietary USB initialisation sequence */
4090 err = scarlett2_usb_init(mixer);
4094 /* Read volume levels and controls from the interface */
4095 err = scarlett2_read_configs(mixer);
4099 /* Create the MSD control */
4100 err = scarlett2_add_msd_ctl(mixer);
4104 /* If MSD mode is enabled, don't create any other controls */
4105 if (((struct scarlett2_data *)mixer->private_data)->msd_switch)
4108 /* Create the analogue output controls */
4109 err = scarlett2_add_line_out_ctls(mixer);
4113 /* Create the analogue input controls */
4114 err = scarlett2_add_line_in_ctls(mixer);
4118 /* Create the input, output, and mixer mux input selections */
4119 err = scarlett2_add_mux_enums(mixer);
4123 /* Create the matrix mixer controls */
4124 err = scarlett2_add_mixer_ctls(mixer);
4128 /* Create the level meter controls */
4129 err = scarlett2_add_meter_ctl(mixer);
4133 /* Create the sync control */
4134 err = scarlett2_add_sync_ctl(mixer);
4138 /* Create the direct monitor control */
4139 err = scarlett2_add_direct_monitor_ctl(mixer);
4143 /* Create the speaker switching control */
4144 err = scarlett2_add_speaker_switch_ctl(mixer);
4148 /* Create the talkback controls */
4149 err = scarlett2_add_talkback_ctls(mixer);
4153 /* Create the standalone control */
4154 err = scarlett2_add_standalone_ctl(mixer);
4158 /* Set up the interrupt polling */
4159 err = scarlett2_init_notify(mixer);
4166 int snd_scarlett_gen2_init(struct usb_mixer_interface *mixer)
4168 struct snd_usb_audio *chip = mixer->chip;
4171 /* only use UAC_VERSION_2 */
4172 if (!mixer->protocol)
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 "
4181 USB_ID_VENDOR(chip->usb_id),
4182 USB_ID_PRODUCT(chip->usb_id));
4186 usb_audio_info(chip,
4187 "Focusrite Scarlett Gen 2/3 Mixer Driver enabled pid=0x%04x",
4188 USB_ID_PRODUCT(chip->usb_id));
4190 err = snd_scarlett_gen2_controls_create(mixer);
4192 usb_audio_err(mixer->chip,
4193 "Error initialising Scarlett Mixer Driver: %d",