1 // SPDX-License-Identifier: GPL-2.0
3 * Focusrite Scarlett Gen 2/3 Driver for ALSA
6 * - 6i6/18i8/18i20 Gen 2
7 * - 4i4/8i6/18i8/18i20 Gen 3
9 * Copyright (c) 2018-2021 by Geoffrey D. Bennett <g at b4.vu>
11 * Based on the Scarlett (Gen 1) Driver for ALSA:
13 * Copyright (c) 2013 by Tobias Hoffmann
14 * Copyright (c) 2013 by Robin Gareus <robin at gareus.org>
15 * Copyright (c) 2002 by Takashi Iwai <tiwai at suse.de>
16 * Copyright (c) 2014 by Chris J Arges <chris.j.arges at canonical.com>
18 * Many codes borrowed from audio.c by
19 * Alan Cox (alan at lxorguk.ukuu.org.uk)
20 * Thomas Sailer (sailer at ife.ee.ethz.ch)
23 * David Henningsson <david.henningsson at canonical.com>
26 /* The protocol was reverse engineered by looking at the communication
27 * between Focusrite Control 2.3.4 and the Focusrite(R) Scarlett 18i20
28 * (firmware 1083) using usbmon in July-August 2018.
30 * Scarlett 18i8 support added in April 2019.
32 * Scarlett 6i6 support added in June 2019 (thanks to Martin Wittmann
33 * for providing usbmon output and testing).
35 * Scarlett 4i4/8i6 Gen 3 support added in May 2020 (thanks to Laurent
36 * Debricon for donating a 4i4 and to Fredrik Unger for providing 8i6
37 * usbmon output and testing).
39 * Scarlett 18i8/18i20 Gen 3 support added in June 2020 (thanks to
40 * Darren Jaeckel, Alex Sedlack, and Clovis Lunel for providing usbmon
41 * output, protocol traces and testing).
43 * Support for loading mixer volume and mux configuration from the
44 * interface during driver initialisation added in May 2021 (thanks to
45 * Vladimir Sadovnikov for figuring out how).
47 * This ALSA mixer gives access to:
48 * - input, output, mixer-matrix muxes
49 * - mixer-matrix gain stages
50 * - gain/volume/mute controls
52 * - line/inst level and pad controls
55 * /--------------\ 18chn 20chn /--------------\
56 * | Hardware in +--+------\ /-------------+--+ ALSA PCM out |
57 * \--------------/ | | | | \--------------/
61 * | +---------------+ | |
62 * | \ Matrix Mux / | |
69 * | +------------+ | |
73 * | | 18x10 Gain | | |
75 * | +-----+------+ | |
77 * |18chn |10chn | |20chn
82 * ===========================
83 * +---------------+ +--—------------+
84 * \ Output Mux / \ Capture Mux /
85 * +---+---+---+ +-----+-----+
89 * /--------------\ | | | /--------------\
90 * | S/PDIF, ADAT |<--/ |10chn \-->| ALSA PCM in |
91 * | Hardware out | | \--------------/
94 * +-------------+ Software gain per channel.
95 * | Master Gain |<-- 18i20 only: Switch per channel
96 * +------+------+ to select HW or SW gain control.
100 * | Analogue |<------/
107 #include <linux/slab.h>
108 #include <linux/usb.h>
109 #include <linux/moduleparam.h>
111 #include <sound/control.h>
112 #include <sound/tlv.h>
114 #include "usbaudio.h"
118 #include "mixer_scarlett_gen2.h"
120 /* device_setup value to enable */
121 #define SCARLETT2_ENABLE 0x01
123 /* some gui mixers can't handle negative ctl values */
124 #define SCARLETT2_VOLUME_BIAS 127
126 /* mixer range from -80dB to +6dB in 0.5dB steps */
127 #define SCARLETT2_MIXER_MIN_DB -80
128 #define SCARLETT2_MIXER_BIAS (-SCARLETT2_MIXER_MIN_DB * 2)
129 #define SCARLETT2_MIXER_MAX_DB 6
130 #define SCARLETT2_MIXER_MAX_VALUE \
131 ((SCARLETT2_MIXER_MAX_DB - SCARLETT2_MIXER_MIN_DB) * 2)
132 #define SCARLETT2_MIXER_VALUE_COUNT (SCARLETT2_MIXER_MAX_VALUE + 1)
134 /* map from (dB + 80) * 2 to mixer value
135 * for dB in 0 .. 172: int(8192 * pow(10, ((dB - 160) / 2 / 20)))
137 static const u16 scarlett2_mixer_values[SCARLETT2_MIXER_VALUE_COUNT] = {
138 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2,
139 2, 2, 3, 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 6, 6, 6, 7, 7, 8, 8,
140 9, 9, 10, 10, 11, 12, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21,
141 23, 24, 25, 27, 29, 30, 32, 34, 36, 38, 41, 43, 46, 48, 51,
142 54, 57, 61, 65, 68, 73, 77, 81, 86, 91, 97, 103, 109, 115,
143 122, 129, 137, 145, 154, 163, 173, 183, 194, 205, 217, 230,
144 244, 259, 274, 290, 307, 326, 345, 365, 387, 410, 434, 460,
145 487, 516, 547, 579, 614, 650, 689, 730, 773, 819, 867, 919,
146 973, 1031, 1092, 1157, 1225, 1298, 1375, 1456, 1543, 1634,
147 1731, 1833, 1942, 2057, 2179, 2308, 2445, 2590, 2744, 2906,
148 3078, 3261, 3454, 3659, 3876, 4105, 4349, 4606, 4879, 5168,
149 5475, 5799, 6143, 6507, 6892, 7301, 7733, 8192, 8677, 9191,
150 9736, 10313, 10924, 11571, 12257, 12983, 13752, 14567, 15430,
154 /* Maximum number of analogue outputs */
155 #define SCARLETT2_ANALOGUE_MAX 10
157 /* Maximum number of level and pad switches */
158 #define SCARLETT2_LEVEL_SWITCH_MAX 2
159 #define SCARLETT2_PAD_SWITCH_MAX 8
161 /* Maximum number of inputs to the mixer */
162 #define SCARLETT2_INPUT_MIX_MAX 25
164 /* Maximum number of outputs from the mixer */
165 #define SCARLETT2_OUTPUT_MIX_MAX 12
167 /* Maximum size of the data in the USB mux assignment message:
168 * 20 inputs, 20 outputs, 25 matrix inputs, 12 spare
170 #define SCARLETT2_MUX_MAX 77
172 /* Maximum number of meters (sum of output port counts) */
173 #define SCARLETT2_MAX_METERS 65
175 /* Hardware port types:
176 * - None (no input to mux)
184 SCARLETT2_PORT_TYPE_NONE = 0,
185 SCARLETT2_PORT_TYPE_ANALOGUE = 1,
186 SCARLETT2_PORT_TYPE_SPDIF = 2,
187 SCARLETT2_PORT_TYPE_ADAT = 3,
188 SCARLETT2_PORT_TYPE_MIX = 4,
189 SCARLETT2_PORT_TYPE_PCM = 5,
190 SCARLETT2_PORT_TYPE_COUNT = 6,
193 /* I/O count of each port type kept in struct scarlett2_ports */
195 SCARLETT2_PORT_IN = 0,
196 SCARLETT2_PORT_OUT = 1,
197 SCARLETT2_PORT_DIRNS = 2,
200 /* Dim/Mute buttons on the 18i20 */
202 SCARLETT2_BUTTON_MUTE = 0,
203 SCARLETT2_BUTTON_DIM = 1,
204 SCARLETT2_DIM_MUTE_COUNT = 2,
207 static const char *const scarlett2_dim_mute_names[SCARLETT2_DIM_MUTE_COUNT] = {
211 /* Description of each hardware port type:
212 * - id: hardware ID of this port type
213 * - src_descr: printf format string for mux input selections
214 * - src_num_offset: added to channel number for the fprintf
215 * - dst_descr: printf format string for mixer controls
217 struct scarlett2_port {
219 const char * const src_descr;
221 const char * const dst_descr;
224 static const struct scarlett2_port scarlett2_ports[SCARLETT2_PORT_TYPE_COUNT] = {
225 [SCARLETT2_PORT_TYPE_NONE] = {
229 [SCARLETT2_PORT_TYPE_ANALOGUE] = {
231 .src_descr = "Analogue %d",
233 .dst_descr = "Analogue Output %02d Playback"
235 [SCARLETT2_PORT_TYPE_SPDIF] = {
237 .src_descr = "S/PDIF %d",
239 .dst_descr = "S/PDIF Output %d Playback"
241 [SCARLETT2_PORT_TYPE_ADAT] = {
243 .src_descr = "ADAT %d",
245 .dst_descr = "ADAT Output %d Playback"
247 [SCARLETT2_PORT_TYPE_MIX] = {
249 .src_descr = "Mix %c",
250 .src_num_offset = 'A',
251 .dst_descr = "Mixer Input %02d Capture"
253 [SCARLETT2_PORT_TYPE_PCM] = {
255 .src_descr = "PCM %d",
257 .dst_descr = "PCM %02d Capture"
261 /* Number of mux tables: one for each band of sample rates
262 * (44.1/48kHz, 88.2/96kHz, and 176.4/176kHz)
264 #define SCARLETT2_MUX_TABLES 3
266 /* Maximum number of entries in a mux table */
267 #define SCARLETT2_MAX_MUX_ENTRIES 10
269 /* One entry within mux_assignment defines the port type and range of
270 * ports to add to the set_mux message. The end of the list is marked
273 struct scarlett2_mux_entry {
279 struct scarlett2_device_info {
280 u32 usb_id; /* USB device identifier */
282 /* line out hw volume is sw controlled */
285 /* the number of analogue inputs with a software switchable
286 * level control that can be set to line or instrument
288 u8 level_input_count;
290 /* the number of analogue inputs with a software switchable
295 /* additional description for the line out volume controls */
296 const char * const line_out_descrs[SCARLETT2_ANALOGUE_MAX];
298 /* number of sources/destinations of each port type */
299 const int port_count[SCARLETT2_PORT_TYPE_COUNT][SCARLETT2_PORT_DIRNS];
301 /* layout/order of the entries in the set_mux message */
302 struct scarlett2_mux_entry mux_assignment[SCARLETT2_MUX_TABLES]
303 [SCARLETT2_MAX_MUX_ENTRIES];
306 struct scarlett2_data {
307 struct usb_mixer_interface *mixer;
308 struct mutex usb_mutex; /* prevent sending concurrent USB requests */
309 struct mutex data_mutex; /* lock access to this data */
310 struct delayed_work work;
311 const struct scarlett2_device_info *info;
312 __u8 bInterfaceNumber;
313 __u8 bEndpointAddress;
314 __u16 wMaxPacketSize;
323 u8 vol[SCARLETT2_ANALOGUE_MAX];
324 u8 vol_sw_hw_switch[SCARLETT2_ANALOGUE_MAX];
325 u8 mute_switch[SCARLETT2_ANALOGUE_MAX];
326 u8 level_switch[SCARLETT2_LEVEL_SWITCH_MAX];
327 u8 pad_switch[SCARLETT2_PAD_SWITCH_MAX];
328 u8 dim_mute[SCARLETT2_DIM_MUTE_COUNT];
329 struct snd_kcontrol *sync_ctl;
330 struct snd_kcontrol *master_vol_ctl;
331 struct snd_kcontrol *vol_ctls[SCARLETT2_ANALOGUE_MAX];
332 struct snd_kcontrol *mute_ctls[SCARLETT2_ANALOGUE_MAX];
333 struct snd_kcontrol *dim_mute_ctls[SCARLETT2_DIM_MUTE_COUNT];
334 u8 mux[SCARLETT2_MUX_MAX];
335 u8 mix[SCARLETT2_INPUT_MIX_MAX * SCARLETT2_OUTPUT_MIX_MAX];
338 /*** Model-specific data ***/
340 static const struct scarlett2_device_info s6i6_gen2_info = {
341 .usb_id = USB_ID(0x1235, 0x8203),
343 .level_input_count = 2,
344 .pad_input_count = 2,
354 [SCARLETT2_PORT_TYPE_NONE] = { 1, 0 },
355 [SCARLETT2_PORT_TYPE_ANALOGUE] = { 4, 4 },
356 [SCARLETT2_PORT_TYPE_SPDIF] = { 2, 2 },
357 [SCARLETT2_PORT_TYPE_MIX] = { 10, 18 },
358 [SCARLETT2_PORT_TYPE_PCM] = { 6, 6 },
361 .mux_assignment = { {
362 { SCARLETT2_PORT_TYPE_PCM, 0, 6 },
363 { SCARLETT2_PORT_TYPE_ANALOGUE, 0, 4 },
364 { SCARLETT2_PORT_TYPE_SPDIF, 0, 2 },
365 { SCARLETT2_PORT_TYPE_MIX, 0, 18 },
366 { SCARLETT2_PORT_TYPE_NONE, 0, 8 },
369 { SCARLETT2_PORT_TYPE_PCM, 0, 6 },
370 { SCARLETT2_PORT_TYPE_ANALOGUE, 0, 4 },
371 { SCARLETT2_PORT_TYPE_SPDIF, 0, 2 },
372 { SCARLETT2_PORT_TYPE_MIX, 0, 18 },
373 { SCARLETT2_PORT_TYPE_NONE, 0, 8 },
376 { SCARLETT2_PORT_TYPE_PCM, 0, 6 },
377 { SCARLETT2_PORT_TYPE_ANALOGUE, 0, 4 },
378 { SCARLETT2_PORT_TYPE_SPDIF, 0, 2 },
379 { SCARLETT2_PORT_TYPE_MIX, 0, 18 },
380 { SCARLETT2_PORT_TYPE_NONE, 0, 8 },
385 static const struct scarlett2_device_info s18i8_gen2_info = {
386 .usb_id = USB_ID(0x1235, 0x8204),
388 .level_input_count = 2,
389 .pad_input_count = 4,
401 [SCARLETT2_PORT_TYPE_NONE] = { 1, 0 },
402 [SCARLETT2_PORT_TYPE_ANALOGUE] = { 8, 6 },
403 [SCARLETT2_PORT_TYPE_SPDIF] = { 2, 2 },
404 [SCARLETT2_PORT_TYPE_ADAT] = { 8, 0 },
405 [SCARLETT2_PORT_TYPE_MIX] = { 10, 18 },
406 [SCARLETT2_PORT_TYPE_PCM] = { 8, 18 },
409 .mux_assignment = { {
410 { SCARLETT2_PORT_TYPE_PCM, 0, 18 },
411 { SCARLETT2_PORT_TYPE_ANALOGUE, 0, 6 },
412 { SCARLETT2_PORT_TYPE_SPDIF, 0, 2 },
413 { SCARLETT2_PORT_TYPE_MIX, 0, 18 },
414 { SCARLETT2_PORT_TYPE_NONE, 0, 8 },
417 { SCARLETT2_PORT_TYPE_PCM, 0, 14 },
418 { SCARLETT2_PORT_TYPE_ANALOGUE, 0, 6 },
419 { SCARLETT2_PORT_TYPE_SPDIF, 0, 2 },
420 { SCARLETT2_PORT_TYPE_MIX, 0, 18 },
421 { SCARLETT2_PORT_TYPE_NONE, 0, 8 },
424 { SCARLETT2_PORT_TYPE_PCM, 0, 10 },
425 { SCARLETT2_PORT_TYPE_ANALOGUE, 0, 6 },
426 { SCARLETT2_PORT_TYPE_SPDIF, 0, 2 },
427 { SCARLETT2_PORT_TYPE_MIX, 0, 18 },
428 { SCARLETT2_PORT_TYPE_NONE, 0, 4 },
433 static const struct scarlett2_device_info s18i20_gen2_info = {
434 .usb_id = USB_ID(0x1235, 0x8201),
436 .line_out_hw_vol = 1,
452 [SCARLETT2_PORT_TYPE_NONE] = { 1, 0 },
453 [SCARLETT2_PORT_TYPE_ANALOGUE] = { 8, 10 },
454 [SCARLETT2_PORT_TYPE_SPDIF] = { 2, 2 },
455 [SCARLETT2_PORT_TYPE_ADAT] = { 8, 8 },
456 [SCARLETT2_PORT_TYPE_MIX] = { 10, 18 },
457 [SCARLETT2_PORT_TYPE_PCM] = { 20, 18 },
460 .mux_assignment = { {
461 { SCARLETT2_PORT_TYPE_PCM, 0, 18 },
462 { SCARLETT2_PORT_TYPE_ANALOGUE, 0, 10 },
463 { SCARLETT2_PORT_TYPE_SPDIF, 0, 2 },
464 { SCARLETT2_PORT_TYPE_ADAT, 0, 8 },
465 { SCARLETT2_PORT_TYPE_MIX, 0, 18 },
466 { SCARLETT2_PORT_TYPE_NONE, 0, 8 },
469 { SCARLETT2_PORT_TYPE_PCM, 0, 14 },
470 { SCARLETT2_PORT_TYPE_ANALOGUE, 0, 10 },
471 { SCARLETT2_PORT_TYPE_SPDIF, 0, 2 },
472 { SCARLETT2_PORT_TYPE_ADAT, 0, 4 },
473 { SCARLETT2_PORT_TYPE_MIX, 0, 18 },
474 { SCARLETT2_PORT_TYPE_NONE, 0, 8 },
477 { SCARLETT2_PORT_TYPE_PCM, 0, 10 },
478 { SCARLETT2_PORT_TYPE_ANALOGUE, 0, 10 },
479 { SCARLETT2_PORT_TYPE_SPDIF, 0, 2 },
480 { SCARLETT2_PORT_TYPE_MIX, 0, 18 },
481 { SCARLETT2_PORT_TYPE_NONE, 0, 6 },
486 static const struct scarlett2_device_info s4i4_gen3_info = {
487 .usb_id = USB_ID(0x1235, 0x8212),
489 .level_input_count = 2,
490 .pad_input_count = 2,
500 [SCARLETT2_PORT_TYPE_NONE] = { 1, 0 },
501 [SCARLETT2_PORT_TYPE_ANALOGUE] = { 4, 4 },
502 [SCARLETT2_PORT_TYPE_MIX] = { 6, 8 },
503 [SCARLETT2_PORT_TYPE_PCM] = { 4, 6 },
506 .mux_assignment = { {
507 { SCARLETT2_PORT_TYPE_PCM, 0, 6 },
508 { SCARLETT2_PORT_TYPE_ANALOGUE, 0, 4 },
509 { SCARLETT2_PORT_TYPE_MIX, 0, 8 },
510 { SCARLETT2_PORT_TYPE_NONE, 0, 16 },
513 { SCARLETT2_PORT_TYPE_PCM, 0, 6 },
514 { SCARLETT2_PORT_TYPE_ANALOGUE, 0, 4 },
515 { SCARLETT2_PORT_TYPE_MIX, 0, 8 },
516 { SCARLETT2_PORT_TYPE_NONE, 0, 16 },
519 { SCARLETT2_PORT_TYPE_PCM, 0, 6 },
520 { SCARLETT2_PORT_TYPE_ANALOGUE, 0, 4 },
521 { SCARLETT2_PORT_TYPE_MIX, 0, 8 },
522 { SCARLETT2_PORT_TYPE_NONE, 0, 16 },
527 static const struct scarlett2_device_info s8i6_gen3_info = {
528 .usb_id = USB_ID(0x1235, 0x8213),
530 .level_input_count = 2,
531 .pad_input_count = 2,
541 [SCARLETT2_PORT_TYPE_NONE] = { 1, 0 },
542 [SCARLETT2_PORT_TYPE_ANALOGUE] = { 6, 4 },
543 [SCARLETT2_PORT_TYPE_SPDIF] = { 2, 2 },
544 [SCARLETT2_PORT_TYPE_MIX] = { 8, 8 },
545 [SCARLETT2_PORT_TYPE_PCM] = { 6, 10 },
548 .mux_assignment = { {
549 { SCARLETT2_PORT_TYPE_PCM, 0, 8 },
550 { SCARLETT2_PORT_TYPE_ANALOGUE, 0, 4 },
551 { SCARLETT2_PORT_TYPE_SPDIF, 0, 2 },
552 { SCARLETT2_PORT_TYPE_PCM, 8, 2 },
553 { SCARLETT2_PORT_TYPE_MIX, 0, 8 },
554 { SCARLETT2_PORT_TYPE_NONE, 0, 18 },
557 { SCARLETT2_PORT_TYPE_PCM, 0, 8 },
558 { SCARLETT2_PORT_TYPE_ANALOGUE, 0, 4 },
559 { SCARLETT2_PORT_TYPE_SPDIF, 0, 2 },
560 { SCARLETT2_PORT_TYPE_PCM, 8, 2 },
561 { SCARLETT2_PORT_TYPE_MIX, 0, 8 },
562 { SCARLETT2_PORT_TYPE_NONE, 0, 18 },
565 { SCARLETT2_PORT_TYPE_PCM, 0, 8 },
566 { SCARLETT2_PORT_TYPE_ANALOGUE, 0, 4 },
567 { SCARLETT2_PORT_TYPE_SPDIF, 0, 2 },
568 { SCARLETT2_PORT_TYPE_PCM, 8, 2 },
569 { SCARLETT2_PORT_TYPE_MIX, 0, 8 },
570 { SCARLETT2_PORT_TYPE_NONE, 0, 18 },
575 static const struct scarlett2_device_info s18i8_gen3_info = {
576 .usb_id = USB_ID(0x1235, 0x8214),
578 .line_out_hw_vol = 1,
579 .level_input_count = 2,
580 .pad_input_count = 2,
594 [SCARLETT2_PORT_TYPE_NONE] = { 1, 0 },
595 [SCARLETT2_PORT_TYPE_ANALOGUE] = { 8, 8 },
596 [SCARLETT2_PORT_TYPE_SPDIF] = { 2, 2 },
597 [SCARLETT2_PORT_TYPE_ADAT] = { 8, 0 },
598 [SCARLETT2_PORT_TYPE_MIX] = { 10, 20 },
599 [SCARLETT2_PORT_TYPE_PCM] = { 8, 20 },
602 .mux_assignment = { {
603 { SCARLETT2_PORT_TYPE_PCM, 0, 10 },
604 { SCARLETT2_PORT_TYPE_PCM, 12, 8 },
605 { SCARLETT2_PORT_TYPE_ANALOGUE, 0, 2 },
606 { SCARLETT2_PORT_TYPE_ANALOGUE, 6, 2 },
607 { SCARLETT2_PORT_TYPE_ANALOGUE, 2, 4 },
608 { SCARLETT2_PORT_TYPE_SPDIF, 0, 2 },
609 { SCARLETT2_PORT_TYPE_PCM, 10, 2 },
610 { SCARLETT2_PORT_TYPE_MIX, 0, 20 },
611 { SCARLETT2_PORT_TYPE_NONE, 0, 10 },
614 { SCARLETT2_PORT_TYPE_PCM, 0, 10 },
615 { SCARLETT2_PORT_TYPE_PCM, 12, 4 },
616 { SCARLETT2_PORT_TYPE_ANALOGUE, 0, 2 },
617 { SCARLETT2_PORT_TYPE_ANALOGUE, 6, 2 },
618 { SCARLETT2_PORT_TYPE_ANALOGUE, 2, 4 },
619 { SCARLETT2_PORT_TYPE_SPDIF, 0, 2 },
620 { SCARLETT2_PORT_TYPE_PCM, 10, 2 },
621 { SCARLETT2_PORT_TYPE_MIX, 0, 20 },
622 { SCARLETT2_PORT_TYPE_NONE, 0, 10 },
625 { SCARLETT2_PORT_TYPE_PCM, 0, 10 },
626 { SCARLETT2_PORT_TYPE_ANALOGUE, 0, 2 },
627 { SCARLETT2_PORT_TYPE_ANALOGUE, 6, 2 },
628 { SCARLETT2_PORT_TYPE_ANALOGUE, 2, 4 },
629 { SCARLETT2_PORT_TYPE_SPDIF, 0, 2 },
630 { SCARLETT2_PORT_TYPE_MIX, 0, 20 },
631 { SCARLETT2_PORT_TYPE_NONE, 0, 10 },
636 static const struct scarlett2_device_info s18i20_gen3_info = {
637 .usb_id = USB_ID(0x1235, 0x8215),
639 .line_out_hw_vol = 1,
640 .level_input_count = 2,
641 .pad_input_count = 8,
657 [SCARLETT2_PORT_TYPE_NONE] = { 1, 0 },
658 [SCARLETT2_PORT_TYPE_ANALOGUE] = { 9, 10 },
659 [SCARLETT2_PORT_TYPE_SPDIF] = { 2, 2 },
660 [SCARLETT2_PORT_TYPE_ADAT] = { 8, 8 },
661 [SCARLETT2_PORT_TYPE_MIX] = { 12, 25 },
662 [SCARLETT2_PORT_TYPE_PCM] = { 20, 20 },
665 .mux_assignment = { {
666 { SCARLETT2_PORT_TYPE_PCM, 0, 8 },
667 { SCARLETT2_PORT_TYPE_PCM, 10, 10 },
668 { SCARLETT2_PORT_TYPE_ANALOGUE, 0, 10 },
669 { SCARLETT2_PORT_TYPE_SPDIF, 0, 2 },
670 { SCARLETT2_PORT_TYPE_ADAT, 0, 8 },
671 { SCARLETT2_PORT_TYPE_PCM, 8, 2 },
672 { SCARLETT2_PORT_TYPE_MIX, 0, 25 },
673 { SCARLETT2_PORT_TYPE_NONE, 0, 12 },
676 { SCARLETT2_PORT_TYPE_PCM, 0, 8 },
677 { SCARLETT2_PORT_TYPE_PCM, 10, 8 },
678 { SCARLETT2_PORT_TYPE_ANALOGUE, 0, 10 },
679 { SCARLETT2_PORT_TYPE_SPDIF, 0, 2 },
680 { SCARLETT2_PORT_TYPE_ADAT, 0, 8 },
681 { SCARLETT2_PORT_TYPE_PCM, 8, 2 },
682 { SCARLETT2_PORT_TYPE_MIX, 0, 25 },
683 { SCARLETT2_PORT_TYPE_NONE, 0, 10 },
686 { SCARLETT2_PORT_TYPE_PCM, 0, 10 },
687 { SCARLETT2_PORT_TYPE_ANALOGUE, 0, 10 },
688 { SCARLETT2_PORT_TYPE_SPDIF, 0, 2 },
689 { SCARLETT2_PORT_TYPE_NONE, 0, 24 },
694 static const struct scarlett2_device_info *scarlett2_devices[] = {
695 /* Supported Gen 2 devices */
700 /* Supported Gen 3 devices */
710 /* get the starting port index number for a given port type/direction */
711 static int scarlett2_get_port_start_num(
712 const int port_count[][SCARLETT2_PORT_DIRNS],
713 int direction, int port_type)
717 for (i = 0; i < port_type; i++)
718 num += port_count[i][direction];
723 /*** USB Interactions ***/
725 /* Notifications from the interface */
726 #define SCARLETT2_USB_NOTIFY_SYNC 0x00000008
727 #define SCARLETT2_USB_NOTIFY_DIM_MUTE 0x00200000
728 #define SCARLETT2_USB_NOTIFY_MONITOR 0x00400000
730 /* Commands for sending/receiving requests/responses */
731 #define SCARLETT2_USB_CMD_INIT 0
732 #define SCARLETT2_USB_CMD_REQ 2
733 #define SCARLETT2_USB_CMD_RESP 3
735 #define SCARLETT2_USB_INIT_1 0x00000000
736 #define SCARLETT2_USB_INIT_2 0x00000002
737 #define SCARLETT2_USB_GET_METER 0x00001001
738 #define SCARLETT2_USB_GET_MIX 0x00002001
739 #define SCARLETT2_USB_SET_MIX 0x00002002
740 #define SCARLETT2_USB_GET_MUX 0x00003001
741 #define SCARLETT2_USB_SET_MUX 0x00003002
742 #define SCARLETT2_USB_GET_SYNC 0x00006004
743 #define SCARLETT2_USB_GET_DATA 0x00800000
744 #define SCARLETT2_USB_SET_DATA 0x00800001
745 #define SCARLETT2_USB_DATA_CMD 0x00800002
747 #define SCARLETT2_USB_CONFIG_SAVE 6
749 #define SCARLETT2_USB_VOLUME_STATUS_OFFSET 0x31
750 #define SCARLETT2_USB_METER_LEVELS_GET_MAGIC 1
752 /* volume status is read together (matches scarlett2_config_items[]) */
753 struct scarlett2_usb_volume_status {
754 /* dim/mute buttons */
755 u8 dim_mute[SCARLETT2_DIM_MUTE_COUNT];
759 /* software volume setting */
760 s16 sw_vol[SCARLETT2_ANALOGUE_MAX];
762 /* actual volume of output inc. dim (-18dB) */
763 s16 hw_vol[SCARLETT2_ANALOGUE_MAX];
765 /* internal mute buttons */
766 u8 mute_switch[SCARLETT2_ANALOGUE_MAX];
768 /* sw (0) or hw (1) controlled */
769 u8 sw_hw_switch[SCARLETT2_ANALOGUE_MAX];
773 /* front panel volume knob */
777 /* Configuration parameters that can be read and written */
779 SCARLETT2_CONFIG_DIM_MUTE = 0,
780 SCARLETT2_CONFIG_LINE_OUT_VOLUME = 1,
781 SCARLETT2_CONFIG_MUTE_SWITCH = 2,
782 SCARLETT2_CONFIG_SW_HW_SWITCH = 3,
783 SCARLETT2_CONFIG_LEVEL_SWITCH = 4,
784 SCARLETT2_CONFIG_PAD_SWITCH = 5,
785 SCARLETT2_CONFIG_COUNT = 6
788 /* Location, size, and activation command number for the configuration
791 struct scarlett2_config {
797 static const struct scarlett2_config
798 scarlett2_config_items[SCARLETT2_CONFIG_COUNT] = {
799 [SCARLETT2_CONFIG_DIM_MUTE] = {
800 .offset = 0x31, .size = 1, .activate = 2 },
802 [SCARLETT2_CONFIG_LINE_OUT_VOLUME] = {
803 .offset = 0x34, .size = 2, .activate = 1 },
805 [SCARLETT2_CONFIG_MUTE_SWITCH] = {
806 .offset = 0x5c, .size = 1, .activate = 1 },
808 [SCARLETT2_CONFIG_SW_HW_SWITCH] = {
809 .offset = 0x66, .size = 1, .activate = 3 },
811 [SCARLETT2_CONFIG_LEVEL_SWITCH] = {
812 .offset = 0x7c, .size = 1, .activate = 7 },
814 [SCARLETT2_CONFIG_PAD_SWITCH] = {
815 .offset = 0x84, .size = 1, .activate = 8 },
818 /* proprietary request/response format */
819 struct scarlett2_usb_packet {
828 static void scarlett2_fill_request_header(struct scarlett2_data *private,
829 struct scarlett2_usb_packet *req,
830 u32 cmd, u16 req_size)
832 /* sequence must go up by 1 for each request */
833 u16 seq = private->scarlett2_seq++;
835 req->cmd = cpu_to_le32(cmd);
836 req->size = cpu_to_le16(req_size);
837 req->seq = cpu_to_le16(seq);
842 static int scarlett2_usb_tx(struct usb_device *dev, int interface,
845 return snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0),
846 SCARLETT2_USB_CMD_REQ,
847 USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_OUT,
848 0, interface, buf, size);
851 static int scarlett2_usb_rx(struct usb_device *dev, int interface,
852 u32 usb_req, void *buf, u16 size)
854 return snd_usb_ctl_msg(dev, usb_rcvctrlpipe(dev, 0),
856 USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_IN,
857 0, interface, buf, size);
860 /* Send a proprietary format request to the Scarlett interface */
861 static int scarlett2_usb(
862 struct usb_mixer_interface *mixer, u32 cmd,
863 void *req_data, u16 req_size, void *resp_data, u16 resp_size)
865 struct scarlett2_data *private = mixer->private_data;
866 struct usb_device *dev = mixer->chip->dev;
867 u16 req_buf_size = sizeof(struct scarlett2_usb_packet) + req_size;
868 u16 resp_buf_size = sizeof(struct scarlett2_usb_packet) + resp_size;
869 struct scarlett2_usb_packet *req, *resp = NULL;
872 req = kmalloc(req_buf_size, GFP_KERNEL);
878 resp = kmalloc(resp_buf_size, GFP_KERNEL);
884 mutex_lock(&private->usb_mutex);
886 /* build request message and send it */
888 scarlett2_fill_request_header(private, req, cmd, req_size);
891 memcpy(req->data, req_data, req_size);
893 err = scarlett2_usb_tx(dev, private->bInterfaceNumber,
896 if (err != req_buf_size) {
899 "Scarlett Gen 2/3 USB request result cmd %x was %d\n",
905 /* send a second message to get the response */
907 err = scarlett2_usb_rx(dev, private->bInterfaceNumber,
908 SCARLETT2_USB_CMD_RESP,
909 resp, resp_buf_size);
911 /* validate the response */
913 if (err != resp_buf_size) {
916 "Scarlett Gen 2/3 USB response result cmd %x was %d "
918 cmd, err, resp_buf_size);
923 /* cmd/seq/size should match except when initialising
924 * seq sent = 1, response = 0
926 if (resp->cmd != req->cmd ||
927 (resp->seq != req->seq &&
928 (le16_to_cpu(req->seq) != 1 || resp->seq != 0)) ||
929 resp_size != le16_to_cpu(resp->size) ||
934 "Scarlett Gen 2/3 USB invalid response; "
935 "cmd tx/rx %d/%d seq %d/%d size %d/%d "
937 le32_to_cpu(req->cmd), le32_to_cpu(resp->cmd),
938 le16_to_cpu(req->seq), le16_to_cpu(resp->seq),
939 resp_size, le16_to_cpu(resp->size),
940 le32_to_cpu(resp->error),
941 le32_to_cpu(resp->pad));
946 if (resp_data && resp_size > 0)
947 memcpy(resp_data, resp->data, resp_size);
950 mutex_unlock(&private->usb_mutex);
957 /* Send SCARLETT2_USB_DATA_CMD SCARLETT2_USB_CONFIG_SAVE */
958 static void scarlett2_config_save(struct usb_mixer_interface *mixer)
960 __le32 req = cpu_to_le32(SCARLETT2_USB_CONFIG_SAVE);
962 scarlett2_usb(mixer, SCARLETT2_USB_DATA_CMD,
967 /* Delayed work to save config */
968 static void scarlett2_config_save_work(struct work_struct *work)
970 struct scarlett2_data *private =
971 container_of(work, struct scarlett2_data, work.work);
973 scarlett2_config_save(private->mixer);
976 /* Send a USB message to set a configuration parameter (volume level,
977 * sw/hw volume switch, line/inst level switch, or pad switch)
979 static int scarlett2_usb_set_config(
980 struct usb_mixer_interface *mixer,
981 int config_item_num, int index, int value)
983 const struct scarlett2_config *config_item =
984 &scarlett2_config_items[config_item_num];
992 struct scarlett2_data *private = mixer->private_data;
994 /* Cancel any pending NVRAM save */
995 cancel_delayed_work_sync(&private->work);
997 /* Send the configuration parameter data */
998 req.offset = cpu_to_le32(config_item->offset + index * config_item->size);
999 req.bytes = cpu_to_le32(config_item->size);
1000 req.value = cpu_to_le32(value);
1001 err = scarlett2_usb(mixer, SCARLETT2_USB_SET_DATA,
1002 &req, sizeof(u32) * 2 + config_item->size,
1007 /* Activate the change */
1008 req2 = cpu_to_le32(config_item->activate);
1009 err = scarlett2_usb(mixer, SCARLETT2_USB_DATA_CMD,
1010 &req2, sizeof(req2), NULL, 0);
1014 /* Schedule the change to be written to NVRAM */
1015 schedule_delayed_work(&private->work, msecs_to_jiffies(2000));
1020 /* Send a USB message to get data; result placed in *buf */
1021 static int scarlett2_usb_get(
1022 struct usb_mixer_interface *mixer,
1023 int offset, void *buf, int size)
1030 req.offset = cpu_to_le32(offset);
1031 req.size = cpu_to_le32(size);
1032 return scarlett2_usb(mixer, SCARLETT2_USB_GET_DATA,
1033 &req, sizeof(req), buf, size);
1036 /* Send a USB message to get configuration parameters; result placed in *buf */
1037 static int scarlett2_usb_get_config(
1038 struct usb_mixer_interface *mixer,
1039 int config_item_num, int count, void *buf)
1041 const struct scarlett2_config *config_item =
1042 &scarlett2_config_items[config_item_num];
1043 int size = config_item->size * count;
1045 return scarlett2_usb_get(mixer, config_item->offset, buf, size);
1048 /* Send a USB message to get sync status; result placed in *sync */
1049 static int scarlett2_usb_get_sync_status(
1050 struct usb_mixer_interface *mixer,
1056 err = scarlett2_usb(mixer, SCARLETT2_USB_GET_SYNC,
1057 NULL, 0, &data, sizeof(data));
1065 /* Send a USB message to get volume status; result placed in *buf */
1066 static int scarlett2_usb_get_volume_status(
1067 struct usb_mixer_interface *mixer,
1068 struct scarlett2_usb_volume_status *buf)
1070 return scarlett2_usb_get(mixer, SCARLETT2_USB_VOLUME_STATUS_OFFSET,
1074 /* Send a USB message to get the volumes for all inputs of one mix
1075 * and put the values into private->mix[]
1077 static int scarlett2_usb_get_mix(struct usb_mixer_interface *mixer,
1080 struct scarlett2_data *private = mixer->private_data;
1081 const struct scarlett2_device_info *info = private->info;
1084 info->port_count[SCARLETT2_PORT_TYPE_MIX][SCARLETT2_PORT_OUT];
1092 __le16 data[SCARLETT2_INPUT_MIX_MAX];
1094 req.mix_num = cpu_to_le16(mix_num);
1095 req.count = cpu_to_le16(num_mixer_in);
1097 err = scarlett2_usb(mixer, SCARLETT2_USB_GET_MIX,
1099 data, num_mixer_in * sizeof(u16));
1103 for (i = 0, j = mix_num * num_mixer_in; i < num_mixer_in; i++, j++) {
1104 u16 mixer_value = le16_to_cpu(data[i]);
1106 for (k = 0; k < SCARLETT2_MIXER_VALUE_COUNT; k++)
1107 if (scarlett2_mixer_values[k] >= mixer_value)
1109 if (k == SCARLETT2_MIXER_VALUE_COUNT)
1110 k = SCARLETT2_MIXER_MAX_VALUE;
1111 private->mix[j] = k;
1117 /* Send a USB message to set the volumes for all inputs of one mix
1118 * (values obtained from private->mix[])
1120 static int scarlett2_usb_set_mix(struct usb_mixer_interface *mixer,
1123 struct scarlett2_data *private = mixer->private_data;
1124 const struct scarlett2_device_info *info = private->info;
1128 __le16 data[SCARLETT2_INPUT_MIX_MAX];
1133 info->port_count[SCARLETT2_PORT_TYPE_MIX][SCARLETT2_PORT_OUT];
1135 req.mix_num = cpu_to_le16(mix_num);
1137 for (i = 0, j = mix_num * num_mixer_in; i < num_mixer_in; i++, j++)
1138 req.data[i] = cpu_to_le16(
1139 scarlett2_mixer_values[private->mix[j]]
1142 return scarlett2_usb(mixer, SCARLETT2_USB_SET_MIX,
1143 &req, (num_mixer_in + 1) * sizeof(u16),
1147 /* Convert a port number index (per info->port_count) to a hardware ID */
1148 static u32 scarlett2_mux_src_num_to_id(
1149 const int port_count[][SCARLETT2_PORT_DIRNS], int num)
1154 port_type < SCARLETT2_PORT_TYPE_COUNT;
1156 if (num < port_count[port_type][SCARLETT2_PORT_IN])
1157 return scarlett2_ports[port_type].id | num;
1158 num -= port_count[port_type][SCARLETT2_PORT_IN];
1165 /* Convert a hardware ID to a port number index */
1166 static u32 scarlett2_mux_id_to_num(
1167 const int port_count[][SCARLETT2_PORT_DIRNS], int direction, u32 id)
1173 port_type < SCARLETT2_PORT_TYPE_COUNT;
1175 int base = scarlett2_ports[port_type].id;
1176 int count = port_count[port_type][direction];
1178 if (id >= base && id < base + count)
1179 return port_num + id - base;
1187 /* Convert one mux entry from the interface and load into private->mux[] */
1188 static void scarlett2_usb_populate_mux(struct scarlett2_data *private,
1191 const struct scarlett2_device_info *info = private->info;
1192 const int (*port_count)[SCARLETT2_PORT_DIRNS] = info->port_count;
1194 int dst_idx, src_idx;
1196 dst_idx = scarlett2_mux_id_to_num(port_count, SCARLETT2_PORT_OUT,
1201 if (dst_idx >= private->num_mux_dsts) {
1202 usb_audio_err(private->mixer->chip,
1203 "BUG: scarlett2_mux_id_to_num(%06x, OUT): %d >= %d",
1204 mux_entry, dst_idx, private->num_mux_dsts);
1208 src_idx = scarlett2_mux_id_to_num(port_count, SCARLETT2_PORT_IN,
1213 if (src_idx >= private->num_mux_srcs) {
1214 usb_audio_err(private->mixer->chip,
1215 "BUG: scarlett2_mux_id_to_num(%06x, IN): %d >= %d",
1216 mux_entry, src_idx, private->num_mux_srcs);
1220 private->mux[dst_idx] = src_idx;
1223 /* Send USB message to get mux inputs and then populate private->mux[] */
1224 static int scarlett2_usb_get_mux(struct usb_mixer_interface *mixer)
1226 struct scarlett2_data *private = mixer->private_data;
1227 int count = private->num_mux_dsts;
1235 __le32 data[SCARLETT2_MUX_MAX];
1238 req.count = cpu_to_le16(count);
1240 err = scarlett2_usb(mixer, SCARLETT2_USB_GET_MUX,
1242 data, count * sizeof(u32));
1246 for (i = 0; i < count; i++)
1247 scarlett2_usb_populate_mux(private, le32_to_cpu(data[i]));
1252 /* Send USB messages to set mux inputs */
1253 static int scarlett2_usb_set_mux(struct usb_mixer_interface *mixer)
1255 struct scarlett2_data *private = mixer->private_data;
1256 const struct scarlett2_device_info *info = private->info;
1257 const int (*port_count)[SCARLETT2_PORT_DIRNS] = info->port_count;
1263 __le32 data[SCARLETT2_MUX_MAX];
1268 /* set mux settings for each rate */
1269 for (table = 0; table < SCARLETT2_MUX_TABLES; table++) {
1270 const struct scarlett2_mux_entry *entry;
1272 /* i counts over the output array */
1275 req.num = cpu_to_le16(table);
1277 /* loop through each entry */
1278 for (entry = info->mux_assignment[table];
1282 int port_type = entry->port_type;
1283 int port_idx = entry->start;
1284 int mux_idx = scarlett2_get_port_start_num(port_count,
1285 SCARLETT2_PORT_OUT, port_type) + port_idx;
1286 int dst_id = scarlett2_ports[port_type].id + port_idx;
1290 for (j = 0; j < entry->count; j++)
1295 /* Non-empty mux slots use the lower 12 bits
1296 * for the destination and next 12 bits for
1299 for (j = 0; j < entry->count; j++) {
1300 int src_id = scarlett2_mux_src_num_to_id(
1301 port_count, private->mux[mux_idx++]);
1302 req.data[i++] = cpu_to_le32(dst_id |
1308 err = scarlett2_usb(mixer, SCARLETT2_USB_SET_MUX,
1309 &req, (i + 1) * sizeof(u32),
1318 /* Send USB message to get meter levels */
1319 static int scarlett2_usb_get_meter_levels(struct usb_mixer_interface *mixer,
1320 u16 num_meters, u16 *levels)
1327 u32 resp[SCARLETT2_MAX_METERS];
1331 req.num_meters = cpu_to_le16(num_meters);
1332 req.magic = cpu_to_le32(SCARLETT2_USB_METER_LEVELS_GET_MAGIC);
1333 err = scarlett2_usb(mixer, SCARLETT2_USB_GET_METER,
1334 &req, sizeof(req), resp, num_meters * sizeof(u32));
1338 /* copy, convert to u16 */
1339 for (i = 0; i < num_meters; i++)
1340 levels[i] = resp[i];
1345 /*** Control Functions ***/
1347 /* helper function to create a new control */
1348 static int scarlett2_add_new_ctl(struct usb_mixer_interface *mixer,
1349 const struct snd_kcontrol_new *ncontrol,
1350 int index, int channels, const char *name,
1351 struct snd_kcontrol **kctl_return)
1353 struct snd_kcontrol *kctl;
1354 struct usb_mixer_elem_info *elem;
1357 elem = kzalloc(sizeof(*elem), GFP_KERNEL);
1361 /* We set USB_MIXER_BESPOKEN type, so that the core USB mixer code
1362 * ignores them for resume and other operations.
1363 * Also, the head.id field is set to 0, as we don't use this field.
1365 elem->head.mixer = mixer;
1366 elem->control = index;
1368 elem->channels = channels;
1369 elem->val_type = USB_MIXER_BESPOKEN;
1371 kctl = snd_ctl_new1(ncontrol, elem);
1376 kctl->private_free = snd_usb_mixer_elem_free;
1378 strscpy(kctl->id.name, name, sizeof(kctl->id.name));
1380 err = snd_usb_mixer_add_control(&elem->head, kctl);
1385 *kctl_return = kctl;
1390 /*** Sync Control ***/
1392 /* Update sync control after receiving notification that the status
1395 static int scarlett2_update_sync(struct usb_mixer_interface *mixer)
1397 struct scarlett2_data *private = mixer->private_data;
1399 private->sync_updated = 0;
1400 return scarlett2_usb_get_sync_status(mixer, &private->sync);
1403 static int scarlett2_sync_ctl_info(struct snd_kcontrol *kctl,
1404 struct snd_ctl_elem_info *uinfo)
1406 static const char *texts[2] = {
1407 "Unlocked", "Locked"
1409 return snd_ctl_enum_info(uinfo, 1, 2, texts);
1412 static int scarlett2_sync_ctl_get(struct snd_kcontrol *kctl,
1413 struct snd_ctl_elem_value *ucontrol)
1415 struct usb_mixer_elem_info *elem = kctl->private_data;
1416 struct usb_mixer_interface *mixer = elem->head.mixer;
1417 struct scarlett2_data *private = mixer->private_data;
1419 mutex_lock(&private->data_mutex);
1420 if (private->sync_updated)
1421 scarlett2_update_sync(mixer);
1422 ucontrol->value.enumerated.item[0] = private->sync;
1423 mutex_unlock(&private->data_mutex);
1428 static const struct snd_kcontrol_new scarlett2_sync_ctl = {
1429 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1430 .access = SNDRV_CTL_ELEM_ACCESS_READ,
1432 .info = scarlett2_sync_ctl_info,
1433 .get = scarlett2_sync_ctl_get
1436 static int scarlett2_add_sync_ctl(struct usb_mixer_interface *mixer)
1438 struct scarlett2_data *private = mixer->private_data;
1440 return scarlett2_add_new_ctl(mixer, &scarlett2_sync_ctl,
1441 0, 1, "Sync Status", &private->sync_ctl);
1444 /*** Analogue Line Out Volume Controls ***/
1446 /* Update hardware volume controls after receiving notification that
1449 static int scarlett2_update_volumes(struct usb_mixer_interface *mixer)
1451 struct scarlett2_data *private = mixer->private_data;
1452 const struct scarlett2_device_info *info = private->info;
1453 const int (*port_count)[SCARLETT2_PORT_DIRNS] = info->port_count;
1454 struct scarlett2_usb_volume_status volume_status;
1456 port_count[SCARLETT2_PORT_TYPE_ANALOGUE][SCARLETT2_PORT_OUT];
1460 private->vol_updated = 0;
1462 err = scarlett2_usb_get_volume_status(mixer, &volume_status);
1466 private->master_vol = clamp(
1467 volume_status.master_vol + SCARLETT2_VOLUME_BIAS,
1468 0, SCARLETT2_VOLUME_BIAS);
1470 if (info->line_out_hw_vol)
1471 for (i = 0; i < SCARLETT2_DIM_MUTE_COUNT; i++)
1472 private->dim_mute[i] = !!volume_status.dim_mute[i];
1474 mute = private->dim_mute[SCARLETT2_BUTTON_MUTE];
1476 for (i = 0; i < num_line_out; i++)
1477 if (private->vol_sw_hw_switch[i]) {
1478 private->vol[i] = private->master_vol;
1479 private->mute_switch[i] = mute;
1485 static int scarlett2_volume_ctl_info(struct snd_kcontrol *kctl,
1486 struct snd_ctl_elem_info *uinfo)
1488 struct usb_mixer_elem_info *elem = kctl->private_data;
1490 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1491 uinfo->count = elem->channels;
1492 uinfo->value.integer.min = 0;
1493 uinfo->value.integer.max = SCARLETT2_VOLUME_BIAS;
1494 uinfo->value.integer.step = 1;
1498 static int scarlett2_master_volume_ctl_get(struct snd_kcontrol *kctl,
1499 struct snd_ctl_elem_value *ucontrol)
1501 struct usb_mixer_elem_info *elem = kctl->private_data;
1502 struct usb_mixer_interface *mixer = elem->head.mixer;
1503 struct scarlett2_data *private = mixer->private_data;
1505 mutex_lock(&private->data_mutex);
1506 if (private->vol_updated)
1507 scarlett2_update_volumes(mixer);
1508 mutex_unlock(&private->data_mutex);
1510 ucontrol->value.integer.value[0] = private->master_vol;
1514 static int scarlett2_volume_ctl_get(struct snd_kcontrol *kctl,
1515 struct snd_ctl_elem_value *ucontrol)
1517 struct usb_mixer_elem_info *elem = kctl->private_data;
1518 struct usb_mixer_interface *mixer = elem->head.mixer;
1519 struct scarlett2_data *private = mixer->private_data;
1520 int index = elem->control;
1522 mutex_lock(&private->data_mutex);
1523 if (private->vol_updated)
1524 scarlett2_update_volumes(mixer);
1525 mutex_unlock(&private->data_mutex);
1527 ucontrol->value.integer.value[0] = private->vol[index];
1531 static int scarlett2_volume_ctl_put(struct snd_kcontrol *kctl,
1532 struct snd_ctl_elem_value *ucontrol)
1534 struct usb_mixer_elem_info *elem = kctl->private_data;
1535 struct usb_mixer_interface *mixer = elem->head.mixer;
1536 struct scarlett2_data *private = mixer->private_data;
1537 int index = elem->control;
1538 int oval, val, err = 0;
1540 mutex_lock(&private->data_mutex);
1542 oval = private->vol[index];
1543 val = ucontrol->value.integer.value[0];
1548 private->vol[index] = val;
1549 err = scarlett2_usb_set_config(mixer, SCARLETT2_CONFIG_LINE_OUT_VOLUME,
1550 index, val - SCARLETT2_VOLUME_BIAS);
1555 mutex_unlock(&private->data_mutex);
1559 static const DECLARE_TLV_DB_MINMAX(
1560 db_scale_scarlett2_gain, -SCARLETT2_VOLUME_BIAS * 100, 0
1563 static const struct snd_kcontrol_new scarlett2_master_volume_ctl = {
1564 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1565 .access = SNDRV_CTL_ELEM_ACCESS_READ |
1566 SNDRV_CTL_ELEM_ACCESS_TLV_READ,
1568 .info = scarlett2_volume_ctl_info,
1569 .get = scarlett2_master_volume_ctl_get,
1570 .private_value = 0, /* max value */
1571 .tlv = { .p = db_scale_scarlett2_gain }
1574 static const struct snd_kcontrol_new scarlett2_line_out_volume_ctl = {
1575 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1576 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE |
1577 SNDRV_CTL_ELEM_ACCESS_TLV_READ,
1579 .info = scarlett2_volume_ctl_info,
1580 .get = scarlett2_volume_ctl_get,
1581 .put = scarlett2_volume_ctl_put,
1582 .private_value = 0, /* max value */
1583 .tlv = { .p = db_scale_scarlett2_gain }
1586 /*** Mute Switch Controls ***/
1588 static int scarlett2_mute_ctl_get(struct snd_kcontrol *kctl,
1589 struct snd_ctl_elem_value *ucontrol)
1591 struct usb_mixer_elem_info *elem = kctl->private_data;
1592 struct scarlett2_data *private = elem->head.mixer->private_data;
1593 int index = elem->control;
1595 ucontrol->value.integer.value[0] = private->mute_switch[index];
1599 static int scarlett2_mute_ctl_put(struct snd_kcontrol *kctl,
1600 struct snd_ctl_elem_value *ucontrol)
1602 struct usb_mixer_elem_info *elem = kctl->private_data;
1603 struct usb_mixer_interface *mixer = elem->head.mixer;
1604 struct scarlett2_data *private = mixer->private_data;
1605 int index = elem->control;
1606 int oval, val, err = 0;
1608 mutex_lock(&private->data_mutex);
1610 oval = private->mute_switch[index];
1611 val = !!ucontrol->value.integer.value[0];
1616 private->mute_switch[index] = val;
1618 /* Send mute change to the device */
1619 err = scarlett2_usb_set_config(mixer, SCARLETT2_CONFIG_MUTE_SWITCH,
1623 mutex_unlock(&private->data_mutex);
1627 static const struct snd_kcontrol_new scarlett2_mute_ctl = {
1628 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1630 .info = snd_ctl_boolean_mono_info,
1631 .get = scarlett2_mute_ctl_get,
1632 .put = scarlett2_mute_ctl_put,
1635 /*** HW/SW Volume Switch Controls ***/
1637 static int scarlett2_sw_hw_enum_ctl_info(struct snd_kcontrol *kctl,
1638 struct snd_ctl_elem_info *uinfo)
1640 static const char *const values[2] = {
1644 return snd_ctl_enum_info(uinfo, 1, 2, values);
1647 static int scarlett2_sw_hw_enum_ctl_get(struct snd_kcontrol *kctl,
1648 struct snd_ctl_elem_value *ucontrol)
1650 struct usb_mixer_elem_info *elem = kctl->private_data;
1651 struct scarlett2_data *private = elem->head.mixer->private_data;
1653 ucontrol->value.enumerated.item[0] =
1654 private->vol_sw_hw_switch[elem->control];
1658 static void scarlett2_vol_ctl_set_writable(struct usb_mixer_interface *mixer,
1659 int index, int value)
1661 struct scarlett2_data *private = mixer->private_data;
1662 struct snd_card *card = mixer->chip->card;
1664 /* Set/Clear write bits */
1666 private->vol_ctls[index]->vd[0].access |=
1667 SNDRV_CTL_ELEM_ACCESS_WRITE;
1668 private->mute_ctls[index]->vd[0].access |=
1669 SNDRV_CTL_ELEM_ACCESS_WRITE;
1671 private->vol_ctls[index]->vd[0].access &=
1672 ~SNDRV_CTL_ELEM_ACCESS_WRITE;
1673 private->mute_ctls[index]->vd[0].access &=
1674 ~SNDRV_CTL_ELEM_ACCESS_WRITE;
1677 /* Notify of write bit change */
1678 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_INFO,
1679 &private->vol_ctls[index]->id);
1680 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_INFO,
1681 &private->mute_ctls[index]->id);
1684 static int scarlett2_sw_hw_enum_ctl_put(struct snd_kcontrol *kctl,
1685 struct snd_ctl_elem_value *ucontrol)
1687 struct usb_mixer_elem_info *elem = kctl->private_data;
1688 struct usb_mixer_interface *mixer = elem->head.mixer;
1689 struct scarlett2_data *private = mixer->private_data;
1691 int index = elem->control;
1692 int oval, val, err = 0;
1694 mutex_lock(&private->data_mutex);
1696 oval = private->vol_sw_hw_switch[index];
1697 val = !!ucontrol->value.enumerated.item[0];
1702 private->vol_sw_hw_switch[index] = val;
1704 /* Change access mode to RO (hardware controlled volume)
1705 * or RW (software controlled volume)
1707 scarlett2_vol_ctl_set_writable(mixer, index, !val);
1709 /* Reset volume/mute to master volume/mute */
1710 private->vol[index] = private->master_vol;
1711 private->mute_switch[index] = private->dim_mute[SCARLETT2_BUTTON_MUTE];
1713 /* Set SW volume to current HW volume */
1714 err = scarlett2_usb_set_config(
1715 mixer, SCARLETT2_CONFIG_LINE_OUT_VOLUME,
1716 index, private->master_vol - SCARLETT2_VOLUME_BIAS);
1720 /* Set SW mute to current HW mute */
1721 err = scarlett2_usb_set_config(
1722 mixer, SCARLETT2_CONFIG_MUTE_SWITCH,
1723 index, private->dim_mute[SCARLETT2_BUTTON_MUTE]);
1727 /* Send SW/HW switch change to the device */
1728 err = scarlett2_usb_set_config(mixer, SCARLETT2_CONFIG_SW_HW_SWITCH,
1734 mutex_unlock(&private->data_mutex);
1738 static const struct snd_kcontrol_new scarlett2_sw_hw_enum_ctl = {
1739 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1741 .info = scarlett2_sw_hw_enum_ctl_info,
1742 .get = scarlett2_sw_hw_enum_ctl_get,
1743 .put = scarlett2_sw_hw_enum_ctl_put,
1746 /*** Line Level/Instrument Level Switch Controls ***/
1748 static int scarlett2_level_enum_ctl_info(struct snd_kcontrol *kctl,
1749 struct snd_ctl_elem_info *uinfo)
1751 static const char *const values[2] = {
1755 return snd_ctl_enum_info(uinfo, 1, 2, values);
1758 static int scarlett2_level_enum_ctl_get(struct snd_kcontrol *kctl,
1759 struct snd_ctl_elem_value *ucontrol)
1761 struct usb_mixer_elem_info *elem = kctl->private_data;
1762 struct scarlett2_data *private = elem->head.mixer->private_data;
1764 ucontrol->value.enumerated.item[0] =
1765 private->level_switch[elem->control];
1769 static int scarlett2_level_enum_ctl_put(struct snd_kcontrol *kctl,
1770 struct snd_ctl_elem_value *ucontrol)
1772 struct usb_mixer_elem_info *elem = kctl->private_data;
1773 struct usb_mixer_interface *mixer = elem->head.mixer;
1774 struct scarlett2_data *private = mixer->private_data;
1776 int index = elem->control;
1777 int oval, val, err = 0;
1779 mutex_lock(&private->data_mutex);
1781 oval = private->level_switch[index];
1782 val = !!ucontrol->value.enumerated.item[0];
1787 private->level_switch[index] = val;
1789 /* Send switch change to the device */
1790 err = scarlett2_usb_set_config(mixer, SCARLETT2_CONFIG_LEVEL_SWITCH,
1796 mutex_unlock(&private->data_mutex);
1800 static const struct snd_kcontrol_new scarlett2_level_enum_ctl = {
1801 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1803 .info = scarlett2_level_enum_ctl_info,
1804 .get = scarlett2_level_enum_ctl_get,
1805 .put = scarlett2_level_enum_ctl_put,
1808 /*** Pad Switch Controls ***/
1810 static int scarlett2_pad_ctl_get(struct snd_kcontrol *kctl,
1811 struct snd_ctl_elem_value *ucontrol)
1813 struct usb_mixer_elem_info *elem = kctl->private_data;
1814 struct scarlett2_data *private = elem->head.mixer->private_data;
1816 ucontrol->value.integer.value[0] =
1817 private->pad_switch[elem->control];
1821 static int scarlett2_pad_ctl_put(struct snd_kcontrol *kctl,
1822 struct snd_ctl_elem_value *ucontrol)
1824 struct usb_mixer_elem_info *elem = kctl->private_data;
1825 struct usb_mixer_interface *mixer = elem->head.mixer;
1826 struct scarlett2_data *private = mixer->private_data;
1828 int index = elem->control;
1829 int oval, val, err = 0;
1831 mutex_lock(&private->data_mutex);
1833 oval = private->pad_switch[index];
1834 val = !!ucontrol->value.integer.value[0];
1839 private->pad_switch[index] = val;
1841 /* Send switch change to the device */
1842 err = scarlett2_usb_set_config(mixer, SCARLETT2_CONFIG_PAD_SWITCH,
1848 mutex_unlock(&private->data_mutex);
1852 static const struct snd_kcontrol_new scarlett2_pad_ctl = {
1853 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1855 .info = snd_ctl_boolean_mono_info,
1856 .get = scarlett2_pad_ctl_get,
1857 .put = scarlett2_pad_ctl_put,
1860 /*** Dim/Mute Controls ***/
1862 static int scarlett2_dim_mute_ctl_get(struct snd_kcontrol *kctl,
1863 struct snd_ctl_elem_value *ucontrol)
1865 struct usb_mixer_elem_info *elem = kctl->private_data;
1866 struct usb_mixer_interface *mixer = elem->head.mixer;
1867 struct scarlett2_data *private = mixer->private_data;
1869 mutex_lock(&private->data_mutex);
1870 if (private->vol_updated)
1871 scarlett2_update_volumes(mixer);
1872 mutex_unlock(&private->data_mutex);
1874 ucontrol->value.integer.value[0] = private->dim_mute[elem->control];
1878 static int scarlett2_dim_mute_ctl_put(struct snd_kcontrol *kctl,
1879 struct snd_ctl_elem_value *ucontrol)
1881 struct usb_mixer_elem_info *elem = kctl->private_data;
1882 struct usb_mixer_interface *mixer = elem->head.mixer;
1883 struct scarlett2_data *private = mixer->private_data;
1884 const struct scarlett2_device_info *info = private->info;
1885 const int (*port_count)[SCARLETT2_PORT_DIRNS] = info->port_count;
1887 port_count[SCARLETT2_PORT_TYPE_ANALOGUE][SCARLETT2_PORT_OUT];
1889 int index = elem->control;
1890 int oval, val, err = 0, i;
1892 mutex_lock(&private->data_mutex);
1894 oval = private->dim_mute[index];
1895 val = !!ucontrol->value.integer.value[0];
1900 private->dim_mute[index] = val;
1902 /* Send switch change to the device */
1903 err = scarlett2_usb_set_config(mixer, SCARLETT2_CONFIG_DIM_MUTE,
1908 if (index == SCARLETT2_BUTTON_MUTE)
1909 for (i = 0; i < num_line_out; i++)
1910 if (private->vol_sw_hw_switch[i]) {
1911 private->mute_switch[i] = val;
1912 snd_ctl_notify(mixer->chip->card,
1913 SNDRV_CTL_EVENT_MASK_INFO,
1914 &private->mute_ctls[i]->id);
1918 mutex_unlock(&private->data_mutex);
1922 static const struct snd_kcontrol_new scarlett2_dim_mute_ctl = {
1923 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1925 .info = snd_ctl_boolean_mono_info,
1926 .get = scarlett2_dim_mute_ctl_get,
1927 .put = scarlett2_dim_mute_ctl_put
1930 /*** Create the analogue output controls ***/
1932 static int scarlett2_add_line_out_ctls(struct usb_mixer_interface *mixer)
1934 struct scarlett2_data *private = mixer->private_data;
1935 const struct scarlett2_device_info *info = private->info;
1936 const int (*port_count)[SCARLETT2_PORT_DIRNS] = info->port_count;
1938 port_count[SCARLETT2_PORT_TYPE_ANALOGUE][SCARLETT2_PORT_OUT];
1940 char s[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
1942 /* Add R/O HW volume control */
1943 if (info->line_out_hw_vol) {
1944 snprintf(s, sizeof(s), "Master HW Playback Volume");
1945 err = scarlett2_add_new_ctl(mixer,
1946 &scarlett2_master_volume_ctl,
1947 0, 1, s, &private->master_vol_ctl);
1952 /* Add volume controls */
1953 for (i = 0; i < num_line_out; i++) {
1956 if (info->line_out_descrs[i])
1957 snprintf(s, sizeof(s),
1958 "Line %02d (%s) Playback Volume",
1959 i + 1, info->line_out_descrs[i]);
1961 snprintf(s, sizeof(s),
1962 "Line %02d Playback Volume",
1964 err = scarlett2_add_new_ctl(mixer,
1965 &scarlett2_line_out_volume_ctl,
1966 i, 1, s, &private->vol_ctls[i]);
1971 snprintf(s, sizeof(s),
1972 "Line %02d Mute Playback Switch",
1974 err = scarlett2_add_new_ctl(mixer,
1975 &scarlett2_mute_ctl,
1977 &private->mute_ctls[i]);
1981 /* Make the fader and mute controls read-only if the
1982 * SW/HW switch is set to HW
1984 if (private->vol_sw_hw_switch[i])
1985 scarlett2_vol_ctl_set_writable(mixer, i, 0);
1988 if (info->line_out_hw_vol) {
1989 snprintf(s, sizeof(s),
1990 "Line Out %02d Volume Control Playback Enum",
1992 err = scarlett2_add_new_ctl(mixer,
1993 &scarlett2_sw_hw_enum_ctl,
2000 /* Add dim/mute controls */
2001 if (info->line_out_hw_vol)
2002 for (i = 0; i < SCARLETT2_DIM_MUTE_COUNT; i++) {
2003 err = scarlett2_add_new_ctl(
2004 mixer, &scarlett2_dim_mute_ctl,
2005 i, 1, scarlett2_dim_mute_names[i],
2006 &private->dim_mute_ctls[i]);
2014 /*** Create the analogue input controls ***/
2016 static int scarlett2_add_line_in_ctls(struct usb_mixer_interface *mixer)
2018 struct scarlett2_data *private = mixer->private_data;
2019 const struct scarlett2_device_info *info = private->info;
2021 char s[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
2022 const char *fmt = "Line In %d %s Capture %s";
2024 /* Add input level (line/inst) controls */
2025 for (i = 0; i < info->level_input_count; i++) {
2026 snprintf(s, sizeof(s), fmt, i + 1, "Level", "Enum");
2027 err = scarlett2_add_new_ctl(mixer, &scarlett2_level_enum_ctl,
2033 /* Add input pad controls */
2034 for (i = 0; i < info->pad_input_count; i++) {
2035 snprintf(s, sizeof(s), fmt, i + 1, "Pad", "Switch");
2036 err = scarlett2_add_new_ctl(mixer, &scarlett2_pad_ctl,
2045 /*** Mixer Volume Controls ***/
2047 static int scarlett2_mixer_ctl_info(struct snd_kcontrol *kctl,
2048 struct snd_ctl_elem_info *uinfo)
2050 struct usb_mixer_elem_info *elem = kctl->private_data;
2052 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2053 uinfo->count = elem->channels;
2054 uinfo->value.integer.min = 0;
2055 uinfo->value.integer.max = SCARLETT2_MIXER_MAX_VALUE;
2056 uinfo->value.integer.step = 1;
2060 static int scarlett2_mixer_ctl_get(struct snd_kcontrol *kctl,
2061 struct snd_ctl_elem_value *ucontrol)
2063 struct usb_mixer_elem_info *elem = kctl->private_data;
2064 struct scarlett2_data *private = elem->head.mixer->private_data;
2066 ucontrol->value.integer.value[0] = private->mix[elem->control];
2070 static int scarlett2_mixer_ctl_put(struct snd_kcontrol *kctl,
2071 struct snd_ctl_elem_value *ucontrol)
2073 struct usb_mixer_elem_info *elem = kctl->private_data;
2074 struct usb_mixer_interface *mixer = elem->head.mixer;
2075 struct scarlett2_data *private = mixer->private_data;
2076 const struct scarlett2_device_info *info = private->info;
2077 const int (*port_count)[SCARLETT2_PORT_DIRNS] = info->port_count;
2078 int oval, val, num_mixer_in, mix_num, err = 0;
2079 int index = elem->control;
2081 mutex_lock(&private->data_mutex);
2083 oval = private->mix[index];
2084 val = ucontrol->value.integer.value[0];
2085 num_mixer_in = port_count[SCARLETT2_PORT_TYPE_MIX][SCARLETT2_PORT_OUT];
2086 mix_num = index / num_mixer_in;
2091 private->mix[index] = val;
2092 err = scarlett2_usb_set_mix(mixer, mix_num);
2097 mutex_unlock(&private->data_mutex);
2101 static const DECLARE_TLV_DB_MINMAX(
2102 db_scale_scarlett2_mixer,
2103 SCARLETT2_MIXER_MIN_DB * 100,
2104 SCARLETT2_MIXER_MAX_DB * 100
2107 static const struct snd_kcontrol_new scarlett2_mixer_ctl = {
2108 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2109 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE |
2110 SNDRV_CTL_ELEM_ACCESS_TLV_READ,
2112 .info = scarlett2_mixer_ctl_info,
2113 .get = scarlett2_mixer_ctl_get,
2114 .put = scarlett2_mixer_ctl_put,
2115 .private_value = SCARLETT2_MIXER_MAX_DB, /* max value */
2116 .tlv = { .p = db_scale_scarlett2_mixer }
2119 static int scarlett2_add_mixer_ctls(struct usb_mixer_interface *mixer)
2121 struct scarlett2_data *private = mixer->private_data;
2122 const struct scarlett2_device_info *info = private->info;
2123 const int (*port_count)[SCARLETT2_PORT_DIRNS] = info->port_count;
2126 char s[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
2129 port_count[SCARLETT2_PORT_TYPE_MIX][SCARLETT2_PORT_OUT];
2131 port_count[SCARLETT2_PORT_TYPE_MIX][SCARLETT2_PORT_IN];
2133 for (i = 0, index = 0; i < num_outputs; i++)
2134 for (j = 0; j < num_inputs; j++, index++) {
2135 snprintf(s, sizeof(s),
2136 "Mix %c Input %02d Playback Volume",
2138 err = scarlett2_add_new_ctl(mixer, &scarlett2_mixer_ctl,
2147 /*** Mux Source Selection Controls ***/
2149 static int scarlett2_mux_src_enum_ctl_info(struct snd_kcontrol *kctl,
2150 struct snd_ctl_elem_info *uinfo)
2152 struct usb_mixer_elem_info *elem = kctl->private_data;
2153 struct scarlett2_data *private = elem->head.mixer->private_data;
2154 const struct scarlett2_device_info *info = private->info;
2155 const int (*port_count)[SCARLETT2_PORT_DIRNS] = info->port_count;
2156 unsigned int item = uinfo->value.enumerated.item;
2157 int items = private->num_mux_srcs;
2160 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2161 uinfo->count = elem->channels;
2162 uinfo->value.enumerated.items = items;
2165 item = uinfo->value.enumerated.item = items - 1;
2168 port_type < SCARLETT2_PORT_TYPE_COUNT;
2170 if (item < port_count[port_type][SCARLETT2_PORT_IN]) {
2171 const struct scarlett2_port *port =
2172 &scarlett2_ports[port_type];
2174 sprintf(uinfo->value.enumerated.name,
2175 port->src_descr, item + port->src_num_offset);
2178 item -= port_count[port_type][SCARLETT2_PORT_IN];
2184 static int scarlett2_mux_src_enum_ctl_get(struct snd_kcontrol *kctl,
2185 struct snd_ctl_elem_value *ucontrol)
2187 struct usb_mixer_elem_info *elem = kctl->private_data;
2188 struct scarlett2_data *private = elem->head.mixer->private_data;
2190 ucontrol->value.enumerated.item[0] = private->mux[elem->control];
2194 static int scarlett2_mux_src_enum_ctl_put(struct snd_kcontrol *kctl,
2195 struct snd_ctl_elem_value *ucontrol)
2197 struct usb_mixer_elem_info *elem = kctl->private_data;
2198 struct usb_mixer_interface *mixer = elem->head.mixer;
2199 struct scarlett2_data *private = mixer->private_data;
2200 int index = elem->control;
2201 int oval, val, err = 0;
2203 mutex_lock(&private->data_mutex);
2205 oval = private->mux[index];
2206 val = min(ucontrol->value.enumerated.item[0],
2207 private->num_mux_srcs - 1U);
2212 private->mux[index] = val;
2213 err = scarlett2_usb_set_mux(mixer);
2218 mutex_unlock(&private->data_mutex);
2222 static const struct snd_kcontrol_new scarlett2_mux_src_enum_ctl = {
2223 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2225 .info = scarlett2_mux_src_enum_ctl_info,
2226 .get = scarlett2_mux_src_enum_ctl_get,
2227 .put = scarlett2_mux_src_enum_ctl_put,
2230 static int scarlett2_add_mux_enums(struct usb_mixer_interface *mixer)
2232 struct scarlett2_data *private = mixer->private_data;
2233 const struct scarlett2_device_info *info = private->info;
2234 const int (*port_count)[SCARLETT2_PORT_DIRNS] = info->port_count;
2235 int port_type, channel, i;
2237 for (i = 0, port_type = 0;
2238 port_type < SCARLETT2_PORT_TYPE_COUNT;
2241 channel < port_count[port_type][SCARLETT2_PORT_OUT];
2244 char s[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
2245 const char *const descr =
2246 scarlett2_ports[port_type].dst_descr;
2248 snprintf(s, sizeof(s) - 5, descr, channel + 1);
2251 err = scarlett2_add_new_ctl(mixer,
2252 &scarlett2_mux_src_enum_ctl,
2262 /*** Meter Controls ***/
2264 static int scarlett2_meter_ctl_info(struct snd_kcontrol *kctl,
2265 struct snd_ctl_elem_info *uinfo)
2267 struct usb_mixer_elem_info *elem = kctl->private_data;
2269 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2270 uinfo->count = elem->channels;
2271 uinfo->value.integer.min = 0;
2272 uinfo->value.integer.max = 4095;
2273 uinfo->value.integer.step = 1;
2277 static int scarlett2_meter_ctl_get(struct snd_kcontrol *kctl,
2278 struct snd_ctl_elem_value *ucontrol)
2280 struct usb_mixer_elem_info *elem = kctl->private_data;
2281 u16 meter_levels[SCARLETT2_MAX_METERS];
2284 err = scarlett2_usb_get_meter_levels(elem->head.mixer, elem->channels,
2289 for (i = 0; i < elem->channels; i++)
2290 ucontrol->value.integer.value[i] = meter_levels[i];
2295 static const struct snd_kcontrol_new scarlett2_meter_ctl = {
2296 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
2297 .access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE,
2299 .info = scarlett2_meter_ctl_info,
2300 .get = scarlett2_meter_ctl_get
2303 static int scarlett2_add_meter_ctl(struct usb_mixer_interface *mixer)
2305 struct scarlett2_data *private = mixer->private_data;
2307 return scarlett2_add_new_ctl(mixer, &scarlett2_meter_ctl,
2308 0, private->num_mux_dsts,
2309 "Level Meter", NULL);
2312 /*** Cleanup/Suspend Callbacks ***/
2314 static void scarlett2_private_free(struct usb_mixer_interface *mixer)
2316 struct scarlett2_data *private = mixer->private_data;
2318 cancel_delayed_work_sync(&private->work);
2320 mixer->private_data = NULL;
2323 static void scarlett2_private_suspend(struct usb_mixer_interface *mixer)
2325 struct scarlett2_data *private = mixer->private_data;
2327 if (cancel_delayed_work_sync(&private->work))
2328 scarlett2_config_save(private->mixer);
2331 /*** Initialisation ***/
2333 static void scarlett2_count_mux_io(struct scarlett2_data *private)
2335 const struct scarlett2_device_info *info = private->info;
2336 const int (*port_count)[SCARLETT2_PORT_DIRNS] = info->port_count;
2337 int port_type, srcs = 0, dsts = 0;
2340 port_type < SCARLETT2_PORT_TYPE_COUNT;
2342 srcs += port_count[port_type][SCARLETT2_PORT_IN];
2343 dsts += port_count[port_type][SCARLETT2_PORT_OUT];
2346 private->num_mux_srcs = srcs;
2347 private->num_mux_dsts = dsts;
2350 /* Look through the interface descriptors for the Focusrite Control
2351 * interface (bInterfaceClass = 255 Vendor Specific Class) and set
2352 * bInterfaceNumber, bEndpointAddress, wMaxPacketSize, and bInterval
2355 static int scarlett2_find_fc_interface(struct usb_device *dev,
2356 struct scarlett2_data *private)
2358 struct usb_host_config *config = dev->actconfig;
2361 for (i = 0; i < config->desc.bNumInterfaces; i++) {
2362 struct usb_interface *intf = config->interface[i];
2363 struct usb_interface_descriptor *desc =
2364 &intf->altsetting[0].desc;
2365 struct usb_endpoint_descriptor *epd;
2367 if (desc->bInterfaceClass != 255)
2370 epd = get_endpoint(intf->altsetting, 0);
2371 private->bInterfaceNumber = desc->bInterfaceNumber;
2372 private->bEndpointAddress = epd->bEndpointAddress &
2373 USB_ENDPOINT_NUMBER_MASK;
2374 private->wMaxPacketSize = le16_to_cpu(epd->wMaxPacketSize);
2375 private->bInterval = epd->bInterval;
2382 /* Initialise private data */
2383 static int scarlett2_init_private(struct usb_mixer_interface *mixer,
2384 const struct scarlett2_device_info *info)
2386 struct scarlett2_data *private =
2387 kzalloc(sizeof(struct scarlett2_data), GFP_KERNEL);
2392 mutex_init(&private->usb_mutex);
2393 mutex_init(&private->data_mutex);
2394 INIT_DELAYED_WORK(&private->work, scarlett2_config_save_work);
2396 mixer->private_data = private;
2397 mixer->private_free = scarlett2_private_free;
2398 mixer->private_suspend = scarlett2_private_suspend;
2400 private->info = info;
2401 scarlett2_count_mux_io(private);
2402 private->scarlett2_seq = 0;
2403 private->mixer = mixer;
2405 return scarlett2_find_fc_interface(mixer->chip->dev, private);
2408 /* Cargo cult proprietary initialisation sequence */
2409 static int scarlett2_usb_init(struct usb_mixer_interface *mixer)
2411 struct usb_device *dev = mixer->chip->dev;
2412 struct scarlett2_data *private = mixer->private_data;
2416 if (usb_pipe_type_check(dev, usb_sndctrlpipe(dev, 0)))
2420 err = scarlett2_usb_rx(dev, private->bInterfaceNumber,
2421 SCARLETT2_USB_CMD_INIT, buf, sizeof(buf));
2426 private->scarlett2_seq = 1;
2427 err = scarlett2_usb(mixer, SCARLETT2_USB_INIT_1, NULL, 0, NULL, 0);
2432 private->scarlett2_seq = 1;
2433 return scarlett2_usb(mixer, SCARLETT2_USB_INIT_2, NULL, 0, NULL, 84);
2436 /* Read configuration from the interface on start */
2437 static int scarlett2_read_configs(struct usb_mixer_interface *mixer)
2439 struct scarlett2_data *private = mixer->private_data;
2440 const struct scarlett2_device_info *info = private->info;
2441 const int (*port_count)[SCARLETT2_PORT_DIRNS] = info->port_count;
2443 port_count[SCARLETT2_PORT_TYPE_ANALOGUE][SCARLETT2_PORT_OUT];
2445 port_count[SCARLETT2_PORT_TYPE_MIX][SCARLETT2_PORT_IN];
2446 struct scarlett2_usb_volume_status volume_status;
2449 if (info->level_input_count) {
2450 err = scarlett2_usb_get_config(
2452 SCARLETT2_CONFIG_LEVEL_SWITCH,
2453 info->level_input_count,
2454 private->level_switch);
2459 if (info->pad_input_count) {
2460 err = scarlett2_usb_get_config(
2462 SCARLETT2_CONFIG_PAD_SWITCH,
2463 info->pad_input_count,
2464 private->pad_switch);
2469 err = scarlett2_update_sync(mixer);
2473 err = scarlett2_usb_get_volume_status(mixer, &volume_status);
2477 if (info->line_out_hw_vol)
2478 for (i = 0; i < SCARLETT2_DIM_MUTE_COUNT; i++)
2479 private->dim_mute[i] = !!volume_status.dim_mute[i];
2481 private->master_vol = clamp(
2482 volume_status.master_vol + SCARLETT2_VOLUME_BIAS,
2483 0, SCARLETT2_VOLUME_BIAS);
2485 for (i = 0; i < num_line_out; i++) {
2488 private->vol_sw_hw_switch[i] =
2489 info->line_out_hw_vol
2490 && volume_status.sw_hw_switch[i];
2492 volume = private->vol_sw_hw_switch[i]
2493 ? volume_status.master_vol
2494 : volume_status.sw_vol[i];
2495 volume = clamp(volume + SCARLETT2_VOLUME_BIAS,
2496 0, SCARLETT2_VOLUME_BIAS);
2497 private->vol[i] = volume;
2499 mute = private->vol_sw_hw_switch[i]
2500 ? private->dim_mute[SCARLETT2_BUTTON_MUTE]
2501 : volume_status.mute_switch[i];
2502 private->mute_switch[i] = mute;
2505 for (i = 0; i < num_mixer_out; i++) {
2506 err = scarlett2_usb_get_mix(mixer, i);
2511 return scarlett2_usb_get_mux(mixer);
2514 /* Notify on sync change */
2515 static void scarlett2_notify_sync(
2516 struct usb_mixer_interface *mixer)
2518 struct scarlett2_data *private = mixer->private_data;
2520 private->sync_updated = 1;
2522 snd_ctl_notify(mixer->chip->card, SNDRV_CTL_EVENT_MASK_VALUE,
2523 &private->sync_ctl->id);
2526 /* Notify on monitor change */
2527 static void scarlett2_notify_monitor(
2528 struct usb_mixer_interface *mixer)
2530 struct scarlett2_data *private = mixer->private_data;
2531 const struct scarlett2_device_info *info = private->info;
2532 const int (*port_count)[SCARLETT2_PORT_DIRNS] = info->port_count;
2534 port_count[SCARLETT2_PORT_TYPE_ANALOGUE][SCARLETT2_PORT_OUT];
2537 /* if line_out_hw_vol is 0, there are no controls to update */
2538 if (!info->line_out_hw_vol)
2541 private->vol_updated = 1;
2543 snd_ctl_notify(mixer->chip->card, SNDRV_CTL_EVENT_MASK_VALUE,
2544 &private->master_vol_ctl->id);
2546 for (i = 0; i < num_line_out; i++) {
2547 if (!private->vol_sw_hw_switch[i])
2549 snd_ctl_notify(mixer->chip->card, SNDRV_CTL_EVENT_MASK_VALUE,
2550 &private->vol_ctls[i]->id);
2554 /* Notify on dim/mute change */
2555 static void scarlett2_notify_dim_mute(
2556 struct usb_mixer_interface *mixer)
2558 struct snd_card *card = mixer->chip->card;
2559 struct scarlett2_data *private = mixer->private_data;
2560 const struct scarlett2_device_info *info = private->info;
2561 const int (*port_count)[SCARLETT2_PORT_DIRNS] = info->port_count;
2563 port_count[SCARLETT2_PORT_TYPE_ANALOGUE][SCARLETT2_PORT_OUT];
2566 private->vol_updated = 1;
2568 if (!info->line_out_hw_vol)
2571 for (i = 0; i < SCARLETT2_DIM_MUTE_COUNT; i++)
2572 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE,
2573 &private->dim_mute_ctls[i]->id);
2575 for (i = 0; i < num_line_out; i++)
2576 if (private->vol_sw_hw_switch[i])
2577 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE,
2578 &private->mute_ctls[i]->id);
2581 /* Interrupt callback */
2582 static void scarlett2_notify(struct urb *urb)
2584 struct usb_mixer_interface *mixer = urb->context;
2585 int len = urb->actual_length;
2586 int ustatus = urb->status;
2589 if (ustatus != 0 || len != 8)
2592 data = le32_to_cpu(*(__le32 *)urb->transfer_buffer);
2593 if (data & SCARLETT2_USB_NOTIFY_SYNC)
2594 scarlett2_notify_sync(mixer);
2595 if (data & SCARLETT2_USB_NOTIFY_MONITOR)
2596 scarlett2_notify_monitor(mixer);
2597 if (data & SCARLETT2_USB_NOTIFY_DIM_MUTE)
2598 scarlett2_notify_dim_mute(mixer);
2601 if (ustatus != -ENOENT &&
2602 ustatus != -ECONNRESET &&
2603 ustatus != -ESHUTDOWN) {
2604 urb->dev = mixer->chip->dev;
2605 usb_submit_urb(urb, GFP_ATOMIC);
2609 static int scarlett2_init_notify(struct usb_mixer_interface *mixer)
2611 struct usb_device *dev = mixer->chip->dev;
2612 struct scarlett2_data *private = mixer->private_data;
2613 unsigned int pipe = usb_rcvintpipe(dev, private->bEndpointAddress);
2614 void *transfer_buffer;
2617 usb_audio_err(mixer->chip,
2618 "%s: mixer urb already in use!\n", __func__);
2622 if (usb_pipe_type_check(dev, pipe))
2625 mixer->urb = usb_alloc_urb(0, GFP_KERNEL);
2629 transfer_buffer = kmalloc(private->wMaxPacketSize, GFP_KERNEL);
2630 if (!transfer_buffer)
2633 usb_fill_int_urb(mixer->urb, dev, pipe,
2634 transfer_buffer, private->wMaxPacketSize,
2635 scarlett2_notify, mixer, private->bInterval);
2637 return usb_submit_urb(mixer->urb, GFP_KERNEL);
2640 static int snd_scarlett_gen2_controls_create(struct usb_mixer_interface *mixer)
2642 const struct scarlett2_device_info **info = scarlett2_devices;
2645 /* Find device in scarlett2_devices */
2646 while (*info && (*info)->usb_id != mixer->chip->usb_id)
2651 /* Initialise private data */
2652 err = scarlett2_init_private(mixer, *info);
2656 /* Send proprietary USB initialisation sequence */
2657 err = scarlett2_usb_init(mixer);
2661 /* Read volume levels and controls from the interface */
2662 err = scarlett2_read_configs(mixer);
2666 /* Create the analogue output controls */
2667 err = scarlett2_add_line_out_ctls(mixer);
2671 /* Create the analogue input controls */
2672 err = scarlett2_add_line_in_ctls(mixer);
2676 /* Create the input, output, and mixer mux input selections */
2677 err = scarlett2_add_mux_enums(mixer);
2681 /* Create the matrix mixer controls */
2682 err = scarlett2_add_mixer_ctls(mixer);
2686 /* Create the level meter controls */
2687 err = scarlett2_add_meter_ctl(mixer);
2691 /* Create the sync control */
2692 err = scarlett2_add_sync_ctl(mixer);
2696 /* Set up the interrupt polling */
2697 err = scarlett2_init_notify(mixer);
2704 int snd_scarlett_gen2_init(struct usb_mixer_interface *mixer)
2706 struct snd_usb_audio *chip = mixer->chip;
2709 /* only use UAC_VERSION_2 */
2710 if (!mixer->protocol)
2713 if (!(chip->setup & SCARLETT2_ENABLE)) {
2714 usb_audio_info(chip,
2715 "Focusrite Scarlett Gen 2/3 Mixer Driver disabled; "
2716 "use options snd_usb_audio vid=0x%04x pid=0x%04x "
2717 "device_setup=1 to enable and report any issues "
2719 USB_ID_VENDOR(chip->usb_id),
2720 USB_ID_PRODUCT(chip->usb_id));
2724 usb_audio_info(chip,
2725 "Focusrite Scarlett Gen 2/3 Mixer Driver enabled pid=0x%04x",
2726 USB_ID_PRODUCT(chip->usb_id));
2728 err = snd_scarlett_gen2_controls_create(mixer);
2730 usb_audio_err(mixer->chip,
2731 "Error initialising Scarlett Mixer Driver: %d",