1 // SPDX-License-Identifier: GPL-2.0
3 * Focusrite Scarlett 2 Protocol Driver for ALSA
4 * (including Scarlett 2nd Gen, 3rd Gen, 4th Gen, Clarett USB, and
5 * Clarett+ series products)
8 * - 6i6/18i8/18i20 Gen 2
9 * - Solo/2i2/4i4/8i6/18i8/18i20 Gen 3
10 * - Solo/2i2/4i4 Gen 4
11 * - Clarett 2Pre/4Pre/8Pre USB
12 * - Clarett+ 2Pre/4Pre/8Pre
14 * Copyright (c) 2018-2023 by Geoffrey D. Bennett <g at b4.vu>
15 * Copyright (c) 2020-2021 by Vladimir Sadovnikov <sadko4u@gmail.com>
16 * Copyright (c) 2022 by Christian Colglazier <christian@cacolglazier.com>
18 * Based on the Scarlett (Gen 1) Driver for ALSA:
20 * Copyright (c) 2013 by Tobias Hoffmann
21 * Copyright (c) 2013 by Robin Gareus <robin at gareus.org>
22 * Copyright (c) 2002 by Takashi Iwai <tiwai at suse.de>
23 * Copyright (c) 2014 by Chris J Arges <chris.j.arges at canonical.com>
25 * Many codes borrowed from audio.c by
26 * Alan Cox (alan at lxorguk.ukuu.org.uk)
27 * Thomas Sailer (sailer at ife.ee.ethz.ch)
30 * David Henningsson <david.henningsson at canonical.com>
33 /* The protocol was reverse engineered by looking at the communication
34 * between Focusrite Control 2.3.4 and the Focusrite(R) Scarlett 18i20
35 * (firmware 1083) using usbmon in July-August 2018.
37 * Scarlett 18i8 support added in April 2019.
39 * Scarlett 6i6 support added in June 2019 (thanks to Martin Wittmann
40 * for providing usbmon output and testing).
42 * Scarlett 4i4/8i6 Gen 3 support added in May 2020 (thanks to Laurent
43 * Debricon for donating a 4i4 and to Fredrik Unger for providing 8i6
44 * usbmon output and testing).
46 * Scarlett 18i8/18i20 Gen 3 support added in June 2020 (thanks to
47 * Darren Jaeckel, Alex Sedlack, and Clovis Lunel for providing usbmon
48 * output, protocol traces and testing).
50 * Support for loading mixer volume and mux configuration from the
51 * interface during driver initialisation added in May 2021 (thanks to
52 * Vladimir Sadovnikov for figuring out how).
54 * Support for Solo/2i2 Gen 3 added in May 2021 (thanks to Alexander
55 * Vorona for 2i2 protocol traces).
57 * Support for phantom power, direct monitoring, speaker switching,
58 * and talkback added in May-June 2021.
60 * Support for Clarett+ 8Pre added in Aug 2022 by Christian
63 * Support for Clarett 8Pre USB added in Sep 2023 (thanks to Philippe
64 * Perrot for confirmation).
66 * Support for Clarett+ 4Pre and 2Pre added in Sep 2023 (thanks to
67 * Gregory Rozzo for donating a 4Pre, and David Sherwood and Patrice
68 * Peterson for usbmon output).
70 * Support for Clarett 2Pre and 4Pre USB added in Oct 2023.
72 * Support for firmware updates added in Dec 2023.
74 * Support for Scarlett Solo/2i2/4i4 Gen 4 added in Dec 2023 (thanks
75 * to many LinuxMusicians people and to Focusrite for hardware
78 * This ALSA mixer gives access to (model-dependent):
79 * - input, output, mixer-matrix muxes
80 * - mixer-matrix gain stages
81 * - gain/volume/mute controls
83 * - line/inst level, pad, and air controls
84 * - phantom power, direct monitor, speaker switching, and talkback
86 * - disable/enable MSD mode
87 * - disable/enable standalone mode
88 * - input gain, autogain, safe mode
89 * - direct monitor mixes
92 * /--------------\ 18chn 20chn /--------------\
93 * | Hardware in +--+------\ /-------------+--+ ALSA PCM out |
94 * \--------------/ | | | | \--------------/
98 * | +---------------+ | |
99 * | \ Matrix Mux / | |
100 * | +-----+-----+ | |
106 * | +------------+ | |
110 * | | 18x10 Gain | | |
112 * | +-----+------+ | |
114 * |18chn |10chn | |20chn
119 * ===========================
120 * +---------------+ +--—------------+
121 * \ Output Mux / \ Capture Mux /
122 * +---+---+---+ +-----+-----+
126 * /--------------\ | | | /--------------\
127 * | S/PDIF, ADAT |<--/ |10chn \-->| ALSA PCM in |
128 * | Hardware out | | \--------------/
131 * +-------------+ Software gain per channel.
132 * | Master Gain |<-- 18i20 only: Switch per channel
133 * +------+------+ to select HW or SW gain control.
137 * | Analogue |<------/
142 * Gen 3/4 devices have a Mass Storage Device (MSD) mode where a small
143 * disk with registration and driver download information is presented
144 * to the host. To access the full functionality of the device without
145 * proprietary software, MSD mode can be disabled by:
146 * - holding down the 48V button for five seconds while powering on
148 * - using this driver and alsamixer to change the "MSD Mode" setting
149 * to Off and power-cycling the device
152 #include <linux/slab.h>
153 #include <linux/usb.h>
154 #include <linux/moduleparam.h>
156 #include <sound/control.h>
157 #include <sound/tlv.h>
158 #include <sound/hwdep.h>
160 #include <uapi/sound/scarlett2.h>
162 #include "usbaudio.h"
166 #include "mixer_scarlett2.h"
168 /* device_setup value to allow turning MSD mode back on */
169 #define SCARLETT2_MSD_ENABLE 0x02
171 /* device_setup value to disable this mixer driver */
172 #define SCARLETT2_DISABLE 0x04
174 /* some gui mixers can't handle negative ctl values */
175 #define SCARLETT2_VOLUME_BIAS 127
177 /* maximum preamp input gain and value
178 * values are from 0 to 70, preamp gain is from 0 to 69 dB
180 #define SCARLETT2_MAX_GAIN_VALUE 70
181 #define SCARLETT2_MAX_GAIN_DB 69
183 /* mixer range from -80dB to +6dB in 0.5dB steps */
184 #define SCARLETT2_MIXER_MIN_DB -80
185 #define SCARLETT2_MIXER_BIAS (-SCARLETT2_MIXER_MIN_DB * 2)
186 #define SCARLETT2_MIXER_MAX_DB 6
187 #define SCARLETT2_MIXER_MAX_VALUE \
188 ((SCARLETT2_MIXER_MAX_DB - SCARLETT2_MIXER_MIN_DB) * 2)
189 #define SCARLETT2_MIXER_VALUE_COUNT (SCARLETT2_MIXER_MAX_VALUE + 1)
191 /* map from (dB + 80) * 2 to mixer value
192 * for dB in 0 .. 172: int(8192 * pow(10, ((dB - 160) / 2 / 20)))
194 static const u16 scarlett2_mixer_values[SCARLETT2_MIXER_VALUE_COUNT] = {
195 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2,
196 2, 2, 3, 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 6, 6, 6, 7, 7, 8, 8,
197 9, 9, 10, 10, 11, 12, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21,
198 23, 24, 25, 27, 29, 30, 32, 34, 36, 38, 41, 43, 46, 48, 51,
199 54, 57, 61, 65, 68, 73, 77, 81, 86, 91, 97, 103, 109, 115,
200 122, 129, 137, 145, 154, 163, 173, 183, 194, 205, 217, 230,
201 244, 259, 274, 290, 307, 326, 345, 365, 387, 410, 434, 460,
202 487, 516, 547, 579, 614, 650, 689, 730, 773, 819, 867, 919,
203 973, 1031, 1092, 1157, 1225, 1298, 1375, 1456, 1543, 1634,
204 1731, 1833, 1942, 2057, 2179, 2308, 2445, 2590, 2744, 2906,
205 3078, 3261, 3454, 3659, 3876, 4105, 4349, 4606, 4879, 5168,
206 5475, 5799, 6143, 6507, 6892, 7301, 7733, 8192, 8677, 9191,
207 9736, 10313, 10924, 11571, 12257, 12983, 13752, 14567, 15430,
211 /* Maximum number of analogue outputs */
212 #define SCARLETT2_ANALOGUE_MAX 10
214 /* Maximum number of various input controls */
215 #define SCARLETT2_LEVEL_SWITCH_MAX 2
216 #define SCARLETT2_PAD_SWITCH_MAX 8
217 #define SCARLETT2_AIR_SWITCH_MAX 8
218 #define SCARLETT2_PHANTOM_SWITCH_MAX 2
219 #define SCARLETT2_INPUT_GAIN_MAX 2
221 /* Maximum number of inputs to the mixer */
222 #define SCARLETT2_INPUT_MIX_MAX 25
224 /* Maximum number of outputs from the mixer */
225 #define SCARLETT2_OUTPUT_MIX_MAX 12
227 /* Maximum number of mixer gain controls */
228 #define SCARLETT2_MIX_MAX (SCARLETT2_INPUT_MIX_MAX * SCARLETT2_OUTPUT_MIX_MAX)
230 /* Maximum number of direct monitor mixer gain controls
231 * 1 (Solo) or 2 (2i2) direct monitor selections (Mono & Stereo)
232 * 2 Mix outputs (A/Left & B/Right)
235 #define SCARLETT2_MONITOR_MIX_MAX (2 * 2 * 4)
237 /* Maximum size of the data in the USB mux assignment message:
238 * 20 inputs, 20 outputs, 25 matrix inputs, 12 spare
240 #define SCARLETT2_MUX_MAX 77
242 /* Maximum number of sources (sum of input port counts) */
243 #define SCARLETT2_MAX_SRCS 52
245 /* Maximum number of meters (sum of output port counts) */
246 #define SCARLETT2_MAX_METERS 65
248 /* Hardware port types:
249 * - None (no input to mux)
257 SCARLETT2_PORT_TYPE_NONE,
258 SCARLETT2_PORT_TYPE_ANALOGUE,
259 SCARLETT2_PORT_TYPE_SPDIF,
260 SCARLETT2_PORT_TYPE_ADAT,
261 SCARLETT2_PORT_TYPE_MIX,
262 SCARLETT2_PORT_TYPE_PCM,
263 SCARLETT2_PORT_TYPE_COUNT
266 /* I/O count of each port type kept in struct scarlett2_ports */
273 /* Dim/Mute buttons on the 18i20 */
275 SCARLETT2_BUTTON_MUTE,
276 SCARLETT2_BUTTON_DIM,
277 SCARLETT2_DIM_MUTE_COUNT
280 /* Flash Write State */
282 SCARLETT2_FLASH_WRITE_STATE_IDLE,
283 SCARLETT2_FLASH_WRITE_STATE_SELECTED,
284 SCARLETT2_FLASH_WRITE_STATE_ERASING,
285 SCARLETT2_FLASH_WRITE_STATE_WRITE
288 static const char *const scarlett2_dim_mute_names[SCARLETT2_DIM_MUTE_COUNT] = {
289 "Mute Playback Switch", "Dim Playback Switch"
292 /* The autogain_status is set based on the autogain_switch and
293 * raw_autogain_status values.
295 * If autogain_switch is set, autogain_status is set to 0 (Running).
296 * The other status values are from the raw_autogain_status value + 1.
298 static const char *const scarlett2_autogain_status_texts[] = {
310 /* Power Status Values */
312 SCARLETT2_POWER_STATUS_EXT,
313 SCARLETT2_POWER_STATUS_BUS,
314 SCARLETT2_POWER_STATUS_FAIL,
315 SCARLETT2_POWER_STATUS_COUNT
318 /* Notification callback functions */
319 struct scarlett2_notification {
321 void (*func)(struct usb_mixer_interface *mixer);
324 static void scarlett2_notify_sync(struct usb_mixer_interface *mixer);
325 static void scarlett2_notify_dim_mute(struct usb_mixer_interface *mixer);
326 static void scarlett2_notify_monitor(struct usb_mixer_interface *mixer);
327 static void scarlett2_notify_volume(struct usb_mixer_interface *mixer);
328 static void scarlett2_notify_input_level(struct usb_mixer_interface *mixer);
329 static void scarlett2_notify_input_pad(struct usb_mixer_interface *mixer);
330 static void scarlett2_notify_input_air(struct usb_mixer_interface *mixer);
331 static void scarlett2_notify_input_phantom(struct usb_mixer_interface *mixer);
332 static void scarlett2_notify_input_other(struct usb_mixer_interface *mixer);
333 static void scarlett2_notify_input_select(struct usb_mixer_interface *mixer);
334 static void scarlett2_notify_input_gain(struct usb_mixer_interface *mixer);
335 static void scarlett2_notify_autogain(struct usb_mixer_interface *mixer);
336 static void scarlett2_notify_input_safe(struct usb_mixer_interface *mixer);
337 static void scarlett2_notify_monitor_other(struct usb_mixer_interface *mixer);
338 static void scarlett2_notify_direct_monitor(struct usb_mixer_interface *mixer);
339 static void scarlett2_notify_power_status(struct usb_mixer_interface *mixer);
340 static void scarlett2_notify_pcm_input_switch(
341 struct usb_mixer_interface *mixer);
343 /* Arrays of notification callback functions */
345 static const struct scarlett2_notification scarlett2_notifications[] = {
346 { 0x00000001, NULL }, /* ack, gets ignored */
347 { 0x00000008, scarlett2_notify_sync },
348 { 0x00200000, scarlett2_notify_dim_mute },
349 { 0x00400000, scarlett2_notify_monitor },
350 { 0x00800000, scarlett2_notify_input_other },
351 { 0x01000000, scarlett2_notify_monitor_other },
355 static const struct scarlett2_notification scarlett3a_notifications[] = {
356 { 0x00000001, NULL }, /* ack, gets ignored */
357 { 0x00800000, scarlett2_notify_input_other },
358 { 0x01000000, scarlett2_notify_direct_monitor },
362 static const struct scarlett2_notification scarlett4_solo_notifications[] = {
363 { 0x00000001, NULL }, /* ack, gets ignored */
364 { 0x00000008, scarlett2_notify_sync },
365 { 0x00400000, scarlett2_notify_input_air },
366 { 0x00800000, scarlett2_notify_direct_monitor },
367 { 0x01000000, scarlett2_notify_input_level },
368 { 0x02000000, scarlett2_notify_input_phantom },
369 { 0x04000000, scarlett2_notify_pcm_input_switch },
373 static const struct scarlett2_notification scarlett4_2i2_notifications[] = {
374 { 0x00000001, NULL }, /* ack, gets ignored */
375 { 0x00000008, scarlett2_notify_sync },
376 { 0x00200000, scarlett2_notify_input_safe },
377 { 0x00400000, scarlett2_notify_autogain },
378 { 0x00800000, scarlett2_notify_input_air },
379 { 0x01000000, scarlett2_notify_direct_monitor },
380 { 0x02000000, scarlett2_notify_input_select },
381 { 0x04000000, scarlett2_notify_input_level },
382 { 0x08000000, scarlett2_notify_input_phantom },
383 { 0x10000000, NULL }, /* power status, ignored */
384 { 0x40000000, scarlett2_notify_input_gain },
385 { 0x80000000, NULL }, /* power status, ignored */
389 static const struct scarlett2_notification scarlett4_4i4_notifications[] = {
390 { 0x00000001, NULL }, /* ack, gets ignored */
391 { 0x00000008, scarlett2_notify_sync },
392 { 0x00200000, scarlett2_notify_input_safe },
393 { 0x00400000, scarlett2_notify_autogain },
394 { 0x00800000, scarlett2_notify_input_air },
395 { 0x01000000, scarlett2_notify_input_select },
396 { 0x02000000, scarlett2_notify_input_level },
397 { 0x04000000, scarlett2_notify_input_phantom },
398 { 0x08000000, scarlett2_notify_power_status }, /* power external */
399 { 0x20000000, scarlett2_notify_input_gain },
400 { 0x40000000, scarlett2_notify_power_status }, /* power status */
401 { 0x80000000, scarlett2_notify_volume },
405 /* Configuration parameters that can be read and written */
407 SCARLETT2_CONFIG_DIM_MUTE,
408 SCARLETT2_CONFIG_LINE_OUT_VOLUME,
409 SCARLETT2_CONFIG_MUTE_SWITCH,
410 SCARLETT2_CONFIG_SW_HW_SWITCH,
411 SCARLETT2_CONFIG_MASTER_VOLUME,
412 SCARLETT2_CONFIG_HEADPHONE_VOLUME,
413 SCARLETT2_CONFIG_LEVEL_SWITCH,
414 SCARLETT2_CONFIG_PAD_SWITCH,
415 SCARLETT2_CONFIG_MSD_SWITCH,
416 SCARLETT2_CONFIG_AIR_SWITCH,
417 SCARLETT2_CONFIG_STANDALONE_SWITCH,
418 SCARLETT2_CONFIG_PHANTOM_SWITCH,
419 SCARLETT2_CONFIG_PHANTOM_PERSISTENCE,
420 SCARLETT2_CONFIG_DIRECT_MONITOR,
421 SCARLETT2_CONFIG_MONITOR_OTHER_SWITCH,
422 SCARLETT2_CONFIG_MONITOR_OTHER_ENABLE,
423 SCARLETT2_CONFIG_TALKBACK_MAP,
424 SCARLETT2_CONFIG_AUTOGAIN_SWITCH,
425 SCARLETT2_CONFIG_AUTOGAIN_STATUS,
426 SCARLETT2_CONFIG_INPUT_GAIN,
427 SCARLETT2_CONFIG_SAFE_SWITCH,
428 SCARLETT2_CONFIG_INPUT_SELECT_SWITCH,
429 SCARLETT2_CONFIG_INPUT_LINK_SWITCH,
430 SCARLETT2_CONFIG_POWER_EXT,
431 SCARLETT2_CONFIG_POWER_LOW,
432 SCARLETT2_CONFIG_PCM_INPUT_SWITCH,
433 SCARLETT2_CONFIG_DIRECT_MONITOR_GAIN,
434 SCARLETT2_CONFIG_COUNT
437 /* Location, size, and activation command number for the configuration
438 * parameters. Size is in bits and may be 0, 1, 8, or 16.
440 * A size of 0 indicates that the parameter is a byte-sized Scarlett
441 * Gen 4 configuration which is written through the gen4_write_addr
442 * location (but still read through the given offset location).
444 * Some Gen 4 configuration parameters are written with 0x02 for a
445 * desired value of 0x01, and 0x03 for 0x00. These are indicated with
446 * mute set to 1. 0x02 and 0x03 are temporary values while the device
447 * makes the change and the channel and/or corresponding DSP channel
450 struct scarlett2_config {
457 struct scarlett2_config_set {
458 const struct scarlett2_notification *notifications;
460 const struct scarlett2_config items[SCARLETT2_CONFIG_COUNT];
463 /* Gen 2 devices without SW/HW volume switch: 6i6, 18i8 */
465 static const struct scarlett2_config_set scarlett2_config_set_gen2a = {
466 .notifications = scarlett2_notifications,
468 [SCARLETT2_CONFIG_LINE_OUT_VOLUME] = {
469 .offset = 0x34, .size = 16, .activate = 1 },
471 [SCARLETT2_CONFIG_MUTE_SWITCH] = {
472 .offset = 0x5c, .size = 8, .activate = 1 },
474 [SCARLETT2_CONFIG_LEVEL_SWITCH] = {
475 .offset = 0x7c, .size = 8, .activate = 7 },
477 [SCARLETT2_CONFIG_PAD_SWITCH] = {
478 .offset = 0x84, .size = 8, .activate = 8 },
480 [SCARLETT2_CONFIG_STANDALONE_SWITCH] = {
481 .offset = 0x8d, .size = 8, .activate = 6 },
485 /* Gen 2 devices with SW/HW volume switch: 18i20 */
487 static const struct scarlett2_config_set scarlett2_config_set_gen2b = {
488 .notifications = scarlett2_notifications,
490 [SCARLETT2_CONFIG_DIM_MUTE] = {
491 .offset = 0x31, .size = 8, .activate = 2 },
493 [SCARLETT2_CONFIG_LINE_OUT_VOLUME] = {
494 .offset = 0x34, .size = 16, .activate = 1 },
496 [SCARLETT2_CONFIG_MUTE_SWITCH] = {
497 .offset = 0x5c, .size = 8, .activate = 1 },
499 [SCARLETT2_CONFIG_SW_HW_SWITCH] = {
500 .offset = 0x66, .size = 8, .activate = 3 },
502 [SCARLETT2_CONFIG_MASTER_VOLUME] = {
503 .offset = 0x76, .size = 16 },
505 [SCARLETT2_CONFIG_LEVEL_SWITCH] = {
506 .offset = 0x7c, .size = 8, .activate = 7 },
508 [SCARLETT2_CONFIG_PAD_SWITCH] = {
509 .offset = 0x84, .size = 8, .activate = 8 },
511 [SCARLETT2_CONFIG_STANDALONE_SWITCH] = {
512 .offset = 0x8d, .size = 8, .activate = 6 },
516 /* Gen 3 devices without a mixer (Solo and 2i2) */
517 static const struct scarlett2_config_set scarlett2_config_set_gen3a = {
518 .notifications = scarlett3a_notifications,
520 [SCARLETT2_CONFIG_MSD_SWITCH] = {
521 .offset = 0x04, .size = 8, .activate = 6 },
523 [SCARLETT2_CONFIG_PHANTOM_PERSISTENCE] = {
524 .offset = 0x05, .size = 8, .activate = 6 },
526 [SCARLETT2_CONFIG_PHANTOM_SWITCH] = {
527 .offset = 0x06, .size = 8, .activate = 3 },
529 [SCARLETT2_CONFIG_DIRECT_MONITOR] = {
530 .offset = 0x07, .size = 8, .activate = 4 },
532 [SCARLETT2_CONFIG_LEVEL_SWITCH] = {
533 .offset = 0x08, .size = 1, .activate = 7 },
535 [SCARLETT2_CONFIG_AIR_SWITCH] = {
536 .offset = 0x09, .size = 1, .activate = 8 },
540 /* Gen 3 devices without SW/HW volume switch: 4i4, 8i6 */
541 static const struct scarlett2_config_set scarlett2_config_set_gen3b = {
542 .notifications = scarlett2_notifications,
544 [SCARLETT2_CONFIG_LINE_OUT_VOLUME] = {
545 .offset = 0x34, .size = 16, .activate = 1 },
547 [SCARLETT2_CONFIG_MUTE_SWITCH] = {
548 .offset = 0x5c, .size = 8, .activate = 1 },
550 [SCARLETT2_CONFIG_LEVEL_SWITCH] = {
551 .offset = 0x7c, .size = 8, .activate = 7 },
553 [SCARLETT2_CONFIG_PAD_SWITCH] = {
554 .offset = 0x84, .size = 8, .activate = 8 },
556 [SCARLETT2_CONFIG_AIR_SWITCH] = {
557 .offset = 0x8c, .size = 8, .activate = 8 },
559 [SCARLETT2_CONFIG_STANDALONE_SWITCH] = {
560 .offset = 0x95, .size = 8, .activate = 6 },
562 [SCARLETT2_CONFIG_PHANTOM_SWITCH] = {
563 .offset = 0x9c, .size = 1, .activate = 8 },
565 [SCARLETT2_CONFIG_MSD_SWITCH] = {
566 .offset = 0x9d, .size = 8, .activate = 6 },
568 [SCARLETT2_CONFIG_PHANTOM_PERSISTENCE] = {
569 .offset = 0x9e, .size = 8, .activate = 6 },
573 /* Gen 3 devices with SW/HW volume switch: 18i8, 18i20 */
574 static const struct scarlett2_config_set scarlett2_config_set_gen3c = {
575 .notifications = scarlett2_notifications,
577 [SCARLETT2_CONFIG_DIM_MUTE] = {
578 .offset = 0x31, .size = 8, .activate = 2 },
580 [SCARLETT2_CONFIG_LINE_OUT_VOLUME] = {
581 .offset = 0x34, .size = 16, .activate = 1 },
583 [SCARLETT2_CONFIG_MUTE_SWITCH] = {
584 .offset = 0x5c, .size = 8, .activate = 1 },
586 [SCARLETT2_CONFIG_SW_HW_SWITCH] = {
587 .offset = 0x66, .size = 8, .activate = 3 },
589 [SCARLETT2_CONFIG_MASTER_VOLUME] = {
590 .offset = 0x76, .size = 16 },
592 [SCARLETT2_CONFIG_LEVEL_SWITCH] = {
593 .offset = 0x7c, .size = 8, .activate = 7 },
595 [SCARLETT2_CONFIG_PAD_SWITCH] = {
596 .offset = 0x84, .size = 8, .activate = 8 },
598 [SCARLETT2_CONFIG_AIR_SWITCH] = {
599 .offset = 0x8c, .size = 8, .activate = 8 },
601 [SCARLETT2_CONFIG_STANDALONE_SWITCH] = {
602 .offset = 0x95, .size = 8, .activate = 6 },
604 [SCARLETT2_CONFIG_PHANTOM_SWITCH] = {
605 .offset = 0x9c, .size = 1, .activate = 8 },
607 [SCARLETT2_CONFIG_MSD_SWITCH] = {
608 .offset = 0x9d, .size = 8, .activate = 6 },
610 [SCARLETT2_CONFIG_PHANTOM_PERSISTENCE] = {
611 .offset = 0x9e, .size = 8, .activate = 6 },
613 [SCARLETT2_CONFIG_MONITOR_OTHER_SWITCH] = {
614 .offset = 0x9f, .size = 1, .activate = 10 },
616 [SCARLETT2_CONFIG_MONITOR_OTHER_ENABLE] = {
617 .offset = 0xa0, .size = 1, .activate = 10 },
619 [SCARLETT2_CONFIG_TALKBACK_MAP] = {
620 .offset = 0xb0, .size = 16, .activate = 10 },
625 static const struct scarlett2_config_set scarlett2_config_set_gen4_solo = {
626 .notifications = scarlett4_solo_notifications,
627 .gen4_write_addr = 0xd8,
629 [SCARLETT2_CONFIG_MSD_SWITCH] = {
630 .offset = 0x47, .size = 8, .activate = 4 },
632 [SCARLETT2_CONFIG_DIRECT_MONITOR] = {
633 .offset = 0x108, .activate = 12 },
635 [SCARLETT2_CONFIG_PHANTOM_SWITCH] = {
636 .offset = 0x46, .activate = 9, .mute = 1 },
638 [SCARLETT2_CONFIG_LEVEL_SWITCH] = {
639 .offset = 0x3d, .activate = 10, .mute = 1 },
641 [SCARLETT2_CONFIG_AIR_SWITCH] = {
642 .offset = 0x3e, .activate = 11 },
644 [SCARLETT2_CONFIG_PCM_INPUT_SWITCH] = {
645 .offset = 0x206, .activate = 25 },
647 [SCARLETT2_CONFIG_DIRECT_MONITOR_GAIN] = {
648 .offset = 0x232, .size = 16, .activate = 26 }
653 static const struct scarlett2_config_set scarlett2_config_set_gen4_2i2 = {
654 .notifications = scarlett4_2i2_notifications,
655 .gen4_write_addr = 0xfc,
657 [SCARLETT2_CONFIG_MSD_SWITCH] = {
658 .offset = 0x49, .size = 8, .activate = 4 }, // 0x41 ??
660 [SCARLETT2_CONFIG_DIRECT_MONITOR] = {
661 .offset = 0x14a, .activate = 16 },
663 [SCARLETT2_CONFIG_AUTOGAIN_SWITCH] = {
664 .offset = 0x135, .activate = 10 },
666 [SCARLETT2_CONFIG_AUTOGAIN_STATUS] = {
669 [SCARLETT2_CONFIG_PHANTOM_SWITCH] = {
670 .offset = 0x48, .activate = 11, .mute = 1 },
672 [SCARLETT2_CONFIG_INPUT_GAIN] = {
673 .offset = 0x4b, .activate = 12 },
675 [SCARLETT2_CONFIG_LEVEL_SWITCH] = {
676 .offset = 0x3c, .activate = 13, .mute = 1 },
678 [SCARLETT2_CONFIG_SAFE_SWITCH] = {
679 .offset = 0x147, .activate = 14 },
681 [SCARLETT2_CONFIG_AIR_SWITCH] = {
682 .offset = 0x3e, .activate = 15 },
684 [SCARLETT2_CONFIG_INPUT_SELECT_SWITCH] = {
685 .offset = 0x14b, .activate = 17 },
687 [SCARLETT2_CONFIG_INPUT_LINK_SWITCH] = {
688 .offset = 0x14e, .activate = 18 },
690 [SCARLETT2_CONFIG_DIRECT_MONITOR_GAIN] = {
691 .offset = 0x2a0, .size = 16, .activate = 36 }
696 static const struct scarlett2_config_set scarlett2_config_set_gen4_4i4 = {
697 .notifications = scarlett4_4i4_notifications,
698 .gen4_write_addr = 0x130,
700 [SCARLETT2_CONFIG_MSD_SWITCH] = {
701 .offset = 0x5c, .size = 8, .activate = 4 },
703 [SCARLETT2_CONFIG_AUTOGAIN_SWITCH] = {
704 .offset = 0x13e, .activate = 10 },
706 [SCARLETT2_CONFIG_AUTOGAIN_STATUS] = {
709 [SCARLETT2_CONFIG_PHANTOM_SWITCH] = {
710 .offset = 0x5a, .activate = 11, .mute = 1 },
712 [SCARLETT2_CONFIG_INPUT_GAIN] = {
713 .offset = 0x5e, .activate = 12 },
715 [SCARLETT2_CONFIG_LEVEL_SWITCH] = {
716 .offset = 0x4e, .activate = 13, .mute = 1 },
718 [SCARLETT2_CONFIG_SAFE_SWITCH] = {
719 .offset = 0x150, .activate = 14 },
721 [SCARLETT2_CONFIG_AIR_SWITCH] = {
722 .offset = 0x50, .activate = 15 },
724 [SCARLETT2_CONFIG_INPUT_SELECT_SWITCH] = {
725 .offset = 0x153, .activate = 16 },
727 [SCARLETT2_CONFIG_INPUT_LINK_SWITCH] = {
728 .offset = 0x156, .activate = 17 },
730 [SCARLETT2_CONFIG_MASTER_VOLUME] = {
731 .offset = 0x32, .size = 16 },
733 [SCARLETT2_CONFIG_HEADPHONE_VOLUME] = {
734 .offset = 0x3a, .size = 16 },
736 [SCARLETT2_CONFIG_POWER_EXT] = {
739 [SCARLETT2_CONFIG_POWER_LOW] = {
744 /* Clarett USB and Clarett+ devices: 2Pre, 4Pre, 8Pre */
745 static const struct scarlett2_config_set scarlett2_config_set_clarett = {
746 .notifications = scarlett2_notifications,
748 [SCARLETT2_CONFIG_DIM_MUTE] = {
749 .offset = 0x31, .size = 8, .activate = 2 },
751 [SCARLETT2_CONFIG_LINE_OUT_VOLUME] = {
752 .offset = 0x34, .size = 16, .activate = 1 },
754 [SCARLETT2_CONFIG_MUTE_SWITCH] = {
755 .offset = 0x5c, .size = 8, .activate = 1 },
757 [SCARLETT2_CONFIG_SW_HW_SWITCH] = {
758 .offset = 0x66, .size = 8, .activate = 3 },
760 [SCARLETT2_CONFIG_MASTER_VOLUME] = {
761 .offset = 0x76, .size = 16 },
763 [SCARLETT2_CONFIG_LEVEL_SWITCH] = {
764 .offset = 0x7c, .size = 8, .activate = 7 },
766 [SCARLETT2_CONFIG_AIR_SWITCH] = {
767 .offset = 0x95, .size = 8, .activate = 8 },
769 [SCARLETT2_CONFIG_STANDALONE_SWITCH] = {
770 .offset = 0x8d, .size = 8, .activate = 6 },
774 /* Description of each hardware port type:
775 * - id: hardware ID of this port type
776 * - src_descr: printf format string for mux input selections
777 * - src_num_offset: added to channel number for the fprintf
778 * - dst_descr: printf format string for mixer controls
780 struct scarlett2_port {
782 const char * const src_descr;
784 const char * const dst_descr;
785 const char * const dsp_src_descr;
786 const char * const dsp_dst_descr;
789 static const struct scarlett2_port scarlett2_ports[SCARLETT2_PORT_TYPE_COUNT] = {
790 [SCARLETT2_PORT_TYPE_NONE] = {
794 [SCARLETT2_PORT_TYPE_ANALOGUE] = {
796 .src_descr = "Analogue %d",
798 .dst_descr = "Analogue Output %02d Playback"
800 [SCARLETT2_PORT_TYPE_SPDIF] = {
802 .src_descr = "S/PDIF %d",
804 .dst_descr = "S/PDIF Output %d Playback"
806 [SCARLETT2_PORT_TYPE_ADAT] = {
808 .src_descr = "ADAT %d",
810 .dst_descr = "ADAT Output %d Playback"
812 [SCARLETT2_PORT_TYPE_MIX] = {
814 .src_descr = "Mix %c",
815 .src_num_offset = 'A',
816 .dst_descr = "Mixer Input %02d Capture",
817 .dsp_src_descr = "DSP %d",
818 .dsp_dst_descr = "DSP Input %d Capture"
820 [SCARLETT2_PORT_TYPE_PCM] = {
822 .src_descr = "PCM %d",
824 .dst_descr = "PCM %02d Capture"
828 /* Number of mux tables: one for each band of sample rates
829 * (44.1/48kHz, 88.2/96kHz, and 176.4/176kHz)
831 #define SCARLETT2_MUX_TABLES 3
833 /* Maximum number of entries in a mux table */
834 #define SCARLETT2_MAX_MUX_ENTRIES 10
836 /* One entry within mux_assignment defines the port type and range of
837 * ports to add to the set_mux message. The end of the list is marked
840 struct scarlett2_mux_entry {
846 /* Maximum number of entries in a mux table */
847 #define SCARLETT2_MAX_METER_ENTRIES 9
849 /* One entry within meter_assignment defines the range of mux outputs
850 * that consecutive meter entries are mapped to. The end of the list
851 * is marked with count == 0.
853 struct scarlett2_meter_entry {
858 struct scarlett2_device_info {
859 /* which set of configuration parameters the device uses */
860 const struct scarlett2_config_set *config_set;
862 /* minimum firmware version required */
863 u16 min_firmware_version;
865 /* support for main/alt speaker switching */
866 u8 has_speaker_switching;
868 /* support for talkback microphone */
871 /* the number of analogue inputs with a software switchable
872 * level control that can be set to line or instrument
874 u8 level_input_count;
876 /* the first input with a level control (0-based) */
877 u8 level_input_first;
879 /* the number of analogue inputs with a software switchable
884 /* the number of analogue inputs with a software switchable
889 /* the first input with an air control (0-based) */
892 /* number of additional air options
893 * 0 for air presence only (Gen 3)
894 * 1 for air presence+drive (Gen 4)
898 /* the number of phantom (48V) software switchable controls */
901 /* the first input with phantom power control (0-based) */
904 /* the number of inputs each phantom switch controls */
905 u8 inputs_per_phantom;
907 /* the number of inputs with software-controllable gain */
910 /* the number of direct monitor options
911 * (0 = none, 1 = mono only, 2 = mono/stereo)
915 /* the number of DSP channels */
918 /* remap analogue outputs; 18i8 Gen 3 has "line 3/4" connected
919 * internally to the analogue 7/8 outputs
921 u8 line_out_remap_enable;
922 u8 line_out_remap[SCARLETT2_ANALOGUE_MAX];
923 u8 line_out_unmap[SCARLETT2_ANALOGUE_MAX];
925 /* additional description for the line out volume controls */
926 const char * const line_out_descrs[SCARLETT2_ANALOGUE_MAX];
928 /* number of sources/destinations of each port type */
929 const int port_count[SCARLETT2_PORT_TYPE_COUNT][SCARLETT2_PORT_DIRNS];
931 /* layout/order of the entries in the set_mux message */
932 struct scarlett2_mux_entry mux_assignment[SCARLETT2_MUX_TABLES]
933 [SCARLETT2_MAX_MUX_ENTRIES];
935 /* map from meter level order returned by
936 * SCARLETT2_USB_GET_METER to index into mux[] entries (same
937 * as the order returned by scarlett2_meter_ctl_get())
939 struct scarlett2_meter_entry meter_map[SCARLETT2_MAX_METER_ENTRIES];
942 struct scarlett2_data {
943 struct usb_mixer_interface *mixer;
944 struct mutex usb_mutex; /* prevent sending concurrent USB requests */
945 struct mutex data_mutex; /* lock access to this data */
947 u8 selected_flash_segment_id;
948 u8 flash_write_state;
949 struct delayed_work work;
950 const struct scarlett2_device_info *info;
951 const struct scarlett2_config_set *config_set;
952 const char *series_name;
953 __u8 bInterfaceNumber;
954 __u8 bEndpointAddress;
955 __u16 wMaxPacketSize;
962 u8 num_monitor_mix_ctls;
963 u32 firmware_version;
964 u8 flash_segment_nums[SCARLETT2_SEGMENT_ID_COUNT];
965 u8 flash_segment_blocks[SCARLETT2_SEGMENT_ID_COUNT];
970 u8 input_level_updated;
971 u8 input_pad_updated;
972 u8 input_air_updated;
973 u8 input_phantom_updated;
974 u8 input_select_updated;
975 u8 input_gain_updated;
977 u8 input_safe_updated;
978 u8 pcm_input_switch_updated;
979 u8 monitor_other_updated;
980 u8 direct_monitor_updated;
983 u8 speaker_switching_switched;
984 u8 power_status_updated;
988 u8 vol[SCARLETT2_ANALOGUE_MAX];
989 u8 vol_sw_hw_switch[SCARLETT2_ANALOGUE_MAX];
990 u8 mute_switch[SCARLETT2_ANALOGUE_MAX];
991 u8 level_switch[SCARLETT2_LEVEL_SWITCH_MAX];
992 u8 pad_switch[SCARLETT2_PAD_SWITCH_MAX];
993 u8 dim_mute[SCARLETT2_DIM_MUTE_COUNT];
994 u8 air_switch[SCARLETT2_AIR_SWITCH_MAX];
995 u8 phantom_switch[SCARLETT2_PHANTOM_SWITCH_MAX];
996 u8 phantom_persistence;
997 u8 input_select_switch;
998 u8 input_link_switch[SCARLETT2_INPUT_GAIN_MAX / 2];
999 u8 gain[SCARLETT2_INPUT_GAIN_MAX];
1000 u8 autogain_switch[SCARLETT2_INPUT_GAIN_MAX];
1001 u8 autogain_status[SCARLETT2_INPUT_GAIN_MAX];
1002 u8 safe_switch[SCARLETT2_INPUT_GAIN_MAX];
1003 u8 pcm_input_switch;
1004 u8 direct_monitor_switch;
1005 u8 speaker_switching_switch;
1007 u8 talkback_map[SCARLETT2_OUTPUT_MIX_MAX];
1009 u8 standalone_switch;
1011 u8 meter_level_map[SCARLETT2_MAX_METERS];
1012 struct snd_kcontrol *sync_ctl;
1013 struct snd_kcontrol *master_vol_ctl;
1014 struct snd_kcontrol *headphone_vol_ctl;
1015 struct snd_kcontrol *vol_ctls[SCARLETT2_ANALOGUE_MAX];
1016 struct snd_kcontrol *sw_hw_ctls[SCARLETT2_ANALOGUE_MAX];
1017 struct snd_kcontrol *mute_ctls[SCARLETT2_ANALOGUE_MAX];
1018 struct snd_kcontrol *dim_mute_ctls[SCARLETT2_DIM_MUTE_COUNT];
1019 struct snd_kcontrol *level_ctls[SCARLETT2_LEVEL_SWITCH_MAX];
1020 struct snd_kcontrol *pad_ctls[SCARLETT2_PAD_SWITCH_MAX];
1021 struct snd_kcontrol *air_ctls[SCARLETT2_AIR_SWITCH_MAX];
1022 struct snd_kcontrol *phantom_ctls[SCARLETT2_PHANTOM_SWITCH_MAX];
1023 struct snd_kcontrol *input_select_ctl;
1024 struct snd_kcontrol *input_link_ctls[SCARLETT2_INPUT_GAIN_MAX / 2];
1025 struct snd_kcontrol *input_gain_ctls[SCARLETT2_INPUT_GAIN_MAX];
1026 struct snd_kcontrol *autogain_ctls[SCARLETT2_INPUT_GAIN_MAX];
1027 struct snd_kcontrol *autogain_status_ctls[SCARLETT2_INPUT_GAIN_MAX];
1028 struct snd_kcontrol *safe_ctls[SCARLETT2_INPUT_GAIN_MAX];
1029 struct snd_kcontrol *pcm_input_switch_ctl;
1030 struct snd_kcontrol *mux_ctls[SCARLETT2_MUX_MAX];
1031 struct snd_kcontrol *mix_ctls[SCARLETT2_MIX_MAX];
1032 struct snd_kcontrol *direct_monitor_ctl;
1033 struct snd_kcontrol *speaker_switching_ctl;
1034 struct snd_kcontrol *talkback_ctl;
1035 struct snd_kcontrol *power_status_ctl;
1036 u8 mux[SCARLETT2_MUX_MAX];
1037 u8 mix[SCARLETT2_MIX_MAX];
1038 u8 monitor_mix[SCARLETT2_MONITOR_MIX_MAX];
1041 /*** Model-specific data ***/
1043 static const struct scarlett2_device_info s6i6_gen2_info = {
1044 .config_set = &scarlett2_config_set_gen2a,
1045 .level_input_count = 2,
1046 .pad_input_count = 2,
1048 .line_out_descrs = {
1056 [SCARLETT2_PORT_TYPE_NONE] = { 1, 0 },
1057 [SCARLETT2_PORT_TYPE_ANALOGUE] = { 4, 4 },
1058 [SCARLETT2_PORT_TYPE_SPDIF] = { 2, 2 },
1059 [SCARLETT2_PORT_TYPE_MIX] = { 10, 18 },
1060 [SCARLETT2_PORT_TYPE_PCM] = { 6, 6 },
1063 .mux_assignment = { {
1064 { SCARLETT2_PORT_TYPE_PCM, 0, 6 },
1065 { SCARLETT2_PORT_TYPE_ANALOGUE, 0, 4 },
1066 { SCARLETT2_PORT_TYPE_SPDIF, 0, 2 },
1067 { SCARLETT2_PORT_TYPE_MIX, 0, 18 },
1068 { SCARLETT2_PORT_TYPE_NONE, 0, 8 },
1071 { SCARLETT2_PORT_TYPE_PCM, 0, 6 },
1072 { SCARLETT2_PORT_TYPE_ANALOGUE, 0, 4 },
1073 { SCARLETT2_PORT_TYPE_SPDIF, 0, 2 },
1074 { SCARLETT2_PORT_TYPE_MIX, 0, 18 },
1075 { SCARLETT2_PORT_TYPE_NONE, 0, 8 },
1078 { SCARLETT2_PORT_TYPE_PCM, 0, 6 },
1079 { SCARLETT2_PORT_TYPE_ANALOGUE, 0, 4 },
1080 { SCARLETT2_PORT_TYPE_SPDIF, 0, 2 },
1081 { SCARLETT2_PORT_TYPE_MIX, 0, 18 },
1082 { SCARLETT2_PORT_TYPE_NONE, 0, 8 },
1093 static const struct scarlett2_device_info s18i8_gen2_info = {
1094 .config_set = &scarlett2_config_set_gen2a,
1095 .level_input_count = 2,
1096 .pad_input_count = 4,
1098 .line_out_descrs = {
1108 [SCARLETT2_PORT_TYPE_NONE] = { 1, 0 },
1109 [SCARLETT2_PORT_TYPE_ANALOGUE] = { 8, 6 },
1110 [SCARLETT2_PORT_TYPE_SPDIF] = { 2, 2 },
1111 [SCARLETT2_PORT_TYPE_ADAT] = { 8, 0 },
1112 [SCARLETT2_PORT_TYPE_MIX] = { 10, 18 },
1113 [SCARLETT2_PORT_TYPE_PCM] = { 8, 18 },
1116 .mux_assignment = { {
1117 { SCARLETT2_PORT_TYPE_PCM, 0, 18 },
1118 { SCARLETT2_PORT_TYPE_ANALOGUE, 0, 6 },
1119 { SCARLETT2_PORT_TYPE_SPDIF, 0, 2 },
1120 { SCARLETT2_PORT_TYPE_MIX, 0, 18 },
1121 { SCARLETT2_PORT_TYPE_NONE, 0, 8 },
1124 { SCARLETT2_PORT_TYPE_PCM, 0, 14 },
1125 { SCARLETT2_PORT_TYPE_ANALOGUE, 0, 6 },
1126 { SCARLETT2_PORT_TYPE_SPDIF, 0, 2 },
1127 { SCARLETT2_PORT_TYPE_MIX, 0, 18 },
1128 { SCARLETT2_PORT_TYPE_NONE, 0, 8 },
1131 { SCARLETT2_PORT_TYPE_PCM, 0, 10 },
1132 { SCARLETT2_PORT_TYPE_ANALOGUE, 0, 6 },
1133 { SCARLETT2_PORT_TYPE_SPDIF, 0, 2 },
1134 { SCARLETT2_PORT_TYPE_MIX, 0, 18 },
1135 { SCARLETT2_PORT_TYPE_NONE, 0, 4 },
1146 static const struct scarlett2_device_info s18i20_gen2_info = {
1147 .config_set = &scarlett2_config_set_gen2b,
1149 .line_out_descrs = {
1163 [SCARLETT2_PORT_TYPE_NONE] = { 1, 0 },
1164 [SCARLETT2_PORT_TYPE_ANALOGUE] = { 8, 10 },
1165 [SCARLETT2_PORT_TYPE_SPDIF] = { 2, 2 },
1166 [SCARLETT2_PORT_TYPE_ADAT] = { 8, 8 },
1167 [SCARLETT2_PORT_TYPE_MIX] = { 10, 18 },
1168 [SCARLETT2_PORT_TYPE_PCM] = { 20, 18 },
1171 .mux_assignment = { {
1172 { SCARLETT2_PORT_TYPE_PCM, 0, 18 },
1173 { SCARLETT2_PORT_TYPE_ANALOGUE, 0, 10 },
1174 { SCARLETT2_PORT_TYPE_SPDIF, 0, 2 },
1175 { SCARLETT2_PORT_TYPE_ADAT, 0, 8 },
1176 { SCARLETT2_PORT_TYPE_MIX, 0, 18 },
1177 { SCARLETT2_PORT_TYPE_NONE, 0, 8 },
1180 { SCARLETT2_PORT_TYPE_PCM, 0, 14 },
1181 { SCARLETT2_PORT_TYPE_ANALOGUE, 0, 10 },
1182 { SCARLETT2_PORT_TYPE_SPDIF, 0, 2 },
1183 { SCARLETT2_PORT_TYPE_ADAT, 0, 4 },
1184 { SCARLETT2_PORT_TYPE_MIX, 0, 18 },
1185 { SCARLETT2_PORT_TYPE_NONE, 0, 8 },
1188 { SCARLETT2_PORT_TYPE_PCM, 0, 10 },
1189 { SCARLETT2_PORT_TYPE_ANALOGUE, 0, 10 },
1190 { SCARLETT2_PORT_TYPE_SPDIF, 0, 2 },
1191 { SCARLETT2_PORT_TYPE_MIX, 0, 18 },
1192 { SCARLETT2_PORT_TYPE_NONE, 0, 6 },
1203 static const struct scarlett2_device_info solo_gen3_info = {
1204 .config_set = &scarlett2_config_set_gen3a,
1205 .level_input_count = 1,
1206 .level_input_first = 1,
1207 .air_input_count = 1,
1209 .inputs_per_phantom = 1,
1210 .direct_monitor = 1,
1213 static const struct scarlett2_device_info s2i2_gen3_info = {
1214 .config_set = &scarlett2_config_set_gen3a,
1215 .level_input_count = 2,
1216 .air_input_count = 2,
1218 .inputs_per_phantom = 2,
1219 .direct_monitor = 2,
1222 static const struct scarlett2_device_info s4i4_gen3_info = {
1223 .config_set = &scarlett2_config_set_gen3b,
1224 .level_input_count = 2,
1225 .pad_input_count = 2,
1226 .air_input_count = 2,
1228 .inputs_per_phantom = 2,
1230 .line_out_descrs = {
1238 [SCARLETT2_PORT_TYPE_NONE] = { 1, 0 },
1239 [SCARLETT2_PORT_TYPE_ANALOGUE] = { 4, 4 },
1240 [SCARLETT2_PORT_TYPE_MIX] = { 6, 8 },
1241 [SCARLETT2_PORT_TYPE_PCM] = { 4, 6 },
1244 .mux_assignment = { {
1245 { SCARLETT2_PORT_TYPE_PCM, 0, 6 },
1246 { SCARLETT2_PORT_TYPE_ANALOGUE, 0, 4 },
1247 { SCARLETT2_PORT_TYPE_MIX, 0, 8 },
1248 { SCARLETT2_PORT_TYPE_NONE, 0, 16 },
1251 { SCARLETT2_PORT_TYPE_PCM, 0, 6 },
1252 { SCARLETT2_PORT_TYPE_ANALOGUE, 0, 4 },
1253 { SCARLETT2_PORT_TYPE_MIX, 0, 8 },
1254 { SCARLETT2_PORT_TYPE_NONE, 0, 16 },
1257 { SCARLETT2_PORT_TYPE_PCM, 0, 6 },
1258 { SCARLETT2_PORT_TYPE_ANALOGUE, 0, 4 },
1259 { SCARLETT2_PORT_TYPE_MIX, 0, 8 },
1260 { SCARLETT2_PORT_TYPE_NONE, 0, 16 },
1271 static const struct scarlett2_device_info s8i6_gen3_info = {
1272 .config_set = &scarlett2_config_set_gen3b,
1273 .level_input_count = 2,
1274 .pad_input_count = 2,
1275 .air_input_count = 2,
1277 .inputs_per_phantom = 2,
1279 .line_out_descrs = {
1287 [SCARLETT2_PORT_TYPE_NONE] = { 1, 0 },
1288 [SCARLETT2_PORT_TYPE_ANALOGUE] = { 6, 4 },
1289 [SCARLETT2_PORT_TYPE_SPDIF] = { 2, 2 },
1290 [SCARLETT2_PORT_TYPE_MIX] = { 8, 8 },
1291 [SCARLETT2_PORT_TYPE_PCM] = { 6, 10 },
1294 .mux_assignment = { {
1295 { SCARLETT2_PORT_TYPE_PCM, 0, 8 },
1296 { SCARLETT2_PORT_TYPE_ANALOGUE, 0, 4 },
1297 { SCARLETT2_PORT_TYPE_SPDIF, 0, 2 },
1298 { SCARLETT2_PORT_TYPE_PCM, 8, 2 },
1299 { SCARLETT2_PORT_TYPE_MIX, 0, 8 },
1300 { SCARLETT2_PORT_TYPE_NONE, 0, 18 },
1303 { SCARLETT2_PORT_TYPE_PCM, 0, 8 },
1304 { SCARLETT2_PORT_TYPE_ANALOGUE, 0, 4 },
1305 { SCARLETT2_PORT_TYPE_SPDIF, 0, 2 },
1306 { SCARLETT2_PORT_TYPE_PCM, 8, 2 },
1307 { SCARLETT2_PORT_TYPE_MIX, 0, 8 },
1308 { SCARLETT2_PORT_TYPE_NONE, 0, 18 },
1311 { SCARLETT2_PORT_TYPE_PCM, 0, 8 },
1312 { SCARLETT2_PORT_TYPE_ANALOGUE, 0, 4 },
1313 { SCARLETT2_PORT_TYPE_SPDIF, 0, 2 },
1314 { SCARLETT2_PORT_TYPE_PCM, 8, 2 },
1315 { SCARLETT2_PORT_TYPE_MIX, 0, 8 },
1316 { SCARLETT2_PORT_TYPE_NONE, 0, 18 },
1329 static const struct scarlett2_device_info s18i8_gen3_info = {
1330 .config_set = &scarlett2_config_set_gen3c,
1331 .has_speaker_switching = 1,
1332 .level_input_count = 2,
1333 .pad_input_count = 4,
1334 .air_input_count = 4,
1336 .inputs_per_phantom = 2,
1338 .line_out_remap_enable = 1,
1339 .line_out_remap = { 0, 1, 6, 7, 2, 3, 4, 5 },
1340 .line_out_unmap = { 0, 1, 4, 5, 6, 7, 2, 3 },
1342 .line_out_descrs = {
1354 [SCARLETT2_PORT_TYPE_NONE] = { 1, 0 },
1355 [SCARLETT2_PORT_TYPE_ANALOGUE] = { 8, 8 },
1356 [SCARLETT2_PORT_TYPE_SPDIF] = { 2, 2 },
1357 [SCARLETT2_PORT_TYPE_ADAT] = { 8, 0 },
1358 [SCARLETT2_PORT_TYPE_MIX] = { 10, 20 },
1359 [SCARLETT2_PORT_TYPE_PCM] = { 8, 20 },
1362 .mux_assignment = { {
1363 { SCARLETT2_PORT_TYPE_PCM, 0, 10 },
1364 { SCARLETT2_PORT_TYPE_PCM, 12, 8 },
1365 { SCARLETT2_PORT_TYPE_ANALOGUE, 0, 2 },
1366 { SCARLETT2_PORT_TYPE_ANALOGUE, 6, 2 },
1367 { SCARLETT2_PORT_TYPE_ANALOGUE, 2, 4 },
1368 { SCARLETT2_PORT_TYPE_SPDIF, 0, 2 },
1369 { SCARLETT2_PORT_TYPE_PCM, 10, 2 },
1370 { SCARLETT2_PORT_TYPE_MIX, 0, 20 },
1371 { SCARLETT2_PORT_TYPE_NONE, 0, 10 },
1374 { SCARLETT2_PORT_TYPE_PCM, 0, 10 },
1375 { SCARLETT2_PORT_TYPE_PCM, 12, 4 },
1376 { SCARLETT2_PORT_TYPE_ANALOGUE, 0, 2 },
1377 { SCARLETT2_PORT_TYPE_ANALOGUE, 6, 2 },
1378 { SCARLETT2_PORT_TYPE_ANALOGUE, 2, 4 },
1379 { SCARLETT2_PORT_TYPE_SPDIF, 0, 2 },
1380 { SCARLETT2_PORT_TYPE_PCM, 10, 2 },
1381 { SCARLETT2_PORT_TYPE_MIX, 0, 20 },
1382 { SCARLETT2_PORT_TYPE_NONE, 0, 10 },
1385 { SCARLETT2_PORT_TYPE_PCM, 0, 10 },
1386 { SCARLETT2_PORT_TYPE_ANALOGUE, 0, 2 },
1387 { SCARLETT2_PORT_TYPE_ANALOGUE, 6, 2 },
1388 { SCARLETT2_PORT_TYPE_ANALOGUE, 2, 4 },
1389 { SCARLETT2_PORT_TYPE_SPDIF, 0, 2 },
1390 { SCARLETT2_PORT_TYPE_MIX, 0, 20 },
1391 { SCARLETT2_PORT_TYPE_NONE, 0, 10 },
1408 static const struct scarlett2_device_info s18i20_gen3_info = {
1409 .config_set = &scarlett2_config_set_gen3c,
1410 .has_speaker_switching = 1,
1412 .level_input_count = 2,
1413 .pad_input_count = 8,
1414 .air_input_count = 8,
1416 .inputs_per_phantom = 4,
1418 .line_out_descrs = {
1432 [SCARLETT2_PORT_TYPE_NONE] = { 1, 0 },
1433 [SCARLETT2_PORT_TYPE_ANALOGUE] = { 9, 10 },
1434 [SCARLETT2_PORT_TYPE_SPDIF] = { 2, 2 },
1435 [SCARLETT2_PORT_TYPE_ADAT] = { 8, 8 },
1436 [SCARLETT2_PORT_TYPE_MIX] = { 12, 25 },
1437 [SCARLETT2_PORT_TYPE_PCM] = { 20, 20 },
1440 .mux_assignment = { {
1441 { SCARLETT2_PORT_TYPE_PCM, 0, 8 },
1442 { SCARLETT2_PORT_TYPE_PCM, 10, 10 },
1443 { SCARLETT2_PORT_TYPE_ANALOGUE, 0, 10 },
1444 { SCARLETT2_PORT_TYPE_SPDIF, 0, 2 },
1445 { SCARLETT2_PORT_TYPE_ADAT, 0, 8 },
1446 { SCARLETT2_PORT_TYPE_PCM, 8, 2 },
1447 { SCARLETT2_PORT_TYPE_MIX, 0, 25 },
1448 { SCARLETT2_PORT_TYPE_NONE, 0, 12 },
1451 { SCARLETT2_PORT_TYPE_PCM, 0, 8 },
1452 { SCARLETT2_PORT_TYPE_PCM, 10, 8 },
1453 { SCARLETT2_PORT_TYPE_ANALOGUE, 0, 10 },
1454 { SCARLETT2_PORT_TYPE_SPDIF, 0, 2 },
1455 { SCARLETT2_PORT_TYPE_ADAT, 0, 8 },
1456 { SCARLETT2_PORT_TYPE_PCM, 8, 2 },
1457 { SCARLETT2_PORT_TYPE_MIX, 0, 25 },
1458 { SCARLETT2_PORT_TYPE_NONE, 0, 10 },
1461 { SCARLETT2_PORT_TYPE_PCM, 0, 10 },
1462 { SCARLETT2_PORT_TYPE_ANALOGUE, 0, 10 },
1463 { SCARLETT2_PORT_TYPE_SPDIF, 0, 2 },
1464 { SCARLETT2_PORT_TYPE_NONE, 0, 24 },
1478 static const struct scarlett2_device_info solo_gen4_info = {
1479 .config_set = &scarlett2_config_set_gen4_solo,
1480 .min_firmware_version = 2115,
1482 .level_input_count = 1,
1483 .air_input_count = 1,
1484 .air_input_first = 1,
1488 .inputs_per_phantom = 1,
1489 .direct_monitor = 1,
1493 [SCARLETT2_PORT_TYPE_NONE] = { 1, 0 },
1494 [SCARLETT2_PORT_TYPE_ANALOGUE] = { 2, 2 },
1495 [SCARLETT2_PORT_TYPE_MIX] = { 8, 6 },
1496 [SCARLETT2_PORT_TYPE_PCM] = { 2, 4 },
1499 .mux_assignment = { {
1500 { SCARLETT2_PORT_TYPE_MIX, 4, 2 },
1501 { SCARLETT2_PORT_TYPE_MIX, 2, 2 },
1502 { SCARLETT2_PORT_TYPE_PCM, 0, 4 },
1503 { SCARLETT2_PORT_TYPE_MIX, 0, 2 },
1504 { SCARLETT2_PORT_TYPE_ANALOGUE, 0, 2 },
1507 { SCARLETT2_PORT_TYPE_MIX, 4, 2 },
1508 { SCARLETT2_PORT_TYPE_MIX, 2, 2 },
1509 { SCARLETT2_PORT_TYPE_PCM, 0, 4 },
1510 { SCARLETT2_PORT_TYPE_MIX, 0, 2 },
1511 { SCARLETT2_PORT_TYPE_ANALOGUE, 0, 2 },
1514 { SCARLETT2_PORT_TYPE_MIX, 4, 2 },
1515 { SCARLETT2_PORT_TYPE_MIX, 2, 2 },
1516 { SCARLETT2_PORT_TYPE_PCM, 0, 4 },
1517 { SCARLETT2_PORT_TYPE_MIX, 0, 2 },
1518 { SCARLETT2_PORT_TYPE_ANALOGUE, 0, 2 },
1532 static const struct scarlett2_device_info s2i2_gen4_info = {
1533 .config_set = &scarlett2_config_set_gen4_2i2,
1534 .min_firmware_version = 2115,
1536 .level_input_count = 2,
1537 .air_input_count = 2,
1540 .inputs_per_phantom = 2,
1541 .gain_input_count = 2,
1542 .direct_monitor = 2,
1546 [SCARLETT2_PORT_TYPE_NONE] = { 1, 0 },
1547 [SCARLETT2_PORT_TYPE_ANALOGUE] = { 2, 2 },
1548 [SCARLETT2_PORT_TYPE_MIX] = { 6, 6 },
1549 [SCARLETT2_PORT_TYPE_PCM] = { 2, 4 },
1552 .mux_assignment = { {
1553 { SCARLETT2_PORT_TYPE_MIX, 4, 2 },
1554 { SCARLETT2_PORT_TYPE_MIX, 2, 2 },
1555 { SCARLETT2_PORT_TYPE_PCM, 0, 4 },
1556 { SCARLETT2_PORT_TYPE_MIX, 0, 2 },
1557 { SCARLETT2_PORT_TYPE_ANALOGUE, 0, 2 },
1560 { SCARLETT2_PORT_TYPE_MIX, 4, 2 },
1561 { SCARLETT2_PORT_TYPE_MIX, 2, 2 },
1562 { SCARLETT2_PORT_TYPE_PCM, 0, 4 },
1563 { SCARLETT2_PORT_TYPE_MIX, 0, 2 },
1564 { SCARLETT2_PORT_TYPE_ANALOGUE, 0, 2 },
1567 { SCARLETT2_PORT_TYPE_MIX, 4, 2 },
1568 { SCARLETT2_PORT_TYPE_MIX, 2, 2 },
1569 { SCARLETT2_PORT_TYPE_PCM, 0, 4 },
1570 { SCARLETT2_PORT_TYPE_MIX, 0, 2 },
1571 { SCARLETT2_PORT_TYPE_ANALOGUE, 0, 2 },
1585 static const struct scarlett2_device_info s4i4_gen4_info = {
1586 .config_set = &scarlett2_config_set_gen4_4i4,
1587 .min_firmware_version = 2089,
1589 .level_input_count = 2,
1590 .air_input_count = 2,
1593 .inputs_per_phantom = 1,
1594 .gain_input_count = 2,
1598 [SCARLETT2_PORT_TYPE_NONE] = { 1, 0 },
1599 [SCARLETT2_PORT_TYPE_ANALOGUE] = { 4, 6 },
1600 [SCARLETT2_PORT_TYPE_MIX] = { 8, 12 },
1601 [SCARLETT2_PORT_TYPE_PCM] = { 6, 6 },
1604 .mux_assignment = { {
1605 { SCARLETT2_PORT_TYPE_MIX, 10, 2 },
1606 { SCARLETT2_PORT_TYPE_PCM, 0, 6 },
1607 { SCARLETT2_PORT_TYPE_MIX, 0, 10 },
1608 { SCARLETT2_PORT_TYPE_ANALOGUE, 0, 6 },
1611 { SCARLETT2_PORT_TYPE_MIX, 10, 2 },
1612 { SCARLETT2_PORT_TYPE_PCM, 0, 6 },
1613 { SCARLETT2_PORT_TYPE_MIX, 0, 10 },
1614 { SCARLETT2_PORT_TYPE_ANALOGUE, 0, 6 },
1617 { SCARLETT2_PORT_TYPE_MIX, 10, 2 },
1618 { SCARLETT2_PORT_TYPE_PCM, 0, 6 },
1619 { SCARLETT2_PORT_TYPE_MIX, 0, 10 },
1620 { SCARLETT2_PORT_TYPE_ANALOGUE, 0, 6 },
1632 static const struct scarlett2_device_info clarett_2pre_info = {
1633 .config_set = &scarlett2_config_set_clarett,
1634 .level_input_count = 2,
1635 .air_input_count = 2,
1637 .line_out_descrs = {
1645 [SCARLETT2_PORT_TYPE_NONE] = { 1, 0 },
1646 [SCARLETT2_PORT_TYPE_ANALOGUE] = { 2, 4 },
1647 [SCARLETT2_PORT_TYPE_SPDIF] = { 2, 0 },
1648 [SCARLETT2_PORT_TYPE_ADAT] = { 8, 0 },
1649 [SCARLETT2_PORT_TYPE_MIX] = { 10, 18 },
1650 [SCARLETT2_PORT_TYPE_PCM] = { 4, 12 },
1653 .mux_assignment = { {
1654 { SCARLETT2_PORT_TYPE_PCM, 0, 12 },
1655 { SCARLETT2_PORT_TYPE_ANALOGUE, 0, 4 },
1656 { SCARLETT2_PORT_TYPE_MIX, 0, 18 },
1657 { SCARLETT2_PORT_TYPE_NONE, 0, 8 },
1660 { SCARLETT2_PORT_TYPE_PCM, 0, 8 },
1661 { SCARLETT2_PORT_TYPE_ANALOGUE, 0, 4 },
1662 { SCARLETT2_PORT_TYPE_MIX, 0, 18 },
1663 { SCARLETT2_PORT_TYPE_NONE, 0, 8 },
1666 { SCARLETT2_PORT_TYPE_PCM, 0, 2 },
1667 { SCARLETT2_PORT_TYPE_ANALOGUE, 0, 4 },
1668 { SCARLETT2_PORT_TYPE_NONE, 0, 26 },
1679 static const struct scarlett2_device_info clarett_4pre_info = {
1680 .config_set = &scarlett2_config_set_clarett,
1681 .level_input_count = 2,
1682 .air_input_count = 4,
1684 .line_out_descrs = {
1694 [SCARLETT2_PORT_TYPE_NONE] = { 1, 0 },
1695 [SCARLETT2_PORT_TYPE_ANALOGUE] = { 8, 6 },
1696 [SCARLETT2_PORT_TYPE_SPDIF] = { 2, 2 },
1697 [SCARLETT2_PORT_TYPE_ADAT] = { 8, 0 },
1698 [SCARLETT2_PORT_TYPE_MIX] = { 10, 18 },
1699 [SCARLETT2_PORT_TYPE_PCM] = { 8, 18 },
1702 .mux_assignment = { {
1703 { SCARLETT2_PORT_TYPE_PCM, 0, 18 },
1704 { SCARLETT2_PORT_TYPE_ANALOGUE, 0, 6 },
1705 { SCARLETT2_PORT_TYPE_SPDIF, 0, 2 },
1706 { SCARLETT2_PORT_TYPE_MIX, 0, 18 },
1707 { SCARLETT2_PORT_TYPE_NONE, 0, 8 },
1710 { SCARLETT2_PORT_TYPE_PCM, 0, 14 },
1711 { SCARLETT2_PORT_TYPE_ANALOGUE, 0, 6 },
1712 { SCARLETT2_PORT_TYPE_SPDIF, 0, 2 },
1713 { SCARLETT2_PORT_TYPE_MIX, 0, 18 },
1714 { SCARLETT2_PORT_TYPE_NONE, 0, 8 },
1717 { SCARLETT2_PORT_TYPE_PCM, 0, 12 },
1718 { SCARLETT2_PORT_TYPE_ANALOGUE, 0, 6 },
1719 { SCARLETT2_PORT_TYPE_SPDIF, 0, 2 },
1720 { SCARLETT2_PORT_TYPE_NONE, 0, 24 },
1731 static const struct scarlett2_device_info clarett_8pre_info = {
1732 .config_set = &scarlett2_config_set_clarett,
1733 .level_input_count = 2,
1734 .air_input_count = 8,
1736 .line_out_descrs = {
1750 [SCARLETT2_PORT_TYPE_NONE] = { 1, 0 },
1751 [SCARLETT2_PORT_TYPE_ANALOGUE] = { 8, 10 },
1752 [SCARLETT2_PORT_TYPE_SPDIF] = { 2, 2 },
1753 [SCARLETT2_PORT_TYPE_ADAT] = { 8, 8 },
1754 [SCARLETT2_PORT_TYPE_MIX] = { 10, 18 },
1755 [SCARLETT2_PORT_TYPE_PCM] = { 20, 18 },
1758 .mux_assignment = { {
1759 { SCARLETT2_PORT_TYPE_PCM, 0, 18 },
1760 { SCARLETT2_PORT_TYPE_ANALOGUE, 0, 10 },
1761 { SCARLETT2_PORT_TYPE_SPDIF, 0, 2 },
1762 { SCARLETT2_PORT_TYPE_ADAT, 0, 8 },
1763 { SCARLETT2_PORT_TYPE_MIX, 0, 18 },
1764 { SCARLETT2_PORT_TYPE_NONE, 0, 8 },
1767 { SCARLETT2_PORT_TYPE_PCM, 0, 14 },
1768 { SCARLETT2_PORT_TYPE_ANALOGUE, 0, 10 },
1769 { SCARLETT2_PORT_TYPE_SPDIF, 0, 2 },
1770 { SCARLETT2_PORT_TYPE_ADAT, 0, 4 },
1771 { SCARLETT2_PORT_TYPE_MIX, 0, 18 },
1772 { SCARLETT2_PORT_TYPE_NONE, 0, 8 },
1775 { SCARLETT2_PORT_TYPE_PCM, 0, 12 },
1776 { SCARLETT2_PORT_TYPE_ANALOGUE, 0, 10 },
1777 { SCARLETT2_PORT_TYPE_SPDIF, 0, 2 },
1778 { SCARLETT2_PORT_TYPE_NONE, 0, 22 },
1789 struct scarlett2_device_entry {
1790 const u32 usb_id; /* USB device identifier */
1791 const struct scarlett2_device_info *info;
1792 const char *series_name;
1795 static const struct scarlett2_device_entry scarlett2_devices[] = {
1796 /* Supported Gen 2 devices */
1797 { USB_ID(0x1235, 0x8203), &s6i6_gen2_info, "Scarlett Gen 2" },
1798 { USB_ID(0x1235, 0x8204), &s18i8_gen2_info, "Scarlett Gen 2" },
1799 { USB_ID(0x1235, 0x8201), &s18i20_gen2_info, "Scarlett Gen 2" },
1801 /* Supported Gen 3 devices */
1802 { USB_ID(0x1235, 0x8211), &solo_gen3_info, "Scarlett Gen 3" },
1803 { USB_ID(0x1235, 0x8210), &s2i2_gen3_info, "Scarlett Gen 3" },
1804 { USB_ID(0x1235, 0x8212), &s4i4_gen3_info, "Scarlett Gen 3" },
1805 { USB_ID(0x1235, 0x8213), &s8i6_gen3_info, "Scarlett Gen 3" },
1806 { USB_ID(0x1235, 0x8214), &s18i8_gen3_info, "Scarlett Gen 3" },
1807 { USB_ID(0x1235, 0x8215), &s18i20_gen3_info, "Scarlett Gen 3" },
1809 /* Supported Gen 4 devices */
1810 { USB_ID(0x1235, 0x8218), &solo_gen4_info, "Scarlett Gen 4" },
1811 { USB_ID(0x1235, 0x8219), &s2i2_gen4_info, "Scarlett Gen 4" },
1812 { USB_ID(0x1235, 0x821a), &s4i4_gen4_info, "Scarlett Gen 4" },
1814 /* Supported Clarett USB/Clarett+ devices */
1815 { USB_ID(0x1235, 0x8206), &clarett_2pre_info, "Clarett USB" },
1816 { USB_ID(0x1235, 0x8207), &clarett_4pre_info, "Clarett USB" },
1817 { USB_ID(0x1235, 0x8208), &clarett_8pre_info, "Clarett USB" },
1818 { USB_ID(0x1235, 0x820a), &clarett_2pre_info, "Clarett+" },
1819 { USB_ID(0x1235, 0x820b), &clarett_4pre_info, "Clarett+" },
1820 { USB_ID(0x1235, 0x820c), &clarett_8pre_info, "Clarett+" },
1826 /* get the starting port index number for a given port type/direction */
1827 static int scarlett2_get_port_start_num(
1828 const int port_count[][SCARLETT2_PORT_DIRNS],
1829 int direction, int port_type)
1833 for (i = 0; i < port_type; i++)
1834 num += port_count[i][direction];
1839 /*** USB Interactions ***/
1841 /* Commands for sending/receiving requests/responses */
1842 #define SCARLETT2_USB_CMD_INIT 0
1843 #define SCARLETT2_USB_CMD_REQ 2
1844 #define SCARLETT2_USB_CMD_RESP 3
1846 #define SCARLETT2_USB_INIT_1 0x00000000
1847 #define SCARLETT2_USB_INIT_2 0x00000002
1848 #define SCARLETT2_USB_REBOOT 0x00000003
1849 #define SCARLETT2_USB_GET_METER 0x00001001
1850 #define SCARLETT2_USB_GET_MIX 0x00002001
1851 #define SCARLETT2_USB_SET_MIX 0x00002002
1852 #define SCARLETT2_USB_GET_MUX 0x00003001
1853 #define SCARLETT2_USB_SET_MUX 0x00003002
1854 #define SCARLETT2_USB_INFO_FLASH 0x00004000
1855 #define SCARLETT2_USB_INFO_SEGMENT 0x00004001
1856 #define SCARLETT2_USB_ERASE_SEGMENT 0x00004002
1857 #define SCARLETT2_USB_GET_ERASE 0x00004003
1858 #define SCARLETT2_USB_WRITE_SEGMENT 0x00004004
1859 #define SCARLETT2_USB_GET_SYNC 0x00006004
1860 #define SCARLETT2_USB_GET_DATA 0x00800000
1861 #define SCARLETT2_USB_SET_DATA 0x00800001
1862 #define SCARLETT2_USB_DATA_CMD 0x00800002
1864 #define SCARLETT2_USB_CONFIG_SAVE 6
1866 #define SCARLETT2_USB_METER_LEVELS_GET_MAGIC 1
1868 #define SCARLETT2_FLASH_BLOCK_SIZE 4096
1869 #define SCARLETT2_FLASH_WRITE_MAX 1024
1870 #define SCARLETT2_SEGMENT_NUM_MIN 1
1871 #define SCARLETT2_SEGMENT_NUM_MAX 4
1873 #define SCARLETT2_SEGMENT_SETTINGS_NAME "App_Settings"
1874 #define SCARLETT2_SEGMENT_FIRMWARE_NAME "App_Upgrade"
1876 /* proprietary request/response format */
1877 struct scarlett2_usb_packet {
1886 static void scarlett2_fill_request_header(struct scarlett2_data *private,
1887 struct scarlett2_usb_packet *req,
1888 u32 cmd, u16 req_size)
1890 /* sequence must go up by 1 for each request */
1891 u16 seq = private->scarlett2_seq++;
1893 req->cmd = cpu_to_le32(cmd);
1894 req->size = cpu_to_le16(req_size);
1895 req->seq = cpu_to_le16(seq);
1900 static int scarlett2_usb_tx(struct usb_device *dev, int interface,
1901 void *buf, u16 size)
1903 return snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0),
1904 SCARLETT2_USB_CMD_REQ,
1905 USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_OUT,
1906 0, interface, buf, size);
1909 static int scarlett2_usb_rx(struct usb_device *dev, int interface,
1910 u32 usb_req, void *buf, u16 size)
1912 return snd_usb_ctl_msg(dev, usb_rcvctrlpipe(dev, 0),
1914 USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_IN,
1915 0, interface, buf, size);
1918 /* Send a proprietary format request to the Scarlett interface */
1919 static int scarlett2_usb(
1920 struct usb_mixer_interface *mixer, u32 cmd,
1921 void *req_data, u16 req_size, void *resp_data, u16 resp_size)
1923 struct scarlett2_data *private = mixer->private_data;
1924 struct usb_device *dev = mixer->chip->dev;
1925 struct scarlett2_usb_packet *req, *resp = NULL;
1926 size_t req_buf_size = struct_size(req, data, req_size);
1927 size_t resp_buf_size = struct_size(resp, data, resp_size);
1930 req = kmalloc(req_buf_size, GFP_KERNEL);
1936 resp = kmalloc(resp_buf_size, GFP_KERNEL);
1942 mutex_lock(&private->usb_mutex);
1944 /* build request message and send it */
1946 scarlett2_fill_request_header(private, req, cmd, req_size);
1949 memcpy(req->data, req_data, req_size);
1951 err = scarlett2_usb_tx(dev, private->bInterfaceNumber,
1954 if (err != req_buf_size) {
1957 "%s USB request result cmd %x was %d\n",
1958 private->series_name, cmd, err);
1963 /* send a second message to get the response */
1965 err = scarlett2_usb_rx(dev, private->bInterfaceNumber,
1966 SCARLETT2_USB_CMD_RESP,
1967 resp, resp_buf_size);
1969 /* validate the response */
1971 if (err != resp_buf_size) {
1973 /* ESHUTDOWN and EPROTO are valid responses to a
1976 if (cmd == SCARLETT2_USB_REBOOT &&
1977 (err == -ESHUTDOWN || err == -EPROTO)) {
1984 "%s USB response result cmd %x was %d expected %zu\n",
1985 private->series_name, cmd, err, resp_buf_size);
1990 /* cmd/seq/size should match except when initialising
1991 * seq sent = 1, response = 0
1993 if (resp->cmd != req->cmd ||
1994 (resp->seq != req->seq &&
1995 (le16_to_cpu(req->seq) != 1 || resp->seq != 0)) ||
1996 resp_size != le16_to_cpu(resp->size) ||
2001 "%s USB invalid response; "
2002 "cmd tx/rx %d/%d seq %d/%d size %d/%d "
2003 "error %d pad %d\n",
2004 private->series_name,
2005 le32_to_cpu(req->cmd), le32_to_cpu(resp->cmd),
2006 le16_to_cpu(req->seq), le16_to_cpu(resp->seq),
2007 resp_size, le16_to_cpu(resp->size),
2008 le32_to_cpu(resp->error),
2009 le32_to_cpu(resp->pad));
2014 if (resp_data && resp_size > 0)
2015 memcpy(resp_data, resp->data, resp_size);
2018 mutex_unlock(&private->usb_mutex);
2025 /* Send a USB message to get data; result placed in *buf */
2026 static int scarlett2_usb_get(
2027 struct usb_mixer_interface *mixer,
2028 int offset, void *buf, int size)
2035 req.offset = cpu_to_le32(offset);
2036 req.size = cpu_to_le32(size);
2037 return scarlett2_usb(mixer, SCARLETT2_USB_GET_DATA,
2038 &req, sizeof(req), buf, size);
2041 /* Return true if the given configuration item is present in the
2042 * configuration set used by this device.
2044 static int scarlett2_has_config_item(
2045 struct scarlett2_data *private, int config_item_num)
2047 return !!private->config_set->items[config_item_num].offset;
2050 /* Send a USB message to get configuration parameters; result placed in *buf */
2051 static int scarlett2_usb_get_config(
2052 struct usb_mixer_interface *mixer,
2053 int config_item_num, int count, void *buf)
2055 struct scarlett2_data *private = mixer->private_data;
2056 const struct scarlett2_config *config_item =
2057 &private->config_set->items[config_item_num];
2062 /* Check that the configuration item is present in the
2063 * configuration set used by this device
2065 if (!config_item->offset)
2068 /* Gen 4 style parameters are always 1 byte */
2069 size = config_item->size ? config_item->size : 8;
2071 /* For byte-sized parameters, retrieve directly into buf */
2073 size = size / 8 * count;
2074 err = scarlett2_usb_get(mixer, config_item->offset, buf, size);
2080 for (i = 0; i < count; i++, buf_16++)
2081 *buf_16 = le16_to_cpu(*(__le16 *)buf_16);
2086 /* For bit-sized parameters, retrieve into value */
2087 err = scarlett2_usb_get(mixer, config_item->offset, &value, 1);
2091 /* then unpack from value into buf[] */
2093 for (i = 0; i < 8 && i < count; i++, value >>= 1)
2094 *buf_8++ = value & 1;
2099 /* Send a SCARLETT2_USB_SET_DATA command.
2100 * offset: location in the device's data space
2101 * size: size in bytes of the value (1, 2, 4)
2103 static int scarlett2_usb_set_data(
2104 struct usb_mixer_interface *mixer,
2105 int offset, int size, int value)
2107 struct scarlett2_data *private = mixer->private_data;
2114 req.offset = cpu_to_le32(offset);
2115 req.size = cpu_to_le32(size);
2116 req.value = cpu_to_le32(value);
2117 return scarlett2_usb(private->mixer, SCARLETT2_USB_SET_DATA,
2118 &req, sizeof(u32) * 2 + size, NULL, 0);
2121 /* Send a SCARLETT2_USB_DATA_CMD command.
2122 * Configuration changes require activation with this after they have
2123 * been uploaded by a previous SCARLETT2_USB_SET_DATA.
2124 * The value for activate needed is determined by the configuration
2127 static int scarlett2_usb_activate_config(
2128 struct usb_mixer_interface *mixer, int activate)
2132 req = cpu_to_le32(activate);
2133 return scarlett2_usb(mixer, SCARLETT2_USB_DATA_CMD,
2134 &req, sizeof(req), NULL, 0);
2137 /* Send USB messages to set a SCARLETT2_CONFIG_* parameter */
2138 static int scarlett2_usb_set_config(
2139 struct usb_mixer_interface *mixer,
2140 int config_item_num, int index, int value)
2142 struct scarlett2_data *private = mixer->private_data;
2143 const struct scarlett2_config_set *config_set = private->config_set;
2144 const struct scarlett2_config *config_item =
2145 &config_set->items[config_item_num];
2149 /* Check that the configuration item is present in the
2150 * configuration set used by this device
2152 if (!config_item->offset)
2155 /* Gen 4 style writes are selected with size = 0;
2156 * these are only byte-sized values written through a shared
2157 * location, different to the read address
2159 if (!config_item->size) {
2160 if (!config_set->gen4_write_addr)
2163 /* Place index in gen4_write_addr + 1 */
2164 err = scarlett2_usb_set_data(
2165 mixer, config_set->gen4_write_addr + 1, 1, index);
2169 /* Place value in gen4_write_addr */
2170 err = scarlett2_usb_set_data(
2171 mixer, config_set->gen4_write_addr, 1, value);
2175 /* Request the interface do the write */
2176 return scarlett2_usb_activate_config(
2177 mixer, config_item->activate);
2180 /* Not-Gen 4 style needs NVRAM save, supports
2181 * bit-modification, and writing is done to the same place
2182 * that the value can be read from
2185 /* Cancel any pending NVRAM save */
2186 cancel_delayed_work_sync(&private->work);
2188 /* Convert config_item->size in bits to size in bytes and
2191 if (config_item->size >= 8) {
2192 size = config_item->size / 8;
2193 offset = config_item->offset + index * size;
2195 /* If updating a bit, retrieve the old value, set/clear the
2196 * bit as needed, and update value
2202 offset = config_item->offset;
2204 err = scarlett2_usb_get(mixer, offset, &tmp, 1);
2209 tmp |= (1 << index);
2211 tmp &= ~(1 << index);
2216 /* Send the configuration parameter data */
2217 err = scarlett2_usb_set_data(mixer, offset, size, value);
2221 /* Activate the change */
2222 err = scarlett2_usb_activate_config(mixer, config_item->activate);
2226 /* Gen 2 style writes to Gen 4 devices don't need saving */
2227 if (config_set->gen4_write_addr)
2230 /* Schedule the change to be written to NVRAM */
2231 if (config_item->activate != SCARLETT2_USB_CONFIG_SAVE)
2232 schedule_delayed_work(&private->work, msecs_to_jiffies(2000));
2237 /* Send SCARLETT2_USB_DATA_CMD SCARLETT2_USB_CONFIG_SAVE */
2238 static void scarlett2_config_save(struct usb_mixer_interface *mixer)
2242 err = scarlett2_usb_activate_config(mixer, SCARLETT2_USB_CONFIG_SAVE);
2244 usb_audio_err(mixer->chip, "config save failed: %d\n", err);
2247 /* Delayed work to save config */
2248 static void scarlett2_config_save_work(struct work_struct *work)
2250 struct scarlett2_data *private =
2251 container_of(work, struct scarlett2_data, work.work);
2253 scarlett2_config_save(private->mixer);
2256 /* Send a USB message to get sync status; result placed in *sync */
2257 static int scarlett2_usb_get_sync_status(
2258 struct usb_mixer_interface *mixer,
2264 err = scarlett2_usb(mixer, SCARLETT2_USB_GET_SYNC,
2265 NULL, 0, &data, sizeof(data));
2273 /* Return true if the device has a mixer that we can control */
2274 static int scarlett2_has_mixer(struct scarlett2_data *private)
2276 return !!private->info->mux_assignment[0][0].count;
2279 /* Map from mixer value to (db + 80) * 2
2280 * (reverse of scarlett2_mixer_values[])
2282 static int scarlett2_mixer_value_to_db(int value)
2286 for (i = 0; i < SCARLETT2_MIXER_VALUE_COUNT; i++)
2287 if (scarlett2_mixer_values[i] >= value)
2289 return SCARLETT2_MIXER_MAX_VALUE;
2292 /* Send a USB message to get the volumes for all inputs of one mix
2293 * and put the values into private->mix[]
2295 static int scarlett2_usb_get_mix(struct usb_mixer_interface *mixer,
2298 struct scarlett2_data *private = mixer->private_data;
2300 int num_mixer_in = private->num_mix_in;
2308 __le16 data[SCARLETT2_INPUT_MIX_MAX];
2310 req.mix_num = cpu_to_le16(mix_num);
2311 req.count = cpu_to_le16(num_mixer_in);
2313 err = scarlett2_usb(mixer, SCARLETT2_USB_GET_MIX,
2315 data, num_mixer_in * sizeof(u16));
2319 for (i = 0, j = mix_num * num_mixer_in; i < num_mixer_in; i++, j++)
2320 private->mix[j] = scarlett2_mixer_value_to_db(
2321 le16_to_cpu(data[i]));
2326 /* Send a USB message to set the volumes for all inputs of one mix
2327 * (values obtained from private->mix[])
2329 static int scarlett2_usb_set_mix(struct usb_mixer_interface *mixer,
2332 struct scarlett2_data *private = mixer->private_data;
2336 __le16 data[SCARLETT2_INPUT_MIX_MAX];
2340 int num_mixer_in = private->num_mix_in;
2342 req.mix_num = cpu_to_le16(mix_num);
2344 for (i = 0, j = mix_num * num_mixer_in; i < num_mixer_in; i++, j++)
2345 req.data[i] = cpu_to_le16(
2346 scarlett2_mixer_values[private->mix[j]]
2349 return scarlett2_usb(mixer, SCARLETT2_USB_SET_MIX,
2350 &req, (num_mixer_in + 1) * sizeof(u16),
2354 /* Convert a port number index (per info->port_count) to a hardware ID */
2355 static u32 scarlett2_mux_src_num_to_id(
2356 const int port_count[][SCARLETT2_PORT_DIRNS], int num)
2361 port_type < SCARLETT2_PORT_TYPE_COUNT;
2363 if (num < port_count[port_type][SCARLETT2_PORT_IN])
2364 return scarlett2_ports[port_type].id | num;
2365 num -= port_count[port_type][SCARLETT2_PORT_IN];
2372 /* Convert a hardware ID to a port number index */
2373 static u32 scarlett2_mux_id_to_num(
2374 const int port_count[][SCARLETT2_PORT_DIRNS], int direction, u32 id)
2380 port_type < SCARLETT2_PORT_TYPE_COUNT;
2382 int base = scarlett2_ports[port_type].id;
2383 int count = port_count[port_type][direction];
2385 if (id >= base && id < base + count)
2386 return port_num + id - base;
2394 /* Convert one mux entry from the interface and load into private->mux[] */
2395 static void scarlett2_usb_populate_mux(struct scarlett2_data *private,
2398 const struct scarlett2_device_info *info = private->info;
2399 const int (*port_count)[SCARLETT2_PORT_DIRNS] = info->port_count;
2401 int dst_idx, src_idx;
2403 dst_idx = scarlett2_mux_id_to_num(port_count, SCARLETT2_PORT_OUT,
2408 if (dst_idx >= private->num_mux_dsts) {
2409 usb_audio_err(private->mixer->chip,
2410 "BUG: scarlett2_mux_id_to_num(%06x, OUT): %d >= %d",
2411 mux_entry, dst_idx, private->num_mux_dsts);
2415 src_idx = scarlett2_mux_id_to_num(port_count, SCARLETT2_PORT_IN,
2420 if (src_idx >= private->num_mux_srcs) {
2421 usb_audio_err(private->mixer->chip,
2422 "BUG: scarlett2_mux_id_to_num(%06x, IN): %d >= %d",
2423 mux_entry, src_idx, private->num_mux_srcs);
2427 private->mux[dst_idx] = src_idx;
2430 /* Update the meter level map
2432 * The meter level data from the interface (SCARLETT2_USB_GET_METER
2433 * request) is returned in mux_assignment order, but to avoid exposing
2434 * that to userspace, scarlett2_meter_ctl_get() rearranges the data
2435 * into scarlett2_ports order using the meter_level_map[] array which
2436 * is set up by this function.
2438 * In addition, the meter level data values returned from the
2439 * interface are invalid for destinations where:
2441 * - the source is "Off"; therefore we set those values to zero (map
2444 * - the source is assigned to a previous (with respect to the
2445 * mux_assignment order) destination; therefore we set those values
2446 * to the value previously reported for that source
2448 static void scarlett2_update_meter_level_map(struct scarlett2_data *private)
2450 const struct scarlett2_device_info *info = private->info;
2451 const struct scarlett2_meter_entry *entry;
2453 /* sources already assigned to a destination
2454 * value is 255 for None, otherwise the value of i
2455 * (index into array returned by
2456 * scarlett2_usb_get_meter_levels())
2458 u8 seen_src[SCARLETT2_MAX_SRCS] = { 1 };
2459 u8 seen_src_value[SCARLETT2_MAX_SRCS] = { 255 };
2461 /* index in meter_map[] order */
2464 /* go through the meter_map[] entries */
2465 for (entry = info->meter_map;
2469 /* fill in each meter_level_map[] entry */
2472 for (j = 0, mux_idx = entry->start;
2474 i++, j++, mux_idx++) {
2476 /* convert mux_idx using line_out_unmap[] */
2478 info->line_out_remap_enable &&
2479 mux_idx < private->num_line_out
2480 ) ? info->line_out_unmap[mux_idx]
2483 /* check which source is connected, and if
2484 * that source is already connected elsewhere,
2485 * use that existing connection's destination
2486 * for this meter entry instead
2488 int mux_src = private->mux[mux_idx];
2490 if (!seen_src[mux_src]) {
2491 seen_src[mux_src] = 1;
2492 seen_src_value[mux_src] = i;
2494 private->meter_level_map[map_mux_idx] =
2495 seen_src_value[mux_src];
2500 /* Send USB message to get mux inputs and then populate private->mux[] */
2501 static int scarlett2_usb_get_mux(struct usb_mixer_interface *mixer)
2503 struct scarlett2_data *private = mixer->private_data;
2504 int count = private->num_mux_dsts;
2512 __le32 data[SCARLETT2_MUX_MAX];
2514 private->mux_updated = 0;
2517 req.count = cpu_to_le16(count);
2519 err = scarlett2_usb(mixer, SCARLETT2_USB_GET_MUX,
2521 data, count * sizeof(u32));
2525 for (i = 0; i < count; i++)
2526 scarlett2_usb_populate_mux(private, le32_to_cpu(data[i]));
2528 scarlett2_update_meter_level_map(private);
2533 /* Send USB messages to set mux inputs */
2534 static int scarlett2_usb_set_mux(struct usb_mixer_interface *mixer)
2536 struct scarlett2_data *private = mixer->private_data;
2537 const struct scarlett2_device_info *info = private->info;
2538 const int (*port_count)[SCARLETT2_PORT_DIRNS] = info->port_count;
2544 __le32 data[SCARLETT2_MUX_MAX];
2549 /* set mux settings for each rate */
2550 for (table = 0; table < SCARLETT2_MUX_TABLES; table++) {
2551 const struct scarlett2_mux_entry *entry;
2553 /* i counts over the output array */
2556 req.num = cpu_to_le16(table);
2558 /* loop through each entry */
2559 for (entry = info->mux_assignment[table];
2563 int port_type = entry->port_type;
2564 int port_idx = entry->start;
2565 int mux_idx = scarlett2_get_port_start_num(port_count,
2566 SCARLETT2_PORT_OUT, port_type) + port_idx;
2567 int dst_id = scarlett2_ports[port_type].id + port_idx;
2571 for (j = 0; j < entry->count; j++)
2576 /* Non-empty mux slots use the lower 12 bits
2577 * for the destination and next 12 bits for
2580 for (j = 0; j < entry->count; j++) {
2581 int src_id = scarlett2_mux_src_num_to_id(
2582 port_count, private->mux[mux_idx++]);
2583 req.data[i++] = cpu_to_le32(dst_id |
2589 err = scarlett2_usb(mixer, SCARLETT2_USB_SET_MUX,
2590 &req, (i + 1) * sizeof(u32),
2596 scarlett2_update_meter_level_map(private);
2601 /* Send USB message to get meter levels */
2602 static int scarlett2_usb_get_meter_levels(struct usb_mixer_interface *mixer,
2603 u16 num_meters, u16 *levels)
2610 __le32 resp[SCARLETT2_MAX_METERS];
2614 req.num_meters = cpu_to_le16(num_meters);
2615 req.magic = cpu_to_le32(SCARLETT2_USB_METER_LEVELS_GET_MAGIC);
2616 err = scarlett2_usb(mixer, SCARLETT2_USB_GET_METER,
2617 &req, sizeof(req), resp, num_meters * sizeof(u32));
2621 /* copy, convert to u16 */
2622 for (i = 0; i < num_meters; i++)
2623 levels[i] = le32_to_cpu(resp[i]);
2628 /* For config items with mute=1, xor bits 0 & 1 together to get the
2629 * current/next state. This won't have any effect on values which are
2632 static uint8_t scarlett2_decode_muteable(uint8_t v)
2634 return (v ^ (v >> 1)) & 1;
2637 /*** Control Functions ***/
2639 /* helper function to create a new control */
2640 static int scarlett2_add_new_ctl(struct usb_mixer_interface *mixer,
2641 const struct snd_kcontrol_new *ncontrol,
2642 int index, int channels, const char *name,
2643 struct snd_kcontrol **kctl_return)
2645 struct snd_kcontrol *kctl;
2646 struct usb_mixer_elem_info *elem;
2649 elem = kzalloc(sizeof(*elem), GFP_KERNEL);
2653 /* We set USB_MIXER_BESPOKEN type, so that the core USB mixer code
2654 * ignores them for resume and other operations.
2655 * Also, the head.id field is set to 0, as we don't use this field.
2657 elem->head.mixer = mixer;
2658 elem->control = index;
2660 elem->channels = channels;
2661 elem->val_type = USB_MIXER_BESPOKEN;
2663 kctl = snd_ctl_new1(ncontrol, elem);
2668 kctl->private_free = snd_usb_mixer_elem_free;
2670 strscpy(kctl->id.name, name, sizeof(kctl->id.name));
2672 err = snd_usb_mixer_add_control(&elem->head, kctl);
2677 *kctl_return = kctl;
2682 /*** Firmware Version Control ***/
2684 static int scarlett2_firmware_version_ctl_get(
2685 struct snd_kcontrol *kctl,
2686 struct snd_ctl_elem_value *ucontrol)
2688 struct usb_mixer_elem_info *elem = kctl->private_data;
2689 struct scarlett2_data *private = elem->head.mixer->private_data;
2691 ucontrol->value.integer.value[0] = private->firmware_version;
2696 static int scarlett2_firmware_version_ctl_info(
2697 struct snd_kcontrol *kctl,
2698 struct snd_ctl_elem_info *uinfo)
2700 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2706 static const struct snd_kcontrol_new scarlett2_firmware_version_ctl = {
2707 .iface = SNDRV_CTL_ELEM_IFACE_CARD,
2708 .access = SNDRV_CTL_ELEM_ACCESS_READ,
2710 .info = scarlett2_firmware_version_ctl_info,
2711 .get = scarlett2_firmware_version_ctl_get
2714 static int scarlett2_add_firmware_version_ctl(
2715 struct usb_mixer_interface *mixer)
2717 return scarlett2_add_new_ctl(mixer, &scarlett2_firmware_version_ctl,
2718 0, 0, "Firmware Version", NULL);
2721 /*** Minimum Firmware Version Control ***/
2723 static int scarlett2_min_firmware_version_ctl_get(
2724 struct snd_kcontrol *kctl,
2725 struct snd_ctl_elem_value *ucontrol)
2727 struct usb_mixer_elem_info *elem = kctl->private_data;
2728 struct scarlett2_data *private = elem->head.mixer->private_data;
2730 ucontrol->value.integer.value[0] = private->info->min_firmware_version;
2735 static int scarlett2_min_firmware_version_ctl_info(
2736 struct snd_kcontrol *kctl,
2737 struct snd_ctl_elem_info *uinfo)
2739 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2745 static const struct snd_kcontrol_new scarlett2_min_firmware_version_ctl = {
2746 .iface = SNDRV_CTL_ELEM_IFACE_CARD,
2747 .access = SNDRV_CTL_ELEM_ACCESS_READ,
2749 .info = scarlett2_min_firmware_version_ctl_info,
2750 .get = scarlett2_min_firmware_version_ctl_get
2753 static int scarlett2_add_min_firmware_version_ctl(
2754 struct usb_mixer_interface *mixer)
2756 return scarlett2_add_new_ctl(mixer, &scarlett2_min_firmware_version_ctl,
2757 0, 0, "Minimum Firmware Version", NULL);
2760 /*** Sync Control ***/
2762 /* Update sync control after receiving notification that the status
2765 static int scarlett2_update_sync(struct usb_mixer_interface *mixer)
2767 struct scarlett2_data *private = mixer->private_data;
2769 private->sync_updated = 0;
2770 return scarlett2_usb_get_sync_status(mixer, &private->sync);
2773 static int scarlett2_sync_ctl_info(struct snd_kcontrol *kctl,
2774 struct snd_ctl_elem_info *uinfo)
2776 static const char *texts[2] = {
2777 "Unlocked", "Locked"
2779 return snd_ctl_enum_info(uinfo, 1, 2, texts);
2782 static int scarlett2_sync_ctl_get(struct snd_kcontrol *kctl,
2783 struct snd_ctl_elem_value *ucontrol)
2785 struct usb_mixer_elem_info *elem = kctl->private_data;
2786 struct usb_mixer_interface *mixer = elem->head.mixer;
2787 struct scarlett2_data *private = mixer->private_data;
2790 mutex_lock(&private->data_mutex);
2792 if (private->hwdep_in_use) {
2797 if (private->sync_updated) {
2798 err = scarlett2_update_sync(mixer);
2802 ucontrol->value.enumerated.item[0] = private->sync;
2805 mutex_unlock(&private->data_mutex);
2809 static const struct snd_kcontrol_new scarlett2_sync_ctl = {
2810 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2811 .access = SNDRV_CTL_ELEM_ACCESS_READ,
2813 .info = scarlett2_sync_ctl_info,
2814 .get = scarlett2_sync_ctl_get
2817 static int scarlett2_add_sync_ctl(struct usb_mixer_interface *mixer)
2819 struct scarlett2_data *private = mixer->private_data;
2821 /* devices without a mixer also don't support reporting sync status */
2822 if (!scarlett2_has_mixer(private))
2825 return scarlett2_add_new_ctl(mixer, &scarlett2_sync_ctl,
2826 0, 1, "Sync Status", &private->sync_ctl);
2829 /*** Autogain Switch and Status Controls ***/
2831 /* Forward declarations as phantom power and autogain can disable each other */
2832 static int scarlett2_check_input_phantom_updated(struct usb_mixer_interface *);
2833 static int scarlett2_phantom_is_switching(struct scarlett2_data *, int);
2835 /* Set the access mode of a control to read-only (val = 0) or
2836 * read-write (val = 1).
2838 static void scarlett2_set_ctl_access(struct snd_kcontrol *kctl, int val)
2841 kctl->vd[0].access |= SNDRV_CTL_ELEM_ACCESS_WRITE;
2843 kctl->vd[0].access &= ~SNDRV_CTL_ELEM_ACCESS_WRITE;
2846 /* Check if autogain is running on any input */
2847 static int scarlett2_autogain_is_running(struct scarlett2_data *private)
2851 /* autogain_status[] is 0 if autogain is running */
2852 for (i = 0; i < private->info->gain_input_count; i++)
2853 if (!private->autogain_status[i])
2859 static int scarlett2_update_autogain(struct usb_mixer_interface *mixer)
2861 struct scarlett2_data *private = mixer->private_data;
2862 const struct scarlett2_device_info *info = private->info;
2864 u8 raw_autogain_status[SCARLETT2_INPUT_GAIN_MAX];
2866 private->autogain_updated = 0;
2868 if (!info->gain_input_count)
2871 err = scarlett2_usb_get_config(
2872 mixer, SCARLETT2_CONFIG_AUTOGAIN_SWITCH,
2873 info->gain_input_count, private->autogain_switch);
2876 err = scarlett2_usb_get_config(
2877 mixer, SCARLETT2_CONFIG_AUTOGAIN_STATUS,
2878 info->gain_input_count, raw_autogain_status);
2882 /* Translate autogain_switch and raw_autogain_status into
2885 * When autogain_switch[] is set, the status is the first
2886 * element in scarlett2_autogain_status_texts[] (Running). The
2887 * subsequent elements correspond to the status value from the
2888 * device (raw_autogain_status[]) + 1. The last element is
2889 * "Invalid", in case the device reports a status outside the
2890 * range of scarlett2_autogain_status_texts[].
2892 for (i = 0; i < info->gain_input_count; i++)
2893 if (private->autogain_switch[i])
2894 private->autogain_status[i] = 0;
2895 else if (raw_autogain_status[i] <
2896 ARRAY_SIZE(scarlett2_autogain_status_texts) - 1)
2897 private->autogain_status[i] =
2898 raw_autogain_status[i] + 1;
2900 private->autogain_status[i] =
2901 ARRAY_SIZE(scarlett2_autogain_status_texts) - 1;
2906 /* Update access mode for controls affected by autogain */
2907 static void scarlett2_autogain_update_access(struct usb_mixer_interface *mixer)
2909 struct scarlett2_data *private = mixer->private_data;
2910 const struct scarlett2_device_info *info = private->info;
2911 int val = !scarlett2_autogain_is_running(private);
2914 scarlett2_set_ctl_access(private->input_select_ctl, val);
2915 for (i = 0; i < info->gain_input_count / 2; i++)
2916 scarlett2_set_ctl_access(private->input_link_ctls[i], val);
2917 for (i = 0; i < info->gain_input_count; i++) {
2918 scarlett2_set_ctl_access(private->input_gain_ctls[i], val);
2919 scarlett2_set_ctl_access(private->safe_ctls[i], val);
2921 for (i = 0; i < info->level_input_count; i++)
2922 scarlett2_set_ctl_access(private->level_ctls[i], val);
2923 for (i = 0; i < info->air_input_count; i++)
2924 scarlett2_set_ctl_access(private->air_ctls[i], val);
2925 for (i = 0; i < info->phantom_count; i++)
2926 scarlett2_set_ctl_access(private->phantom_ctls[i], val);
2929 /* Notify of access mode change for all controls read-only while
2932 static void scarlett2_autogain_notify_access(struct usb_mixer_interface *mixer)
2934 struct snd_card *card = mixer->chip->card;
2935 struct scarlett2_data *private = mixer->private_data;
2936 const struct scarlett2_device_info *info = private->info;
2939 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_INFO,
2940 &private->input_select_ctl->id);
2941 for (i = 0; i < info->gain_input_count / 2; i++)
2942 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_INFO,
2943 &private->input_link_ctls[i]->id);
2944 for (i = 0; i < info->gain_input_count; i++) {
2945 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_INFO,
2946 &private->input_gain_ctls[i]->id);
2947 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_INFO,
2948 &private->safe_ctls[i]->id);
2950 for (i = 0; i < info->level_input_count; i++)
2951 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_INFO,
2952 &private->level_ctls[i]->id);
2953 for (i = 0; i < info->air_input_count; i++)
2954 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_INFO,
2955 &private->air_ctls[i]->id);
2956 for (i = 0; i < info->phantom_count; i++)
2957 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_INFO,
2958 &private->phantom_ctls[i]->id);
2961 /* Call scarlett2_update_autogain() and
2962 * scarlett2_autogain_update_access() if autogain_updated is set.
2964 static int scarlett2_check_autogain_updated(
2965 struct usb_mixer_interface *mixer)
2967 struct scarlett2_data *private = mixer->private_data;
2970 if (!private->autogain_updated)
2973 err = scarlett2_update_autogain(mixer);
2977 scarlett2_autogain_update_access(mixer);
2982 /* If autogain_updated is set when a *_ctl_put() function for a
2983 * control that is meant to be read-only while autogain is running,
2984 * update the autogain status and access mode of affected controls.
2985 * Return -EPERM if autogain is running.
2987 static int scarlett2_check_put_during_autogain(
2988 struct usb_mixer_interface *mixer)
2990 int err = scarlett2_check_autogain_updated(mixer);
2995 if (scarlett2_autogain_is_running(mixer->private_data))
3001 static int scarlett2_autogain_switch_ctl_info(
3002 struct snd_kcontrol *kctl, struct snd_ctl_elem_info *uinfo)
3004 struct usb_mixer_elem_info *elem = kctl->private_data;
3005 struct usb_mixer_interface *mixer = elem->head.mixer;
3006 struct scarlett2_data *private = mixer->private_data;
3009 mutex_lock(&private->data_mutex);
3011 err = scarlett2_check_input_phantom_updated(mixer);
3015 err = snd_ctl_boolean_mono_info(kctl, uinfo);
3018 mutex_unlock(&private->data_mutex);
3022 static int scarlett2_autogain_switch_ctl_get(
3023 struct snd_kcontrol *kctl, struct snd_ctl_elem_value *ucontrol)
3025 struct usb_mixer_elem_info *elem = kctl->private_data;
3026 struct usb_mixer_interface *mixer = elem->head.mixer;
3027 struct scarlett2_data *private = mixer->private_data;
3030 mutex_lock(&private->data_mutex);
3032 if (private->hwdep_in_use) {
3037 err = scarlett2_check_autogain_updated(mixer);
3041 ucontrol->value.enumerated.item[0] =
3042 private->autogain_switch[elem->control];
3045 mutex_unlock(&private->data_mutex);
3049 static int scarlett2_autogain_status_ctl_get(
3050 struct snd_kcontrol *kctl, struct snd_ctl_elem_value *ucontrol)
3052 struct usb_mixer_elem_info *elem = kctl->private_data;
3053 struct usb_mixer_interface *mixer = elem->head.mixer;
3054 struct scarlett2_data *private = mixer->private_data;
3057 mutex_lock(&private->data_mutex);
3059 if (private->hwdep_in_use) {
3064 err = scarlett2_check_autogain_updated(mixer);
3068 ucontrol->value.enumerated.item[0] =
3069 private->autogain_status[elem->control];
3072 mutex_unlock(&private->data_mutex);
3076 static int scarlett2_autogain_switch_ctl_put(
3077 struct snd_kcontrol *kctl, struct snd_ctl_elem_value *ucontrol)
3079 struct usb_mixer_elem_info *elem = kctl->private_data;
3080 struct usb_mixer_interface *mixer = elem->head.mixer;
3081 struct scarlett2_data *private = mixer->private_data;
3083 int index = elem->control;
3086 mutex_lock(&private->data_mutex);
3088 if (private->hwdep_in_use) {
3093 err = scarlett2_check_input_phantom_updated(mixer);
3097 if (scarlett2_phantom_is_switching(private, index)) {
3102 oval = private->autogain_switch[index];
3103 val = !!ucontrol->value.integer.value[0];
3108 private->autogain_switch[index] = val;
3110 /* Send switch change to the device */
3111 err = scarlett2_usb_set_config(
3112 mixer, SCARLETT2_CONFIG_AUTOGAIN_SWITCH, index, val);
3116 scarlett2_autogain_update_access(mixer);
3117 scarlett2_autogain_notify_access(mixer);
3120 mutex_unlock(&private->data_mutex);
3124 static int scarlett2_autogain_status_ctl_info(
3125 struct snd_kcontrol *kctl, struct snd_ctl_elem_info *uinfo)
3127 return snd_ctl_enum_info(
3129 ARRAY_SIZE(scarlett2_autogain_status_texts),
3130 scarlett2_autogain_status_texts);
3133 static const struct snd_kcontrol_new scarlett2_autogain_switch_ctl = {
3134 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3136 .info = scarlett2_autogain_switch_ctl_info,
3137 .get = scarlett2_autogain_switch_ctl_get,
3138 .put = scarlett2_autogain_switch_ctl_put
3141 static const struct snd_kcontrol_new scarlett2_autogain_status_ctl = {
3142 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3143 .access = SNDRV_CTL_ELEM_ACCESS_READ,
3145 .info = scarlett2_autogain_status_ctl_info,
3146 .get = scarlett2_autogain_status_ctl_get,
3149 /*** Input Select Control ***/
3151 static int scarlett2_update_input_select(struct usb_mixer_interface *mixer)
3153 struct scarlett2_data *private = mixer->private_data;
3154 const struct scarlett2_device_info *info = private->info;
3155 int link_count = info->gain_input_count / 2;
3158 private->input_select_updated = 0;
3163 err = scarlett2_usb_get_config(
3164 mixer, SCARLETT2_CONFIG_INPUT_SELECT_SWITCH,
3165 1, &private->input_select_switch);
3169 err = scarlett2_usb_get_config(
3170 mixer, SCARLETT2_CONFIG_INPUT_LINK_SWITCH,
3171 link_count, private->input_link_switch);
3175 /* simplified because no model yet has link_count > 1 */
3176 if (private->input_link_switch[0])
3177 private->input_select_switch = 0;
3182 static int scarlett2_input_select_ctl_get(
3183 struct snd_kcontrol *kctl, struct snd_ctl_elem_value *ucontrol)
3185 struct usb_mixer_elem_info *elem = kctl->private_data;
3186 struct usb_mixer_interface *mixer = elem->head.mixer;
3187 struct scarlett2_data *private = mixer->private_data;
3190 mutex_lock(&private->data_mutex);
3192 if (private->hwdep_in_use) {
3197 if (private->input_select_updated) {
3198 err = scarlett2_update_input_select(mixer);
3202 ucontrol->value.enumerated.item[0] = private->input_select_switch;
3205 mutex_unlock(&private->data_mutex);
3209 static int scarlett2_input_select_ctl_put(
3210 struct snd_kcontrol *kctl, struct snd_ctl_elem_value *ucontrol)
3212 struct usb_mixer_elem_info *elem = kctl->private_data;
3213 struct usb_mixer_interface *mixer = elem->head.mixer;
3214 struct scarlett2_data *private = mixer->private_data;
3217 int max_val = private->input_link_switch[0] ? 0 : 1;
3219 mutex_lock(&private->data_mutex);
3221 if (private->hwdep_in_use) {
3226 err = scarlett2_check_put_during_autogain(mixer);
3230 oval = private->input_select_switch;
3231 val = ucontrol->value.integer.value[0];
3235 else if (val > max_val)
3241 private->input_select_switch = val;
3243 /* Send switch change to the device if inputs not linked */
3244 if (!private->input_link_switch[0])
3245 err = scarlett2_usb_set_config(
3246 mixer, SCARLETT2_CONFIG_INPUT_SELECT_SWITCH,
3252 mutex_unlock(&private->data_mutex);
3256 static int scarlett2_input_select_ctl_info(
3257 struct snd_kcontrol *kctl, struct snd_ctl_elem_info *uinfo)
3259 struct usb_mixer_elem_info *elem = kctl->private_data;
3260 struct usb_mixer_interface *mixer = elem->head.mixer;
3261 struct scarlett2_data *private = mixer->private_data;
3263 int inputs = private->info->gain_input_count;
3266 char **values = kcalloc(inputs, sizeof(char *), GFP_KERNEL);
3271 mutex_lock(&private->data_mutex);
3273 if (private->hwdep_in_use) {
3278 err = scarlett2_check_autogain_updated(mixer);
3282 /* Loop through each input
3283 * Linked inputs have one value for the pair
3285 for (i = 0, j = 0; i < inputs; i++) {
3286 if (private->input_link_switch[i / 2]) {
3287 values[j++] = kasprintf(
3288 GFP_KERNEL, "Input %d-%d", i + 1, i + 2);
3291 values[j++] = kasprintf(
3292 GFP_KERNEL, "Input %d", i + 1);
3296 err = snd_ctl_enum_info(uinfo, 1, j,
3297 (const char * const *)values);
3300 mutex_unlock(&private->data_mutex);
3302 for (i = 0; i < inputs; i++)
3309 static const struct snd_kcontrol_new scarlett2_input_select_ctl = {
3310 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3312 .info = scarlett2_input_select_ctl_info,
3313 .get = scarlett2_input_select_ctl_get,
3314 .put = scarlett2_input_select_ctl_put,
3317 /*** Input Link Switch Controls ***/
3319 /* snd_ctl_boolean_mono_info() with autogain-updated check
3320 * (for controls that are read-only while autogain is running)
3322 static int scarlett2_autogain_disables_ctl_info(struct snd_kcontrol *kctl,
3323 struct snd_ctl_elem_info *uinfo)
3325 struct usb_mixer_elem_info *elem = kctl->private_data;
3326 struct usb_mixer_interface *mixer = elem->head.mixer;
3327 struct scarlett2_data *private = mixer->private_data;
3330 mutex_lock(&private->data_mutex);
3332 if (private->hwdep_in_use) {
3337 err = scarlett2_check_autogain_updated(mixer);
3341 err = snd_ctl_boolean_mono_info(kctl, uinfo);
3344 mutex_unlock(&private->data_mutex);
3348 static int scarlett2_input_link_ctl_get(
3349 struct snd_kcontrol *kctl, struct snd_ctl_elem_value *ucontrol)
3351 struct usb_mixer_elem_info *elem = kctl->private_data;
3352 struct usb_mixer_interface *mixer = elem->head.mixer;
3353 struct scarlett2_data *private = mixer->private_data;
3356 mutex_lock(&private->data_mutex);
3358 if (private->hwdep_in_use) {
3363 if (private->input_select_updated) {
3364 err = scarlett2_update_input_select(mixer);
3368 ucontrol->value.enumerated.item[0] =
3369 private->input_link_switch[elem->control];
3372 mutex_unlock(&private->data_mutex);
3376 static int scarlett2_input_link_ctl_put(
3377 struct snd_kcontrol *kctl, struct snd_ctl_elem_value *ucontrol)
3379 struct usb_mixer_elem_info *elem = kctl->private_data;
3380 struct usb_mixer_interface *mixer = elem->head.mixer;
3381 struct scarlett2_data *private = mixer->private_data;
3383 int index = elem->control;
3386 mutex_lock(&private->data_mutex);
3388 if (private->hwdep_in_use) {
3393 err = scarlett2_check_put_during_autogain(mixer);
3397 oval = private->input_link_switch[index];
3398 val = !!ucontrol->value.integer.value[0];
3403 private->input_link_switch[index] = val;
3405 /* Notify of change in input select options available */
3406 snd_ctl_notify(mixer->chip->card,
3407 SNDRV_CTL_EVENT_MASK_VALUE | SNDRV_CTL_EVENT_MASK_INFO,
3408 &private->input_select_ctl->id);
3409 private->input_select_updated = 1;
3411 /* Send switch change to the device
3412 * Link for channels 1-2 is at index 1
3413 * No device yet has more than 2 channels linked
3415 err = scarlett2_usb_set_config(
3416 mixer, SCARLETT2_CONFIG_INPUT_LINK_SWITCH, index + 1, val);
3421 mutex_unlock(&private->data_mutex);
3425 static const struct snd_kcontrol_new scarlett2_input_link_ctl = {
3426 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3428 .info = scarlett2_autogain_disables_ctl_info,
3429 .get = scarlett2_input_link_ctl_get,
3430 .put = scarlett2_input_link_ctl_put
3433 /*** Input Gain Controls ***/
3435 static int scarlett2_update_input_gain(struct usb_mixer_interface *mixer)
3437 struct scarlett2_data *private = mixer->private_data;
3438 const struct scarlett2_device_info *info = private->info;
3440 private->input_gain_updated = 0;
3442 if (!info->gain_input_count)
3445 return scarlett2_usb_get_config(
3446 mixer, SCARLETT2_CONFIG_INPUT_GAIN,
3447 info->gain_input_count, private->gain);
3450 static int scarlett2_input_gain_ctl_info(struct snd_kcontrol *kctl,
3451 struct snd_ctl_elem_info *uinfo)
3453 struct usb_mixer_elem_info *elem = kctl->private_data;
3454 struct usb_mixer_interface *mixer = elem->head.mixer;
3455 struct scarlett2_data *private = mixer->private_data;
3458 mutex_lock(&private->data_mutex);
3460 if (private->hwdep_in_use) {
3465 err = scarlett2_check_autogain_updated(mixer);
3469 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
3470 uinfo->count = elem->channels;
3471 uinfo->value.integer.min = 0;
3472 uinfo->value.integer.max = SCARLETT2_MAX_GAIN_VALUE;
3473 uinfo->value.integer.step = 1;
3476 mutex_unlock(&private->data_mutex);
3480 static int scarlett2_input_gain_ctl_get(struct snd_kcontrol *kctl,
3481 struct snd_ctl_elem_value *ucontrol)
3483 struct usb_mixer_elem_info *elem = kctl->private_data;
3484 struct usb_mixer_interface *mixer = elem->head.mixer;
3485 struct scarlett2_data *private = mixer->private_data;
3488 mutex_lock(&private->data_mutex);
3490 if (private->hwdep_in_use) {
3495 if (private->input_gain_updated) {
3496 err = scarlett2_update_input_gain(mixer);
3500 ucontrol->value.integer.value[0] =
3501 private->gain[elem->control];
3504 mutex_unlock(&private->data_mutex);
3508 static int scarlett2_input_gain_ctl_put(struct snd_kcontrol *kctl,
3509 struct snd_ctl_elem_value *ucontrol)
3511 struct usb_mixer_elem_info *elem = kctl->private_data;
3512 struct usb_mixer_interface *mixer = elem->head.mixer;
3513 struct scarlett2_data *private = mixer->private_data;
3515 int index = elem->control;
3518 mutex_lock(&private->data_mutex);
3520 if (private->hwdep_in_use) {
3525 err = scarlett2_check_put_during_autogain(mixer);
3529 oval = private->gain[index];
3530 val = ucontrol->value.integer.value[0];
3535 private->gain[index] = val;
3537 /* Send gain change to the device */
3538 err = scarlett2_usb_set_config(mixer, SCARLETT2_CONFIG_INPUT_GAIN,
3544 mutex_unlock(&private->data_mutex);
3548 static const DECLARE_TLV_DB_MINMAX(
3549 db_scale_scarlett2_gain, 0, SCARLETT2_MAX_GAIN_DB * 100
3552 static const struct snd_kcontrol_new scarlett2_input_gain_ctl = {
3553 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3554 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE |
3555 SNDRV_CTL_ELEM_ACCESS_TLV_READ,
3557 .info = scarlett2_input_gain_ctl_info,
3558 .get = scarlett2_input_gain_ctl_get,
3559 .put = scarlett2_input_gain_ctl_put,
3560 .private_value = 0, /* max value */
3561 .tlv = { .p = db_scale_scarlett2_gain }
3564 /*** Safe Controls ***/
3566 static int scarlett2_update_input_safe(struct usb_mixer_interface *mixer)
3568 struct scarlett2_data *private = mixer->private_data;
3569 const struct scarlett2_device_info *info = private->info;
3571 private->input_safe_updated = 0;
3573 if (!info->gain_input_count)
3576 return scarlett2_usb_get_config(
3577 mixer, SCARLETT2_CONFIG_SAFE_SWITCH,
3578 info->gain_input_count, private->safe_switch);
3581 static int scarlett2_safe_ctl_get(struct snd_kcontrol *kctl,
3582 struct snd_ctl_elem_value *ucontrol)
3584 struct usb_mixer_elem_info *elem = kctl->private_data;
3585 struct usb_mixer_interface *mixer = elem->head.mixer;
3586 struct scarlett2_data *private = mixer->private_data;
3589 mutex_lock(&private->data_mutex);
3591 if (private->hwdep_in_use) {
3596 if (private->input_safe_updated) {
3597 err = scarlett2_update_input_safe(mixer);
3601 ucontrol->value.integer.value[0] =
3602 private->safe_switch[elem->control];
3605 mutex_unlock(&private->data_mutex);
3609 static int scarlett2_safe_ctl_put(struct snd_kcontrol *kctl,
3610 struct snd_ctl_elem_value *ucontrol)
3612 struct usb_mixer_elem_info *elem = kctl->private_data;
3613 struct usb_mixer_interface *mixer = elem->head.mixer;
3614 struct scarlett2_data *private = mixer->private_data;
3616 int index = elem->control;
3619 mutex_lock(&private->data_mutex);
3621 if (private->hwdep_in_use) {
3626 err = scarlett2_check_put_during_autogain(mixer);
3630 oval = private->safe_switch[index];
3631 val = !!ucontrol->value.integer.value[0];
3636 private->safe_switch[index] = val;
3638 /* Send switch change to the device */
3639 err = scarlett2_usb_set_config(mixer, SCARLETT2_CONFIG_SAFE_SWITCH,
3645 mutex_unlock(&private->data_mutex);
3649 static const struct snd_kcontrol_new scarlett2_safe_ctl = {
3650 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3652 .info = scarlett2_autogain_disables_ctl_info,
3653 .get = scarlett2_safe_ctl_get,
3654 .put = scarlett2_safe_ctl_put,
3657 /*** PCM Input Control ***/
3659 static int scarlett2_update_pcm_input_switch(struct usb_mixer_interface *mixer)
3661 struct scarlett2_data *private = mixer->private_data;
3664 private->pcm_input_switch_updated = 0;
3666 err = scarlett2_usb_get_config(
3667 mixer, SCARLETT2_CONFIG_PCM_INPUT_SWITCH,
3668 1, &private->pcm_input_switch);
3675 static int scarlett2_pcm_input_switch_ctl_get(
3676 struct snd_kcontrol *kctl, struct snd_ctl_elem_value *ucontrol)
3678 struct usb_mixer_elem_info *elem = kctl->private_data;
3679 struct usb_mixer_interface *mixer = elem->head.mixer;
3680 struct scarlett2_data *private = elem->head.mixer->private_data;
3683 mutex_lock(&private->data_mutex);
3685 if (private->pcm_input_switch_updated) {
3686 err = scarlett2_update_pcm_input_switch(mixer);
3690 ucontrol->value.enumerated.item[0] = private->pcm_input_switch;
3693 mutex_unlock(&private->data_mutex);
3697 static int scarlett2_pcm_input_switch_ctl_put(
3698 struct snd_kcontrol *kctl, struct snd_ctl_elem_value *ucontrol)
3700 struct usb_mixer_elem_info *elem = kctl->private_data;
3701 struct usb_mixer_interface *mixer = elem->head.mixer;
3702 struct scarlett2_data *private = mixer->private_data;
3704 int oval, val, err = 0;
3706 mutex_lock(&private->data_mutex);
3708 if (private->hwdep_in_use) {
3713 oval = private->pcm_input_switch;
3714 val = !!ucontrol->value.integer.value[0];
3719 private->pcm_input_switch = val;
3721 /* Send switch change to the device */
3722 err = scarlett2_usb_set_config(
3723 mixer, SCARLETT2_CONFIG_PCM_INPUT_SWITCH,
3729 mutex_unlock(&private->data_mutex);
3733 static int scarlett2_pcm_input_switch_ctl_info(
3734 struct snd_kcontrol *kctl, struct snd_ctl_elem_info *uinfo)
3736 static const char *const values[2] = {
3740 return snd_ctl_enum_info(
3741 uinfo, 1, 2, values);
3744 static const struct snd_kcontrol_new scarlett2_pcm_input_switch_ctl = {
3745 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3747 .info = scarlett2_pcm_input_switch_ctl_info,
3748 .get = scarlett2_pcm_input_switch_ctl_get,
3749 .put = scarlett2_pcm_input_switch_ctl_put
3752 /*** Analogue Line Out Volume Controls ***/
3754 /* Update hardware volume controls after receiving notification that
3757 static int scarlett2_update_volumes(struct usb_mixer_interface *mixer)
3759 struct scarlett2_data *private = mixer->private_data;
3763 private->vol_updated = 0;
3765 if (scarlett2_has_config_item(private,
3766 SCARLETT2_CONFIG_MASTER_VOLUME)) {
3767 err = scarlett2_usb_get_config(
3768 mixer, SCARLETT2_CONFIG_MASTER_VOLUME,
3773 private->master_vol = clamp(vol + SCARLETT2_VOLUME_BIAS,
3774 0, SCARLETT2_VOLUME_BIAS);
3776 if (scarlett2_has_config_item(private,
3777 SCARLETT2_CONFIG_SW_HW_SWITCH))
3778 for (i = 0; i < private->num_line_out; i++)
3779 if (private->vol_sw_hw_switch[i])
3780 private->vol[i] = private->master_vol;
3783 if (scarlett2_has_config_item(private,
3784 SCARLETT2_CONFIG_HEADPHONE_VOLUME)) {
3785 err = scarlett2_usb_get_config(
3786 mixer, SCARLETT2_CONFIG_HEADPHONE_VOLUME,
3791 private->headphone_vol = clamp(vol + SCARLETT2_VOLUME_BIAS,
3792 0, SCARLETT2_VOLUME_BIAS);
3798 static int scarlett2_volume_ctl_info(struct snd_kcontrol *kctl,
3799 struct snd_ctl_elem_info *uinfo)
3801 struct usb_mixer_elem_info *elem = kctl->private_data;
3803 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
3804 uinfo->count = elem->channels;
3805 uinfo->value.integer.min = 0;
3806 uinfo->value.integer.max = SCARLETT2_VOLUME_BIAS;
3807 uinfo->value.integer.step = 1;
3811 static int scarlett2_master_volume_ctl_get(struct snd_kcontrol *kctl,
3812 struct snd_ctl_elem_value *ucontrol)
3814 struct usb_mixer_elem_info *elem = kctl->private_data;
3815 struct usb_mixer_interface *mixer = elem->head.mixer;
3816 struct scarlett2_data *private = mixer->private_data;
3819 mutex_lock(&private->data_mutex);
3821 if (private->hwdep_in_use) {
3826 if (private->vol_updated) {
3827 err = scarlett2_update_volumes(mixer);
3831 ucontrol->value.integer.value[0] = private->master_vol;
3834 mutex_unlock(&private->data_mutex);
3838 static int scarlett2_headphone_volume_ctl_get(
3839 struct snd_kcontrol *kctl,
3840 struct snd_ctl_elem_value *ucontrol)
3842 struct usb_mixer_elem_info *elem = kctl->private_data;
3843 struct usb_mixer_interface *mixer = elem->head.mixer;
3844 struct scarlett2_data *private = mixer->private_data;
3847 mutex_lock(&private->data_mutex);
3849 if (private->hwdep_in_use) {
3854 if (private->vol_updated) {
3855 err = scarlett2_update_volumes(mixer);
3859 ucontrol->value.integer.value[0] = private->headphone_vol;
3862 mutex_unlock(&private->data_mutex);
3866 static int line_out_remap(struct scarlett2_data *private, int index)
3868 const struct scarlett2_device_info *info = private->info;
3870 if (!info->line_out_remap_enable)
3873 if (index >= private->num_line_out)
3876 return info->line_out_remap[index];
3879 static int scarlett2_volume_ctl_get(struct snd_kcontrol *kctl,
3880 struct snd_ctl_elem_value *ucontrol)
3882 struct usb_mixer_elem_info *elem = kctl->private_data;
3883 struct usb_mixer_interface *mixer = elem->head.mixer;
3884 struct scarlett2_data *private = mixer->private_data;
3885 int index = line_out_remap(private, elem->control);
3888 mutex_lock(&private->data_mutex);
3890 if (private->hwdep_in_use) {
3895 if (private->vol_updated) {
3896 err = scarlett2_update_volumes(mixer);
3900 ucontrol->value.integer.value[0] = private->vol[index];
3903 mutex_unlock(&private->data_mutex);
3907 static int scarlett2_volume_ctl_put(struct snd_kcontrol *kctl,
3908 struct snd_ctl_elem_value *ucontrol)
3910 struct usb_mixer_elem_info *elem = kctl->private_data;
3911 struct usb_mixer_interface *mixer = elem->head.mixer;
3912 struct scarlett2_data *private = mixer->private_data;
3913 int index = line_out_remap(private, elem->control);
3914 int oval, val, err = 0;
3916 mutex_lock(&private->data_mutex);
3918 if (private->hwdep_in_use) {
3923 oval = private->vol[index];
3924 val = ucontrol->value.integer.value[0];
3929 private->vol[index] = val;
3930 err = scarlett2_usb_set_config(mixer, SCARLETT2_CONFIG_LINE_OUT_VOLUME,
3931 index, val - SCARLETT2_VOLUME_BIAS);
3936 mutex_unlock(&private->data_mutex);
3940 static const DECLARE_TLV_DB_MINMAX(
3941 db_scale_scarlett2_volume, -SCARLETT2_VOLUME_BIAS * 100, 0
3944 static const struct snd_kcontrol_new scarlett2_master_volume_ctl = {
3945 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3946 .access = SNDRV_CTL_ELEM_ACCESS_READ |
3947 SNDRV_CTL_ELEM_ACCESS_TLV_READ,
3949 .info = scarlett2_volume_ctl_info,
3950 .get = scarlett2_master_volume_ctl_get,
3951 .private_value = 0, /* max value */
3952 .tlv = { .p = db_scale_scarlett2_volume }
3955 static const struct snd_kcontrol_new scarlett2_headphone_volume_ctl = {
3956 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3957 .access = SNDRV_CTL_ELEM_ACCESS_READ |
3958 SNDRV_CTL_ELEM_ACCESS_TLV_READ,
3960 .info = scarlett2_volume_ctl_info,
3961 .get = scarlett2_headphone_volume_ctl_get,
3962 .private_value = 0, /* max value */
3963 .tlv = { .p = db_scale_scarlett2_volume }
3966 static const struct snd_kcontrol_new scarlett2_line_out_volume_ctl = {
3967 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3968 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE |
3969 SNDRV_CTL_ELEM_ACCESS_TLV_READ,
3971 .info = scarlett2_volume_ctl_info,
3972 .get = scarlett2_volume_ctl_get,
3973 .put = scarlett2_volume_ctl_put,
3974 .private_value = 0, /* max value */
3975 .tlv = { .p = db_scale_scarlett2_volume }
3978 /*** Mute Switch Controls ***/
3980 static int scarlett2_update_dim_mute(struct usb_mixer_interface *mixer)
3982 struct scarlett2_data *private = mixer->private_data;
3986 private->dim_mute_updated = 0;
3988 if (!scarlett2_has_config_item(private, SCARLETT2_CONFIG_SW_HW_SWITCH))
3991 err = scarlett2_usb_get_config(
3992 mixer, SCARLETT2_CONFIG_DIM_MUTE,
3993 SCARLETT2_DIM_MUTE_COUNT, private->dim_mute);
3997 for (i = 0; i < SCARLETT2_DIM_MUTE_COUNT; i++)
3998 private->dim_mute[i] = !!private->dim_mute[i];
4000 mute = private->dim_mute[SCARLETT2_BUTTON_MUTE];
4002 for (i = 0; i < private->num_line_out; i++)
4003 if (private->vol_sw_hw_switch[i])
4004 private->mute_switch[i] = mute;
4009 static int scarlett2_mute_ctl_get(struct snd_kcontrol *kctl,
4010 struct snd_ctl_elem_value *ucontrol)
4012 struct usb_mixer_elem_info *elem = kctl->private_data;
4013 struct usb_mixer_interface *mixer = elem->head.mixer;
4014 struct scarlett2_data *private = mixer->private_data;
4015 int index = line_out_remap(private, elem->control);
4018 mutex_lock(&private->data_mutex);
4020 if (private->hwdep_in_use) {
4025 if (private->dim_mute_updated) {
4026 err = scarlett2_update_dim_mute(mixer);
4030 ucontrol->value.integer.value[0] = private->mute_switch[index];
4033 mutex_unlock(&private->data_mutex);
4037 static int scarlett2_mute_ctl_put(struct snd_kcontrol *kctl,
4038 struct snd_ctl_elem_value *ucontrol)
4040 struct usb_mixer_elem_info *elem = kctl->private_data;
4041 struct usb_mixer_interface *mixer = elem->head.mixer;
4042 struct scarlett2_data *private = mixer->private_data;
4043 int index = line_out_remap(private, elem->control);
4044 int oval, val, err = 0;
4046 mutex_lock(&private->data_mutex);
4048 if (private->hwdep_in_use) {
4053 oval = private->mute_switch[index];
4054 val = !!ucontrol->value.integer.value[0];
4059 private->mute_switch[index] = val;
4061 /* Send mute change to the device */
4062 err = scarlett2_usb_set_config(mixer, SCARLETT2_CONFIG_MUTE_SWITCH,
4068 mutex_unlock(&private->data_mutex);
4072 static const struct snd_kcontrol_new scarlett2_mute_ctl = {
4073 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
4075 .info = snd_ctl_boolean_mono_info,
4076 .get = scarlett2_mute_ctl_get,
4077 .put = scarlett2_mute_ctl_put,
4080 /*** HW/SW Volume Switch Controls ***/
4082 static void scarlett2_sw_hw_ctl_ro(struct scarlett2_data *private, int index)
4084 private->sw_hw_ctls[index]->vd[0].access &=
4085 ~SNDRV_CTL_ELEM_ACCESS_WRITE;
4088 static void scarlett2_sw_hw_ctl_rw(struct scarlett2_data *private, int index)
4090 private->sw_hw_ctls[index]->vd[0].access |=
4091 SNDRV_CTL_ELEM_ACCESS_WRITE;
4094 static int scarlett2_sw_hw_enum_ctl_info(struct snd_kcontrol *kctl,
4095 struct snd_ctl_elem_info *uinfo)
4097 static const char *const values[2] = {
4101 return snd_ctl_enum_info(uinfo, 1, 2, values);
4104 static int scarlett2_sw_hw_enum_ctl_get(struct snd_kcontrol *kctl,
4105 struct snd_ctl_elem_value *ucontrol)
4107 struct usb_mixer_elem_info *elem = kctl->private_data;
4108 struct scarlett2_data *private = elem->head.mixer->private_data;
4109 int index = line_out_remap(private, elem->control);
4111 ucontrol->value.enumerated.item[0] = private->vol_sw_hw_switch[index];
4115 static void scarlett2_vol_ctl_set_writable(struct usb_mixer_interface *mixer,
4116 int index, int value)
4118 struct scarlett2_data *private = mixer->private_data;
4119 struct snd_card *card = mixer->chip->card;
4121 /* Set/Clear write bits */
4123 private->vol_ctls[index]->vd[0].access |=
4124 SNDRV_CTL_ELEM_ACCESS_WRITE;
4125 private->mute_ctls[index]->vd[0].access |=
4126 SNDRV_CTL_ELEM_ACCESS_WRITE;
4128 private->vol_ctls[index]->vd[0].access &=
4129 ~SNDRV_CTL_ELEM_ACCESS_WRITE;
4130 private->mute_ctls[index]->vd[0].access &=
4131 ~SNDRV_CTL_ELEM_ACCESS_WRITE;
4134 /* Notify of write bit and possible value change */
4135 snd_ctl_notify(card,
4136 SNDRV_CTL_EVENT_MASK_VALUE | SNDRV_CTL_EVENT_MASK_INFO,
4137 &private->vol_ctls[index]->id);
4138 snd_ctl_notify(card,
4139 SNDRV_CTL_EVENT_MASK_VALUE | SNDRV_CTL_EVENT_MASK_INFO,
4140 &private->mute_ctls[index]->id);
4143 static int scarlett2_sw_hw_change(struct usb_mixer_interface *mixer,
4144 int ctl_index, int val)
4146 struct scarlett2_data *private = mixer->private_data;
4147 int index = line_out_remap(private, ctl_index);
4150 private->vol_sw_hw_switch[index] = val;
4152 /* Change access mode to RO (hardware controlled volume)
4153 * or RW (software controlled volume)
4155 scarlett2_vol_ctl_set_writable(mixer, ctl_index, !val);
4157 /* Reset volume/mute to master volume/mute */
4158 private->vol[index] = private->master_vol;
4159 private->mute_switch[index] = private->dim_mute[SCARLETT2_BUTTON_MUTE];
4161 /* Set SW volume to current HW volume */
4162 err = scarlett2_usb_set_config(
4163 mixer, SCARLETT2_CONFIG_LINE_OUT_VOLUME,
4164 index, private->master_vol - SCARLETT2_VOLUME_BIAS);
4168 /* Set SW mute to current HW mute */
4169 err = scarlett2_usb_set_config(
4170 mixer, SCARLETT2_CONFIG_MUTE_SWITCH,
4171 index, private->dim_mute[SCARLETT2_BUTTON_MUTE]);
4175 /* Send SW/HW switch change to the device */
4176 return scarlett2_usb_set_config(mixer, SCARLETT2_CONFIG_SW_HW_SWITCH,
4180 static int scarlett2_sw_hw_enum_ctl_put(struct snd_kcontrol *kctl,
4181 struct snd_ctl_elem_value *ucontrol)
4183 struct usb_mixer_elem_info *elem = kctl->private_data;
4184 struct usb_mixer_interface *mixer = elem->head.mixer;
4185 struct scarlett2_data *private = mixer->private_data;
4186 int ctl_index = elem->control;
4187 int index = line_out_remap(private, ctl_index);
4188 int oval, val, err = 0;
4190 mutex_lock(&private->data_mutex);
4192 if (private->hwdep_in_use) {
4197 oval = private->vol_sw_hw_switch[index];
4198 val = !!ucontrol->value.enumerated.item[0];
4203 err = scarlett2_sw_hw_change(mixer, ctl_index, val);
4208 mutex_unlock(&private->data_mutex);
4212 static const struct snd_kcontrol_new scarlett2_sw_hw_enum_ctl = {
4213 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
4215 .info = scarlett2_sw_hw_enum_ctl_info,
4216 .get = scarlett2_sw_hw_enum_ctl_get,
4217 .put = scarlett2_sw_hw_enum_ctl_put,
4220 /*** Line Level/Instrument Level Switch Controls ***/
4222 static int scarlett2_update_input_level(struct usb_mixer_interface *mixer)
4224 struct scarlett2_data *private = mixer->private_data;
4225 const struct scarlett2_device_info *info = private->info;
4227 private->input_level_updated = 0;
4229 if (!info->level_input_count)
4232 return scarlett2_usb_get_config(
4233 mixer, SCARLETT2_CONFIG_LEVEL_SWITCH,
4234 info->level_input_count + info->level_input_first,
4235 private->level_switch);
4238 static int scarlett2_level_enum_ctl_info(struct snd_kcontrol *kctl,
4239 struct snd_ctl_elem_info *uinfo)
4241 static const char *const values[2] = {
4244 struct usb_mixer_elem_info *elem = kctl->private_data;
4245 struct usb_mixer_interface *mixer = elem->head.mixer;
4246 struct scarlett2_data *private = mixer->private_data;
4249 mutex_lock(&private->data_mutex);
4251 if (private->hwdep_in_use) {
4256 err = scarlett2_check_autogain_updated(mixer);
4260 err = snd_ctl_enum_info(uinfo, 1, 2, values);
4263 mutex_unlock(&private->data_mutex);
4267 static int scarlett2_level_enum_ctl_get(struct snd_kcontrol *kctl,
4268 struct snd_ctl_elem_value *ucontrol)
4270 struct usb_mixer_elem_info *elem = kctl->private_data;
4271 struct usb_mixer_interface *mixer = elem->head.mixer;
4272 struct scarlett2_data *private = mixer->private_data;
4273 const struct scarlett2_device_info *info = private->info;
4275 int index = elem->control + info->level_input_first;
4278 mutex_lock(&private->data_mutex);
4280 if (private->hwdep_in_use) {
4285 if (private->input_level_updated) {
4286 err = scarlett2_update_input_level(mixer);
4290 ucontrol->value.enumerated.item[0] = scarlett2_decode_muteable(
4291 private->level_switch[index]);
4294 mutex_unlock(&private->data_mutex);
4298 static int scarlett2_level_enum_ctl_put(struct snd_kcontrol *kctl,
4299 struct snd_ctl_elem_value *ucontrol)
4301 struct usb_mixer_elem_info *elem = kctl->private_data;
4302 struct usb_mixer_interface *mixer = elem->head.mixer;
4303 struct scarlett2_data *private = mixer->private_data;
4304 const struct scarlett2_device_info *info = private->info;
4306 int index = elem->control + info->level_input_first;
4309 mutex_lock(&private->data_mutex);
4311 if (private->hwdep_in_use) {
4316 err = scarlett2_check_put_during_autogain(mixer);
4320 oval = private->level_switch[index];
4321 val = !!ucontrol->value.enumerated.item[0];
4326 private->level_switch[index] = val;
4328 /* To set the Gen 4 muteable controls, bit 1 gets set instead */
4329 if (private->config_set->items[SCARLETT2_CONFIG_LEVEL_SWITCH].mute)
4330 val = (!val) | 0x02;
4332 /* Send switch change to the device */
4333 err = scarlett2_usb_set_config(mixer, SCARLETT2_CONFIG_LEVEL_SWITCH,
4339 mutex_unlock(&private->data_mutex);
4343 static const struct snd_kcontrol_new scarlett2_level_enum_ctl = {
4344 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
4346 .info = scarlett2_level_enum_ctl_info,
4347 .get = scarlett2_level_enum_ctl_get,
4348 .put = scarlett2_level_enum_ctl_put,
4351 /*** Pad Switch Controls ***/
4353 static int scarlett2_update_input_pad(struct usb_mixer_interface *mixer)
4355 struct scarlett2_data *private = mixer->private_data;
4356 const struct scarlett2_device_info *info = private->info;
4358 private->input_pad_updated = 0;
4360 if (!info->pad_input_count)
4363 return scarlett2_usb_get_config(
4364 mixer, SCARLETT2_CONFIG_PAD_SWITCH,
4365 info->pad_input_count, private->pad_switch);
4368 static int scarlett2_pad_ctl_get(struct snd_kcontrol *kctl,
4369 struct snd_ctl_elem_value *ucontrol)
4371 struct usb_mixer_elem_info *elem = kctl->private_data;
4372 struct usb_mixer_interface *mixer = elem->head.mixer;
4373 struct scarlett2_data *private = mixer->private_data;
4376 mutex_lock(&private->data_mutex);
4378 if (private->hwdep_in_use) {
4383 if (private->input_pad_updated) {
4384 err = scarlett2_update_input_pad(mixer);
4388 ucontrol->value.integer.value[0] =
4389 private->pad_switch[elem->control];
4392 mutex_unlock(&private->data_mutex);
4396 static int scarlett2_pad_ctl_put(struct snd_kcontrol *kctl,
4397 struct snd_ctl_elem_value *ucontrol)
4399 struct usb_mixer_elem_info *elem = kctl->private_data;
4400 struct usb_mixer_interface *mixer = elem->head.mixer;
4401 struct scarlett2_data *private = mixer->private_data;
4403 int index = elem->control;
4404 int oval, val, err = 0;
4406 mutex_lock(&private->data_mutex);
4408 if (private->hwdep_in_use) {
4413 oval = private->pad_switch[index];
4414 val = !!ucontrol->value.integer.value[0];
4419 private->pad_switch[index] = val;
4421 /* Send switch change to the device */
4422 err = scarlett2_usb_set_config(mixer, SCARLETT2_CONFIG_PAD_SWITCH,
4428 mutex_unlock(&private->data_mutex);
4432 static const struct snd_kcontrol_new scarlett2_pad_ctl = {
4433 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
4435 .info = snd_ctl_boolean_mono_info,
4436 .get = scarlett2_pad_ctl_get,
4437 .put = scarlett2_pad_ctl_put,
4440 /*** Air Switch Controls ***/
4442 static int scarlett2_update_input_air(struct usb_mixer_interface *mixer)
4444 struct scarlett2_data *private = mixer->private_data;
4445 const struct scarlett2_device_info *info = private->info;
4447 private->input_air_updated = 0;
4449 if (!info->air_input_count)
4452 return scarlett2_usb_get_config(
4453 mixer, SCARLETT2_CONFIG_AIR_SWITCH,
4454 info->air_input_count, private->air_switch);
4457 static int scarlett2_air_ctl_get(struct snd_kcontrol *kctl,
4458 struct snd_ctl_elem_value *ucontrol)
4460 struct usb_mixer_elem_info *elem = kctl->private_data;
4461 struct usb_mixer_interface *mixer = elem->head.mixer;
4462 struct scarlett2_data *private = mixer->private_data;
4465 mutex_lock(&private->data_mutex);
4467 if (private->hwdep_in_use) {
4472 if (private->input_air_updated) {
4473 err = scarlett2_update_input_air(mixer);
4477 ucontrol->value.integer.value[0] = private->air_switch[elem->control];
4480 mutex_unlock(&private->data_mutex);
4484 static int scarlett2_air_ctl_put(struct snd_kcontrol *kctl,
4485 struct snd_ctl_elem_value *ucontrol)
4487 struct usb_mixer_elem_info *elem = kctl->private_data;
4488 struct usb_mixer_interface *mixer = elem->head.mixer;
4489 struct scarlett2_data *private = mixer->private_data;
4491 int index = elem->control;
4494 mutex_lock(&private->data_mutex);
4496 if (private->hwdep_in_use) {
4501 err = scarlett2_check_put_during_autogain(mixer);
4505 oval = private->air_switch[index];
4506 val = ucontrol->value.integer.value[0];
4511 private->air_switch[index] = val;
4513 /* Send switch change to the device */
4514 err = scarlett2_usb_set_config(mixer, SCARLETT2_CONFIG_AIR_SWITCH,
4520 mutex_unlock(&private->data_mutex);
4524 static int scarlett2_air_with_drive_ctl_info(
4525 struct snd_kcontrol *kctl, struct snd_ctl_elem_info *uinfo)
4527 static const char *const values[3] = {
4528 "Off", "Presence", "Presence + Drive"
4530 struct usb_mixer_elem_info *elem = kctl->private_data;
4531 struct usb_mixer_interface *mixer = elem->head.mixer;
4532 struct scarlett2_data *private = mixer->private_data;
4535 mutex_lock(&private->data_mutex);
4537 if (private->hwdep_in_use) {
4542 err = scarlett2_check_autogain_updated(mixer);
4546 err = snd_ctl_enum_info(uinfo, 1, 3, values);
4549 mutex_unlock(&private->data_mutex);
4553 static const struct snd_kcontrol_new scarlett2_air_ctl[2] = {
4555 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
4557 .info = snd_ctl_boolean_mono_info,
4558 .get = scarlett2_air_ctl_get,
4559 .put = scarlett2_air_ctl_put,
4562 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
4564 .info = scarlett2_air_with_drive_ctl_info,
4565 .get = scarlett2_air_ctl_get,
4566 .put = scarlett2_air_ctl_put,
4570 /*** Phantom Switch Controls ***/
4572 static int scarlett2_update_input_phantom(struct usb_mixer_interface *mixer)
4574 struct scarlett2_data *private = mixer->private_data;
4575 const struct scarlett2_device_info *info = private->info;
4578 private->input_phantom_updated = 0;
4580 if (!info->phantom_count)
4583 err = scarlett2_usb_get_config(
4584 mixer, SCARLETT2_CONFIG_PHANTOM_SWITCH,
4585 info->phantom_count, private->phantom_switch);
4589 if (scarlett2_has_config_item(private,
4590 SCARLETT2_CONFIG_PHANTOM_PERSISTENCE)) {
4591 err = scarlett2_usb_get_config(
4592 mixer, SCARLETT2_CONFIG_PHANTOM_PERSISTENCE,
4593 1, &private->phantom_persistence);
4601 /* Check if phantom power on the given input is currently changing state */
4602 static int scarlett2_phantom_is_switching(
4603 struct scarlett2_data *private, int line_num)
4605 const struct scarlett2_device_info *info = private->info;
4606 int index = line_num / info->inputs_per_phantom;
4608 return !!(private->phantom_switch[index] & 0x02);
4611 /* Update autogain controls' access mode when phantom power changes state */
4612 static void scarlett2_phantom_update_access(struct usb_mixer_interface *mixer)
4614 struct scarlett2_data *private = mixer->private_data;
4615 const struct scarlett2_device_info *info = private->info;
4618 /* Disable autogain controls if phantom power is changing state */
4619 for (i = 0; i < info->gain_input_count; i++) {
4620 int val = !scarlett2_phantom_is_switching(private, i);
4622 scarlett2_set_ctl_access(private->autogain_ctls[i], val);
4626 /* Notify of access mode change for autogain which can't be enabled
4627 * while phantom power is changing.
4629 static void scarlett2_phantom_notify_access(struct usb_mixer_interface *mixer)
4631 struct snd_card *card = mixer->chip->card;
4632 struct scarlett2_data *private = mixer->private_data;
4633 const struct scarlett2_device_info *info = private->info;
4636 for (i = 0; i < info->gain_input_count; i++)
4637 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_INFO,
4638 &private->autogain_ctls[i]->id);
4641 /* Call scarlett2_update_input_phantom() and
4642 * scarlett2_phantom_update_access() if input_phantom_updated is set.
4644 static int scarlett2_check_input_phantom_updated(
4645 struct usb_mixer_interface *mixer)
4647 struct scarlett2_data *private = mixer->private_data;
4650 if (!private->input_phantom_updated)
4653 err = scarlett2_update_input_phantom(mixer);
4657 scarlett2_phantom_update_access(mixer);
4662 static int scarlett2_phantom_ctl_get(struct snd_kcontrol *kctl,
4663 struct snd_ctl_elem_value *ucontrol)
4665 struct usb_mixer_elem_info *elem = kctl->private_data;
4666 struct usb_mixer_interface *mixer = elem->head.mixer;
4667 struct scarlett2_data *private = mixer->private_data;
4670 mutex_lock(&private->data_mutex);
4672 if (private->hwdep_in_use) {
4677 err = scarlett2_check_input_phantom_updated(mixer);
4681 ucontrol->value.integer.value[0] = scarlett2_decode_muteable(
4682 private->phantom_switch[elem->control]);
4685 mutex_unlock(&private->data_mutex);
4689 static int scarlett2_phantom_ctl_put(struct snd_kcontrol *kctl,
4690 struct snd_ctl_elem_value *ucontrol)
4692 struct usb_mixer_elem_info *elem = kctl->private_data;
4693 struct usb_mixer_interface *mixer = elem->head.mixer;
4694 struct scarlett2_data *private = mixer->private_data;
4695 const struct scarlett2_device_info *info = private->info;
4697 int index = elem->control;
4700 mutex_lock(&private->data_mutex);
4702 if (private->hwdep_in_use) {
4707 err = scarlett2_check_put_during_autogain(mixer);
4711 oval = private->phantom_switch[index];
4712 val = !!ucontrol->value.integer.value[0];
4717 private->phantom_switch[index] = val;
4719 /* To set the Gen 4 muteable controls, bit 1 gets set */
4720 if (private->config_set->items[SCARLETT2_CONFIG_PHANTOM_SWITCH].mute)
4721 val = (!val) | 0x02;
4723 /* Send switch change to the device */
4724 err = scarlett2_usb_set_config(mixer, SCARLETT2_CONFIG_PHANTOM_SWITCH,
4725 index + info->phantom_first, val);
4729 scarlett2_phantom_update_access(mixer);
4730 scarlett2_phantom_notify_access(mixer);
4733 mutex_unlock(&private->data_mutex);
4737 static const struct snd_kcontrol_new scarlett2_phantom_ctl = {
4738 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
4740 .info = scarlett2_autogain_disables_ctl_info,
4741 .get = scarlett2_phantom_ctl_get,
4742 .put = scarlett2_phantom_ctl_put,
4745 /*** Phantom Persistence Control ***/
4747 static int scarlett2_phantom_persistence_ctl_get(
4748 struct snd_kcontrol *kctl, struct snd_ctl_elem_value *ucontrol)
4750 struct usb_mixer_elem_info *elem = kctl->private_data;
4751 struct scarlett2_data *private = elem->head.mixer->private_data;
4753 ucontrol->value.integer.value[0] = private->phantom_persistence;
4757 static int scarlett2_phantom_persistence_ctl_put(
4758 struct snd_kcontrol *kctl, struct snd_ctl_elem_value *ucontrol)
4760 struct usb_mixer_elem_info *elem = kctl->private_data;
4761 struct usb_mixer_interface *mixer = elem->head.mixer;
4762 struct scarlett2_data *private = mixer->private_data;
4764 int index = elem->control;
4765 int oval, val, err = 0;
4767 mutex_lock(&private->data_mutex);
4769 if (private->hwdep_in_use) {
4774 oval = private->phantom_persistence;
4775 val = !!ucontrol->value.integer.value[0];
4780 private->phantom_persistence = val;
4782 /* Send switch change to the device */
4783 err = scarlett2_usb_set_config(
4784 mixer, SCARLETT2_CONFIG_PHANTOM_PERSISTENCE, index, val);
4789 mutex_unlock(&private->data_mutex);
4793 static const struct snd_kcontrol_new scarlett2_phantom_persistence_ctl = {
4794 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
4796 .info = snd_ctl_boolean_mono_info,
4797 .get = scarlett2_phantom_persistence_ctl_get,
4798 .put = scarlett2_phantom_persistence_ctl_put,
4801 /*** Speaker Switching Control ***/
4803 static int scarlett2_update_monitor_other(struct usb_mixer_interface *mixer)
4805 struct scarlett2_data *private = mixer->private_data;
4806 const struct scarlett2_device_info *info = private->info;
4809 /* monitor_other_enable[0] enables speaker switching
4810 * monitor_other_enable[1] enables talkback
4812 u8 monitor_other_enable[2];
4814 /* monitor_other_switch[0] activates the alternate speakers
4815 * monitor_other_switch[1] activates talkback
4817 u8 monitor_other_switch[2];
4819 private->monitor_other_updated = 0;
4821 /* if it doesn't do speaker switching then it also doesn't do
4824 if (!info->has_speaker_switching)
4827 err = scarlett2_usb_get_config(
4828 mixer, SCARLETT2_CONFIG_MONITOR_OTHER_ENABLE,
4829 2, monitor_other_enable);
4833 err = scarlett2_usb_get_config(
4834 mixer, SCARLETT2_CONFIG_MONITOR_OTHER_SWITCH,
4835 2, monitor_other_switch);
4839 if (!monitor_other_enable[0])
4840 private->speaker_switching_switch = 0;
4842 private->speaker_switching_switch = monitor_other_switch[0] + 1;
4844 if (info->has_talkback) {
4848 if (!monitor_other_enable[1])
4849 private->talkback_switch = 0;
4851 private->talkback_switch = monitor_other_switch[1] + 1;
4853 err = scarlett2_usb_get_config(mixer,
4854 SCARLETT2_CONFIG_TALKBACK_MAP,
4858 for (i = 0; i < private->num_mix_out; i++, bitmap >>= 1)
4859 private->talkback_map[i] = bitmap & 1;
4865 static int scarlett2_speaker_switch_enum_ctl_info(
4866 struct snd_kcontrol *kctl, struct snd_ctl_elem_info *uinfo)
4868 static const char *const values[3] = {
4869 "Off", "Main", "Alt"
4872 return snd_ctl_enum_info(uinfo, 1, 3, values);
4875 static int scarlett2_speaker_switch_enum_ctl_get(
4876 struct snd_kcontrol *kctl, struct snd_ctl_elem_value *ucontrol)
4878 struct usb_mixer_elem_info *elem = kctl->private_data;
4879 struct usb_mixer_interface *mixer = elem->head.mixer;
4880 struct scarlett2_data *private = mixer->private_data;
4883 mutex_lock(&private->data_mutex);
4885 if (private->hwdep_in_use) {
4890 if (private->monitor_other_updated) {
4891 err = scarlett2_update_monitor_other(mixer);
4895 ucontrol->value.enumerated.item[0] = private->speaker_switching_switch;
4898 mutex_unlock(&private->data_mutex);
4902 /* when speaker switching gets enabled, switch the main/alt speakers
4903 * to HW volume and disable those controls
4905 static int scarlett2_speaker_switch_enable(struct usb_mixer_interface *mixer)
4907 struct snd_card *card = mixer->chip->card;
4908 struct scarlett2_data *private = mixer->private_data;
4911 for (i = 0; i < 4; i++) {
4912 int index = line_out_remap(private, i);
4914 /* switch the main/alt speakers to HW volume */
4915 if (!private->vol_sw_hw_switch[index]) {
4916 err = scarlett2_sw_hw_change(private->mixer, i, 1);
4921 /* disable the line out SW/HW switch */
4922 scarlett2_sw_hw_ctl_ro(private, i);
4923 snd_ctl_notify(card,
4924 SNDRV_CTL_EVENT_MASK_VALUE |
4925 SNDRV_CTL_EVENT_MASK_INFO,
4926 &private->sw_hw_ctls[i]->id);
4929 /* when the next monitor-other notify comes in, update the mux
4932 private->speaker_switching_switched = 1;
4937 /* when speaker switching gets disabled, reenable the hw/sw controls
4938 * and invalidate the routing
4940 static void scarlett2_speaker_switch_disable(struct usb_mixer_interface *mixer)
4942 struct snd_card *card = mixer->chip->card;
4943 struct scarlett2_data *private = mixer->private_data;
4946 /* enable the line out SW/HW switch */
4947 for (i = 0; i < 4; i++) {
4948 scarlett2_sw_hw_ctl_rw(private, i);
4949 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_INFO,
4950 &private->sw_hw_ctls[i]->id);
4953 /* when the next monitor-other notify comes in, update the mux
4956 private->speaker_switching_switched = 1;
4959 static int scarlett2_speaker_switch_enum_ctl_put(
4960 struct snd_kcontrol *kctl, struct snd_ctl_elem_value *ucontrol)
4962 struct usb_mixer_elem_info *elem = kctl->private_data;
4963 struct usb_mixer_interface *mixer = elem->head.mixer;
4964 struct scarlett2_data *private = mixer->private_data;
4966 int oval, val, err = 0;
4968 mutex_lock(&private->data_mutex);
4970 if (private->hwdep_in_use) {
4975 oval = private->speaker_switching_switch;
4976 val = min(ucontrol->value.enumerated.item[0], 2U);
4981 private->speaker_switching_switch = val;
4983 /* enable/disable speaker switching */
4984 err = scarlett2_usb_set_config(
4985 mixer, SCARLETT2_CONFIG_MONITOR_OTHER_ENABLE,
4990 /* if speaker switching is enabled, select main or alt */
4991 err = scarlett2_usb_set_config(
4992 mixer, SCARLETT2_CONFIG_MONITOR_OTHER_SWITCH,
4997 /* update controls if speaker switching gets enabled or disabled */
4999 err = scarlett2_speaker_switch_enable(mixer);
5000 else if (oval && !val)
5001 scarlett2_speaker_switch_disable(mixer);
5007 mutex_unlock(&private->data_mutex);
5011 static const struct snd_kcontrol_new scarlett2_speaker_switch_enum_ctl = {
5012 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
5014 .info = scarlett2_speaker_switch_enum_ctl_info,
5015 .get = scarlett2_speaker_switch_enum_ctl_get,
5016 .put = scarlett2_speaker_switch_enum_ctl_put,
5019 static int scarlett2_add_speaker_switch_ctl(struct usb_mixer_interface *mixer)
5021 struct scarlett2_data *private = mixer->private_data;
5022 const struct scarlett2_device_info *info = private->info;
5024 if (!info->has_speaker_switching)
5027 return scarlett2_add_new_ctl(
5028 mixer, &scarlett2_speaker_switch_enum_ctl,
5029 0, 1, "Speaker Switching Playback Enum",
5030 &private->speaker_switching_ctl);
5033 /*** Talkback and Talkback Map Controls ***/
5035 static int scarlett2_talkback_enum_ctl_info(
5036 struct snd_kcontrol *kctl, struct snd_ctl_elem_info *uinfo)
5038 static const char *const values[3] = {
5039 "Disabled", "Off", "On"
5042 return snd_ctl_enum_info(uinfo, 1, 3, values);
5045 static int scarlett2_talkback_enum_ctl_get(
5046 struct snd_kcontrol *kctl, struct snd_ctl_elem_value *ucontrol)
5048 struct usb_mixer_elem_info *elem = kctl->private_data;
5049 struct usb_mixer_interface *mixer = elem->head.mixer;
5050 struct scarlett2_data *private = mixer->private_data;
5053 mutex_lock(&private->data_mutex);
5055 if (private->hwdep_in_use) {
5060 if (private->monitor_other_updated) {
5061 err = scarlett2_update_monitor_other(mixer);
5065 ucontrol->value.enumerated.item[0] = private->talkback_switch;
5068 mutex_unlock(&private->data_mutex);
5072 static int scarlett2_talkback_enum_ctl_put(
5073 struct snd_kcontrol *kctl, struct snd_ctl_elem_value *ucontrol)
5075 struct usb_mixer_elem_info *elem = kctl->private_data;
5076 struct usb_mixer_interface *mixer = elem->head.mixer;
5077 struct scarlett2_data *private = mixer->private_data;
5079 int oval, val, err = 0;
5081 mutex_lock(&private->data_mutex);
5083 if (private->hwdep_in_use) {
5088 oval = private->talkback_switch;
5089 val = min(ucontrol->value.enumerated.item[0], 2U);
5094 private->talkback_switch = val;
5096 /* enable/disable talkback */
5097 err = scarlett2_usb_set_config(
5098 mixer, SCARLETT2_CONFIG_MONITOR_OTHER_ENABLE,
5103 /* if talkback is enabled, select main or alt */
5104 err = scarlett2_usb_set_config(
5105 mixer, SCARLETT2_CONFIG_MONITOR_OTHER_SWITCH,
5111 mutex_unlock(&private->data_mutex);
5115 static const struct snd_kcontrol_new scarlett2_talkback_enum_ctl = {
5116 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
5118 .info = scarlett2_talkback_enum_ctl_info,
5119 .get = scarlett2_talkback_enum_ctl_get,
5120 .put = scarlett2_talkback_enum_ctl_put,
5123 static int scarlett2_talkback_map_ctl_get(
5124 struct snd_kcontrol *kctl, struct snd_ctl_elem_value *ucontrol)
5126 struct usb_mixer_elem_info *elem = kctl->private_data;
5127 struct usb_mixer_interface *mixer = elem->head.mixer;
5128 struct scarlett2_data *private = mixer->private_data;
5129 int index = elem->control;
5131 ucontrol->value.integer.value[0] = private->talkback_map[index];
5136 static int scarlett2_talkback_map_ctl_put(
5137 struct snd_kcontrol *kctl, struct snd_ctl_elem_value *ucontrol)
5139 struct usb_mixer_elem_info *elem = kctl->private_data;
5140 struct usb_mixer_interface *mixer = elem->head.mixer;
5141 struct scarlett2_data *private = mixer->private_data;
5142 int index = elem->control;
5143 int oval, val, err = 0, i;
5146 mutex_lock(&private->data_mutex);
5148 if (private->hwdep_in_use) {
5153 oval = private->talkback_map[index];
5154 val = !!ucontrol->value.integer.value[0];
5159 private->talkback_map[index] = val;
5161 for (i = 0; i < private->num_mix_out; i++)
5162 bitmap |= private->talkback_map[i] << i;
5164 /* Send updated bitmap to the device */
5165 err = scarlett2_usb_set_config(mixer, SCARLETT2_CONFIG_TALKBACK_MAP,
5171 mutex_unlock(&private->data_mutex);
5175 static const struct snd_kcontrol_new scarlett2_talkback_map_ctl = {
5176 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
5178 .info = snd_ctl_boolean_mono_info,
5179 .get = scarlett2_talkback_map_ctl_get,
5180 .put = scarlett2_talkback_map_ctl_put,
5183 static int scarlett2_add_talkback_ctls(struct usb_mixer_interface *mixer)
5185 struct scarlett2_data *private = mixer->private_data;
5186 const struct scarlett2_device_info *info = private->info;
5188 char s[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
5190 if (!info->has_talkback)
5193 err = scarlett2_add_new_ctl(
5194 mixer, &scarlett2_talkback_enum_ctl,
5195 0, 1, "Talkback Playback Enum",
5196 &private->talkback_ctl);
5200 for (i = 0; i < private->num_mix_out; i++) {
5201 snprintf(s, sizeof(s),
5202 "Talkback Mix %c Playback Switch", i + 'A');
5203 err = scarlett2_add_new_ctl(mixer, &scarlett2_talkback_map_ctl,
5212 /*** Dim/Mute Controls ***/
5214 static int scarlett2_dim_mute_ctl_get(struct snd_kcontrol *kctl,
5215 struct snd_ctl_elem_value *ucontrol)
5217 struct usb_mixer_elem_info *elem = kctl->private_data;
5218 struct usb_mixer_interface *mixer = elem->head.mixer;
5219 struct scarlett2_data *private = mixer->private_data;
5222 mutex_lock(&private->data_mutex);
5224 if (private->hwdep_in_use) {
5229 if (private->dim_mute_updated) {
5230 err = scarlett2_update_dim_mute(mixer);
5234 ucontrol->value.integer.value[0] = private->dim_mute[elem->control];
5237 mutex_unlock(&private->data_mutex);
5241 static int scarlett2_dim_mute_ctl_put(struct snd_kcontrol *kctl,
5242 struct snd_ctl_elem_value *ucontrol)
5244 struct usb_mixer_elem_info *elem = kctl->private_data;
5245 struct usb_mixer_interface *mixer = elem->head.mixer;
5246 struct scarlett2_data *private = mixer->private_data;
5247 int index = elem->control;
5248 int oval, val, err = 0, i;
5250 mutex_lock(&private->data_mutex);
5252 if (private->hwdep_in_use) {
5257 oval = private->dim_mute[index];
5258 val = !!ucontrol->value.integer.value[0];
5263 private->dim_mute[index] = val;
5265 /* Send switch change to the device */
5266 err = scarlett2_usb_set_config(mixer, SCARLETT2_CONFIG_DIM_MUTE,
5271 if (index == SCARLETT2_BUTTON_MUTE)
5272 for (i = 0; i < private->num_line_out; i++) {
5273 int line_index = line_out_remap(private, i);
5275 if (private->vol_sw_hw_switch[line_index]) {
5276 private->mute_switch[line_index] = val;
5277 snd_ctl_notify(mixer->chip->card,
5278 SNDRV_CTL_EVENT_MASK_VALUE,
5279 &private->mute_ctls[i]->id);
5284 mutex_unlock(&private->data_mutex);
5288 static const struct snd_kcontrol_new scarlett2_dim_mute_ctl = {
5289 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
5291 .info = snd_ctl_boolean_mono_info,
5292 .get = scarlett2_dim_mute_ctl_get,
5293 .put = scarlett2_dim_mute_ctl_put
5296 /*** Create the analogue output controls ***/
5298 static int scarlett2_add_line_out_ctls(struct usb_mixer_interface *mixer)
5300 struct scarlett2_data *private = mixer->private_data;
5301 const struct scarlett2_device_info *info = private->info;
5303 char s[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
5305 /* Add R/O HW volume control */
5306 if (scarlett2_has_config_item(private,
5307 SCARLETT2_CONFIG_MASTER_VOLUME)) {
5308 snprintf(s, sizeof(s), "Master HW Playback Volume");
5309 err = scarlett2_add_new_ctl(mixer,
5310 &scarlett2_master_volume_ctl,
5311 0, 1, s, &private->master_vol_ctl);
5316 /* Add R/O headphone volume control */
5317 if (scarlett2_has_config_item(private,
5318 SCARLETT2_CONFIG_HEADPHONE_VOLUME)) {
5319 snprintf(s, sizeof(s), "Headphone Playback Volume");
5320 err = scarlett2_add_new_ctl(mixer,
5321 &scarlett2_headphone_volume_ctl,
5323 &private->headphone_vol_ctl);
5328 /* Remaining controls are only applicable if the device
5329 * has per-channel line-out volume controls.
5331 if (!scarlett2_has_config_item(private,
5332 SCARLETT2_CONFIG_LINE_OUT_VOLUME))
5335 /* Add volume controls */
5336 for (i = 0; i < private->num_line_out; i++) {
5337 int index = line_out_remap(private, i);
5340 if (info->line_out_descrs[i])
5341 snprintf(s, sizeof(s),
5342 "Line %02d (%s) Playback Volume",
5343 i + 1, info->line_out_descrs[i]);
5345 snprintf(s, sizeof(s),
5346 "Line %02d Playback Volume",
5348 err = scarlett2_add_new_ctl(mixer,
5349 &scarlett2_line_out_volume_ctl,
5350 i, 1, s, &private->vol_ctls[i]);
5355 snprintf(s, sizeof(s),
5356 "Line %02d Mute Playback Switch",
5358 err = scarlett2_add_new_ctl(mixer,
5359 &scarlett2_mute_ctl,
5361 &private->mute_ctls[i]);
5366 if (scarlett2_has_config_item(private,
5367 SCARLETT2_CONFIG_SW_HW_SWITCH)) {
5369 /* Make the fader and mute controls read-only if the
5370 * SW/HW switch is set to HW
5372 if (private->vol_sw_hw_switch[index])
5373 scarlett2_vol_ctl_set_writable(mixer, i, 0);
5375 scnprintf(s, sizeof(s),
5376 "Line Out %02d Volume Control Playback Enum",
5378 err = scarlett2_add_new_ctl(mixer,
5379 &scarlett2_sw_hw_enum_ctl,
5381 &private->sw_hw_ctls[i]);
5385 /* Make the switch read-only if the line is
5386 * involved in speaker switching
5388 if (private->speaker_switching_switch && i < 4)
5389 scarlett2_sw_hw_ctl_ro(private, i);
5393 /* Add dim/mute controls */
5394 if (scarlett2_has_config_item(private, SCARLETT2_CONFIG_DIM_MUTE))
5395 for (i = 0; i < SCARLETT2_DIM_MUTE_COUNT; i++) {
5396 err = scarlett2_add_new_ctl(
5397 mixer, &scarlett2_dim_mute_ctl,
5398 i, 1, scarlett2_dim_mute_names[i],
5399 &private->dim_mute_ctls[i]);
5407 /*** Create the analogue input controls ***/
5409 static int scarlett2_add_line_in_ctls(struct usb_mixer_interface *mixer)
5411 struct scarlett2_data *private = mixer->private_data;
5412 const struct scarlett2_device_info *info = private->info;
5414 char s[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
5415 const char *fmt = "Line In %d %s Capture %s";
5416 const char *fmt2 = "Line In %d-%d %s Capture %s";
5418 /* Add input level (line/inst) controls */
5419 for (i = 0; i < info->level_input_count; i++) {
5420 scnprintf(s, sizeof(s), fmt, i + 1 + info->level_input_first,
5422 err = scarlett2_add_new_ctl(mixer, &scarlett2_level_enum_ctl,
5423 i, 1, s, &private->level_ctls[i]);
5428 /* Add input pad controls */
5429 for (i = 0; i < info->pad_input_count; i++) {
5430 scnprintf(s, sizeof(s), fmt, i + 1, "Pad", "Switch");
5431 err = scarlett2_add_new_ctl(mixer, &scarlett2_pad_ctl,
5432 i, 1, s, &private->pad_ctls[i]);
5437 /* Add input air controls */
5438 for (i = 0; i < info->air_input_count; i++) {
5439 scnprintf(s, sizeof(s), fmt, i + 1 + info->air_input_first,
5440 "Air", info->air_option ? "Enum" : "Switch");
5441 err = scarlett2_add_new_ctl(
5442 mixer, &scarlett2_air_ctl[info->air_option],
5443 i, 1, s, &private->air_ctls[i]);
5448 /* Add input phantom controls */
5449 if (info->inputs_per_phantom == 1) {
5450 for (i = 0; i < info->phantom_count; i++) {
5451 scnprintf(s, sizeof(s), fmt,
5452 i + 1 + info->phantom_first,
5453 "Phantom Power", "Switch");
5454 err = scarlett2_add_new_ctl(
5455 mixer, &scarlett2_phantom_ctl,
5456 i, 1, s, &private->phantom_ctls[i]);
5460 } else if (info->inputs_per_phantom > 1) {
5461 for (i = 0; i < info->phantom_count; i++) {
5462 int from = i * info->inputs_per_phantom + 1;
5463 int to = (i + 1) * info->inputs_per_phantom;
5465 scnprintf(s, sizeof(s), fmt2, from, to,
5466 "Phantom Power", "Switch");
5467 err = scarlett2_add_new_ctl(
5468 mixer, &scarlett2_phantom_ctl,
5469 i, 1, s, &private->phantom_ctls[i]);
5474 if (info->phantom_count &&
5475 scarlett2_has_config_item(private,
5476 SCARLETT2_CONFIG_PHANTOM_PERSISTENCE)) {
5477 err = scarlett2_add_new_ctl(
5478 mixer, &scarlett2_phantom_persistence_ctl, 0, 1,
5479 "Phantom Power Persistence Capture Switch", NULL);
5484 /* Add software-controllable input gain controls */
5485 if (info->gain_input_count) {
5486 err = scarlett2_add_new_ctl(
5487 mixer, &scarlett2_input_select_ctl, 0, 1,
5488 "Input Select Capture Enum",
5489 &private->input_select_ctl);
5493 for (i = 0; i < info->gain_input_count; i++) {
5495 scnprintf(s, sizeof(s),
5496 "Line In %d-%d Link Capture Switch",
5498 err = scarlett2_add_new_ctl(
5499 mixer, &scarlett2_input_link_ctl,
5501 &private->input_link_ctls[i / 2]);
5506 scnprintf(s, sizeof(s), fmt, i + 1,
5508 err = scarlett2_add_new_ctl(
5509 mixer, &scarlett2_input_gain_ctl,
5510 i, 1, s, &private->input_gain_ctls[i]);
5514 scnprintf(s, sizeof(s), fmt, i + 1,
5515 "Autogain", "Switch");
5516 err = scarlett2_add_new_ctl(
5517 mixer, &scarlett2_autogain_switch_ctl,
5518 i, 1, s, &private->autogain_ctls[i]);
5522 scnprintf(s, sizeof(s), fmt, i + 1,
5523 "Autogain Status", "Enum");
5524 err = scarlett2_add_new_ctl(
5525 mixer, &scarlett2_autogain_status_ctl,
5526 i, 1, s, &private->autogain_status_ctls[i]);
5528 scnprintf(s, sizeof(s), fmt, i + 1,
5530 err = scarlett2_add_new_ctl(
5531 mixer, &scarlett2_safe_ctl,
5532 i, 1, s, &private->safe_ctls[i]);
5538 /* Add PCM Input Switch control */
5539 if (scarlett2_has_config_item(private,
5540 SCARLETT2_CONFIG_PCM_INPUT_SWITCH)) {
5541 err = scarlett2_add_new_ctl(
5542 mixer, &scarlett2_pcm_input_switch_ctl, 0, 1,
5543 "PCM Input Capture Switch",
5544 &private->pcm_input_switch_ctl);
5552 /*** Mixer Volume Controls ***/
5554 static int scarlett2_update_mix(struct usb_mixer_interface *mixer)
5556 struct scarlett2_data *private = mixer->private_data;
5559 private->mix_updated = 0;
5561 for (i = 0; i < private->num_mix_out; i++) {
5562 err = scarlett2_usb_get_mix(mixer, i);
5570 static int scarlett2_mixer_ctl_info(struct snd_kcontrol *kctl,
5571 struct snd_ctl_elem_info *uinfo)
5573 struct usb_mixer_elem_info *elem = kctl->private_data;
5575 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
5576 uinfo->count = elem->channels;
5577 uinfo->value.integer.min = 0;
5578 uinfo->value.integer.max = SCARLETT2_MIXER_MAX_VALUE;
5579 uinfo->value.integer.step = 1;
5583 static int scarlett2_mixer_ctl_get(struct snd_kcontrol *kctl,
5584 struct snd_ctl_elem_value *ucontrol)
5586 struct usb_mixer_elem_info *elem = kctl->private_data;
5587 struct usb_mixer_interface *mixer = elem->head.mixer;
5588 struct scarlett2_data *private = mixer->private_data;
5591 mutex_lock(&private->data_mutex);
5593 if (private->hwdep_in_use) {
5598 if (private->mix_updated) {
5599 err = scarlett2_update_mix(mixer);
5603 ucontrol->value.integer.value[0] = private->mix[elem->control];
5606 mutex_unlock(&private->data_mutex);
5610 static int scarlett2_mixer_ctl_put(struct snd_kcontrol *kctl,
5611 struct snd_ctl_elem_value *ucontrol)
5613 struct usb_mixer_elem_info *elem = kctl->private_data;
5614 struct usb_mixer_interface *mixer = elem->head.mixer;
5615 struct scarlett2_data *private = mixer->private_data;
5616 int oval, val, mix_num, err = 0;
5617 int index = elem->control;
5619 mutex_lock(&private->data_mutex);
5621 if (private->hwdep_in_use) {
5626 oval = private->mix[index];
5627 val = clamp(ucontrol->value.integer.value[0],
5628 0L, (long)SCARLETT2_MIXER_MAX_VALUE);
5629 mix_num = index / private->num_mix_in;
5634 private->mix[index] = val;
5635 err = scarlett2_usb_set_mix(mixer, mix_num);
5640 mutex_unlock(&private->data_mutex);
5644 static const DECLARE_TLV_DB_MINMAX(
5645 db_scale_scarlett2_mixer,
5646 SCARLETT2_MIXER_MIN_DB * 100,
5647 SCARLETT2_MIXER_MAX_DB * 100
5650 static const struct snd_kcontrol_new scarlett2_mixer_ctl = {
5651 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
5652 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE |
5653 SNDRV_CTL_ELEM_ACCESS_TLV_READ,
5655 .info = scarlett2_mixer_ctl_info,
5656 .get = scarlett2_mixer_ctl_get,
5657 .put = scarlett2_mixer_ctl_put,
5658 .private_value = SCARLETT2_MIXER_MAX_DB, /* max value */
5659 .tlv = { .p = db_scale_scarlett2_mixer }
5662 static int scarlett2_add_mixer_ctls(struct usb_mixer_interface *mixer)
5664 struct scarlett2_data *private = mixer->private_data;
5667 char s[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
5669 for (i = 0, index = 0; i < private->num_mix_out; i++)
5670 for (j = 0; j < private->num_mix_in; j++, index++) {
5671 snprintf(s, sizeof(s),
5672 "Mix %c Input %02d Playback Volume",
5674 err = scarlett2_add_new_ctl(mixer, &scarlett2_mixer_ctl,
5676 &private->mix_ctls[index]);
5684 /*** Direct Monitor Control ***/
5686 static int scarlett2_update_direct_monitor(struct usb_mixer_interface *mixer)
5688 struct scarlett2_data *private = mixer->private_data;
5690 private->direct_monitor_updated = 0;
5692 if (!private->info->direct_monitor)
5695 return scarlett2_usb_get_config(
5696 mixer, SCARLETT2_CONFIG_DIRECT_MONITOR,
5697 1, &private->direct_monitor_switch);
5700 static int scarlett2_update_monitor_mix(struct usb_mixer_interface *mixer)
5702 struct scarlett2_data *private = mixer->private_data;
5704 u16 mix_values[SCARLETT2_MONITOR_MIX_MAX];
5706 if (!private->num_monitor_mix_ctls)
5709 err = scarlett2_usb_get_config(
5710 mixer, SCARLETT2_CONFIG_DIRECT_MONITOR_GAIN,
5711 private->num_monitor_mix_ctls, mix_values);
5715 for (i = 0; i < private->num_monitor_mix_ctls; i++)
5716 private->monitor_mix[i] = scarlett2_mixer_value_to_db(
5722 static int scarlett2_direct_monitor_ctl_get(
5723 struct snd_kcontrol *kctl, struct snd_ctl_elem_value *ucontrol)
5725 struct usb_mixer_elem_info *elem = kctl->private_data;
5726 struct usb_mixer_interface *mixer = elem->head.mixer;
5727 struct scarlett2_data *private = mixer->private_data;
5730 mutex_lock(&private->data_mutex);
5732 if (private->hwdep_in_use) {
5737 if (private->direct_monitor_updated) {
5738 err = scarlett2_update_direct_monitor(mixer);
5742 ucontrol->value.enumerated.item[0] = private->direct_monitor_switch;
5745 mutex_unlock(&private->data_mutex);
5749 static int scarlett2_direct_monitor_ctl_put(
5750 struct snd_kcontrol *kctl, struct snd_ctl_elem_value *ucontrol)
5752 struct usb_mixer_elem_info *elem = kctl->private_data;
5753 struct usb_mixer_interface *mixer = elem->head.mixer;
5754 struct scarlett2_data *private = mixer->private_data;
5756 int index = elem->control;
5757 int oval, val, err = 0;
5759 mutex_lock(&private->data_mutex);
5761 if (private->hwdep_in_use) {
5766 oval = private->direct_monitor_switch;
5767 val = min(ucontrol->value.enumerated.item[0], 2U);
5772 private->direct_monitor_switch = val;
5774 /* Send switch change to the device */
5775 err = scarlett2_usb_set_config(
5776 mixer, SCARLETT2_CONFIG_DIRECT_MONITOR, index, val);
5781 mutex_unlock(&private->data_mutex);
5785 static int scarlett2_direct_monitor_stereo_enum_ctl_info(
5786 struct snd_kcontrol *kctl, struct snd_ctl_elem_info *uinfo)
5788 static const char *const values[3] = {
5789 "Off", "Mono", "Stereo"
5792 return snd_ctl_enum_info(uinfo, 1, 3, values);
5795 /* Direct Monitor for Solo is mono-only and only needs a boolean control
5796 * Direct Monitor for 2i2 is selectable between Off/Mono/Stereo
5798 static const struct snd_kcontrol_new scarlett2_direct_monitor_ctl[2] = {
5800 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
5802 .info = snd_ctl_boolean_mono_info,
5803 .get = scarlett2_direct_monitor_ctl_get,
5804 .put = scarlett2_direct_monitor_ctl_put,
5807 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
5809 .info = scarlett2_direct_monitor_stereo_enum_ctl_info,
5810 .get = scarlett2_direct_monitor_ctl_get,
5811 .put = scarlett2_direct_monitor_ctl_put,
5815 static int scarlett2_monitor_mix_ctl_get(struct snd_kcontrol *kctl,
5816 struct snd_ctl_elem_value *ucontrol)
5818 struct usb_mixer_elem_info *elem = kctl->private_data;
5819 struct scarlett2_data *private = elem->head.mixer->private_data;
5821 ucontrol->value.integer.value[0] = private->monitor_mix[elem->control];
5826 static int scarlett2_monitor_mix_ctl_put(struct snd_kcontrol *kctl,
5827 struct snd_ctl_elem_value *ucontrol)
5829 struct usb_mixer_elem_info *elem = kctl->private_data;
5830 struct usb_mixer_interface *mixer = elem->head.mixer;
5831 struct scarlett2_data *private = mixer->private_data;
5832 int oval, val, err = 0;
5833 int index = elem->control;
5835 mutex_lock(&private->data_mutex);
5837 if (private->hwdep_in_use) {
5842 oval = private->monitor_mix[index];
5843 val = clamp(ucontrol->value.integer.value[0],
5844 0L, (long)SCARLETT2_MIXER_MAX_VALUE);
5849 private->monitor_mix[index] = val;
5850 err = scarlett2_usb_set_config(
5851 mixer, SCARLETT2_CONFIG_DIRECT_MONITOR_GAIN,
5852 index, scarlett2_mixer_values[val]);
5857 mutex_unlock(&private->data_mutex);
5861 static const struct snd_kcontrol_new scarlett2_monitor_mix_ctl = {
5862 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
5863 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE |
5864 SNDRV_CTL_ELEM_ACCESS_TLV_READ,
5866 .info = scarlett2_mixer_ctl_info,
5867 .get = scarlett2_monitor_mix_ctl_get,
5868 .put = scarlett2_monitor_mix_ctl_put,
5869 .private_value = SCARLETT2_MIXER_MAX_DB, /* max value */
5870 .tlv = { .p = db_scale_scarlett2_mixer }
5873 static int scarlett2_add_direct_monitor_ctls(struct usb_mixer_interface *mixer)
5875 struct scarlett2_data *private = mixer->private_data;
5876 const struct scarlett2_device_info *info = private->info;
5878 int err, i, j, k, index;
5880 if (!info->direct_monitor)
5883 s = info->direct_monitor == 1
5884 ? "Direct Monitor Playback Switch"
5885 : "Direct Monitor Playback Enum";
5887 err = scarlett2_add_new_ctl(
5888 mixer, &scarlett2_direct_monitor_ctl[info->direct_monitor - 1],
5889 0, 1, s, &private->direct_monitor_ctl);
5893 if (!private->num_monitor_mix_ctls)
5896 /* 1 or 2 direct monitor selections (Mono & Stereo) */
5897 for (i = 0, index = 0; i < info->direct_monitor; i++) {
5898 const char * const format =
5899 "Monitor %sMix %c Input %02d Playback Volume";
5900 const char *mix_type;
5902 if (info->direct_monitor == 1)
5909 /* 2 Mix outputs, A/Left & B/Right */
5910 for (j = 0; j < 2; j++)
5913 for (k = 0; k < private->num_mix_in; k++, index++) {
5914 char name[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
5916 scnprintf(name, sizeof(name), format,
5917 mix_type, 'A' + j, k + 1);
5919 err = scarlett2_add_new_ctl(
5920 mixer, &scarlett2_monitor_mix_ctl,
5921 index, 1, name, NULL);
5930 /*** Mux Source Selection Controls ***/
5932 static int scarlett2_mux_src_enum_ctl_info(struct snd_kcontrol *kctl,
5933 struct snd_ctl_elem_info *uinfo)
5935 struct usb_mixer_elem_info *elem = kctl->private_data;
5936 struct scarlett2_data *private = elem->head.mixer->private_data;
5937 const struct scarlett2_device_info *info = private->info;
5938 const int (*port_count)[SCARLETT2_PORT_DIRNS] = info->port_count;
5939 unsigned int item = uinfo->value.enumerated.item;
5940 int items = private->num_mux_srcs;
5943 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
5944 uinfo->count = elem->channels;
5945 uinfo->value.enumerated.items = items;
5948 item = uinfo->value.enumerated.item = items - 1;
5951 port_type < SCARLETT2_PORT_TYPE_COUNT;
5953 if (item < port_count[port_type][SCARLETT2_PORT_IN]) {
5954 const struct scarlett2_port *port =
5955 &scarlett2_ports[port_type];
5957 if (port_type == SCARLETT2_PORT_TYPE_MIX &&
5958 item >= private->num_mix_out)
5959 sprintf(uinfo->value.enumerated.name,
5960 port->dsp_src_descr,
5961 item - private->num_mix_out + 1);
5963 sprintf(uinfo->value.enumerated.name,
5965 item + port->src_num_offset);
5969 item -= port_count[port_type][SCARLETT2_PORT_IN];
5975 static int scarlett2_mux_src_enum_ctl_get(struct snd_kcontrol *kctl,
5976 struct snd_ctl_elem_value *ucontrol)
5978 struct usb_mixer_elem_info *elem = kctl->private_data;
5979 struct usb_mixer_interface *mixer = elem->head.mixer;
5980 struct scarlett2_data *private = mixer->private_data;
5981 int index = line_out_remap(private, elem->control);
5984 mutex_lock(&private->data_mutex);
5986 if (private->hwdep_in_use) {
5991 if (private->mux_updated) {
5992 err = scarlett2_usb_get_mux(mixer);
5996 ucontrol->value.enumerated.item[0] = private->mux[index];
5999 mutex_unlock(&private->data_mutex);
6003 static int scarlett2_mux_src_enum_ctl_put(struct snd_kcontrol *kctl,
6004 struct snd_ctl_elem_value *ucontrol)
6006 struct usb_mixer_elem_info *elem = kctl->private_data;
6007 struct usb_mixer_interface *mixer = elem->head.mixer;
6008 struct scarlett2_data *private = mixer->private_data;
6009 int index = line_out_remap(private, elem->control);
6010 int oval, val, err = 0;
6012 mutex_lock(&private->data_mutex);
6014 if (private->hwdep_in_use) {
6019 oval = private->mux[index];
6020 val = min(ucontrol->value.enumerated.item[0],
6021 private->num_mux_srcs - 1U);
6026 private->mux[index] = val;
6027 err = scarlett2_usb_set_mux(mixer);
6032 mutex_unlock(&private->data_mutex);
6036 static const struct snd_kcontrol_new scarlett2_mux_src_enum_ctl = {
6037 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
6039 .info = scarlett2_mux_src_enum_ctl_info,
6040 .get = scarlett2_mux_src_enum_ctl_get,
6041 .put = scarlett2_mux_src_enum_ctl_put,
6044 static int scarlett2_add_mux_enums(struct usb_mixer_interface *mixer)
6046 struct scarlett2_data *private = mixer->private_data;
6047 const struct scarlett2_device_info *info = private->info;
6048 const int (*port_count)[SCARLETT2_PORT_DIRNS] = info->port_count;
6049 int port_type, channel, i;
6051 for (i = 0, port_type = 0;
6052 port_type < SCARLETT2_PORT_TYPE_COUNT;
6055 channel < port_count[port_type][SCARLETT2_PORT_OUT];
6058 char s[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
6059 int channel_num = channel + 1;
6060 const struct scarlett2_port *port =
6061 &scarlett2_ports[port_type];
6062 const char *descr = port->dst_descr;
6064 if (port_type == SCARLETT2_PORT_TYPE_MIX &&
6065 channel >= private->num_mix_in) {
6066 channel_num -= private->num_mix_in;
6067 descr = port->dsp_dst_descr;
6070 snprintf(s, sizeof(s) - 5, descr, channel_num);
6073 err = scarlett2_add_new_ctl(mixer,
6074 &scarlett2_mux_src_enum_ctl,
6076 &private->mux_ctls[i]);
6085 /*** Meter Controls ***/
6087 static int scarlett2_meter_ctl_info(struct snd_kcontrol *kctl,
6088 struct snd_ctl_elem_info *uinfo)
6090 struct usb_mixer_elem_info *elem = kctl->private_data;
6092 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
6093 uinfo->count = elem->channels;
6094 uinfo->value.integer.min = 0;
6095 uinfo->value.integer.max = 4095;
6096 uinfo->value.integer.step = 1;
6100 static int scarlett2_meter_ctl_get(struct snd_kcontrol *kctl,
6101 struct snd_ctl_elem_value *ucontrol)
6103 struct usb_mixer_elem_info *elem = kctl->private_data;
6104 struct usb_mixer_interface *mixer = elem->head.mixer;
6105 struct scarlett2_data *private = mixer->private_data;
6106 u8 *meter_level_map = private->meter_level_map;
6107 u16 meter_levels[SCARLETT2_MAX_METERS];
6110 mutex_lock(&private->data_mutex);
6112 if (private->hwdep_in_use) {
6117 err = scarlett2_usb_get_meter_levels(mixer, elem->channels,
6122 /* copy & translate from meter_levels[] using meter_level_map[] */
6123 for (i = 0; i < elem->channels; i++) {
6124 int idx = meter_level_map[i];
6130 value = meter_levels[idx];
6132 ucontrol->value.integer.value[i] = value;
6136 mutex_unlock(&private->data_mutex);
6141 static const struct snd_kcontrol_new scarlett2_meter_ctl = {
6142 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
6143 .access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE,
6145 .info = scarlett2_meter_ctl_info,
6146 .get = scarlett2_meter_ctl_get
6149 static int scarlett2_add_meter_ctl(struct usb_mixer_interface *mixer)
6151 struct scarlett2_data *private = mixer->private_data;
6153 /* devices without a mixer also don't support reporting levels */
6154 if (!scarlett2_has_mixer(private))
6157 return scarlett2_add_new_ctl(mixer, &scarlett2_meter_ctl,
6158 0, private->num_mux_dsts,
6159 "Level Meter", NULL);
6162 /*** MSD Controls ***/
6164 static int scarlett2_msd_ctl_get(struct snd_kcontrol *kctl,
6165 struct snd_ctl_elem_value *ucontrol)
6167 struct usb_mixer_elem_info *elem = kctl->private_data;
6168 struct scarlett2_data *private = elem->head.mixer->private_data;
6170 ucontrol->value.integer.value[0] = private->msd_switch;
6174 static int scarlett2_msd_ctl_put(struct snd_kcontrol *kctl,
6175 struct snd_ctl_elem_value *ucontrol)
6177 struct usb_mixer_elem_info *elem = kctl->private_data;
6178 struct usb_mixer_interface *mixer = elem->head.mixer;
6179 struct scarlett2_data *private = mixer->private_data;
6181 int oval, val, err = 0;
6183 mutex_lock(&private->data_mutex);
6185 if (private->hwdep_in_use) {
6190 oval = private->msd_switch;
6191 val = !!ucontrol->value.integer.value[0];
6196 private->msd_switch = val;
6198 /* Send switch change to the device */
6199 err = scarlett2_usb_set_config(mixer, SCARLETT2_CONFIG_MSD_SWITCH,
6205 mutex_unlock(&private->data_mutex);
6209 static const struct snd_kcontrol_new scarlett2_msd_ctl = {
6210 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
6212 .info = snd_ctl_boolean_mono_info,
6213 .get = scarlett2_msd_ctl_get,
6214 .put = scarlett2_msd_ctl_put,
6217 static int scarlett2_add_msd_ctl(struct usb_mixer_interface *mixer)
6219 struct scarlett2_data *private = mixer->private_data;
6221 if (!scarlett2_has_config_item(private, SCARLETT2_CONFIG_MSD_SWITCH))
6224 /* If MSD mode is off, hide the switch by default */
6225 if (!private->msd_switch && !(mixer->chip->setup & SCARLETT2_MSD_ENABLE))
6228 /* Add MSD control */
6229 return scarlett2_add_new_ctl(mixer, &scarlett2_msd_ctl,
6230 0, 1, "MSD Mode Switch", NULL);
6233 /*** Standalone Control ***/
6235 static int scarlett2_standalone_ctl_get(struct snd_kcontrol *kctl,
6236 struct snd_ctl_elem_value *ucontrol)
6238 struct usb_mixer_elem_info *elem = kctl->private_data;
6239 struct scarlett2_data *private = elem->head.mixer->private_data;
6241 ucontrol->value.integer.value[0] = private->standalone_switch;
6245 static int scarlett2_standalone_ctl_put(struct snd_kcontrol *kctl,
6246 struct snd_ctl_elem_value *ucontrol)
6248 struct usb_mixer_elem_info *elem = kctl->private_data;
6249 struct usb_mixer_interface *mixer = elem->head.mixer;
6250 struct scarlett2_data *private = mixer->private_data;
6252 int oval, val, err = 0;
6254 mutex_lock(&private->data_mutex);
6256 if (private->hwdep_in_use) {
6261 oval = private->standalone_switch;
6262 val = !!ucontrol->value.integer.value[0];
6267 private->standalone_switch = val;
6269 /* Send switch change to the device */
6270 err = scarlett2_usb_set_config(mixer,
6271 SCARLETT2_CONFIG_STANDALONE_SWITCH,
6277 mutex_unlock(&private->data_mutex);
6281 static const struct snd_kcontrol_new scarlett2_standalone_ctl = {
6282 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
6284 .info = snd_ctl_boolean_mono_info,
6285 .get = scarlett2_standalone_ctl_get,
6286 .put = scarlett2_standalone_ctl_put,
6289 static int scarlett2_add_standalone_ctl(struct usb_mixer_interface *mixer)
6291 struct scarlett2_data *private = mixer->private_data;
6293 if (!scarlett2_has_config_item(private,
6294 SCARLETT2_CONFIG_STANDALONE_SWITCH))
6297 /* Add standalone control */
6298 return scarlett2_add_new_ctl(mixer, &scarlett2_standalone_ctl,
6299 0, 1, "Standalone Switch", NULL);
6302 /*** Power Status ***/
6304 static int scarlett2_update_power_status(struct usb_mixer_interface *mixer)
6306 struct scarlett2_data *private = mixer->private_data;
6308 u8 power_ext, power_low;
6310 private->power_status_updated = 0;
6312 err = scarlett2_usb_get_config(mixer, SCARLETT2_CONFIG_POWER_EXT,
6317 err = scarlett2_usb_get_config(mixer, SCARLETT2_CONFIG_POWER_LOW,
6323 private->power_status = SCARLETT2_POWER_STATUS_FAIL;
6325 private->power_status = SCARLETT2_POWER_STATUS_EXT;
6327 private->power_status = SCARLETT2_POWER_STATUS_BUS;
6332 static int scarlett2_power_status_ctl_get(struct snd_kcontrol *kctl,
6333 struct snd_ctl_elem_value *ucontrol)
6335 struct usb_mixer_elem_info *elem = kctl->private_data;
6336 struct usb_mixer_interface *mixer = elem->head.mixer;
6337 struct scarlett2_data *private = mixer->private_data;
6340 mutex_lock(&private->data_mutex);
6342 if (private->power_status_updated) {
6343 err = scarlett2_update_power_status(mixer);
6347 ucontrol->value.integer.value[0] = private->power_status;
6350 mutex_unlock(&private->data_mutex);
6354 static int scarlett2_power_status_ctl_info(
6355 struct snd_kcontrol *kctl, struct snd_ctl_elem_info *uinfo)
6357 static const char *const values[3] = {
6358 "External", "Bus", "Fail"
6361 return snd_ctl_enum_info(uinfo, 1, 3, values);
6364 static const struct snd_kcontrol_new scarlett2_power_status_ctl = {
6365 .iface = SNDRV_CTL_ELEM_IFACE_CARD,
6366 .access = SNDRV_CTL_ELEM_ACCESS_READ,
6368 .info = scarlett2_power_status_ctl_info,
6369 .get = scarlett2_power_status_ctl_get,
6372 static int scarlett2_add_power_status_ctl(struct usb_mixer_interface *mixer)
6374 struct scarlett2_data *private = mixer->private_data;
6376 if (!scarlett2_has_config_item(private,
6377 SCARLETT2_CONFIG_POWER_EXT))
6380 /* Add power status control */
6381 return scarlett2_add_new_ctl(mixer, &scarlett2_power_status_ctl,
6382 0, 1, "Power Status Card Enum",
6383 &private->power_status_ctl);
6386 /*** Cleanup/Suspend Callbacks ***/
6388 static void scarlett2_private_free(struct usb_mixer_interface *mixer)
6390 struct scarlett2_data *private = mixer->private_data;
6392 cancel_delayed_work_sync(&private->work);
6394 mixer->private_data = NULL;
6397 static void scarlett2_private_suspend(struct usb_mixer_interface *mixer)
6399 struct scarlett2_data *private = mixer->private_data;
6401 if (cancel_delayed_work_sync(&private->work))
6402 scarlett2_config_save(private->mixer);
6405 /*** Initialisation ***/
6407 static void scarlett2_count_io(struct scarlett2_data *private)
6409 const struct scarlett2_device_info *info = private->info;
6410 const int (*port_count)[SCARLETT2_PORT_DIRNS] = info->port_count;
6411 int port_type, srcs = 0, dsts = 0;
6413 /* Count the number of mux sources and destinations */
6415 port_type < SCARLETT2_PORT_TYPE_COUNT;
6417 srcs += port_count[port_type][SCARLETT2_PORT_IN];
6418 dsts += port_count[port_type][SCARLETT2_PORT_OUT];
6421 private->num_mux_srcs = srcs;
6422 private->num_mux_dsts = dsts;
6424 /* Mixer inputs are mux outputs and vice versa.
6425 * Scarlett Gen 4 DSP I/O uses SCARLETT2_PORT_TYPE_MIX but
6426 * doesn't have mixer controls.
6428 private->num_mix_in =
6429 port_count[SCARLETT2_PORT_TYPE_MIX][SCARLETT2_PORT_OUT] -
6432 private->num_mix_out =
6433 port_count[SCARLETT2_PORT_TYPE_MIX][SCARLETT2_PORT_IN] -
6436 /* Number of analogue line outputs */
6437 private->num_line_out =
6438 port_count[SCARLETT2_PORT_TYPE_ANALOGUE][SCARLETT2_PORT_OUT];
6440 /* Number of monitor mix controls */
6441 private->num_monitor_mix_ctls =
6442 info->direct_monitor * 2 * private->num_mix_in;
6445 /* Look through the interface descriptors for the Focusrite Control
6446 * interface (bInterfaceClass = 255 Vendor Specific Class) and set
6447 * bInterfaceNumber, bEndpointAddress, wMaxPacketSize, and bInterval
6450 static int scarlett2_find_fc_interface(struct usb_device *dev,
6451 struct scarlett2_data *private)
6453 struct usb_host_config *config = dev->actconfig;
6456 for (i = 0; i < config->desc.bNumInterfaces; i++) {
6457 struct usb_interface *intf = config->interface[i];
6458 struct usb_interface_descriptor *desc =
6459 &intf->altsetting[0].desc;
6460 struct usb_endpoint_descriptor *epd;
6462 if (desc->bInterfaceClass != 255)
6465 epd = get_endpoint(intf->altsetting, 0);
6466 private->bInterfaceNumber = desc->bInterfaceNumber;
6467 private->bEndpointAddress = epd->bEndpointAddress &
6468 USB_ENDPOINT_NUMBER_MASK;
6469 private->wMaxPacketSize = le16_to_cpu(epd->wMaxPacketSize);
6470 private->bInterval = epd->bInterval;
6477 /* Initialise private data */
6478 static int scarlett2_init_private(struct usb_mixer_interface *mixer,
6479 const struct scarlett2_device_entry *entry)
6481 struct scarlett2_data *private =
6482 kzalloc(sizeof(struct scarlett2_data), GFP_KERNEL);
6487 mutex_init(&private->usb_mutex);
6488 mutex_init(&private->data_mutex);
6489 INIT_DELAYED_WORK(&private->work, scarlett2_config_save_work);
6491 mixer->private_data = private;
6492 mixer->private_free = scarlett2_private_free;
6493 mixer->private_suspend = scarlett2_private_suspend;
6495 private->info = entry->info;
6496 private->config_set = entry->info->config_set;
6497 private->series_name = entry->series_name;
6498 scarlett2_count_io(private);
6499 private->scarlett2_seq = 0;
6500 private->mixer = mixer;
6502 return scarlett2_find_fc_interface(mixer->chip->dev, private);
6505 /* Cargo cult proprietary initialisation sequence */
6506 static int scarlett2_usb_init(struct usb_mixer_interface *mixer)
6508 struct usb_device *dev = mixer->chip->dev;
6509 struct scarlett2_data *private = mixer->private_data;
6514 if (usb_pipe_type_check(dev, usb_sndctrlpipe(dev, 0)))
6518 err = scarlett2_usb_rx(dev, private->bInterfaceNumber,
6519 SCARLETT2_USB_CMD_INIT,
6520 step0_buf, sizeof(step0_buf));
6525 private->scarlett2_seq = 1;
6526 err = scarlett2_usb(mixer, SCARLETT2_USB_INIT_1, NULL, 0, NULL, 0);
6531 private->scarlett2_seq = 1;
6532 err = scarlett2_usb(mixer, SCARLETT2_USB_INIT_2,
6534 step2_buf, sizeof(step2_buf));
6538 /* extract 4-byte firmware version from step2_buf[8] */
6539 private->firmware_version = le32_to_cpu(*(__le32 *)(step2_buf + 8));
6540 usb_audio_info(mixer->chip,
6541 "Firmware version %d\n",
6542 private->firmware_version);
6547 /* Get the flash segment numbers for the App_Settings and App_Upgrade
6548 * segments and put them in the private data
6550 static int scarlett2_get_flash_segment_nums(struct usb_mixer_interface *mixer)
6552 struct scarlett2_data *private = mixer->private_data;
6559 } __packed flash_info;
6565 } __packed segment_info;
6567 err = scarlett2_usb(mixer, SCARLETT2_USB_INFO_FLASH,
6569 &flash_info, sizeof(flash_info));
6573 count = le32_to_cpu(flash_info.count);
6575 /* sanity check count */
6576 if (count < SCARLETT2_SEGMENT_NUM_MIN ||
6577 count > SCARLETT2_SEGMENT_NUM_MAX + 1) {
6578 usb_audio_err(mixer->chip,
6579 "invalid flash segment count: %d\n", count);
6583 for (i = 0; i < count; i++) {
6584 __le32 segment_num_req = cpu_to_le32(i);
6585 int flash_segment_id;
6587 err = scarlett2_usb(mixer, SCARLETT2_USB_INFO_SEGMENT,
6588 &segment_num_req, sizeof(segment_num_req),
6589 &segment_info, sizeof(segment_info));
6591 usb_audio_err(mixer->chip,
6592 "failed to get flash segment info %d: %d\n",
6597 if (!strncmp(segment_info.name,
6598 SCARLETT2_SEGMENT_SETTINGS_NAME, 16))
6599 flash_segment_id = SCARLETT2_SEGMENT_ID_SETTINGS;
6600 else if (!strncmp(segment_info.name,
6601 SCARLETT2_SEGMENT_FIRMWARE_NAME, 16))
6602 flash_segment_id = SCARLETT2_SEGMENT_ID_FIRMWARE;
6606 private->flash_segment_nums[flash_segment_id] = i;
6607 private->flash_segment_blocks[flash_segment_id] =
6608 le32_to_cpu(segment_info.size) /
6609 SCARLETT2_FLASH_BLOCK_SIZE;
6612 /* segment 0 is App_Gold and we never want to touch that, so
6613 * use 0 as the "not-found" value
6615 if (!private->flash_segment_nums[SCARLETT2_SEGMENT_ID_SETTINGS]) {
6616 usb_audio_err(mixer->chip,
6617 "failed to find flash segment %s\n",
6618 SCARLETT2_SEGMENT_SETTINGS_NAME);
6621 if (!private->flash_segment_nums[SCARLETT2_SEGMENT_ID_FIRMWARE]) {
6622 usb_audio_err(mixer->chip,
6623 "failed to find flash segment %s\n",
6624 SCARLETT2_SEGMENT_FIRMWARE_NAME);
6631 /* Read configuration from the interface on start */
6632 static int scarlett2_read_configs(struct usb_mixer_interface *mixer)
6634 struct scarlett2_data *private = mixer->private_data;
6635 const struct scarlett2_device_info *info = private->info;
6638 if (scarlett2_has_config_item(private, SCARLETT2_CONFIG_MSD_SWITCH)) {
6639 err = scarlett2_usb_get_config(
6640 mixer, SCARLETT2_CONFIG_MSD_SWITCH,
6641 1, &private->msd_switch);
6646 if (private->firmware_version < info->min_firmware_version) {
6647 usb_audio_err(mixer->chip,
6648 "Focusrite %s firmware version %d is too old; "
6650 private->series_name,
6651 private->firmware_version,
6652 info->min_firmware_version);
6656 /* no other controls are created if MSD mode is on */
6657 if (private->msd_switch)
6660 err = scarlett2_update_input_level(mixer);
6664 err = scarlett2_update_input_pad(mixer);
6668 err = scarlett2_update_input_air(mixer);
6672 err = scarlett2_update_input_phantom(mixer);
6676 err = scarlett2_update_direct_monitor(mixer);
6680 /* the rest of the configuration is for devices with a mixer */
6681 if (!scarlett2_has_mixer(private))
6684 err = scarlett2_update_monitor_mix(mixer);
6688 err = scarlett2_update_monitor_other(mixer);
6692 if (scarlett2_has_config_item(private,
6693 SCARLETT2_CONFIG_STANDALONE_SWITCH)) {
6694 err = scarlett2_usb_get_config(
6695 mixer, SCARLETT2_CONFIG_STANDALONE_SWITCH,
6696 1, &private->standalone_switch);
6701 if (scarlett2_has_config_item(private,
6702 SCARLETT2_CONFIG_POWER_EXT)) {
6703 err = scarlett2_update_power_status(mixer);
6708 err = scarlett2_update_sync(mixer);
6712 if (scarlett2_has_config_item(private,
6713 SCARLETT2_CONFIG_LINE_OUT_VOLUME)) {
6714 s16 sw_vol[SCARLETT2_ANALOGUE_MAX];
6716 /* read SW line out volume */
6717 err = scarlett2_usb_get_config(
6718 mixer, SCARLETT2_CONFIG_LINE_OUT_VOLUME,
6719 private->num_line_out, &sw_vol);
6723 for (i = 0; i < private->num_line_out; i++)
6724 private->vol[i] = clamp(
6725 sw_vol[i] + SCARLETT2_VOLUME_BIAS,
6726 0, SCARLETT2_VOLUME_BIAS);
6729 err = scarlett2_usb_get_config(
6730 mixer, SCARLETT2_CONFIG_MUTE_SWITCH,
6731 private->num_line_out, &private->mute_switch);
6735 for (i = 0; i < private->num_line_out; i++)
6736 private->mute_switch[i] =
6737 !!private->mute_switch[i];
6739 /* read SW/HW switches */
6740 if (scarlett2_has_config_item(private,
6741 SCARLETT2_CONFIG_SW_HW_SWITCH)) {
6742 err = scarlett2_usb_get_config(
6743 mixer, SCARLETT2_CONFIG_SW_HW_SWITCH,
6744 private->num_line_out,
6745 &private->vol_sw_hw_switch);
6749 for (i = 0; i < private->num_line_out; i++)
6750 private->vol_sw_hw_switch[i] =
6751 !!private->vol_sw_hw_switch[i];
6755 err = scarlett2_update_volumes(mixer);
6759 err = scarlett2_update_dim_mute(mixer);
6763 err = scarlett2_update_input_select(mixer);
6767 err = scarlett2_update_input_gain(mixer);
6771 err = scarlett2_update_autogain(mixer);
6775 err = scarlett2_update_input_safe(mixer);
6779 if (scarlett2_has_config_item(private,
6780 SCARLETT2_CONFIG_PCM_INPUT_SWITCH)) {
6781 err = scarlett2_update_pcm_input_switch(mixer);
6786 err = scarlett2_update_mix(mixer);
6790 return scarlett2_usb_get_mux(mixer);
6793 /* Notify on sync change */
6794 static void scarlett2_notify_sync(struct usb_mixer_interface *mixer)
6796 struct scarlett2_data *private = mixer->private_data;
6798 private->sync_updated = 1;
6800 snd_ctl_notify(mixer->chip->card, SNDRV_CTL_EVENT_MASK_VALUE,
6801 &private->sync_ctl->id);
6804 /* Notify on monitor change (Gen 2/3) */
6805 static void scarlett2_notify_monitor(struct usb_mixer_interface *mixer)
6807 struct snd_card *card = mixer->chip->card;
6808 struct scarlett2_data *private = mixer->private_data;
6811 if (!scarlett2_has_config_item(private, SCARLETT2_CONFIG_SW_HW_SWITCH))
6814 private->vol_updated = 1;
6816 snd_ctl_notify(mixer->chip->card, SNDRV_CTL_EVENT_MASK_VALUE,
6817 &private->master_vol_ctl->id);
6819 for (i = 0; i < private->num_line_out; i++)
6820 if (private->vol_sw_hw_switch[line_out_remap(private, i)])
6821 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE,
6822 &private->vol_ctls[i]->id);
6825 /* Notify on volume change (Gen 4) */
6826 static void scarlett2_notify_volume(struct usb_mixer_interface *mixer)
6828 struct scarlett2_data *private = mixer->private_data;
6830 private->vol_updated = 1;
6832 snd_ctl_notify(mixer->chip->card, SNDRV_CTL_EVENT_MASK_VALUE,
6833 &private->master_vol_ctl->id);
6834 snd_ctl_notify(mixer->chip->card, SNDRV_CTL_EVENT_MASK_VALUE,
6835 &private->headphone_vol_ctl->id);
6838 /* Notify on dim/mute change */
6839 static void scarlett2_notify_dim_mute(struct usb_mixer_interface *mixer)
6841 struct snd_card *card = mixer->chip->card;
6842 struct scarlett2_data *private = mixer->private_data;
6845 if (!scarlett2_has_config_item(private, SCARLETT2_CONFIG_SW_HW_SWITCH))
6848 private->dim_mute_updated = 1;
6850 for (i = 0; i < SCARLETT2_DIM_MUTE_COUNT; i++)
6851 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE,
6852 &private->dim_mute_ctls[i]->id);
6854 for (i = 0; i < private->num_line_out; i++)
6855 if (private->vol_sw_hw_switch[line_out_remap(private, i)])
6856 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE,
6857 &private->mute_ctls[i]->id);
6860 /* Notify on input level switch change */
6861 static void scarlett2_notify_input_level(struct usb_mixer_interface *mixer)
6863 struct snd_card *card = mixer->chip->card;
6864 struct scarlett2_data *private = mixer->private_data;
6865 const struct scarlett2_device_info *info = private->info;
6868 private->input_level_updated = 1;
6870 for (i = 0; i < info->level_input_count; i++)
6871 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE,
6872 &private->level_ctls[i]->id);
6875 /* Notify on input pad switch change */
6876 static void scarlett2_notify_input_pad(struct usb_mixer_interface *mixer)
6878 struct snd_card *card = mixer->chip->card;
6879 struct scarlett2_data *private = mixer->private_data;
6880 const struct scarlett2_device_info *info = private->info;
6883 private->input_pad_updated = 1;
6885 for (i = 0; i < info->pad_input_count; i++)
6886 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE,
6887 &private->pad_ctls[i]->id);
6890 /* Notify on input air switch change */
6891 static void scarlett2_notify_input_air(struct usb_mixer_interface *mixer)
6893 struct snd_card *card = mixer->chip->card;
6894 struct scarlett2_data *private = mixer->private_data;
6895 const struct scarlett2_device_info *info = private->info;
6898 private->input_air_updated = 1;
6900 for (i = 0; i < info->air_input_count; i++)
6901 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE,
6902 &private->air_ctls[i]->id);
6905 /* Notify on input phantom switch change */
6906 static void scarlett2_notify_input_phantom(struct usb_mixer_interface *mixer)
6908 struct snd_card *card = mixer->chip->card;
6909 struct scarlett2_data *private = mixer->private_data;
6910 const struct scarlett2_device_info *info = private->info;
6913 private->input_phantom_updated = 1;
6915 for (i = 0; i < info->phantom_count; i++)
6916 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE,
6917 &private->phantom_ctls[i]->id);
6919 scarlett2_phantom_notify_access(mixer);
6922 /* Notify on "input other" change (level/pad/air/phantom) */
6923 static void scarlett2_notify_input_other(struct usb_mixer_interface *mixer)
6925 scarlett2_notify_input_level(mixer);
6926 scarlett2_notify_input_pad(mixer);
6927 scarlett2_notify_input_air(mixer);
6928 scarlett2_notify_input_phantom(mixer);
6931 /* Notify on input select change */
6932 static void scarlett2_notify_input_select(struct usb_mixer_interface *mixer)
6934 struct snd_card *card = mixer->chip->card;
6935 struct scarlett2_data *private = mixer->private_data;
6936 const struct scarlett2_device_info *info = private->info;
6939 if (!info->gain_input_count)
6942 private->input_select_updated = 1;
6944 snd_ctl_notify(card,
6945 SNDRV_CTL_EVENT_MASK_VALUE | SNDRV_CTL_EVENT_MASK_INFO,
6946 &private->input_select_ctl->id);
6948 for (i = 0; i < info->gain_input_count / 2; i++)
6949 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE,
6950 &private->input_link_ctls[i]->id);
6953 /* Notify on input gain change */
6954 static void scarlett2_notify_input_gain(struct usb_mixer_interface *mixer)
6956 struct snd_card *card = mixer->chip->card;
6957 struct scarlett2_data *private = mixer->private_data;
6958 const struct scarlett2_device_info *info = private->info;
6961 if (!info->gain_input_count)
6964 private->input_gain_updated = 1;
6966 for (i = 0; i < info->gain_input_count; i++)
6967 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE,
6968 &private->input_gain_ctls[i]->id);
6971 /* Notify on autogain change */
6972 static void scarlett2_notify_autogain(struct usb_mixer_interface *mixer)
6974 struct snd_card *card = mixer->chip->card;
6975 struct scarlett2_data *private = mixer->private_data;
6976 const struct scarlett2_device_info *info = private->info;
6979 if (!info->gain_input_count)
6982 private->autogain_updated = 1;
6984 for (i = 0; i < info->gain_input_count; i++) {
6985 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE,
6986 &private->autogain_ctls[i]->id);
6987 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE,
6988 &private->autogain_status_ctls[i]->id);
6991 scarlett2_autogain_notify_access(mixer);
6994 /* Notify on input safe switch change */
6995 static void scarlett2_notify_input_safe(struct usb_mixer_interface *mixer)
6997 struct snd_card *card = mixer->chip->card;
6998 struct scarlett2_data *private = mixer->private_data;
6999 const struct scarlett2_device_info *info = private->info;
7002 if (!info->gain_input_count)
7005 private->input_safe_updated = 1;
7007 for (i = 0; i < info->gain_input_count; i++)
7008 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE,
7009 &private->safe_ctls[i]->id);
7012 /* Notify on "monitor other" change (speaker switching, talkback) */
7013 static void scarlett2_notify_monitor_other(struct usb_mixer_interface *mixer)
7015 struct snd_card *card = mixer->chip->card;
7016 struct scarlett2_data *private = mixer->private_data;
7017 const struct scarlett2_device_info *info = private->info;
7019 private->monitor_other_updated = 1;
7021 if (info->has_speaker_switching)
7022 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE,
7023 &private->speaker_switching_ctl->id);
7025 if (info->has_talkback)
7026 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE,
7027 &private->talkback_ctl->id);
7029 /* if speaker switching was recently enabled or disabled,
7030 * invalidate the dim/mute and mux enum controls
7032 if (private->speaker_switching_switched) {
7035 scarlett2_notify_dim_mute(mixer);
7037 private->speaker_switching_switched = 0;
7038 private->mux_updated = 1;
7040 for (i = 0; i < private->num_mux_dsts; i++)
7041 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE,
7042 &private->mux_ctls[i]->id);
7046 /* Notify on direct monitor switch change */
7047 static void scarlett2_notify_direct_monitor(struct usb_mixer_interface *mixer)
7049 struct snd_card *card = mixer->chip->card;
7050 struct scarlett2_data *private = mixer->private_data;
7051 int count = private->num_mix_in * private->num_mix_out;
7054 private->direct_monitor_updated = 1;
7056 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE,
7057 &private->direct_monitor_ctl->id);
7059 if (!scarlett2_has_mixer(private))
7062 private->mix_updated = 1;
7064 /* Notify of change to the mix controls */
7065 for (i = 0; i < count; i++)
7066 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE,
7067 &private->mix_ctls[i]->id);
7070 /* Notify on power change */
7071 static void scarlett2_notify_power_status(struct usb_mixer_interface *mixer)
7073 struct snd_card *card = mixer->chip->card;
7074 struct scarlett2_data *private = mixer->private_data;
7076 private->power_status_updated = 1;
7078 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE,
7079 &private->power_status_ctl->id);
7082 /* Notify on mux change */
7083 static void scarlett2_notify_mux(struct usb_mixer_interface *mixer)
7085 struct snd_card *card = mixer->chip->card;
7086 struct scarlett2_data *private = mixer->private_data;
7089 private->mux_updated = 1;
7091 for (i = 0; i < private->num_mux_dsts; i++)
7092 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE,
7093 &private->mux_ctls[i]->id);
7096 /* Notify on PCM input switch change */
7097 static void scarlett2_notify_pcm_input_switch(struct usb_mixer_interface *mixer)
7099 struct snd_card *card = mixer->chip->card;
7100 struct scarlett2_data *private = mixer->private_data;
7102 private->pcm_input_switch_updated = 1;
7104 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE,
7105 &private->pcm_input_switch_ctl->id);
7107 scarlett2_notify_mux(mixer);
7110 /* Interrupt callback */
7111 static void scarlett2_notify(struct urb *urb)
7113 struct usb_mixer_interface *mixer = urb->context;
7114 int len = urb->actual_length;
7115 int ustatus = urb->status;
7117 struct scarlett2_data *private = mixer->private_data;
7118 const struct scarlett2_notification *notifications =
7119 private->config_set->notifications;
7121 if (ustatus != 0 || len != 8)
7124 data = le32_to_cpu(*(__le32 *)urb->transfer_buffer);
7126 while (data && notifications->mask) {
7127 if (data & notifications->mask) {
7128 data &= ~notifications->mask;
7129 if (notifications->func)
7130 notifications->func(mixer);
7136 usb_audio_warn(mixer->chip,
7137 "%s: Unhandled notification: 0x%08x\n",
7141 if (ustatus != -ENOENT &&
7142 ustatus != -ECONNRESET &&
7143 ustatus != -ESHUTDOWN) {
7144 urb->dev = mixer->chip->dev;
7145 usb_submit_urb(urb, GFP_ATOMIC);
7149 static int scarlett2_init_notify(struct usb_mixer_interface *mixer)
7151 struct usb_device *dev = mixer->chip->dev;
7152 struct scarlett2_data *private = mixer->private_data;
7153 unsigned int pipe = usb_rcvintpipe(dev, private->bEndpointAddress);
7154 void *transfer_buffer;
7157 usb_audio_err(mixer->chip,
7158 "%s: mixer urb already in use!\n", __func__);
7162 if (usb_pipe_type_check(dev, pipe))
7165 mixer->urb = usb_alloc_urb(0, GFP_KERNEL);
7169 transfer_buffer = kmalloc(private->wMaxPacketSize, GFP_KERNEL);
7170 if (!transfer_buffer)
7173 usb_fill_int_urb(mixer->urb, dev, pipe,
7174 transfer_buffer, private->wMaxPacketSize,
7175 scarlett2_notify, mixer, private->bInterval);
7177 return usb_submit_urb(mixer->urb, GFP_KERNEL);
7180 static const struct scarlett2_device_entry *get_scarlett2_device_entry(
7181 struct usb_mixer_interface *mixer)
7183 const struct scarlett2_device_entry *entry = scarlett2_devices;
7185 /* Find entry in scarlett2_devices */
7186 while (entry->usb_id && entry->usb_id != mixer->chip->usb_id)
7194 static int snd_scarlett2_controls_create(
7195 struct usb_mixer_interface *mixer,
7196 const struct scarlett2_device_entry *entry)
7198 struct scarlett2_data *private;
7201 /* Initialise private data */
7202 err = scarlett2_init_private(mixer, entry);
7206 private = mixer->private_data;
7208 /* Send proprietary USB initialisation sequence */
7209 err = scarlett2_usb_init(mixer);
7213 /* Get the upgrade & settings flash segment numbers */
7214 err = scarlett2_get_flash_segment_nums(mixer);
7218 /* Add firmware version control */
7219 err = scarlett2_add_firmware_version_ctl(mixer);
7223 /* Add minimum firmware version control */
7224 err = scarlett2_add_min_firmware_version_ctl(mixer);
7228 /* Read volume levels and controls from the interface */
7229 err = scarlett2_read_configs(mixer);
7233 /* Create the MSD control */
7234 err = scarlett2_add_msd_ctl(mixer);
7238 /* If MSD mode is enabled, or if the firmware version is too
7239 * old, don't create any other controls
7241 if (private->msd_switch ||
7242 private->firmware_version < private->info->min_firmware_version)
7245 /* Create the analogue output controls */
7246 err = scarlett2_add_line_out_ctls(mixer);
7250 /* Create the analogue input controls */
7251 err = scarlett2_add_line_in_ctls(mixer);
7255 /* Create the input, output, and mixer mux input selections */
7256 err = scarlett2_add_mux_enums(mixer);
7260 /* Create the matrix mixer controls */
7261 err = scarlett2_add_mixer_ctls(mixer);
7265 /* Create the level meter controls */
7266 err = scarlett2_add_meter_ctl(mixer);
7270 /* Create the sync control */
7271 err = scarlett2_add_sync_ctl(mixer);
7275 /* Create the direct monitor control(s) */
7276 err = scarlett2_add_direct_monitor_ctls(mixer);
7280 /* Create the speaker switching control */
7281 err = scarlett2_add_speaker_switch_ctl(mixer);
7285 /* Create the talkback controls */
7286 err = scarlett2_add_talkback_ctls(mixer);
7290 /* Create the standalone control */
7291 err = scarlett2_add_standalone_ctl(mixer);
7295 /* Create the power status control */
7296 err = scarlett2_add_power_status_ctl(mixer);
7300 /* Set the access mode of controls disabled during
7301 * autogain/phantom power switching.
7303 if (private->info->gain_input_count) {
7304 scarlett2_autogain_update_access(mixer);
7305 scarlett2_phantom_update_access(mixer);
7308 /* Set up the interrupt polling */
7309 err = scarlett2_init_notify(mixer);
7316 /*** hwdep interface ***/
7318 /* Set private->hwdep_in_use; prevents access to the ALSA controls
7319 * while doing a config erase/firmware upgrade.
7321 static void scarlett2_lock(struct scarlett2_data *private)
7323 mutex_lock(&private->data_mutex);
7324 private->hwdep_in_use = 1;
7325 mutex_unlock(&private->data_mutex);
7328 /* Call SCARLETT2_USB_GET_ERASE to get the erase progress */
7329 static int scarlett2_get_erase_progress(struct usb_mixer_interface *mixer)
7331 struct scarlett2_data *private = mixer->private_data;
7332 int segment_id, segment_num, err;
7338 } __packed erase_req;
7340 segment_id = private->selected_flash_segment_id;
7341 segment_num = private->flash_segment_nums[segment_id];
7343 if (segment_num < SCARLETT2_SEGMENT_NUM_MIN ||
7344 segment_num > SCARLETT2_SEGMENT_NUM_MAX)
7347 /* Send the erase progress request */
7348 erase_req.segment_num = cpu_to_le32(segment_num);
7351 err = scarlett2_usb(mixer, SCARLETT2_USB_GET_ERASE,
7352 &erase_req, sizeof(erase_req),
7353 &erase_resp, sizeof(erase_resp));
7360 /* Repeatedly call scarlett2_get_erase_progress() until it returns
7361 * 0xff (erase complete) or we've waited 10 seconds (it usually takes
7364 static int scarlett2_wait_for_erase(struct usb_mixer_interface *mixer)
7368 for (i = 0; i < 100; i++) {
7369 err = scarlett2_get_erase_progress(mixer);
7382 /* Reboot the device; wait for the erase to complete if one is in
7385 static int scarlett2_reboot(struct usb_mixer_interface *mixer)
7387 struct scarlett2_data *private = mixer->private_data;
7389 if (private->flash_write_state ==
7390 SCARLETT2_FLASH_WRITE_STATE_ERASING) {
7391 int err = scarlett2_wait_for_erase(mixer);
7397 return scarlett2_usb(mixer, SCARLETT2_USB_REBOOT, NULL, 0, NULL, 0);
7400 /* Select a flash segment for erasing (and possibly writing to) */
7401 static int scarlett2_ioctl_select_flash_segment(
7402 struct usb_mixer_interface *mixer,
7405 struct scarlett2_data *private = mixer->private_data;
7406 int segment_id, segment_num;
7408 if (get_user(segment_id, (int __user *)arg))
7411 /* Check the segment ID and segment number */
7412 if (segment_id < 0 || segment_id >= SCARLETT2_SEGMENT_ID_COUNT)
7415 segment_num = private->flash_segment_nums[segment_id];
7416 if (segment_num < SCARLETT2_SEGMENT_NUM_MIN ||
7417 segment_num > SCARLETT2_SEGMENT_NUM_MAX) {
7418 usb_audio_err(mixer->chip,
7419 "%s: invalid segment number %d\n",
7420 __func__, segment_id);
7424 /* If erasing, wait for it to complete */
7425 if (private->flash_write_state == SCARLETT2_FLASH_WRITE_STATE_ERASING) {
7426 int err = scarlett2_wait_for_erase(mixer);
7432 /* Save the selected segment ID and set the state to SELECTED */
7433 private->selected_flash_segment_id = segment_id;
7434 private->flash_write_state = SCARLETT2_FLASH_WRITE_STATE_SELECTED;
7439 /* Erase the previously-selected flash segment */
7440 static int scarlett2_ioctl_erase_flash_segment(
7441 struct usb_mixer_interface *mixer)
7443 struct scarlett2_data *private = mixer->private_data;
7444 int segment_id, segment_num, err;
7449 } __packed erase_req;
7451 if (private->flash_write_state != SCARLETT2_FLASH_WRITE_STATE_SELECTED)
7454 segment_id = private->selected_flash_segment_id;
7455 segment_num = private->flash_segment_nums[segment_id];
7457 if (segment_num < SCARLETT2_SEGMENT_NUM_MIN ||
7458 segment_num > SCARLETT2_SEGMENT_NUM_MAX)
7461 /* Prevent access to ALSA controls that access the device from
7464 scarlett2_lock(private);
7466 /* Send the erase request */
7467 erase_req.segment_num = cpu_to_le32(segment_num);
7470 err = scarlett2_usb(mixer, SCARLETT2_USB_ERASE_SEGMENT,
7471 &erase_req, sizeof(erase_req),
7476 /* On success, change the state from SELECTED to ERASING */
7477 private->flash_write_state = SCARLETT2_FLASH_WRITE_STATE_ERASING;
7482 /* Get the erase progress from the device */
7483 static int scarlett2_ioctl_get_erase_progress(
7484 struct usb_mixer_interface *mixer,
7487 struct scarlett2_data *private = mixer->private_data;
7488 struct scarlett2_flash_segment_erase_progress progress;
7489 int segment_id, segment_num, err;
7495 } __packed erase_req;
7497 /* Check that we're erasing */
7498 if (private->flash_write_state != SCARLETT2_FLASH_WRITE_STATE_ERASING)
7501 segment_id = private->selected_flash_segment_id;
7502 segment_num = private->flash_segment_nums[segment_id];
7504 if (segment_num < SCARLETT2_SEGMENT_NUM_MIN ||
7505 segment_num > SCARLETT2_SEGMENT_NUM_MAX)
7508 /* Send the erase progress request */
7509 erase_req.segment_num = cpu_to_le32(segment_num);
7512 err = scarlett2_usb(mixer, SCARLETT2_USB_GET_ERASE,
7513 &erase_req, sizeof(erase_req),
7514 &erase_resp, sizeof(erase_resp));
7518 progress.progress = erase_resp;
7519 progress.num_blocks = private->flash_segment_blocks[segment_id];
7521 if (copy_to_user((void __user *)arg, &progress, sizeof(progress)))
7524 /* If the erase is complete, change the state from ERASING to
7527 if (progress.progress == 0xff)
7528 private->flash_write_state = SCARLETT2_FLASH_WRITE_STATE_WRITE;
7533 static int scarlett2_hwdep_open(struct snd_hwdep *hw, struct file *file)
7535 struct usb_mixer_interface *mixer = hw->private_data;
7536 struct scarlett2_data *private = mixer->private_data;
7538 /* If erasing, wait for it to complete */
7539 if (private->flash_write_state ==
7540 SCARLETT2_FLASH_WRITE_STATE_ERASING) {
7541 int err = scarlett2_wait_for_erase(mixer);
7547 /* Set the state to IDLE */
7548 private->flash_write_state = SCARLETT2_FLASH_WRITE_STATE_IDLE;
7553 static int scarlett2_hwdep_ioctl(struct snd_hwdep *hw, struct file *file,
7554 unsigned int cmd, unsigned long arg)
7556 struct usb_mixer_interface *mixer = hw->private_data;
7560 case SCARLETT2_IOCTL_PVERSION:
7561 return put_user(SCARLETT2_HWDEP_VERSION,
7562 (int __user *)arg) ? -EFAULT : 0;
7564 case SCARLETT2_IOCTL_REBOOT:
7565 return scarlett2_reboot(mixer);
7567 case SCARLETT2_IOCTL_SELECT_FLASH_SEGMENT:
7568 return scarlett2_ioctl_select_flash_segment(mixer, arg);
7570 case SCARLETT2_IOCTL_ERASE_FLASH_SEGMENT:
7571 return scarlett2_ioctl_erase_flash_segment(mixer);
7573 case SCARLETT2_IOCTL_GET_ERASE_PROGRESS:
7574 return scarlett2_ioctl_get_erase_progress(mixer, arg);
7577 return -ENOIOCTLCMD;
7581 static long scarlett2_hwdep_write(struct snd_hwdep *hw,
7582 const char __user *buf,
7583 long count, loff_t *offset)
7585 struct usb_mixer_interface *mixer = hw->private_data;
7586 struct scarlett2_data *private = mixer->private_data;
7587 int segment_id, segment_num, err, len;
7590 /* SCARLETT2_USB_WRITE_SEGMENT request data */
7598 /* Calculate the maximum permitted in data[] */
7599 const size_t max_data_size = SCARLETT2_FLASH_WRITE_MAX -
7600 offsetof(typeof(*req), data);
7602 /* If erasing, wait for it to complete */
7603 if (private->flash_write_state ==
7604 SCARLETT2_FLASH_WRITE_STATE_ERASING) {
7605 err = scarlett2_wait_for_erase(mixer);
7608 private->flash_write_state = SCARLETT2_FLASH_WRITE_STATE_WRITE;
7610 /* Check that an erase has been done & completed */
7611 } else if (private->flash_write_state !=
7612 SCARLETT2_FLASH_WRITE_STATE_WRITE) {
7616 /* Check that we're writing to the upgrade firmware */
7617 segment_id = private->selected_flash_segment_id;
7618 if (segment_id != SCARLETT2_SEGMENT_ID_FIRMWARE)
7621 segment_num = private->flash_segment_nums[segment_id];
7622 if (segment_num < SCARLETT2_SEGMENT_NUM_MIN ||
7623 segment_num > SCARLETT2_SEGMENT_NUM_MAX)
7626 /* Validate the offset and count */
7627 flash_size = private->flash_segment_blocks[segment_id] *
7628 SCARLETT2_FLASH_BLOCK_SIZE;
7630 if (count < 0 || *offset < 0 || *offset + count >= flash_size)
7636 /* Limit the *req size to SCARLETT2_FLASH_WRITE_MAX */
7637 if (count > max_data_size)
7638 count = max_data_size;
7640 /* Create and send the request */
7641 len = struct_size(req, data, count);
7642 req = kzalloc(len, GFP_KERNEL);
7646 req->segment_num = cpu_to_le32(segment_num);
7647 req->offset = cpu_to_le32(*offset);
7650 if (copy_from_user(req->data, buf, count)) {
7655 err = scarlett2_usb(mixer, SCARLETT2_USB_WRITE_SEGMENT,
7668 static int scarlett2_hwdep_release(struct snd_hwdep *hw, struct file *file)
7670 struct usb_mixer_interface *mixer = hw->private_data;
7671 struct scarlett2_data *private = mixer->private_data;
7673 /* Return from the SELECTED or WRITE state to IDLE.
7674 * The ERASING state is left as-is, and checked on next open.
7677 private->hwdep_in_use &&
7678 private->flash_write_state != SCARLETT2_FLASH_WRITE_STATE_ERASING)
7679 private->flash_write_state = SCARLETT2_FLASH_WRITE_STATE_IDLE;
7684 static int scarlett2_hwdep_init(struct usb_mixer_interface *mixer)
7686 struct snd_hwdep *hw;
7689 err = snd_hwdep_new(mixer->chip->card, "Focusrite Control", 0, &hw);
7693 hw->private_data = mixer;
7695 hw->ops.open = scarlett2_hwdep_open;
7696 hw->ops.ioctl = scarlett2_hwdep_ioctl;
7697 hw->ops.write = scarlett2_hwdep_write;
7698 hw->ops.release = scarlett2_hwdep_release;
7703 int snd_scarlett2_init(struct usb_mixer_interface *mixer)
7705 struct snd_usb_audio *chip = mixer->chip;
7706 const struct scarlett2_device_entry *entry;
7709 /* only use UAC_VERSION_2 */
7710 if (!mixer->protocol)
7713 /* find entry in scarlett2_devices */
7714 entry = get_scarlett2_device_entry(mixer);
7716 usb_audio_err(mixer->chip,
7717 "%s: missing device entry for %04x:%04x\n",
7719 USB_ID_VENDOR(chip->usb_id),
7720 USB_ID_PRODUCT(chip->usb_id));
7724 if (chip->setup & SCARLETT2_DISABLE) {
7725 usb_audio_info(chip,
7726 "Focusrite %s Mixer Driver disabled "
7727 "by modprobe options (snd_usb_audio "
7728 "vid=0x%04x pid=0x%04x device_setup=%d)\n",
7730 USB_ID_VENDOR(chip->usb_id),
7731 USB_ID_PRODUCT(chip->usb_id),
7736 usb_audio_info(chip,
7737 "Focusrite %s Mixer Driver enabled (pid=0x%04x); "
7738 "report any issues to "
7739 "https://github.com/geoffreybennett/scarlett-gen2/issues",
7741 USB_ID_PRODUCT(chip->usb_id));
7743 err = snd_scarlett2_controls_create(mixer, entry);
7745 usb_audio_err(mixer->chip,
7746 "Error initialising %s Mixer Driver: %d",
7752 err = scarlett2_hwdep_init(mixer);
7754 usb_audio_err(mixer->chip,
7755 "Error creating %s hwdep device: %d",