1 // SPDX-License-Identifier: GPL-2.0
3 * Focusrite Scarlett Gen 2/3 Driver for ALSA
6 * - 6i6/18i8/18i20 Gen 2
7 * - Solo/2i2/4i4/8i6/18i8/18i20 Gen 3
9 * Copyright (c) 2018-2022 by Geoffrey D. Bennett <g at b4.vu>
10 * Copyright (c) 2020-2021 by Vladimir Sadovnikov <sadko4u@gmail.com>
12 * Based on the Scarlett (Gen 1) Driver for ALSA:
14 * Copyright (c) 2013 by Tobias Hoffmann
15 * Copyright (c) 2013 by Robin Gareus <robin at gareus.org>
16 * Copyright (c) 2002 by Takashi Iwai <tiwai at suse.de>
17 * Copyright (c) 2014 by Chris J Arges <chris.j.arges at canonical.com>
19 * Many codes borrowed from audio.c by
20 * Alan Cox (alan at lxorguk.ukuu.org.uk)
21 * Thomas Sailer (sailer at ife.ee.ethz.ch)
24 * David Henningsson <david.henningsson at canonical.com>
27 /* The protocol was reverse engineered by looking at the communication
28 * between Focusrite Control 2.3.4 and the Focusrite(R) Scarlett 18i20
29 * (firmware 1083) using usbmon in July-August 2018.
31 * Scarlett 18i8 support added in April 2019.
33 * Scarlett 6i6 support added in June 2019 (thanks to Martin Wittmann
34 * for providing usbmon output and testing).
36 * Scarlett 4i4/8i6 Gen 3 support added in May 2020 (thanks to Laurent
37 * Debricon for donating a 4i4 and to Fredrik Unger for providing 8i6
38 * usbmon output and testing).
40 * Scarlett 18i8/18i20 Gen 3 support added in June 2020 (thanks to
41 * Darren Jaeckel, Alex Sedlack, and Clovis Lunel for providing usbmon
42 * output, protocol traces and testing).
44 * Support for loading mixer volume and mux configuration from the
45 * interface during driver initialisation added in May 2021 (thanks to
46 * Vladimir Sadovnikov for figuring out how).
48 * Support for Solo/2i2 Gen 3 added in May 2021 (thanks to Alexander
49 * Vorona for 2i2 protocol traces).
51 * Support for phantom power, direct monitoring, speaker switching,
52 * and talkback added in May-June 2021.
54 * This ALSA mixer gives access to (model-dependent):
55 * - input, output, mixer-matrix muxes
56 * - mixer-matrix gain stages
57 * - gain/volume/mute controls
59 * - line/inst level, pad, and air controls
60 * - phantom power, direct monitor, speaker switching, and talkback
62 * - disable/enable MSD mode
63 * - disable/enable standalone mode
66 * /--------------\ 18chn 20chn /--------------\
67 * | Hardware in +--+------\ /-------------+--+ ALSA PCM out |
68 * \--------------/ | | | | \--------------/
72 * | +---------------+ | |
73 * | \ Matrix Mux / | |
80 * | +------------+ | |
84 * | | 18x10 Gain | | |
86 * | +-----+------+ | |
88 * |18chn |10chn | |20chn
93 * ===========================
94 * +---------------+ +--—------------+
95 * \ Output Mux / \ Capture Mux /
96 * +---+---+---+ +-----+-----+
100 * /--------------\ | | | /--------------\
101 * | S/PDIF, ADAT |<--/ |10chn \-->| ALSA PCM in |
102 * | Hardware out | | \--------------/
105 * +-------------+ Software gain per channel.
106 * | Master Gain |<-- 18i20 only: Switch per channel
107 * +------+------+ to select HW or SW gain control.
111 * | Analogue |<------/
116 * Gen 3 devices have a Mass Storage Device (MSD) mode where a small
117 * disk with registration and driver download information is presented
118 * to the host. To access the full functionality of the device without
119 * proprietary software, MSD mode can be disabled by:
120 * - holding down the 48V button for five seconds while powering on
122 * - using this driver and alsamixer to change the "MSD Mode" setting
123 * to Off and power-cycling the device
126 #include <linux/slab.h>
127 #include <linux/usb.h>
128 #include <linux/moduleparam.h>
130 #include <sound/control.h>
131 #include <sound/tlv.h>
133 #include "usbaudio.h"
137 #include "mixer_scarlett_gen2.h"
139 /* device_setup value to enable */
140 #define SCARLETT2_ENABLE 0x01
142 /* device_setup value to allow turning MSD mode back on */
143 #define SCARLETT2_MSD_ENABLE 0x02
145 /* some gui mixers can't handle negative ctl values */
146 #define SCARLETT2_VOLUME_BIAS 127
148 /* mixer range from -80dB to +6dB in 0.5dB steps */
149 #define SCARLETT2_MIXER_MIN_DB -80
150 #define SCARLETT2_MIXER_BIAS (-SCARLETT2_MIXER_MIN_DB * 2)
151 #define SCARLETT2_MIXER_MAX_DB 6
152 #define SCARLETT2_MIXER_MAX_VALUE \
153 ((SCARLETT2_MIXER_MAX_DB - SCARLETT2_MIXER_MIN_DB) * 2)
154 #define SCARLETT2_MIXER_VALUE_COUNT (SCARLETT2_MIXER_MAX_VALUE + 1)
156 /* map from (dB + 80) * 2 to mixer value
157 * for dB in 0 .. 172: int(8192 * pow(10, ((dB - 160) / 2 / 20)))
159 static const u16 scarlett2_mixer_values[SCARLETT2_MIXER_VALUE_COUNT] = {
160 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2,
161 2, 2, 3, 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 6, 6, 6, 7, 7, 8, 8,
162 9, 9, 10, 10, 11, 12, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21,
163 23, 24, 25, 27, 29, 30, 32, 34, 36, 38, 41, 43, 46, 48, 51,
164 54, 57, 61, 65, 68, 73, 77, 81, 86, 91, 97, 103, 109, 115,
165 122, 129, 137, 145, 154, 163, 173, 183, 194, 205, 217, 230,
166 244, 259, 274, 290, 307, 326, 345, 365, 387, 410, 434, 460,
167 487, 516, 547, 579, 614, 650, 689, 730, 773, 819, 867, 919,
168 973, 1031, 1092, 1157, 1225, 1298, 1375, 1456, 1543, 1634,
169 1731, 1833, 1942, 2057, 2179, 2308, 2445, 2590, 2744, 2906,
170 3078, 3261, 3454, 3659, 3876, 4105, 4349, 4606, 4879, 5168,
171 5475, 5799, 6143, 6507, 6892, 7301, 7733, 8192, 8677, 9191,
172 9736, 10313, 10924, 11571, 12257, 12983, 13752, 14567, 15430,
176 /* Maximum number of analogue outputs */
177 #define SCARLETT2_ANALOGUE_MAX 10
179 /* Maximum number of level and pad switches */
180 #define SCARLETT2_LEVEL_SWITCH_MAX 2
181 #define SCARLETT2_PAD_SWITCH_MAX 8
182 #define SCARLETT2_AIR_SWITCH_MAX 8
183 #define SCARLETT2_PHANTOM_SWITCH_MAX 2
185 /* Maximum number of inputs to the mixer */
186 #define SCARLETT2_INPUT_MIX_MAX 25
188 /* Maximum number of outputs from the mixer */
189 #define SCARLETT2_OUTPUT_MIX_MAX 12
191 /* Maximum size of the data in the USB mux assignment message:
192 * 20 inputs, 20 outputs, 25 matrix inputs, 12 spare
194 #define SCARLETT2_MUX_MAX 77
196 /* Maximum number of meters (sum of output port counts) */
197 #define SCARLETT2_MAX_METERS 65
199 /* There are three different sets of configuration parameters across
203 SCARLETT2_CONFIG_SET_NO_MIXER = 0,
204 SCARLETT2_CONFIG_SET_GEN_2 = 1,
205 SCARLETT2_CONFIG_SET_GEN_3 = 2,
206 SCARLETT2_CONFIG_SET_COUNT = 3
209 /* Hardware port types:
210 * - None (no input to mux)
218 SCARLETT2_PORT_TYPE_NONE = 0,
219 SCARLETT2_PORT_TYPE_ANALOGUE = 1,
220 SCARLETT2_PORT_TYPE_SPDIF = 2,
221 SCARLETT2_PORT_TYPE_ADAT = 3,
222 SCARLETT2_PORT_TYPE_MIX = 4,
223 SCARLETT2_PORT_TYPE_PCM = 5,
224 SCARLETT2_PORT_TYPE_COUNT = 6,
227 /* I/O count of each port type kept in struct scarlett2_ports */
229 SCARLETT2_PORT_IN = 0,
230 SCARLETT2_PORT_OUT = 1,
231 SCARLETT2_PORT_DIRNS = 2,
234 /* Dim/Mute buttons on the 18i20 */
236 SCARLETT2_BUTTON_MUTE = 0,
237 SCARLETT2_BUTTON_DIM = 1,
238 SCARLETT2_DIM_MUTE_COUNT = 2,
241 static const char *const scarlett2_dim_mute_names[SCARLETT2_DIM_MUTE_COUNT] = {
242 "Mute Playback Switch", "Dim Playback Switch"
245 /* Description of each hardware port type:
246 * - id: hardware ID of this port type
247 * - src_descr: printf format string for mux input selections
248 * - src_num_offset: added to channel number for the fprintf
249 * - dst_descr: printf format string for mixer controls
251 struct scarlett2_port {
253 const char * const src_descr;
255 const char * const dst_descr;
258 static const struct scarlett2_port scarlett2_ports[SCARLETT2_PORT_TYPE_COUNT] = {
259 [SCARLETT2_PORT_TYPE_NONE] = {
263 [SCARLETT2_PORT_TYPE_ANALOGUE] = {
265 .src_descr = "Analogue %d",
267 .dst_descr = "Analogue Output %02d Playback"
269 [SCARLETT2_PORT_TYPE_SPDIF] = {
271 .src_descr = "S/PDIF %d",
273 .dst_descr = "S/PDIF Output %d Playback"
275 [SCARLETT2_PORT_TYPE_ADAT] = {
277 .src_descr = "ADAT %d",
279 .dst_descr = "ADAT Output %d Playback"
281 [SCARLETT2_PORT_TYPE_MIX] = {
283 .src_descr = "Mix %c",
284 .src_num_offset = 'A',
285 .dst_descr = "Mixer Input %02d Capture"
287 [SCARLETT2_PORT_TYPE_PCM] = {
289 .src_descr = "PCM %d",
291 .dst_descr = "PCM %02d Capture"
295 /* Number of mux tables: one for each band of sample rates
296 * (44.1/48kHz, 88.2/96kHz, and 176.4/176kHz)
298 #define SCARLETT2_MUX_TABLES 3
300 /* Maximum number of entries in a mux table */
301 #define SCARLETT2_MAX_MUX_ENTRIES 10
303 /* One entry within mux_assignment defines the port type and range of
304 * ports to add to the set_mux message. The end of the list is marked
307 struct scarlett2_mux_entry {
313 struct scarlett2_device_info {
314 u32 usb_id; /* USB device identifier */
316 /* Gen 3 devices have an internal MSD mode switch that needs
317 * to be disabled in order to access the full functionality of
322 /* which set of configuration parameters the device uses */
325 /* line out hw volume is sw controlled */
328 /* support for main/alt speaker switching */
329 u8 has_speaker_switching;
331 /* support for talkback microphone */
334 /* the number of analogue inputs with a software switchable
335 * level control that can be set to line or instrument
337 u8 level_input_count;
339 /* the first input with a level control (0-based) */
340 u8 level_input_first;
342 /* the number of analogue inputs with a software switchable
347 /* the number of analogue inputs with a software switchable
352 /* the number of phantom (48V) software switchable controls */
355 /* the number of inputs each phantom switch controls */
356 u8 inputs_per_phantom;
358 /* the number of direct monitor options
359 * (0 = none, 1 = mono only, 2 = mono/stereo)
363 /* remap analogue outputs; 18i8 Gen 3 has "line 3/4" connected
364 * internally to the analogue 7/8 outputs
366 u8 line_out_remap_enable;
367 u8 line_out_remap[SCARLETT2_ANALOGUE_MAX];
369 /* additional description for the line out volume controls */
370 const char * const line_out_descrs[SCARLETT2_ANALOGUE_MAX];
372 /* number of sources/destinations of each port type */
373 const int port_count[SCARLETT2_PORT_TYPE_COUNT][SCARLETT2_PORT_DIRNS];
375 /* layout/order of the entries in the set_mux message */
376 struct scarlett2_mux_entry mux_assignment[SCARLETT2_MUX_TABLES]
377 [SCARLETT2_MAX_MUX_ENTRIES];
380 struct scarlett2_data {
381 struct usb_mixer_interface *mixer;
382 struct mutex usb_mutex; /* prevent sending concurrent USB requests */
383 struct mutex data_mutex; /* lock access to this data */
384 struct delayed_work work;
385 const struct scarlett2_device_info *info;
386 __u8 bInterfaceNumber;
387 __u8 bEndpointAddress;
388 __u16 wMaxPacketSize;
395 u8 input_other_updated;
396 u8 monitor_other_updated;
398 u8 speaker_switching_switched;
401 u8 vol[SCARLETT2_ANALOGUE_MAX];
402 u8 vol_sw_hw_switch[SCARLETT2_ANALOGUE_MAX];
403 u8 mute_switch[SCARLETT2_ANALOGUE_MAX];
404 u8 level_switch[SCARLETT2_LEVEL_SWITCH_MAX];
405 u8 pad_switch[SCARLETT2_PAD_SWITCH_MAX];
406 u8 dim_mute[SCARLETT2_DIM_MUTE_COUNT];
407 u8 air_switch[SCARLETT2_AIR_SWITCH_MAX];
408 u8 phantom_switch[SCARLETT2_PHANTOM_SWITCH_MAX];
409 u8 phantom_persistence;
410 u8 direct_monitor_switch;
411 u8 speaker_switching_switch;
413 u8 talkback_map[SCARLETT2_OUTPUT_MIX_MAX];
415 u8 standalone_switch;
416 struct snd_kcontrol *sync_ctl;
417 struct snd_kcontrol *master_vol_ctl;
418 struct snd_kcontrol *vol_ctls[SCARLETT2_ANALOGUE_MAX];
419 struct snd_kcontrol *sw_hw_ctls[SCARLETT2_ANALOGUE_MAX];
420 struct snd_kcontrol *mute_ctls[SCARLETT2_ANALOGUE_MAX];
421 struct snd_kcontrol *dim_mute_ctls[SCARLETT2_DIM_MUTE_COUNT];
422 struct snd_kcontrol *level_ctls[SCARLETT2_LEVEL_SWITCH_MAX];
423 struct snd_kcontrol *pad_ctls[SCARLETT2_PAD_SWITCH_MAX];
424 struct snd_kcontrol *air_ctls[SCARLETT2_AIR_SWITCH_MAX];
425 struct snd_kcontrol *phantom_ctls[SCARLETT2_PHANTOM_SWITCH_MAX];
426 struct snd_kcontrol *mux_ctls[SCARLETT2_MUX_MAX];
427 struct snd_kcontrol *direct_monitor_ctl;
428 struct snd_kcontrol *speaker_switching_ctl;
429 struct snd_kcontrol *talkback_ctl;
430 u8 mux[SCARLETT2_MUX_MAX];
431 u8 mix[SCARLETT2_INPUT_MIX_MAX * SCARLETT2_OUTPUT_MIX_MAX];
434 /*** Model-specific data ***/
436 static const struct scarlett2_device_info s6i6_gen2_info = {
437 .usb_id = USB_ID(0x1235, 0x8203),
439 .config_set = SCARLETT2_CONFIG_SET_GEN_2,
440 .level_input_count = 2,
441 .pad_input_count = 2,
451 [SCARLETT2_PORT_TYPE_NONE] = { 1, 0 },
452 [SCARLETT2_PORT_TYPE_ANALOGUE] = { 4, 4 },
453 [SCARLETT2_PORT_TYPE_SPDIF] = { 2, 2 },
454 [SCARLETT2_PORT_TYPE_MIX] = { 10, 18 },
455 [SCARLETT2_PORT_TYPE_PCM] = { 6, 6 },
458 .mux_assignment = { {
459 { SCARLETT2_PORT_TYPE_PCM, 0, 6 },
460 { SCARLETT2_PORT_TYPE_ANALOGUE, 0, 4 },
461 { SCARLETT2_PORT_TYPE_SPDIF, 0, 2 },
462 { SCARLETT2_PORT_TYPE_MIX, 0, 18 },
463 { SCARLETT2_PORT_TYPE_NONE, 0, 8 },
466 { SCARLETT2_PORT_TYPE_PCM, 0, 6 },
467 { SCARLETT2_PORT_TYPE_ANALOGUE, 0, 4 },
468 { SCARLETT2_PORT_TYPE_SPDIF, 0, 2 },
469 { SCARLETT2_PORT_TYPE_MIX, 0, 18 },
470 { SCARLETT2_PORT_TYPE_NONE, 0, 8 },
473 { SCARLETT2_PORT_TYPE_PCM, 0, 6 },
474 { SCARLETT2_PORT_TYPE_ANALOGUE, 0, 4 },
475 { SCARLETT2_PORT_TYPE_SPDIF, 0, 2 },
476 { SCARLETT2_PORT_TYPE_MIX, 0, 18 },
477 { SCARLETT2_PORT_TYPE_NONE, 0, 8 },
482 static const struct scarlett2_device_info s18i8_gen2_info = {
483 .usb_id = USB_ID(0x1235, 0x8204),
485 .config_set = SCARLETT2_CONFIG_SET_GEN_2,
486 .level_input_count = 2,
487 .pad_input_count = 4,
499 [SCARLETT2_PORT_TYPE_NONE] = { 1, 0 },
500 [SCARLETT2_PORT_TYPE_ANALOGUE] = { 8, 6 },
501 [SCARLETT2_PORT_TYPE_SPDIF] = { 2, 2 },
502 [SCARLETT2_PORT_TYPE_ADAT] = { 8, 0 },
503 [SCARLETT2_PORT_TYPE_MIX] = { 10, 18 },
504 [SCARLETT2_PORT_TYPE_PCM] = { 8, 18 },
507 .mux_assignment = { {
508 { SCARLETT2_PORT_TYPE_PCM, 0, 18 },
509 { SCARLETT2_PORT_TYPE_ANALOGUE, 0, 6 },
510 { SCARLETT2_PORT_TYPE_SPDIF, 0, 2 },
511 { SCARLETT2_PORT_TYPE_MIX, 0, 18 },
512 { SCARLETT2_PORT_TYPE_NONE, 0, 8 },
515 { SCARLETT2_PORT_TYPE_PCM, 0, 14 },
516 { SCARLETT2_PORT_TYPE_ANALOGUE, 0, 6 },
517 { SCARLETT2_PORT_TYPE_SPDIF, 0, 2 },
518 { SCARLETT2_PORT_TYPE_MIX, 0, 18 },
519 { SCARLETT2_PORT_TYPE_NONE, 0, 8 },
522 { SCARLETT2_PORT_TYPE_PCM, 0, 10 },
523 { SCARLETT2_PORT_TYPE_ANALOGUE, 0, 6 },
524 { SCARLETT2_PORT_TYPE_SPDIF, 0, 2 },
525 { SCARLETT2_PORT_TYPE_MIX, 0, 18 },
526 { SCARLETT2_PORT_TYPE_NONE, 0, 4 },
531 static const struct scarlett2_device_info s18i20_gen2_info = {
532 .usb_id = USB_ID(0x1235, 0x8201),
534 .config_set = SCARLETT2_CONFIG_SET_GEN_2,
535 .line_out_hw_vol = 1,
551 [SCARLETT2_PORT_TYPE_NONE] = { 1, 0 },
552 [SCARLETT2_PORT_TYPE_ANALOGUE] = { 8, 10 },
553 [SCARLETT2_PORT_TYPE_SPDIF] = { 2, 2 },
554 [SCARLETT2_PORT_TYPE_ADAT] = { 8, 8 },
555 [SCARLETT2_PORT_TYPE_MIX] = { 10, 18 },
556 [SCARLETT2_PORT_TYPE_PCM] = { 20, 18 },
559 .mux_assignment = { {
560 { SCARLETT2_PORT_TYPE_PCM, 0, 18 },
561 { SCARLETT2_PORT_TYPE_ANALOGUE, 0, 10 },
562 { SCARLETT2_PORT_TYPE_SPDIF, 0, 2 },
563 { SCARLETT2_PORT_TYPE_ADAT, 0, 8 },
564 { SCARLETT2_PORT_TYPE_MIX, 0, 18 },
565 { SCARLETT2_PORT_TYPE_NONE, 0, 8 },
568 { SCARLETT2_PORT_TYPE_PCM, 0, 14 },
569 { SCARLETT2_PORT_TYPE_ANALOGUE, 0, 10 },
570 { SCARLETT2_PORT_TYPE_SPDIF, 0, 2 },
571 { SCARLETT2_PORT_TYPE_ADAT, 0, 4 },
572 { SCARLETT2_PORT_TYPE_MIX, 0, 18 },
573 { SCARLETT2_PORT_TYPE_NONE, 0, 8 },
576 { SCARLETT2_PORT_TYPE_PCM, 0, 10 },
577 { SCARLETT2_PORT_TYPE_ANALOGUE, 0, 10 },
578 { SCARLETT2_PORT_TYPE_SPDIF, 0, 2 },
579 { SCARLETT2_PORT_TYPE_MIX, 0, 18 },
580 { SCARLETT2_PORT_TYPE_NONE, 0, 6 },
585 static const struct scarlett2_device_info solo_gen3_info = {
586 .usb_id = USB_ID(0x1235, 0x8211),
589 .config_set = SCARLETT2_CONFIG_SET_NO_MIXER,
590 .level_input_count = 1,
591 .level_input_first = 1,
592 .air_input_count = 1,
594 .inputs_per_phantom = 1,
598 static const struct scarlett2_device_info s2i2_gen3_info = {
599 .usb_id = USB_ID(0x1235, 0x8210),
602 .config_set = SCARLETT2_CONFIG_SET_NO_MIXER,
603 .level_input_count = 2,
604 .air_input_count = 2,
606 .inputs_per_phantom = 2,
610 static const struct scarlett2_device_info s4i4_gen3_info = {
611 .usb_id = USB_ID(0x1235, 0x8212),
614 .config_set = SCARLETT2_CONFIG_SET_GEN_3,
615 .level_input_count = 2,
616 .pad_input_count = 2,
617 .air_input_count = 2,
619 .inputs_per_phantom = 2,
629 [SCARLETT2_PORT_TYPE_NONE] = { 1, 0 },
630 [SCARLETT2_PORT_TYPE_ANALOGUE] = { 4, 4 },
631 [SCARLETT2_PORT_TYPE_MIX] = { 6, 8 },
632 [SCARLETT2_PORT_TYPE_PCM] = { 4, 6 },
635 .mux_assignment = { {
636 { SCARLETT2_PORT_TYPE_PCM, 0, 6 },
637 { SCARLETT2_PORT_TYPE_ANALOGUE, 0, 4 },
638 { SCARLETT2_PORT_TYPE_MIX, 0, 8 },
639 { SCARLETT2_PORT_TYPE_NONE, 0, 16 },
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 },
656 static const struct scarlett2_device_info s8i6_gen3_info = {
657 .usb_id = USB_ID(0x1235, 0x8213),
660 .config_set = SCARLETT2_CONFIG_SET_GEN_3,
661 .level_input_count = 2,
662 .pad_input_count = 2,
663 .air_input_count = 2,
665 .inputs_per_phantom = 2,
675 [SCARLETT2_PORT_TYPE_NONE] = { 1, 0 },
676 [SCARLETT2_PORT_TYPE_ANALOGUE] = { 6, 4 },
677 [SCARLETT2_PORT_TYPE_SPDIF] = { 2, 2 },
678 [SCARLETT2_PORT_TYPE_MIX] = { 8, 8 },
679 [SCARLETT2_PORT_TYPE_PCM] = { 6, 10 },
682 .mux_assignment = { {
683 { SCARLETT2_PORT_TYPE_PCM, 0, 8 },
684 { SCARLETT2_PORT_TYPE_ANALOGUE, 0, 4 },
685 { SCARLETT2_PORT_TYPE_SPDIF, 0, 2 },
686 { SCARLETT2_PORT_TYPE_PCM, 8, 2 },
687 { SCARLETT2_PORT_TYPE_MIX, 0, 8 },
688 { SCARLETT2_PORT_TYPE_NONE, 0, 18 },
691 { SCARLETT2_PORT_TYPE_PCM, 0, 8 },
692 { SCARLETT2_PORT_TYPE_ANALOGUE, 0, 4 },
693 { SCARLETT2_PORT_TYPE_SPDIF, 0, 2 },
694 { SCARLETT2_PORT_TYPE_PCM, 8, 2 },
695 { SCARLETT2_PORT_TYPE_MIX, 0, 8 },
696 { SCARLETT2_PORT_TYPE_NONE, 0, 18 },
699 { SCARLETT2_PORT_TYPE_PCM, 0, 8 },
700 { SCARLETT2_PORT_TYPE_ANALOGUE, 0, 4 },
701 { SCARLETT2_PORT_TYPE_SPDIF, 0, 2 },
702 { SCARLETT2_PORT_TYPE_PCM, 8, 2 },
703 { SCARLETT2_PORT_TYPE_MIX, 0, 8 },
704 { SCARLETT2_PORT_TYPE_NONE, 0, 18 },
709 static const struct scarlett2_device_info s18i8_gen3_info = {
710 .usb_id = USB_ID(0x1235, 0x8214),
713 .config_set = SCARLETT2_CONFIG_SET_GEN_3,
714 .line_out_hw_vol = 1,
715 .has_speaker_switching = 1,
716 .level_input_count = 2,
717 .pad_input_count = 4,
718 .air_input_count = 4,
720 .inputs_per_phantom = 2,
722 .line_out_remap_enable = 1,
723 .line_out_remap = { 0, 1, 6, 7, 2, 3, 4, 5 },
737 [SCARLETT2_PORT_TYPE_NONE] = { 1, 0 },
738 [SCARLETT2_PORT_TYPE_ANALOGUE] = { 8, 8 },
739 [SCARLETT2_PORT_TYPE_SPDIF] = { 2, 2 },
740 [SCARLETT2_PORT_TYPE_ADAT] = { 8, 0 },
741 [SCARLETT2_PORT_TYPE_MIX] = { 10, 20 },
742 [SCARLETT2_PORT_TYPE_PCM] = { 8, 20 },
745 .mux_assignment = { {
746 { SCARLETT2_PORT_TYPE_PCM, 0, 10 },
747 { SCARLETT2_PORT_TYPE_PCM, 12, 8 },
748 { SCARLETT2_PORT_TYPE_ANALOGUE, 0, 2 },
749 { SCARLETT2_PORT_TYPE_ANALOGUE, 6, 2 },
750 { SCARLETT2_PORT_TYPE_ANALOGUE, 2, 4 },
751 { SCARLETT2_PORT_TYPE_SPDIF, 0, 2 },
752 { SCARLETT2_PORT_TYPE_PCM, 10, 2 },
753 { SCARLETT2_PORT_TYPE_MIX, 0, 20 },
754 { SCARLETT2_PORT_TYPE_NONE, 0, 10 },
757 { SCARLETT2_PORT_TYPE_PCM, 0, 10 },
758 { SCARLETT2_PORT_TYPE_PCM, 12, 4 },
759 { SCARLETT2_PORT_TYPE_ANALOGUE, 0, 2 },
760 { SCARLETT2_PORT_TYPE_ANALOGUE, 6, 2 },
761 { SCARLETT2_PORT_TYPE_ANALOGUE, 2, 4 },
762 { SCARLETT2_PORT_TYPE_SPDIF, 0, 2 },
763 { SCARLETT2_PORT_TYPE_PCM, 10, 2 },
764 { SCARLETT2_PORT_TYPE_MIX, 0, 20 },
765 { SCARLETT2_PORT_TYPE_NONE, 0, 10 },
768 { SCARLETT2_PORT_TYPE_PCM, 0, 10 },
769 { SCARLETT2_PORT_TYPE_ANALOGUE, 0, 2 },
770 { SCARLETT2_PORT_TYPE_ANALOGUE, 6, 2 },
771 { SCARLETT2_PORT_TYPE_ANALOGUE, 2, 4 },
772 { SCARLETT2_PORT_TYPE_SPDIF, 0, 2 },
773 { SCARLETT2_PORT_TYPE_MIX, 0, 20 },
774 { SCARLETT2_PORT_TYPE_NONE, 0, 10 },
779 static const struct scarlett2_device_info s18i20_gen3_info = {
780 .usb_id = USB_ID(0x1235, 0x8215),
783 .config_set = SCARLETT2_CONFIG_SET_GEN_3,
784 .line_out_hw_vol = 1,
785 .has_speaker_switching = 1,
787 .level_input_count = 2,
788 .pad_input_count = 8,
789 .air_input_count = 8,
791 .inputs_per_phantom = 4,
807 [SCARLETT2_PORT_TYPE_NONE] = { 1, 0 },
808 [SCARLETT2_PORT_TYPE_ANALOGUE] = { 9, 10 },
809 [SCARLETT2_PORT_TYPE_SPDIF] = { 2, 2 },
810 [SCARLETT2_PORT_TYPE_ADAT] = { 8, 8 },
811 [SCARLETT2_PORT_TYPE_MIX] = { 12, 25 },
812 [SCARLETT2_PORT_TYPE_PCM] = { 20, 20 },
815 .mux_assignment = { {
816 { SCARLETT2_PORT_TYPE_PCM, 0, 8 },
817 { SCARLETT2_PORT_TYPE_PCM, 10, 10 },
818 { SCARLETT2_PORT_TYPE_ANALOGUE, 0, 10 },
819 { SCARLETT2_PORT_TYPE_SPDIF, 0, 2 },
820 { SCARLETT2_PORT_TYPE_ADAT, 0, 8 },
821 { SCARLETT2_PORT_TYPE_PCM, 8, 2 },
822 { SCARLETT2_PORT_TYPE_MIX, 0, 25 },
823 { SCARLETT2_PORT_TYPE_NONE, 0, 12 },
826 { SCARLETT2_PORT_TYPE_PCM, 0, 8 },
827 { SCARLETT2_PORT_TYPE_PCM, 10, 8 },
828 { SCARLETT2_PORT_TYPE_ANALOGUE, 0, 10 },
829 { SCARLETT2_PORT_TYPE_SPDIF, 0, 2 },
830 { SCARLETT2_PORT_TYPE_ADAT, 0, 8 },
831 { SCARLETT2_PORT_TYPE_PCM, 8, 2 },
832 { SCARLETT2_PORT_TYPE_MIX, 0, 25 },
833 { SCARLETT2_PORT_TYPE_NONE, 0, 10 },
836 { SCARLETT2_PORT_TYPE_PCM, 0, 10 },
837 { SCARLETT2_PORT_TYPE_ANALOGUE, 0, 10 },
838 { SCARLETT2_PORT_TYPE_SPDIF, 0, 2 },
839 { SCARLETT2_PORT_TYPE_NONE, 0, 24 },
844 static const struct scarlett2_device_info *scarlett2_devices[] = {
845 /* Supported Gen 2 devices */
850 /* Supported Gen 3 devices */
862 /* get the starting port index number for a given port type/direction */
863 static int scarlett2_get_port_start_num(
864 const int port_count[][SCARLETT2_PORT_DIRNS],
865 int direction, int port_type)
869 for (i = 0; i < port_type; i++)
870 num += port_count[i][direction];
875 /*** USB Interactions ***/
877 /* Notifications from the interface */
878 #define SCARLETT2_USB_NOTIFY_SYNC 0x00000008
879 #define SCARLETT2_USB_NOTIFY_DIM_MUTE 0x00200000
880 #define SCARLETT2_USB_NOTIFY_MONITOR 0x00400000
881 #define SCARLETT2_USB_NOTIFY_INPUT_OTHER 0x00800000
882 #define SCARLETT2_USB_NOTIFY_MONITOR_OTHER 0x01000000
884 /* Commands for sending/receiving requests/responses */
885 #define SCARLETT2_USB_CMD_INIT 0
886 #define SCARLETT2_USB_CMD_REQ 2
887 #define SCARLETT2_USB_CMD_RESP 3
889 #define SCARLETT2_USB_INIT_1 0x00000000
890 #define SCARLETT2_USB_INIT_2 0x00000002
891 #define SCARLETT2_USB_GET_METER 0x00001001
892 #define SCARLETT2_USB_GET_MIX 0x00002001
893 #define SCARLETT2_USB_SET_MIX 0x00002002
894 #define SCARLETT2_USB_GET_MUX 0x00003001
895 #define SCARLETT2_USB_SET_MUX 0x00003002
896 #define SCARLETT2_USB_GET_SYNC 0x00006004
897 #define SCARLETT2_USB_GET_DATA 0x00800000
898 #define SCARLETT2_USB_SET_DATA 0x00800001
899 #define SCARLETT2_USB_DATA_CMD 0x00800002
901 #define SCARLETT2_USB_CONFIG_SAVE 6
903 #define SCARLETT2_USB_VOLUME_STATUS_OFFSET 0x31
904 #define SCARLETT2_USB_METER_LEVELS_GET_MAGIC 1
906 /* volume status is read together (matches scarlett2_config_items[1]) */
907 struct scarlett2_usb_volume_status {
908 /* dim/mute buttons */
909 u8 dim_mute[SCARLETT2_DIM_MUTE_COUNT];
913 /* software volume setting */
914 s16 sw_vol[SCARLETT2_ANALOGUE_MAX];
916 /* actual volume of output inc. dim (-18dB) */
917 s16 hw_vol[SCARLETT2_ANALOGUE_MAX];
919 /* internal mute buttons */
920 u8 mute_switch[SCARLETT2_ANALOGUE_MAX];
922 /* sw (0) or hw (1) controlled */
923 u8 sw_hw_switch[SCARLETT2_ANALOGUE_MAX];
927 /* front panel volume knob */
931 /* Configuration parameters that can be read and written */
933 SCARLETT2_CONFIG_DIM_MUTE = 0,
934 SCARLETT2_CONFIG_LINE_OUT_VOLUME = 1,
935 SCARLETT2_CONFIG_MUTE_SWITCH = 2,
936 SCARLETT2_CONFIG_SW_HW_SWITCH = 3,
937 SCARLETT2_CONFIG_LEVEL_SWITCH = 4,
938 SCARLETT2_CONFIG_PAD_SWITCH = 5,
939 SCARLETT2_CONFIG_MSD_SWITCH = 6,
940 SCARLETT2_CONFIG_AIR_SWITCH = 7,
941 SCARLETT2_CONFIG_STANDALONE_SWITCH = 8,
942 SCARLETT2_CONFIG_PHANTOM_SWITCH = 9,
943 SCARLETT2_CONFIG_PHANTOM_PERSISTENCE = 10,
944 SCARLETT2_CONFIG_DIRECT_MONITOR = 11,
945 SCARLETT2_CONFIG_MONITOR_OTHER_SWITCH = 12,
946 SCARLETT2_CONFIG_MONITOR_OTHER_ENABLE = 13,
947 SCARLETT2_CONFIG_TALKBACK_MAP = 14,
948 SCARLETT2_CONFIG_COUNT = 15
951 /* Location, size, and activation command number for the configuration
952 * parameters. Size is in bits and may be 1, 8, or 16.
954 struct scarlett2_config {
960 static const struct scarlett2_config
961 scarlett2_config_items[SCARLETT2_CONFIG_SET_COUNT]
962 [SCARLETT2_CONFIG_COUNT] =
964 /* Devices without a mixer (Gen 3 Solo and 2i2) */
966 [SCARLETT2_CONFIG_MSD_SWITCH] = {
967 .offset = 0x04, .size = 8, .activate = 6 },
969 [SCARLETT2_CONFIG_PHANTOM_PERSISTENCE] = {
970 .offset = 0x05, .size = 8, .activate = 6 },
972 [SCARLETT2_CONFIG_PHANTOM_SWITCH] = {
973 .offset = 0x06, .size = 8, .activate = 3 },
975 [SCARLETT2_CONFIG_DIRECT_MONITOR] = {
976 .offset = 0x07, .size = 8, .activate = 4 },
978 [SCARLETT2_CONFIG_LEVEL_SWITCH] = {
979 .offset = 0x08, .size = 1, .activate = 7 },
981 [SCARLETT2_CONFIG_AIR_SWITCH] = {
982 .offset = 0x09, .size = 1, .activate = 8 },
984 /* Gen 2 devices: 6i6, 18i8, 18i20 */
986 [SCARLETT2_CONFIG_DIM_MUTE] = {
987 .offset = 0x31, .size = 8, .activate = 2 },
989 [SCARLETT2_CONFIG_LINE_OUT_VOLUME] = {
990 .offset = 0x34, .size = 16, .activate = 1 },
992 [SCARLETT2_CONFIG_MUTE_SWITCH] = {
993 .offset = 0x5c, .size = 8, .activate = 1 },
995 [SCARLETT2_CONFIG_SW_HW_SWITCH] = {
996 .offset = 0x66, .size = 8, .activate = 3 },
998 [SCARLETT2_CONFIG_LEVEL_SWITCH] = {
999 .offset = 0x7c, .size = 8, .activate = 7 },
1001 [SCARLETT2_CONFIG_PAD_SWITCH] = {
1002 .offset = 0x84, .size = 8, .activate = 8 },
1004 [SCARLETT2_CONFIG_STANDALONE_SWITCH] = {
1005 .offset = 0x8d, .size = 8, .activate = 6 },
1007 /* Gen 3 devices: 4i4, 8i6, 18i8, 18i20 */
1009 [SCARLETT2_CONFIG_DIM_MUTE] = {
1010 .offset = 0x31, .size = 8, .activate = 2 },
1012 [SCARLETT2_CONFIG_LINE_OUT_VOLUME] = {
1013 .offset = 0x34, .size = 16, .activate = 1 },
1015 [SCARLETT2_CONFIG_MUTE_SWITCH] = {
1016 .offset = 0x5c, .size = 8, .activate = 1 },
1018 [SCARLETT2_CONFIG_SW_HW_SWITCH] = {
1019 .offset = 0x66, .size = 8, .activate = 3 },
1021 [SCARLETT2_CONFIG_LEVEL_SWITCH] = {
1022 .offset = 0x7c, .size = 8, .activate = 7 },
1024 [SCARLETT2_CONFIG_PAD_SWITCH] = {
1025 .offset = 0x84, .size = 8, .activate = 8 },
1027 [SCARLETT2_CONFIG_AIR_SWITCH] = {
1028 .offset = 0x8c, .size = 8, .activate = 8 },
1030 [SCARLETT2_CONFIG_STANDALONE_SWITCH] = {
1031 .offset = 0x95, .size = 8, .activate = 6 },
1033 [SCARLETT2_CONFIG_PHANTOM_SWITCH] = {
1034 .offset = 0x9c, .size = 1, .activate = 8 },
1036 [SCARLETT2_CONFIG_MSD_SWITCH] = {
1037 .offset = 0x9d, .size = 8, .activate = 6 },
1039 [SCARLETT2_CONFIG_PHANTOM_PERSISTENCE] = {
1040 .offset = 0x9e, .size = 8, .activate = 6 },
1042 [SCARLETT2_CONFIG_MONITOR_OTHER_SWITCH] = {
1043 .offset = 0x9f, .size = 1, .activate = 10 },
1045 [SCARLETT2_CONFIG_MONITOR_OTHER_ENABLE] = {
1046 .offset = 0xa0, .size = 1, .activate = 10 },
1048 [SCARLETT2_CONFIG_TALKBACK_MAP] = {
1049 .offset = 0xb0, .size = 16, .activate = 10 },
1052 /* proprietary request/response format */
1053 struct scarlett2_usb_packet {
1062 static void scarlett2_fill_request_header(struct scarlett2_data *private,
1063 struct scarlett2_usb_packet *req,
1064 u32 cmd, u16 req_size)
1066 /* sequence must go up by 1 for each request */
1067 u16 seq = private->scarlett2_seq++;
1069 req->cmd = cpu_to_le32(cmd);
1070 req->size = cpu_to_le16(req_size);
1071 req->seq = cpu_to_le16(seq);
1076 static int scarlett2_usb_tx(struct usb_device *dev, int interface,
1077 void *buf, u16 size)
1079 return snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0),
1080 SCARLETT2_USB_CMD_REQ,
1081 USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_OUT,
1082 0, interface, buf, size);
1085 static int scarlett2_usb_rx(struct usb_device *dev, int interface,
1086 u32 usb_req, void *buf, u16 size)
1088 return snd_usb_ctl_msg(dev, usb_rcvctrlpipe(dev, 0),
1090 USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_IN,
1091 0, interface, buf, size);
1094 /* Send a proprietary format request to the Scarlett interface */
1095 static int scarlett2_usb(
1096 struct usb_mixer_interface *mixer, u32 cmd,
1097 void *req_data, u16 req_size, void *resp_data, u16 resp_size)
1099 struct scarlett2_data *private = mixer->private_data;
1100 struct usb_device *dev = mixer->chip->dev;
1101 struct scarlett2_usb_packet *req, *resp = NULL;
1102 size_t req_buf_size = struct_size(req, data, req_size);
1103 size_t resp_buf_size = struct_size(resp, data, resp_size);
1106 req = kmalloc(req_buf_size, GFP_KERNEL);
1112 resp = kmalloc(resp_buf_size, GFP_KERNEL);
1118 mutex_lock(&private->usb_mutex);
1120 /* build request message and send it */
1122 scarlett2_fill_request_header(private, req, cmd, req_size);
1125 memcpy(req->data, req_data, req_size);
1127 err = scarlett2_usb_tx(dev, private->bInterfaceNumber,
1130 if (err != req_buf_size) {
1133 "Scarlett Gen 2/3 USB request result cmd %x was %d\n",
1139 /* send a second message to get the response */
1141 err = scarlett2_usb_rx(dev, private->bInterfaceNumber,
1142 SCARLETT2_USB_CMD_RESP,
1143 resp, resp_buf_size);
1145 /* validate the response */
1147 if (err != resp_buf_size) {
1150 "Scarlett Gen 2/3 USB response result cmd %x was %d "
1152 cmd, err, resp_buf_size);
1157 /* cmd/seq/size should match except when initialising
1158 * seq sent = 1, response = 0
1160 if (resp->cmd != req->cmd ||
1161 (resp->seq != req->seq &&
1162 (le16_to_cpu(req->seq) != 1 || resp->seq != 0)) ||
1163 resp_size != le16_to_cpu(resp->size) ||
1168 "Scarlett Gen 2/3 USB invalid response; "
1169 "cmd tx/rx %d/%d seq %d/%d size %d/%d "
1170 "error %d pad %d\n",
1171 le32_to_cpu(req->cmd), le32_to_cpu(resp->cmd),
1172 le16_to_cpu(req->seq), le16_to_cpu(resp->seq),
1173 resp_size, le16_to_cpu(resp->size),
1174 le32_to_cpu(resp->error),
1175 le32_to_cpu(resp->pad));
1180 if (resp_data && resp_size > 0)
1181 memcpy(resp_data, resp->data, resp_size);
1184 mutex_unlock(&private->usb_mutex);
1191 /* Send a USB message to get data; result placed in *buf */
1192 static int scarlett2_usb_get(
1193 struct usb_mixer_interface *mixer,
1194 int offset, void *buf, int size)
1201 req.offset = cpu_to_le32(offset);
1202 req.size = cpu_to_le32(size);
1203 return scarlett2_usb(mixer, SCARLETT2_USB_GET_DATA,
1204 &req, sizeof(req), buf, size);
1207 /* Send a USB message to get configuration parameters; result placed in *buf */
1208 static int scarlett2_usb_get_config(
1209 struct usb_mixer_interface *mixer,
1210 int config_item_num, int count, void *buf)
1212 struct scarlett2_data *private = mixer->private_data;
1213 const struct scarlett2_device_info *info = private->info;
1214 const struct scarlett2_config *config_item =
1215 &scarlett2_config_items[info->config_set][config_item_num];
1220 /* For byte-sized parameters, retrieve directly into buf */
1221 if (config_item->size >= 8) {
1222 size = config_item->size / 8 * count;
1223 err = scarlett2_usb_get(mixer, config_item->offset, buf, size);
1229 for (i = 0; i < count; i++, buf_16++)
1230 *buf_16 = le16_to_cpu(*(__le16 *)buf_16);
1235 /* For bit-sized parameters, retrieve into value */
1236 err = scarlett2_usb_get(mixer, config_item->offset, &value, 1);
1240 /* then unpack from value into buf[] */
1242 for (i = 0; i < 8 && i < count; i++, value >>= 1)
1243 *buf_8++ = value & 1;
1248 /* Send SCARLETT2_USB_DATA_CMD SCARLETT2_USB_CONFIG_SAVE */
1249 static void scarlett2_config_save(struct usb_mixer_interface *mixer)
1251 __le32 req = cpu_to_le32(SCARLETT2_USB_CONFIG_SAVE);
1253 scarlett2_usb(mixer, SCARLETT2_USB_DATA_CMD,
1258 /* Delayed work to save config */
1259 static void scarlett2_config_save_work(struct work_struct *work)
1261 struct scarlett2_data *private =
1262 container_of(work, struct scarlett2_data, work.work);
1264 scarlett2_config_save(private->mixer);
1267 /* Send a USB message to set a SCARLETT2_CONFIG_* parameter */
1268 static int scarlett2_usb_set_config(
1269 struct usb_mixer_interface *mixer,
1270 int config_item_num, int index, int value)
1272 struct scarlett2_data *private = mixer->private_data;
1273 const struct scarlett2_device_info *info = private->info;
1274 const struct scarlett2_config *config_item =
1275 &scarlett2_config_items[info->config_set][config_item_num];
1285 /* Cancel any pending NVRAM save */
1286 cancel_delayed_work_sync(&private->work);
1288 /* Convert config_item->size in bits to size in bytes and
1291 if (config_item->size >= 8) {
1292 size = config_item->size / 8;
1293 offset = config_item->offset + index * size;
1295 /* If updating a bit, retrieve the old value, set/clear the
1296 * bit as needed, and update value
1302 offset = config_item->offset;
1304 scarlett2_usb_get(mixer, offset, &tmp, 1);
1306 tmp |= (1 << index);
1308 tmp &= ~(1 << index);
1313 /* Send the configuration parameter data */
1314 req.offset = cpu_to_le32(offset);
1315 req.bytes = cpu_to_le32(size);
1316 req.value = cpu_to_le32(value);
1317 err = scarlett2_usb(mixer, SCARLETT2_USB_SET_DATA,
1318 &req, sizeof(u32) * 2 + size,
1323 /* Activate the change */
1324 req2 = cpu_to_le32(config_item->activate);
1325 err = scarlett2_usb(mixer, SCARLETT2_USB_DATA_CMD,
1326 &req2, sizeof(req2), NULL, 0);
1330 /* Schedule the change to be written to NVRAM */
1331 if (config_item->activate != SCARLETT2_USB_CONFIG_SAVE)
1332 schedule_delayed_work(&private->work, msecs_to_jiffies(2000));
1337 /* Send a USB message to get sync status; result placed in *sync */
1338 static int scarlett2_usb_get_sync_status(
1339 struct usb_mixer_interface *mixer,
1345 err = scarlett2_usb(mixer, SCARLETT2_USB_GET_SYNC,
1346 NULL, 0, &data, sizeof(data));
1354 /* Send a USB message to get volume status; result placed in *buf */
1355 static int scarlett2_usb_get_volume_status(
1356 struct usb_mixer_interface *mixer,
1357 struct scarlett2_usb_volume_status *buf)
1359 return scarlett2_usb_get(mixer, SCARLETT2_USB_VOLUME_STATUS_OFFSET,
1363 /* Send a USB message to get the volumes for all inputs of one mix
1364 * and put the values into private->mix[]
1366 static int scarlett2_usb_get_mix(struct usb_mixer_interface *mixer,
1369 struct scarlett2_data *private = mixer->private_data;
1370 const struct scarlett2_device_info *info = private->info;
1373 info->port_count[SCARLETT2_PORT_TYPE_MIX][SCARLETT2_PORT_OUT];
1381 __le16 data[SCARLETT2_INPUT_MIX_MAX];
1383 req.mix_num = cpu_to_le16(mix_num);
1384 req.count = cpu_to_le16(num_mixer_in);
1386 err = scarlett2_usb(mixer, SCARLETT2_USB_GET_MIX,
1388 data, num_mixer_in * sizeof(u16));
1392 for (i = 0, j = mix_num * num_mixer_in; i < num_mixer_in; i++, j++) {
1393 u16 mixer_value = le16_to_cpu(data[i]);
1395 for (k = 0; k < SCARLETT2_MIXER_VALUE_COUNT; k++)
1396 if (scarlett2_mixer_values[k] >= mixer_value)
1398 if (k == SCARLETT2_MIXER_VALUE_COUNT)
1399 k = SCARLETT2_MIXER_MAX_VALUE;
1400 private->mix[j] = k;
1406 /* Send a USB message to set the volumes for all inputs of one mix
1407 * (values obtained from private->mix[])
1409 static int scarlett2_usb_set_mix(struct usb_mixer_interface *mixer,
1412 struct scarlett2_data *private = mixer->private_data;
1413 const struct scarlett2_device_info *info = private->info;
1417 __le16 data[SCARLETT2_INPUT_MIX_MAX];
1422 info->port_count[SCARLETT2_PORT_TYPE_MIX][SCARLETT2_PORT_OUT];
1424 req.mix_num = cpu_to_le16(mix_num);
1426 for (i = 0, j = mix_num * num_mixer_in; i < num_mixer_in; i++, j++)
1427 req.data[i] = cpu_to_le16(
1428 scarlett2_mixer_values[private->mix[j]]
1431 return scarlett2_usb(mixer, SCARLETT2_USB_SET_MIX,
1432 &req, (num_mixer_in + 1) * sizeof(u16),
1436 /* Convert a port number index (per info->port_count) to a hardware ID */
1437 static u32 scarlett2_mux_src_num_to_id(
1438 const int port_count[][SCARLETT2_PORT_DIRNS], int num)
1443 port_type < SCARLETT2_PORT_TYPE_COUNT;
1445 if (num < port_count[port_type][SCARLETT2_PORT_IN])
1446 return scarlett2_ports[port_type].id | num;
1447 num -= port_count[port_type][SCARLETT2_PORT_IN];
1454 /* Convert a hardware ID to a port number index */
1455 static u32 scarlett2_mux_id_to_num(
1456 const int port_count[][SCARLETT2_PORT_DIRNS], int direction, u32 id)
1462 port_type < SCARLETT2_PORT_TYPE_COUNT;
1464 int base = scarlett2_ports[port_type].id;
1465 int count = port_count[port_type][direction];
1467 if (id >= base && id < base + count)
1468 return port_num + id - base;
1476 /* Convert one mux entry from the interface and load into private->mux[] */
1477 static void scarlett2_usb_populate_mux(struct scarlett2_data *private,
1480 const struct scarlett2_device_info *info = private->info;
1481 const int (*port_count)[SCARLETT2_PORT_DIRNS] = info->port_count;
1483 int dst_idx, src_idx;
1485 dst_idx = scarlett2_mux_id_to_num(port_count, SCARLETT2_PORT_OUT,
1490 if (dst_idx >= private->num_mux_dsts) {
1491 usb_audio_err(private->mixer->chip,
1492 "BUG: scarlett2_mux_id_to_num(%06x, OUT): %d >= %d",
1493 mux_entry, dst_idx, private->num_mux_dsts);
1497 src_idx = scarlett2_mux_id_to_num(port_count, SCARLETT2_PORT_IN,
1502 if (src_idx >= private->num_mux_srcs) {
1503 usb_audio_err(private->mixer->chip,
1504 "BUG: scarlett2_mux_id_to_num(%06x, IN): %d >= %d",
1505 mux_entry, src_idx, private->num_mux_srcs);
1509 private->mux[dst_idx] = src_idx;
1512 /* Send USB message to get mux inputs and then populate private->mux[] */
1513 static int scarlett2_usb_get_mux(struct usb_mixer_interface *mixer)
1515 struct scarlett2_data *private = mixer->private_data;
1516 int count = private->num_mux_dsts;
1524 __le32 data[SCARLETT2_MUX_MAX];
1526 private->mux_updated = 0;
1529 req.count = cpu_to_le16(count);
1531 err = scarlett2_usb(mixer, SCARLETT2_USB_GET_MUX,
1533 data, count * sizeof(u32));
1537 for (i = 0; i < count; i++)
1538 scarlett2_usb_populate_mux(private, le32_to_cpu(data[i]));
1543 /* Send USB messages to set mux inputs */
1544 static int scarlett2_usb_set_mux(struct usb_mixer_interface *mixer)
1546 struct scarlett2_data *private = mixer->private_data;
1547 const struct scarlett2_device_info *info = private->info;
1548 const int (*port_count)[SCARLETT2_PORT_DIRNS] = info->port_count;
1554 __le32 data[SCARLETT2_MUX_MAX];
1559 /* set mux settings for each rate */
1560 for (table = 0; table < SCARLETT2_MUX_TABLES; table++) {
1561 const struct scarlett2_mux_entry *entry;
1563 /* i counts over the output array */
1566 req.num = cpu_to_le16(table);
1568 /* loop through each entry */
1569 for (entry = info->mux_assignment[table];
1573 int port_type = entry->port_type;
1574 int port_idx = entry->start;
1575 int mux_idx = scarlett2_get_port_start_num(port_count,
1576 SCARLETT2_PORT_OUT, port_type) + port_idx;
1577 int dst_id = scarlett2_ports[port_type].id + port_idx;
1581 for (j = 0; j < entry->count; j++)
1586 /* Non-empty mux slots use the lower 12 bits
1587 * for the destination and next 12 bits for
1590 for (j = 0; j < entry->count; j++) {
1591 int src_id = scarlett2_mux_src_num_to_id(
1592 port_count, private->mux[mux_idx++]);
1593 req.data[i++] = cpu_to_le32(dst_id |
1599 err = scarlett2_usb(mixer, SCARLETT2_USB_SET_MUX,
1600 &req, (i + 1) * sizeof(u32),
1609 /* Send USB message to get meter levels */
1610 static int scarlett2_usb_get_meter_levels(struct usb_mixer_interface *mixer,
1611 u16 num_meters, u16 *levels)
1618 u32 resp[SCARLETT2_MAX_METERS];
1622 req.num_meters = cpu_to_le16(num_meters);
1623 req.magic = cpu_to_le32(SCARLETT2_USB_METER_LEVELS_GET_MAGIC);
1624 err = scarlett2_usb(mixer, SCARLETT2_USB_GET_METER,
1625 &req, sizeof(req), resp, num_meters * sizeof(u32));
1629 /* copy, convert to u16 */
1630 for (i = 0; i < num_meters; i++)
1631 levels[i] = resp[i];
1636 /*** Control Functions ***/
1638 /* helper function to create a new control */
1639 static int scarlett2_add_new_ctl(struct usb_mixer_interface *mixer,
1640 const struct snd_kcontrol_new *ncontrol,
1641 int index, int channels, const char *name,
1642 struct snd_kcontrol **kctl_return)
1644 struct snd_kcontrol *kctl;
1645 struct usb_mixer_elem_info *elem;
1648 elem = kzalloc(sizeof(*elem), GFP_KERNEL);
1652 /* We set USB_MIXER_BESPOKEN type, so that the core USB mixer code
1653 * ignores them for resume and other operations.
1654 * Also, the head.id field is set to 0, as we don't use this field.
1656 elem->head.mixer = mixer;
1657 elem->control = index;
1659 elem->channels = channels;
1660 elem->val_type = USB_MIXER_BESPOKEN;
1662 kctl = snd_ctl_new1(ncontrol, elem);
1667 kctl->private_free = snd_usb_mixer_elem_free;
1669 strscpy(kctl->id.name, name, sizeof(kctl->id.name));
1671 err = snd_usb_mixer_add_control(&elem->head, kctl);
1676 *kctl_return = kctl;
1681 /*** Sync Control ***/
1683 /* Update sync control after receiving notification that the status
1686 static int scarlett2_update_sync(struct usb_mixer_interface *mixer)
1688 struct scarlett2_data *private = mixer->private_data;
1690 private->sync_updated = 0;
1691 return scarlett2_usb_get_sync_status(mixer, &private->sync);
1694 static int scarlett2_sync_ctl_info(struct snd_kcontrol *kctl,
1695 struct snd_ctl_elem_info *uinfo)
1697 static const char *texts[2] = {
1698 "Unlocked", "Locked"
1700 return snd_ctl_enum_info(uinfo, 1, 2, texts);
1703 static int scarlett2_sync_ctl_get(struct snd_kcontrol *kctl,
1704 struct snd_ctl_elem_value *ucontrol)
1706 struct usb_mixer_elem_info *elem = kctl->private_data;
1707 struct usb_mixer_interface *mixer = elem->head.mixer;
1708 struct scarlett2_data *private = mixer->private_data;
1710 mutex_lock(&private->data_mutex);
1711 if (private->sync_updated)
1712 scarlett2_update_sync(mixer);
1713 ucontrol->value.enumerated.item[0] = private->sync;
1714 mutex_unlock(&private->data_mutex);
1719 static const struct snd_kcontrol_new scarlett2_sync_ctl = {
1720 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1721 .access = SNDRV_CTL_ELEM_ACCESS_READ,
1723 .info = scarlett2_sync_ctl_info,
1724 .get = scarlett2_sync_ctl_get
1727 static int scarlett2_add_sync_ctl(struct usb_mixer_interface *mixer)
1729 struct scarlett2_data *private = mixer->private_data;
1731 /* devices without a mixer also don't support reporting sync status */
1732 if (private->info->config_set == SCARLETT2_CONFIG_SET_NO_MIXER)
1735 return scarlett2_add_new_ctl(mixer, &scarlett2_sync_ctl,
1736 0, 1, "Sync Status", &private->sync_ctl);
1739 /*** Analogue Line Out Volume Controls ***/
1741 /* Update hardware volume controls after receiving notification that
1744 static int scarlett2_update_volumes(struct usb_mixer_interface *mixer)
1746 struct scarlett2_data *private = mixer->private_data;
1747 const struct scarlett2_device_info *info = private->info;
1748 const int (*port_count)[SCARLETT2_PORT_DIRNS] = info->port_count;
1749 struct scarlett2_usb_volume_status volume_status;
1751 port_count[SCARLETT2_PORT_TYPE_ANALOGUE][SCARLETT2_PORT_OUT];
1755 private->vol_updated = 0;
1757 err = scarlett2_usb_get_volume_status(mixer, &volume_status);
1761 private->master_vol = clamp(
1762 volume_status.master_vol + SCARLETT2_VOLUME_BIAS,
1763 0, SCARLETT2_VOLUME_BIAS);
1765 if (info->line_out_hw_vol)
1766 for (i = 0; i < SCARLETT2_DIM_MUTE_COUNT; i++)
1767 private->dim_mute[i] = !!volume_status.dim_mute[i];
1769 mute = private->dim_mute[SCARLETT2_BUTTON_MUTE];
1771 for (i = 0; i < num_line_out; i++)
1772 if (private->vol_sw_hw_switch[i]) {
1773 private->vol[i] = private->master_vol;
1774 private->mute_switch[i] = mute;
1780 static int scarlett2_volume_ctl_info(struct snd_kcontrol *kctl,
1781 struct snd_ctl_elem_info *uinfo)
1783 struct usb_mixer_elem_info *elem = kctl->private_data;
1785 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1786 uinfo->count = elem->channels;
1787 uinfo->value.integer.min = 0;
1788 uinfo->value.integer.max = SCARLETT2_VOLUME_BIAS;
1789 uinfo->value.integer.step = 1;
1793 static int scarlett2_master_volume_ctl_get(struct snd_kcontrol *kctl,
1794 struct snd_ctl_elem_value *ucontrol)
1796 struct usb_mixer_elem_info *elem = kctl->private_data;
1797 struct usb_mixer_interface *mixer = elem->head.mixer;
1798 struct scarlett2_data *private = mixer->private_data;
1800 mutex_lock(&private->data_mutex);
1801 if (private->vol_updated)
1802 scarlett2_update_volumes(mixer);
1803 mutex_unlock(&private->data_mutex);
1805 ucontrol->value.integer.value[0] = private->master_vol;
1809 static int line_out_remap(struct scarlett2_data *private, int index)
1811 const struct scarlett2_device_info *info = private->info;
1813 if (!info->line_out_remap_enable)
1815 return info->line_out_remap[index];
1818 static int scarlett2_volume_ctl_get(struct snd_kcontrol *kctl,
1819 struct snd_ctl_elem_value *ucontrol)
1821 struct usb_mixer_elem_info *elem = kctl->private_data;
1822 struct usb_mixer_interface *mixer = elem->head.mixer;
1823 struct scarlett2_data *private = mixer->private_data;
1824 int index = line_out_remap(private, elem->control);
1826 mutex_lock(&private->data_mutex);
1827 if (private->vol_updated)
1828 scarlett2_update_volumes(mixer);
1829 mutex_unlock(&private->data_mutex);
1831 ucontrol->value.integer.value[0] = private->vol[index];
1835 static int scarlett2_volume_ctl_put(struct snd_kcontrol *kctl,
1836 struct snd_ctl_elem_value *ucontrol)
1838 struct usb_mixer_elem_info *elem = kctl->private_data;
1839 struct usb_mixer_interface *mixer = elem->head.mixer;
1840 struct scarlett2_data *private = mixer->private_data;
1841 int index = line_out_remap(private, elem->control);
1842 int oval, val, err = 0;
1844 mutex_lock(&private->data_mutex);
1846 oval = private->vol[index];
1847 val = ucontrol->value.integer.value[0];
1852 private->vol[index] = val;
1853 err = scarlett2_usb_set_config(mixer, SCARLETT2_CONFIG_LINE_OUT_VOLUME,
1854 index, val - SCARLETT2_VOLUME_BIAS);
1859 mutex_unlock(&private->data_mutex);
1863 static const DECLARE_TLV_DB_MINMAX(
1864 db_scale_scarlett2_gain, -SCARLETT2_VOLUME_BIAS * 100, 0
1867 static const struct snd_kcontrol_new scarlett2_master_volume_ctl = {
1868 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1869 .access = SNDRV_CTL_ELEM_ACCESS_READ |
1870 SNDRV_CTL_ELEM_ACCESS_TLV_READ,
1872 .info = scarlett2_volume_ctl_info,
1873 .get = scarlett2_master_volume_ctl_get,
1874 .private_value = 0, /* max value */
1875 .tlv = { .p = db_scale_scarlett2_gain }
1878 static const struct snd_kcontrol_new scarlett2_line_out_volume_ctl = {
1879 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1880 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE |
1881 SNDRV_CTL_ELEM_ACCESS_TLV_READ,
1883 .info = scarlett2_volume_ctl_info,
1884 .get = scarlett2_volume_ctl_get,
1885 .put = scarlett2_volume_ctl_put,
1886 .private_value = 0, /* max value */
1887 .tlv = { .p = db_scale_scarlett2_gain }
1890 /*** Mute Switch Controls ***/
1892 static int scarlett2_mute_ctl_get(struct snd_kcontrol *kctl,
1893 struct snd_ctl_elem_value *ucontrol)
1895 struct usb_mixer_elem_info *elem = kctl->private_data;
1896 struct usb_mixer_interface *mixer = elem->head.mixer;
1897 struct scarlett2_data *private = mixer->private_data;
1898 int index = line_out_remap(private, elem->control);
1900 mutex_lock(&private->data_mutex);
1901 if (private->vol_updated)
1902 scarlett2_update_volumes(mixer);
1903 mutex_unlock(&private->data_mutex);
1905 ucontrol->value.integer.value[0] = private->mute_switch[index];
1909 static int scarlett2_mute_ctl_put(struct snd_kcontrol *kctl,
1910 struct snd_ctl_elem_value *ucontrol)
1912 struct usb_mixer_elem_info *elem = kctl->private_data;
1913 struct usb_mixer_interface *mixer = elem->head.mixer;
1914 struct scarlett2_data *private = mixer->private_data;
1915 int index = line_out_remap(private, elem->control);
1916 int oval, val, err = 0;
1918 mutex_lock(&private->data_mutex);
1920 oval = private->mute_switch[index];
1921 val = !!ucontrol->value.integer.value[0];
1926 private->mute_switch[index] = val;
1928 /* Send mute change to the device */
1929 err = scarlett2_usb_set_config(mixer, SCARLETT2_CONFIG_MUTE_SWITCH,
1935 mutex_unlock(&private->data_mutex);
1939 static const struct snd_kcontrol_new scarlett2_mute_ctl = {
1940 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1942 .info = snd_ctl_boolean_mono_info,
1943 .get = scarlett2_mute_ctl_get,
1944 .put = scarlett2_mute_ctl_put,
1947 /*** HW/SW Volume Switch Controls ***/
1949 static void scarlett2_sw_hw_ctl_ro(struct scarlett2_data *private, int index)
1951 private->sw_hw_ctls[index]->vd[0].access &=
1952 ~SNDRV_CTL_ELEM_ACCESS_WRITE;
1955 static void scarlett2_sw_hw_ctl_rw(struct scarlett2_data *private, int index)
1957 private->sw_hw_ctls[index]->vd[0].access |=
1958 SNDRV_CTL_ELEM_ACCESS_WRITE;
1961 static int scarlett2_sw_hw_enum_ctl_info(struct snd_kcontrol *kctl,
1962 struct snd_ctl_elem_info *uinfo)
1964 static const char *const values[2] = {
1968 return snd_ctl_enum_info(uinfo, 1, 2, values);
1971 static int scarlett2_sw_hw_enum_ctl_get(struct snd_kcontrol *kctl,
1972 struct snd_ctl_elem_value *ucontrol)
1974 struct usb_mixer_elem_info *elem = kctl->private_data;
1975 struct scarlett2_data *private = elem->head.mixer->private_data;
1976 int index = line_out_remap(private, elem->control);
1978 ucontrol->value.enumerated.item[0] = private->vol_sw_hw_switch[index];
1982 static void scarlett2_vol_ctl_set_writable(struct usb_mixer_interface *mixer,
1983 int index, int value)
1985 struct scarlett2_data *private = mixer->private_data;
1986 struct snd_card *card = mixer->chip->card;
1988 /* Set/Clear write bits */
1990 private->vol_ctls[index]->vd[0].access |=
1991 SNDRV_CTL_ELEM_ACCESS_WRITE;
1992 private->mute_ctls[index]->vd[0].access |=
1993 SNDRV_CTL_ELEM_ACCESS_WRITE;
1995 private->vol_ctls[index]->vd[0].access &=
1996 ~SNDRV_CTL_ELEM_ACCESS_WRITE;
1997 private->mute_ctls[index]->vd[0].access &=
1998 ~SNDRV_CTL_ELEM_ACCESS_WRITE;
2001 /* Notify of write bit and possible value change */
2002 snd_ctl_notify(card,
2003 SNDRV_CTL_EVENT_MASK_VALUE | SNDRV_CTL_EVENT_MASK_INFO,
2004 &private->vol_ctls[index]->id);
2005 snd_ctl_notify(card,
2006 SNDRV_CTL_EVENT_MASK_VALUE | SNDRV_CTL_EVENT_MASK_INFO,
2007 &private->mute_ctls[index]->id);
2010 static int scarlett2_sw_hw_change(struct usb_mixer_interface *mixer,
2011 int ctl_index, int val)
2013 struct scarlett2_data *private = mixer->private_data;
2014 int index = line_out_remap(private, ctl_index);
2017 private->vol_sw_hw_switch[index] = val;
2019 /* Change access mode to RO (hardware controlled volume)
2020 * or RW (software controlled volume)
2022 scarlett2_vol_ctl_set_writable(mixer, ctl_index, !val);
2024 /* Reset volume/mute to master volume/mute */
2025 private->vol[index] = private->master_vol;
2026 private->mute_switch[index] = private->dim_mute[SCARLETT2_BUTTON_MUTE];
2028 /* Set SW volume to current HW volume */
2029 err = scarlett2_usb_set_config(
2030 mixer, SCARLETT2_CONFIG_LINE_OUT_VOLUME,
2031 index, private->master_vol - SCARLETT2_VOLUME_BIAS);
2035 /* Set SW mute to current HW mute */
2036 err = scarlett2_usb_set_config(
2037 mixer, SCARLETT2_CONFIG_MUTE_SWITCH,
2038 index, private->dim_mute[SCARLETT2_BUTTON_MUTE]);
2042 /* Send SW/HW switch change to the device */
2043 return scarlett2_usb_set_config(mixer, SCARLETT2_CONFIG_SW_HW_SWITCH,
2047 static int scarlett2_sw_hw_enum_ctl_put(struct snd_kcontrol *kctl,
2048 struct snd_ctl_elem_value *ucontrol)
2050 struct usb_mixer_elem_info *elem = kctl->private_data;
2051 struct usb_mixer_interface *mixer = elem->head.mixer;
2052 struct scarlett2_data *private = mixer->private_data;
2053 int ctl_index = elem->control;
2054 int index = line_out_remap(private, ctl_index);
2055 int oval, val, err = 0;
2057 mutex_lock(&private->data_mutex);
2059 oval = private->vol_sw_hw_switch[index];
2060 val = !!ucontrol->value.enumerated.item[0];
2065 err = scarlett2_sw_hw_change(mixer, ctl_index, val);
2070 mutex_unlock(&private->data_mutex);
2074 static const struct snd_kcontrol_new scarlett2_sw_hw_enum_ctl = {
2075 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2077 .info = scarlett2_sw_hw_enum_ctl_info,
2078 .get = scarlett2_sw_hw_enum_ctl_get,
2079 .put = scarlett2_sw_hw_enum_ctl_put,
2082 /*** Line Level/Instrument Level Switch Controls ***/
2084 static int scarlett2_update_input_other(struct usb_mixer_interface *mixer)
2086 struct scarlett2_data *private = mixer->private_data;
2087 const struct scarlett2_device_info *info = private->info;
2089 private->input_other_updated = 0;
2091 if (info->level_input_count) {
2092 int err = scarlett2_usb_get_config(
2093 mixer, SCARLETT2_CONFIG_LEVEL_SWITCH,
2094 info->level_input_count + info->level_input_first,
2095 private->level_switch);
2100 if (info->pad_input_count) {
2101 int err = scarlett2_usb_get_config(
2102 mixer, SCARLETT2_CONFIG_PAD_SWITCH,
2103 info->pad_input_count, private->pad_switch);
2108 if (info->air_input_count) {
2109 int err = scarlett2_usb_get_config(
2110 mixer, SCARLETT2_CONFIG_AIR_SWITCH,
2111 info->air_input_count, private->air_switch);
2116 if (info->phantom_count) {
2117 int err = scarlett2_usb_get_config(
2118 mixer, SCARLETT2_CONFIG_PHANTOM_SWITCH,
2119 info->phantom_count, private->phantom_switch);
2123 err = scarlett2_usb_get_config(
2124 mixer, SCARLETT2_CONFIG_PHANTOM_PERSISTENCE,
2125 1, &private->phantom_persistence);
2133 static int scarlett2_level_enum_ctl_info(struct snd_kcontrol *kctl,
2134 struct snd_ctl_elem_info *uinfo)
2136 static const char *const values[2] = {
2140 return snd_ctl_enum_info(uinfo, 1, 2, values);
2143 static int scarlett2_level_enum_ctl_get(struct snd_kcontrol *kctl,
2144 struct snd_ctl_elem_value *ucontrol)
2146 struct usb_mixer_elem_info *elem = kctl->private_data;
2147 struct usb_mixer_interface *mixer = elem->head.mixer;
2148 struct scarlett2_data *private = mixer->private_data;
2149 const struct scarlett2_device_info *info = private->info;
2151 int index = elem->control + info->level_input_first;
2153 mutex_lock(&private->data_mutex);
2154 if (private->input_other_updated)
2155 scarlett2_update_input_other(mixer);
2156 ucontrol->value.enumerated.item[0] = private->level_switch[index];
2157 mutex_unlock(&private->data_mutex);
2162 static int scarlett2_level_enum_ctl_put(struct snd_kcontrol *kctl,
2163 struct snd_ctl_elem_value *ucontrol)
2165 struct usb_mixer_elem_info *elem = kctl->private_data;
2166 struct usb_mixer_interface *mixer = elem->head.mixer;
2167 struct scarlett2_data *private = mixer->private_data;
2168 const struct scarlett2_device_info *info = private->info;
2170 int index = elem->control + info->level_input_first;
2171 int oval, val, err = 0;
2173 mutex_lock(&private->data_mutex);
2175 oval = private->level_switch[index];
2176 val = !!ucontrol->value.enumerated.item[0];
2181 private->level_switch[index] = val;
2183 /* Send switch change to the device */
2184 err = scarlett2_usb_set_config(mixer, SCARLETT2_CONFIG_LEVEL_SWITCH,
2190 mutex_unlock(&private->data_mutex);
2194 static const struct snd_kcontrol_new scarlett2_level_enum_ctl = {
2195 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2197 .info = scarlett2_level_enum_ctl_info,
2198 .get = scarlett2_level_enum_ctl_get,
2199 .put = scarlett2_level_enum_ctl_put,
2202 /*** Pad Switch Controls ***/
2204 static int scarlett2_pad_ctl_get(struct snd_kcontrol *kctl,
2205 struct snd_ctl_elem_value *ucontrol)
2207 struct usb_mixer_elem_info *elem = kctl->private_data;
2208 struct usb_mixer_interface *mixer = elem->head.mixer;
2209 struct scarlett2_data *private = mixer->private_data;
2211 mutex_lock(&private->data_mutex);
2212 if (private->input_other_updated)
2213 scarlett2_update_input_other(mixer);
2214 ucontrol->value.integer.value[0] =
2215 private->pad_switch[elem->control];
2216 mutex_unlock(&private->data_mutex);
2221 static int scarlett2_pad_ctl_put(struct snd_kcontrol *kctl,
2222 struct snd_ctl_elem_value *ucontrol)
2224 struct usb_mixer_elem_info *elem = kctl->private_data;
2225 struct usb_mixer_interface *mixer = elem->head.mixer;
2226 struct scarlett2_data *private = mixer->private_data;
2228 int index = elem->control;
2229 int oval, val, err = 0;
2231 mutex_lock(&private->data_mutex);
2233 oval = private->pad_switch[index];
2234 val = !!ucontrol->value.integer.value[0];
2239 private->pad_switch[index] = val;
2241 /* Send switch change to the device */
2242 err = scarlett2_usb_set_config(mixer, SCARLETT2_CONFIG_PAD_SWITCH,
2248 mutex_unlock(&private->data_mutex);
2252 static const struct snd_kcontrol_new scarlett2_pad_ctl = {
2253 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2255 .info = snd_ctl_boolean_mono_info,
2256 .get = scarlett2_pad_ctl_get,
2257 .put = scarlett2_pad_ctl_put,
2260 /*** Air Switch Controls ***/
2262 static int scarlett2_air_ctl_get(struct snd_kcontrol *kctl,
2263 struct snd_ctl_elem_value *ucontrol)
2265 struct usb_mixer_elem_info *elem = kctl->private_data;
2266 struct usb_mixer_interface *mixer = elem->head.mixer;
2267 struct scarlett2_data *private = mixer->private_data;
2269 mutex_lock(&private->data_mutex);
2270 if (private->input_other_updated)
2271 scarlett2_update_input_other(mixer);
2272 ucontrol->value.integer.value[0] = private->air_switch[elem->control];
2273 mutex_unlock(&private->data_mutex);
2278 static int scarlett2_air_ctl_put(struct snd_kcontrol *kctl,
2279 struct snd_ctl_elem_value *ucontrol)
2281 struct usb_mixer_elem_info *elem = kctl->private_data;
2282 struct usb_mixer_interface *mixer = elem->head.mixer;
2283 struct scarlett2_data *private = mixer->private_data;
2285 int index = elem->control;
2286 int oval, val, err = 0;
2288 mutex_lock(&private->data_mutex);
2290 oval = private->air_switch[index];
2291 val = !!ucontrol->value.integer.value[0];
2296 private->air_switch[index] = val;
2298 /* Send switch change to the device */
2299 err = scarlett2_usb_set_config(mixer, SCARLETT2_CONFIG_AIR_SWITCH,
2305 mutex_unlock(&private->data_mutex);
2309 static const struct snd_kcontrol_new scarlett2_air_ctl = {
2310 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2312 .info = snd_ctl_boolean_mono_info,
2313 .get = scarlett2_air_ctl_get,
2314 .put = scarlett2_air_ctl_put,
2317 /*** Phantom Switch Controls ***/
2319 static int scarlett2_phantom_ctl_get(struct snd_kcontrol *kctl,
2320 struct snd_ctl_elem_value *ucontrol)
2322 struct usb_mixer_elem_info *elem = kctl->private_data;
2323 struct usb_mixer_interface *mixer = elem->head.mixer;
2324 struct scarlett2_data *private = mixer->private_data;
2326 mutex_lock(&private->data_mutex);
2327 if (private->input_other_updated)
2328 scarlett2_update_input_other(mixer);
2329 ucontrol->value.integer.value[0] =
2330 private->phantom_switch[elem->control];
2331 mutex_unlock(&private->data_mutex);
2336 static int scarlett2_phantom_ctl_put(struct snd_kcontrol *kctl,
2337 struct snd_ctl_elem_value *ucontrol)
2339 struct usb_mixer_elem_info *elem = kctl->private_data;
2340 struct usb_mixer_interface *mixer = elem->head.mixer;
2341 struct scarlett2_data *private = mixer->private_data;
2343 int index = elem->control;
2344 int oval, val, err = 0;
2346 mutex_lock(&private->data_mutex);
2348 oval = private->phantom_switch[index];
2349 val = !!ucontrol->value.integer.value[0];
2354 private->phantom_switch[index] = val;
2356 /* Send switch change to the device */
2357 err = scarlett2_usb_set_config(mixer, SCARLETT2_CONFIG_PHANTOM_SWITCH,
2363 mutex_unlock(&private->data_mutex);
2367 static const struct snd_kcontrol_new scarlett2_phantom_ctl = {
2368 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2370 .info = snd_ctl_boolean_mono_info,
2371 .get = scarlett2_phantom_ctl_get,
2372 .put = scarlett2_phantom_ctl_put,
2375 /*** Phantom Persistence Control ***/
2377 static int scarlett2_phantom_persistence_ctl_get(
2378 struct snd_kcontrol *kctl, struct snd_ctl_elem_value *ucontrol)
2380 struct usb_mixer_elem_info *elem = kctl->private_data;
2381 struct scarlett2_data *private = elem->head.mixer->private_data;
2383 ucontrol->value.integer.value[0] = private->phantom_persistence;
2387 static int scarlett2_phantom_persistence_ctl_put(
2388 struct snd_kcontrol *kctl, struct snd_ctl_elem_value *ucontrol)
2390 struct usb_mixer_elem_info *elem = kctl->private_data;
2391 struct usb_mixer_interface *mixer = elem->head.mixer;
2392 struct scarlett2_data *private = mixer->private_data;
2394 int index = elem->control;
2395 int oval, val, err = 0;
2397 mutex_lock(&private->data_mutex);
2399 oval = private->phantom_persistence;
2400 val = !!ucontrol->value.integer.value[0];
2405 private->phantom_persistence = val;
2407 /* Send switch change to the device */
2408 err = scarlett2_usb_set_config(
2409 mixer, SCARLETT2_CONFIG_PHANTOM_PERSISTENCE, index, val);
2414 mutex_unlock(&private->data_mutex);
2418 static const struct snd_kcontrol_new scarlett2_phantom_persistence_ctl = {
2419 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2421 .info = snd_ctl_boolean_mono_info,
2422 .get = scarlett2_phantom_persistence_ctl_get,
2423 .put = scarlett2_phantom_persistence_ctl_put,
2426 /*** Direct Monitor Control ***/
2428 static int scarlett2_update_monitor_other(struct usb_mixer_interface *mixer)
2430 struct scarlett2_data *private = mixer->private_data;
2431 const struct scarlett2_device_info *info = private->info;
2434 /* monitor_other_enable[0] enables speaker switching
2435 * monitor_other_enable[1] enables talkback
2437 u8 monitor_other_enable[2];
2439 /* monitor_other_switch[0] activates the alternate speakers
2440 * monitor_other_switch[1] activates talkback
2442 u8 monitor_other_switch[2];
2444 private->monitor_other_updated = 0;
2446 if (info->direct_monitor)
2447 return scarlett2_usb_get_config(
2448 mixer, SCARLETT2_CONFIG_DIRECT_MONITOR,
2449 1, &private->direct_monitor_switch);
2451 /* if it doesn't do speaker switching then it also doesn't do
2454 if (!info->has_speaker_switching)
2457 err = scarlett2_usb_get_config(
2458 mixer, SCARLETT2_CONFIG_MONITOR_OTHER_ENABLE,
2459 2, monitor_other_enable);
2463 err = scarlett2_usb_get_config(
2464 mixer, SCARLETT2_CONFIG_MONITOR_OTHER_SWITCH,
2465 2, monitor_other_switch);
2469 if (!monitor_other_enable[0])
2470 private->speaker_switching_switch = 0;
2472 private->speaker_switching_switch = monitor_other_switch[0] + 1;
2474 if (info->has_talkback) {
2475 const int (*port_count)[SCARLETT2_PORT_DIRNS] =
2478 port_count[SCARLETT2_PORT_TYPE_MIX][SCARLETT2_PORT_IN];
2482 if (!monitor_other_enable[1])
2483 private->talkback_switch = 0;
2485 private->talkback_switch = monitor_other_switch[1] + 1;
2487 err = scarlett2_usb_get_config(mixer,
2488 SCARLETT2_CONFIG_TALKBACK_MAP,
2492 for (i = 0; i < num_mixes; i++, bitmap >>= 1)
2493 private->talkback_map[i] = bitmap & 1;
2499 static int scarlett2_direct_monitor_ctl_get(
2500 struct snd_kcontrol *kctl, struct snd_ctl_elem_value *ucontrol)
2502 struct usb_mixer_elem_info *elem = kctl->private_data;
2503 struct usb_mixer_interface *mixer = elem->head.mixer;
2504 struct scarlett2_data *private = elem->head.mixer->private_data;
2506 mutex_lock(&private->data_mutex);
2507 if (private->monitor_other_updated)
2508 scarlett2_update_monitor_other(mixer);
2509 ucontrol->value.enumerated.item[0] = private->direct_monitor_switch;
2510 mutex_unlock(&private->data_mutex);
2515 static int scarlett2_direct_monitor_ctl_put(
2516 struct snd_kcontrol *kctl, struct snd_ctl_elem_value *ucontrol)
2518 struct usb_mixer_elem_info *elem = kctl->private_data;
2519 struct usb_mixer_interface *mixer = elem->head.mixer;
2520 struct scarlett2_data *private = mixer->private_data;
2522 int index = elem->control;
2523 int oval, val, err = 0;
2525 mutex_lock(&private->data_mutex);
2527 oval = private->direct_monitor_switch;
2528 val = min(ucontrol->value.enumerated.item[0], 2U);
2533 private->direct_monitor_switch = val;
2535 /* Send switch change to the device */
2536 err = scarlett2_usb_set_config(
2537 mixer, SCARLETT2_CONFIG_DIRECT_MONITOR, index, val);
2542 mutex_unlock(&private->data_mutex);
2546 static int scarlett2_direct_monitor_stereo_enum_ctl_info(
2547 struct snd_kcontrol *kctl, struct snd_ctl_elem_info *uinfo)
2549 static const char *const values[3] = {
2550 "Off", "Mono", "Stereo"
2553 return snd_ctl_enum_info(uinfo, 1, 3, values);
2556 /* Direct Monitor for Solo is mono-only and only needs a boolean control
2557 * Direct Monitor for 2i2 is selectable between Off/Mono/Stereo
2559 static const struct snd_kcontrol_new scarlett2_direct_monitor_ctl[2] = {
2561 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2563 .info = snd_ctl_boolean_mono_info,
2564 .get = scarlett2_direct_monitor_ctl_get,
2565 .put = scarlett2_direct_monitor_ctl_put,
2568 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2570 .info = scarlett2_direct_monitor_stereo_enum_ctl_info,
2571 .get = scarlett2_direct_monitor_ctl_get,
2572 .put = scarlett2_direct_monitor_ctl_put,
2576 static int scarlett2_add_direct_monitor_ctl(struct usb_mixer_interface *mixer)
2578 struct scarlett2_data *private = mixer->private_data;
2579 const struct scarlett2_device_info *info = private->info;
2582 if (!info->direct_monitor)
2585 s = info->direct_monitor == 1
2586 ? "Direct Monitor Playback Switch"
2587 : "Direct Monitor Playback Enum";
2589 return scarlett2_add_new_ctl(
2590 mixer, &scarlett2_direct_monitor_ctl[info->direct_monitor - 1],
2591 0, 1, s, &private->direct_monitor_ctl);
2594 /*** Speaker Switching Control ***/
2596 static int scarlett2_speaker_switch_enum_ctl_info(
2597 struct snd_kcontrol *kctl, struct snd_ctl_elem_info *uinfo)
2599 static const char *const values[3] = {
2600 "Off", "Main", "Alt"
2603 return snd_ctl_enum_info(uinfo, 1, 3, values);
2606 static int scarlett2_speaker_switch_enum_ctl_get(
2607 struct snd_kcontrol *kctl, struct snd_ctl_elem_value *ucontrol)
2609 struct usb_mixer_elem_info *elem = kctl->private_data;
2610 struct usb_mixer_interface *mixer = elem->head.mixer;
2611 struct scarlett2_data *private = mixer->private_data;
2613 mutex_lock(&private->data_mutex);
2614 if (private->monitor_other_updated)
2615 scarlett2_update_monitor_other(mixer);
2616 ucontrol->value.enumerated.item[0] = private->speaker_switching_switch;
2617 mutex_unlock(&private->data_mutex);
2622 /* when speaker switching gets enabled, switch the main/alt speakers
2623 * to HW volume and disable those controls
2625 static int scarlett2_speaker_switch_enable(struct usb_mixer_interface *mixer)
2627 struct snd_card *card = mixer->chip->card;
2628 struct scarlett2_data *private = mixer->private_data;
2631 for (i = 0; i < 4; i++) {
2632 int index = line_out_remap(private, i);
2634 /* switch the main/alt speakers to HW volume */
2635 if (!private->vol_sw_hw_switch[index]) {
2636 err = scarlett2_sw_hw_change(private->mixer, i, 1);
2641 /* disable the line out SW/HW switch */
2642 scarlett2_sw_hw_ctl_ro(private, i);
2643 snd_ctl_notify(card,
2644 SNDRV_CTL_EVENT_MASK_VALUE |
2645 SNDRV_CTL_EVENT_MASK_INFO,
2646 &private->sw_hw_ctls[i]->id);
2649 /* when the next monitor-other notify comes in, update the mux
2652 private->speaker_switching_switched = 1;
2657 /* when speaker switching gets disabled, reenable the hw/sw controls
2658 * and invalidate the routing
2660 static void scarlett2_speaker_switch_disable(struct usb_mixer_interface *mixer)
2662 struct snd_card *card = mixer->chip->card;
2663 struct scarlett2_data *private = mixer->private_data;
2666 /* enable the line out SW/HW switch */
2667 for (i = 0; i < 4; i++) {
2668 scarlett2_sw_hw_ctl_rw(private, i);
2669 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_INFO,
2670 &private->sw_hw_ctls[i]->id);
2673 /* when the next monitor-other notify comes in, update the mux
2676 private->speaker_switching_switched = 1;
2679 static int scarlett2_speaker_switch_enum_ctl_put(
2680 struct snd_kcontrol *kctl, struct snd_ctl_elem_value *ucontrol)
2682 struct usb_mixer_elem_info *elem = kctl->private_data;
2683 struct usb_mixer_interface *mixer = elem->head.mixer;
2684 struct scarlett2_data *private = mixer->private_data;
2686 int oval, val, err = 0;
2688 mutex_lock(&private->data_mutex);
2690 oval = private->speaker_switching_switch;
2691 val = min(ucontrol->value.enumerated.item[0], 2U);
2696 private->speaker_switching_switch = val;
2698 /* enable/disable speaker switching */
2699 err = scarlett2_usb_set_config(
2700 mixer, SCARLETT2_CONFIG_MONITOR_OTHER_ENABLE,
2705 /* if speaker switching is enabled, select main or alt */
2706 err = scarlett2_usb_set_config(
2707 mixer, SCARLETT2_CONFIG_MONITOR_OTHER_SWITCH,
2712 /* update controls if speaker switching gets enabled or disabled */
2714 err = scarlett2_speaker_switch_enable(mixer);
2715 else if (oval && !val)
2716 scarlett2_speaker_switch_disable(mixer);
2722 mutex_unlock(&private->data_mutex);
2726 static const struct snd_kcontrol_new scarlett2_speaker_switch_enum_ctl = {
2727 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2729 .info = scarlett2_speaker_switch_enum_ctl_info,
2730 .get = scarlett2_speaker_switch_enum_ctl_get,
2731 .put = scarlett2_speaker_switch_enum_ctl_put,
2734 static int scarlett2_add_speaker_switch_ctl(
2735 struct usb_mixer_interface *mixer)
2737 struct scarlett2_data *private = mixer->private_data;
2738 const struct scarlett2_device_info *info = private->info;
2740 if (!info->has_speaker_switching)
2743 return scarlett2_add_new_ctl(
2744 mixer, &scarlett2_speaker_switch_enum_ctl,
2745 0, 1, "Speaker Switching Playback Enum",
2746 &private->speaker_switching_ctl);
2749 /*** Talkback and Talkback Map Controls ***/
2751 static int scarlett2_talkback_enum_ctl_info(
2752 struct snd_kcontrol *kctl, struct snd_ctl_elem_info *uinfo)
2754 static const char *const values[3] = {
2755 "Disabled", "Off", "On"
2758 return snd_ctl_enum_info(uinfo, 1, 3, values);
2761 static int scarlett2_talkback_enum_ctl_get(
2762 struct snd_kcontrol *kctl, struct snd_ctl_elem_value *ucontrol)
2764 struct usb_mixer_elem_info *elem = kctl->private_data;
2765 struct usb_mixer_interface *mixer = elem->head.mixer;
2766 struct scarlett2_data *private = mixer->private_data;
2768 mutex_lock(&private->data_mutex);
2769 if (private->monitor_other_updated)
2770 scarlett2_update_monitor_other(mixer);
2771 ucontrol->value.enumerated.item[0] = private->talkback_switch;
2772 mutex_unlock(&private->data_mutex);
2777 static int scarlett2_talkback_enum_ctl_put(
2778 struct snd_kcontrol *kctl, struct snd_ctl_elem_value *ucontrol)
2780 struct usb_mixer_elem_info *elem = kctl->private_data;
2781 struct usb_mixer_interface *mixer = elem->head.mixer;
2782 struct scarlett2_data *private = mixer->private_data;
2784 int oval, val, err = 0;
2786 mutex_lock(&private->data_mutex);
2788 oval = private->talkback_switch;
2789 val = min(ucontrol->value.enumerated.item[0], 2U);
2794 private->talkback_switch = val;
2796 /* enable/disable talkback */
2797 err = scarlett2_usb_set_config(
2798 mixer, SCARLETT2_CONFIG_MONITOR_OTHER_ENABLE,
2803 /* if talkback is enabled, select main or alt */
2804 err = scarlett2_usb_set_config(
2805 mixer, SCARLETT2_CONFIG_MONITOR_OTHER_SWITCH,
2811 mutex_unlock(&private->data_mutex);
2815 static const struct snd_kcontrol_new scarlett2_talkback_enum_ctl = {
2816 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2818 .info = scarlett2_talkback_enum_ctl_info,
2819 .get = scarlett2_talkback_enum_ctl_get,
2820 .put = scarlett2_talkback_enum_ctl_put,
2823 static int scarlett2_talkback_map_ctl_get(
2824 struct snd_kcontrol *kctl, struct snd_ctl_elem_value *ucontrol)
2826 struct usb_mixer_elem_info *elem = kctl->private_data;
2827 struct usb_mixer_interface *mixer = elem->head.mixer;
2828 struct scarlett2_data *private = mixer->private_data;
2829 int index = elem->control;
2831 ucontrol->value.integer.value[0] = private->talkback_map[index];
2836 static int scarlett2_talkback_map_ctl_put(
2837 struct snd_kcontrol *kctl, struct snd_ctl_elem_value *ucontrol)
2839 struct usb_mixer_elem_info *elem = kctl->private_data;
2840 struct usb_mixer_interface *mixer = elem->head.mixer;
2841 struct scarlett2_data *private = mixer->private_data;
2842 const int (*port_count)[SCARLETT2_PORT_DIRNS] =
2843 private->info->port_count;
2844 int num_mixes = port_count[SCARLETT2_PORT_TYPE_MIX][SCARLETT2_PORT_IN];
2846 int index = elem->control;
2847 int oval, val, err = 0, i;
2850 mutex_lock(&private->data_mutex);
2852 oval = private->talkback_map[index];
2853 val = !!ucontrol->value.integer.value[0];
2858 private->talkback_map[index] = val;
2860 for (i = 0; i < num_mixes; i++)
2861 bitmap |= private->talkback_map[i] << i;
2863 /* Send updated bitmap to the device */
2864 err = scarlett2_usb_set_config(mixer, SCARLETT2_CONFIG_TALKBACK_MAP,
2870 mutex_unlock(&private->data_mutex);
2874 static const struct snd_kcontrol_new scarlett2_talkback_map_ctl = {
2875 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2877 .info = snd_ctl_boolean_mono_info,
2878 .get = scarlett2_talkback_map_ctl_get,
2879 .put = scarlett2_talkback_map_ctl_put,
2882 static int scarlett2_add_talkback_ctls(
2883 struct usb_mixer_interface *mixer)
2885 struct scarlett2_data *private = mixer->private_data;
2886 const struct scarlett2_device_info *info = private->info;
2887 const int (*port_count)[SCARLETT2_PORT_DIRNS] = info->port_count;
2888 int num_mixes = port_count[SCARLETT2_PORT_TYPE_MIX][SCARLETT2_PORT_IN];
2890 char s[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
2892 if (!info->has_talkback)
2895 err = scarlett2_add_new_ctl(
2896 mixer, &scarlett2_talkback_enum_ctl,
2897 0, 1, "Talkback Playback Enum",
2898 &private->talkback_ctl);
2902 for (i = 0; i < num_mixes; i++) {
2903 snprintf(s, sizeof(s),
2904 "Talkback Mix %c Playback Switch", i + 'A');
2905 err = scarlett2_add_new_ctl(mixer, &scarlett2_talkback_map_ctl,
2914 /*** Dim/Mute Controls ***/
2916 static int scarlett2_dim_mute_ctl_get(struct snd_kcontrol *kctl,
2917 struct snd_ctl_elem_value *ucontrol)
2919 struct usb_mixer_elem_info *elem = kctl->private_data;
2920 struct usb_mixer_interface *mixer = elem->head.mixer;
2921 struct scarlett2_data *private = mixer->private_data;
2923 mutex_lock(&private->data_mutex);
2924 if (private->vol_updated)
2925 scarlett2_update_volumes(mixer);
2926 mutex_unlock(&private->data_mutex);
2928 ucontrol->value.integer.value[0] = private->dim_mute[elem->control];
2932 static int scarlett2_dim_mute_ctl_put(struct snd_kcontrol *kctl,
2933 struct snd_ctl_elem_value *ucontrol)
2935 struct usb_mixer_elem_info *elem = kctl->private_data;
2936 struct usb_mixer_interface *mixer = elem->head.mixer;
2937 struct scarlett2_data *private = mixer->private_data;
2938 const struct scarlett2_device_info *info = private->info;
2939 const int (*port_count)[SCARLETT2_PORT_DIRNS] = info->port_count;
2941 port_count[SCARLETT2_PORT_TYPE_ANALOGUE][SCARLETT2_PORT_OUT];
2943 int index = elem->control;
2944 int oval, val, err = 0, i;
2946 mutex_lock(&private->data_mutex);
2948 oval = private->dim_mute[index];
2949 val = !!ucontrol->value.integer.value[0];
2954 private->dim_mute[index] = val;
2956 /* Send switch change to the device */
2957 err = scarlett2_usb_set_config(mixer, SCARLETT2_CONFIG_DIM_MUTE,
2962 if (index == SCARLETT2_BUTTON_MUTE)
2963 for (i = 0; i < num_line_out; i++) {
2964 int line_index = line_out_remap(private, i);
2966 if (private->vol_sw_hw_switch[line_index]) {
2967 private->mute_switch[line_index] = val;
2968 snd_ctl_notify(mixer->chip->card,
2969 SNDRV_CTL_EVENT_MASK_VALUE,
2970 &private->mute_ctls[i]->id);
2975 mutex_unlock(&private->data_mutex);
2979 static const struct snd_kcontrol_new scarlett2_dim_mute_ctl = {
2980 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2982 .info = snd_ctl_boolean_mono_info,
2983 .get = scarlett2_dim_mute_ctl_get,
2984 .put = scarlett2_dim_mute_ctl_put
2987 /*** Create the analogue output controls ***/
2989 static int scarlett2_add_line_out_ctls(struct usb_mixer_interface *mixer)
2991 struct scarlett2_data *private = mixer->private_data;
2992 const struct scarlett2_device_info *info = private->info;
2993 const int (*port_count)[SCARLETT2_PORT_DIRNS] = info->port_count;
2995 port_count[SCARLETT2_PORT_TYPE_ANALOGUE][SCARLETT2_PORT_OUT];
2997 char s[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
2999 /* Add R/O HW volume control */
3000 if (info->line_out_hw_vol) {
3001 snprintf(s, sizeof(s), "Master HW Playback Volume");
3002 err = scarlett2_add_new_ctl(mixer,
3003 &scarlett2_master_volume_ctl,
3004 0, 1, s, &private->master_vol_ctl);
3009 /* Add volume controls */
3010 for (i = 0; i < num_line_out; i++) {
3011 int index = line_out_remap(private, i);
3014 if (info->line_out_descrs[i])
3015 snprintf(s, sizeof(s),
3016 "Line %02d (%s) Playback Volume",
3017 i + 1, info->line_out_descrs[i]);
3019 snprintf(s, sizeof(s),
3020 "Line %02d Playback Volume",
3022 err = scarlett2_add_new_ctl(mixer,
3023 &scarlett2_line_out_volume_ctl,
3024 i, 1, s, &private->vol_ctls[i]);
3029 snprintf(s, sizeof(s),
3030 "Line %02d Mute Playback Switch",
3032 err = scarlett2_add_new_ctl(mixer,
3033 &scarlett2_mute_ctl,
3035 &private->mute_ctls[i]);
3039 /* Make the fader and mute controls read-only if the
3040 * SW/HW switch is set to HW
3042 if (private->vol_sw_hw_switch[index])
3043 scarlett2_vol_ctl_set_writable(mixer, i, 0);
3046 if (info->line_out_hw_vol) {
3047 snprintf(s, sizeof(s),
3048 "Line Out %02d Volume Control Playback Enum",
3050 err = scarlett2_add_new_ctl(mixer,
3051 &scarlett2_sw_hw_enum_ctl,
3053 &private->sw_hw_ctls[i]);
3057 /* Make the switch read-only if the line is
3058 * involved in speaker switching
3060 if (private->speaker_switching_switch && i < 4)
3061 scarlett2_sw_hw_ctl_ro(private, i);
3065 /* Add dim/mute controls */
3066 if (info->line_out_hw_vol)
3067 for (i = 0; i < SCARLETT2_DIM_MUTE_COUNT; i++) {
3068 err = scarlett2_add_new_ctl(
3069 mixer, &scarlett2_dim_mute_ctl,
3070 i, 1, scarlett2_dim_mute_names[i],
3071 &private->dim_mute_ctls[i]);
3079 /*** Create the analogue input controls ***/
3081 static int scarlett2_add_line_in_ctls(struct usb_mixer_interface *mixer)
3083 struct scarlett2_data *private = mixer->private_data;
3084 const struct scarlett2_device_info *info = private->info;
3086 char s[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
3087 const char *fmt = "Line In %d %s Capture %s";
3088 const char *fmt2 = "Line In %d-%d %s Capture %s";
3090 /* Add input level (line/inst) controls */
3091 for (i = 0; i < info->level_input_count; i++) {
3092 snprintf(s, sizeof(s), fmt, i + 1 + info->level_input_first,
3094 err = scarlett2_add_new_ctl(mixer, &scarlett2_level_enum_ctl,
3095 i, 1, s, &private->level_ctls[i]);
3100 /* Add input pad controls */
3101 for (i = 0; i < info->pad_input_count; i++) {
3102 snprintf(s, sizeof(s), fmt, i + 1, "Pad", "Switch");
3103 err = scarlett2_add_new_ctl(mixer, &scarlett2_pad_ctl,
3104 i, 1, s, &private->pad_ctls[i]);
3109 /* Add input air controls */
3110 for (i = 0; i < info->air_input_count; i++) {
3111 snprintf(s, sizeof(s), fmt, i + 1, "Air", "Switch");
3112 err = scarlett2_add_new_ctl(mixer, &scarlett2_air_ctl,
3113 i, 1, s, &private->air_ctls[i]);
3118 /* Add input phantom controls */
3119 if (info->inputs_per_phantom == 1) {
3120 for (i = 0; i < info->phantom_count; i++) {
3121 snprintf(s, sizeof(s), fmt, i + 1,
3122 "Phantom Power", "Switch");
3123 err = scarlett2_add_new_ctl(
3124 mixer, &scarlett2_phantom_ctl,
3125 i, 1, s, &private->phantom_ctls[i]);
3129 } else if (info->inputs_per_phantom > 1) {
3130 for (i = 0; i < info->phantom_count; i++) {
3131 int from = i * info->inputs_per_phantom + 1;
3132 int to = (i + 1) * info->inputs_per_phantom;
3134 snprintf(s, sizeof(s), fmt2, from, to,
3135 "Phantom Power", "Switch");
3136 err = scarlett2_add_new_ctl(
3137 mixer, &scarlett2_phantom_ctl,
3138 i, 1, s, &private->phantom_ctls[i]);
3143 if (info->phantom_count) {
3144 err = scarlett2_add_new_ctl(
3145 mixer, &scarlett2_phantom_persistence_ctl, 0, 1,
3146 "Phantom Power Persistence Capture Switch", NULL);
3154 /*** Mixer Volume Controls ***/
3156 static int scarlett2_mixer_ctl_info(struct snd_kcontrol *kctl,
3157 struct snd_ctl_elem_info *uinfo)
3159 struct usb_mixer_elem_info *elem = kctl->private_data;
3161 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
3162 uinfo->count = elem->channels;
3163 uinfo->value.integer.min = 0;
3164 uinfo->value.integer.max = SCARLETT2_MIXER_MAX_VALUE;
3165 uinfo->value.integer.step = 1;
3169 static int scarlett2_mixer_ctl_get(struct snd_kcontrol *kctl,
3170 struct snd_ctl_elem_value *ucontrol)
3172 struct usb_mixer_elem_info *elem = kctl->private_data;
3173 struct scarlett2_data *private = elem->head.mixer->private_data;
3175 ucontrol->value.integer.value[0] = private->mix[elem->control];
3179 static int scarlett2_mixer_ctl_put(struct snd_kcontrol *kctl,
3180 struct snd_ctl_elem_value *ucontrol)
3182 struct usb_mixer_elem_info *elem = kctl->private_data;
3183 struct usb_mixer_interface *mixer = elem->head.mixer;
3184 struct scarlett2_data *private = mixer->private_data;
3185 const struct scarlett2_device_info *info = private->info;
3186 const int (*port_count)[SCARLETT2_PORT_DIRNS] = info->port_count;
3187 int oval, val, num_mixer_in, mix_num, err = 0;
3188 int index = elem->control;
3190 mutex_lock(&private->data_mutex);
3192 oval = private->mix[index];
3193 val = ucontrol->value.integer.value[0];
3194 num_mixer_in = port_count[SCARLETT2_PORT_TYPE_MIX][SCARLETT2_PORT_OUT];
3195 mix_num = index / num_mixer_in;
3200 private->mix[index] = val;
3201 err = scarlett2_usb_set_mix(mixer, mix_num);
3206 mutex_unlock(&private->data_mutex);
3210 static const DECLARE_TLV_DB_MINMAX(
3211 db_scale_scarlett2_mixer,
3212 SCARLETT2_MIXER_MIN_DB * 100,
3213 SCARLETT2_MIXER_MAX_DB * 100
3216 static const struct snd_kcontrol_new scarlett2_mixer_ctl = {
3217 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3218 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE |
3219 SNDRV_CTL_ELEM_ACCESS_TLV_READ,
3221 .info = scarlett2_mixer_ctl_info,
3222 .get = scarlett2_mixer_ctl_get,
3223 .put = scarlett2_mixer_ctl_put,
3224 .private_value = SCARLETT2_MIXER_MAX_DB, /* max value */
3225 .tlv = { .p = db_scale_scarlett2_mixer }
3228 static int scarlett2_add_mixer_ctls(struct usb_mixer_interface *mixer)
3230 struct scarlett2_data *private = mixer->private_data;
3231 const struct scarlett2_device_info *info = private->info;
3232 const int (*port_count)[SCARLETT2_PORT_DIRNS] = info->port_count;
3235 char s[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
3238 port_count[SCARLETT2_PORT_TYPE_MIX][SCARLETT2_PORT_OUT];
3240 port_count[SCARLETT2_PORT_TYPE_MIX][SCARLETT2_PORT_IN];
3242 for (i = 0, index = 0; i < num_outputs; i++)
3243 for (j = 0; j < num_inputs; j++, index++) {
3244 snprintf(s, sizeof(s),
3245 "Mix %c Input %02d Playback Volume",
3247 err = scarlett2_add_new_ctl(mixer, &scarlett2_mixer_ctl,
3256 /*** Mux Source Selection Controls ***/
3258 static int scarlett2_mux_src_enum_ctl_info(struct snd_kcontrol *kctl,
3259 struct snd_ctl_elem_info *uinfo)
3261 struct usb_mixer_elem_info *elem = kctl->private_data;
3262 struct scarlett2_data *private = elem->head.mixer->private_data;
3263 const struct scarlett2_device_info *info = private->info;
3264 const int (*port_count)[SCARLETT2_PORT_DIRNS] = info->port_count;
3265 unsigned int item = uinfo->value.enumerated.item;
3266 int items = private->num_mux_srcs;
3269 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
3270 uinfo->count = elem->channels;
3271 uinfo->value.enumerated.items = items;
3274 item = uinfo->value.enumerated.item = items - 1;
3277 port_type < SCARLETT2_PORT_TYPE_COUNT;
3279 if (item < port_count[port_type][SCARLETT2_PORT_IN]) {
3280 const struct scarlett2_port *port =
3281 &scarlett2_ports[port_type];
3283 sprintf(uinfo->value.enumerated.name,
3284 port->src_descr, item + port->src_num_offset);
3287 item -= port_count[port_type][SCARLETT2_PORT_IN];
3293 static int scarlett2_mux_src_enum_ctl_get(struct snd_kcontrol *kctl,
3294 struct snd_ctl_elem_value *ucontrol)
3296 struct usb_mixer_elem_info *elem = kctl->private_data;
3297 struct usb_mixer_interface *mixer = elem->head.mixer;
3298 struct scarlett2_data *private = mixer->private_data;
3299 const struct scarlett2_device_info *info = private->info;
3300 const int (*port_count)[SCARLETT2_PORT_DIRNS] = info->port_count;
3301 int line_out_count =
3302 port_count[SCARLETT2_PORT_TYPE_ANALOGUE][SCARLETT2_PORT_OUT];
3303 int index = elem->control;
3305 if (index < line_out_count)
3306 index = line_out_remap(private, index);
3308 mutex_lock(&private->data_mutex);
3309 if (private->mux_updated)
3310 scarlett2_usb_get_mux(mixer);
3311 ucontrol->value.enumerated.item[0] = private->mux[index];
3312 mutex_unlock(&private->data_mutex);
3317 static int scarlett2_mux_src_enum_ctl_put(struct snd_kcontrol *kctl,
3318 struct snd_ctl_elem_value *ucontrol)
3320 struct usb_mixer_elem_info *elem = kctl->private_data;
3321 struct usb_mixer_interface *mixer = elem->head.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;
3325 int line_out_count =
3326 port_count[SCARLETT2_PORT_TYPE_ANALOGUE][SCARLETT2_PORT_OUT];
3327 int index = elem->control;
3328 int oval, val, err = 0;
3330 if (index < line_out_count)
3331 index = line_out_remap(private, index);
3333 mutex_lock(&private->data_mutex);
3335 oval = private->mux[index];
3336 val = min(ucontrol->value.enumerated.item[0],
3337 private->num_mux_srcs - 1U);
3342 private->mux[index] = val;
3343 err = scarlett2_usb_set_mux(mixer);
3348 mutex_unlock(&private->data_mutex);
3352 static const struct snd_kcontrol_new scarlett2_mux_src_enum_ctl = {
3353 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3355 .info = scarlett2_mux_src_enum_ctl_info,
3356 .get = scarlett2_mux_src_enum_ctl_get,
3357 .put = scarlett2_mux_src_enum_ctl_put,
3360 static int scarlett2_add_mux_enums(struct usb_mixer_interface *mixer)
3362 struct scarlett2_data *private = mixer->private_data;
3363 const struct scarlett2_device_info *info = private->info;
3364 const int (*port_count)[SCARLETT2_PORT_DIRNS] = info->port_count;
3365 int port_type, channel, i;
3367 for (i = 0, port_type = 0;
3368 port_type < SCARLETT2_PORT_TYPE_COUNT;
3371 channel < port_count[port_type][SCARLETT2_PORT_OUT];
3374 char s[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
3375 const char *const descr =
3376 scarlett2_ports[port_type].dst_descr;
3378 snprintf(s, sizeof(s) - 5, descr, channel + 1);
3381 err = scarlett2_add_new_ctl(mixer,
3382 &scarlett2_mux_src_enum_ctl,
3384 &private->mux_ctls[i]);
3393 /*** Meter Controls ***/
3395 static int scarlett2_meter_ctl_info(struct snd_kcontrol *kctl,
3396 struct snd_ctl_elem_info *uinfo)
3398 struct usb_mixer_elem_info *elem = kctl->private_data;
3400 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
3401 uinfo->count = elem->channels;
3402 uinfo->value.integer.min = 0;
3403 uinfo->value.integer.max = 4095;
3404 uinfo->value.integer.step = 1;
3408 static int scarlett2_meter_ctl_get(struct snd_kcontrol *kctl,
3409 struct snd_ctl_elem_value *ucontrol)
3411 struct usb_mixer_elem_info *elem = kctl->private_data;
3412 u16 meter_levels[SCARLETT2_MAX_METERS];
3415 err = scarlett2_usb_get_meter_levels(elem->head.mixer, elem->channels,
3420 for (i = 0; i < elem->channels; i++)
3421 ucontrol->value.integer.value[i] = meter_levels[i];
3426 static const struct snd_kcontrol_new scarlett2_meter_ctl = {
3427 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
3428 .access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE,
3430 .info = scarlett2_meter_ctl_info,
3431 .get = scarlett2_meter_ctl_get
3434 static int scarlett2_add_meter_ctl(struct usb_mixer_interface *mixer)
3436 struct scarlett2_data *private = mixer->private_data;
3438 /* devices without a mixer also don't support reporting levels */
3439 if (private->info->config_set == SCARLETT2_CONFIG_SET_NO_MIXER)
3442 return scarlett2_add_new_ctl(mixer, &scarlett2_meter_ctl,
3443 0, private->num_mux_dsts,
3444 "Level Meter", NULL);
3447 /*** MSD Controls ***/
3449 static int scarlett2_msd_ctl_get(struct snd_kcontrol *kctl,
3450 struct snd_ctl_elem_value *ucontrol)
3452 struct usb_mixer_elem_info *elem = kctl->private_data;
3453 struct scarlett2_data *private = elem->head.mixer->private_data;
3455 ucontrol->value.integer.value[0] = private->msd_switch;
3459 static int scarlett2_msd_ctl_put(struct snd_kcontrol *kctl,
3460 struct snd_ctl_elem_value *ucontrol)
3462 struct usb_mixer_elem_info *elem = kctl->private_data;
3463 struct usb_mixer_interface *mixer = elem->head.mixer;
3464 struct scarlett2_data *private = mixer->private_data;
3466 int oval, val, err = 0;
3468 mutex_lock(&private->data_mutex);
3470 oval = private->msd_switch;
3471 val = !!ucontrol->value.integer.value[0];
3476 private->msd_switch = val;
3478 /* Send switch change to the device */
3479 err = scarlett2_usb_set_config(mixer, SCARLETT2_CONFIG_MSD_SWITCH,
3485 mutex_unlock(&private->data_mutex);
3489 static const struct snd_kcontrol_new scarlett2_msd_ctl = {
3490 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3492 .info = snd_ctl_boolean_mono_info,
3493 .get = scarlett2_msd_ctl_get,
3494 .put = scarlett2_msd_ctl_put,
3497 static int scarlett2_add_msd_ctl(struct usb_mixer_interface *mixer)
3499 struct scarlett2_data *private = mixer->private_data;
3500 const struct scarlett2_device_info *info = private->info;
3502 if (!info->has_msd_mode)
3505 /* If MSD mode is off, hide the switch by default */
3506 if (!private->msd_switch && !(mixer->chip->setup & SCARLETT2_MSD_ENABLE))
3509 /* Add MSD control */
3510 return scarlett2_add_new_ctl(mixer, &scarlett2_msd_ctl,
3511 0, 1, "MSD Mode Switch", NULL);
3514 /*** Standalone Control ***/
3516 static int scarlett2_standalone_ctl_get(struct snd_kcontrol *kctl,
3517 struct snd_ctl_elem_value *ucontrol)
3519 struct usb_mixer_elem_info *elem = kctl->private_data;
3520 struct scarlett2_data *private = elem->head.mixer->private_data;
3522 ucontrol->value.integer.value[0] = private->standalone_switch;
3526 static int scarlett2_standalone_ctl_put(struct snd_kcontrol *kctl,
3527 struct snd_ctl_elem_value *ucontrol)
3529 struct usb_mixer_elem_info *elem = kctl->private_data;
3530 struct usb_mixer_interface *mixer = elem->head.mixer;
3531 struct scarlett2_data *private = mixer->private_data;
3533 int oval, val, err = 0;
3535 mutex_lock(&private->data_mutex);
3537 oval = private->standalone_switch;
3538 val = !!ucontrol->value.integer.value[0];
3543 private->standalone_switch = val;
3545 /* Send switch change to the device */
3546 err = scarlett2_usb_set_config(mixer,
3547 SCARLETT2_CONFIG_STANDALONE_SWITCH,
3553 mutex_unlock(&private->data_mutex);
3557 static const struct snd_kcontrol_new scarlett2_standalone_ctl = {
3558 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3560 .info = snd_ctl_boolean_mono_info,
3561 .get = scarlett2_standalone_ctl_get,
3562 .put = scarlett2_standalone_ctl_put,
3565 static int scarlett2_add_standalone_ctl(struct usb_mixer_interface *mixer)
3567 struct scarlett2_data *private = mixer->private_data;
3569 if (private->info->config_set == SCARLETT2_CONFIG_SET_NO_MIXER)
3572 /* Add standalone control */
3573 return scarlett2_add_new_ctl(mixer, &scarlett2_standalone_ctl,
3574 0, 1, "Standalone Switch", NULL);
3577 /*** Cleanup/Suspend Callbacks ***/
3579 static void scarlett2_private_free(struct usb_mixer_interface *mixer)
3581 struct scarlett2_data *private = mixer->private_data;
3583 cancel_delayed_work_sync(&private->work);
3585 mixer->private_data = NULL;
3588 static void scarlett2_private_suspend(struct usb_mixer_interface *mixer)
3590 struct scarlett2_data *private = mixer->private_data;
3592 if (cancel_delayed_work_sync(&private->work))
3593 scarlett2_config_save(private->mixer);
3596 /*** Initialisation ***/
3598 static void scarlett2_count_mux_io(struct scarlett2_data *private)
3600 const struct scarlett2_device_info *info = private->info;
3601 const int (*port_count)[SCARLETT2_PORT_DIRNS] = info->port_count;
3602 int port_type, srcs = 0, dsts = 0;
3605 port_type < SCARLETT2_PORT_TYPE_COUNT;
3607 srcs += port_count[port_type][SCARLETT2_PORT_IN];
3608 dsts += port_count[port_type][SCARLETT2_PORT_OUT];
3611 private->num_mux_srcs = srcs;
3612 private->num_mux_dsts = dsts;
3615 /* Look through the interface descriptors for the Focusrite Control
3616 * interface (bInterfaceClass = 255 Vendor Specific Class) and set
3617 * bInterfaceNumber, bEndpointAddress, wMaxPacketSize, and bInterval
3620 static int scarlett2_find_fc_interface(struct usb_device *dev,
3621 struct scarlett2_data *private)
3623 struct usb_host_config *config = dev->actconfig;
3626 for (i = 0; i < config->desc.bNumInterfaces; i++) {
3627 struct usb_interface *intf = config->interface[i];
3628 struct usb_interface_descriptor *desc =
3629 &intf->altsetting[0].desc;
3630 struct usb_endpoint_descriptor *epd;
3632 if (desc->bInterfaceClass != 255)
3635 epd = get_endpoint(intf->altsetting, 0);
3636 private->bInterfaceNumber = desc->bInterfaceNumber;
3637 private->bEndpointAddress = epd->bEndpointAddress &
3638 USB_ENDPOINT_NUMBER_MASK;
3639 private->wMaxPacketSize = le16_to_cpu(epd->wMaxPacketSize);
3640 private->bInterval = epd->bInterval;
3647 /* Initialise private data */
3648 static int scarlett2_init_private(struct usb_mixer_interface *mixer,
3649 const struct scarlett2_device_info *info)
3651 struct scarlett2_data *private =
3652 kzalloc(sizeof(struct scarlett2_data), GFP_KERNEL);
3657 mutex_init(&private->usb_mutex);
3658 mutex_init(&private->data_mutex);
3659 INIT_DELAYED_WORK(&private->work, scarlett2_config_save_work);
3661 mixer->private_data = private;
3662 mixer->private_free = scarlett2_private_free;
3663 mixer->private_suspend = scarlett2_private_suspend;
3665 private->info = info;
3666 scarlett2_count_mux_io(private);
3667 private->scarlett2_seq = 0;
3668 private->mixer = mixer;
3670 return scarlett2_find_fc_interface(mixer->chip->dev, private);
3673 /* Cargo cult proprietary initialisation sequence */
3674 static int scarlett2_usb_init(struct usb_mixer_interface *mixer)
3676 struct usb_device *dev = mixer->chip->dev;
3677 struct scarlett2_data *private = mixer->private_data;
3681 if (usb_pipe_type_check(dev, usb_sndctrlpipe(dev, 0)))
3685 err = scarlett2_usb_rx(dev, private->bInterfaceNumber,
3686 SCARLETT2_USB_CMD_INIT, buf, sizeof(buf));
3691 private->scarlett2_seq = 1;
3692 err = scarlett2_usb(mixer, SCARLETT2_USB_INIT_1, NULL, 0, NULL, 0);
3697 private->scarlett2_seq = 1;
3698 return scarlett2_usb(mixer, SCARLETT2_USB_INIT_2, NULL, 0, NULL, 84);
3701 /* Read configuration from the interface on start */
3702 static int scarlett2_read_configs(struct usb_mixer_interface *mixer)
3704 struct scarlett2_data *private = mixer->private_data;
3705 const struct scarlett2_device_info *info = private->info;
3706 const int (*port_count)[SCARLETT2_PORT_DIRNS] = info->port_count;
3708 port_count[SCARLETT2_PORT_TYPE_ANALOGUE][SCARLETT2_PORT_OUT];
3710 port_count[SCARLETT2_PORT_TYPE_MIX][SCARLETT2_PORT_IN];
3711 struct scarlett2_usb_volume_status volume_status;
3714 if (info->has_msd_mode) {
3715 err = scarlett2_usb_get_config(
3716 mixer, SCARLETT2_CONFIG_MSD_SWITCH,
3717 1, &private->msd_switch);
3721 /* no other controls are created if MSD mode is on */
3722 if (private->msd_switch)
3726 err = scarlett2_update_input_other(mixer);
3730 err = scarlett2_update_monitor_other(mixer);
3734 /* the rest of the configuration is for devices with a mixer */
3735 if (info->config_set == SCARLETT2_CONFIG_SET_NO_MIXER)
3738 err = scarlett2_usb_get_config(
3739 mixer, SCARLETT2_CONFIG_STANDALONE_SWITCH,
3740 1, &private->standalone_switch);
3744 err = scarlett2_update_sync(mixer);
3748 err = scarlett2_usb_get_volume_status(mixer, &volume_status);
3752 if (info->line_out_hw_vol)
3753 for (i = 0; i < SCARLETT2_DIM_MUTE_COUNT; i++)
3754 private->dim_mute[i] = !!volume_status.dim_mute[i];
3756 private->master_vol = clamp(
3757 volume_status.master_vol + SCARLETT2_VOLUME_BIAS,
3758 0, SCARLETT2_VOLUME_BIAS);
3760 for (i = 0; i < num_line_out; i++) {
3763 private->vol_sw_hw_switch[i] =
3764 info->line_out_hw_vol
3765 && volume_status.sw_hw_switch[i];
3767 volume = private->vol_sw_hw_switch[i]
3768 ? volume_status.master_vol
3769 : volume_status.sw_vol[i];
3770 volume = clamp(volume + SCARLETT2_VOLUME_BIAS,
3771 0, SCARLETT2_VOLUME_BIAS);
3772 private->vol[i] = volume;
3774 mute = private->vol_sw_hw_switch[i]
3775 ? private->dim_mute[SCARLETT2_BUTTON_MUTE]
3776 : volume_status.mute_switch[i];
3777 private->mute_switch[i] = mute;
3780 for (i = 0; i < num_mixer_out; i++) {
3781 err = scarlett2_usb_get_mix(mixer, i);
3786 return scarlett2_usb_get_mux(mixer);
3789 /* Notify on sync change */
3790 static void scarlett2_notify_sync(
3791 struct usb_mixer_interface *mixer)
3793 struct scarlett2_data *private = mixer->private_data;
3795 private->sync_updated = 1;
3797 snd_ctl_notify(mixer->chip->card, SNDRV_CTL_EVENT_MASK_VALUE,
3798 &private->sync_ctl->id);
3801 /* Notify on monitor change */
3802 static void scarlett2_notify_monitor(
3803 struct usb_mixer_interface *mixer)
3805 struct snd_card *card = mixer->chip->card;
3806 struct scarlett2_data *private = mixer->private_data;
3807 const struct scarlett2_device_info *info = private->info;
3808 const int (*port_count)[SCARLETT2_PORT_DIRNS] = info->port_count;
3810 port_count[SCARLETT2_PORT_TYPE_ANALOGUE][SCARLETT2_PORT_OUT];
3813 /* if line_out_hw_vol is 0, there are no controls to update */
3814 if (!info->line_out_hw_vol)
3817 private->vol_updated = 1;
3819 snd_ctl_notify(mixer->chip->card, SNDRV_CTL_EVENT_MASK_VALUE,
3820 &private->master_vol_ctl->id);
3822 for (i = 0; i < num_line_out; i++)
3823 if (private->vol_sw_hw_switch[line_out_remap(private, i)])
3824 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE,
3825 &private->vol_ctls[i]->id);
3828 /* Notify on dim/mute change */
3829 static void scarlett2_notify_dim_mute(
3830 struct usb_mixer_interface *mixer)
3832 struct snd_card *card = mixer->chip->card;
3833 struct scarlett2_data *private = mixer->private_data;
3834 const struct scarlett2_device_info *info = private->info;
3835 const int (*port_count)[SCARLETT2_PORT_DIRNS] = info->port_count;
3837 port_count[SCARLETT2_PORT_TYPE_ANALOGUE][SCARLETT2_PORT_OUT];
3840 private->vol_updated = 1;
3842 if (!info->line_out_hw_vol)
3845 for (i = 0; i < SCARLETT2_DIM_MUTE_COUNT; i++)
3846 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE,
3847 &private->dim_mute_ctls[i]->id);
3849 for (i = 0; i < num_line_out; i++)
3850 if (private->vol_sw_hw_switch[line_out_remap(private, i)])
3851 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE,
3852 &private->mute_ctls[i]->id);
3855 /* Notify on "input other" change (level/pad/air) */
3856 static void scarlett2_notify_input_other(
3857 struct usb_mixer_interface *mixer)
3859 struct snd_card *card = mixer->chip->card;
3860 struct scarlett2_data *private = mixer->private_data;
3861 const struct scarlett2_device_info *info = private->info;
3864 private->input_other_updated = 1;
3866 for (i = 0; i < info->level_input_count; i++)
3867 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE,
3868 &private->level_ctls[i]->id);
3869 for (i = 0; i < info->pad_input_count; i++)
3870 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE,
3871 &private->pad_ctls[i]->id);
3872 for (i = 0; i < info->air_input_count; i++)
3873 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE,
3874 &private->air_ctls[i]->id);
3875 for (i = 0; i < info->phantom_count; i++)
3876 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE,
3877 &private->phantom_ctls[i]->id);
3880 /* Notify on "monitor other" change (direct monitor, speaker
3881 * switching, talkback)
3883 static void scarlett2_notify_monitor_other(
3884 struct usb_mixer_interface *mixer)
3886 struct snd_card *card = mixer->chip->card;
3887 struct scarlett2_data *private = mixer->private_data;
3888 const struct scarlett2_device_info *info = private->info;
3890 private->monitor_other_updated = 1;
3892 if (info->direct_monitor) {
3893 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE,
3894 &private->direct_monitor_ctl->id);
3898 if (info->has_speaker_switching)
3899 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE,
3900 &private->speaker_switching_ctl->id);
3902 if (info->has_talkback)
3903 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE,
3904 &private->talkback_ctl->id);
3906 /* if speaker switching was recently enabled or disabled,
3907 * invalidate the dim/mute and mux enum controls
3909 if (private->speaker_switching_switched) {
3912 scarlett2_notify_dim_mute(mixer);
3914 private->speaker_switching_switched = 0;
3915 private->mux_updated = 1;
3917 for (i = 0; i < private->num_mux_dsts; i++)
3918 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE,
3919 &private->mux_ctls[i]->id);
3923 /* Interrupt callback */
3924 static void scarlett2_notify(struct urb *urb)
3926 struct usb_mixer_interface *mixer = urb->context;
3927 int len = urb->actual_length;
3928 int ustatus = urb->status;
3931 if (ustatus != 0 || len != 8)
3934 data = le32_to_cpu(*(__le32 *)urb->transfer_buffer);
3935 if (data & SCARLETT2_USB_NOTIFY_SYNC)
3936 scarlett2_notify_sync(mixer);
3937 if (data & SCARLETT2_USB_NOTIFY_MONITOR)
3938 scarlett2_notify_monitor(mixer);
3939 if (data & SCARLETT2_USB_NOTIFY_DIM_MUTE)
3940 scarlett2_notify_dim_mute(mixer);
3941 if (data & SCARLETT2_USB_NOTIFY_INPUT_OTHER)
3942 scarlett2_notify_input_other(mixer);
3943 if (data & SCARLETT2_USB_NOTIFY_MONITOR_OTHER)
3944 scarlett2_notify_monitor_other(mixer);
3947 if (ustatus != -ENOENT &&
3948 ustatus != -ECONNRESET &&
3949 ustatus != -ESHUTDOWN) {
3950 urb->dev = mixer->chip->dev;
3951 usb_submit_urb(urb, GFP_ATOMIC);
3955 static int scarlett2_init_notify(struct usb_mixer_interface *mixer)
3957 struct usb_device *dev = mixer->chip->dev;
3958 struct scarlett2_data *private = mixer->private_data;
3959 unsigned int pipe = usb_rcvintpipe(dev, private->bEndpointAddress);
3960 void *transfer_buffer;
3963 usb_audio_err(mixer->chip,
3964 "%s: mixer urb already in use!\n", __func__);
3968 if (usb_pipe_type_check(dev, pipe))
3971 mixer->urb = usb_alloc_urb(0, GFP_KERNEL);
3975 transfer_buffer = kmalloc(private->wMaxPacketSize, GFP_KERNEL);
3976 if (!transfer_buffer)
3979 usb_fill_int_urb(mixer->urb, dev, pipe,
3980 transfer_buffer, private->wMaxPacketSize,
3981 scarlett2_notify, mixer, private->bInterval);
3983 return usb_submit_urb(mixer->urb, GFP_KERNEL);
3986 static int snd_scarlett_gen2_controls_create(struct usb_mixer_interface *mixer)
3988 const struct scarlett2_device_info **info = scarlett2_devices;
3991 /* Find device in scarlett2_devices */
3992 while (*info && (*info)->usb_id != mixer->chip->usb_id)
3997 /* Initialise private data */
3998 err = scarlett2_init_private(mixer, *info);
4002 /* Send proprietary USB initialisation sequence */
4003 err = scarlett2_usb_init(mixer);
4007 /* Read volume levels and controls from the interface */
4008 err = scarlett2_read_configs(mixer);
4012 /* Create the MSD control */
4013 err = scarlett2_add_msd_ctl(mixer);
4017 /* If MSD mode is enabled, don't create any other controls */
4018 if (((struct scarlett2_data *)mixer->private_data)->msd_switch)
4021 /* Create the analogue output controls */
4022 err = scarlett2_add_line_out_ctls(mixer);
4026 /* Create the analogue input controls */
4027 err = scarlett2_add_line_in_ctls(mixer);
4031 /* Create the input, output, and mixer mux input selections */
4032 err = scarlett2_add_mux_enums(mixer);
4036 /* Create the matrix mixer controls */
4037 err = scarlett2_add_mixer_ctls(mixer);
4041 /* Create the level meter controls */
4042 err = scarlett2_add_meter_ctl(mixer);
4046 /* Create the sync control */
4047 err = scarlett2_add_sync_ctl(mixer);
4051 /* Create the direct monitor control */
4052 err = scarlett2_add_direct_monitor_ctl(mixer);
4056 /* Create the speaker switching control */
4057 err = scarlett2_add_speaker_switch_ctl(mixer);
4061 /* Create the talkback controls */
4062 err = scarlett2_add_talkback_ctls(mixer);
4066 /* Create the standalone control */
4067 err = scarlett2_add_standalone_ctl(mixer);
4071 /* Set up the interrupt polling */
4072 err = scarlett2_init_notify(mixer);
4079 int snd_scarlett_gen2_init(struct usb_mixer_interface *mixer)
4081 struct snd_usb_audio *chip = mixer->chip;
4084 /* only use UAC_VERSION_2 */
4085 if (!mixer->protocol)
4088 if (!(chip->setup & SCARLETT2_ENABLE)) {
4089 usb_audio_info(chip,
4090 "Focusrite Scarlett Gen 2/3 Mixer Driver disabled; "
4091 "use options snd_usb_audio vid=0x%04x pid=0x%04x "
4092 "device_setup=1 to enable and report any issues "
4094 USB_ID_VENDOR(chip->usb_id),
4095 USB_ID_PRODUCT(chip->usb_id));
4099 usb_audio_info(chip,
4100 "Focusrite Scarlett Gen 2/3 Mixer Driver enabled pid=0x%04x",
4101 USB_ID_PRODUCT(chip->usb_id));
4103 err = snd_scarlett_gen2_controls_create(mixer);
4105 usb_audio_err(mixer->chip,
4106 "Error initialising Scarlett Mixer Driver: %d",