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 int err = scarlett2_usb(mixer, SCARLETT2_USB_DATA_CMD,
1344 usb_audio_err(mixer->chip, "config save failed: %d\n", err);
1347 /* Delayed work to save config */
1348 static void scarlett2_config_save_work(struct work_struct *work)
1350 struct scarlett2_data *private =
1351 container_of(work, struct scarlett2_data, work.work);
1353 scarlett2_config_save(private->mixer);
1356 /* Send a USB message to set a SCARLETT2_CONFIG_* parameter */
1357 static int scarlett2_usb_set_config(
1358 struct usb_mixer_interface *mixer,
1359 int config_item_num, int index, int value)
1361 struct scarlett2_data *private = mixer->private_data;
1362 const struct scarlett2_device_info *info = private->info;
1363 const struct scarlett2_config *config_item =
1364 &scarlett2_config_items[info->config_set][config_item_num];
1374 /* Cancel any pending NVRAM save */
1375 cancel_delayed_work_sync(&private->work);
1377 /* Convert config_item->size in bits to size in bytes and
1380 if (config_item->size >= 8) {
1381 size = config_item->size / 8;
1382 offset = config_item->offset + index * size;
1384 /* If updating a bit, retrieve the old value, set/clear the
1385 * bit as needed, and update value
1391 offset = config_item->offset;
1393 err = scarlett2_usb_get(mixer, offset, &tmp, 1);
1398 tmp |= (1 << index);
1400 tmp &= ~(1 << index);
1405 /* Send the configuration parameter data */
1406 req.offset = cpu_to_le32(offset);
1407 req.bytes = cpu_to_le32(size);
1408 req.value = cpu_to_le32(value);
1409 err = scarlett2_usb(mixer, SCARLETT2_USB_SET_DATA,
1410 &req, sizeof(u32) * 2 + size,
1415 /* Activate the change */
1416 req2 = cpu_to_le32(config_item->activate);
1417 err = scarlett2_usb(mixer, SCARLETT2_USB_DATA_CMD,
1418 &req2, sizeof(req2), NULL, 0);
1422 /* Schedule the change to be written to NVRAM */
1423 if (config_item->activate != SCARLETT2_USB_CONFIG_SAVE)
1424 schedule_delayed_work(&private->work, msecs_to_jiffies(2000));
1429 /* Send a USB message to get sync status; result placed in *sync */
1430 static int scarlett2_usb_get_sync_status(
1431 struct usb_mixer_interface *mixer,
1437 err = scarlett2_usb(mixer, SCARLETT2_USB_GET_SYNC,
1438 NULL, 0, &data, sizeof(data));
1446 /* Send a USB message to get volume status; result placed in *buf */
1447 static int scarlett2_usb_get_volume_status(
1448 struct usb_mixer_interface *mixer,
1449 struct scarlett2_usb_volume_status *buf)
1451 return scarlett2_usb_get(mixer, SCARLETT2_USB_VOLUME_STATUS_OFFSET,
1455 /* Send a USB message to get the volumes for all inputs of one mix
1456 * and put the values into private->mix[]
1458 static int scarlett2_usb_get_mix(struct usb_mixer_interface *mixer,
1461 struct scarlett2_data *private = mixer->private_data;
1462 const struct scarlett2_device_info *info = private->info;
1465 info->port_count[SCARLETT2_PORT_TYPE_MIX][SCARLETT2_PORT_OUT];
1473 __le16 data[SCARLETT2_INPUT_MIX_MAX];
1475 req.mix_num = cpu_to_le16(mix_num);
1476 req.count = cpu_to_le16(num_mixer_in);
1478 err = scarlett2_usb(mixer, SCARLETT2_USB_GET_MIX,
1480 data, num_mixer_in * sizeof(u16));
1484 for (i = 0, j = mix_num * num_mixer_in; i < num_mixer_in; i++, j++) {
1485 u16 mixer_value = le16_to_cpu(data[i]);
1487 for (k = 0; k < SCARLETT2_MIXER_VALUE_COUNT; k++)
1488 if (scarlett2_mixer_values[k] >= mixer_value)
1490 if (k == SCARLETT2_MIXER_VALUE_COUNT)
1491 k = SCARLETT2_MIXER_MAX_VALUE;
1492 private->mix[j] = k;
1498 /* Send a USB message to set the volumes for all inputs of one mix
1499 * (values obtained from private->mix[])
1501 static int scarlett2_usb_set_mix(struct usb_mixer_interface *mixer,
1504 struct scarlett2_data *private = mixer->private_data;
1505 const struct scarlett2_device_info *info = private->info;
1509 __le16 data[SCARLETT2_INPUT_MIX_MAX];
1514 info->port_count[SCARLETT2_PORT_TYPE_MIX][SCARLETT2_PORT_OUT];
1516 req.mix_num = cpu_to_le16(mix_num);
1518 for (i = 0, j = mix_num * num_mixer_in; i < num_mixer_in; i++, j++)
1519 req.data[i] = cpu_to_le16(
1520 scarlett2_mixer_values[private->mix[j]]
1523 return scarlett2_usb(mixer, SCARLETT2_USB_SET_MIX,
1524 &req, (num_mixer_in + 1) * sizeof(u16),
1528 /* Convert a port number index (per info->port_count) to a hardware ID */
1529 static u32 scarlett2_mux_src_num_to_id(
1530 const int port_count[][SCARLETT2_PORT_DIRNS], int num)
1535 port_type < SCARLETT2_PORT_TYPE_COUNT;
1537 if (num < port_count[port_type][SCARLETT2_PORT_IN])
1538 return scarlett2_ports[port_type].id | num;
1539 num -= port_count[port_type][SCARLETT2_PORT_IN];
1546 /* Convert a hardware ID to a port number index */
1547 static u32 scarlett2_mux_id_to_num(
1548 const int port_count[][SCARLETT2_PORT_DIRNS], int direction, u32 id)
1554 port_type < SCARLETT2_PORT_TYPE_COUNT;
1556 int base = scarlett2_ports[port_type].id;
1557 int count = port_count[port_type][direction];
1559 if (id >= base && id < base + count)
1560 return port_num + id - base;
1568 /* Convert one mux entry from the interface and load into private->mux[] */
1569 static void scarlett2_usb_populate_mux(struct scarlett2_data *private,
1572 const struct scarlett2_device_info *info = private->info;
1573 const int (*port_count)[SCARLETT2_PORT_DIRNS] = info->port_count;
1575 int dst_idx, src_idx;
1577 dst_idx = scarlett2_mux_id_to_num(port_count, SCARLETT2_PORT_OUT,
1582 if (dst_idx >= private->num_mux_dsts) {
1583 usb_audio_err(private->mixer->chip,
1584 "BUG: scarlett2_mux_id_to_num(%06x, OUT): %d >= %d",
1585 mux_entry, dst_idx, private->num_mux_dsts);
1589 src_idx = scarlett2_mux_id_to_num(port_count, SCARLETT2_PORT_IN,
1594 if (src_idx >= private->num_mux_srcs) {
1595 usb_audio_err(private->mixer->chip,
1596 "BUG: scarlett2_mux_id_to_num(%06x, IN): %d >= %d",
1597 mux_entry, src_idx, private->num_mux_srcs);
1601 private->mux[dst_idx] = src_idx;
1604 /* Send USB message to get mux inputs and then populate private->mux[] */
1605 static int scarlett2_usb_get_mux(struct usb_mixer_interface *mixer)
1607 struct scarlett2_data *private = mixer->private_data;
1608 int count = private->num_mux_dsts;
1616 __le32 data[SCARLETT2_MUX_MAX];
1618 private->mux_updated = 0;
1621 req.count = cpu_to_le16(count);
1623 err = scarlett2_usb(mixer, SCARLETT2_USB_GET_MUX,
1625 data, count * sizeof(u32));
1629 for (i = 0; i < count; i++)
1630 scarlett2_usb_populate_mux(private, le32_to_cpu(data[i]));
1635 /* Send USB messages to set mux inputs */
1636 static int scarlett2_usb_set_mux(struct usb_mixer_interface *mixer)
1638 struct scarlett2_data *private = mixer->private_data;
1639 const struct scarlett2_device_info *info = private->info;
1640 const int (*port_count)[SCARLETT2_PORT_DIRNS] = info->port_count;
1646 __le32 data[SCARLETT2_MUX_MAX];
1651 /* set mux settings for each rate */
1652 for (table = 0; table < SCARLETT2_MUX_TABLES; table++) {
1653 const struct scarlett2_mux_entry *entry;
1655 /* i counts over the output array */
1658 req.num = cpu_to_le16(table);
1660 /* loop through each entry */
1661 for (entry = info->mux_assignment[table];
1665 int port_type = entry->port_type;
1666 int port_idx = entry->start;
1667 int mux_idx = scarlett2_get_port_start_num(port_count,
1668 SCARLETT2_PORT_OUT, port_type) + port_idx;
1669 int dst_id = scarlett2_ports[port_type].id + port_idx;
1673 for (j = 0; j < entry->count; j++)
1678 /* Non-empty mux slots use the lower 12 bits
1679 * for the destination and next 12 bits for
1682 for (j = 0; j < entry->count; j++) {
1683 int src_id = scarlett2_mux_src_num_to_id(
1684 port_count, private->mux[mux_idx++]);
1685 req.data[i++] = cpu_to_le32(dst_id |
1691 err = scarlett2_usb(mixer, SCARLETT2_USB_SET_MUX,
1692 &req, (i + 1) * sizeof(u32),
1701 /* Send USB message to get meter levels */
1702 static int scarlett2_usb_get_meter_levels(struct usb_mixer_interface *mixer,
1703 u16 num_meters, u16 *levels)
1710 u32 resp[SCARLETT2_MAX_METERS];
1714 req.num_meters = cpu_to_le16(num_meters);
1715 req.magic = cpu_to_le32(SCARLETT2_USB_METER_LEVELS_GET_MAGIC);
1716 err = scarlett2_usb(mixer, SCARLETT2_USB_GET_METER,
1717 &req, sizeof(req), resp, num_meters * sizeof(u32));
1721 /* copy, convert to u16 */
1722 for (i = 0; i < num_meters; i++)
1723 levels[i] = resp[i];
1728 /*** Control Functions ***/
1730 /* helper function to create a new control */
1731 static int scarlett2_add_new_ctl(struct usb_mixer_interface *mixer,
1732 const struct snd_kcontrol_new *ncontrol,
1733 int index, int channels, const char *name,
1734 struct snd_kcontrol **kctl_return)
1736 struct snd_kcontrol *kctl;
1737 struct usb_mixer_elem_info *elem;
1740 elem = kzalloc(sizeof(*elem), GFP_KERNEL);
1744 /* We set USB_MIXER_BESPOKEN type, so that the core USB mixer code
1745 * ignores them for resume and other operations.
1746 * Also, the head.id field is set to 0, as we don't use this field.
1748 elem->head.mixer = mixer;
1749 elem->control = index;
1751 elem->channels = channels;
1752 elem->val_type = USB_MIXER_BESPOKEN;
1754 kctl = snd_ctl_new1(ncontrol, elem);
1759 kctl->private_free = snd_usb_mixer_elem_free;
1761 strscpy(kctl->id.name, name, sizeof(kctl->id.name));
1763 err = snd_usb_mixer_add_control(&elem->head, kctl);
1768 *kctl_return = kctl;
1773 /*** Sync Control ***/
1775 /* Update sync control after receiving notification that the status
1778 static int scarlett2_update_sync(struct usb_mixer_interface *mixer)
1780 struct scarlett2_data *private = mixer->private_data;
1782 private->sync_updated = 0;
1783 return scarlett2_usb_get_sync_status(mixer, &private->sync);
1786 static int scarlett2_sync_ctl_info(struct snd_kcontrol *kctl,
1787 struct snd_ctl_elem_info *uinfo)
1789 static const char *texts[2] = {
1790 "Unlocked", "Locked"
1792 return snd_ctl_enum_info(uinfo, 1, 2, texts);
1795 static int scarlett2_sync_ctl_get(struct snd_kcontrol *kctl,
1796 struct snd_ctl_elem_value *ucontrol)
1798 struct usb_mixer_elem_info *elem = kctl->private_data;
1799 struct usb_mixer_interface *mixer = elem->head.mixer;
1800 struct scarlett2_data *private = mixer->private_data;
1802 mutex_lock(&private->data_mutex);
1803 if (private->sync_updated)
1804 scarlett2_update_sync(mixer);
1805 ucontrol->value.enumerated.item[0] = private->sync;
1806 mutex_unlock(&private->data_mutex);
1811 static const struct snd_kcontrol_new scarlett2_sync_ctl = {
1812 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1813 .access = SNDRV_CTL_ELEM_ACCESS_READ,
1815 .info = scarlett2_sync_ctl_info,
1816 .get = scarlett2_sync_ctl_get
1819 static int scarlett2_add_sync_ctl(struct usb_mixer_interface *mixer)
1821 struct scarlett2_data *private = mixer->private_data;
1823 /* devices without a mixer also don't support reporting sync status */
1824 if (private->info->config_set == SCARLETT2_CONFIG_SET_NO_MIXER)
1827 return scarlett2_add_new_ctl(mixer, &scarlett2_sync_ctl,
1828 0, 1, "Sync Status", &private->sync_ctl);
1831 /*** Analogue Line Out Volume Controls ***/
1833 /* Update hardware volume controls after receiving notification that
1836 static int scarlett2_update_volumes(struct usb_mixer_interface *mixer)
1838 struct scarlett2_data *private = mixer->private_data;
1839 const struct scarlett2_device_info *info = private->info;
1840 const int (*port_count)[SCARLETT2_PORT_DIRNS] = info->port_count;
1841 struct scarlett2_usb_volume_status volume_status;
1843 port_count[SCARLETT2_PORT_TYPE_ANALOGUE][SCARLETT2_PORT_OUT];
1847 private->vol_updated = 0;
1849 err = scarlett2_usb_get_volume_status(mixer, &volume_status);
1853 private->master_vol = clamp(
1854 volume_status.master_vol + SCARLETT2_VOLUME_BIAS,
1855 0, SCARLETT2_VOLUME_BIAS);
1857 if (info->line_out_hw_vol)
1858 for (i = 0; i < SCARLETT2_DIM_MUTE_COUNT; i++)
1859 private->dim_mute[i] = !!volume_status.dim_mute[i];
1861 mute = private->dim_mute[SCARLETT2_BUTTON_MUTE];
1863 for (i = 0; i < num_line_out; i++)
1864 if (private->vol_sw_hw_switch[i]) {
1865 private->vol[i] = private->master_vol;
1866 private->mute_switch[i] = mute;
1872 static int scarlett2_volume_ctl_info(struct snd_kcontrol *kctl,
1873 struct snd_ctl_elem_info *uinfo)
1875 struct usb_mixer_elem_info *elem = kctl->private_data;
1877 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1878 uinfo->count = elem->channels;
1879 uinfo->value.integer.min = 0;
1880 uinfo->value.integer.max = SCARLETT2_VOLUME_BIAS;
1881 uinfo->value.integer.step = 1;
1885 static int scarlett2_master_volume_ctl_get(struct snd_kcontrol *kctl,
1886 struct snd_ctl_elem_value *ucontrol)
1888 struct usb_mixer_elem_info *elem = kctl->private_data;
1889 struct usb_mixer_interface *mixer = elem->head.mixer;
1890 struct scarlett2_data *private = mixer->private_data;
1892 mutex_lock(&private->data_mutex);
1893 if (private->vol_updated)
1894 scarlett2_update_volumes(mixer);
1895 mutex_unlock(&private->data_mutex);
1897 ucontrol->value.integer.value[0] = private->master_vol;
1901 static int line_out_remap(struct scarlett2_data *private, int index)
1903 const struct scarlett2_device_info *info = private->info;
1905 if (!info->line_out_remap_enable)
1907 return info->line_out_remap[index];
1910 static int scarlett2_volume_ctl_get(struct snd_kcontrol *kctl,
1911 struct snd_ctl_elem_value *ucontrol)
1913 struct usb_mixer_elem_info *elem = kctl->private_data;
1914 struct usb_mixer_interface *mixer = elem->head.mixer;
1915 struct scarlett2_data *private = mixer->private_data;
1916 int index = line_out_remap(private, elem->control);
1918 mutex_lock(&private->data_mutex);
1919 if (private->vol_updated)
1920 scarlett2_update_volumes(mixer);
1921 mutex_unlock(&private->data_mutex);
1923 ucontrol->value.integer.value[0] = private->vol[index];
1927 static int scarlett2_volume_ctl_put(struct snd_kcontrol *kctl,
1928 struct snd_ctl_elem_value *ucontrol)
1930 struct usb_mixer_elem_info *elem = kctl->private_data;
1931 struct usb_mixer_interface *mixer = elem->head.mixer;
1932 struct scarlett2_data *private = mixer->private_data;
1933 int index = line_out_remap(private, elem->control);
1934 int oval, val, err = 0;
1936 mutex_lock(&private->data_mutex);
1938 oval = private->vol[index];
1939 val = ucontrol->value.integer.value[0];
1944 private->vol[index] = val;
1945 err = scarlett2_usb_set_config(mixer, SCARLETT2_CONFIG_LINE_OUT_VOLUME,
1946 index, val - SCARLETT2_VOLUME_BIAS);
1951 mutex_unlock(&private->data_mutex);
1955 static const DECLARE_TLV_DB_MINMAX(
1956 db_scale_scarlett2_gain, -SCARLETT2_VOLUME_BIAS * 100, 0
1959 static const struct snd_kcontrol_new scarlett2_master_volume_ctl = {
1960 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1961 .access = SNDRV_CTL_ELEM_ACCESS_READ |
1962 SNDRV_CTL_ELEM_ACCESS_TLV_READ,
1964 .info = scarlett2_volume_ctl_info,
1965 .get = scarlett2_master_volume_ctl_get,
1966 .private_value = 0, /* max value */
1967 .tlv = { .p = db_scale_scarlett2_gain }
1970 static const struct snd_kcontrol_new scarlett2_line_out_volume_ctl = {
1971 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1972 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE |
1973 SNDRV_CTL_ELEM_ACCESS_TLV_READ,
1975 .info = scarlett2_volume_ctl_info,
1976 .get = scarlett2_volume_ctl_get,
1977 .put = scarlett2_volume_ctl_put,
1978 .private_value = 0, /* max value */
1979 .tlv = { .p = db_scale_scarlett2_gain }
1982 /*** Mute Switch Controls ***/
1984 static int scarlett2_mute_ctl_get(struct snd_kcontrol *kctl,
1985 struct snd_ctl_elem_value *ucontrol)
1987 struct usb_mixer_elem_info *elem = kctl->private_data;
1988 struct usb_mixer_interface *mixer = elem->head.mixer;
1989 struct scarlett2_data *private = mixer->private_data;
1990 int index = line_out_remap(private, elem->control);
1992 mutex_lock(&private->data_mutex);
1993 if (private->vol_updated)
1994 scarlett2_update_volumes(mixer);
1995 mutex_unlock(&private->data_mutex);
1997 ucontrol->value.integer.value[0] = private->mute_switch[index];
2001 static int scarlett2_mute_ctl_put(struct snd_kcontrol *kctl,
2002 struct snd_ctl_elem_value *ucontrol)
2004 struct usb_mixer_elem_info *elem = kctl->private_data;
2005 struct usb_mixer_interface *mixer = elem->head.mixer;
2006 struct scarlett2_data *private = mixer->private_data;
2007 int index = line_out_remap(private, elem->control);
2008 int oval, val, err = 0;
2010 mutex_lock(&private->data_mutex);
2012 oval = private->mute_switch[index];
2013 val = !!ucontrol->value.integer.value[0];
2018 private->mute_switch[index] = val;
2020 /* Send mute change to the device */
2021 err = scarlett2_usb_set_config(mixer, SCARLETT2_CONFIG_MUTE_SWITCH,
2027 mutex_unlock(&private->data_mutex);
2031 static const struct snd_kcontrol_new scarlett2_mute_ctl = {
2032 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2034 .info = snd_ctl_boolean_mono_info,
2035 .get = scarlett2_mute_ctl_get,
2036 .put = scarlett2_mute_ctl_put,
2039 /*** HW/SW Volume Switch Controls ***/
2041 static void scarlett2_sw_hw_ctl_ro(struct scarlett2_data *private, int index)
2043 private->sw_hw_ctls[index]->vd[0].access &=
2044 ~SNDRV_CTL_ELEM_ACCESS_WRITE;
2047 static void scarlett2_sw_hw_ctl_rw(struct scarlett2_data *private, int index)
2049 private->sw_hw_ctls[index]->vd[0].access |=
2050 SNDRV_CTL_ELEM_ACCESS_WRITE;
2053 static int scarlett2_sw_hw_enum_ctl_info(struct snd_kcontrol *kctl,
2054 struct snd_ctl_elem_info *uinfo)
2056 static const char *const values[2] = {
2060 return snd_ctl_enum_info(uinfo, 1, 2, values);
2063 static int scarlett2_sw_hw_enum_ctl_get(struct snd_kcontrol *kctl,
2064 struct snd_ctl_elem_value *ucontrol)
2066 struct usb_mixer_elem_info *elem = kctl->private_data;
2067 struct scarlett2_data *private = elem->head.mixer->private_data;
2068 int index = line_out_remap(private, elem->control);
2070 ucontrol->value.enumerated.item[0] = private->vol_sw_hw_switch[index];
2074 static void scarlett2_vol_ctl_set_writable(struct usb_mixer_interface *mixer,
2075 int index, int value)
2077 struct scarlett2_data *private = mixer->private_data;
2078 struct snd_card *card = mixer->chip->card;
2080 /* Set/Clear write bits */
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;
2087 private->vol_ctls[index]->vd[0].access &=
2088 ~SNDRV_CTL_ELEM_ACCESS_WRITE;
2089 private->mute_ctls[index]->vd[0].access &=
2090 ~SNDRV_CTL_ELEM_ACCESS_WRITE;
2093 /* Notify of write bit and possible value change */
2094 snd_ctl_notify(card,
2095 SNDRV_CTL_EVENT_MASK_VALUE | SNDRV_CTL_EVENT_MASK_INFO,
2096 &private->vol_ctls[index]->id);
2097 snd_ctl_notify(card,
2098 SNDRV_CTL_EVENT_MASK_VALUE | SNDRV_CTL_EVENT_MASK_INFO,
2099 &private->mute_ctls[index]->id);
2102 static int scarlett2_sw_hw_change(struct usb_mixer_interface *mixer,
2103 int ctl_index, int val)
2105 struct scarlett2_data *private = mixer->private_data;
2106 int index = line_out_remap(private, ctl_index);
2109 private->vol_sw_hw_switch[index] = val;
2111 /* Change access mode to RO (hardware controlled volume)
2112 * or RW (software controlled volume)
2114 scarlett2_vol_ctl_set_writable(mixer, ctl_index, !val);
2116 /* Reset volume/mute to master volume/mute */
2117 private->vol[index] = private->master_vol;
2118 private->mute_switch[index] = private->dim_mute[SCARLETT2_BUTTON_MUTE];
2120 /* Set SW volume to current HW volume */
2121 err = scarlett2_usb_set_config(
2122 mixer, SCARLETT2_CONFIG_LINE_OUT_VOLUME,
2123 index, private->master_vol - SCARLETT2_VOLUME_BIAS);
2127 /* Set SW mute to current HW mute */
2128 err = scarlett2_usb_set_config(
2129 mixer, SCARLETT2_CONFIG_MUTE_SWITCH,
2130 index, private->dim_mute[SCARLETT2_BUTTON_MUTE]);
2134 /* Send SW/HW switch change to the device */
2135 return scarlett2_usb_set_config(mixer, SCARLETT2_CONFIG_SW_HW_SWITCH,
2139 static int scarlett2_sw_hw_enum_ctl_put(struct snd_kcontrol *kctl,
2140 struct snd_ctl_elem_value *ucontrol)
2142 struct usb_mixer_elem_info *elem = kctl->private_data;
2143 struct usb_mixer_interface *mixer = elem->head.mixer;
2144 struct scarlett2_data *private = mixer->private_data;
2145 int ctl_index = elem->control;
2146 int index = line_out_remap(private, ctl_index);
2147 int oval, val, err = 0;
2149 mutex_lock(&private->data_mutex);
2151 oval = private->vol_sw_hw_switch[index];
2152 val = !!ucontrol->value.enumerated.item[0];
2157 err = scarlett2_sw_hw_change(mixer, ctl_index, val);
2162 mutex_unlock(&private->data_mutex);
2166 static const struct snd_kcontrol_new scarlett2_sw_hw_enum_ctl = {
2167 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2169 .info = scarlett2_sw_hw_enum_ctl_info,
2170 .get = scarlett2_sw_hw_enum_ctl_get,
2171 .put = scarlett2_sw_hw_enum_ctl_put,
2174 /*** Line Level/Instrument Level Switch Controls ***/
2176 static int scarlett2_update_input_other(struct usb_mixer_interface *mixer)
2178 struct scarlett2_data *private = mixer->private_data;
2179 const struct scarlett2_device_info *info = private->info;
2181 private->input_other_updated = 0;
2183 if (info->level_input_count) {
2184 int err = scarlett2_usb_get_config(
2185 mixer, SCARLETT2_CONFIG_LEVEL_SWITCH,
2186 info->level_input_count + info->level_input_first,
2187 private->level_switch);
2192 if (info->pad_input_count) {
2193 int err = scarlett2_usb_get_config(
2194 mixer, SCARLETT2_CONFIG_PAD_SWITCH,
2195 info->pad_input_count, private->pad_switch);
2200 if (info->air_input_count) {
2201 int err = scarlett2_usb_get_config(
2202 mixer, SCARLETT2_CONFIG_AIR_SWITCH,
2203 info->air_input_count, private->air_switch);
2208 if (info->phantom_count) {
2209 int err = scarlett2_usb_get_config(
2210 mixer, SCARLETT2_CONFIG_PHANTOM_SWITCH,
2211 info->phantom_count, private->phantom_switch);
2215 err = scarlett2_usb_get_config(
2216 mixer, SCARLETT2_CONFIG_PHANTOM_PERSISTENCE,
2217 1, &private->phantom_persistence);
2225 static int scarlett2_level_enum_ctl_info(struct snd_kcontrol *kctl,
2226 struct snd_ctl_elem_info *uinfo)
2228 static const char *const values[2] = {
2232 return snd_ctl_enum_info(uinfo, 1, 2, values);
2235 static int scarlett2_level_enum_ctl_get(struct snd_kcontrol *kctl,
2236 struct snd_ctl_elem_value *ucontrol)
2238 struct usb_mixer_elem_info *elem = kctl->private_data;
2239 struct usb_mixer_interface *mixer = elem->head.mixer;
2240 struct scarlett2_data *private = mixer->private_data;
2241 const struct scarlett2_device_info *info = private->info;
2243 int index = elem->control + info->level_input_first;
2245 mutex_lock(&private->data_mutex);
2246 if (private->input_other_updated)
2247 scarlett2_update_input_other(mixer);
2248 ucontrol->value.enumerated.item[0] = private->level_switch[index];
2249 mutex_unlock(&private->data_mutex);
2254 static int scarlett2_level_enum_ctl_put(struct snd_kcontrol *kctl,
2255 struct snd_ctl_elem_value *ucontrol)
2257 struct usb_mixer_elem_info *elem = kctl->private_data;
2258 struct usb_mixer_interface *mixer = elem->head.mixer;
2259 struct scarlett2_data *private = mixer->private_data;
2260 const struct scarlett2_device_info *info = private->info;
2262 int index = elem->control + info->level_input_first;
2263 int oval, val, err = 0;
2265 mutex_lock(&private->data_mutex);
2267 oval = private->level_switch[index];
2268 val = !!ucontrol->value.enumerated.item[0];
2273 private->level_switch[index] = val;
2275 /* Send switch change to the device */
2276 err = scarlett2_usb_set_config(mixer, SCARLETT2_CONFIG_LEVEL_SWITCH,
2282 mutex_unlock(&private->data_mutex);
2286 static const struct snd_kcontrol_new scarlett2_level_enum_ctl = {
2287 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2289 .info = scarlett2_level_enum_ctl_info,
2290 .get = scarlett2_level_enum_ctl_get,
2291 .put = scarlett2_level_enum_ctl_put,
2294 /*** Pad Switch Controls ***/
2296 static int scarlett2_pad_ctl_get(struct snd_kcontrol *kctl,
2297 struct snd_ctl_elem_value *ucontrol)
2299 struct usb_mixer_elem_info *elem = kctl->private_data;
2300 struct usb_mixer_interface *mixer = elem->head.mixer;
2301 struct scarlett2_data *private = mixer->private_data;
2303 mutex_lock(&private->data_mutex);
2304 if (private->input_other_updated)
2305 scarlett2_update_input_other(mixer);
2306 ucontrol->value.integer.value[0] =
2307 private->pad_switch[elem->control];
2308 mutex_unlock(&private->data_mutex);
2313 static int scarlett2_pad_ctl_put(struct snd_kcontrol *kctl,
2314 struct snd_ctl_elem_value *ucontrol)
2316 struct usb_mixer_elem_info *elem = kctl->private_data;
2317 struct usb_mixer_interface *mixer = elem->head.mixer;
2318 struct scarlett2_data *private = mixer->private_data;
2320 int index = elem->control;
2321 int oval, val, err = 0;
2323 mutex_lock(&private->data_mutex);
2325 oval = private->pad_switch[index];
2326 val = !!ucontrol->value.integer.value[0];
2331 private->pad_switch[index] = val;
2333 /* Send switch change to the device */
2334 err = scarlett2_usb_set_config(mixer, SCARLETT2_CONFIG_PAD_SWITCH,
2340 mutex_unlock(&private->data_mutex);
2344 static const struct snd_kcontrol_new scarlett2_pad_ctl = {
2345 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2347 .info = snd_ctl_boolean_mono_info,
2348 .get = scarlett2_pad_ctl_get,
2349 .put = scarlett2_pad_ctl_put,
2352 /*** Air Switch Controls ***/
2354 static int scarlett2_air_ctl_get(struct snd_kcontrol *kctl,
2355 struct snd_ctl_elem_value *ucontrol)
2357 struct usb_mixer_elem_info *elem = kctl->private_data;
2358 struct usb_mixer_interface *mixer = elem->head.mixer;
2359 struct scarlett2_data *private = mixer->private_data;
2361 mutex_lock(&private->data_mutex);
2362 if (private->input_other_updated)
2363 scarlett2_update_input_other(mixer);
2364 ucontrol->value.integer.value[0] = private->air_switch[elem->control];
2365 mutex_unlock(&private->data_mutex);
2370 static int scarlett2_air_ctl_put(struct snd_kcontrol *kctl,
2371 struct snd_ctl_elem_value *ucontrol)
2373 struct usb_mixer_elem_info *elem = kctl->private_data;
2374 struct usb_mixer_interface *mixer = elem->head.mixer;
2375 struct scarlett2_data *private = mixer->private_data;
2377 int index = elem->control;
2378 int oval, val, err = 0;
2380 mutex_lock(&private->data_mutex);
2382 oval = private->air_switch[index];
2383 val = !!ucontrol->value.integer.value[0];
2388 private->air_switch[index] = val;
2390 /* Send switch change to the device */
2391 err = scarlett2_usb_set_config(mixer, SCARLETT2_CONFIG_AIR_SWITCH,
2397 mutex_unlock(&private->data_mutex);
2401 static const struct snd_kcontrol_new scarlett2_air_ctl = {
2402 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2404 .info = snd_ctl_boolean_mono_info,
2405 .get = scarlett2_air_ctl_get,
2406 .put = scarlett2_air_ctl_put,
2409 /*** Phantom Switch Controls ***/
2411 static int scarlett2_phantom_ctl_get(struct snd_kcontrol *kctl,
2412 struct snd_ctl_elem_value *ucontrol)
2414 struct usb_mixer_elem_info *elem = kctl->private_data;
2415 struct usb_mixer_interface *mixer = elem->head.mixer;
2416 struct scarlett2_data *private = mixer->private_data;
2418 mutex_lock(&private->data_mutex);
2419 if (private->input_other_updated)
2420 scarlett2_update_input_other(mixer);
2421 ucontrol->value.integer.value[0] =
2422 private->phantom_switch[elem->control];
2423 mutex_unlock(&private->data_mutex);
2428 static int scarlett2_phantom_ctl_put(struct snd_kcontrol *kctl,
2429 struct snd_ctl_elem_value *ucontrol)
2431 struct usb_mixer_elem_info *elem = kctl->private_data;
2432 struct usb_mixer_interface *mixer = elem->head.mixer;
2433 struct scarlett2_data *private = mixer->private_data;
2435 int index = elem->control;
2436 int oval, val, err = 0;
2438 mutex_lock(&private->data_mutex);
2440 oval = private->phantom_switch[index];
2441 val = !!ucontrol->value.integer.value[0];
2446 private->phantom_switch[index] = val;
2448 /* Send switch change to the device */
2449 err = scarlett2_usb_set_config(mixer, SCARLETT2_CONFIG_PHANTOM_SWITCH,
2455 mutex_unlock(&private->data_mutex);
2459 static const struct snd_kcontrol_new scarlett2_phantom_ctl = {
2460 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2462 .info = snd_ctl_boolean_mono_info,
2463 .get = scarlett2_phantom_ctl_get,
2464 .put = scarlett2_phantom_ctl_put,
2467 /*** Phantom Persistence Control ***/
2469 static int scarlett2_phantom_persistence_ctl_get(
2470 struct snd_kcontrol *kctl, struct snd_ctl_elem_value *ucontrol)
2472 struct usb_mixer_elem_info *elem = kctl->private_data;
2473 struct scarlett2_data *private = elem->head.mixer->private_data;
2475 ucontrol->value.integer.value[0] = private->phantom_persistence;
2479 static int scarlett2_phantom_persistence_ctl_put(
2480 struct snd_kcontrol *kctl, struct snd_ctl_elem_value *ucontrol)
2482 struct usb_mixer_elem_info *elem = kctl->private_data;
2483 struct usb_mixer_interface *mixer = elem->head.mixer;
2484 struct scarlett2_data *private = mixer->private_data;
2486 int index = elem->control;
2487 int oval, val, err = 0;
2489 mutex_lock(&private->data_mutex);
2491 oval = private->phantom_persistence;
2492 val = !!ucontrol->value.integer.value[0];
2497 private->phantom_persistence = val;
2499 /* Send switch change to the device */
2500 err = scarlett2_usb_set_config(
2501 mixer, SCARLETT2_CONFIG_PHANTOM_PERSISTENCE, index, val);
2506 mutex_unlock(&private->data_mutex);
2510 static const struct snd_kcontrol_new scarlett2_phantom_persistence_ctl = {
2511 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2513 .info = snd_ctl_boolean_mono_info,
2514 .get = scarlett2_phantom_persistence_ctl_get,
2515 .put = scarlett2_phantom_persistence_ctl_put,
2518 /*** Direct Monitor Control ***/
2520 static int scarlett2_update_monitor_other(struct usb_mixer_interface *mixer)
2522 struct scarlett2_data *private = mixer->private_data;
2523 const struct scarlett2_device_info *info = private->info;
2526 /* monitor_other_enable[0] enables speaker switching
2527 * monitor_other_enable[1] enables talkback
2529 u8 monitor_other_enable[2];
2531 /* monitor_other_switch[0] activates the alternate speakers
2532 * monitor_other_switch[1] activates talkback
2534 u8 monitor_other_switch[2];
2536 private->monitor_other_updated = 0;
2538 if (info->direct_monitor)
2539 return scarlett2_usb_get_config(
2540 mixer, SCARLETT2_CONFIG_DIRECT_MONITOR,
2541 1, &private->direct_monitor_switch);
2543 /* if it doesn't do speaker switching then it also doesn't do
2546 if (!info->has_speaker_switching)
2549 err = scarlett2_usb_get_config(
2550 mixer, SCARLETT2_CONFIG_MONITOR_OTHER_ENABLE,
2551 2, monitor_other_enable);
2555 err = scarlett2_usb_get_config(
2556 mixer, SCARLETT2_CONFIG_MONITOR_OTHER_SWITCH,
2557 2, monitor_other_switch);
2561 if (!monitor_other_enable[0])
2562 private->speaker_switching_switch = 0;
2564 private->speaker_switching_switch = monitor_other_switch[0] + 1;
2566 if (info->has_talkback) {
2567 const int (*port_count)[SCARLETT2_PORT_DIRNS] =
2570 port_count[SCARLETT2_PORT_TYPE_MIX][SCARLETT2_PORT_IN];
2574 if (!monitor_other_enable[1])
2575 private->talkback_switch = 0;
2577 private->talkback_switch = monitor_other_switch[1] + 1;
2579 err = scarlett2_usb_get_config(mixer,
2580 SCARLETT2_CONFIG_TALKBACK_MAP,
2584 for (i = 0; i < num_mixes; i++, bitmap >>= 1)
2585 private->talkback_map[i] = bitmap & 1;
2591 static int scarlett2_direct_monitor_ctl_get(
2592 struct snd_kcontrol *kctl, struct snd_ctl_elem_value *ucontrol)
2594 struct usb_mixer_elem_info *elem = kctl->private_data;
2595 struct usb_mixer_interface *mixer = elem->head.mixer;
2596 struct scarlett2_data *private = elem->head.mixer->private_data;
2598 mutex_lock(&private->data_mutex);
2599 if (private->monitor_other_updated)
2600 scarlett2_update_monitor_other(mixer);
2601 ucontrol->value.enumerated.item[0] = private->direct_monitor_switch;
2602 mutex_unlock(&private->data_mutex);
2607 static int scarlett2_direct_monitor_ctl_put(
2608 struct snd_kcontrol *kctl, struct snd_ctl_elem_value *ucontrol)
2610 struct usb_mixer_elem_info *elem = kctl->private_data;
2611 struct usb_mixer_interface *mixer = elem->head.mixer;
2612 struct scarlett2_data *private = mixer->private_data;
2614 int index = elem->control;
2615 int oval, val, err = 0;
2617 mutex_lock(&private->data_mutex);
2619 oval = private->direct_monitor_switch;
2620 val = min(ucontrol->value.enumerated.item[0], 2U);
2625 private->direct_monitor_switch = val;
2627 /* Send switch change to the device */
2628 err = scarlett2_usb_set_config(
2629 mixer, SCARLETT2_CONFIG_DIRECT_MONITOR, index, val);
2634 mutex_unlock(&private->data_mutex);
2638 static int scarlett2_direct_monitor_stereo_enum_ctl_info(
2639 struct snd_kcontrol *kctl, struct snd_ctl_elem_info *uinfo)
2641 static const char *const values[3] = {
2642 "Off", "Mono", "Stereo"
2645 return snd_ctl_enum_info(uinfo, 1, 3, values);
2648 /* Direct Monitor for Solo is mono-only and only needs a boolean control
2649 * Direct Monitor for 2i2 is selectable between Off/Mono/Stereo
2651 static const struct snd_kcontrol_new scarlett2_direct_monitor_ctl[2] = {
2653 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2655 .info = snd_ctl_boolean_mono_info,
2656 .get = scarlett2_direct_monitor_ctl_get,
2657 .put = scarlett2_direct_monitor_ctl_put,
2660 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2662 .info = scarlett2_direct_monitor_stereo_enum_ctl_info,
2663 .get = scarlett2_direct_monitor_ctl_get,
2664 .put = scarlett2_direct_monitor_ctl_put,
2668 static int scarlett2_add_direct_monitor_ctl(struct usb_mixer_interface *mixer)
2670 struct scarlett2_data *private = mixer->private_data;
2671 const struct scarlett2_device_info *info = private->info;
2674 if (!info->direct_monitor)
2677 s = info->direct_monitor == 1
2678 ? "Direct Monitor Playback Switch"
2679 : "Direct Monitor Playback Enum";
2681 return scarlett2_add_new_ctl(
2682 mixer, &scarlett2_direct_monitor_ctl[info->direct_monitor - 1],
2683 0, 1, s, &private->direct_monitor_ctl);
2686 /*** Speaker Switching Control ***/
2688 static int scarlett2_speaker_switch_enum_ctl_info(
2689 struct snd_kcontrol *kctl, struct snd_ctl_elem_info *uinfo)
2691 static const char *const values[3] = {
2692 "Off", "Main", "Alt"
2695 return snd_ctl_enum_info(uinfo, 1, 3, values);
2698 static int scarlett2_speaker_switch_enum_ctl_get(
2699 struct snd_kcontrol *kctl, struct snd_ctl_elem_value *ucontrol)
2701 struct usb_mixer_elem_info *elem = kctl->private_data;
2702 struct usb_mixer_interface *mixer = elem->head.mixer;
2703 struct scarlett2_data *private = mixer->private_data;
2705 mutex_lock(&private->data_mutex);
2706 if (private->monitor_other_updated)
2707 scarlett2_update_monitor_other(mixer);
2708 ucontrol->value.enumerated.item[0] = private->speaker_switching_switch;
2709 mutex_unlock(&private->data_mutex);
2714 /* when speaker switching gets enabled, switch the main/alt speakers
2715 * to HW volume and disable those controls
2717 static int scarlett2_speaker_switch_enable(struct usb_mixer_interface *mixer)
2719 struct snd_card *card = mixer->chip->card;
2720 struct scarlett2_data *private = mixer->private_data;
2723 for (i = 0; i < 4; i++) {
2724 int index = line_out_remap(private, i);
2726 /* switch the main/alt speakers to HW volume */
2727 if (!private->vol_sw_hw_switch[index]) {
2728 err = scarlett2_sw_hw_change(private->mixer, i, 1);
2733 /* disable the line out SW/HW switch */
2734 scarlett2_sw_hw_ctl_ro(private, i);
2735 snd_ctl_notify(card,
2736 SNDRV_CTL_EVENT_MASK_VALUE |
2737 SNDRV_CTL_EVENT_MASK_INFO,
2738 &private->sw_hw_ctls[i]->id);
2741 /* when the next monitor-other notify comes in, update the mux
2744 private->speaker_switching_switched = 1;
2749 /* when speaker switching gets disabled, reenable the hw/sw controls
2750 * and invalidate the routing
2752 static void scarlett2_speaker_switch_disable(struct usb_mixer_interface *mixer)
2754 struct snd_card *card = mixer->chip->card;
2755 struct scarlett2_data *private = mixer->private_data;
2758 /* enable the line out SW/HW switch */
2759 for (i = 0; i < 4; i++) {
2760 scarlett2_sw_hw_ctl_rw(private, i);
2761 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_INFO,
2762 &private->sw_hw_ctls[i]->id);
2765 /* when the next monitor-other notify comes in, update the mux
2768 private->speaker_switching_switched = 1;
2771 static int scarlett2_speaker_switch_enum_ctl_put(
2772 struct snd_kcontrol *kctl, struct snd_ctl_elem_value *ucontrol)
2774 struct usb_mixer_elem_info *elem = kctl->private_data;
2775 struct usb_mixer_interface *mixer = elem->head.mixer;
2776 struct scarlett2_data *private = mixer->private_data;
2778 int oval, val, err = 0;
2780 mutex_lock(&private->data_mutex);
2782 oval = private->speaker_switching_switch;
2783 val = min(ucontrol->value.enumerated.item[0], 2U);
2788 private->speaker_switching_switch = val;
2790 /* enable/disable speaker switching */
2791 err = scarlett2_usb_set_config(
2792 mixer, SCARLETT2_CONFIG_MONITOR_OTHER_ENABLE,
2797 /* if speaker switching is enabled, select main or alt */
2798 err = scarlett2_usb_set_config(
2799 mixer, SCARLETT2_CONFIG_MONITOR_OTHER_SWITCH,
2804 /* update controls if speaker switching gets enabled or disabled */
2806 err = scarlett2_speaker_switch_enable(mixer);
2807 else if (oval && !val)
2808 scarlett2_speaker_switch_disable(mixer);
2814 mutex_unlock(&private->data_mutex);
2818 static const struct snd_kcontrol_new scarlett2_speaker_switch_enum_ctl = {
2819 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2821 .info = scarlett2_speaker_switch_enum_ctl_info,
2822 .get = scarlett2_speaker_switch_enum_ctl_get,
2823 .put = scarlett2_speaker_switch_enum_ctl_put,
2826 static int scarlett2_add_speaker_switch_ctl(
2827 struct usb_mixer_interface *mixer)
2829 struct scarlett2_data *private = mixer->private_data;
2830 const struct scarlett2_device_info *info = private->info;
2832 if (!info->has_speaker_switching)
2835 return scarlett2_add_new_ctl(
2836 mixer, &scarlett2_speaker_switch_enum_ctl,
2837 0, 1, "Speaker Switching Playback Enum",
2838 &private->speaker_switching_ctl);
2841 /*** Talkback and Talkback Map Controls ***/
2843 static int scarlett2_talkback_enum_ctl_info(
2844 struct snd_kcontrol *kctl, struct snd_ctl_elem_info *uinfo)
2846 static const char *const values[3] = {
2847 "Disabled", "Off", "On"
2850 return snd_ctl_enum_info(uinfo, 1, 3, values);
2853 static int scarlett2_talkback_enum_ctl_get(
2854 struct snd_kcontrol *kctl, struct snd_ctl_elem_value *ucontrol)
2856 struct usb_mixer_elem_info *elem = kctl->private_data;
2857 struct usb_mixer_interface *mixer = elem->head.mixer;
2858 struct scarlett2_data *private = mixer->private_data;
2860 mutex_lock(&private->data_mutex);
2861 if (private->monitor_other_updated)
2862 scarlett2_update_monitor_other(mixer);
2863 ucontrol->value.enumerated.item[0] = private->talkback_switch;
2864 mutex_unlock(&private->data_mutex);
2869 static int scarlett2_talkback_enum_ctl_put(
2870 struct snd_kcontrol *kctl, struct snd_ctl_elem_value *ucontrol)
2872 struct usb_mixer_elem_info *elem = kctl->private_data;
2873 struct usb_mixer_interface *mixer = elem->head.mixer;
2874 struct scarlett2_data *private = mixer->private_data;
2876 int oval, val, err = 0;
2878 mutex_lock(&private->data_mutex);
2880 oval = private->talkback_switch;
2881 val = min(ucontrol->value.enumerated.item[0], 2U);
2886 private->talkback_switch = val;
2888 /* enable/disable talkback */
2889 err = scarlett2_usb_set_config(
2890 mixer, SCARLETT2_CONFIG_MONITOR_OTHER_ENABLE,
2895 /* if talkback is enabled, select main or alt */
2896 err = scarlett2_usb_set_config(
2897 mixer, SCARLETT2_CONFIG_MONITOR_OTHER_SWITCH,
2903 mutex_unlock(&private->data_mutex);
2907 static const struct snd_kcontrol_new scarlett2_talkback_enum_ctl = {
2908 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2910 .info = scarlett2_talkback_enum_ctl_info,
2911 .get = scarlett2_talkback_enum_ctl_get,
2912 .put = scarlett2_talkback_enum_ctl_put,
2915 static int scarlett2_talkback_map_ctl_get(
2916 struct snd_kcontrol *kctl, struct snd_ctl_elem_value *ucontrol)
2918 struct usb_mixer_elem_info *elem = kctl->private_data;
2919 struct usb_mixer_interface *mixer = elem->head.mixer;
2920 struct scarlett2_data *private = mixer->private_data;
2921 int index = elem->control;
2923 ucontrol->value.integer.value[0] = private->talkback_map[index];
2928 static int scarlett2_talkback_map_ctl_put(
2929 struct snd_kcontrol *kctl, struct snd_ctl_elem_value *ucontrol)
2931 struct usb_mixer_elem_info *elem = kctl->private_data;
2932 struct usb_mixer_interface *mixer = elem->head.mixer;
2933 struct scarlett2_data *private = mixer->private_data;
2934 const int (*port_count)[SCARLETT2_PORT_DIRNS] =
2935 private->info->port_count;
2936 int num_mixes = port_count[SCARLETT2_PORT_TYPE_MIX][SCARLETT2_PORT_IN];
2938 int index = elem->control;
2939 int oval, val, err = 0, i;
2942 mutex_lock(&private->data_mutex);
2944 oval = private->talkback_map[index];
2945 val = !!ucontrol->value.integer.value[0];
2950 private->talkback_map[index] = val;
2952 for (i = 0; i < num_mixes; i++)
2953 bitmap |= private->talkback_map[i] << i;
2955 /* Send updated bitmap to the device */
2956 err = scarlett2_usb_set_config(mixer, SCARLETT2_CONFIG_TALKBACK_MAP,
2962 mutex_unlock(&private->data_mutex);
2966 static const struct snd_kcontrol_new scarlett2_talkback_map_ctl = {
2967 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2969 .info = snd_ctl_boolean_mono_info,
2970 .get = scarlett2_talkback_map_ctl_get,
2971 .put = scarlett2_talkback_map_ctl_put,
2974 static int scarlett2_add_talkback_ctls(
2975 struct usb_mixer_interface *mixer)
2977 struct scarlett2_data *private = mixer->private_data;
2978 const struct scarlett2_device_info *info = private->info;
2979 const int (*port_count)[SCARLETT2_PORT_DIRNS] = info->port_count;
2980 int num_mixes = port_count[SCARLETT2_PORT_TYPE_MIX][SCARLETT2_PORT_IN];
2982 char s[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
2984 if (!info->has_talkback)
2987 err = scarlett2_add_new_ctl(
2988 mixer, &scarlett2_talkback_enum_ctl,
2989 0, 1, "Talkback Playback Enum",
2990 &private->talkback_ctl);
2994 for (i = 0; i < num_mixes; i++) {
2995 snprintf(s, sizeof(s),
2996 "Talkback Mix %c Playback Switch", i + 'A');
2997 err = scarlett2_add_new_ctl(mixer, &scarlett2_talkback_map_ctl,
3006 /*** Dim/Mute Controls ***/
3008 static int scarlett2_dim_mute_ctl_get(struct snd_kcontrol *kctl,
3009 struct snd_ctl_elem_value *ucontrol)
3011 struct usb_mixer_elem_info *elem = kctl->private_data;
3012 struct usb_mixer_interface *mixer = elem->head.mixer;
3013 struct scarlett2_data *private = mixer->private_data;
3015 mutex_lock(&private->data_mutex);
3016 if (private->vol_updated)
3017 scarlett2_update_volumes(mixer);
3018 mutex_unlock(&private->data_mutex);
3020 ucontrol->value.integer.value[0] = private->dim_mute[elem->control];
3024 static int scarlett2_dim_mute_ctl_put(struct snd_kcontrol *kctl,
3025 struct snd_ctl_elem_value *ucontrol)
3027 struct usb_mixer_elem_info *elem = kctl->private_data;
3028 struct usb_mixer_interface *mixer = elem->head.mixer;
3029 struct scarlett2_data *private = mixer->private_data;
3030 const struct scarlett2_device_info *info = private->info;
3031 const int (*port_count)[SCARLETT2_PORT_DIRNS] = info->port_count;
3033 port_count[SCARLETT2_PORT_TYPE_ANALOGUE][SCARLETT2_PORT_OUT];
3035 int index = elem->control;
3036 int oval, val, err = 0, i;
3038 mutex_lock(&private->data_mutex);
3040 oval = private->dim_mute[index];
3041 val = !!ucontrol->value.integer.value[0];
3046 private->dim_mute[index] = val;
3048 /* Send switch change to the device */
3049 err = scarlett2_usb_set_config(mixer, SCARLETT2_CONFIG_DIM_MUTE,
3054 if (index == SCARLETT2_BUTTON_MUTE)
3055 for (i = 0; i < num_line_out; i++) {
3056 int line_index = line_out_remap(private, i);
3058 if (private->vol_sw_hw_switch[line_index]) {
3059 private->mute_switch[line_index] = val;
3060 snd_ctl_notify(mixer->chip->card,
3061 SNDRV_CTL_EVENT_MASK_VALUE,
3062 &private->mute_ctls[i]->id);
3067 mutex_unlock(&private->data_mutex);
3071 static const struct snd_kcontrol_new scarlett2_dim_mute_ctl = {
3072 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3074 .info = snd_ctl_boolean_mono_info,
3075 .get = scarlett2_dim_mute_ctl_get,
3076 .put = scarlett2_dim_mute_ctl_put
3079 /*** Create the analogue output controls ***/
3081 static int scarlett2_add_line_out_ctls(struct usb_mixer_interface *mixer)
3083 struct scarlett2_data *private = mixer->private_data;
3084 const struct scarlett2_device_info *info = private->info;
3085 const int (*port_count)[SCARLETT2_PORT_DIRNS] = info->port_count;
3087 port_count[SCARLETT2_PORT_TYPE_ANALOGUE][SCARLETT2_PORT_OUT];
3089 char s[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
3091 /* Add R/O HW volume control */
3092 if (info->line_out_hw_vol) {
3093 snprintf(s, sizeof(s), "Master HW Playback Volume");
3094 err = scarlett2_add_new_ctl(mixer,
3095 &scarlett2_master_volume_ctl,
3096 0, 1, s, &private->master_vol_ctl);
3101 /* Add volume controls */
3102 for (i = 0; i < num_line_out; i++) {
3103 int index = line_out_remap(private, i);
3106 if (info->line_out_descrs[i])
3107 snprintf(s, sizeof(s),
3108 "Line %02d (%s) Playback Volume",
3109 i + 1, info->line_out_descrs[i]);
3111 snprintf(s, sizeof(s),
3112 "Line %02d Playback Volume",
3114 err = scarlett2_add_new_ctl(mixer,
3115 &scarlett2_line_out_volume_ctl,
3116 i, 1, s, &private->vol_ctls[i]);
3121 snprintf(s, sizeof(s),
3122 "Line %02d Mute Playback Switch",
3124 err = scarlett2_add_new_ctl(mixer,
3125 &scarlett2_mute_ctl,
3127 &private->mute_ctls[i]);
3131 /* Make the fader and mute controls read-only if the
3132 * SW/HW switch is set to HW
3134 if (private->vol_sw_hw_switch[index])
3135 scarlett2_vol_ctl_set_writable(mixer, i, 0);
3138 if (info->line_out_hw_vol) {
3139 snprintf(s, sizeof(s),
3140 "Line Out %02d Volume Control Playback Enum",
3142 err = scarlett2_add_new_ctl(mixer,
3143 &scarlett2_sw_hw_enum_ctl,
3145 &private->sw_hw_ctls[i]);
3149 /* Make the switch read-only if the line is
3150 * involved in speaker switching
3152 if (private->speaker_switching_switch && i < 4)
3153 scarlett2_sw_hw_ctl_ro(private, i);
3157 /* Add dim/mute controls */
3158 if (info->line_out_hw_vol)
3159 for (i = 0; i < SCARLETT2_DIM_MUTE_COUNT; i++) {
3160 err = scarlett2_add_new_ctl(
3161 mixer, &scarlett2_dim_mute_ctl,
3162 i, 1, scarlett2_dim_mute_names[i],
3163 &private->dim_mute_ctls[i]);
3171 /*** Create the analogue input controls ***/
3173 static int scarlett2_add_line_in_ctls(struct usb_mixer_interface *mixer)
3175 struct scarlett2_data *private = mixer->private_data;
3176 const struct scarlett2_device_info *info = private->info;
3178 char s[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
3179 const char *fmt = "Line In %d %s Capture %s";
3180 const char *fmt2 = "Line In %d-%d %s Capture %s";
3182 /* Add input level (line/inst) controls */
3183 for (i = 0; i < info->level_input_count; i++) {
3184 snprintf(s, sizeof(s), fmt, i + 1 + info->level_input_first,
3186 err = scarlett2_add_new_ctl(mixer, &scarlett2_level_enum_ctl,
3187 i, 1, s, &private->level_ctls[i]);
3192 /* Add input pad controls */
3193 for (i = 0; i < info->pad_input_count; i++) {
3194 snprintf(s, sizeof(s), fmt, i + 1, "Pad", "Switch");
3195 err = scarlett2_add_new_ctl(mixer, &scarlett2_pad_ctl,
3196 i, 1, s, &private->pad_ctls[i]);
3201 /* Add input air controls */
3202 for (i = 0; i < info->air_input_count; i++) {
3203 snprintf(s, sizeof(s), fmt, i + 1, "Air", "Switch");
3204 err = scarlett2_add_new_ctl(mixer, &scarlett2_air_ctl,
3205 i, 1, s, &private->air_ctls[i]);
3210 /* Add input phantom controls */
3211 if (info->inputs_per_phantom == 1) {
3212 for (i = 0; i < info->phantom_count; i++) {
3213 scnprintf(s, sizeof(s), fmt, i + 1,
3214 "Phantom Power", "Switch");
3215 err = scarlett2_add_new_ctl(
3216 mixer, &scarlett2_phantom_ctl,
3217 i, 1, s, &private->phantom_ctls[i]);
3221 } else if (info->inputs_per_phantom > 1) {
3222 for (i = 0; i < info->phantom_count; i++) {
3223 int from = i * info->inputs_per_phantom + 1;
3224 int to = (i + 1) * info->inputs_per_phantom;
3226 scnprintf(s, sizeof(s), fmt2, from, to,
3227 "Phantom Power", "Switch");
3228 err = scarlett2_add_new_ctl(
3229 mixer, &scarlett2_phantom_ctl,
3230 i, 1, s, &private->phantom_ctls[i]);
3235 if (info->phantom_count) {
3236 err = scarlett2_add_new_ctl(
3237 mixer, &scarlett2_phantom_persistence_ctl, 0, 1,
3238 "Phantom Power Persistence Capture Switch", NULL);
3246 /*** Mixer Volume Controls ***/
3248 static int scarlett2_mixer_ctl_info(struct snd_kcontrol *kctl,
3249 struct snd_ctl_elem_info *uinfo)
3251 struct usb_mixer_elem_info *elem = kctl->private_data;
3253 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
3254 uinfo->count = elem->channels;
3255 uinfo->value.integer.min = 0;
3256 uinfo->value.integer.max = SCARLETT2_MIXER_MAX_VALUE;
3257 uinfo->value.integer.step = 1;
3261 static int scarlett2_mixer_ctl_get(struct snd_kcontrol *kctl,
3262 struct snd_ctl_elem_value *ucontrol)
3264 struct usb_mixer_elem_info *elem = kctl->private_data;
3265 struct scarlett2_data *private = elem->head.mixer->private_data;
3267 ucontrol->value.integer.value[0] = private->mix[elem->control];
3271 static int scarlett2_mixer_ctl_put(struct snd_kcontrol *kctl,
3272 struct snd_ctl_elem_value *ucontrol)
3274 struct usb_mixer_elem_info *elem = kctl->private_data;
3275 struct usb_mixer_interface *mixer = elem->head.mixer;
3276 struct scarlett2_data *private = mixer->private_data;
3277 const struct scarlett2_device_info *info = private->info;
3278 const int (*port_count)[SCARLETT2_PORT_DIRNS] = info->port_count;
3279 int oval, val, num_mixer_in, mix_num, err = 0;
3280 int index = elem->control;
3282 mutex_lock(&private->data_mutex);
3284 oval = private->mix[index];
3285 val = ucontrol->value.integer.value[0];
3286 num_mixer_in = port_count[SCARLETT2_PORT_TYPE_MIX][SCARLETT2_PORT_OUT];
3287 mix_num = index / num_mixer_in;
3292 private->mix[index] = val;
3293 err = scarlett2_usb_set_mix(mixer, mix_num);
3298 mutex_unlock(&private->data_mutex);
3302 static const DECLARE_TLV_DB_MINMAX(
3303 db_scale_scarlett2_mixer,
3304 SCARLETT2_MIXER_MIN_DB * 100,
3305 SCARLETT2_MIXER_MAX_DB * 100
3308 static const struct snd_kcontrol_new scarlett2_mixer_ctl = {
3309 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3310 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE |
3311 SNDRV_CTL_ELEM_ACCESS_TLV_READ,
3313 .info = scarlett2_mixer_ctl_info,
3314 .get = scarlett2_mixer_ctl_get,
3315 .put = scarlett2_mixer_ctl_put,
3316 .private_value = SCARLETT2_MIXER_MAX_DB, /* max value */
3317 .tlv = { .p = db_scale_scarlett2_mixer }
3320 static int scarlett2_add_mixer_ctls(struct usb_mixer_interface *mixer)
3322 struct scarlett2_data *private = mixer->private_data;
3323 const struct scarlett2_device_info *info = private->info;
3324 const int (*port_count)[SCARLETT2_PORT_DIRNS] = info->port_count;
3327 char s[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
3330 port_count[SCARLETT2_PORT_TYPE_MIX][SCARLETT2_PORT_OUT];
3332 port_count[SCARLETT2_PORT_TYPE_MIX][SCARLETT2_PORT_IN];
3334 for (i = 0, index = 0; i < num_outputs; i++)
3335 for (j = 0; j < num_inputs; j++, index++) {
3336 snprintf(s, sizeof(s),
3337 "Mix %c Input %02d Playback Volume",
3339 err = scarlett2_add_new_ctl(mixer, &scarlett2_mixer_ctl,
3348 /*** Mux Source Selection Controls ***/
3350 static int scarlett2_mux_src_enum_ctl_info(struct snd_kcontrol *kctl,
3351 struct snd_ctl_elem_info *uinfo)
3353 struct usb_mixer_elem_info *elem = kctl->private_data;
3354 struct scarlett2_data *private = elem->head.mixer->private_data;
3355 const struct scarlett2_device_info *info = private->info;
3356 const int (*port_count)[SCARLETT2_PORT_DIRNS] = info->port_count;
3357 unsigned int item = uinfo->value.enumerated.item;
3358 int items = private->num_mux_srcs;
3361 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
3362 uinfo->count = elem->channels;
3363 uinfo->value.enumerated.items = items;
3366 item = uinfo->value.enumerated.item = items - 1;
3369 port_type < SCARLETT2_PORT_TYPE_COUNT;
3371 if (item < port_count[port_type][SCARLETT2_PORT_IN]) {
3372 const struct scarlett2_port *port =
3373 &scarlett2_ports[port_type];
3375 sprintf(uinfo->value.enumerated.name,
3376 port->src_descr, item + port->src_num_offset);
3379 item -= port_count[port_type][SCARLETT2_PORT_IN];
3385 static int scarlett2_mux_src_enum_ctl_get(struct snd_kcontrol *kctl,
3386 struct snd_ctl_elem_value *ucontrol)
3388 struct usb_mixer_elem_info *elem = kctl->private_data;
3389 struct usb_mixer_interface *mixer = elem->head.mixer;
3390 struct scarlett2_data *private = mixer->private_data;
3391 const struct scarlett2_device_info *info = private->info;
3392 const int (*port_count)[SCARLETT2_PORT_DIRNS] = info->port_count;
3393 int line_out_count =
3394 port_count[SCARLETT2_PORT_TYPE_ANALOGUE][SCARLETT2_PORT_OUT];
3395 int index = elem->control;
3397 if (index < line_out_count)
3398 index = line_out_remap(private, index);
3400 mutex_lock(&private->data_mutex);
3401 if (private->mux_updated)
3402 scarlett2_usb_get_mux(mixer);
3403 ucontrol->value.enumerated.item[0] = private->mux[index];
3404 mutex_unlock(&private->data_mutex);
3409 static int scarlett2_mux_src_enum_ctl_put(struct snd_kcontrol *kctl,
3410 struct snd_ctl_elem_value *ucontrol)
3412 struct usb_mixer_elem_info *elem = kctl->private_data;
3413 struct usb_mixer_interface *mixer = elem->head.mixer;
3414 struct scarlett2_data *private = mixer->private_data;
3415 const struct scarlett2_device_info *info = private->info;
3416 const int (*port_count)[SCARLETT2_PORT_DIRNS] = info->port_count;
3417 int line_out_count =
3418 port_count[SCARLETT2_PORT_TYPE_ANALOGUE][SCARLETT2_PORT_OUT];
3419 int index = elem->control;
3420 int oval, val, err = 0;
3422 if (index < line_out_count)
3423 index = line_out_remap(private, index);
3425 mutex_lock(&private->data_mutex);
3427 oval = private->mux[index];
3428 val = min(ucontrol->value.enumerated.item[0],
3429 private->num_mux_srcs - 1U);
3434 private->mux[index] = val;
3435 err = scarlett2_usb_set_mux(mixer);
3440 mutex_unlock(&private->data_mutex);
3444 static const struct snd_kcontrol_new scarlett2_mux_src_enum_ctl = {
3445 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3447 .info = scarlett2_mux_src_enum_ctl_info,
3448 .get = scarlett2_mux_src_enum_ctl_get,
3449 .put = scarlett2_mux_src_enum_ctl_put,
3452 static int scarlett2_add_mux_enums(struct usb_mixer_interface *mixer)
3454 struct scarlett2_data *private = mixer->private_data;
3455 const struct scarlett2_device_info *info = private->info;
3456 const int (*port_count)[SCARLETT2_PORT_DIRNS] = info->port_count;
3457 int port_type, channel, i;
3459 for (i = 0, port_type = 0;
3460 port_type < SCARLETT2_PORT_TYPE_COUNT;
3463 channel < port_count[port_type][SCARLETT2_PORT_OUT];
3466 char s[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
3467 const char *const descr =
3468 scarlett2_ports[port_type].dst_descr;
3470 snprintf(s, sizeof(s) - 5, descr, channel + 1);
3473 err = scarlett2_add_new_ctl(mixer,
3474 &scarlett2_mux_src_enum_ctl,
3476 &private->mux_ctls[i]);
3485 /*** Meter Controls ***/
3487 static int scarlett2_meter_ctl_info(struct snd_kcontrol *kctl,
3488 struct snd_ctl_elem_info *uinfo)
3490 struct usb_mixer_elem_info *elem = kctl->private_data;
3492 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
3493 uinfo->count = elem->channels;
3494 uinfo->value.integer.min = 0;
3495 uinfo->value.integer.max = 4095;
3496 uinfo->value.integer.step = 1;
3500 static int scarlett2_meter_ctl_get(struct snd_kcontrol *kctl,
3501 struct snd_ctl_elem_value *ucontrol)
3503 struct usb_mixer_elem_info *elem = kctl->private_data;
3504 u16 meter_levels[SCARLETT2_MAX_METERS];
3507 err = scarlett2_usb_get_meter_levels(elem->head.mixer, elem->channels,
3512 for (i = 0; i < elem->channels; i++)
3513 ucontrol->value.integer.value[i] = meter_levels[i];
3518 static const struct snd_kcontrol_new scarlett2_meter_ctl = {
3519 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
3520 .access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE,
3522 .info = scarlett2_meter_ctl_info,
3523 .get = scarlett2_meter_ctl_get
3526 static int scarlett2_add_meter_ctl(struct usb_mixer_interface *mixer)
3528 struct scarlett2_data *private = mixer->private_data;
3530 /* devices without a mixer also don't support reporting levels */
3531 if (private->info->config_set == SCARLETT2_CONFIG_SET_NO_MIXER)
3534 return scarlett2_add_new_ctl(mixer, &scarlett2_meter_ctl,
3535 0, private->num_mux_dsts,
3536 "Level Meter", NULL);
3539 /*** MSD Controls ***/
3541 static int scarlett2_msd_ctl_get(struct snd_kcontrol *kctl,
3542 struct snd_ctl_elem_value *ucontrol)
3544 struct usb_mixer_elem_info *elem = kctl->private_data;
3545 struct scarlett2_data *private = elem->head.mixer->private_data;
3547 ucontrol->value.integer.value[0] = private->msd_switch;
3551 static int scarlett2_msd_ctl_put(struct snd_kcontrol *kctl,
3552 struct snd_ctl_elem_value *ucontrol)
3554 struct usb_mixer_elem_info *elem = kctl->private_data;
3555 struct usb_mixer_interface *mixer = elem->head.mixer;
3556 struct scarlett2_data *private = mixer->private_data;
3558 int oval, val, err = 0;
3560 mutex_lock(&private->data_mutex);
3562 oval = private->msd_switch;
3563 val = !!ucontrol->value.integer.value[0];
3568 private->msd_switch = val;
3570 /* Send switch change to the device */
3571 err = scarlett2_usb_set_config(mixer, SCARLETT2_CONFIG_MSD_SWITCH,
3577 mutex_unlock(&private->data_mutex);
3581 static const struct snd_kcontrol_new scarlett2_msd_ctl = {
3582 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3584 .info = snd_ctl_boolean_mono_info,
3585 .get = scarlett2_msd_ctl_get,
3586 .put = scarlett2_msd_ctl_put,
3589 static int scarlett2_add_msd_ctl(struct usb_mixer_interface *mixer)
3591 struct scarlett2_data *private = mixer->private_data;
3592 const struct scarlett2_device_info *info = private->info;
3594 if (!info->has_msd_mode)
3597 /* If MSD mode is off, hide the switch by default */
3598 if (!private->msd_switch && !(mixer->chip->setup & SCARLETT2_MSD_ENABLE))
3601 /* Add MSD control */
3602 return scarlett2_add_new_ctl(mixer, &scarlett2_msd_ctl,
3603 0, 1, "MSD Mode Switch", NULL);
3606 /*** Standalone Control ***/
3608 static int scarlett2_standalone_ctl_get(struct snd_kcontrol *kctl,
3609 struct snd_ctl_elem_value *ucontrol)
3611 struct usb_mixer_elem_info *elem = kctl->private_data;
3612 struct scarlett2_data *private = elem->head.mixer->private_data;
3614 ucontrol->value.integer.value[0] = private->standalone_switch;
3618 static int scarlett2_standalone_ctl_put(struct snd_kcontrol *kctl,
3619 struct snd_ctl_elem_value *ucontrol)
3621 struct usb_mixer_elem_info *elem = kctl->private_data;
3622 struct usb_mixer_interface *mixer = elem->head.mixer;
3623 struct scarlett2_data *private = mixer->private_data;
3625 int oval, val, err = 0;
3627 mutex_lock(&private->data_mutex);
3629 oval = private->standalone_switch;
3630 val = !!ucontrol->value.integer.value[0];
3635 private->standalone_switch = val;
3637 /* Send switch change to the device */
3638 err = scarlett2_usb_set_config(mixer,
3639 SCARLETT2_CONFIG_STANDALONE_SWITCH,
3645 mutex_unlock(&private->data_mutex);
3649 static const struct snd_kcontrol_new scarlett2_standalone_ctl = {
3650 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3652 .info = snd_ctl_boolean_mono_info,
3653 .get = scarlett2_standalone_ctl_get,
3654 .put = scarlett2_standalone_ctl_put,
3657 static int scarlett2_add_standalone_ctl(struct usb_mixer_interface *mixer)
3659 struct scarlett2_data *private = mixer->private_data;
3661 if (private->info->config_set == SCARLETT2_CONFIG_SET_NO_MIXER)
3664 /* Add standalone control */
3665 return scarlett2_add_new_ctl(mixer, &scarlett2_standalone_ctl,
3666 0, 1, "Standalone Switch", NULL);
3669 /*** Cleanup/Suspend Callbacks ***/
3671 static void scarlett2_private_free(struct usb_mixer_interface *mixer)
3673 struct scarlett2_data *private = mixer->private_data;
3675 cancel_delayed_work_sync(&private->work);
3677 mixer->private_data = NULL;
3680 static void scarlett2_private_suspend(struct usb_mixer_interface *mixer)
3682 struct scarlett2_data *private = mixer->private_data;
3684 if (cancel_delayed_work_sync(&private->work))
3685 scarlett2_config_save(private->mixer);
3688 /*** Initialisation ***/
3690 static void scarlett2_count_mux_io(struct scarlett2_data *private)
3692 const struct scarlett2_device_info *info = private->info;
3693 const int (*port_count)[SCARLETT2_PORT_DIRNS] = info->port_count;
3694 int port_type, srcs = 0, dsts = 0;
3697 port_type < SCARLETT2_PORT_TYPE_COUNT;
3699 srcs += port_count[port_type][SCARLETT2_PORT_IN];
3700 dsts += port_count[port_type][SCARLETT2_PORT_OUT];
3703 private->num_mux_srcs = srcs;
3704 private->num_mux_dsts = dsts;
3707 /* Look through the interface descriptors for the Focusrite Control
3708 * interface (bInterfaceClass = 255 Vendor Specific Class) and set
3709 * bInterfaceNumber, bEndpointAddress, wMaxPacketSize, and bInterval
3712 static int scarlett2_find_fc_interface(struct usb_device *dev,
3713 struct scarlett2_data *private)
3715 struct usb_host_config *config = dev->actconfig;
3718 for (i = 0; i < config->desc.bNumInterfaces; i++) {
3719 struct usb_interface *intf = config->interface[i];
3720 struct usb_interface_descriptor *desc =
3721 &intf->altsetting[0].desc;
3722 struct usb_endpoint_descriptor *epd;
3724 if (desc->bInterfaceClass != 255)
3727 epd = get_endpoint(intf->altsetting, 0);
3728 private->bInterfaceNumber = desc->bInterfaceNumber;
3729 private->bEndpointAddress = epd->bEndpointAddress &
3730 USB_ENDPOINT_NUMBER_MASK;
3731 private->wMaxPacketSize = le16_to_cpu(epd->wMaxPacketSize);
3732 private->bInterval = epd->bInterval;
3739 /* Initialise private data */
3740 static int scarlett2_init_private(struct usb_mixer_interface *mixer,
3741 const struct scarlett2_device_info *info)
3743 struct scarlett2_data *private =
3744 kzalloc(sizeof(struct scarlett2_data), GFP_KERNEL);
3749 mutex_init(&private->usb_mutex);
3750 mutex_init(&private->data_mutex);
3751 INIT_DELAYED_WORK(&private->work, scarlett2_config_save_work);
3753 mixer->private_data = private;
3754 mixer->private_free = scarlett2_private_free;
3755 mixer->private_suspend = scarlett2_private_suspend;
3757 private->info = info;
3758 scarlett2_count_mux_io(private);
3759 private->scarlett2_seq = 0;
3760 private->mixer = mixer;
3762 return scarlett2_find_fc_interface(mixer->chip->dev, private);
3765 /* Cargo cult proprietary initialisation sequence */
3766 static int scarlett2_usb_init(struct usb_mixer_interface *mixer)
3768 struct usb_device *dev = mixer->chip->dev;
3769 struct scarlett2_data *private = mixer->private_data;
3773 if (usb_pipe_type_check(dev, usb_sndctrlpipe(dev, 0)))
3777 err = scarlett2_usb_rx(dev, private->bInterfaceNumber,
3778 SCARLETT2_USB_CMD_INIT, buf, sizeof(buf));
3783 private->scarlett2_seq = 1;
3784 err = scarlett2_usb(mixer, SCARLETT2_USB_INIT_1, NULL, 0, NULL, 0);
3789 private->scarlett2_seq = 1;
3790 return scarlett2_usb(mixer, SCARLETT2_USB_INIT_2, NULL, 0, NULL, 84);
3793 /* Read configuration from the interface on start */
3794 static int scarlett2_read_configs(struct usb_mixer_interface *mixer)
3796 struct scarlett2_data *private = mixer->private_data;
3797 const struct scarlett2_device_info *info = private->info;
3798 const int (*port_count)[SCARLETT2_PORT_DIRNS] = info->port_count;
3800 port_count[SCARLETT2_PORT_TYPE_ANALOGUE][SCARLETT2_PORT_OUT];
3802 port_count[SCARLETT2_PORT_TYPE_MIX][SCARLETT2_PORT_IN];
3803 struct scarlett2_usb_volume_status volume_status;
3806 if (info->has_msd_mode) {
3807 err = scarlett2_usb_get_config(
3808 mixer, SCARLETT2_CONFIG_MSD_SWITCH,
3809 1, &private->msd_switch);
3813 /* no other controls are created if MSD mode is on */
3814 if (private->msd_switch)
3818 err = scarlett2_update_input_other(mixer);
3822 err = scarlett2_update_monitor_other(mixer);
3826 /* the rest of the configuration is for devices with a mixer */
3827 if (info->config_set == SCARLETT2_CONFIG_SET_NO_MIXER)
3830 err = scarlett2_usb_get_config(
3831 mixer, SCARLETT2_CONFIG_STANDALONE_SWITCH,
3832 1, &private->standalone_switch);
3836 err = scarlett2_update_sync(mixer);
3840 err = scarlett2_usb_get_volume_status(mixer, &volume_status);
3844 if (info->line_out_hw_vol)
3845 for (i = 0; i < SCARLETT2_DIM_MUTE_COUNT; i++)
3846 private->dim_mute[i] = !!volume_status.dim_mute[i];
3848 private->master_vol = clamp(
3849 volume_status.master_vol + SCARLETT2_VOLUME_BIAS,
3850 0, SCARLETT2_VOLUME_BIAS);
3852 for (i = 0; i < num_line_out; i++) {
3855 private->vol_sw_hw_switch[i] =
3856 info->line_out_hw_vol
3857 && volume_status.sw_hw_switch[i];
3859 volume = private->vol_sw_hw_switch[i]
3860 ? volume_status.master_vol
3861 : volume_status.sw_vol[i];
3862 volume = clamp(volume + SCARLETT2_VOLUME_BIAS,
3863 0, SCARLETT2_VOLUME_BIAS);
3864 private->vol[i] = volume;
3866 mute = private->vol_sw_hw_switch[i]
3867 ? private->dim_mute[SCARLETT2_BUTTON_MUTE]
3868 : volume_status.mute_switch[i];
3869 private->mute_switch[i] = mute;
3872 for (i = 0; i < num_mixer_out; i++) {
3873 err = scarlett2_usb_get_mix(mixer, i);
3878 return scarlett2_usb_get_mux(mixer);
3881 /* Notify on sync change */
3882 static void scarlett2_notify_sync(
3883 struct usb_mixer_interface *mixer)
3885 struct scarlett2_data *private = mixer->private_data;
3887 private->sync_updated = 1;
3889 snd_ctl_notify(mixer->chip->card, SNDRV_CTL_EVENT_MASK_VALUE,
3890 &private->sync_ctl->id);
3893 /* Notify on monitor change */
3894 static void scarlett2_notify_monitor(
3895 struct usb_mixer_interface *mixer)
3897 struct snd_card *card = mixer->chip->card;
3898 struct scarlett2_data *private = mixer->private_data;
3899 const struct scarlett2_device_info *info = private->info;
3900 const int (*port_count)[SCARLETT2_PORT_DIRNS] = info->port_count;
3902 port_count[SCARLETT2_PORT_TYPE_ANALOGUE][SCARLETT2_PORT_OUT];
3905 /* if line_out_hw_vol is 0, there are no controls to update */
3906 if (!info->line_out_hw_vol)
3909 private->vol_updated = 1;
3911 snd_ctl_notify(mixer->chip->card, SNDRV_CTL_EVENT_MASK_VALUE,
3912 &private->master_vol_ctl->id);
3914 for (i = 0; i < num_line_out; i++)
3915 if (private->vol_sw_hw_switch[line_out_remap(private, i)])
3916 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE,
3917 &private->vol_ctls[i]->id);
3920 /* Notify on dim/mute change */
3921 static void scarlett2_notify_dim_mute(
3922 struct usb_mixer_interface *mixer)
3924 struct snd_card *card = mixer->chip->card;
3925 struct scarlett2_data *private = mixer->private_data;
3926 const struct scarlett2_device_info *info = private->info;
3927 const int (*port_count)[SCARLETT2_PORT_DIRNS] = info->port_count;
3929 port_count[SCARLETT2_PORT_TYPE_ANALOGUE][SCARLETT2_PORT_OUT];
3932 private->vol_updated = 1;
3934 if (!info->line_out_hw_vol)
3937 for (i = 0; i < SCARLETT2_DIM_MUTE_COUNT; i++)
3938 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE,
3939 &private->dim_mute_ctls[i]->id);
3941 for (i = 0; i < num_line_out; i++)
3942 if (private->vol_sw_hw_switch[line_out_remap(private, i)])
3943 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE,
3944 &private->mute_ctls[i]->id);
3947 /* Notify on "input other" change (level/pad/air) */
3948 static void scarlett2_notify_input_other(
3949 struct usb_mixer_interface *mixer)
3951 struct snd_card *card = mixer->chip->card;
3952 struct scarlett2_data *private = mixer->private_data;
3953 const struct scarlett2_device_info *info = private->info;
3956 private->input_other_updated = 1;
3958 for (i = 0; i < info->level_input_count; i++)
3959 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE,
3960 &private->level_ctls[i]->id);
3961 for (i = 0; i < info->pad_input_count; i++)
3962 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE,
3963 &private->pad_ctls[i]->id);
3964 for (i = 0; i < info->air_input_count; i++)
3965 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE,
3966 &private->air_ctls[i]->id);
3967 for (i = 0; i < info->phantom_count; i++)
3968 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE,
3969 &private->phantom_ctls[i]->id);
3972 /* Notify on "monitor other" change (direct monitor, speaker
3973 * switching, talkback)
3975 static void scarlett2_notify_monitor_other(
3976 struct usb_mixer_interface *mixer)
3978 struct snd_card *card = mixer->chip->card;
3979 struct scarlett2_data *private = mixer->private_data;
3980 const struct scarlett2_device_info *info = private->info;
3982 private->monitor_other_updated = 1;
3984 if (info->direct_monitor) {
3985 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE,
3986 &private->direct_monitor_ctl->id);
3990 if (info->has_speaker_switching)
3991 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE,
3992 &private->speaker_switching_ctl->id);
3994 if (info->has_talkback)
3995 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE,
3996 &private->talkback_ctl->id);
3998 /* if speaker switching was recently enabled or disabled,
3999 * invalidate the dim/mute and mux enum controls
4001 if (private->speaker_switching_switched) {
4004 scarlett2_notify_dim_mute(mixer);
4006 private->speaker_switching_switched = 0;
4007 private->mux_updated = 1;
4009 for (i = 0; i < private->num_mux_dsts; i++)
4010 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE,
4011 &private->mux_ctls[i]->id);
4015 /* Interrupt callback */
4016 static void scarlett2_notify(struct urb *urb)
4018 struct usb_mixer_interface *mixer = urb->context;
4019 int len = urb->actual_length;
4020 int ustatus = urb->status;
4023 if (ustatus != 0 || len != 8)
4026 data = le32_to_cpu(*(__le32 *)urb->transfer_buffer);
4027 if (data & SCARLETT2_USB_NOTIFY_SYNC)
4028 scarlett2_notify_sync(mixer);
4029 if (data & SCARLETT2_USB_NOTIFY_MONITOR)
4030 scarlett2_notify_monitor(mixer);
4031 if (data & SCARLETT2_USB_NOTIFY_DIM_MUTE)
4032 scarlett2_notify_dim_mute(mixer);
4033 if (data & SCARLETT2_USB_NOTIFY_INPUT_OTHER)
4034 scarlett2_notify_input_other(mixer);
4035 if (data & SCARLETT2_USB_NOTIFY_MONITOR_OTHER)
4036 scarlett2_notify_monitor_other(mixer);
4039 if (ustatus != -ENOENT &&
4040 ustatus != -ECONNRESET &&
4041 ustatus != -ESHUTDOWN) {
4042 urb->dev = mixer->chip->dev;
4043 usb_submit_urb(urb, GFP_ATOMIC);
4047 static int scarlett2_init_notify(struct usb_mixer_interface *mixer)
4049 struct usb_device *dev = mixer->chip->dev;
4050 struct scarlett2_data *private = mixer->private_data;
4051 unsigned int pipe = usb_rcvintpipe(dev, private->bEndpointAddress);
4052 void *transfer_buffer;
4055 usb_audio_err(mixer->chip,
4056 "%s: mixer urb already in use!\n", __func__);
4060 if (usb_pipe_type_check(dev, pipe))
4063 mixer->urb = usb_alloc_urb(0, GFP_KERNEL);
4067 transfer_buffer = kmalloc(private->wMaxPacketSize, GFP_KERNEL);
4068 if (!transfer_buffer)
4071 usb_fill_int_urb(mixer->urb, dev, pipe,
4072 transfer_buffer, private->wMaxPacketSize,
4073 scarlett2_notify, mixer, private->bInterval);
4075 return usb_submit_urb(mixer->urb, GFP_KERNEL);
4078 static int snd_scarlett_gen2_controls_create(struct usb_mixer_interface *mixer)
4080 const struct scarlett2_device_info **info = scarlett2_devices;
4083 /* Find device in scarlett2_devices */
4084 while (*info && (*info)->usb_id != mixer->chip->usb_id)
4089 /* Initialise private data */
4090 err = scarlett2_init_private(mixer, *info);
4094 /* Send proprietary USB initialisation sequence */
4095 err = scarlett2_usb_init(mixer);
4099 /* Read volume levels and controls from the interface */
4100 err = scarlett2_read_configs(mixer);
4104 /* Create the MSD control */
4105 err = scarlett2_add_msd_ctl(mixer);
4109 /* If MSD mode is enabled, don't create any other controls */
4110 if (((struct scarlett2_data *)mixer->private_data)->msd_switch)
4113 /* Create the analogue output controls */
4114 err = scarlett2_add_line_out_ctls(mixer);
4118 /* Create the analogue input controls */
4119 err = scarlett2_add_line_in_ctls(mixer);
4123 /* Create the input, output, and mixer mux input selections */
4124 err = scarlett2_add_mux_enums(mixer);
4128 /* Create the matrix mixer controls */
4129 err = scarlett2_add_mixer_ctls(mixer);
4133 /* Create the level meter controls */
4134 err = scarlett2_add_meter_ctl(mixer);
4138 /* Create the sync control */
4139 err = scarlett2_add_sync_ctl(mixer);
4143 /* Create the direct monitor control */
4144 err = scarlett2_add_direct_monitor_ctl(mixer);
4148 /* Create the speaker switching control */
4149 err = scarlett2_add_speaker_switch_ctl(mixer);
4153 /* Create the talkback controls */
4154 err = scarlett2_add_talkback_ctls(mixer);
4158 /* Create the standalone control */
4159 err = scarlett2_add_standalone_ctl(mixer);
4163 /* Set up the interrupt polling */
4164 err = scarlett2_init_notify(mixer);
4171 int snd_scarlett_gen2_init(struct usb_mixer_interface *mixer)
4173 struct snd_usb_audio *chip = mixer->chip;
4176 /* only use UAC_VERSION_2 */
4177 if (!mixer->protocol)
4180 if (!(chip->setup & SCARLETT2_ENABLE)) {
4181 usb_audio_info(chip,
4182 "Focusrite Scarlett Gen 2/3 Mixer Driver disabled; "
4183 "use options snd_usb_audio vid=0x%04x pid=0x%04x "
4184 "device_setup=1 to enable and report any issues "
4186 USB_ID_VENDOR(chip->usb_id),
4187 USB_ID_PRODUCT(chip->usb_id));
4191 usb_audio_info(chip,
4192 "Focusrite Scarlett Gen 2/3 Mixer Driver enabled pid=0x%04x",
4193 USB_ID_PRODUCT(chip->usb_id));
4195 err = snd_scarlett_gen2_controls_create(mixer);
4197 usb_audio_err(mixer->chip,
4198 "Error initialising Scarlett Mixer Driver: %d",