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-2021 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
65 * /--------------\ 18chn 20chn /--------------\
66 * | Hardware in +--+------\ /-------------+--+ ALSA PCM out |
67 * \--------------/ | | | | \--------------/
71 * | +---------------+ | |
72 * | \ Matrix Mux / | |
79 * | +------------+ | |
83 * | | 18x10 Gain | | |
85 * | +-----+------+ | |
87 * |18chn |10chn | |20chn
92 * ===========================
93 * +---------------+ +--—------------+
94 * \ Output Mux / \ Capture Mux /
95 * +---+---+---+ +-----+-----+
99 * /--------------\ | | | /--------------\
100 * | S/PDIF, ADAT |<--/ |10chn \-->| ALSA PCM in |
101 * | Hardware out | | \--------------/
104 * +-------------+ Software gain per channel.
105 * | Master Gain |<-- 18i20 only: Switch per channel
106 * +------+------+ to select HW or SW gain control.
110 * | Analogue |<------/
115 * Gen 3 devices have a Mass Storage Device (MSD) mode where a small
116 * disk with registration and driver download information is presented
117 * to the host. To access the full functionality of the device without
118 * proprietary software, MSD mode can be disabled by:
119 * - holding down the 48V button for five seconds while powering on
121 * - using this driver and alsamixer to change the "MSD Mode" setting
122 * to Off and power-cycling the device
125 #include <linux/slab.h>
126 #include <linux/usb.h>
127 #include <linux/moduleparam.h>
129 #include <sound/control.h>
130 #include <sound/tlv.h>
132 #include "usbaudio.h"
136 #include "mixer_scarlett_gen2.h"
138 /* device_setup value to enable */
139 #define SCARLETT2_ENABLE 0x01
141 /* device_setup value to allow turning MSD mode back on */
142 #define SCARLETT2_MSD_ENABLE 0x02
144 /* some gui mixers can't handle negative ctl values */
145 #define SCARLETT2_VOLUME_BIAS 127
147 /* mixer range from -80dB to +6dB in 0.5dB steps */
148 #define SCARLETT2_MIXER_MIN_DB -80
149 #define SCARLETT2_MIXER_BIAS (-SCARLETT2_MIXER_MIN_DB * 2)
150 #define SCARLETT2_MIXER_MAX_DB 6
151 #define SCARLETT2_MIXER_MAX_VALUE \
152 ((SCARLETT2_MIXER_MAX_DB - SCARLETT2_MIXER_MIN_DB) * 2)
153 #define SCARLETT2_MIXER_VALUE_COUNT (SCARLETT2_MIXER_MAX_VALUE + 1)
155 /* map from (dB + 80) * 2 to mixer value
156 * for dB in 0 .. 172: int(8192 * pow(10, ((dB - 160) / 2 / 20)))
158 static const u16 scarlett2_mixer_values[SCARLETT2_MIXER_VALUE_COUNT] = {
159 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2,
160 2, 2, 3, 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 6, 6, 6, 7, 7, 8, 8,
161 9, 9, 10, 10, 11, 12, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21,
162 23, 24, 25, 27, 29, 30, 32, 34, 36, 38, 41, 43, 46, 48, 51,
163 54, 57, 61, 65, 68, 73, 77, 81, 86, 91, 97, 103, 109, 115,
164 122, 129, 137, 145, 154, 163, 173, 183, 194, 205, 217, 230,
165 244, 259, 274, 290, 307, 326, 345, 365, 387, 410, 434, 460,
166 487, 516, 547, 579, 614, 650, 689, 730, 773, 819, 867, 919,
167 973, 1031, 1092, 1157, 1225, 1298, 1375, 1456, 1543, 1634,
168 1731, 1833, 1942, 2057, 2179, 2308, 2445, 2590, 2744, 2906,
169 3078, 3261, 3454, 3659, 3876, 4105, 4349, 4606, 4879, 5168,
170 5475, 5799, 6143, 6507, 6892, 7301, 7733, 8192, 8677, 9191,
171 9736, 10313, 10924, 11571, 12257, 12983, 13752, 14567, 15430,
175 /* Maximum number of analogue outputs */
176 #define SCARLETT2_ANALOGUE_MAX 10
178 /* Maximum number of level and pad switches */
179 #define SCARLETT2_LEVEL_SWITCH_MAX 2
180 #define SCARLETT2_PAD_SWITCH_MAX 8
181 #define SCARLETT2_AIR_SWITCH_MAX 8
182 #define SCARLETT2_PHANTOM_SWITCH_MAX 2
184 /* Maximum number of inputs to the mixer */
185 #define SCARLETT2_INPUT_MIX_MAX 25
187 /* Maximum number of outputs from the mixer */
188 #define SCARLETT2_OUTPUT_MIX_MAX 12
190 /* Maximum size of the data in the USB mux assignment message:
191 * 20 inputs, 20 outputs, 25 matrix inputs, 12 spare
193 #define SCARLETT2_MUX_MAX 77
195 /* Maximum number of meters (sum of output port counts) */
196 #define SCARLETT2_MAX_METERS 65
198 /* Hardware port types:
199 * - None (no input to mux)
207 SCARLETT2_PORT_TYPE_NONE = 0,
208 SCARLETT2_PORT_TYPE_ANALOGUE = 1,
209 SCARLETT2_PORT_TYPE_SPDIF = 2,
210 SCARLETT2_PORT_TYPE_ADAT = 3,
211 SCARLETT2_PORT_TYPE_MIX = 4,
212 SCARLETT2_PORT_TYPE_PCM = 5,
213 SCARLETT2_PORT_TYPE_COUNT = 6,
216 /* I/O count of each port type kept in struct scarlett2_ports */
218 SCARLETT2_PORT_IN = 0,
219 SCARLETT2_PORT_OUT = 1,
220 SCARLETT2_PORT_DIRNS = 2,
223 /* Dim/Mute buttons on the 18i20 */
225 SCARLETT2_BUTTON_MUTE = 0,
226 SCARLETT2_BUTTON_DIM = 1,
227 SCARLETT2_DIM_MUTE_COUNT = 2,
230 static const char *const scarlett2_dim_mute_names[SCARLETT2_DIM_MUTE_COUNT] = {
234 /* Description of each hardware port type:
235 * - id: hardware ID of this port type
236 * - src_descr: printf format string for mux input selections
237 * - src_num_offset: added to channel number for the fprintf
238 * - dst_descr: printf format string for mixer controls
240 struct scarlett2_port {
242 const char * const src_descr;
244 const char * const dst_descr;
247 static const struct scarlett2_port scarlett2_ports[SCARLETT2_PORT_TYPE_COUNT] = {
248 [SCARLETT2_PORT_TYPE_NONE] = {
252 [SCARLETT2_PORT_TYPE_ANALOGUE] = {
254 .src_descr = "Analogue %d",
256 .dst_descr = "Analogue Output %02d Playback"
258 [SCARLETT2_PORT_TYPE_SPDIF] = {
260 .src_descr = "S/PDIF %d",
262 .dst_descr = "S/PDIF Output %d Playback"
264 [SCARLETT2_PORT_TYPE_ADAT] = {
266 .src_descr = "ADAT %d",
268 .dst_descr = "ADAT Output %d Playback"
270 [SCARLETT2_PORT_TYPE_MIX] = {
272 .src_descr = "Mix %c",
273 .src_num_offset = 'A',
274 .dst_descr = "Mixer Input %02d Capture"
276 [SCARLETT2_PORT_TYPE_PCM] = {
278 .src_descr = "PCM %d",
280 .dst_descr = "PCM %02d Capture"
284 /* Number of mux tables: one for each band of sample rates
285 * (44.1/48kHz, 88.2/96kHz, and 176.4/176kHz)
287 #define SCARLETT2_MUX_TABLES 3
289 /* Maximum number of entries in a mux table */
290 #define SCARLETT2_MAX_MUX_ENTRIES 10
292 /* One entry within mux_assignment defines the port type and range of
293 * ports to add to the set_mux message. The end of the list is marked
296 struct scarlett2_mux_entry {
302 struct scarlett2_device_info {
303 u32 usb_id; /* USB device identifier */
305 /* Gen 3 devices have an internal MSD mode switch that needs
306 * to be disabled in order to access the full functionality of
311 /* Gen 3 devices without a mixer have a different
316 /* line out hw volume is sw controlled */
319 /* support for main/alt speaker switching */
320 u8 has_speaker_switching;
322 /* support for talkback microphone */
325 /* the number of analogue inputs with a software switchable
326 * level control that can be set to line or instrument
328 u8 level_input_count;
330 /* the first input with a level control (0-based) */
331 u8 level_input_first;
333 /* the number of analogue inputs with a software switchable
338 /* the number of analogue inputs with a software switchable
343 /* the number of phantom (48V) software switchable controls */
346 /* the number of inputs each phantom switch controls */
347 u8 inputs_per_phantom;
349 /* the number of direct monitor options
350 * (0 = none, 1 = mono only, 2 = mono/stereo)
354 /* remap analogue outputs; 18i8 Gen 3 has "line 3/4" connected
355 * internally to the analogue 7/8 outputs
357 u8 line_out_remap_enable;
358 u8 line_out_remap[SCARLETT2_ANALOGUE_MAX];
360 /* additional description for the line out volume controls */
361 const char * const line_out_descrs[SCARLETT2_ANALOGUE_MAX];
363 /* number of sources/destinations of each port type */
364 const int port_count[SCARLETT2_PORT_TYPE_COUNT][SCARLETT2_PORT_DIRNS];
366 /* layout/order of the entries in the set_mux message */
367 struct scarlett2_mux_entry mux_assignment[SCARLETT2_MUX_TABLES]
368 [SCARLETT2_MAX_MUX_ENTRIES];
371 struct scarlett2_data {
372 struct usb_mixer_interface *mixer;
373 struct mutex usb_mutex; /* prevent sending concurrent USB requests */
374 struct mutex data_mutex; /* lock access to this data */
375 struct delayed_work work;
376 const struct scarlett2_device_info *info;
377 __u8 bInterfaceNumber;
378 __u8 bEndpointAddress;
379 __u16 wMaxPacketSize;
386 u8 input_other_updated;
387 u8 monitor_other_updated;
389 u8 speaker_switching_switched;
392 u8 vol[SCARLETT2_ANALOGUE_MAX];
393 u8 vol_sw_hw_switch[SCARLETT2_ANALOGUE_MAX];
394 u8 mute_switch[SCARLETT2_ANALOGUE_MAX];
395 u8 level_switch[SCARLETT2_LEVEL_SWITCH_MAX];
396 u8 pad_switch[SCARLETT2_PAD_SWITCH_MAX];
397 u8 dim_mute[SCARLETT2_DIM_MUTE_COUNT];
398 u8 air_switch[SCARLETT2_AIR_SWITCH_MAX];
399 u8 phantom_switch[SCARLETT2_PHANTOM_SWITCH_MAX];
400 u8 phantom_persistence;
401 u8 direct_monitor_switch;
402 u8 speaker_switching_switch;
404 u8 talkback_map[SCARLETT2_OUTPUT_MIX_MAX];
406 struct snd_kcontrol *sync_ctl;
407 struct snd_kcontrol *master_vol_ctl;
408 struct snd_kcontrol *vol_ctls[SCARLETT2_ANALOGUE_MAX];
409 struct snd_kcontrol *sw_hw_ctls[SCARLETT2_ANALOGUE_MAX];
410 struct snd_kcontrol *mute_ctls[SCARLETT2_ANALOGUE_MAX];
411 struct snd_kcontrol *dim_mute_ctls[SCARLETT2_DIM_MUTE_COUNT];
412 struct snd_kcontrol *level_ctls[SCARLETT2_LEVEL_SWITCH_MAX];
413 struct snd_kcontrol *pad_ctls[SCARLETT2_PAD_SWITCH_MAX];
414 struct snd_kcontrol *air_ctls[SCARLETT2_AIR_SWITCH_MAX];
415 struct snd_kcontrol *phantom_ctls[SCARLETT2_PHANTOM_SWITCH_MAX];
416 struct snd_kcontrol *mux_ctls[SCARLETT2_MUX_MAX];
417 struct snd_kcontrol *direct_monitor_ctl;
418 struct snd_kcontrol *speaker_switching_ctl;
419 struct snd_kcontrol *talkback_ctl;
420 u8 mux[SCARLETT2_MUX_MAX];
421 u8 mix[SCARLETT2_INPUT_MIX_MAX * SCARLETT2_OUTPUT_MIX_MAX];
424 /*** Model-specific data ***/
426 static const struct scarlett2_device_info s6i6_gen2_info = {
427 .usb_id = USB_ID(0x1235, 0x8203),
430 .level_input_count = 2,
431 .pad_input_count = 2,
441 [SCARLETT2_PORT_TYPE_NONE] = { 1, 0 },
442 [SCARLETT2_PORT_TYPE_ANALOGUE] = { 4, 4 },
443 [SCARLETT2_PORT_TYPE_SPDIF] = { 2, 2 },
444 [SCARLETT2_PORT_TYPE_MIX] = { 10, 18 },
445 [SCARLETT2_PORT_TYPE_PCM] = { 6, 6 },
448 .mux_assignment = { {
449 { SCARLETT2_PORT_TYPE_PCM, 0, 6 },
450 { SCARLETT2_PORT_TYPE_ANALOGUE, 0, 4 },
451 { SCARLETT2_PORT_TYPE_SPDIF, 0, 2 },
452 { SCARLETT2_PORT_TYPE_MIX, 0, 18 },
453 { SCARLETT2_PORT_TYPE_NONE, 0, 8 },
456 { SCARLETT2_PORT_TYPE_PCM, 0, 6 },
457 { SCARLETT2_PORT_TYPE_ANALOGUE, 0, 4 },
458 { SCARLETT2_PORT_TYPE_SPDIF, 0, 2 },
459 { SCARLETT2_PORT_TYPE_MIX, 0, 18 },
460 { SCARLETT2_PORT_TYPE_NONE, 0, 8 },
463 { SCARLETT2_PORT_TYPE_PCM, 0, 6 },
464 { SCARLETT2_PORT_TYPE_ANALOGUE, 0, 4 },
465 { SCARLETT2_PORT_TYPE_SPDIF, 0, 2 },
466 { SCARLETT2_PORT_TYPE_MIX, 0, 18 },
467 { SCARLETT2_PORT_TYPE_NONE, 0, 8 },
472 static const struct scarlett2_device_info s18i8_gen2_info = {
473 .usb_id = USB_ID(0x1235, 0x8204),
476 .level_input_count = 2,
477 .pad_input_count = 4,
489 [SCARLETT2_PORT_TYPE_NONE] = { 1, 0 },
490 [SCARLETT2_PORT_TYPE_ANALOGUE] = { 8, 6 },
491 [SCARLETT2_PORT_TYPE_SPDIF] = { 2, 2 },
492 [SCARLETT2_PORT_TYPE_ADAT] = { 8, 0 },
493 [SCARLETT2_PORT_TYPE_MIX] = { 10, 18 },
494 [SCARLETT2_PORT_TYPE_PCM] = { 8, 18 },
497 .mux_assignment = { {
498 { SCARLETT2_PORT_TYPE_PCM, 0, 18 },
499 { SCARLETT2_PORT_TYPE_ANALOGUE, 0, 6 },
500 { SCARLETT2_PORT_TYPE_SPDIF, 0, 2 },
501 { SCARLETT2_PORT_TYPE_MIX, 0, 18 },
502 { SCARLETT2_PORT_TYPE_NONE, 0, 8 },
505 { SCARLETT2_PORT_TYPE_PCM, 0, 14 },
506 { SCARLETT2_PORT_TYPE_ANALOGUE, 0, 6 },
507 { SCARLETT2_PORT_TYPE_SPDIF, 0, 2 },
508 { SCARLETT2_PORT_TYPE_MIX, 0, 18 },
509 { SCARLETT2_PORT_TYPE_NONE, 0, 8 },
512 { SCARLETT2_PORT_TYPE_PCM, 0, 10 },
513 { SCARLETT2_PORT_TYPE_ANALOGUE, 0, 6 },
514 { SCARLETT2_PORT_TYPE_SPDIF, 0, 2 },
515 { SCARLETT2_PORT_TYPE_MIX, 0, 18 },
516 { SCARLETT2_PORT_TYPE_NONE, 0, 4 },
521 static const struct scarlett2_device_info s18i20_gen2_info = {
522 .usb_id = USB_ID(0x1235, 0x8201),
525 .line_out_hw_vol = 1,
541 [SCARLETT2_PORT_TYPE_NONE] = { 1, 0 },
542 [SCARLETT2_PORT_TYPE_ANALOGUE] = { 8, 10 },
543 [SCARLETT2_PORT_TYPE_SPDIF] = { 2, 2 },
544 [SCARLETT2_PORT_TYPE_ADAT] = { 8, 8 },
545 [SCARLETT2_PORT_TYPE_MIX] = { 10, 18 },
546 [SCARLETT2_PORT_TYPE_PCM] = { 20, 18 },
549 .mux_assignment = { {
550 { SCARLETT2_PORT_TYPE_PCM, 0, 18 },
551 { SCARLETT2_PORT_TYPE_ANALOGUE, 0, 10 },
552 { SCARLETT2_PORT_TYPE_SPDIF, 0, 2 },
553 { SCARLETT2_PORT_TYPE_ADAT, 0, 8 },
554 { SCARLETT2_PORT_TYPE_MIX, 0, 18 },
555 { SCARLETT2_PORT_TYPE_NONE, 0, 8 },
558 { SCARLETT2_PORT_TYPE_PCM, 0, 14 },
559 { SCARLETT2_PORT_TYPE_ANALOGUE, 0, 10 },
560 { SCARLETT2_PORT_TYPE_SPDIF, 0, 2 },
561 { SCARLETT2_PORT_TYPE_ADAT, 0, 4 },
562 { SCARLETT2_PORT_TYPE_MIX, 0, 18 },
563 { SCARLETT2_PORT_TYPE_NONE, 0, 8 },
566 { SCARLETT2_PORT_TYPE_PCM, 0, 10 },
567 { SCARLETT2_PORT_TYPE_ANALOGUE, 0, 10 },
568 { SCARLETT2_PORT_TYPE_SPDIF, 0, 2 },
569 { SCARLETT2_PORT_TYPE_MIX, 0, 18 },
570 { SCARLETT2_PORT_TYPE_NONE, 0, 6 },
575 static const struct scarlett2_device_info solo_gen3_info = {
576 .usb_id = USB_ID(0x1235, 0x8211),
579 .level_input_count = 1,
580 .level_input_first = 1,
581 .air_input_count = 1,
583 .inputs_per_phantom = 1,
587 static const struct scarlett2_device_info s2i2_gen3_info = {
588 .usb_id = USB_ID(0x1235, 0x8210),
591 .level_input_count = 2,
592 .air_input_count = 2,
594 .inputs_per_phantom = 2,
598 static const struct scarlett2_device_info s4i4_gen3_info = {
599 .usb_id = USB_ID(0x1235, 0x8212),
603 .level_input_count = 2,
604 .pad_input_count = 2,
605 .air_input_count = 2,
607 .inputs_per_phantom = 2,
617 [SCARLETT2_PORT_TYPE_NONE] = { 1, 0 },
618 [SCARLETT2_PORT_TYPE_ANALOGUE] = { 4, 4 },
619 [SCARLETT2_PORT_TYPE_MIX] = { 6, 8 },
620 [SCARLETT2_PORT_TYPE_PCM] = { 4, 6 },
623 .mux_assignment = { {
624 { SCARLETT2_PORT_TYPE_PCM, 0, 6 },
625 { SCARLETT2_PORT_TYPE_ANALOGUE, 0, 4 },
626 { SCARLETT2_PORT_TYPE_MIX, 0, 8 },
627 { SCARLETT2_PORT_TYPE_NONE, 0, 16 },
630 { SCARLETT2_PORT_TYPE_PCM, 0, 6 },
631 { SCARLETT2_PORT_TYPE_ANALOGUE, 0, 4 },
632 { SCARLETT2_PORT_TYPE_MIX, 0, 8 },
633 { SCARLETT2_PORT_TYPE_NONE, 0, 16 },
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 },
644 static const struct scarlett2_device_info s8i6_gen3_info = {
645 .usb_id = USB_ID(0x1235, 0x8213),
649 .level_input_count = 2,
650 .pad_input_count = 2,
651 .air_input_count = 2,
653 .inputs_per_phantom = 2,
663 [SCARLETT2_PORT_TYPE_NONE] = { 1, 0 },
664 [SCARLETT2_PORT_TYPE_ANALOGUE] = { 6, 4 },
665 [SCARLETT2_PORT_TYPE_SPDIF] = { 2, 2 },
666 [SCARLETT2_PORT_TYPE_MIX] = { 8, 8 },
667 [SCARLETT2_PORT_TYPE_PCM] = { 6, 10 },
670 .mux_assignment = { {
671 { SCARLETT2_PORT_TYPE_PCM, 0, 8 },
672 { SCARLETT2_PORT_TYPE_ANALOGUE, 0, 4 },
673 { SCARLETT2_PORT_TYPE_SPDIF, 0, 2 },
674 { SCARLETT2_PORT_TYPE_PCM, 8, 2 },
675 { SCARLETT2_PORT_TYPE_MIX, 0, 8 },
676 { SCARLETT2_PORT_TYPE_NONE, 0, 18 },
679 { SCARLETT2_PORT_TYPE_PCM, 0, 8 },
680 { SCARLETT2_PORT_TYPE_ANALOGUE, 0, 4 },
681 { SCARLETT2_PORT_TYPE_SPDIF, 0, 2 },
682 { SCARLETT2_PORT_TYPE_PCM, 8, 2 },
683 { SCARLETT2_PORT_TYPE_MIX, 0, 8 },
684 { SCARLETT2_PORT_TYPE_NONE, 0, 18 },
687 { SCARLETT2_PORT_TYPE_PCM, 0, 8 },
688 { SCARLETT2_PORT_TYPE_ANALOGUE, 0, 4 },
689 { SCARLETT2_PORT_TYPE_SPDIF, 0, 2 },
690 { SCARLETT2_PORT_TYPE_PCM, 8, 2 },
691 { SCARLETT2_PORT_TYPE_MIX, 0, 8 },
692 { SCARLETT2_PORT_TYPE_NONE, 0, 18 },
697 static const struct scarlett2_device_info s18i8_gen3_info = {
698 .usb_id = USB_ID(0x1235, 0x8214),
702 .line_out_hw_vol = 1,
703 .has_speaker_switching = 1,
704 .level_input_count = 2,
705 .pad_input_count = 4,
706 .air_input_count = 4,
708 .inputs_per_phantom = 2,
710 .line_out_remap_enable = 1,
711 .line_out_remap = { 0, 1, 6, 7, 2, 3, 4, 5 },
725 [SCARLETT2_PORT_TYPE_NONE] = { 1, 0 },
726 [SCARLETT2_PORT_TYPE_ANALOGUE] = { 8, 8 },
727 [SCARLETT2_PORT_TYPE_SPDIF] = { 2, 2 },
728 [SCARLETT2_PORT_TYPE_ADAT] = { 8, 0 },
729 [SCARLETT2_PORT_TYPE_MIX] = { 10, 20 },
730 [SCARLETT2_PORT_TYPE_PCM] = { 8, 20 },
733 .mux_assignment = { {
734 { SCARLETT2_PORT_TYPE_PCM, 0, 10 },
735 { SCARLETT2_PORT_TYPE_PCM, 12, 8 },
736 { SCARLETT2_PORT_TYPE_ANALOGUE, 0, 2 },
737 { SCARLETT2_PORT_TYPE_ANALOGUE, 6, 2 },
738 { SCARLETT2_PORT_TYPE_ANALOGUE, 2, 4 },
739 { SCARLETT2_PORT_TYPE_SPDIF, 0, 2 },
740 { SCARLETT2_PORT_TYPE_PCM, 10, 2 },
741 { SCARLETT2_PORT_TYPE_MIX, 0, 20 },
742 { SCARLETT2_PORT_TYPE_NONE, 0, 10 },
745 { SCARLETT2_PORT_TYPE_PCM, 0, 10 },
746 { SCARLETT2_PORT_TYPE_PCM, 12, 4 },
747 { SCARLETT2_PORT_TYPE_ANALOGUE, 0, 2 },
748 { SCARLETT2_PORT_TYPE_ANALOGUE, 6, 2 },
749 { SCARLETT2_PORT_TYPE_ANALOGUE, 2, 4 },
750 { SCARLETT2_PORT_TYPE_SPDIF, 0, 2 },
751 { SCARLETT2_PORT_TYPE_PCM, 10, 2 },
752 { SCARLETT2_PORT_TYPE_MIX, 0, 20 },
753 { SCARLETT2_PORT_TYPE_NONE, 0, 10 },
756 { SCARLETT2_PORT_TYPE_PCM, 0, 10 },
757 { SCARLETT2_PORT_TYPE_ANALOGUE, 0, 2 },
758 { SCARLETT2_PORT_TYPE_ANALOGUE, 6, 2 },
759 { SCARLETT2_PORT_TYPE_ANALOGUE, 2, 4 },
760 { SCARLETT2_PORT_TYPE_SPDIF, 0, 2 },
761 { SCARLETT2_PORT_TYPE_MIX, 0, 20 },
762 { SCARLETT2_PORT_TYPE_NONE, 0, 10 },
767 static const struct scarlett2_device_info s18i20_gen3_info = {
768 .usb_id = USB_ID(0x1235, 0x8215),
772 .line_out_hw_vol = 1,
773 .has_speaker_switching = 1,
775 .level_input_count = 2,
776 .pad_input_count = 8,
777 .air_input_count = 8,
779 .inputs_per_phantom = 4,
795 [SCARLETT2_PORT_TYPE_NONE] = { 1, 0 },
796 [SCARLETT2_PORT_TYPE_ANALOGUE] = { 9, 10 },
797 [SCARLETT2_PORT_TYPE_SPDIF] = { 2, 2 },
798 [SCARLETT2_PORT_TYPE_ADAT] = { 8, 8 },
799 [SCARLETT2_PORT_TYPE_MIX] = { 12, 25 },
800 [SCARLETT2_PORT_TYPE_PCM] = { 20, 20 },
803 .mux_assignment = { {
804 { SCARLETT2_PORT_TYPE_PCM, 0, 8 },
805 { SCARLETT2_PORT_TYPE_PCM, 10, 10 },
806 { SCARLETT2_PORT_TYPE_ANALOGUE, 0, 10 },
807 { SCARLETT2_PORT_TYPE_SPDIF, 0, 2 },
808 { SCARLETT2_PORT_TYPE_ADAT, 0, 8 },
809 { SCARLETT2_PORT_TYPE_PCM, 8, 2 },
810 { SCARLETT2_PORT_TYPE_MIX, 0, 25 },
811 { SCARLETT2_PORT_TYPE_NONE, 0, 12 },
814 { SCARLETT2_PORT_TYPE_PCM, 0, 8 },
815 { SCARLETT2_PORT_TYPE_PCM, 10, 8 },
816 { SCARLETT2_PORT_TYPE_ANALOGUE, 0, 10 },
817 { SCARLETT2_PORT_TYPE_SPDIF, 0, 2 },
818 { SCARLETT2_PORT_TYPE_ADAT, 0, 8 },
819 { SCARLETT2_PORT_TYPE_PCM, 8, 2 },
820 { SCARLETT2_PORT_TYPE_MIX, 0, 25 },
821 { SCARLETT2_PORT_TYPE_NONE, 0, 10 },
824 { SCARLETT2_PORT_TYPE_PCM, 0, 10 },
825 { SCARLETT2_PORT_TYPE_ANALOGUE, 0, 10 },
826 { SCARLETT2_PORT_TYPE_SPDIF, 0, 2 },
827 { SCARLETT2_PORT_TYPE_NONE, 0, 24 },
832 static const struct scarlett2_device_info *scarlett2_devices[] = {
833 /* Supported Gen 2 devices */
838 /* Supported Gen 3 devices */
850 /* get the starting port index number for a given port type/direction */
851 static int scarlett2_get_port_start_num(
852 const int port_count[][SCARLETT2_PORT_DIRNS],
853 int direction, int port_type)
857 for (i = 0; i < port_type; i++)
858 num += port_count[i][direction];
863 /*** USB Interactions ***/
865 /* Notifications from the interface */
866 #define SCARLETT2_USB_NOTIFY_SYNC 0x00000008
867 #define SCARLETT2_USB_NOTIFY_DIM_MUTE 0x00200000
868 #define SCARLETT2_USB_NOTIFY_MONITOR 0x00400000
869 #define SCARLETT2_USB_NOTIFY_INPUT_OTHER 0x00800000
870 #define SCARLETT2_USB_NOTIFY_MONITOR_OTHER 0x01000000
872 /* Commands for sending/receiving requests/responses */
873 #define SCARLETT2_USB_CMD_INIT 0
874 #define SCARLETT2_USB_CMD_REQ 2
875 #define SCARLETT2_USB_CMD_RESP 3
877 #define SCARLETT2_USB_INIT_1 0x00000000
878 #define SCARLETT2_USB_INIT_2 0x00000002
879 #define SCARLETT2_USB_GET_METER 0x00001001
880 #define SCARLETT2_USB_GET_MIX 0x00002001
881 #define SCARLETT2_USB_SET_MIX 0x00002002
882 #define SCARLETT2_USB_GET_MUX 0x00003001
883 #define SCARLETT2_USB_SET_MUX 0x00003002
884 #define SCARLETT2_USB_GET_SYNC 0x00006004
885 #define SCARLETT2_USB_GET_DATA 0x00800000
886 #define SCARLETT2_USB_SET_DATA 0x00800001
887 #define SCARLETT2_USB_DATA_CMD 0x00800002
889 #define SCARLETT2_USB_CONFIG_SAVE 6
891 #define SCARLETT2_USB_VOLUME_STATUS_OFFSET 0x31
892 #define SCARLETT2_USB_METER_LEVELS_GET_MAGIC 1
894 /* volume status is read together (matches scarlett2_config_items[1]) */
895 struct scarlett2_usb_volume_status {
896 /* dim/mute buttons */
897 u8 dim_mute[SCARLETT2_DIM_MUTE_COUNT];
901 /* software volume setting */
902 s16 sw_vol[SCARLETT2_ANALOGUE_MAX];
904 /* actual volume of output inc. dim (-18dB) */
905 s16 hw_vol[SCARLETT2_ANALOGUE_MAX];
907 /* internal mute buttons */
908 u8 mute_switch[SCARLETT2_ANALOGUE_MAX];
910 /* sw (0) or hw (1) controlled */
911 u8 sw_hw_switch[SCARLETT2_ANALOGUE_MAX];
915 /* front panel volume knob */
919 /* Configuration parameters that can be read and written */
921 SCARLETT2_CONFIG_DIM_MUTE = 0,
922 SCARLETT2_CONFIG_LINE_OUT_VOLUME = 1,
923 SCARLETT2_CONFIG_MUTE_SWITCH = 2,
924 SCARLETT2_CONFIG_SW_HW_SWITCH = 3,
925 SCARLETT2_CONFIG_LEVEL_SWITCH = 4,
926 SCARLETT2_CONFIG_PAD_SWITCH = 5,
927 SCARLETT2_CONFIG_MSD_SWITCH = 6,
928 SCARLETT2_CONFIG_AIR_SWITCH = 7,
929 SCARLETT2_CONFIG_PHANTOM_SWITCH = 8,
930 SCARLETT2_CONFIG_PHANTOM_PERSISTENCE = 9,
931 SCARLETT2_CONFIG_DIRECT_MONITOR = 10,
932 SCARLETT2_CONFIG_MONITOR_OTHER_SWITCH = 11,
933 SCARLETT2_CONFIG_MONITOR_OTHER_ENABLE = 12,
934 SCARLETT2_CONFIG_TALKBACK_MAP = 13,
935 SCARLETT2_CONFIG_COUNT = 14
938 /* Location, size, and activation command number for the configuration
939 * parameters. Size is in bits and may be 1, 8, or 16.
941 struct scarlett2_config {
947 /* scarlett2_config_items[0] is for devices without a mixer
948 * scarlett2_config_items[1] is for devices with a mixer
950 static const struct scarlett2_config
951 scarlett2_config_items[2][SCARLETT2_CONFIG_COUNT] =
953 /* Devices without a mixer (Solo and 2i2 Gen 3) */
955 [SCARLETT2_CONFIG_MSD_SWITCH] = {
956 .offset = 0x04, .size = 8, .activate = 6 },
958 [SCARLETT2_CONFIG_PHANTOM_PERSISTENCE] = {
959 .offset = 0x05, .size = 8, .activate = 6 },
961 [SCARLETT2_CONFIG_PHANTOM_SWITCH] = {
962 .offset = 0x06, .size = 8, .activate = 3 },
964 [SCARLETT2_CONFIG_DIRECT_MONITOR] = {
965 .offset = 0x07, .size = 8, .activate = 4 },
967 [SCARLETT2_CONFIG_LEVEL_SWITCH] = {
968 .offset = 0x08, .size = 1, .activate = 7 },
970 [SCARLETT2_CONFIG_AIR_SWITCH] = {
971 .offset = 0x09, .size = 1, .activate = 8 },
973 /* Devices with a mixer (Gen 2 and all other Gen 3) */
975 [SCARLETT2_CONFIG_DIM_MUTE] = {
976 .offset = 0x31, .size = 8, .activate = 2 },
978 [SCARLETT2_CONFIG_LINE_OUT_VOLUME] = {
979 .offset = 0x34, .size = 16, .activate = 1 },
981 [SCARLETT2_CONFIG_MUTE_SWITCH] = {
982 .offset = 0x5c, .size = 8, .activate = 1 },
984 [SCARLETT2_CONFIG_SW_HW_SWITCH] = {
985 .offset = 0x66, .size = 8, .activate = 3 },
987 [SCARLETT2_CONFIG_LEVEL_SWITCH] = {
988 .offset = 0x7c, .size = 8, .activate = 7 },
990 [SCARLETT2_CONFIG_PAD_SWITCH] = {
991 .offset = 0x84, .size = 8, .activate = 8 },
993 [SCARLETT2_CONFIG_AIR_SWITCH] = {
994 .offset = 0x8c, .size = 8, .activate = 8 },
996 [SCARLETT2_CONFIG_PHANTOM_SWITCH] = {
997 .offset = 0x9c, .size = 1, .activate = 8 },
999 [SCARLETT2_CONFIG_MSD_SWITCH] = {
1000 .offset = 0x9d, .size = 8, .activate = 6 },
1002 [SCARLETT2_CONFIG_PHANTOM_PERSISTENCE] = {
1003 .offset = 0x9e, .size = 8, .activate = 6 },
1005 [SCARLETT2_CONFIG_MONITOR_OTHER_SWITCH] = {
1006 .offset = 0x9f, .size = 1, .activate = 10 },
1008 [SCARLETT2_CONFIG_MONITOR_OTHER_ENABLE] = {
1009 .offset = 0xa0, .size = 1, .activate = 10 },
1011 [SCARLETT2_CONFIG_TALKBACK_MAP] = {
1012 .offset = 0xb0, .size = 16, .activate = 10 },
1015 /* proprietary request/response format */
1016 struct scarlett2_usb_packet {
1025 static void scarlett2_fill_request_header(struct scarlett2_data *private,
1026 struct scarlett2_usb_packet *req,
1027 u32 cmd, u16 req_size)
1029 /* sequence must go up by 1 for each request */
1030 u16 seq = private->scarlett2_seq++;
1032 req->cmd = cpu_to_le32(cmd);
1033 req->size = cpu_to_le16(req_size);
1034 req->seq = cpu_to_le16(seq);
1039 static int scarlett2_usb_tx(struct usb_device *dev, int interface,
1040 void *buf, u16 size)
1042 return snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0),
1043 SCARLETT2_USB_CMD_REQ,
1044 USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_OUT,
1045 0, interface, buf, size);
1048 static int scarlett2_usb_rx(struct usb_device *dev, int interface,
1049 u32 usb_req, void *buf, u16 size)
1051 return snd_usb_ctl_msg(dev, usb_rcvctrlpipe(dev, 0),
1053 USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_IN,
1054 0, interface, buf, size);
1057 /* Send a proprietary format request to the Scarlett interface */
1058 static int scarlett2_usb(
1059 struct usb_mixer_interface *mixer, u32 cmd,
1060 void *req_data, u16 req_size, void *resp_data, u16 resp_size)
1062 struct scarlett2_data *private = mixer->private_data;
1063 struct usb_device *dev = mixer->chip->dev;
1064 u16 req_buf_size = sizeof(struct scarlett2_usb_packet) + req_size;
1065 u16 resp_buf_size = sizeof(struct scarlett2_usb_packet) + resp_size;
1066 struct scarlett2_usb_packet *req, *resp = NULL;
1069 req = kmalloc(req_buf_size, GFP_KERNEL);
1075 resp = kmalloc(resp_buf_size, GFP_KERNEL);
1081 mutex_lock(&private->usb_mutex);
1083 /* build request message and send it */
1085 scarlett2_fill_request_header(private, req, cmd, req_size);
1088 memcpy(req->data, req_data, req_size);
1090 err = scarlett2_usb_tx(dev, private->bInterfaceNumber,
1093 if (err != req_buf_size) {
1096 "Scarlett Gen 2/3 USB request result cmd %x was %d\n",
1102 /* send a second message to get the response */
1104 err = scarlett2_usb_rx(dev, private->bInterfaceNumber,
1105 SCARLETT2_USB_CMD_RESP,
1106 resp, resp_buf_size);
1108 /* validate the response */
1110 if (err != resp_buf_size) {
1113 "Scarlett Gen 2/3 USB response result cmd %x was %d "
1115 cmd, err, resp_buf_size);
1120 /* cmd/seq/size should match except when initialising
1121 * seq sent = 1, response = 0
1123 if (resp->cmd != req->cmd ||
1124 (resp->seq != req->seq &&
1125 (le16_to_cpu(req->seq) != 1 || resp->seq != 0)) ||
1126 resp_size != le16_to_cpu(resp->size) ||
1131 "Scarlett Gen 2/3 USB invalid response; "
1132 "cmd tx/rx %d/%d seq %d/%d size %d/%d "
1133 "error %d pad %d\n",
1134 le32_to_cpu(req->cmd), le32_to_cpu(resp->cmd),
1135 le16_to_cpu(req->seq), le16_to_cpu(resp->seq),
1136 resp_size, le16_to_cpu(resp->size),
1137 le32_to_cpu(resp->error),
1138 le32_to_cpu(resp->pad));
1143 if (resp_data && resp_size > 0)
1144 memcpy(resp_data, resp->data, resp_size);
1147 mutex_unlock(&private->usb_mutex);
1154 /* Send a USB message to get data; result placed in *buf */
1155 static int scarlett2_usb_get(
1156 struct usb_mixer_interface *mixer,
1157 int offset, void *buf, int size)
1164 req.offset = cpu_to_le32(offset);
1165 req.size = cpu_to_le32(size);
1166 return scarlett2_usb(mixer, SCARLETT2_USB_GET_DATA,
1167 &req, sizeof(req), buf, size);
1170 /* Send a USB message to get configuration parameters; result placed in *buf */
1171 static int scarlett2_usb_get_config(
1172 struct usb_mixer_interface *mixer,
1173 int config_item_num, int count, void *buf)
1175 struct scarlett2_data *private = mixer->private_data;
1176 const struct scarlett2_device_info *info = private->info;
1177 const struct scarlett2_config *config_item =
1178 &scarlett2_config_items[info->has_mixer][config_item_num];
1183 /* For byte-sized parameters, retrieve directly into buf */
1184 if (config_item->size >= 8) {
1185 size = config_item->size / 8 * count;
1186 err = scarlett2_usb_get(mixer, config_item->offset, buf, size);
1192 for (i = 0; i < count; i++, buf_16++)
1193 *buf_16 = le16_to_cpu(*(__le16 *)buf_16);
1198 /* For bit-sized parameters, retrieve into value */
1199 err = scarlett2_usb_get(mixer, config_item->offset, &value, 1);
1203 /* then unpack from value into buf[] */
1205 for (i = 0; i < 8 && i < count; i++, value >>= 1)
1206 *buf_8++ = value & 1;
1211 /* Send SCARLETT2_USB_DATA_CMD SCARLETT2_USB_CONFIG_SAVE */
1212 static void scarlett2_config_save(struct usb_mixer_interface *mixer)
1214 __le32 req = cpu_to_le32(SCARLETT2_USB_CONFIG_SAVE);
1216 scarlett2_usb(mixer, SCARLETT2_USB_DATA_CMD,
1221 /* Delayed work to save config */
1222 static void scarlett2_config_save_work(struct work_struct *work)
1224 struct scarlett2_data *private =
1225 container_of(work, struct scarlett2_data, work.work);
1227 scarlett2_config_save(private->mixer);
1230 /* Send a USB message to set a SCARLETT2_CONFIG_* parameter */
1231 static int scarlett2_usb_set_config(
1232 struct usb_mixer_interface *mixer,
1233 int config_item_num, int index, int value)
1235 struct scarlett2_data *private = mixer->private_data;
1236 const struct scarlett2_device_info *info = private->info;
1237 const struct scarlett2_config *config_item =
1238 &scarlett2_config_items[info->has_mixer][config_item_num];
1248 /* Cancel any pending NVRAM save */
1249 cancel_delayed_work_sync(&private->work);
1251 /* Convert config_item->size in bits to size in bytes and
1254 if (config_item->size >= 8) {
1255 size = config_item->size / 8;
1256 offset = config_item->offset + index * size;
1258 /* If updating a bit, retrieve the old value, set/clear the
1259 * bit as needed, and update value
1265 offset = config_item->offset;
1267 scarlett2_usb_get(mixer, offset, &tmp, 1);
1269 tmp |= (1 << index);
1271 tmp &= ~(1 << index);
1276 /* Send the configuration parameter data */
1277 req.offset = cpu_to_le32(offset);
1278 req.bytes = cpu_to_le32(size);
1279 req.value = cpu_to_le32(value);
1280 err = scarlett2_usb(mixer, SCARLETT2_USB_SET_DATA,
1281 &req, sizeof(u32) * 2 + size,
1286 /* Activate the change */
1287 req2 = cpu_to_le32(config_item->activate);
1288 err = scarlett2_usb(mixer, SCARLETT2_USB_DATA_CMD,
1289 &req2, sizeof(req2), NULL, 0);
1293 /* Schedule the change to be written to NVRAM */
1294 if (config_item->activate != SCARLETT2_USB_CONFIG_SAVE)
1295 schedule_delayed_work(&private->work, msecs_to_jiffies(2000));
1300 /* Send a USB message to get sync status; result placed in *sync */
1301 static int scarlett2_usb_get_sync_status(
1302 struct usb_mixer_interface *mixer,
1308 err = scarlett2_usb(mixer, SCARLETT2_USB_GET_SYNC,
1309 NULL, 0, &data, sizeof(data));
1317 /* Send a USB message to get volume status; result placed in *buf */
1318 static int scarlett2_usb_get_volume_status(
1319 struct usb_mixer_interface *mixer,
1320 struct scarlett2_usb_volume_status *buf)
1322 return scarlett2_usb_get(mixer, SCARLETT2_USB_VOLUME_STATUS_OFFSET,
1326 /* Send a USB message to get the volumes for all inputs of one mix
1327 * and put the values into private->mix[]
1329 static int scarlett2_usb_get_mix(struct usb_mixer_interface *mixer,
1332 struct scarlett2_data *private = mixer->private_data;
1333 const struct scarlett2_device_info *info = private->info;
1336 info->port_count[SCARLETT2_PORT_TYPE_MIX][SCARLETT2_PORT_OUT];
1344 __le16 data[SCARLETT2_INPUT_MIX_MAX];
1346 req.mix_num = cpu_to_le16(mix_num);
1347 req.count = cpu_to_le16(num_mixer_in);
1349 err = scarlett2_usb(mixer, SCARLETT2_USB_GET_MIX,
1351 data, num_mixer_in * sizeof(u16));
1355 for (i = 0, j = mix_num * num_mixer_in; i < num_mixer_in; i++, j++) {
1356 u16 mixer_value = le16_to_cpu(data[i]);
1358 for (k = 0; k < SCARLETT2_MIXER_VALUE_COUNT; k++)
1359 if (scarlett2_mixer_values[k] >= mixer_value)
1361 if (k == SCARLETT2_MIXER_VALUE_COUNT)
1362 k = SCARLETT2_MIXER_MAX_VALUE;
1363 private->mix[j] = k;
1369 /* Send a USB message to set the volumes for all inputs of one mix
1370 * (values obtained from private->mix[])
1372 static int scarlett2_usb_set_mix(struct usb_mixer_interface *mixer,
1375 struct scarlett2_data *private = mixer->private_data;
1376 const struct scarlett2_device_info *info = private->info;
1380 __le16 data[SCARLETT2_INPUT_MIX_MAX];
1385 info->port_count[SCARLETT2_PORT_TYPE_MIX][SCARLETT2_PORT_OUT];
1387 req.mix_num = cpu_to_le16(mix_num);
1389 for (i = 0, j = mix_num * num_mixer_in; i < num_mixer_in; i++, j++)
1390 req.data[i] = cpu_to_le16(
1391 scarlett2_mixer_values[private->mix[j]]
1394 return scarlett2_usb(mixer, SCARLETT2_USB_SET_MIX,
1395 &req, (num_mixer_in + 1) * sizeof(u16),
1399 /* Convert a port number index (per info->port_count) to a hardware ID */
1400 static u32 scarlett2_mux_src_num_to_id(
1401 const int port_count[][SCARLETT2_PORT_DIRNS], int num)
1406 port_type < SCARLETT2_PORT_TYPE_COUNT;
1408 if (num < port_count[port_type][SCARLETT2_PORT_IN])
1409 return scarlett2_ports[port_type].id | num;
1410 num -= port_count[port_type][SCARLETT2_PORT_IN];
1417 /* Convert a hardware ID to a port number index */
1418 static u32 scarlett2_mux_id_to_num(
1419 const int port_count[][SCARLETT2_PORT_DIRNS], int direction, u32 id)
1425 port_type < SCARLETT2_PORT_TYPE_COUNT;
1427 int base = scarlett2_ports[port_type].id;
1428 int count = port_count[port_type][direction];
1430 if (id >= base && id < base + count)
1431 return port_num + id - base;
1439 /* Convert one mux entry from the interface and load into private->mux[] */
1440 static void scarlett2_usb_populate_mux(struct scarlett2_data *private,
1443 const struct scarlett2_device_info *info = private->info;
1444 const int (*port_count)[SCARLETT2_PORT_DIRNS] = info->port_count;
1446 int dst_idx, src_idx;
1448 dst_idx = scarlett2_mux_id_to_num(port_count, SCARLETT2_PORT_OUT,
1453 if (dst_idx >= private->num_mux_dsts) {
1454 usb_audio_err(private->mixer->chip,
1455 "BUG: scarlett2_mux_id_to_num(%06x, OUT): %d >= %d",
1456 mux_entry, dst_idx, private->num_mux_dsts);
1460 src_idx = scarlett2_mux_id_to_num(port_count, SCARLETT2_PORT_IN,
1465 if (src_idx >= private->num_mux_srcs) {
1466 usb_audio_err(private->mixer->chip,
1467 "BUG: scarlett2_mux_id_to_num(%06x, IN): %d >= %d",
1468 mux_entry, src_idx, private->num_mux_srcs);
1472 private->mux[dst_idx] = src_idx;
1475 /* Send USB message to get mux inputs and then populate private->mux[] */
1476 static int scarlett2_usb_get_mux(struct usb_mixer_interface *mixer)
1478 struct scarlett2_data *private = mixer->private_data;
1479 int count = private->num_mux_dsts;
1487 __le32 data[SCARLETT2_MUX_MAX];
1489 private->mux_updated = 0;
1492 req.count = cpu_to_le16(count);
1494 err = scarlett2_usb(mixer, SCARLETT2_USB_GET_MUX,
1496 data, count * sizeof(u32));
1500 for (i = 0; i < count; i++)
1501 scarlett2_usb_populate_mux(private, le32_to_cpu(data[i]));
1506 /* Send USB messages to set mux inputs */
1507 static int scarlett2_usb_set_mux(struct usb_mixer_interface *mixer)
1509 struct scarlett2_data *private = mixer->private_data;
1510 const struct scarlett2_device_info *info = private->info;
1511 const int (*port_count)[SCARLETT2_PORT_DIRNS] = info->port_count;
1517 __le32 data[SCARLETT2_MUX_MAX];
1522 /* set mux settings for each rate */
1523 for (table = 0; table < SCARLETT2_MUX_TABLES; table++) {
1524 const struct scarlett2_mux_entry *entry;
1526 /* i counts over the output array */
1529 req.num = cpu_to_le16(table);
1531 /* loop through each entry */
1532 for (entry = info->mux_assignment[table];
1536 int port_type = entry->port_type;
1537 int port_idx = entry->start;
1538 int mux_idx = scarlett2_get_port_start_num(port_count,
1539 SCARLETT2_PORT_OUT, port_type) + port_idx;
1540 int dst_id = scarlett2_ports[port_type].id + port_idx;
1544 for (j = 0; j < entry->count; j++)
1549 /* Non-empty mux slots use the lower 12 bits
1550 * for the destination and next 12 bits for
1553 for (j = 0; j < entry->count; j++) {
1554 int src_id = scarlett2_mux_src_num_to_id(
1555 port_count, private->mux[mux_idx++]);
1556 req.data[i++] = cpu_to_le32(dst_id |
1562 err = scarlett2_usb(mixer, SCARLETT2_USB_SET_MUX,
1563 &req, (i + 1) * sizeof(u32),
1572 /* Send USB message to get meter levels */
1573 static int scarlett2_usb_get_meter_levels(struct usb_mixer_interface *mixer,
1574 u16 num_meters, u16 *levels)
1581 u32 resp[SCARLETT2_MAX_METERS];
1585 req.num_meters = cpu_to_le16(num_meters);
1586 req.magic = cpu_to_le32(SCARLETT2_USB_METER_LEVELS_GET_MAGIC);
1587 err = scarlett2_usb(mixer, SCARLETT2_USB_GET_METER,
1588 &req, sizeof(req), resp, num_meters * sizeof(u32));
1592 /* copy, convert to u16 */
1593 for (i = 0; i < num_meters; i++)
1594 levels[i] = resp[i];
1599 /*** Control Functions ***/
1601 /* helper function to create a new control */
1602 static int scarlett2_add_new_ctl(struct usb_mixer_interface *mixer,
1603 const struct snd_kcontrol_new *ncontrol,
1604 int index, int channels, const char *name,
1605 struct snd_kcontrol **kctl_return)
1607 struct snd_kcontrol *kctl;
1608 struct usb_mixer_elem_info *elem;
1611 elem = kzalloc(sizeof(*elem), GFP_KERNEL);
1615 /* We set USB_MIXER_BESPOKEN type, so that the core USB mixer code
1616 * ignores them for resume and other operations.
1617 * Also, the head.id field is set to 0, as we don't use this field.
1619 elem->head.mixer = mixer;
1620 elem->control = index;
1622 elem->channels = channels;
1623 elem->val_type = USB_MIXER_BESPOKEN;
1625 kctl = snd_ctl_new1(ncontrol, elem);
1630 kctl->private_free = snd_usb_mixer_elem_free;
1632 strscpy(kctl->id.name, name, sizeof(kctl->id.name));
1634 err = snd_usb_mixer_add_control(&elem->head, kctl);
1639 *kctl_return = kctl;
1644 /*** Sync Control ***/
1646 /* Update sync control after receiving notification that the status
1649 static int scarlett2_update_sync(struct usb_mixer_interface *mixer)
1651 struct scarlett2_data *private = mixer->private_data;
1653 private->sync_updated = 0;
1654 return scarlett2_usb_get_sync_status(mixer, &private->sync);
1657 static int scarlett2_sync_ctl_info(struct snd_kcontrol *kctl,
1658 struct snd_ctl_elem_info *uinfo)
1660 static const char *texts[2] = {
1661 "Unlocked", "Locked"
1663 return snd_ctl_enum_info(uinfo, 1, 2, texts);
1666 static int scarlett2_sync_ctl_get(struct snd_kcontrol *kctl,
1667 struct snd_ctl_elem_value *ucontrol)
1669 struct usb_mixer_elem_info *elem = kctl->private_data;
1670 struct usb_mixer_interface *mixer = elem->head.mixer;
1671 struct scarlett2_data *private = mixer->private_data;
1673 mutex_lock(&private->data_mutex);
1674 if (private->sync_updated)
1675 scarlett2_update_sync(mixer);
1676 ucontrol->value.enumerated.item[0] = private->sync;
1677 mutex_unlock(&private->data_mutex);
1682 static const struct snd_kcontrol_new scarlett2_sync_ctl = {
1683 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1684 .access = SNDRV_CTL_ELEM_ACCESS_READ,
1686 .info = scarlett2_sync_ctl_info,
1687 .get = scarlett2_sync_ctl_get
1690 static int scarlett2_add_sync_ctl(struct usb_mixer_interface *mixer)
1692 struct scarlett2_data *private = mixer->private_data;
1694 /* devices without a mixer also don't support reporting sync status */
1695 if (!private->info->has_mixer)
1698 return scarlett2_add_new_ctl(mixer, &scarlett2_sync_ctl,
1699 0, 1, "Sync Status", &private->sync_ctl);
1702 /*** Analogue Line Out Volume Controls ***/
1704 /* Update hardware volume controls after receiving notification that
1707 static int scarlett2_update_volumes(struct usb_mixer_interface *mixer)
1709 struct scarlett2_data *private = mixer->private_data;
1710 const struct scarlett2_device_info *info = private->info;
1711 const int (*port_count)[SCARLETT2_PORT_DIRNS] = info->port_count;
1712 struct scarlett2_usb_volume_status volume_status;
1714 port_count[SCARLETT2_PORT_TYPE_ANALOGUE][SCARLETT2_PORT_OUT];
1718 private->vol_updated = 0;
1720 err = scarlett2_usb_get_volume_status(mixer, &volume_status);
1724 private->master_vol = clamp(
1725 volume_status.master_vol + SCARLETT2_VOLUME_BIAS,
1726 0, SCARLETT2_VOLUME_BIAS);
1728 if (info->line_out_hw_vol)
1729 for (i = 0; i < SCARLETT2_DIM_MUTE_COUNT; i++)
1730 private->dim_mute[i] = !!volume_status.dim_mute[i];
1732 mute = private->dim_mute[SCARLETT2_BUTTON_MUTE];
1734 for (i = 0; i < num_line_out; i++)
1735 if (private->vol_sw_hw_switch[i]) {
1736 private->vol[i] = private->master_vol;
1737 private->mute_switch[i] = mute;
1743 static int scarlett2_volume_ctl_info(struct snd_kcontrol *kctl,
1744 struct snd_ctl_elem_info *uinfo)
1746 struct usb_mixer_elem_info *elem = kctl->private_data;
1748 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1749 uinfo->count = elem->channels;
1750 uinfo->value.integer.min = 0;
1751 uinfo->value.integer.max = SCARLETT2_VOLUME_BIAS;
1752 uinfo->value.integer.step = 1;
1756 static int scarlett2_master_volume_ctl_get(struct snd_kcontrol *kctl,
1757 struct snd_ctl_elem_value *ucontrol)
1759 struct usb_mixer_elem_info *elem = kctl->private_data;
1760 struct usb_mixer_interface *mixer = elem->head.mixer;
1761 struct scarlett2_data *private = mixer->private_data;
1763 mutex_lock(&private->data_mutex);
1764 if (private->vol_updated)
1765 scarlett2_update_volumes(mixer);
1766 mutex_unlock(&private->data_mutex);
1768 ucontrol->value.integer.value[0] = private->master_vol;
1772 static int line_out_remap(struct scarlett2_data *private, int index)
1774 const struct scarlett2_device_info *info = private->info;
1776 if (!info->line_out_remap_enable)
1778 return info->line_out_remap[index];
1781 static int scarlett2_volume_ctl_get(struct snd_kcontrol *kctl,
1782 struct snd_ctl_elem_value *ucontrol)
1784 struct usb_mixer_elem_info *elem = kctl->private_data;
1785 struct usb_mixer_interface *mixer = elem->head.mixer;
1786 struct scarlett2_data *private = mixer->private_data;
1787 int index = line_out_remap(private, elem->control);
1789 mutex_lock(&private->data_mutex);
1790 if (private->vol_updated)
1791 scarlett2_update_volumes(mixer);
1792 mutex_unlock(&private->data_mutex);
1794 ucontrol->value.integer.value[0] = private->vol[index];
1798 static int scarlett2_volume_ctl_put(struct snd_kcontrol *kctl,
1799 struct snd_ctl_elem_value *ucontrol)
1801 struct usb_mixer_elem_info *elem = kctl->private_data;
1802 struct usb_mixer_interface *mixer = elem->head.mixer;
1803 struct scarlett2_data *private = mixer->private_data;
1804 int index = line_out_remap(private, elem->control);
1805 int oval, val, err = 0;
1807 mutex_lock(&private->data_mutex);
1809 oval = private->vol[index];
1810 val = ucontrol->value.integer.value[0];
1815 private->vol[index] = val;
1816 err = scarlett2_usb_set_config(mixer, SCARLETT2_CONFIG_LINE_OUT_VOLUME,
1817 index, val - SCARLETT2_VOLUME_BIAS);
1822 mutex_unlock(&private->data_mutex);
1826 static const DECLARE_TLV_DB_MINMAX(
1827 db_scale_scarlett2_gain, -SCARLETT2_VOLUME_BIAS * 100, 0
1830 static const struct snd_kcontrol_new scarlett2_master_volume_ctl = {
1831 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1832 .access = SNDRV_CTL_ELEM_ACCESS_READ |
1833 SNDRV_CTL_ELEM_ACCESS_TLV_READ,
1835 .info = scarlett2_volume_ctl_info,
1836 .get = scarlett2_master_volume_ctl_get,
1837 .private_value = 0, /* max value */
1838 .tlv = { .p = db_scale_scarlett2_gain }
1841 static const struct snd_kcontrol_new scarlett2_line_out_volume_ctl = {
1842 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1843 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE |
1844 SNDRV_CTL_ELEM_ACCESS_TLV_READ,
1846 .info = scarlett2_volume_ctl_info,
1847 .get = scarlett2_volume_ctl_get,
1848 .put = scarlett2_volume_ctl_put,
1849 .private_value = 0, /* max value */
1850 .tlv = { .p = db_scale_scarlett2_gain }
1853 /*** Mute Switch Controls ***/
1855 static int scarlett2_mute_ctl_get(struct snd_kcontrol *kctl,
1856 struct snd_ctl_elem_value *ucontrol)
1858 struct usb_mixer_elem_info *elem = kctl->private_data;
1859 struct scarlett2_data *private = elem->head.mixer->private_data;
1860 int index = line_out_remap(private, elem->control);
1862 ucontrol->value.integer.value[0] = private->mute_switch[index];
1866 static int scarlett2_mute_ctl_put(struct snd_kcontrol *kctl,
1867 struct snd_ctl_elem_value *ucontrol)
1869 struct usb_mixer_elem_info *elem = kctl->private_data;
1870 struct usb_mixer_interface *mixer = elem->head.mixer;
1871 struct scarlett2_data *private = mixer->private_data;
1872 int index = line_out_remap(private, elem->control);
1873 int oval, val, err = 0;
1875 mutex_lock(&private->data_mutex);
1877 oval = private->mute_switch[index];
1878 val = !!ucontrol->value.integer.value[0];
1883 private->mute_switch[index] = val;
1885 /* Send mute change to the device */
1886 err = scarlett2_usb_set_config(mixer, SCARLETT2_CONFIG_MUTE_SWITCH,
1892 mutex_unlock(&private->data_mutex);
1896 static const struct snd_kcontrol_new scarlett2_mute_ctl = {
1897 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1899 .info = snd_ctl_boolean_mono_info,
1900 .get = scarlett2_mute_ctl_get,
1901 .put = scarlett2_mute_ctl_put,
1904 /*** HW/SW Volume Switch Controls ***/
1906 static void scarlett2_sw_hw_ctl_ro(struct scarlett2_data *private, int index)
1908 private->sw_hw_ctls[index]->vd[0].access &=
1909 ~SNDRV_CTL_ELEM_ACCESS_WRITE;
1912 static void scarlett2_sw_hw_ctl_rw(struct scarlett2_data *private, int index)
1914 private->sw_hw_ctls[index]->vd[0].access |=
1915 SNDRV_CTL_ELEM_ACCESS_WRITE;
1918 static int scarlett2_sw_hw_enum_ctl_info(struct snd_kcontrol *kctl,
1919 struct snd_ctl_elem_info *uinfo)
1921 static const char *const values[2] = {
1925 return snd_ctl_enum_info(uinfo, 1, 2, values);
1928 static int scarlett2_sw_hw_enum_ctl_get(struct snd_kcontrol *kctl,
1929 struct snd_ctl_elem_value *ucontrol)
1931 struct usb_mixer_elem_info *elem = kctl->private_data;
1932 struct scarlett2_data *private = elem->head.mixer->private_data;
1933 int index = line_out_remap(private, elem->control);
1935 ucontrol->value.enumerated.item[0] = private->vol_sw_hw_switch[index];
1939 static void scarlett2_vol_ctl_set_writable(struct usb_mixer_interface *mixer,
1940 int index, int value)
1942 struct scarlett2_data *private = mixer->private_data;
1943 struct snd_card *card = mixer->chip->card;
1945 /* Set/Clear write bits */
1947 private->vol_ctls[index]->vd[0].access |=
1948 SNDRV_CTL_ELEM_ACCESS_WRITE;
1949 private->mute_ctls[index]->vd[0].access |=
1950 SNDRV_CTL_ELEM_ACCESS_WRITE;
1952 private->vol_ctls[index]->vd[0].access &=
1953 ~SNDRV_CTL_ELEM_ACCESS_WRITE;
1954 private->mute_ctls[index]->vd[0].access &=
1955 ~SNDRV_CTL_ELEM_ACCESS_WRITE;
1958 /* Notify of write bit change */
1959 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_INFO,
1960 &private->vol_ctls[index]->id);
1961 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_INFO,
1962 &private->mute_ctls[index]->id);
1965 static int scarlett2_sw_hw_change(struct usb_mixer_interface *mixer,
1966 int ctl_index, int val)
1968 struct scarlett2_data *private = mixer->private_data;
1969 int index = line_out_remap(private, ctl_index);
1972 private->vol_sw_hw_switch[index] = val;
1974 /* Change access mode to RO (hardware controlled volume)
1975 * or RW (software controlled volume)
1977 scarlett2_vol_ctl_set_writable(mixer, ctl_index, !val);
1979 /* Reset volume/mute to master volume/mute */
1980 private->vol[index] = private->master_vol;
1981 private->mute_switch[index] = private->dim_mute[SCARLETT2_BUTTON_MUTE];
1983 /* Set SW volume to current HW volume */
1984 err = scarlett2_usb_set_config(
1985 mixer, SCARLETT2_CONFIG_LINE_OUT_VOLUME,
1986 index, private->master_vol - SCARLETT2_VOLUME_BIAS);
1990 /* Set SW mute to current HW mute */
1991 err = scarlett2_usb_set_config(
1992 mixer, SCARLETT2_CONFIG_MUTE_SWITCH,
1993 index, private->dim_mute[SCARLETT2_BUTTON_MUTE]);
1997 /* Send SW/HW switch change to the device */
1998 return scarlett2_usb_set_config(mixer, SCARLETT2_CONFIG_SW_HW_SWITCH,
2002 static int scarlett2_sw_hw_enum_ctl_put(struct snd_kcontrol *kctl,
2003 struct snd_ctl_elem_value *ucontrol)
2005 struct usb_mixer_elem_info *elem = kctl->private_data;
2006 struct usb_mixer_interface *mixer = elem->head.mixer;
2007 struct scarlett2_data *private = mixer->private_data;
2008 int ctl_index = elem->control;
2009 int index = line_out_remap(private, ctl_index);
2010 int oval, val, err = 0;
2012 mutex_lock(&private->data_mutex);
2014 oval = private->vol_sw_hw_switch[index];
2015 val = !!ucontrol->value.enumerated.item[0];
2020 err = scarlett2_sw_hw_change(mixer, ctl_index, val);
2025 mutex_unlock(&private->data_mutex);
2029 static const struct snd_kcontrol_new scarlett2_sw_hw_enum_ctl = {
2030 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2032 .info = scarlett2_sw_hw_enum_ctl_info,
2033 .get = scarlett2_sw_hw_enum_ctl_get,
2034 .put = scarlett2_sw_hw_enum_ctl_put,
2037 /*** Line Level/Instrument Level Switch Controls ***/
2039 static int scarlett2_update_input_other(struct usb_mixer_interface *mixer)
2041 struct scarlett2_data *private = mixer->private_data;
2042 const struct scarlett2_device_info *info = private->info;
2044 private->input_other_updated = 0;
2046 if (info->level_input_count) {
2047 int err = scarlett2_usb_get_config(
2048 mixer, SCARLETT2_CONFIG_LEVEL_SWITCH,
2049 info->level_input_count + info->level_input_first,
2050 private->level_switch);
2055 if (info->pad_input_count) {
2056 int err = scarlett2_usb_get_config(
2057 mixer, SCARLETT2_CONFIG_PAD_SWITCH,
2058 info->pad_input_count, private->pad_switch);
2063 if (info->air_input_count) {
2064 int err = scarlett2_usb_get_config(
2065 mixer, SCARLETT2_CONFIG_AIR_SWITCH,
2066 info->air_input_count, private->air_switch);
2071 if (info->phantom_count) {
2072 int err = scarlett2_usb_get_config(
2073 mixer, SCARLETT2_CONFIG_PHANTOM_SWITCH,
2074 info->phantom_count, private->phantom_switch);
2078 err = scarlett2_usb_get_config(
2079 mixer, SCARLETT2_CONFIG_PHANTOM_PERSISTENCE,
2080 1, &private->phantom_persistence);
2088 static int scarlett2_level_enum_ctl_info(struct snd_kcontrol *kctl,
2089 struct snd_ctl_elem_info *uinfo)
2091 static const char *const values[2] = {
2095 return snd_ctl_enum_info(uinfo, 1, 2, values);
2098 static int scarlett2_level_enum_ctl_get(struct snd_kcontrol *kctl,
2099 struct snd_ctl_elem_value *ucontrol)
2101 struct usb_mixer_elem_info *elem = kctl->private_data;
2102 struct usb_mixer_interface *mixer = elem->head.mixer;
2103 struct scarlett2_data *private = mixer->private_data;
2104 const struct scarlett2_device_info *info = private->info;
2106 int index = elem->control + info->level_input_first;
2108 mutex_lock(&private->data_mutex);
2109 if (private->input_other_updated)
2110 scarlett2_update_input_other(mixer);
2111 ucontrol->value.enumerated.item[0] = private->level_switch[index];
2112 mutex_unlock(&private->data_mutex);
2117 static int scarlett2_level_enum_ctl_put(struct snd_kcontrol *kctl,
2118 struct snd_ctl_elem_value *ucontrol)
2120 struct usb_mixer_elem_info *elem = kctl->private_data;
2121 struct usb_mixer_interface *mixer = elem->head.mixer;
2122 struct scarlett2_data *private = mixer->private_data;
2123 const struct scarlett2_device_info *info = private->info;
2125 int index = elem->control + info->level_input_first;
2126 int oval, val, err = 0;
2128 mutex_lock(&private->data_mutex);
2130 oval = private->level_switch[index];
2131 val = !!ucontrol->value.enumerated.item[0];
2136 private->level_switch[index] = val;
2138 /* Send switch change to the device */
2139 err = scarlett2_usb_set_config(mixer, SCARLETT2_CONFIG_LEVEL_SWITCH,
2145 mutex_unlock(&private->data_mutex);
2149 static const struct snd_kcontrol_new scarlett2_level_enum_ctl = {
2150 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2152 .info = scarlett2_level_enum_ctl_info,
2153 .get = scarlett2_level_enum_ctl_get,
2154 .put = scarlett2_level_enum_ctl_put,
2157 /*** Pad Switch Controls ***/
2159 static int scarlett2_pad_ctl_get(struct snd_kcontrol *kctl,
2160 struct snd_ctl_elem_value *ucontrol)
2162 struct usb_mixer_elem_info *elem = kctl->private_data;
2163 struct usb_mixer_interface *mixer = elem->head.mixer;
2164 struct scarlett2_data *private = mixer->private_data;
2166 mutex_lock(&private->data_mutex);
2167 if (private->input_other_updated)
2168 scarlett2_update_input_other(mixer);
2169 ucontrol->value.integer.value[0] =
2170 private->pad_switch[elem->control];
2171 mutex_unlock(&private->data_mutex);
2176 static int scarlett2_pad_ctl_put(struct snd_kcontrol *kctl,
2177 struct snd_ctl_elem_value *ucontrol)
2179 struct usb_mixer_elem_info *elem = kctl->private_data;
2180 struct usb_mixer_interface *mixer = elem->head.mixer;
2181 struct scarlett2_data *private = mixer->private_data;
2183 int index = elem->control;
2184 int oval, val, err = 0;
2186 mutex_lock(&private->data_mutex);
2188 oval = private->pad_switch[index];
2189 val = !!ucontrol->value.integer.value[0];
2194 private->pad_switch[index] = val;
2196 /* Send switch change to the device */
2197 err = scarlett2_usb_set_config(mixer, SCARLETT2_CONFIG_PAD_SWITCH,
2203 mutex_unlock(&private->data_mutex);
2207 static const struct snd_kcontrol_new scarlett2_pad_ctl = {
2208 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2210 .info = snd_ctl_boolean_mono_info,
2211 .get = scarlett2_pad_ctl_get,
2212 .put = scarlett2_pad_ctl_put,
2215 /*** Air Switch Controls ***/
2217 static int scarlett2_air_ctl_get(struct snd_kcontrol *kctl,
2218 struct snd_ctl_elem_value *ucontrol)
2220 struct usb_mixer_elem_info *elem = kctl->private_data;
2221 struct usb_mixer_interface *mixer = elem->head.mixer;
2222 struct scarlett2_data *private = mixer->private_data;
2224 mutex_lock(&private->data_mutex);
2225 if (private->input_other_updated)
2226 scarlett2_update_input_other(mixer);
2227 ucontrol->value.integer.value[0] = private->air_switch[elem->control];
2228 mutex_unlock(&private->data_mutex);
2233 static int scarlett2_air_ctl_put(struct snd_kcontrol *kctl,
2234 struct snd_ctl_elem_value *ucontrol)
2236 struct usb_mixer_elem_info *elem = kctl->private_data;
2237 struct usb_mixer_interface *mixer = elem->head.mixer;
2238 struct scarlett2_data *private = mixer->private_data;
2240 int index = elem->control;
2241 int oval, val, err = 0;
2243 mutex_lock(&private->data_mutex);
2245 oval = private->air_switch[index];
2246 val = !!ucontrol->value.integer.value[0];
2251 private->air_switch[index] = val;
2253 /* Send switch change to the device */
2254 err = scarlett2_usb_set_config(mixer, SCARLETT2_CONFIG_AIR_SWITCH,
2260 mutex_unlock(&private->data_mutex);
2264 static const struct snd_kcontrol_new scarlett2_air_ctl = {
2265 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2267 .info = snd_ctl_boolean_mono_info,
2268 .get = scarlett2_air_ctl_get,
2269 .put = scarlett2_air_ctl_put,
2272 /*** Phantom Switch Controls ***/
2274 static int scarlett2_phantom_ctl_get(struct snd_kcontrol *kctl,
2275 struct snd_ctl_elem_value *ucontrol)
2277 struct usb_mixer_elem_info *elem = kctl->private_data;
2278 struct usb_mixer_interface *mixer = elem->head.mixer;
2279 struct scarlett2_data *private = mixer->private_data;
2281 mutex_lock(&private->data_mutex);
2282 if (private->input_other_updated)
2283 scarlett2_update_input_other(mixer);
2284 ucontrol->value.integer.value[0] =
2285 private->phantom_switch[elem->control];
2286 mutex_unlock(&private->data_mutex);
2291 static int scarlett2_phantom_ctl_put(struct snd_kcontrol *kctl,
2292 struct snd_ctl_elem_value *ucontrol)
2294 struct usb_mixer_elem_info *elem = kctl->private_data;
2295 struct usb_mixer_interface *mixer = elem->head.mixer;
2296 struct scarlett2_data *private = mixer->private_data;
2298 int index = elem->control;
2299 int oval, val, err = 0;
2301 mutex_lock(&private->data_mutex);
2303 oval = private->phantom_switch[index];
2304 val = !!ucontrol->value.integer.value[0];
2309 private->phantom_switch[index] = val;
2311 /* Send switch change to the device */
2312 err = scarlett2_usb_set_config(mixer, SCARLETT2_CONFIG_PHANTOM_SWITCH,
2318 mutex_unlock(&private->data_mutex);
2322 static const struct snd_kcontrol_new scarlett2_phantom_ctl = {
2323 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2325 .info = snd_ctl_boolean_mono_info,
2326 .get = scarlett2_phantom_ctl_get,
2327 .put = scarlett2_phantom_ctl_put,
2330 /*** Phantom Persistence Control ***/
2332 static int scarlett2_phantom_persistence_ctl_get(
2333 struct snd_kcontrol *kctl, struct snd_ctl_elem_value *ucontrol)
2335 struct usb_mixer_elem_info *elem = kctl->private_data;
2336 struct scarlett2_data *private = elem->head.mixer->private_data;
2338 ucontrol->value.integer.value[0] = private->phantom_persistence;
2342 static int scarlett2_phantom_persistence_ctl_put(
2343 struct snd_kcontrol *kctl, struct snd_ctl_elem_value *ucontrol)
2345 struct usb_mixer_elem_info *elem = kctl->private_data;
2346 struct usb_mixer_interface *mixer = elem->head.mixer;
2347 struct scarlett2_data *private = mixer->private_data;
2349 int index = elem->control;
2350 int oval, val, err = 0;
2352 mutex_lock(&private->data_mutex);
2354 oval = private->phantom_persistence;
2355 val = !!ucontrol->value.integer.value[0];
2360 private->phantom_persistence = val;
2362 /* Send switch change to the device */
2363 err = scarlett2_usb_set_config(
2364 mixer, SCARLETT2_CONFIG_PHANTOM_PERSISTENCE, index, val);
2369 mutex_unlock(&private->data_mutex);
2373 static const struct snd_kcontrol_new scarlett2_phantom_persistence_ctl = {
2374 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2376 .info = snd_ctl_boolean_mono_info,
2377 .get = scarlett2_phantom_persistence_ctl_get,
2378 .put = scarlett2_phantom_persistence_ctl_put,
2381 /*** Direct Monitor Control ***/
2383 static int scarlett2_update_monitor_other(struct usb_mixer_interface *mixer)
2385 struct scarlett2_data *private = mixer->private_data;
2386 const struct scarlett2_device_info *info = private->info;
2389 /* monitor_other_enable[0] enables speaker switching
2390 * monitor_other_enable[1] enables talkback
2392 u8 monitor_other_enable[2];
2394 /* monitor_other_switch[0] activates the alternate speakers
2395 * monitor_other_switch[1] activates talkback
2397 u8 monitor_other_switch[2];
2399 private->monitor_other_updated = 0;
2401 if (info->direct_monitor)
2402 return scarlett2_usb_get_config(
2403 mixer, SCARLETT2_CONFIG_DIRECT_MONITOR,
2404 1, &private->direct_monitor_switch);
2406 /* if it doesn't do speaker switching then it also doesn't do
2409 if (!info->has_speaker_switching)
2412 err = scarlett2_usb_get_config(
2413 mixer, SCARLETT2_CONFIG_MONITOR_OTHER_ENABLE,
2414 2, monitor_other_enable);
2418 err = scarlett2_usb_get_config(
2419 mixer, SCARLETT2_CONFIG_MONITOR_OTHER_SWITCH,
2420 2, monitor_other_switch);
2424 if (!monitor_other_enable[0])
2425 private->speaker_switching_switch = 0;
2427 private->speaker_switching_switch = monitor_other_switch[0] + 1;
2429 if (info->has_talkback) {
2430 const int (*port_count)[SCARLETT2_PORT_DIRNS] =
2433 port_count[SCARLETT2_PORT_TYPE_MIX][SCARLETT2_PORT_IN];
2437 if (!monitor_other_enable[1])
2438 private->talkback_switch = 0;
2440 private->talkback_switch = monitor_other_switch[1] + 1;
2442 err = scarlett2_usb_get_config(mixer,
2443 SCARLETT2_CONFIG_TALKBACK_MAP,
2445 for (i = 0; i < num_mixes; i++, bitmap >>= 1)
2446 private->talkback_map[i] = bitmap & 1;
2452 static int scarlett2_direct_monitor_ctl_get(
2453 struct snd_kcontrol *kctl, struct snd_ctl_elem_value *ucontrol)
2455 struct usb_mixer_elem_info *elem = kctl->private_data;
2456 struct usb_mixer_interface *mixer = elem->head.mixer;
2457 struct scarlett2_data *private = elem->head.mixer->private_data;
2459 mutex_lock(&private->data_mutex);
2460 if (private->monitor_other_updated)
2461 scarlett2_update_monitor_other(mixer);
2462 ucontrol->value.enumerated.item[0] = private->direct_monitor_switch;
2463 mutex_unlock(&private->data_mutex);
2468 static int scarlett2_direct_monitor_ctl_put(
2469 struct snd_kcontrol *kctl, struct snd_ctl_elem_value *ucontrol)
2471 struct usb_mixer_elem_info *elem = kctl->private_data;
2472 struct usb_mixer_interface *mixer = elem->head.mixer;
2473 struct scarlett2_data *private = mixer->private_data;
2475 int index = elem->control;
2476 int oval, val, err = 0;
2478 mutex_lock(&private->data_mutex);
2480 oval = private->direct_monitor_switch;
2481 val = min(ucontrol->value.enumerated.item[0], 2U);
2486 private->direct_monitor_switch = val;
2488 /* Send switch change to the device */
2489 err = scarlett2_usb_set_config(
2490 mixer, SCARLETT2_CONFIG_DIRECT_MONITOR, index, val);
2495 mutex_unlock(&private->data_mutex);
2499 static int scarlett2_direct_monitor_stereo_enum_ctl_info(
2500 struct snd_kcontrol *kctl, struct snd_ctl_elem_info *uinfo)
2502 static const char *const values[3] = {
2503 "Off", "Mono", "Stereo"
2506 return snd_ctl_enum_info(uinfo, 1, 3, values);
2509 /* Direct Monitor for Solo is mono-only and only needs a boolean control
2510 * Direct Monitor for 2i2 is selectable between Off/Mono/Stereo
2512 static const struct snd_kcontrol_new scarlett2_direct_monitor_ctl[2] = {
2514 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2516 .info = snd_ctl_boolean_mono_info,
2517 .get = scarlett2_direct_monitor_ctl_get,
2518 .put = scarlett2_direct_monitor_ctl_put,
2521 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2523 .info = scarlett2_direct_monitor_stereo_enum_ctl_info,
2524 .get = scarlett2_direct_monitor_ctl_get,
2525 .put = scarlett2_direct_monitor_ctl_put,
2529 static int scarlett2_add_direct_monitor_ctl(struct usb_mixer_interface *mixer)
2531 struct scarlett2_data *private = mixer->private_data;
2532 const struct scarlett2_device_info *info = private->info;
2534 if (!info->direct_monitor)
2537 return scarlett2_add_new_ctl(
2538 mixer, &scarlett2_direct_monitor_ctl[info->direct_monitor - 1],
2539 0, 1, "Direct Monitor Playback Switch",
2540 &private->direct_monitor_ctl);
2543 /*** Speaker Switching Control ***/
2545 static int scarlett2_speaker_switch_enum_ctl_info(
2546 struct snd_kcontrol *kctl, struct snd_ctl_elem_info *uinfo)
2548 static const char *const values[3] = {
2549 "Off", "Main", "Alt"
2552 return snd_ctl_enum_info(uinfo, 1, 3, values);
2555 static int scarlett2_speaker_switch_enum_ctl_get(
2556 struct snd_kcontrol *kctl, struct snd_ctl_elem_value *ucontrol)
2558 struct usb_mixer_elem_info *elem = kctl->private_data;
2559 struct usb_mixer_interface *mixer = elem->head.mixer;
2560 struct scarlett2_data *private = mixer->private_data;
2562 mutex_lock(&private->data_mutex);
2563 if (private->monitor_other_updated)
2564 scarlett2_update_monitor_other(mixer);
2565 ucontrol->value.enumerated.item[0] = private->speaker_switching_switch;
2566 mutex_unlock(&private->data_mutex);
2571 /* when speaker switching gets enabled, switch the main/alt speakers
2572 * to HW volume and disable those controls
2574 static int scarlett2_speaker_switch_enable(struct usb_mixer_interface *mixer)
2576 struct snd_card *card = mixer->chip->card;
2577 struct scarlett2_data *private = mixer->private_data;
2580 for (i = 0; i < 4; i++) {
2581 int index = line_out_remap(private, i);
2583 /* switch the main/alt speakers to HW volume */
2584 if (!private->vol_sw_hw_switch[index]) {
2585 err = scarlett2_sw_hw_change(private->mixer, i, 1);
2590 /* disable the line out SW/HW switch */
2591 scarlett2_sw_hw_ctl_ro(private, i);
2592 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_INFO,
2593 &private->sw_hw_ctls[i]->id);
2596 /* when the next monitor-other notify comes in, update the mux
2599 private->speaker_switching_switched = 1;
2604 /* when speaker switching gets disabled, reenable the hw/sw controls
2605 * and invalidate the routing
2607 static void scarlett2_speaker_switch_disable(struct usb_mixer_interface *mixer)
2609 struct snd_card *card = mixer->chip->card;
2610 struct scarlett2_data *private = mixer->private_data;
2613 /* enable the line out SW/HW switch */
2614 for (i = 0; i < 4; i++) {
2615 scarlett2_sw_hw_ctl_rw(private, i);
2616 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_INFO,
2617 &private->sw_hw_ctls[i]->id);
2620 /* when the next monitor-other notify comes in, update the mux
2623 private->speaker_switching_switched = 1;
2626 static int scarlett2_speaker_switch_enum_ctl_put(
2627 struct snd_kcontrol *kctl, struct snd_ctl_elem_value *ucontrol)
2629 struct usb_mixer_elem_info *elem = kctl->private_data;
2630 struct usb_mixer_interface *mixer = elem->head.mixer;
2631 struct scarlett2_data *private = mixer->private_data;
2633 int oval, val, err = 0;
2635 mutex_lock(&private->data_mutex);
2637 oval = private->speaker_switching_switch;
2638 val = min(ucontrol->value.enumerated.item[0], 2U);
2643 private->speaker_switching_switch = val;
2645 /* enable/disable speaker switching */
2646 err = scarlett2_usb_set_config(
2647 mixer, SCARLETT2_CONFIG_MONITOR_OTHER_ENABLE,
2652 /* if speaker switching is enabled, select main or alt */
2653 err = scarlett2_usb_set_config(
2654 mixer, SCARLETT2_CONFIG_MONITOR_OTHER_SWITCH,
2659 /* update controls if speaker switching gets enabled or disabled */
2661 err = scarlett2_speaker_switch_enable(mixer);
2662 else if (oval && !val)
2663 scarlett2_speaker_switch_disable(mixer);
2669 mutex_unlock(&private->data_mutex);
2673 static const struct snd_kcontrol_new scarlett2_speaker_switch_enum_ctl = {
2674 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2676 .info = scarlett2_speaker_switch_enum_ctl_info,
2677 .get = scarlett2_speaker_switch_enum_ctl_get,
2678 .put = scarlett2_speaker_switch_enum_ctl_put,
2681 static int scarlett2_add_speaker_switch_ctl(
2682 struct usb_mixer_interface *mixer)
2684 struct scarlett2_data *private = mixer->private_data;
2685 const struct scarlett2_device_info *info = private->info;
2687 if (!info->has_speaker_switching)
2690 return scarlett2_add_new_ctl(
2691 mixer, &scarlett2_speaker_switch_enum_ctl,
2692 0, 1, "Speaker Switching Playback Enum",
2693 &private->speaker_switching_ctl);
2696 /*** Talkback and Talkback Map Controls ***/
2698 static int scarlett2_talkback_enum_ctl_info(
2699 struct snd_kcontrol *kctl, struct snd_ctl_elem_info *uinfo)
2701 static const char *const values[3] = {
2702 "Disabled", "Off", "On"
2705 return snd_ctl_enum_info(uinfo, 1, 3, values);
2708 static int scarlett2_talkback_enum_ctl_get(
2709 struct snd_kcontrol *kctl, struct snd_ctl_elem_value *ucontrol)
2711 struct usb_mixer_elem_info *elem = kctl->private_data;
2712 struct usb_mixer_interface *mixer = elem->head.mixer;
2713 struct scarlett2_data *private = mixer->private_data;
2715 mutex_lock(&private->data_mutex);
2716 if (private->monitor_other_updated)
2717 scarlett2_update_monitor_other(mixer);
2718 ucontrol->value.enumerated.item[0] = private->talkback_switch;
2719 mutex_unlock(&private->data_mutex);
2724 static int scarlett2_talkback_enum_ctl_put(
2725 struct snd_kcontrol *kctl, struct snd_ctl_elem_value *ucontrol)
2727 struct usb_mixer_elem_info *elem = kctl->private_data;
2728 struct usb_mixer_interface *mixer = elem->head.mixer;
2729 struct scarlett2_data *private = mixer->private_data;
2731 int oval, val, err = 0;
2733 mutex_lock(&private->data_mutex);
2735 oval = private->talkback_switch;
2736 val = min(ucontrol->value.enumerated.item[0], 2U);
2741 private->talkback_switch = val;
2743 /* enable/disable talkback */
2744 err = scarlett2_usb_set_config(
2745 mixer, SCARLETT2_CONFIG_MONITOR_OTHER_ENABLE,
2750 /* if talkback is enabled, select main or alt */
2751 err = scarlett2_usb_set_config(
2752 mixer, SCARLETT2_CONFIG_MONITOR_OTHER_SWITCH,
2758 mutex_unlock(&private->data_mutex);
2762 static const struct snd_kcontrol_new scarlett2_talkback_enum_ctl = {
2763 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2765 .info = scarlett2_talkback_enum_ctl_info,
2766 .get = scarlett2_talkback_enum_ctl_get,
2767 .put = scarlett2_talkback_enum_ctl_put,
2770 static int scarlett2_talkback_map_ctl_get(
2771 struct snd_kcontrol *kctl, struct snd_ctl_elem_value *ucontrol)
2773 struct usb_mixer_elem_info *elem = kctl->private_data;
2774 struct usb_mixer_interface *mixer = elem->head.mixer;
2775 struct scarlett2_data *private = mixer->private_data;
2776 int index = elem->control;
2778 ucontrol->value.integer.value[0] = private->talkback_map[index];
2783 static int scarlett2_talkback_map_ctl_put(
2784 struct snd_kcontrol *kctl, struct snd_ctl_elem_value *ucontrol)
2786 struct usb_mixer_elem_info *elem = kctl->private_data;
2787 struct usb_mixer_interface *mixer = elem->head.mixer;
2788 struct scarlett2_data *private = mixer->private_data;
2789 const int (*port_count)[SCARLETT2_PORT_DIRNS] =
2790 private->info->port_count;
2791 int num_mixes = port_count[SCARLETT2_PORT_TYPE_MIX][SCARLETT2_PORT_IN];
2793 int index = elem->control;
2794 int oval, val, err = 0, i;
2797 mutex_lock(&private->data_mutex);
2799 oval = private->talkback_map[index];
2800 val = !!ucontrol->value.integer.value[0];
2805 private->talkback_map[index] = val;
2807 for (i = 0; i < num_mixes; i++)
2808 bitmap |= private->talkback_map[i] << i;
2810 /* Send updated bitmap to the device */
2811 err = scarlett2_usb_set_config(mixer, SCARLETT2_CONFIG_TALKBACK_MAP,
2817 mutex_unlock(&private->data_mutex);
2821 static const struct snd_kcontrol_new scarlett2_talkback_map_ctl = {
2822 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2824 .info = snd_ctl_boolean_mono_info,
2825 .get = scarlett2_talkback_map_ctl_get,
2826 .put = scarlett2_talkback_map_ctl_put,
2829 static int scarlett2_add_talkback_ctls(
2830 struct usb_mixer_interface *mixer)
2832 struct scarlett2_data *private = mixer->private_data;
2833 const struct scarlett2_device_info *info = private->info;
2834 const int (*port_count)[SCARLETT2_PORT_DIRNS] = info->port_count;
2835 int num_mixes = port_count[SCARLETT2_PORT_TYPE_MIX][SCARLETT2_PORT_IN];
2837 char s[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
2839 if (!info->has_talkback)
2842 err = scarlett2_add_new_ctl(
2843 mixer, &scarlett2_talkback_enum_ctl,
2844 0, 1, "Talkback Playback Enum",
2845 &private->talkback_ctl);
2849 for (i = 0; i < num_mixes; i++) {
2850 snprintf(s, sizeof(s),
2851 "Talkback Mix %c Playback Switch", i + 'A');
2852 err = scarlett2_add_new_ctl(mixer, &scarlett2_talkback_map_ctl,
2861 /*** Dim/Mute Controls ***/
2863 static int scarlett2_dim_mute_ctl_get(struct snd_kcontrol *kctl,
2864 struct snd_ctl_elem_value *ucontrol)
2866 struct usb_mixer_elem_info *elem = kctl->private_data;
2867 struct usb_mixer_interface *mixer = elem->head.mixer;
2868 struct scarlett2_data *private = mixer->private_data;
2870 mutex_lock(&private->data_mutex);
2871 if (private->vol_updated)
2872 scarlett2_update_volumes(mixer);
2873 mutex_unlock(&private->data_mutex);
2875 ucontrol->value.integer.value[0] = private->dim_mute[elem->control];
2879 static int scarlett2_dim_mute_ctl_put(struct snd_kcontrol *kctl,
2880 struct snd_ctl_elem_value *ucontrol)
2882 struct usb_mixer_elem_info *elem = kctl->private_data;
2883 struct usb_mixer_interface *mixer = elem->head.mixer;
2884 struct scarlett2_data *private = mixer->private_data;
2885 const struct scarlett2_device_info *info = private->info;
2886 const int (*port_count)[SCARLETT2_PORT_DIRNS] = info->port_count;
2888 port_count[SCARLETT2_PORT_TYPE_ANALOGUE][SCARLETT2_PORT_OUT];
2890 int index = elem->control;
2891 int oval, val, err = 0, i;
2893 mutex_lock(&private->data_mutex);
2895 oval = private->dim_mute[index];
2896 val = !!ucontrol->value.integer.value[0];
2901 private->dim_mute[index] = val;
2903 /* Send switch change to the device */
2904 err = scarlett2_usb_set_config(mixer, SCARLETT2_CONFIG_DIM_MUTE,
2909 if (index == SCARLETT2_BUTTON_MUTE)
2910 for (i = 0; i < num_line_out; i++) {
2911 int line_index = line_out_remap(private, i);
2913 if (private->vol_sw_hw_switch[line_index]) {
2914 private->mute_switch[line_index] = val;
2915 snd_ctl_notify(mixer->chip->card,
2916 SNDRV_CTL_EVENT_MASK_INFO,
2917 &private->mute_ctls[i]->id);
2922 mutex_unlock(&private->data_mutex);
2926 static const struct snd_kcontrol_new scarlett2_dim_mute_ctl = {
2927 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2929 .info = snd_ctl_boolean_mono_info,
2930 .get = scarlett2_dim_mute_ctl_get,
2931 .put = scarlett2_dim_mute_ctl_put
2934 /*** Create the analogue output controls ***/
2936 static int scarlett2_add_line_out_ctls(struct usb_mixer_interface *mixer)
2938 struct scarlett2_data *private = mixer->private_data;
2939 const struct scarlett2_device_info *info = private->info;
2940 const int (*port_count)[SCARLETT2_PORT_DIRNS] = info->port_count;
2942 port_count[SCARLETT2_PORT_TYPE_ANALOGUE][SCARLETT2_PORT_OUT];
2944 char s[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
2946 /* Add R/O HW volume control */
2947 if (info->line_out_hw_vol) {
2948 snprintf(s, sizeof(s), "Master HW Playback Volume");
2949 err = scarlett2_add_new_ctl(mixer,
2950 &scarlett2_master_volume_ctl,
2951 0, 1, s, &private->master_vol_ctl);
2956 /* Add volume controls */
2957 for (i = 0; i < num_line_out; i++) {
2958 int index = line_out_remap(private, i);
2961 if (info->line_out_descrs[i])
2962 snprintf(s, sizeof(s),
2963 "Line %02d (%s) Playback Volume",
2964 i + 1, info->line_out_descrs[i]);
2966 snprintf(s, sizeof(s),
2967 "Line %02d Playback Volume",
2969 err = scarlett2_add_new_ctl(mixer,
2970 &scarlett2_line_out_volume_ctl,
2971 i, 1, s, &private->vol_ctls[i]);
2976 snprintf(s, sizeof(s),
2977 "Line %02d Mute Playback Switch",
2979 err = scarlett2_add_new_ctl(mixer,
2980 &scarlett2_mute_ctl,
2982 &private->mute_ctls[i]);
2986 /* Make the fader and mute controls read-only if the
2987 * SW/HW switch is set to HW
2989 if (private->vol_sw_hw_switch[index])
2990 scarlett2_vol_ctl_set_writable(mixer, i, 0);
2993 if (info->line_out_hw_vol) {
2994 snprintf(s, sizeof(s),
2995 "Line Out %02d Volume Control Playback Enum",
2997 err = scarlett2_add_new_ctl(mixer,
2998 &scarlett2_sw_hw_enum_ctl,
3000 &private->sw_hw_ctls[i]);
3004 /* Make the switch read-only if the line is
3005 * involved in speaker switching
3007 if (private->speaker_switching_switch && i < 4)
3008 scarlett2_sw_hw_ctl_ro(private, i);
3012 /* Add dim/mute controls */
3013 if (info->line_out_hw_vol)
3014 for (i = 0; i < SCARLETT2_DIM_MUTE_COUNT; i++) {
3015 err = scarlett2_add_new_ctl(
3016 mixer, &scarlett2_dim_mute_ctl,
3017 i, 1, scarlett2_dim_mute_names[i],
3018 &private->dim_mute_ctls[i]);
3026 /*** Create the analogue input controls ***/
3028 static int scarlett2_add_line_in_ctls(struct usb_mixer_interface *mixer)
3030 struct scarlett2_data *private = mixer->private_data;
3031 const struct scarlett2_device_info *info = private->info;
3033 char s[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
3034 const char *fmt = "Line In %d %s Capture %s";
3035 const char *fmt2 = "Line In %d-%d %s Capture %s";
3037 /* Add input level (line/inst) controls */
3038 for (i = 0; i < info->level_input_count; i++) {
3039 snprintf(s, sizeof(s), fmt, i + 1 + info->level_input_first,
3041 err = scarlett2_add_new_ctl(mixer, &scarlett2_level_enum_ctl,
3042 i, 1, s, &private->level_ctls[i]);
3047 /* Add input pad controls */
3048 for (i = 0; i < info->pad_input_count; i++) {
3049 snprintf(s, sizeof(s), fmt, i + 1, "Pad", "Switch");
3050 err = scarlett2_add_new_ctl(mixer, &scarlett2_pad_ctl,
3051 i, 1, s, &private->pad_ctls[i]);
3056 /* Add input air controls */
3057 for (i = 0; i < info->air_input_count; i++) {
3058 snprintf(s, sizeof(s), fmt, i + 1, "Air", "Switch");
3059 err = scarlett2_add_new_ctl(mixer, &scarlett2_air_ctl,
3060 i, 1, s, &private->air_ctls[i]);
3065 /* Add input phantom controls */
3066 if (info->inputs_per_phantom == 1) {
3067 for (i = 0; i < info->phantom_count; i++) {
3068 snprintf(s, sizeof(s), fmt, i + 1,
3069 "Phantom Power", "Switch");
3070 err = scarlett2_add_new_ctl(
3071 mixer, &scarlett2_phantom_ctl,
3072 i, 1, s, &private->phantom_ctls[i]);
3076 } else if (info->inputs_per_phantom > 1) {
3077 for (i = 0; i < info->phantom_count; i++) {
3078 int from = i * info->inputs_per_phantom + 1;
3079 int to = (i + 1) * info->inputs_per_phantom;
3081 snprintf(s, sizeof(s), fmt2, from, to,
3082 "Phantom Power", "Switch");
3083 err = scarlett2_add_new_ctl(
3084 mixer, &scarlett2_phantom_ctl,
3085 i, 1, s, &private->phantom_ctls[i]);
3090 if (info->phantom_count) {
3091 err = scarlett2_add_new_ctl(
3092 mixer, &scarlett2_phantom_persistence_ctl, 0, 1,
3093 "Phantom Power Persistence Capture Switch", NULL);
3101 /*** Mixer Volume Controls ***/
3103 static int scarlett2_mixer_ctl_info(struct snd_kcontrol *kctl,
3104 struct snd_ctl_elem_info *uinfo)
3106 struct usb_mixer_elem_info *elem = kctl->private_data;
3108 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
3109 uinfo->count = elem->channels;
3110 uinfo->value.integer.min = 0;
3111 uinfo->value.integer.max = SCARLETT2_MIXER_MAX_VALUE;
3112 uinfo->value.integer.step = 1;
3116 static int scarlett2_mixer_ctl_get(struct snd_kcontrol *kctl,
3117 struct snd_ctl_elem_value *ucontrol)
3119 struct usb_mixer_elem_info *elem = kctl->private_data;
3120 struct scarlett2_data *private = elem->head.mixer->private_data;
3122 ucontrol->value.integer.value[0] = private->mix[elem->control];
3126 static int scarlett2_mixer_ctl_put(struct snd_kcontrol *kctl,
3127 struct snd_ctl_elem_value *ucontrol)
3129 struct usb_mixer_elem_info *elem = kctl->private_data;
3130 struct usb_mixer_interface *mixer = elem->head.mixer;
3131 struct scarlett2_data *private = mixer->private_data;
3132 const struct scarlett2_device_info *info = private->info;
3133 const int (*port_count)[SCARLETT2_PORT_DIRNS] = info->port_count;
3134 int oval, val, num_mixer_in, mix_num, err = 0;
3135 int index = elem->control;
3137 mutex_lock(&private->data_mutex);
3139 oval = private->mix[index];
3140 val = ucontrol->value.integer.value[0];
3141 num_mixer_in = port_count[SCARLETT2_PORT_TYPE_MIX][SCARLETT2_PORT_OUT];
3142 mix_num = index / num_mixer_in;
3147 private->mix[index] = val;
3148 err = scarlett2_usb_set_mix(mixer, mix_num);
3153 mutex_unlock(&private->data_mutex);
3157 static const DECLARE_TLV_DB_MINMAX(
3158 db_scale_scarlett2_mixer,
3159 SCARLETT2_MIXER_MIN_DB * 100,
3160 SCARLETT2_MIXER_MAX_DB * 100
3163 static const struct snd_kcontrol_new scarlett2_mixer_ctl = {
3164 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3165 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE |
3166 SNDRV_CTL_ELEM_ACCESS_TLV_READ,
3168 .info = scarlett2_mixer_ctl_info,
3169 .get = scarlett2_mixer_ctl_get,
3170 .put = scarlett2_mixer_ctl_put,
3171 .private_value = SCARLETT2_MIXER_MAX_DB, /* max value */
3172 .tlv = { .p = db_scale_scarlett2_mixer }
3175 static int scarlett2_add_mixer_ctls(struct usb_mixer_interface *mixer)
3177 struct scarlett2_data *private = mixer->private_data;
3178 const struct scarlett2_device_info *info = private->info;
3179 const int (*port_count)[SCARLETT2_PORT_DIRNS] = info->port_count;
3182 char s[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
3185 port_count[SCARLETT2_PORT_TYPE_MIX][SCARLETT2_PORT_OUT];
3187 port_count[SCARLETT2_PORT_TYPE_MIX][SCARLETT2_PORT_IN];
3189 for (i = 0, index = 0; i < num_outputs; i++)
3190 for (j = 0; j < num_inputs; j++, index++) {
3191 snprintf(s, sizeof(s),
3192 "Mix %c Input %02d Playback Volume",
3194 err = scarlett2_add_new_ctl(mixer, &scarlett2_mixer_ctl,
3203 /*** Mux Source Selection Controls ***/
3205 static int scarlett2_mux_src_enum_ctl_info(struct snd_kcontrol *kctl,
3206 struct snd_ctl_elem_info *uinfo)
3208 struct usb_mixer_elem_info *elem = kctl->private_data;
3209 struct scarlett2_data *private = elem->head.mixer->private_data;
3210 const struct scarlett2_device_info *info = private->info;
3211 const int (*port_count)[SCARLETT2_PORT_DIRNS] = info->port_count;
3212 unsigned int item = uinfo->value.enumerated.item;
3213 int items = private->num_mux_srcs;
3216 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
3217 uinfo->count = elem->channels;
3218 uinfo->value.enumerated.items = items;
3221 item = uinfo->value.enumerated.item = items - 1;
3224 port_type < SCARLETT2_PORT_TYPE_COUNT;
3226 if (item < port_count[port_type][SCARLETT2_PORT_IN]) {
3227 const struct scarlett2_port *port =
3228 &scarlett2_ports[port_type];
3230 sprintf(uinfo->value.enumerated.name,
3231 port->src_descr, item + port->src_num_offset);
3234 item -= port_count[port_type][SCARLETT2_PORT_IN];
3240 static int scarlett2_mux_src_enum_ctl_get(struct snd_kcontrol *kctl,
3241 struct snd_ctl_elem_value *ucontrol)
3243 struct usb_mixer_elem_info *elem = kctl->private_data;
3244 struct usb_mixer_interface *mixer = elem->head.mixer;
3245 struct scarlett2_data *private = mixer->private_data;
3246 const struct scarlett2_device_info *info = private->info;
3247 const int (*port_count)[SCARLETT2_PORT_DIRNS] = info->port_count;
3248 int line_out_count =
3249 port_count[SCARLETT2_PORT_TYPE_ANALOGUE][SCARLETT2_PORT_OUT];
3250 int index = elem->control;
3252 if (index < line_out_count)
3253 index = line_out_remap(private, index);
3255 mutex_lock(&private->data_mutex);
3256 if (private->mux_updated)
3257 scarlett2_usb_get_mux(mixer);
3258 ucontrol->value.enumerated.item[0] = private->mux[index];
3259 mutex_unlock(&private->data_mutex);
3264 static int scarlett2_mux_src_enum_ctl_put(struct snd_kcontrol *kctl,
3265 struct snd_ctl_elem_value *ucontrol)
3267 struct usb_mixer_elem_info *elem = kctl->private_data;
3268 struct usb_mixer_interface *mixer = elem->head.mixer;
3269 struct scarlett2_data *private = mixer->private_data;
3270 const struct scarlett2_device_info *info = private->info;
3271 const int (*port_count)[SCARLETT2_PORT_DIRNS] = info->port_count;
3272 int line_out_count =
3273 port_count[SCARLETT2_PORT_TYPE_ANALOGUE][SCARLETT2_PORT_OUT];
3274 int index = elem->control;
3275 int oval, val, err = 0;
3277 if (index < line_out_count)
3278 index = line_out_remap(private, index);
3280 mutex_lock(&private->data_mutex);
3282 oval = private->mux[index];
3283 val = min(ucontrol->value.enumerated.item[0],
3284 private->num_mux_srcs - 1U);
3289 private->mux[index] = val;
3290 err = scarlett2_usb_set_mux(mixer);
3295 mutex_unlock(&private->data_mutex);
3299 static const struct snd_kcontrol_new scarlett2_mux_src_enum_ctl = {
3300 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3302 .info = scarlett2_mux_src_enum_ctl_info,
3303 .get = scarlett2_mux_src_enum_ctl_get,
3304 .put = scarlett2_mux_src_enum_ctl_put,
3307 static int scarlett2_add_mux_enums(struct usb_mixer_interface *mixer)
3309 struct scarlett2_data *private = mixer->private_data;
3310 const struct scarlett2_device_info *info = private->info;
3311 const int (*port_count)[SCARLETT2_PORT_DIRNS] = info->port_count;
3312 int port_type, channel, i;
3314 for (i = 0, port_type = 0;
3315 port_type < SCARLETT2_PORT_TYPE_COUNT;
3318 channel < port_count[port_type][SCARLETT2_PORT_OUT];
3321 char s[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
3322 const char *const descr =
3323 scarlett2_ports[port_type].dst_descr;
3325 snprintf(s, sizeof(s) - 5, descr, channel + 1);
3328 err = scarlett2_add_new_ctl(mixer,
3329 &scarlett2_mux_src_enum_ctl,
3331 &private->mux_ctls[i]);
3340 /*** Meter Controls ***/
3342 static int scarlett2_meter_ctl_info(struct snd_kcontrol *kctl,
3343 struct snd_ctl_elem_info *uinfo)
3345 struct usb_mixer_elem_info *elem = kctl->private_data;
3347 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
3348 uinfo->count = elem->channels;
3349 uinfo->value.integer.min = 0;
3350 uinfo->value.integer.max = 4095;
3351 uinfo->value.integer.step = 1;
3355 static int scarlett2_meter_ctl_get(struct snd_kcontrol *kctl,
3356 struct snd_ctl_elem_value *ucontrol)
3358 struct usb_mixer_elem_info *elem = kctl->private_data;
3359 u16 meter_levels[SCARLETT2_MAX_METERS];
3362 err = scarlett2_usb_get_meter_levels(elem->head.mixer, elem->channels,
3367 for (i = 0; i < elem->channels; i++)
3368 ucontrol->value.integer.value[i] = meter_levels[i];
3373 static const struct snd_kcontrol_new scarlett2_meter_ctl = {
3374 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
3375 .access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE,
3377 .info = scarlett2_meter_ctl_info,
3378 .get = scarlett2_meter_ctl_get
3381 static int scarlett2_add_meter_ctl(struct usb_mixer_interface *mixer)
3383 struct scarlett2_data *private = mixer->private_data;
3385 /* devices without a mixer also don't support reporting levels */
3386 if (!private->info->has_mixer)
3389 return scarlett2_add_new_ctl(mixer, &scarlett2_meter_ctl,
3390 0, private->num_mux_dsts,
3391 "Level Meter", NULL);
3394 /*** MSD Controls ***/
3396 static int scarlett2_msd_ctl_get(struct snd_kcontrol *kctl,
3397 struct snd_ctl_elem_value *ucontrol)
3399 struct usb_mixer_elem_info *elem = kctl->private_data;
3400 struct scarlett2_data *private = elem->head.mixer->private_data;
3402 ucontrol->value.integer.value[0] = private->msd_switch;
3406 static int scarlett2_msd_ctl_put(struct snd_kcontrol *kctl,
3407 struct snd_ctl_elem_value *ucontrol)
3409 struct usb_mixer_elem_info *elem = kctl->private_data;
3410 struct usb_mixer_interface *mixer = elem->head.mixer;
3411 struct scarlett2_data *private = mixer->private_data;
3413 int oval, val, err = 0;
3415 mutex_lock(&private->data_mutex);
3417 oval = private->msd_switch;
3418 val = !!ucontrol->value.integer.value[0];
3423 private->msd_switch = val;
3425 /* Send switch change to the device */
3426 err = scarlett2_usb_set_config(mixer, SCARLETT2_CONFIG_MSD_SWITCH,
3432 mutex_unlock(&private->data_mutex);
3436 static const struct snd_kcontrol_new scarlett2_msd_ctl = {
3437 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3439 .info = snd_ctl_boolean_mono_info,
3440 .get = scarlett2_msd_ctl_get,
3441 .put = scarlett2_msd_ctl_put,
3444 static int scarlett2_add_msd_ctl(struct usb_mixer_interface *mixer)
3446 struct scarlett2_data *private = mixer->private_data;
3447 const struct scarlett2_device_info *info = private->info;
3449 if (!info->has_msd_mode)
3452 /* If MSD mode is off, hide the switch by default */
3453 if (!private->msd_switch && !(mixer->chip->setup & SCARLETT2_MSD_ENABLE))
3456 /* Add MSD control */
3457 return scarlett2_add_new_ctl(mixer, &scarlett2_msd_ctl,
3458 0, 1, "MSD Mode", NULL);
3461 /*** Cleanup/Suspend Callbacks ***/
3463 static void scarlett2_private_free(struct usb_mixer_interface *mixer)
3465 struct scarlett2_data *private = mixer->private_data;
3467 cancel_delayed_work_sync(&private->work);
3469 mixer->private_data = NULL;
3472 static void scarlett2_private_suspend(struct usb_mixer_interface *mixer)
3474 struct scarlett2_data *private = mixer->private_data;
3476 if (cancel_delayed_work_sync(&private->work))
3477 scarlett2_config_save(private->mixer);
3480 /*** Initialisation ***/
3482 static void scarlett2_count_mux_io(struct scarlett2_data *private)
3484 const struct scarlett2_device_info *info = private->info;
3485 const int (*port_count)[SCARLETT2_PORT_DIRNS] = info->port_count;
3486 int port_type, srcs = 0, dsts = 0;
3489 port_type < SCARLETT2_PORT_TYPE_COUNT;
3491 srcs += port_count[port_type][SCARLETT2_PORT_IN];
3492 dsts += port_count[port_type][SCARLETT2_PORT_OUT];
3495 private->num_mux_srcs = srcs;
3496 private->num_mux_dsts = dsts;
3499 /* Look through the interface descriptors for the Focusrite Control
3500 * interface (bInterfaceClass = 255 Vendor Specific Class) and set
3501 * bInterfaceNumber, bEndpointAddress, wMaxPacketSize, and bInterval
3504 static int scarlett2_find_fc_interface(struct usb_device *dev,
3505 struct scarlett2_data *private)
3507 struct usb_host_config *config = dev->actconfig;
3510 for (i = 0; i < config->desc.bNumInterfaces; i++) {
3511 struct usb_interface *intf = config->interface[i];
3512 struct usb_interface_descriptor *desc =
3513 &intf->altsetting[0].desc;
3514 struct usb_endpoint_descriptor *epd;
3516 if (desc->bInterfaceClass != 255)
3519 epd = get_endpoint(intf->altsetting, 0);
3520 private->bInterfaceNumber = desc->bInterfaceNumber;
3521 private->bEndpointAddress = epd->bEndpointAddress &
3522 USB_ENDPOINT_NUMBER_MASK;
3523 private->wMaxPacketSize = le16_to_cpu(epd->wMaxPacketSize);
3524 private->bInterval = epd->bInterval;
3531 /* Initialise private data */
3532 static int scarlett2_init_private(struct usb_mixer_interface *mixer,
3533 const struct scarlett2_device_info *info)
3535 struct scarlett2_data *private =
3536 kzalloc(sizeof(struct scarlett2_data), GFP_KERNEL);
3541 mutex_init(&private->usb_mutex);
3542 mutex_init(&private->data_mutex);
3543 INIT_DELAYED_WORK(&private->work, scarlett2_config_save_work);
3545 mixer->private_data = private;
3546 mixer->private_free = scarlett2_private_free;
3547 mixer->private_suspend = scarlett2_private_suspend;
3549 private->info = info;
3550 scarlett2_count_mux_io(private);
3551 private->scarlett2_seq = 0;
3552 private->mixer = mixer;
3554 return scarlett2_find_fc_interface(mixer->chip->dev, private);
3557 /* Cargo cult proprietary initialisation sequence */
3558 static int scarlett2_usb_init(struct usb_mixer_interface *mixer)
3560 struct usb_device *dev = mixer->chip->dev;
3561 struct scarlett2_data *private = mixer->private_data;
3565 if (usb_pipe_type_check(dev, usb_sndctrlpipe(dev, 0)))
3569 err = scarlett2_usb_rx(dev, private->bInterfaceNumber,
3570 SCARLETT2_USB_CMD_INIT, buf, sizeof(buf));
3575 private->scarlett2_seq = 1;
3576 err = scarlett2_usb(mixer, SCARLETT2_USB_INIT_1, NULL, 0, NULL, 0);
3581 private->scarlett2_seq = 1;
3582 return scarlett2_usb(mixer, SCARLETT2_USB_INIT_2, NULL, 0, NULL, 84);
3585 /* Read configuration from the interface on start */
3586 static int scarlett2_read_configs(struct usb_mixer_interface *mixer)
3588 struct scarlett2_data *private = mixer->private_data;
3589 const struct scarlett2_device_info *info = private->info;
3590 const int (*port_count)[SCARLETT2_PORT_DIRNS] = info->port_count;
3592 port_count[SCARLETT2_PORT_TYPE_ANALOGUE][SCARLETT2_PORT_OUT];
3594 port_count[SCARLETT2_PORT_TYPE_MIX][SCARLETT2_PORT_IN];
3595 struct scarlett2_usb_volume_status volume_status;
3598 if (info->has_msd_mode) {
3599 err = scarlett2_usb_get_config(
3600 mixer, SCARLETT2_CONFIG_MSD_SWITCH,
3601 1, &private->msd_switch);
3605 /* no other controls are created if MSD mode is on */
3606 if (private->msd_switch)
3610 err = scarlett2_update_input_other(mixer);
3614 err = scarlett2_update_monitor_other(mixer);
3618 /* the rest of the configuration is for devices with a mixer */
3619 if (!info->has_mixer)
3622 err = scarlett2_update_sync(mixer);
3626 err = scarlett2_usb_get_volume_status(mixer, &volume_status);
3630 if (info->line_out_hw_vol)
3631 for (i = 0; i < SCARLETT2_DIM_MUTE_COUNT; i++)
3632 private->dim_mute[i] = !!volume_status.dim_mute[i];
3634 private->master_vol = clamp(
3635 volume_status.master_vol + SCARLETT2_VOLUME_BIAS,
3636 0, SCARLETT2_VOLUME_BIAS);
3638 for (i = 0; i < num_line_out; i++) {
3641 private->vol_sw_hw_switch[i] =
3642 info->line_out_hw_vol
3643 && volume_status.sw_hw_switch[i];
3645 volume = private->vol_sw_hw_switch[i]
3646 ? volume_status.master_vol
3647 : volume_status.sw_vol[i];
3648 volume = clamp(volume + SCARLETT2_VOLUME_BIAS,
3649 0, SCARLETT2_VOLUME_BIAS);
3650 private->vol[i] = volume;
3652 mute = private->vol_sw_hw_switch[i]
3653 ? private->dim_mute[SCARLETT2_BUTTON_MUTE]
3654 : volume_status.mute_switch[i];
3655 private->mute_switch[i] = mute;
3658 for (i = 0; i < num_mixer_out; i++) {
3659 err = scarlett2_usb_get_mix(mixer, i);
3664 return scarlett2_usb_get_mux(mixer);
3667 /* Notify on sync change */
3668 static void scarlett2_notify_sync(
3669 struct usb_mixer_interface *mixer)
3671 struct scarlett2_data *private = mixer->private_data;
3673 private->sync_updated = 1;
3675 snd_ctl_notify(mixer->chip->card, SNDRV_CTL_EVENT_MASK_VALUE,
3676 &private->sync_ctl->id);
3679 /* Notify on monitor change */
3680 static void scarlett2_notify_monitor(
3681 struct usb_mixer_interface *mixer)
3683 struct snd_card *card = mixer->chip->card;
3684 struct scarlett2_data *private = mixer->private_data;
3685 const struct scarlett2_device_info *info = private->info;
3686 const int (*port_count)[SCARLETT2_PORT_DIRNS] = info->port_count;
3688 port_count[SCARLETT2_PORT_TYPE_ANALOGUE][SCARLETT2_PORT_OUT];
3691 /* if line_out_hw_vol is 0, there are no controls to update */
3692 if (!info->line_out_hw_vol)
3695 private->vol_updated = 1;
3697 snd_ctl_notify(mixer->chip->card, SNDRV_CTL_EVENT_MASK_VALUE,
3698 &private->master_vol_ctl->id);
3700 for (i = 0; i < num_line_out; i++)
3701 if (private->vol_sw_hw_switch[line_out_remap(private, i)])
3702 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE,
3703 &private->vol_ctls[i]->id);
3706 /* Notify on dim/mute change */
3707 static void scarlett2_notify_dim_mute(
3708 struct usb_mixer_interface *mixer)
3710 struct snd_card *card = mixer->chip->card;
3711 struct scarlett2_data *private = mixer->private_data;
3712 const struct scarlett2_device_info *info = private->info;
3713 const int (*port_count)[SCARLETT2_PORT_DIRNS] = info->port_count;
3715 port_count[SCARLETT2_PORT_TYPE_ANALOGUE][SCARLETT2_PORT_OUT];
3718 private->vol_updated = 1;
3720 if (!info->line_out_hw_vol)
3723 for (i = 0; i < SCARLETT2_DIM_MUTE_COUNT; i++)
3724 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE,
3725 &private->dim_mute_ctls[i]->id);
3727 for (i = 0; i < num_line_out; i++)
3728 if (private->vol_sw_hw_switch[line_out_remap(private, i)])
3729 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE,
3730 &private->mute_ctls[i]->id);
3733 /* Notify on "input other" change (level/pad/air) */
3734 static void scarlett2_notify_input_other(
3735 struct usb_mixer_interface *mixer)
3737 struct snd_card *card = mixer->chip->card;
3738 struct scarlett2_data *private = mixer->private_data;
3739 const struct scarlett2_device_info *info = private->info;
3742 private->input_other_updated = 1;
3744 for (i = 0; i < info->level_input_count; i++)
3745 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE,
3746 &private->level_ctls[i]->id);
3747 for (i = 0; i < info->pad_input_count; i++)
3748 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE,
3749 &private->pad_ctls[i]->id);
3750 for (i = 0; i < info->air_input_count; i++)
3751 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE,
3752 &private->air_ctls[i]->id);
3753 for (i = 0; i < info->phantom_count; i++)
3754 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE,
3755 &private->phantom_ctls[i]->id);
3758 /* Notify on "monitor other" change (direct monitor, speaker
3759 * switching, talkback)
3761 static void scarlett2_notify_monitor_other(
3762 struct usb_mixer_interface *mixer)
3764 struct snd_card *card = mixer->chip->card;
3765 struct scarlett2_data *private = mixer->private_data;
3766 const struct scarlett2_device_info *info = private->info;
3768 private->monitor_other_updated = 1;
3770 if (info->direct_monitor) {
3771 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE,
3772 &private->direct_monitor_ctl->id);
3776 if (info->has_speaker_switching)
3777 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE,
3778 &private->speaker_switching_ctl->id);
3780 if (info->has_talkback)
3781 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE,
3782 &private->talkback_ctl->id);
3784 /* if speaker switching was recently enabled or disabled,
3785 * invalidate the dim/mute and mux enum controls
3787 if (private->speaker_switching_switched) {
3790 scarlett2_notify_dim_mute(mixer);
3792 private->speaker_switching_switched = 0;
3793 private->mux_updated = 1;
3795 for (i = 0; i < private->num_mux_dsts; i++)
3796 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE,
3797 &private->mux_ctls[i]->id);
3801 /* Interrupt callback */
3802 static void scarlett2_notify(struct urb *urb)
3804 struct usb_mixer_interface *mixer = urb->context;
3805 int len = urb->actual_length;
3806 int ustatus = urb->status;
3809 if (ustatus != 0 || len != 8)
3812 data = le32_to_cpu(*(__le32 *)urb->transfer_buffer);
3813 if (data & SCARLETT2_USB_NOTIFY_SYNC)
3814 scarlett2_notify_sync(mixer);
3815 if (data & SCARLETT2_USB_NOTIFY_MONITOR)
3816 scarlett2_notify_monitor(mixer);
3817 if (data & SCARLETT2_USB_NOTIFY_DIM_MUTE)
3818 scarlett2_notify_dim_mute(mixer);
3819 if (data & SCARLETT2_USB_NOTIFY_INPUT_OTHER)
3820 scarlett2_notify_input_other(mixer);
3821 if (data & SCARLETT2_USB_NOTIFY_MONITOR_OTHER)
3822 scarlett2_notify_monitor_other(mixer);
3825 if (ustatus != -ENOENT &&
3826 ustatus != -ECONNRESET &&
3827 ustatus != -ESHUTDOWN) {
3828 urb->dev = mixer->chip->dev;
3829 usb_submit_urb(urb, GFP_ATOMIC);
3833 static int scarlett2_init_notify(struct usb_mixer_interface *mixer)
3835 struct usb_device *dev = mixer->chip->dev;
3836 struct scarlett2_data *private = mixer->private_data;
3837 unsigned int pipe = usb_rcvintpipe(dev, private->bEndpointAddress);
3838 void *transfer_buffer;
3841 usb_audio_err(mixer->chip,
3842 "%s: mixer urb already in use!\n", __func__);
3846 if (usb_pipe_type_check(dev, pipe))
3849 mixer->urb = usb_alloc_urb(0, GFP_KERNEL);
3853 transfer_buffer = kmalloc(private->wMaxPacketSize, GFP_KERNEL);
3854 if (!transfer_buffer)
3857 usb_fill_int_urb(mixer->urb, dev, pipe,
3858 transfer_buffer, private->wMaxPacketSize,
3859 scarlett2_notify, mixer, private->bInterval);
3861 return usb_submit_urb(mixer->urb, GFP_KERNEL);
3864 static int snd_scarlett_gen2_controls_create(struct usb_mixer_interface *mixer)
3866 const struct scarlett2_device_info **info = scarlett2_devices;
3869 /* Find device in scarlett2_devices */
3870 while (*info && (*info)->usb_id != mixer->chip->usb_id)
3875 /* Initialise private data */
3876 err = scarlett2_init_private(mixer, *info);
3880 /* Send proprietary USB initialisation sequence */
3881 err = scarlett2_usb_init(mixer);
3885 /* Read volume levels and controls from the interface */
3886 err = scarlett2_read_configs(mixer);
3890 /* Create the MSD control */
3891 err = scarlett2_add_msd_ctl(mixer);
3895 /* If MSD mode is enabled, don't create any other controls */
3896 if (((struct scarlett2_data *)mixer->private_data)->msd_switch)
3899 /* Create the analogue output controls */
3900 err = scarlett2_add_line_out_ctls(mixer);
3904 /* Create the analogue input controls */
3905 err = scarlett2_add_line_in_ctls(mixer);
3909 /* Create the input, output, and mixer mux input selections */
3910 err = scarlett2_add_mux_enums(mixer);
3914 /* Create the matrix mixer controls */
3915 err = scarlett2_add_mixer_ctls(mixer);
3919 /* Create the level meter controls */
3920 err = scarlett2_add_meter_ctl(mixer);
3924 /* Create the sync control */
3925 err = scarlett2_add_sync_ctl(mixer);
3929 /* Create the direct monitor control */
3930 err = scarlett2_add_direct_monitor_ctl(mixer);
3934 /* Create the speaker switching control */
3935 err = scarlett2_add_speaker_switch_ctl(mixer);
3939 /* Create the talkback controls */
3940 err = scarlett2_add_talkback_ctls(mixer);
3944 /* Set up the interrupt polling */
3945 err = scarlett2_init_notify(mixer);
3952 int snd_scarlett_gen2_init(struct usb_mixer_interface *mixer)
3954 struct snd_usb_audio *chip = mixer->chip;
3957 /* only use UAC_VERSION_2 */
3958 if (!mixer->protocol)
3961 if (!(chip->setup & SCARLETT2_ENABLE)) {
3962 usb_audio_info(chip,
3963 "Focusrite Scarlett Gen 2/3 Mixer Driver disabled; "
3964 "use options snd_usb_audio vid=0x%04x pid=0x%04x "
3965 "device_setup=1 to enable and report any issues "
3967 USB_ID_VENDOR(chip->usb_id),
3968 USB_ID_PRODUCT(chip->usb_id));
3972 usb_audio_info(chip,
3973 "Focusrite Scarlett Gen 2/3 Mixer Driver enabled pid=0x%04x",
3974 USB_ID_PRODUCT(chip->usb_id));
3976 err = snd_scarlett_gen2_controls_create(mixer);
3978 usb_audio_err(mixer->chip,
3979 "Error initialising Scarlett Mixer Driver: %d",