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
176 #define SCARLETT2_GAIN_BIAS 70
178 /* mixer range from -80dB to +6dB in 0.5dB steps */
179 #define SCARLETT2_MIXER_MIN_DB -80
180 #define SCARLETT2_MIXER_BIAS (-SCARLETT2_MIXER_MIN_DB * 2)
181 #define SCARLETT2_MIXER_MAX_DB 6
182 #define SCARLETT2_MIXER_MAX_VALUE \
183 ((SCARLETT2_MIXER_MAX_DB - SCARLETT2_MIXER_MIN_DB) * 2)
184 #define SCARLETT2_MIXER_VALUE_COUNT (SCARLETT2_MIXER_MAX_VALUE + 1)
186 /* map from (dB + 80) * 2 to mixer value
187 * for dB in 0 .. 172: int(8192 * pow(10, ((dB - 160) / 2 / 20)))
189 static const u16 scarlett2_mixer_values[SCARLETT2_MIXER_VALUE_COUNT] = {
190 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2,
191 2, 2, 3, 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 6, 6, 6, 7, 7, 8, 8,
192 9, 9, 10, 10, 11, 12, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21,
193 23, 24, 25, 27, 29, 30, 32, 34, 36, 38, 41, 43, 46, 48, 51,
194 54, 57, 61, 65, 68, 73, 77, 81, 86, 91, 97, 103, 109, 115,
195 122, 129, 137, 145, 154, 163, 173, 183, 194, 205, 217, 230,
196 244, 259, 274, 290, 307, 326, 345, 365, 387, 410, 434, 460,
197 487, 516, 547, 579, 614, 650, 689, 730, 773, 819, 867, 919,
198 973, 1031, 1092, 1157, 1225, 1298, 1375, 1456, 1543, 1634,
199 1731, 1833, 1942, 2057, 2179, 2308, 2445, 2590, 2744, 2906,
200 3078, 3261, 3454, 3659, 3876, 4105, 4349, 4606, 4879, 5168,
201 5475, 5799, 6143, 6507, 6892, 7301, 7733, 8192, 8677, 9191,
202 9736, 10313, 10924, 11571, 12257, 12983, 13752, 14567, 15430,
206 /* Maximum number of analogue outputs */
207 #define SCARLETT2_ANALOGUE_MAX 10
209 /* Maximum number of various input controls */
210 #define SCARLETT2_LEVEL_SWITCH_MAX 2
211 #define SCARLETT2_PAD_SWITCH_MAX 8
212 #define SCARLETT2_AIR_SWITCH_MAX 8
213 #define SCARLETT2_PHANTOM_SWITCH_MAX 2
214 #define SCARLETT2_INPUT_GAIN_MAX 2
216 /* Maximum number of inputs to the mixer */
217 #define SCARLETT2_INPUT_MIX_MAX 25
219 /* Maximum number of outputs from the mixer */
220 #define SCARLETT2_OUTPUT_MIX_MAX 12
222 /* Maximum number of mixer gain controls */
223 #define SCARLETT2_MIX_MAX (SCARLETT2_INPUT_MIX_MAX * SCARLETT2_OUTPUT_MIX_MAX)
225 /* Maximum number of direct monitor mixer gain controls
226 * 1 (Solo) or 2 (2i2) direct monitor selections (Mono & Stereo)
227 * 2 Mix outputs (A/Left & B/Right)
230 #define SCARLETT2_MONITOR_MIX_MAX (2 * 2 * 4)
232 /* Maximum size of the data in the USB mux assignment message:
233 * 20 inputs, 20 outputs, 25 matrix inputs, 12 spare
235 #define SCARLETT2_MUX_MAX 77
237 /* Maximum number of sources (sum of input port counts) */
238 #define SCARLETT2_MAX_SRCS 52
240 /* Maximum number of meters (sum of output port counts) */
241 #define SCARLETT2_MAX_METERS 65
243 /* Hardware port types:
244 * - None (no input to mux)
252 SCARLETT2_PORT_TYPE_NONE,
253 SCARLETT2_PORT_TYPE_ANALOGUE,
254 SCARLETT2_PORT_TYPE_SPDIF,
255 SCARLETT2_PORT_TYPE_ADAT,
256 SCARLETT2_PORT_TYPE_MIX,
257 SCARLETT2_PORT_TYPE_PCM,
258 SCARLETT2_PORT_TYPE_COUNT
261 /* I/O count of each port type kept in struct scarlett2_ports */
268 /* Dim/Mute buttons on the 18i20 */
270 SCARLETT2_BUTTON_MUTE,
271 SCARLETT2_BUTTON_DIM,
272 SCARLETT2_DIM_MUTE_COUNT
275 /* Flash Write State */
277 SCARLETT2_FLASH_WRITE_STATE_IDLE,
278 SCARLETT2_FLASH_WRITE_STATE_SELECTED,
279 SCARLETT2_FLASH_WRITE_STATE_ERASING,
280 SCARLETT2_FLASH_WRITE_STATE_WRITE
283 static const char *const scarlett2_dim_mute_names[SCARLETT2_DIM_MUTE_COUNT] = {
284 "Mute Playback Switch", "Dim Playback Switch"
287 /* The autogain_status is set based on the autogain_switch and
288 * raw_autogain_status values.
290 * If autogain_switch is set, autogain_status is set to 0 (Running).
291 * The other status values are from the raw_autogain_status value + 1.
293 static const char *const scarlett2_autogain_status_texts[] = {
305 /* Power Status Values */
307 SCARLETT2_POWER_STATUS_EXT,
308 SCARLETT2_POWER_STATUS_BUS,
309 SCARLETT2_POWER_STATUS_FAIL,
310 SCARLETT2_POWER_STATUS_COUNT
313 /* Notification callback functions */
314 struct scarlett2_notification {
316 void (*func)(struct usb_mixer_interface *mixer);
319 static void scarlett2_notify_sync(struct usb_mixer_interface *mixer);
320 static void scarlett2_notify_dim_mute(struct usb_mixer_interface *mixer);
321 static void scarlett2_notify_monitor(struct usb_mixer_interface *mixer);
322 static void scarlett2_notify_volume(struct usb_mixer_interface *mixer);
323 static void scarlett2_notify_input_level(struct usb_mixer_interface *mixer);
324 static void scarlett2_notify_input_pad(struct usb_mixer_interface *mixer);
325 static void scarlett2_notify_input_air(struct usb_mixer_interface *mixer);
326 static void scarlett2_notify_input_phantom(struct usb_mixer_interface *mixer);
327 static void scarlett2_notify_input_other(struct usb_mixer_interface *mixer);
328 static void scarlett2_notify_input_select(struct usb_mixer_interface *mixer);
329 static void scarlett2_notify_input_gain(struct usb_mixer_interface *mixer);
330 static void scarlett2_notify_autogain(struct usb_mixer_interface *mixer);
331 static void scarlett2_notify_input_safe(struct usb_mixer_interface *mixer);
332 static void scarlett2_notify_monitor_other(struct usb_mixer_interface *mixer);
333 static void scarlett2_notify_direct_monitor(struct usb_mixer_interface *mixer);
334 static void scarlett2_notify_power_status(struct usb_mixer_interface *mixer);
335 static void scarlett2_notify_pcm_input_switch(
336 struct usb_mixer_interface *mixer);
338 /* Arrays of notification callback functions */
340 static const struct scarlett2_notification scarlett2_notifications[] = {
341 { 0x00000001, NULL }, /* ack, gets ignored */
342 { 0x00000008, scarlett2_notify_sync },
343 { 0x00200000, scarlett2_notify_dim_mute },
344 { 0x00400000, scarlett2_notify_monitor },
345 { 0x00800000, scarlett2_notify_input_other },
346 { 0x01000000, scarlett2_notify_monitor_other },
350 static const struct scarlett2_notification scarlett3a_notifications[] = {
351 { 0x00000001, NULL }, /* ack, gets ignored */
352 { 0x00800000, scarlett2_notify_input_other },
353 { 0x01000000, scarlett2_notify_direct_monitor },
357 static const struct scarlett2_notification scarlett4_solo_notifications[] = {
358 { 0x00000001, NULL }, /* ack, gets ignored */
359 { 0x00000008, scarlett2_notify_sync },
360 { 0x00400000, scarlett2_notify_input_air },
361 { 0x00800000, scarlett2_notify_direct_monitor },
362 { 0x01000000, scarlett2_notify_input_level },
363 { 0x02000000, scarlett2_notify_input_phantom },
364 { 0x04000000, scarlett2_notify_pcm_input_switch },
368 static const struct scarlett2_notification scarlett4_2i2_notifications[] = {
369 { 0x00000001, NULL }, /* ack, gets ignored */
370 { 0x00000008, scarlett2_notify_sync },
371 { 0x00200000, scarlett2_notify_input_safe },
372 { 0x00400000, scarlett2_notify_autogain },
373 { 0x00800000, scarlett2_notify_input_air },
374 { 0x01000000, scarlett2_notify_direct_monitor },
375 { 0x02000000, scarlett2_notify_input_select },
376 { 0x04000000, scarlett2_notify_input_level },
377 { 0x08000000, scarlett2_notify_input_phantom },
378 { 0x10000000, NULL }, /* power status, ignored */
379 { 0x40000000, scarlett2_notify_input_gain },
380 { 0x80000000, NULL }, /* power status, ignored */
384 static const struct scarlett2_notification scarlett4_4i4_notifications[] = {
385 { 0x00000001, NULL }, /* ack, gets ignored */
386 { 0x00000008, scarlett2_notify_sync },
387 { 0x00200000, scarlett2_notify_input_safe },
388 { 0x00400000, scarlett2_notify_autogain },
389 { 0x00800000, scarlett2_notify_input_air },
390 { 0x01000000, scarlett2_notify_input_select },
391 { 0x02000000, scarlett2_notify_input_level },
392 { 0x04000000, scarlett2_notify_input_phantom },
393 { 0x08000000, scarlett2_notify_power_status }, /* power external */
394 { 0x20000000, scarlett2_notify_input_gain },
395 { 0x40000000, scarlett2_notify_power_status }, /* power status */
396 { 0x80000000, scarlett2_notify_volume },
400 /* Configuration parameters that can be read and written */
402 SCARLETT2_CONFIG_DIM_MUTE,
403 SCARLETT2_CONFIG_LINE_OUT_VOLUME,
404 SCARLETT2_CONFIG_MUTE_SWITCH,
405 SCARLETT2_CONFIG_SW_HW_SWITCH,
406 SCARLETT2_CONFIG_MASTER_VOLUME,
407 SCARLETT2_CONFIG_HEADPHONE_VOLUME,
408 SCARLETT2_CONFIG_LEVEL_SWITCH,
409 SCARLETT2_CONFIG_PAD_SWITCH,
410 SCARLETT2_CONFIG_MSD_SWITCH,
411 SCARLETT2_CONFIG_AIR_SWITCH,
412 SCARLETT2_CONFIG_STANDALONE_SWITCH,
413 SCARLETT2_CONFIG_PHANTOM_SWITCH,
414 SCARLETT2_CONFIG_PHANTOM_PERSISTENCE,
415 SCARLETT2_CONFIG_DIRECT_MONITOR,
416 SCARLETT2_CONFIG_MONITOR_OTHER_SWITCH,
417 SCARLETT2_CONFIG_MONITOR_OTHER_ENABLE,
418 SCARLETT2_CONFIG_TALKBACK_MAP,
419 SCARLETT2_CONFIG_AUTOGAIN_SWITCH,
420 SCARLETT2_CONFIG_AUTOGAIN_STATUS,
421 SCARLETT2_CONFIG_INPUT_GAIN,
422 SCARLETT2_CONFIG_SAFE_SWITCH,
423 SCARLETT2_CONFIG_INPUT_SELECT_SWITCH,
424 SCARLETT2_CONFIG_INPUT_LINK_SWITCH,
425 SCARLETT2_CONFIG_POWER_EXT,
426 SCARLETT2_CONFIG_POWER_LOW,
427 SCARLETT2_CONFIG_PCM_INPUT_SWITCH,
428 SCARLETT2_CONFIG_DIRECT_MONITOR_GAIN,
429 SCARLETT2_CONFIG_COUNT
432 /* Location, size, and activation command number for the configuration
433 * parameters. Size is in bits and may be 0, 1, 8, or 16.
435 * A size of 0 indicates that the parameter is a byte-sized Scarlett
436 * Gen 4 configuration which is written through the gen4_write_addr
437 * location (but still read through the given offset location).
439 * Some Gen 4 configuration parameters are written with 0x02 for a
440 * desired value of 0x01, and 0x03 for 0x00. These are indicated with
441 * mute set to 1. 0x02 and 0x03 are temporary values while the device
442 * makes the change and the channel and/or corresponding DSP channel
445 struct scarlett2_config {
452 struct scarlett2_config_set {
453 const struct scarlett2_notification *notifications;
455 const struct scarlett2_config items[SCARLETT2_CONFIG_COUNT];
458 /* Gen 2 devices without SW/HW volume switch: 6i6, 18i8 */
460 static const struct scarlett2_config_set scarlett2_config_set_gen2a = {
461 .notifications = scarlett2_notifications,
463 [SCARLETT2_CONFIG_LINE_OUT_VOLUME] = {
464 .offset = 0x34, .size = 16, .activate = 1 },
466 [SCARLETT2_CONFIG_MUTE_SWITCH] = {
467 .offset = 0x5c, .size = 8, .activate = 1 },
469 [SCARLETT2_CONFIG_LEVEL_SWITCH] = {
470 .offset = 0x7c, .size = 8, .activate = 7 },
472 [SCARLETT2_CONFIG_PAD_SWITCH] = {
473 .offset = 0x84, .size = 8, .activate = 8 },
475 [SCARLETT2_CONFIG_STANDALONE_SWITCH] = {
476 .offset = 0x8d, .size = 8, .activate = 6 },
480 /* Gen 2 devices with SW/HW volume switch: 18i20 */
482 static const struct scarlett2_config_set scarlett2_config_set_gen2b = {
483 .notifications = scarlett2_notifications,
485 [SCARLETT2_CONFIG_DIM_MUTE] = {
486 .offset = 0x31, .size = 8, .activate = 2 },
488 [SCARLETT2_CONFIG_LINE_OUT_VOLUME] = {
489 .offset = 0x34, .size = 16, .activate = 1 },
491 [SCARLETT2_CONFIG_MUTE_SWITCH] = {
492 .offset = 0x5c, .size = 8, .activate = 1 },
494 [SCARLETT2_CONFIG_SW_HW_SWITCH] = {
495 .offset = 0x66, .size = 8, .activate = 3 },
497 [SCARLETT2_CONFIG_MASTER_VOLUME] = {
498 .offset = 0x76, .size = 16 },
500 [SCARLETT2_CONFIG_LEVEL_SWITCH] = {
501 .offset = 0x7c, .size = 8, .activate = 7 },
503 [SCARLETT2_CONFIG_PAD_SWITCH] = {
504 .offset = 0x84, .size = 8, .activate = 8 },
506 [SCARLETT2_CONFIG_STANDALONE_SWITCH] = {
507 .offset = 0x8d, .size = 8, .activate = 6 },
511 /* Gen 3 devices without a mixer (Solo and 2i2) */
512 static const struct scarlett2_config_set scarlett2_config_set_gen3a = {
513 .notifications = scarlett3a_notifications,
515 [SCARLETT2_CONFIG_MSD_SWITCH] = {
516 .offset = 0x04, .size = 8, .activate = 6 },
518 [SCARLETT2_CONFIG_PHANTOM_PERSISTENCE] = {
519 .offset = 0x05, .size = 8, .activate = 6 },
521 [SCARLETT2_CONFIG_PHANTOM_SWITCH] = {
522 .offset = 0x06, .size = 8, .activate = 3 },
524 [SCARLETT2_CONFIG_DIRECT_MONITOR] = {
525 .offset = 0x07, .size = 8, .activate = 4 },
527 [SCARLETT2_CONFIG_LEVEL_SWITCH] = {
528 .offset = 0x08, .size = 1, .activate = 7 },
530 [SCARLETT2_CONFIG_AIR_SWITCH] = {
531 .offset = 0x09, .size = 1, .activate = 8 },
535 /* Gen 3 devices without SW/HW volume switch: 4i4, 8i6 */
536 static const struct scarlett2_config_set scarlett2_config_set_gen3b = {
537 .notifications = scarlett2_notifications,
539 [SCARLETT2_CONFIG_LINE_OUT_VOLUME] = {
540 .offset = 0x34, .size = 16, .activate = 1 },
542 [SCARLETT2_CONFIG_MUTE_SWITCH] = {
543 .offset = 0x5c, .size = 8, .activate = 1 },
545 [SCARLETT2_CONFIG_LEVEL_SWITCH] = {
546 .offset = 0x7c, .size = 8, .activate = 7 },
548 [SCARLETT2_CONFIG_PAD_SWITCH] = {
549 .offset = 0x84, .size = 8, .activate = 8 },
551 [SCARLETT2_CONFIG_AIR_SWITCH] = {
552 .offset = 0x8c, .size = 8, .activate = 8 },
554 [SCARLETT2_CONFIG_STANDALONE_SWITCH] = {
555 .offset = 0x95, .size = 8, .activate = 6 },
557 [SCARLETT2_CONFIG_PHANTOM_SWITCH] = {
558 .offset = 0x9c, .size = 1, .activate = 8 },
560 [SCARLETT2_CONFIG_MSD_SWITCH] = {
561 .offset = 0x9d, .size = 8, .activate = 6 },
563 [SCARLETT2_CONFIG_PHANTOM_PERSISTENCE] = {
564 .offset = 0x9e, .size = 8, .activate = 6 },
568 /* Gen 3 devices with SW/HW volume switch: 18i8, 18i20 */
569 static const struct scarlett2_config_set scarlett2_config_set_gen3c = {
570 .notifications = scarlett2_notifications,
572 [SCARLETT2_CONFIG_DIM_MUTE] = {
573 .offset = 0x31, .size = 8, .activate = 2 },
575 [SCARLETT2_CONFIG_LINE_OUT_VOLUME] = {
576 .offset = 0x34, .size = 16, .activate = 1 },
578 [SCARLETT2_CONFIG_MUTE_SWITCH] = {
579 .offset = 0x5c, .size = 8, .activate = 1 },
581 [SCARLETT2_CONFIG_SW_HW_SWITCH] = {
582 .offset = 0x66, .size = 8, .activate = 3 },
584 [SCARLETT2_CONFIG_MASTER_VOLUME] = {
585 .offset = 0x76, .size = 16 },
587 [SCARLETT2_CONFIG_LEVEL_SWITCH] = {
588 .offset = 0x7c, .size = 8, .activate = 7 },
590 [SCARLETT2_CONFIG_PAD_SWITCH] = {
591 .offset = 0x84, .size = 8, .activate = 8 },
593 [SCARLETT2_CONFIG_AIR_SWITCH] = {
594 .offset = 0x8c, .size = 8, .activate = 8 },
596 [SCARLETT2_CONFIG_STANDALONE_SWITCH] = {
597 .offset = 0x95, .size = 8, .activate = 6 },
599 [SCARLETT2_CONFIG_PHANTOM_SWITCH] = {
600 .offset = 0x9c, .size = 1, .activate = 8 },
602 [SCARLETT2_CONFIG_MSD_SWITCH] = {
603 .offset = 0x9d, .size = 8, .activate = 6 },
605 [SCARLETT2_CONFIG_PHANTOM_PERSISTENCE] = {
606 .offset = 0x9e, .size = 8, .activate = 6 },
608 [SCARLETT2_CONFIG_MONITOR_OTHER_SWITCH] = {
609 .offset = 0x9f, .size = 1, .activate = 10 },
611 [SCARLETT2_CONFIG_MONITOR_OTHER_ENABLE] = {
612 .offset = 0xa0, .size = 1, .activate = 10 },
614 [SCARLETT2_CONFIG_TALKBACK_MAP] = {
615 .offset = 0xb0, .size = 16, .activate = 10 },
620 static const struct scarlett2_config_set scarlett2_config_set_gen4_solo = {
621 .notifications = scarlett4_solo_notifications,
622 .gen4_write_addr = 0xd8,
624 [SCARLETT2_CONFIG_MSD_SWITCH] = {
625 .offset = 0x47, .size = 8, .activate = 4 },
627 [SCARLETT2_CONFIG_DIRECT_MONITOR] = {
628 .offset = 0x108, .activate = 12 },
630 [SCARLETT2_CONFIG_PHANTOM_SWITCH] = {
631 .offset = 0x46, .activate = 9, .mute = 1 },
633 [SCARLETT2_CONFIG_LEVEL_SWITCH] = {
634 .offset = 0x3d, .activate = 10, .mute = 1 },
636 [SCARLETT2_CONFIG_AIR_SWITCH] = {
637 .offset = 0x3e, .activate = 11 },
639 [SCARLETT2_CONFIG_PCM_INPUT_SWITCH] = {
640 .offset = 0x206, .activate = 25 },
642 [SCARLETT2_CONFIG_DIRECT_MONITOR_GAIN] = {
643 .offset = 0x232, .size = 16, .activate = 26 }
648 static const struct scarlett2_config_set scarlett2_config_set_gen4_2i2 = {
649 .notifications = scarlett4_2i2_notifications,
650 .gen4_write_addr = 0xfc,
652 [SCARLETT2_CONFIG_MSD_SWITCH] = {
653 .offset = 0x49, .size = 8, .activate = 4 }, // 0x41 ??
655 [SCARLETT2_CONFIG_DIRECT_MONITOR] = {
656 .offset = 0x14a, .activate = 16 },
658 [SCARLETT2_CONFIG_AUTOGAIN_SWITCH] = {
659 .offset = 0x135, .activate = 10 },
661 [SCARLETT2_CONFIG_AUTOGAIN_STATUS] = {
664 [SCARLETT2_CONFIG_PHANTOM_SWITCH] = {
665 .offset = 0x48, .activate = 11, .mute = 1 },
667 [SCARLETT2_CONFIG_INPUT_GAIN] = {
668 .offset = 0x4b, .activate = 12 },
670 [SCARLETT2_CONFIG_LEVEL_SWITCH] = {
671 .offset = 0x3c, .activate = 13, .mute = 1 },
673 [SCARLETT2_CONFIG_SAFE_SWITCH] = {
674 .offset = 0x147, .activate = 14 },
676 [SCARLETT2_CONFIG_AIR_SWITCH] = {
677 .offset = 0x3e, .activate = 15 },
679 [SCARLETT2_CONFIG_INPUT_SELECT_SWITCH] = {
680 .offset = 0x14b, .activate = 17 },
682 [SCARLETT2_CONFIG_INPUT_LINK_SWITCH] = {
683 .offset = 0x14e, .activate = 18 },
685 [SCARLETT2_CONFIG_DIRECT_MONITOR_GAIN] = {
686 .offset = 0x2a0, .size = 16, .activate = 36 }
691 static const struct scarlett2_config_set scarlett2_config_set_gen4_4i4 = {
692 .notifications = scarlett4_4i4_notifications,
693 .gen4_write_addr = 0x130,
695 [SCARLETT2_CONFIG_MSD_SWITCH] = {
696 .offset = 0x5c, .size = 8, .activate = 4 },
698 [SCARLETT2_CONFIG_AUTOGAIN_SWITCH] = {
699 .offset = 0x13e, .activate = 10 },
701 [SCARLETT2_CONFIG_AUTOGAIN_STATUS] = {
704 [SCARLETT2_CONFIG_PHANTOM_SWITCH] = {
705 .offset = 0x5a, .activate = 11, .mute = 1 },
707 [SCARLETT2_CONFIG_INPUT_GAIN] = {
708 .offset = 0x5e, .activate = 12 },
710 [SCARLETT2_CONFIG_LEVEL_SWITCH] = {
711 .offset = 0x4e, .activate = 13, .mute = 1 },
713 [SCARLETT2_CONFIG_SAFE_SWITCH] = {
714 .offset = 0x150, .activate = 14 },
716 [SCARLETT2_CONFIG_AIR_SWITCH] = {
717 .offset = 0x50, .activate = 15 },
719 [SCARLETT2_CONFIG_INPUT_SELECT_SWITCH] = {
720 .offset = 0x153, .activate = 16 },
722 [SCARLETT2_CONFIG_INPUT_LINK_SWITCH] = {
723 .offset = 0x156, .activate = 17 },
725 [SCARLETT2_CONFIG_MASTER_VOLUME] = {
726 .offset = 0x32, .size = 16 },
728 [SCARLETT2_CONFIG_HEADPHONE_VOLUME] = {
729 .offset = 0x3a, .size = 16 },
731 [SCARLETT2_CONFIG_POWER_EXT] = {
734 [SCARLETT2_CONFIG_POWER_LOW] = {
739 /* Clarett USB and Clarett+ devices: 2Pre, 4Pre, 8Pre */
740 static const struct scarlett2_config_set scarlett2_config_set_clarett = {
741 .notifications = scarlett2_notifications,
743 [SCARLETT2_CONFIG_DIM_MUTE] = {
744 .offset = 0x31, .size = 8, .activate = 2 },
746 [SCARLETT2_CONFIG_LINE_OUT_VOLUME] = {
747 .offset = 0x34, .size = 16, .activate = 1 },
749 [SCARLETT2_CONFIG_MUTE_SWITCH] = {
750 .offset = 0x5c, .size = 8, .activate = 1 },
752 [SCARLETT2_CONFIG_SW_HW_SWITCH] = {
753 .offset = 0x66, .size = 8, .activate = 3 },
755 [SCARLETT2_CONFIG_MASTER_VOLUME] = {
756 .offset = 0x76, .size = 16 },
758 [SCARLETT2_CONFIG_LEVEL_SWITCH] = {
759 .offset = 0x7c, .size = 8, .activate = 7 },
761 [SCARLETT2_CONFIG_AIR_SWITCH] = {
762 .offset = 0x95, .size = 8, .activate = 8 },
764 [SCARLETT2_CONFIG_STANDALONE_SWITCH] = {
765 .offset = 0x8d, .size = 8, .activate = 6 },
769 /* Description of each hardware port type:
770 * - id: hardware ID of this port type
771 * - src_descr: printf format string for mux input selections
772 * - src_num_offset: added to channel number for the fprintf
773 * - dst_descr: printf format string for mixer controls
775 struct scarlett2_port {
777 const char * const src_descr;
779 const char * const dst_descr;
780 const char * const dsp_src_descr;
781 const char * const dsp_dst_descr;
784 static const struct scarlett2_port scarlett2_ports[SCARLETT2_PORT_TYPE_COUNT] = {
785 [SCARLETT2_PORT_TYPE_NONE] = {
789 [SCARLETT2_PORT_TYPE_ANALOGUE] = {
791 .src_descr = "Analogue %d",
793 .dst_descr = "Analogue Output %02d Playback"
795 [SCARLETT2_PORT_TYPE_SPDIF] = {
797 .src_descr = "S/PDIF %d",
799 .dst_descr = "S/PDIF Output %d Playback"
801 [SCARLETT2_PORT_TYPE_ADAT] = {
803 .src_descr = "ADAT %d",
805 .dst_descr = "ADAT Output %d Playback"
807 [SCARLETT2_PORT_TYPE_MIX] = {
809 .src_descr = "Mix %c",
810 .src_num_offset = 'A',
811 .dst_descr = "Mixer Input %02d Capture",
812 .dsp_src_descr = "DSP %d",
813 .dsp_dst_descr = "DSP Input %d Capture"
815 [SCARLETT2_PORT_TYPE_PCM] = {
817 .src_descr = "PCM %d",
819 .dst_descr = "PCM %02d Capture"
823 /* Number of mux tables: one for each band of sample rates
824 * (44.1/48kHz, 88.2/96kHz, and 176.4/176kHz)
826 #define SCARLETT2_MUX_TABLES 3
828 /* Maximum number of entries in a mux table */
829 #define SCARLETT2_MAX_MUX_ENTRIES 10
831 /* One entry within mux_assignment defines the port type and range of
832 * ports to add to the set_mux message. The end of the list is marked
835 struct scarlett2_mux_entry {
841 /* Maximum number of entries in a mux table */
842 #define SCARLETT2_MAX_METER_ENTRIES 9
844 /* One entry within meter_assignment defines the range of mux outputs
845 * that consecutive meter entries are mapped to. The end of the list
846 * is marked with count == 0.
848 struct scarlett2_meter_entry {
853 struct scarlett2_device_info {
854 /* which set of configuration parameters the device uses */
855 const struct scarlett2_config_set *config_set;
857 /* minimum firmware version required */
858 u16 min_firmware_version;
860 /* support for main/alt speaker switching */
861 u8 has_speaker_switching;
863 /* support for talkback microphone */
866 /* the number of analogue inputs with a software switchable
867 * level control that can be set to line or instrument
869 u8 level_input_count;
871 /* the first input with a level control (0-based) */
872 u8 level_input_first;
874 /* the number of analogue inputs with a software switchable
879 /* the number of analogue inputs with a software switchable
884 /* the first input with an air control (0-based) */
887 /* number of additional air options
888 * 0 for air presence only (Gen 3)
889 * 1 for air presence+drive (Gen 4)
893 /* the number of phantom (48V) software switchable controls */
896 /* the first input with phantom power control (0-based) */
899 /* the number of inputs each phantom switch controls */
900 u8 inputs_per_phantom;
902 /* the number of inputs with software-controllable gain */
905 /* the number of direct monitor options
906 * (0 = none, 1 = mono only, 2 = mono/stereo)
910 /* the number of DSP channels */
913 /* remap analogue outputs; 18i8 Gen 3 has "line 3/4" connected
914 * internally to the analogue 7/8 outputs
916 u8 line_out_remap_enable;
917 u8 line_out_remap[SCARLETT2_ANALOGUE_MAX];
918 u8 line_out_unmap[SCARLETT2_ANALOGUE_MAX];
920 /* additional description for the line out volume controls */
921 const char * const line_out_descrs[SCARLETT2_ANALOGUE_MAX];
923 /* number of sources/destinations of each port type */
924 const int port_count[SCARLETT2_PORT_TYPE_COUNT][SCARLETT2_PORT_DIRNS];
926 /* layout/order of the entries in the set_mux message */
927 struct scarlett2_mux_entry mux_assignment[SCARLETT2_MUX_TABLES]
928 [SCARLETT2_MAX_MUX_ENTRIES];
930 /* map from meter level order returned by
931 * SCARLETT2_USB_GET_METER to index into mux[] entries (same
932 * as the order returned by scarlett2_meter_ctl_get())
934 struct scarlett2_meter_entry meter_map[SCARLETT2_MAX_METER_ENTRIES];
937 struct scarlett2_data {
938 struct usb_mixer_interface *mixer;
939 struct mutex usb_mutex; /* prevent sending concurrent USB requests */
940 struct mutex data_mutex; /* lock access to this data */
942 u8 selected_flash_segment_id;
943 u8 flash_write_state;
944 struct delayed_work work;
945 const struct scarlett2_device_info *info;
946 const struct scarlett2_config_set *config_set;
947 const char *series_name;
948 __u8 bInterfaceNumber;
949 __u8 bEndpointAddress;
950 __u16 wMaxPacketSize;
957 u8 num_monitor_mix_ctls;
958 u32 firmware_version;
959 u8 flash_segment_nums[SCARLETT2_SEGMENT_ID_COUNT];
960 u8 flash_segment_blocks[SCARLETT2_SEGMENT_ID_COUNT];
965 u8 input_level_updated;
966 u8 input_pad_updated;
967 u8 input_air_updated;
968 u8 input_phantom_updated;
969 u8 input_select_updated;
970 u8 input_gain_updated;
972 u8 input_safe_updated;
973 u8 pcm_input_switch_updated;
974 u8 monitor_other_updated;
975 u8 direct_monitor_updated;
978 u8 speaker_switching_switched;
979 u8 power_status_updated;
983 u8 vol[SCARLETT2_ANALOGUE_MAX];
984 u8 vol_sw_hw_switch[SCARLETT2_ANALOGUE_MAX];
985 u8 mute_switch[SCARLETT2_ANALOGUE_MAX];
986 u8 level_switch[SCARLETT2_LEVEL_SWITCH_MAX];
987 u8 pad_switch[SCARLETT2_PAD_SWITCH_MAX];
988 u8 dim_mute[SCARLETT2_DIM_MUTE_COUNT];
989 u8 air_switch[SCARLETT2_AIR_SWITCH_MAX];
990 u8 phantom_switch[SCARLETT2_PHANTOM_SWITCH_MAX];
991 u8 phantom_persistence;
992 u8 input_select_switch;
993 u8 input_link_switch[SCARLETT2_INPUT_GAIN_MAX / 2];
994 u8 gain[SCARLETT2_INPUT_GAIN_MAX];
995 u8 autogain_switch[SCARLETT2_INPUT_GAIN_MAX];
996 u8 autogain_status[SCARLETT2_INPUT_GAIN_MAX];
997 u8 safe_switch[SCARLETT2_INPUT_GAIN_MAX];
999 u8 direct_monitor_switch;
1000 u8 speaker_switching_switch;
1002 u8 talkback_map[SCARLETT2_OUTPUT_MIX_MAX];
1004 u8 standalone_switch;
1006 u8 meter_level_map[SCARLETT2_MAX_METERS];
1007 struct snd_kcontrol *sync_ctl;
1008 struct snd_kcontrol *master_vol_ctl;
1009 struct snd_kcontrol *headphone_vol_ctl;
1010 struct snd_kcontrol *vol_ctls[SCARLETT2_ANALOGUE_MAX];
1011 struct snd_kcontrol *sw_hw_ctls[SCARLETT2_ANALOGUE_MAX];
1012 struct snd_kcontrol *mute_ctls[SCARLETT2_ANALOGUE_MAX];
1013 struct snd_kcontrol *dim_mute_ctls[SCARLETT2_DIM_MUTE_COUNT];
1014 struct snd_kcontrol *level_ctls[SCARLETT2_LEVEL_SWITCH_MAX];
1015 struct snd_kcontrol *pad_ctls[SCARLETT2_PAD_SWITCH_MAX];
1016 struct snd_kcontrol *air_ctls[SCARLETT2_AIR_SWITCH_MAX];
1017 struct snd_kcontrol *phantom_ctls[SCARLETT2_PHANTOM_SWITCH_MAX];
1018 struct snd_kcontrol *input_select_ctl;
1019 struct snd_kcontrol *input_link_ctls[SCARLETT2_INPUT_GAIN_MAX / 2];
1020 struct snd_kcontrol *input_gain_ctls[SCARLETT2_INPUT_GAIN_MAX];
1021 struct snd_kcontrol *autogain_ctls[SCARLETT2_INPUT_GAIN_MAX];
1022 struct snd_kcontrol *autogain_status_ctls[SCARLETT2_INPUT_GAIN_MAX];
1023 struct snd_kcontrol *safe_ctls[SCARLETT2_INPUT_GAIN_MAX];
1024 struct snd_kcontrol *pcm_input_switch_ctl;
1025 struct snd_kcontrol *mux_ctls[SCARLETT2_MUX_MAX];
1026 struct snd_kcontrol *mix_ctls[SCARLETT2_MIX_MAX];
1027 struct snd_kcontrol *direct_monitor_ctl;
1028 struct snd_kcontrol *speaker_switching_ctl;
1029 struct snd_kcontrol *talkback_ctl;
1030 struct snd_kcontrol *power_status_ctl;
1031 u8 mux[SCARLETT2_MUX_MAX];
1032 u8 mix[SCARLETT2_MIX_MAX];
1033 u8 monitor_mix[SCARLETT2_MONITOR_MIX_MAX];
1036 /*** Model-specific data ***/
1038 static const struct scarlett2_device_info s6i6_gen2_info = {
1039 .config_set = &scarlett2_config_set_gen2a,
1040 .level_input_count = 2,
1041 .pad_input_count = 2,
1043 .line_out_descrs = {
1051 [SCARLETT2_PORT_TYPE_NONE] = { 1, 0 },
1052 [SCARLETT2_PORT_TYPE_ANALOGUE] = { 4, 4 },
1053 [SCARLETT2_PORT_TYPE_SPDIF] = { 2, 2 },
1054 [SCARLETT2_PORT_TYPE_MIX] = { 10, 18 },
1055 [SCARLETT2_PORT_TYPE_PCM] = { 6, 6 },
1058 .mux_assignment = { {
1059 { SCARLETT2_PORT_TYPE_PCM, 0, 6 },
1060 { SCARLETT2_PORT_TYPE_ANALOGUE, 0, 4 },
1061 { SCARLETT2_PORT_TYPE_SPDIF, 0, 2 },
1062 { SCARLETT2_PORT_TYPE_MIX, 0, 18 },
1063 { SCARLETT2_PORT_TYPE_NONE, 0, 8 },
1066 { SCARLETT2_PORT_TYPE_PCM, 0, 6 },
1067 { SCARLETT2_PORT_TYPE_ANALOGUE, 0, 4 },
1068 { SCARLETT2_PORT_TYPE_SPDIF, 0, 2 },
1069 { SCARLETT2_PORT_TYPE_MIX, 0, 18 },
1070 { SCARLETT2_PORT_TYPE_NONE, 0, 8 },
1073 { SCARLETT2_PORT_TYPE_PCM, 0, 6 },
1074 { SCARLETT2_PORT_TYPE_ANALOGUE, 0, 4 },
1075 { SCARLETT2_PORT_TYPE_SPDIF, 0, 2 },
1076 { SCARLETT2_PORT_TYPE_MIX, 0, 18 },
1077 { SCARLETT2_PORT_TYPE_NONE, 0, 8 },
1088 static const struct scarlett2_device_info s18i8_gen2_info = {
1089 .config_set = &scarlett2_config_set_gen2a,
1090 .level_input_count = 2,
1091 .pad_input_count = 4,
1093 .line_out_descrs = {
1103 [SCARLETT2_PORT_TYPE_NONE] = { 1, 0 },
1104 [SCARLETT2_PORT_TYPE_ANALOGUE] = { 8, 6 },
1105 [SCARLETT2_PORT_TYPE_SPDIF] = { 2, 2 },
1106 [SCARLETT2_PORT_TYPE_ADAT] = { 8, 0 },
1107 [SCARLETT2_PORT_TYPE_MIX] = { 10, 18 },
1108 [SCARLETT2_PORT_TYPE_PCM] = { 8, 18 },
1111 .mux_assignment = { {
1112 { SCARLETT2_PORT_TYPE_PCM, 0, 18 },
1113 { SCARLETT2_PORT_TYPE_ANALOGUE, 0, 6 },
1114 { SCARLETT2_PORT_TYPE_SPDIF, 0, 2 },
1115 { SCARLETT2_PORT_TYPE_MIX, 0, 18 },
1116 { SCARLETT2_PORT_TYPE_NONE, 0, 8 },
1119 { SCARLETT2_PORT_TYPE_PCM, 0, 14 },
1120 { SCARLETT2_PORT_TYPE_ANALOGUE, 0, 6 },
1121 { SCARLETT2_PORT_TYPE_SPDIF, 0, 2 },
1122 { SCARLETT2_PORT_TYPE_MIX, 0, 18 },
1123 { SCARLETT2_PORT_TYPE_NONE, 0, 8 },
1126 { SCARLETT2_PORT_TYPE_PCM, 0, 10 },
1127 { SCARLETT2_PORT_TYPE_ANALOGUE, 0, 6 },
1128 { SCARLETT2_PORT_TYPE_SPDIF, 0, 2 },
1129 { SCARLETT2_PORT_TYPE_MIX, 0, 18 },
1130 { SCARLETT2_PORT_TYPE_NONE, 0, 4 },
1141 static const struct scarlett2_device_info s18i20_gen2_info = {
1142 .config_set = &scarlett2_config_set_gen2b,
1144 .line_out_descrs = {
1158 [SCARLETT2_PORT_TYPE_NONE] = { 1, 0 },
1159 [SCARLETT2_PORT_TYPE_ANALOGUE] = { 8, 10 },
1160 [SCARLETT2_PORT_TYPE_SPDIF] = { 2, 2 },
1161 [SCARLETT2_PORT_TYPE_ADAT] = { 8, 8 },
1162 [SCARLETT2_PORT_TYPE_MIX] = { 10, 18 },
1163 [SCARLETT2_PORT_TYPE_PCM] = { 20, 18 },
1166 .mux_assignment = { {
1167 { SCARLETT2_PORT_TYPE_PCM, 0, 18 },
1168 { SCARLETT2_PORT_TYPE_ANALOGUE, 0, 10 },
1169 { SCARLETT2_PORT_TYPE_SPDIF, 0, 2 },
1170 { SCARLETT2_PORT_TYPE_ADAT, 0, 8 },
1171 { SCARLETT2_PORT_TYPE_MIX, 0, 18 },
1172 { SCARLETT2_PORT_TYPE_NONE, 0, 8 },
1175 { SCARLETT2_PORT_TYPE_PCM, 0, 14 },
1176 { SCARLETT2_PORT_TYPE_ANALOGUE, 0, 10 },
1177 { SCARLETT2_PORT_TYPE_SPDIF, 0, 2 },
1178 { SCARLETT2_PORT_TYPE_ADAT, 0, 4 },
1179 { SCARLETT2_PORT_TYPE_MIX, 0, 18 },
1180 { SCARLETT2_PORT_TYPE_NONE, 0, 8 },
1183 { SCARLETT2_PORT_TYPE_PCM, 0, 10 },
1184 { SCARLETT2_PORT_TYPE_ANALOGUE, 0, 10 },
1185 { SCARLETT2_PORT_TYPE_SPDIF, 0, 2 },
1186 { SCARLETT2_PORT_TYPE_MIX, 0, 18 },
1187 { SCARLETT2_PORT_TYPE_NONE, 0, 6 },
1198 static const struct scarlett2_device_info solo_gen3_info = {
1199 .config_set = &scarlett2_config_set_gen3a,
1200 .level_input_count = 1,
1201 .level_input_first = 1,
1202 .air_input_count = 1,
1204 .inputs_per_phantom = 1,
1205 .direct_monitor = 1,
1208 static const struct scarlett2_device_info s2i2_gen3_info = {
1209 .config_set = &scarlett2_config_set_gen3a,
1210 .level_input_count = 2,
1211 .air_input_count = 2,
1213 .inputs_per_phantom = 2,
1214 .direct_monitor = 2,
1217 static const struct scarlett2_device_info s4i4_gen3_info = {
1218 .config_set = &scarlett2_config_set_gen3b,
1219 .level_input_count = 2,
1220 .pad_input_count = 2,
1221 .air_input_count = 2,
1223 .inputs_per_phantom = 2,
1225 .line_out_descrs = {
1233 [SCARLETT2_PORT_TYPE_NONE] = { 1, 0 },
1234 [SCARLETT2_PORT_TYPE_ANALOGUE] = { 4, 4 },
1235 [SCARLETT2_PORT_TYPE_MIX] = { 6, 8 },
1236 [SCARLETT2_PORT_TYPE_PCM] = { 4, 6 },
1239 .mux_assignment = { {
1240 { SCARLETT2_PORT_TYPE_PCM, 0, 6 },
1241 { SCARLETT2_PORT_TYPE_ANALOGUE, 0, 4 },
1242 { SCARLETT2_PORT_TYPE_MIX, 0, 8 },
1243 { SCARLETT2_PORT_TYPE_NONE, 0, 16 },
1246 { SCARLETT2_PORT_TYPE_PCM, 0, 6 },
1247 { SCARLETT2_PORT_TYPE_ANALOGUE, 0, 4 },
1248 { SCARLETT2_PORT_TYPE_MIX, 0, 8 },
1249 { SCARLETT2_PORT_TYPE_NONE, 0, 16 },
1252 { SCARLETT2_PORT_TYPE_PCM, 0, 6 },
1253 { SCARLETT2_PORT_TYPE_ANALOGUE, 0, 4 },
1254 { SCARLETT2_PORT_TYPE_MIX, 0, 8 },
1255 { SCARLETT2_PORT_TYPE_NONE, 0, 16 },
1266 static const struct scarlett2_device_info s8i6_gen3_info = {
1267 .config_set = &scarlett2_config_set_gen3b,
1268 .level_input_count = 2,
1269 .pad_input_count = 2,
1270 .air_input_count = 2,
1272 .inputs_per_phantom = 2,
1274 .line_out_descrs = {
1282 [SCARLETT2_PORT_TYPE_NONE] = { 1, 0 },
1283 [SCARLETT2_PORT_TYPE_ANALOGUE] = { 6, 4 },
1284 [SCARLETT2_PORT_TYPE_SPDIF] = { 2, 2 },
1285 [SCARLETT2_PORT_TYPE_MIX] = { 8, 8 },
1286 [SCARLETT2_PORT_TYPE_PCM] = { 6, 10 },
1289 .mux_assignment = { {
1290 { SCARLETT2_PORT_TYPE_PCM, 0, 8 },
1291 { SCARLETT2_PORT_TYPE_ANALOGUE, 0, 4 },
1292 { SCARLETT2_PORT_TYPE_SPDIF, 0, 2 },
1293 { SCARLETT2_PORT_TYPE_PCM, 8, 2 },
1294 { SCARLETT2_PORT_TYPE_MIX, 0, 8 },
1295 { SCARLETT2_PORT_TYPE_NONE, 0, 18 },
1298 { SCARLETT2_PORT_TYPE_PCM, 0, 8 },
1299 { SCARLETT2_PORT_TYPE_ANALOGUE, 0, 4 },
1300 { SCARLETT2_PORT_TYPE_SPDIF, 0, 2 },
1301 { SCARLETT2_PORT_TYPE_PCM, 8, 2 },
1302 { SCARLETT2_PORT_TYPE_MIX, 0, 8 },
1303 { SCARLETT2_PORT_TYPE_NONE, 0, 18 },
1306 { SCARLETT2_PORT_TYPE_PCM, 0, 8 },
1307 { SCARLETT2_PORT_TYPE_ANALOGUE, 0, 4 },
1308 { SCARLETT2_PORT_TYPE_SPDIF, 0, 2 },
1309 { SCARLETT2_PORT_TYPE_PCM, 8, 2 },
1310 { SCARLETT2_PORT_TYPE_MIX, 0, 8 },
1311 { SCARLETT2_PORT_TYPE_NONE, 0, 18 },
1324 static const struct scarlett2_device_info s18i8_gen3_info = {
1325 .config_set = &scarlett2_config_set_gen3c,
1326 .has_speaker_switching = 1,
1327 .level_input_count = 2,
1328 .pad_input_count = 4,
1329 .air_input_count = 4,
1331 .inputs_per_phantom = 2,
1333 .line_out_remap_enable = 1,
1334 .line_out_remap = { 0, 1, 6, 7, 2, 3, 4, 5 },
1335 .line_out_unmap = { 0, 1, 4, 5, 6, 7, 2, 3 },
1337 .line_out_descrs = {
1349 [SCARLETT2_PORT_TYPE_NONE] = { 1, 0 },
1350 [SCARLETT2_PORT_TYPE_ANALOGUE] = { 8, 8 },
1351 [SCARLETT2_PORT_TYPE_SPDIF] = { 2, 2 },
1352 [SCARLETT2_PORT_TYPE_ADAT] = { 8, 0 },
1353 [SCARLETT2_PORT_TYPE_MIX] = { 10, 20 },
1354 [SCARLETT2_PORT_TYPE_PCM] = { 8, 20 },
1357 .mux_assignment = { {
1358 { SCARLETT2_PORT_TYPE_PCM, 0, 10 },
1359 { SCARLETT2_PORT_TYPE_PCM, 12, 8 },
1360 { SCARLETT2_PORT_TYPE_ANALOGUE, 0, 2 },
1361 { SCARLETT2_PORT_TYPE_ANALOGUE, 6, 2 },
1362 { SCARLETT2_PORT_TYPE_ANALOGUE, 2, 4 },
1363 { SCARLETT2_PORT_TYPE_SPDIF, 0, 2 },
1364 { SCARLETT2_PORT_TYPE_PCM, 10, 2 },
1365 { SCARLETT2_PORT_TYPE_MIX, 0, 20 },
1366 { SCARLETT2_PORT_TYPE_NONE, 0, 10 },
1369 { SCARLETT2_PORT_TYPE_PCM, 0, 10 },
1370 { SCARLETT2_PORT_TYPE_PCM, 12, 4 },
1371 { SCARLETT2_PORT_TYPE_ANALOGUE, 0, 2 },
1372 { SCARLETT2_PORT_TYPE_ANALOGUE, 6, 2 },
1373 { SCARLETT2_PORT_TYPE_ANALOGUE, 2, 4 },
1374 { SCARLETT2_PORT_TYPE_SPDIF, 0, 2 },
1375 { SCARLETT2_PORT_TYPE_PCM, 10, 2 },
1376 { SCARLETT2_PORT_TYPE_MIX, 0, 20 },
1377 { SCARLETT2_PORT_TYPE_NONE, 0, 10 },
1380 { SCARLETT2_PORT_TYPE_PCM, 0, 10 },
1381 { SCARLETT2_PORT_TYPE_ANALOGUE, 0, 2 },
1382 { SCARLETT2_PORT_TYPE_ANALOGUE, 6, 2 },
1383 { SCARLETT2_PORT_TYPE_ANALOGUE, 2, 4 },
1384 { SCARLETT2_PORT_TYPE_SPDIF, 0, 2 },
1385 { SCARLETT2_PORT_TYPE_MIX, 0, 20 },
1386 { SCARLETT2_PORT_TYPE_NONE, 0, 10 },
1403 static const struct scarlett2_device_info s18i20_gen3_info = {
1404 .config_set = &scarlett2_config_set_gen3c,
1405 .has_speaker_switching = 1,
1407 .level_input_count = 2,
1408 .pad_input_count = 8,
1409 .air_input_count = 8,
1411 .inputs_per_phantom = 4,
1413 .line_out_descrs = {
1427 [SCARLETT2_PORT_TYPE_NONE] = { 1, 0 },
1428 [SCARLETT2_PORT_TYPE_ANALOGUE] = { 9, 10 },
1429 [SCARLETT2_PORT_TYPE_SPDIF] = { 2, 2 },
1430 [SCARLETT2_PORT_TYPE_ADAT] = { 8, 8 },
1431 [SCARLETT2_PORT_TYPE_MIX] = { 12, 25 },
1432 [SCARLETT2_PORT_TYPE_PCM] = { 20, 20 },
1435 .mux_assignment = { {
1436 { SCARLETT2_PORT_TYPE_PCM, 0, 8 },
1437 { SCARLETT2_PORT_TYPE_PCM, 10, 10 },
1438 { SCARLETT2_PORT_TYPE_ANALOGUE, 0, 10 },
1439 { SCARLETT2_PORT_TYPE_SPDIF, 0, 2 },
1440 { SCARLETT2_PORT_TYPE_ADAT, 0, 8 },
1441 { SCARLETT2_PORT_TYPE_PCM, 8, 2 },
1442 { SCARLETT2_PORT_TYPE_MIX, 0, 25 },
1443 { SCARLETT2_PORT_TYPE_NONE, 0, 12 },
1446 { SCARLETT2_PORT_TYPE_PCM, 0, 8 },
1447 { SCARLETT2_PORT_TYPE_PCM, 10, 8 },
1448 { SCARLETT2_PORT_TYPE_ANALOGUE, 0, 10 },
1449 { SCARLETT2_PORT_TYPE_SPDIF, 0, 2 },
1450 { SCARLETT2_PORT_TYPE_ADAT, 0, 8 },
1451 { SCARLETT2_PORT_TYPE_PCM, 8, 2 },
1452 { SCARLETT2_PORT_TYPE_MIX, 0, 25 },
1453 { SCARLETT2_PORT_TYPE_NONE, 0, 10 },
1456 { SCARLETT2_PORT_TYPE_PCM, 0, 10 },
1457 { SCARLETT2_PORT_TYPE_ANALOGUE, 0, 10 },
1458 { SCARLETT2_PORT_TYPE_SPDIF, 0, 2 },
1459 { SCARLETT2_PORT_TYPE_NONE, 0, 24 },
1473 static const struct scarlett2_device_info solo_gen4_info = {
1474 .config_set = &scarlett2_config_set_gen4_solo,
1475 .min_firmware_version = 2115,
1477 .level_input_count = 1,
1478 .air_input_count = 1,
1479 .air_input_first = 1,
1483 .inputs_per_phantom = 1,
1484 .direct_monitor = 1,
1488 [SCARLETT2_PORT_TYPE_NONE] = { 1, 0 },
1489 [SCARLETT2_PORT_TYPE_ANALOGUE] = { 2, 2 },
1490 [SCARLETT2_PORT_TYPE_MIX] = { 8, 6 },
1491 [SCARLETT2_PORT_TYPE_PCM] = { 2, 4 },
1494 .mux_assignment = { {
1495 { SCARLETT2_PORT_TYPE_MIX, 4, 2 },
1496 { SCARLETT2_PORT_TYPE_MIX, 2, 2 },
1497 { SCARLETT2_PORT_TYPE_PCM, 0, 4 },
1498 { SCARLETT2_PORT_TYPE_MIX, 0, 2 },
1499 { SCARLETT2_PORT_TYPE_ANALOGUE, 0, 2 },
1502 { SCARLETT2_PORT_TYPE_MIX, 4, 2 },
1503 { SCARLETT2_PORT_TYPE_MIX, 2, 2 },
1504 { SCARLETT2_PORT_TYPE_PCM, 0, 4 },
1505 { SCARLETT2_PORT_TYPE_MIX, 0, 2 },
1506 { SCARLETT2_PORT_TYPE_ANALOGUE, 0, 2 },
1509 { SCARLETT2_PORT_TYPE_MIX, 4, 2 },
1510 { SCARLETT2_PORT_TYPE_MIX, 2, 2 },
1511 { SCARLETT2_PORT_TYPE_PCM, 0, 4 },
1512 { SCARLETT2_PORT_TYPE_MIX, 0, 2 },
1513 { SCARLETT2_PORT_TYPE_ANALOGUE, 0, 2 },
1527 static const struct scarlett2_device_info s2i2_gen4_info = {
1528 .config_set = &scarlett2_config_set_gen4_2i2,
1529 .min_firmware_version = 2115,
1531 .level_input_count = 2,
1532 .air_input_count = 2,
1535 .inputs_per_phantom = 2,
1536 .gain_input_count = 2,
1537 .direct_monitor = 2,
1541 [SCARLETT2_PORT_TYPE_NONE] = { 1, 0 },
1542 [SCARLETT2_PORT_TYPE_ANALOGUE] = { 2, 2 },
1543 [SCARLETT2_PORT_TYPE_MIX] = { 6, 6 },
1544 [SCARLETT2_PORT_TYPE_PCM] = { 2, 4 },
1547 .mux_assignment = { {
1548 { SCARLETT2_PORT_TYPE_MIX, 4, 2 },
1549 { SCARLETT2_PORT_TYPE_MIX, 2, 2 },
1550 { SCARLETT2_PORT_TYPE_PCM, 0, 4 },
1551 { SCARLETT2_PORT_TYPE_MIX, 0, 2 },
1552 { SCARLETT2_PORT_TYPE_ANALOGUE, 0, 2 },
1555 { SCARLETT2_PORT_TYPE_MIX, 4, 2 },
1556 { SCARLETT2_PORT_TYPE_MIX, 2, 2 },
1557 { SCARLETT2_PORT_TYPE_PCM, 0, 4 },
1558 { SCARLETT2_PORT_TYPE_MIX, 0, 2 },
1559 { SCARLETT2_PORT_TYPE_ANALOGUE, 0, 2 },
1562 { SCARLETT2_PORT_TYPE_MIX, 4, 2 },
1563 { SCARLETT2_PORT_TYPE_MIX, 2, 2 },
1564 { SCARLETT2_PORT_TYPE_PCM, 0, 4 },
1565 { SCARLETT2_PORT_TYPE_MIX, 0, 2 },
1566 { SCARLETT2_PORT_TYPE_ANALOGUE, 0, 2 },
1580 static const struct scarlett2_device_info s4i4_gen4_info = {
1581 .config_set = &scarlett2_config_set_gen4_4i4,
1582 .min_firmware_version = 2089,
1584 .level_input_count = 2,
1585 .air_input_count = 2,
1588 .inputs_per_phantom = 1,
1589 .gain_input_count = 2,
1593 [SCARLETT2_PORT_TYPE_NONE] = { 1, 0 },
1594 [SCARLETT2_PORT_TYPE_ANALOGUE] = { 4, 6 },
1595 [SCARLETT2_PORT_TYPE_MIX] = { 8, 12 },
1596 [SCARLETT2_PORT_TYPE_PCM] = { 6, 6 },
1599 .mux_assignment = { {
1600 { SCARLETT2_PORT_TYPE_MIX, 10, 2 },
1601 { SCARLETT2_PORT_TYPE_PCM, 0, 6 },
1602 { SCARLETT2_PORT_TYPE_MIX, 0, 10 },
1603 { SCARLETT2_PORT_TYPE_ANALOGUE, 0, 6 },
1606 { SCARLETT2_PORT_TYPE_MIX, 10, 2 },
1607 { SCARLETT2_PORT_TYPE_PCM, 0, 6 },
1608 { SCARLETT2_PORT_TYPE_MIX, 0, 10 },
1609 { SCARLETT2_PORT_TYPE_ANALOGUE, 0, 6 },
1612 { SCARLETT2_PORT_TYPE_MIX, 10, 2 },
1613 { SCARLETT2_PORT_TYPE_PCM, 0, 6 },
1614 { SCARLETT2_PORT_TYPE_MIX, 0, 10 },
1615 { SCARLETT2_PORT_TYPE_ANALOGUE, 0, 6 },
1627 static const struct scarlett2_device_info clarett_2pre_info = {
1628 .config_set = &scarlett2_config_set_clarett,
1629 .level_input_count = 2,
1630 .air_input_count = 2,
1632 .line_out_descrs = {
1640 [SCARLETT2_PORT_TYPE_NONE] = { 1, 0 },
1641 [SCARLETT2_PORT_TYPE_ANALOGUE] = { 2, 4 },
1642 [SCARLETT2_PORT_TYPE_SPDIF] = { 2, 0 },
1643 [SCARLETT2_PORT_TYPE_ADAT] = { 8, 0 },
1644 [SCARLETT2_PORT_TYPE_MIX] = { 10, 18 },
1645 [SCARLETT2_PORT_TYPE_PCM] = { 4, 12 },
1648 .mux_assignment = { {
1649 { SCARLETT2_PORT_TYPE_PCM, 0, 12 },
1650 { SCARLETT2_PORT_TYPE_ANALOGUE, 0, 4 },
1651 { SCARLETT2_PORT_TYPE_MIX, 0, 18 },
1652 { SCARLETT2_PORT_TYPE_NONE, 0, 8 },
1655 { SCARLETT2_PORT_TYPE_PCM, 0, 8 },
1656 { SCARLETT2_PORT_TYPE_ANALOGUE, 0, 4 },
1657 { SCARLETT2_PORT_TYPE_MIX, 0, 18 },
1658 { SCARLETT2_PORT_TYPE_NONE, 0, 8 },
1661 { SCARLETT2_PORT_TYPE_PCM, 0, 2 },
1662 { SCARLETT2_PORT_TYPE_ANALOGUE, 0, 4 },
1663 { SCARLETT2_PORT_TYPE_NONE, 0, 26 },
1674 static const struct scarlett2_device_info clarett_4pre_info = {
1675 .config_set = &scarlett2_config_set_clarett,
1676 .level_input_count = 2,
1677 .air_input_count = 4,
1679 .line_out_descrs = {
1689 [SCARLETT2_PORT_TYPE_NONE] = { 1, 0 },
1690 [SCARLETT2_PORT_TYPE_ANALOGUE] = { 8, 6 },
1691 [SCARLETT2_PORT_TYPE_SPDIF] = { 2, 2 },
1692 [SCARLETT2_PORT_TYPE_ADAT] = { 8, 0 },
1693 [SCARLETT2_PORT_TYPE_MIX] = { 10, 18 },
1694 [SCARLETT2_PORT_TYPE_PCM] = { 8, 18 },
1697 .mux_assignment = { {
1698 { SCARLETT2_PORT_TYPE_PCM, 0, 18 },
1699 { SCARLETT2_PORT_TYPE_ANALOGUE, 0, 6 },
1700 { SCARLETT2_PORT_TYPE_SPDIF, 0, 2 },
1701 { SCARLETT2_PORT_TYPE_MIX, 0, 18 },
1702 { SCARLETT2_PORT_TYPE_NONE, 0, 8 },
1705 { SCARLETT2_PORT_TYPE_PCM, 0, 14 },
1706 { SCARLETT2_PORT_TYPE_ANALOGUE, 0, 6 },
1707 { SCARLETT2_PORT_TYPE_SPDIF, 0, 2 },
1708 { SCARLETT2_PORT_TYPE_MIX, 0, 18 },
1709 { SCARLETT2_PORT_TYPE_NONE, 0, 8 },
1712 { SCARLETT2_PORT_TYPE_PCM, 0, 12 },
1713 { SCARLETT2_PORT_TYPE_ANALOGUE, 0, 6 },
1714 { SCARLETT2_PORT_TYPE_SPDIF, 0, 2 },
1715 { SCARLETT2_PORT_TYPE_NONE, 0, 24 },
1726 static const struct scarlett2_device_info clarett_8pre_info = {
1727 .config_set = &scarlett2_config_set_clarett,
1728 .level_input_count = 2,
1729 .air_input_count = 8,
1731 .line_out_descrs = {
1745 [SCARLETT2_PORT_TYPE_NONE] = { 1, 0 },
1746 [SCARLETT2_PORT_TYPE_ANALOGUE] = { 8, 10 },
1747 [SCARLETT2_PORT_TYPE_SPDIF] = { 2, 2 },
1748 [SCARLETT2_PORT_TYPE_ADAT] = { 8, 8 },
1749 [SCARLETT2_PORT_TYPE_MIX] = { 10, 18 },
1750 [SCARLETT2_PORT_TYPE_PCM] = { 20, 18 },
1753 .mux_assignment = { {
1754 { SCARLETT2_PORT_TYPE_PCM, 0, 18 },
1755 { SCARLETT2_PORT_TYPE_ANALOGUE, 0, 10 },
1756 { SCARLETT2_PORT_TYPE_SPDIF, 0, 2 },
1757 { SCARLETT2_PORT_TYPE_ADAT, 0, 8 },
1758 { SCARLETT2_PORT_TYPE_MIX, 0, 18 },
1759 { SCARLETT2_PORT_TYPE_NONE, 0, 8 },
1762 { SCARLETT2_PORT_TYPE_PCM, 0, 14 },
1763 { SCARLETT2_PORT_TYPE_ANALOGUE, 0, 10 },
1764 { SCARLETT2_PORT_TYPE_SPDIF, 0, 2 },
1765 { SCARLETT2_PORT_TYPE_ADAT, 0, 4 },
1766 { SCARLETT2_PORT_TYPE_MIX, 0, 18 },
1767 { SCARLETT2_PORT_TYPE_NONE, 0, 8 },
1770 { SCARLETT2_PORT_TYPE_PCM, 0, 12 },
1771 { SCARLETT2_PORT_TYPE_ANALOGUE, 0, 10 },
1772 { SCARLETT2_PORT_TYPE_SPDIF, 0, 2 },
1773 { SCARLETT2_PORT_TYPE_NONE, 0, 22 },
1784 struct scarlett2_device_entry {
1785 const u32 usb_id; /* USB device identifier */
1786 const struct scarlett2_device_info *info;
1787 const char *series_name;
1790 static const struct scarlett2_device_entry scarlett2_devices[] = {
1791 /* Supported Gen 2 devices */
1792 { USB_ID(0x1235, 0x8203), &s6i6_gen2_info, "Scarlett Gen 2" },
1793 { USB_ID(0x1235, 0x8204), &s18i8_gen2_info, "Scarlett Gen 2" },
1794 { USB_ID(0x1235, 0x8201), &s18i20_gen2_info, "Scarlett Gen 2" },
1796 /* Supported Gen 3 devices */
1797 { USB_ID(0x1235, 0x8211), &solo_gen3_info, "Scarlett Gen 3" },
1798 { USB_ID(0x1235, 0x8210), &s2i2_gen3_info, "Scarlett Gen 3" },
1799 { USB_ID(0x1235, 0x8212), &s4i4_gen3_info, "Scarlett Gen 3" },
1800 { USB_ID(0x1235, 0x8213), &s8i6_gen3_info, "Scarlett Gen 3" },
1801 { USB_ID(0x1235, 0x8214), &s18i8_gen3_info, "Scarlett Gen 3" },
1802 { USB_ID(0x1235, 0x8215), &s18i20_gen3_info, "Scarlett Gen 3" },
1804 /* Supported Gen 4 devices */
1805 { USB_ID(0x1235, 0x8218), &solo_gen4_info, "Scarlett Gen 4" },
1806 { USB_ID(0x1235, 0x8219), &s2i2_gen4_info, "Scarlett Gen 4" },
1807 { USB_ID(0x1235, 0x821a), &s4i4_gen4_info, "Scarlett Gen 4" },
1809 /* Supported Clarett USB/Clarett+ devices */
1810 { USB_ID(0x1235, 0x8206), &clarett_2pre_info, "Clarett USB" },
1811 { USB_ID(0x1235, 0x8207), &clarett_4pre_info, "Clarett USB" },
1812 { USB_ID(0x1235, 0x8208), &clarett_8pre_info, "Clarett USB" },
1813 { USB_ID(0x1235, 0x820a), &clarett_2pre_info, "Clarett+" },
1814 { USB_ID(0x1235, 0x820b), &clarett_4pre_info, "Clarett+" },
1815 { USB_ID(0x1235, 0x820c), &clarett_8pre_info, "Clarett+" },
1821 /* get the starting port index number for a given port type/direction */
1822 static int scarlett2_get_port_start_num(
1823 const int port_count[][SCARLETT2_PORT_DIRNS],
1824 int direction, int port_type)
1828 for (i = 0; i < port_type; i++)
1829 num += port_count[i][direction];
1834 /*** USB Interactions ***/
1836 /* Commands for sending/receiving requests/responses */
1837 #define SCARLETT2_USB_CMD_INIT 0
1838 #define SCARLETT2_USB_CMD_REQ 2
1839 #define SCARLETT2_USB_CMD_RESP 3
1841 #define SCARLETT2_USB_INIT_1 0x00000000
1842 #define SCARLETT2_USB_INIT_2 0x00000002
1843 #define SCARLETT2_USB_REBOOT 0x00000003
1844 #define SCARLETT2_USB_GET_METER 0x00001001
1845 #define SCARLETT2_USB_GET_MIX 0x00002001
1846 #define SCARLETT2_USB_SET_MIX 0x00002002
1847 #define SCARLETT2_USB_GET_MUX 0x00003001
1848 #define SCARLETT2_USB_SET_MUX 0x00003002
1849 #define SCARLETT2_USB_INFO_FLASH 0x00004000
1850 #define SCARLETT2_USB_INFO_SEGMENT 0x00004001
1851 #define SCARLETT2_USB_ERASE_SEGMENT 0x00004002
1852 #define SCARLETT2_USB_GET_ERASE 0x00004003
1853 #define SCARLETT2_USB_WRITE_SEGMENT 0x00004004
1854 #define SCARLETT2_USB_GET_SYNC 0x00006004
1855 #define SCARLETT2_USB_GET_DATA 0x00800000
1856 #define SCARLETT2_USB_SET_DATA 0x00800001
1857 #define SCARLETT2_USB_DATA_CMD 0x00800002
1859 #define SCARLETT2_USB_CONFIG_SAVE 6
1861 #define SCARLETT2_USB_METER_LEVELS_GET_MAGIC 1
1863 #define SCARLETT2_FLASH_BLOCK_SIZE 4096
1864 #define SCARLETT2_FLASH_WRITE_MAX 1024
1865 #define SCARLETT2_SEGMENT_NUM_MIN 1
1866 #define SCARLETT2_SEGMENT_NUM_MAX 4
1868 #define SCARLETT2_SEGMENT_SETTINGS_NAME "App_Settings"
1869 #define SCARLETT2_SEGMENT_FIRMWARE_NAME "App_Upgrade"
1871 /* proprietary request/response format */
1872 struct scarlett2_usb_packet {
1881 static void scarlett2_fill_request_header(struct scarlett2_data *private,
1882 struct scarlett2_usb_packet *req,
1883 u32 cmd, u16 req_size)
1885 /* sequence must go up by 1 for each request */
1886 u16 seq = private->scarlett2_seq++;
1888 req->cmd = cpu_to_le32(cmd);
1889 req->size = cpu_to_le16(req_size);
1890 req->seq = cpu_to_le16(seq);
1895 static int scarlett2_usb_tx(struct usb_device *dev, int interface,
1896 void *buf, u16 size)
1898 return snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0),
1899 SCARLETT2_USB_CMD_REQ,
1900 USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_OUT,
1901 0, interface, buf, size);
1904 static int scarlett2_usb_rx(struct usb_device *dev, int interface,
1905 u32 usb_req, void *buf, u16 size)
1907 return snd_usb_ctl_msg(dev, usb_rcvctrlpipe(dev, 0),
1909 USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_IN,
1910 0, interface, buf, size);
1913 /* Send a proprietary format request to the Scarlett interface */
1914 static int scarlett2_usb(
1915 struct usb_mixer_interface *mixer, u32 cmd,
1916 void *req_data, u16 req_size, void *resp_data, u16 resp_size)
1918 struct scarlett2_data *private = mixer->private_data;
1919 struct usb_device *dev = mixer->chip->dev;
1920 struct scarlett2_usb_packet *req, *resp = NULL;
1921 size_t req_buf_size = struct_size(req, data, req_size);
1922 size_t resp_buf_size = struct_size(resp, data, resp_size);
1925 req = kmalloc(req_buf_size, GFP_KERNEL);
1931 resp = kmalloc(resp_buf_size, GFP_KERNEL);
1937 mutex_lock(&private->usb_mutex);
1939 /* build request message and send it */
1941 scarlett2_fill_request_header(private, req, cmd, req_size);
1944 memcpy(req->data, req_data, req_size);
1946 err = scarlett2_usb_tx(dev, private->bInterfaceNumber,
1949 if (err != req_buf_size) {
1952 "%s USB request result cmd %x was %d\n",
1953 private->series_name, cmd, err);
1958 /* send a second message to get the response */
1960 err = scarlett2_usb_rx(dev, private->bInterfaceNumber,
1961 SCARLETT2_USB_CMD_RESP,
1962 resp, resp_buf_size);
1964 /* validate the response */
1966 if (err != resp_buf_size) {
1968 /* ESHUTDOWN and EPROTO are valid responses to a
1971 if (cmd == SCARLETT2_USB_REBOOT &&
1972 (err == -ESHUTDOWN || err == -EPROTO)) {
1979 "%s USB response result cmd %x was %d expected %zu\n",
1980 private->series_name, cmd, err, resp_buf_size);
1985 /* cmd/seq/size should match except when initialising
1986 * seq sent = 1, response = 0
1988 if (resp->cmd != req->cmd ||
1989 (resp->seq != req->seq &&
1990 (le16_to_cpu(req->seq) != 1 || resp->seq != 0)) ||
1991 resp_size != le16_to_cpu(resp->size) ||
1996 "%s USB invalid response; "
1997 "cmd tx/rx %d/%d seq %d/%d size %d/%d "
1998 "error %d pad %d\n",
1999 private->series_name,
2000 le32_to_cpu(req->cmd), le32_to_cpu(resp->cmd),
2001 le16_to_cpu(req->seq), le16_to_cpu(resp->seq),
2002 resp_size, le16_to_cpu(resp->size),
2003 le32_to_cpu(resp->error),
2004 le32_to_cpu(resp->pad));
2009 if (resp_data && resp_size > 0)
2010 memcpy(resp_data, resp->data, resp_size);
2013 mutex_unlock(&private->usb_mutex);
2020 /* Send a USB message to get data; result placed in *buf */
2021 static int scarlett2_usb_get(
2022 struct usb_mixer_interface *mixer,
2023 int offset, void *buf, int size)
2030 req.offset = cpu_to_le32(offset);
2031 req.size = cpu_to_le32(size);
2032 return scarlett2_usb(mixer, SCARLETT2_USB_GET_DATA,
2033 &req, sizeof(req), buf, size);
2036 /* Return true if the given configuration item is present in the
2037 * configuration set used by this device.
2039 static int scarlett2_has_config_item(
2040 struct scarlett2_data *private, int config_item_num)
2042 return !!private->config_set->items[config_item_num].offset;
2045 /* Send a USB message to get configuration parameters; result placed in *buf */
2046 static int scarlett2_usb_get_config(
2047 struct usb_mixer_interface *mixer,
2048 int config_item_num, int count, void *buf)
2050 struct scarlett2_data *private = mixer->private_data;
2051 const struct scarlett2_config *config_item =
2052 &private->config_set->items[config_item_num];
2057 /* Check that the configuration item is present in the
2058 * configuration set used by this device
2060 if (!config_item->offset)
2063 /* Gen 4 style parameters are always 1 byte */
2064 size = config_item->size ? config_item->size : 8;
2066 /* For byte-sized parameters, retrieve directly into buf */
2068 size = size / 8 * count;
2069 err = scarlett2_usb_get(mixer, config_item->offset, buf, size);
2075 for (i = 0; i < count; i++, buf_16++)
2076 *buf_16 = le16_to_cpu(*(__le16 *)buf_16);
2081 /* For bit-sized parameters, retrieve into value */
2082 err = scarlett2_usb_get(mixer, config_item->offset, &value, 1);
2086 /* then unpack from value into buf[] */
2088 for (i = 0; i < 8 && i < count; i++, value >>= 1)
2089 *buf_8++ = value & 1;
2094 /* Send a SCARLETT2_USB_SET_DATA command.
2095 * offset: location in the device's data space
2096 * size: size in bytes of the value (1, 2, 4)
2098 static int scarlett2_usb_set_data(
2099 struct usb_mixer_interface *mixer,
2100 int offset, int size, int value)
2102 struct scarlett2_data *private = mixer->private_data;
2109 req.offset = cpu_to_le32(offset);
2110 req.size = cpu_to_le32(size);
2111 req.value = cpu_to_le32(value);
2112 return scarlett2_usb(private->mixer, SCARLETT2_USB_SET_DATA,
2113 &req, sizeof(u32) * 2 + size, NULL, 0);
2116 /* Send a SCARLETT2_USB_DATA_CMD command.
2117 * Configuration changes require activation with this after they have
2118 * been uploaded by a previous SCARLETT2_USB_SET_DATA.
2119 * The value for activate needed is determined by the configuration
2122 static int scarlett2_usb_activate_config(
2123 struct usb_mixer_interface *mixer, int activate)
2127 req = cpu_to_le32(activate);
2128 return scarlett2_usb(mixer, SCARLETT2_USB_DATA_CMD,
2129 &req, sizeof(req), NULL, 0);
2132 /* Send USB messages to set a SCARLETT2_CONFIG_* parameter */
2133 static int scarlett2_usb_set_config(
2134 struct usb_mixer_interface *mixer,
2135 int config_item_num, int index, int value)
2137 struct scarlett2_data *private = mixer->private_data;
2138 const struct scarlett2_config_set *config_set = private->config_set;
2139 const struct scarlett2_config *config_item =
2140 &config_set->items[config_item_num];
2144 /* Check that the configuration item is present in the
2145 * configuration set used by this device
2147 if (!config_item->offset)
2150 /* Gen 4 style writes are selected with size = 0;
2151 * these are only byte-sized values written through a shared
2152 * location, different to the read address
2154 if (!config_item->size) {
2155 if (!config_set->gen4_write_addr)
2158 /* Place index in gen4_write_addr + 1 */
2159 err = scarlett2_usb_set_data(
2160 mixer, config_set->gen4_write_addr + 1, 1, index);
2164 /* Place value in gen4_write_addr */
2165 err = scarlett2_usb_set_data(
2166 mixer, config_set->gen4_write_addr, 1, value);
2170 /* Request the interface do the write */
2171 return scarlett2_usb_activate_config(
2172 mixer, config_item->activate);
2175 /* Not-Gen 4 style needs NVRAM save, supports
2176 * bit-modification, and writing is done to the same place
2177 * that the value can be read from
2180 /* Cancel any pending NVRAM save */
2181 cancel_delayed_work_sync(&private->work);
2183 /* Convert config_item->size in bits to size in bytes and
2186 if (config_item->size >= 8) {
2187 size = config_item->size / 8;
2188 offset = config_item->offset + index * size;
2190 /* If updating a bit, retrieve the old value, set/clear the
2191 * bit as needed, and update value
2197 offset = config_item->offset;
2199 err = scarlett2_usb_get(mixer, offset, &tmp, 1);
2204 tmp |= (1 << index);
2206 tmp &= ~(1 << index);
2211 /* Send the configuration parameter data */
2212 err = scarlett2_usb_set_data(mixer, offset, size, value);
2216 /* Activate the change */
2217 err = scarlett2_usb_activate_config(mixer, config_item->activate);
2221 /* Gen 2 style writes to Gen 4 devices don't need saving */
2222 if (config_set->gen4_write_addr)
2225 /* Schedule the change to be written to NVRAM */
2226 if (config_item->activate != SCARLETT2_USB_CONFIG_SAVE)
2227 schedule_delayed_work(&private->work, msecs_to_jiffies(2000));
2232 /* Send SCARLETT2_USB_DATA_CMD SCARLETT2_USB_CONFIG_SAVE */
2233 static void scarlett2_config_save(struct usb_mixer_interface *mixer)
2237 err = scarlett2_usb_activate_config(mixer, SCARLETT2_USB_CONFIG_SAVE);
2239 usb_audio_err(mixer->chip, "config save failed: %d\n", err);
2242 /* Delayed work to save config */
2243 static void scarlett2_config_save_work(struct work_struct *work)
2245 struct scarlett2_data *private =
2246 container_of(work, struct scarlett2_data, work.work);
2248 scarlett2_config_save(private->mixer);
2251 /* Send a USB message to get sync status; result placed in *sync */
2252 static int scarlett2_usb_get_sync_status(
2253 struct usb_mixer_interface *mixer,
2259 err = scarlett2_usb(mixer, SCARLETT2_USB_GET_SYNC,
2260 NULL, 0, &data, sizeof(data));
2268 /* Return true if the device has a mixer that we can control */
2269 static int scarlett2_has_mixer(struct scarlett2_data *private)
2271 return !!private->info->mux_assignment[0][0].count;
2274 /* Map from mixer value to (db + 80) * 2
2275 * (reverse of scarlett2_mixer_values[])
2277 static int scarlett2_mixer_value_to_db(int value)
2281 for (i = 0; i < SCARLETT2_MIXER_VALUE_COUNT; i++)
2282 if (scarlett2_mixer_values[i] >= value)
2284 return SCARLETT2_MIXER_MAX_VALUE;
2287 /* Send a USB message to get the volumes for all inputs of one mix
2288 * and put the values into private->mix[]
2290 static int scarlett2_usb_get_mix(struct usb_mixer_interface *mixer,
2293 struct scarlett2_data *private = mixer->private_data;
2295 int num_mixer_in = private->num_mix_in;
2303 __le16 data[SCARLETT2_INPUT_MIX_MAX];
2305 req.mix_num = cpu_to_le16(mix_num);
2306 req.count = cpu_to_le16(num_mixer_in);
2308 err = scarlett2_usb(mixer, SCARLETT2_USB_GET_MIX,
2310 data, num_mixer_in * sizeof(u16));
2314 for (i = 0, j = mix_num * num_mixer_in; i < num_mixer_in; i++, j++)
2315 private->mix[j] = scarlett2_mixer_value_to_db(
2316 le16_to_cpu(data[i]));
2321 /* Send a USB message to set the volumes for all inputs of one mix
2322 * (values obtained from private->mix[])
2324 static int scarlett2_usb_set_mix(struct usb_mixer_interface *mixer,
2327 struct scarlett2_data *private = mixer->private_data;
2331 __le16 data[SCARLETT2_INPUT_MIX_MAX];
2335 int num_mixer_in = private->num_mix_in;
2337 req.mix_num = cpu_to_le16(mix_num);
2339 for (i = 0, j = mix_num * num_mixer_in; i < num_mixer_in; i++, j++)
2340 req.data[i] = cpu_to_le16(
2341 scarlett2_mixer_values[private->mix[j]]
2344 return scarlett2_usb(mixer, SCARLETT2_USB_SET_MIX,
2345 &req, (num_mixer_in + 1) * sizeof(u16),
2349 /* Convert a port number index (per info->port_count) to a hardware ID */
2350 static u32 scarlett2_mux_src_num_to_id(
2351 const int port_count[][SCARLETT2_PORT_DIRNS], int num)
2356 port_type < SCARLETT2_PORT_TYPE_COUNT;
2358 if (num < port_count[port_type][SCARLETT2_PORT_IN])
2359 return scarlett2_ports[port_type].id | num;
2360 num -= port_count[port_type][SCARLETT2_PORT_IN];
2367 /* Convert a hardware ID to a port number index */
2368 static u32 scarlett2_mux_id_to_num(
2369 const int port_count[][SCARLETT2_PORT_DIRNS], int direction, u32 id)
2375 port_type < SCARLETT2_PORT_TYPE_COUNT;
2377 int base = scarlett2_ports[port_type].id;
2378 int count = port_count[port_type][direction];
2380 if (id >= base && id < base + count)
2381 return port_num + id - base;
2389 /* Convert one mux entry from the interface and load into private->mux[] */
2390 static void scarlett2_usb_populate_mux(struct scarlett2_data *private,
2393 const struct scarlett2_device_info *info = private->info;
2394 const int (*port_count)[SCARLETT2_PORT_DIRNS] = info->port_count;
2396 int dst_idx, src_idx;
2398 dst_idx = scarlett2_mux_id_to_num(port_count, SCARLETT2_PORT_OUT,
2403 if (dst_idx >= private->num_mux_dsts) {
2404 usb_audio_err(private->mixer->chip,
2405 "BUG: scarlett2_mux_id_to_num(%06x, OUT): %d >= %d",
2406 mux_entry, dst_idx, private->num_mux_dsts);
2410 src_idx = scarlett2_mux_id_to_num(port_count, SCARLETT2_PORT_IN,
2415 if (src_idx >= private->num_mux_srcs) {
2416 usb_audio_err(private->mixer->chip,
2417 "BUG: scarlett2_mux_id_to_num(%06x, IN): %d >= %d",
2418 mux_entry, src_idx, private->num_mux_srcs);
2422 private->mux[dst_idx] = src_idx;
2425 /* Update the meter level map
2427 * The meter level data from the interface (SCARLETT2_USB_GET_METER
2428 * request) is returned in mux_assignment order, but to avoid exposing
2429 * that to userspace, scarlett2_meter_ctl_get() rearranges the data
2430 * into scarlett2_ports order using the meter_level_map[] array which
2431 * is set up by this function.
2433 * In addition, the meter level data values returned from the
2434 * interface are invalid for destinations where:
2436 * - the source is "Off"; therefore we set those values to zero (map
2439 * - the source is assigned to a previous (with respect to the
2440 * mux_assignment order) destination; therefore we set those values
2441 * to the value previously reported for that source
2443 static void scarlett2_update_meter_level_map(struct scarlett2_data *private)
2445 const struct scarlett2_device_info *info = private->info;
2446 const struct scarlett2_meter_entry *entry;
2448 /* sources already assigned to a destination
2449 * value is 255 for None, otherwise the value of i
2450 * (index into array returned by
2451 * scarlett2_usb_get_meter_levels())
2453 u8 seen_src[SCARLETT2_MAX_SRCS] = { 1 };
2454 u8 seen_src_value[SCARLETT2_MAX_SRCS] = { 255 };
2456 /* index in meter_map[] order */
2459 /* go through the meter_map[] entries */
2460 for (entry = info->meter_map;
2464 /* fill in each meter_level_map[] entry */
2467 for (j = 0, mux_idx = entry->start;
2469 i++, j++, mux_idx++) {
2471 /* convert mux_idx using line_out_unmap[] */
2473 info->line_out_remap_enable &&
2474 mux_idx < private->num_line_out
2475 ) ? info->line_out_unmap[mux_idx]
2478 /* check which source is connected, and if
2479 * that source is already connected elsewhere,
2480 * use that existing connection's destination
2481 * for this meter entry instead
2483 int mux_src = private->mux[mux_idx];
2485 if (!seen_src[mux_src]) {
2486 seen_src[mux_src] = 1;
2487 seen_src_value[mux_src] = i;
2489 private->meter_level_map[map_mux_idx] =
2490 seen_src_value[mux_src];
2495 /* Send USB message to get mux inputs and then populate private->mux[] */
2496 static int scarlett2_usb_get_mux(struct usb_mixer_interface *mixer)
2498 struct scarlett2_data *private = mixer->private_data;
2499 int count = private->num_mux_dsts;
2507 __le32 data[SCARLETT2_MUX_MAX];
2509 private->mux_updated = 0;
2512 req.count = cpu_to_le16(count);
2514 err = scarlett2_usb(mixer, SCARLETT2_USB_GET_MUX,
2516 data, count * sizeof(u32));
2520 for (i = 0; i < count; i++)
2521 scarlett2_usb_populate_mux(private, le32_to_cpu(data[i]));
2523 scarlett2_update_meter_level_map(private);
2528 /* Send USB messages to set mux inputs */
2529 static int scarlett2_usb_set_mux(struct usb_mixer_interface *mixer)
2531 struct scarlett2_data *private = mixer->private_data;
2532 const struct scarlett2_device_info *info = private->info;
2533 const int (*port_count)[SCARLETT2_PORT_DIRNS] = info->port_count;
2539 __le32 data[SCARLETT2_MUX_MAX];
2544 /* set mux settings for each rate */
2545 for (table = 0; table < SCARLETT2_MUX_TABLES; table++) {
2546 const struct scarlett2_mux_entry *entry;
2548 /* i counts over the output array */
2551 req.num = cpu_to_le16(table);
2553 /* loop through each entry */
2554 for (entry = info->mux_assignment[table];
2558 int port_type = entry->port_type;
2559 int port_idx = entry->start;
2560 int mux_idx = scarlett2_get_port_start_num(port_count,
2561 SCARLETT2_PORT_OUT, port_type) + port_idx;
2562 int dst_id = scarlett2_ports[port_type].id + port_idx;
2566 for (j = 0; j < entry->count; j++)
2571 /* Non-empty mux slots use the lower 12 bits
2572 * for the destination and next 12 bits for
2575 for (j = 0; j < entry->count; j++) {
2576 int src_id = scarlett2_mux_src_num_to_id(
2577 port_count, private->mux[mux_idx++]);
2578 req.data[i++] = cpu_to_le32(dst_id |
2584 err = scarlett2_usb(mixer, SCARLETT2_USB_SET_MUX,
2585 &req, (i + 1) * sizeof(u32),
2591 scarlett2_update_meter_level_map(private);
2596 /* Send USB message to get meter levels */
2597 static int scarlett2_usb_get_meter_levels(struct usb_mixer_interface *mixer,
2598 u16 num_meters, u16 *levels)
2605 __le32 resp[SCARLETT2_MAX_METERS];
2609 req.num_meters = cpu_to_le16(num_meters);
2610 req.magic = cpu_to_le32(SCARLETT2_USB_METER_LEVELS_GET_MAGIC);
2611 err = scarlett2_usb(mixer, SCARLETT2_USB_GET_METER,
2612 &req, sizeof(req), resp, num_meters * sizeof(u32));
2616 /* copy, convert to u16 */
2617 for (i = 0; i < num_meters; i++)
2618 levels[i] = le32_to_cpu(resp[i]);
2623 /* For config items with mute=1, xor bits 0 & 1 together to get the
2624 * current/next state. This won't have any effect on values which are
2627 static uint8_t scarlett2_decode_muteable(uint8_t v)
2629 return (v ^ (v >> 1)) & 1;
2632 /*** Control Functions ***/
2634 /* helper function to create a new control */
2635 static int scarlett2_add_new_ctl(struct usb_mixer_interface *mixer,
2636 const struct snd_kcontrol_new *ncontrol,
2637 int index, int channels, const char *name,
2638 struct snd_kcontrol **kctl_return)
2640 struct snd_kcontrol *kctl;
2641 struct usb_mixer_elem_info *elem;
2644 elem = kzalloc(sizeof(*elem), GFP_KERNEL);
2648 /* We set USB_MIXER_BESPOKEN type, so that the core USB mixer code
2649 * ignores them for resume and other operations.
2650 * Also, the head.id field is set to 0, as we don't use this field.
2652 elem->head.mixer = mixer;
2653 elem->control = index;
2655 elem->channels = channels;
2656 elem->val_type = USB_MIXER_BESPOKEN;
2658 kctl = snd_ctl_new1(ncontrol, elem);
2663 kctl->private_free = snd_usb_mixer_elem_free;
2665 strscpy(kctl->id.name, name, sizeof(kctl->id.name));
2667 err = snd_usb_mixer_add_control(&elem->head, kctl);
2672 *kctl_return = kctl;
2677 /*** Firmware Version Control ***/
2679 static int scarlett2_firmware_version_ctl_get(
2680 struct snd_kcontrol *kctl,
2681 struct snd_ctl_elem_value *ucontrol)
2683 struct usb_mixer_elem_info *elem = kctl->private_data;
2684 struct scarlett2_data *private = elem->head.mixer->private_data;
2686 ucontrol->value.integer.value[0] = private->firmware_version;
2691 static int scarlett2_firmware_version_ctl_info(
2692 struct snd_kcontrol *kctl,
2693 struct snd_ctl_elem_info *uinfo)
2695 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2701 static const struct snd_kcontrol_new scarlett2_firmware_version_ctl = {
2702 .iface = SNDRV_CTL_ELEM_IFACE_CARD,
2703 .access = SNDRV_CTL_ELEM_ACCESS_READ,
2705 .info = scarlett2_firmware_version_ctl_info,
2706 .get = scarlett2_firmware_version_ctl_get
2709 static int scarlett2_add_firmware_version_ctl(
2710 struct usb_mixer_interface *mixer)
2712 return scarlett2_add_new_ctl(mixer, &scarlett2_firmware_version_ctl,
2713 0, 0, "Firmware Version", NULL);
2716 /*** Minimum Firmware Version Control ***/
2718 static int scarlett2_min_firmware_version_ctl_get(
2719 struct snd_kcontrol *kctl,
2720 struct snd_ctl_elem_value *ucontrol)
2722 struct usb_mixer_elem_info *elem = kctl->private_data;
2723 struct scarlett2_data *private = elem->head.mixer->private_data;
2725 ucontrol->value.integer.value[0] = private->info->min_firmware_version;
2730 static int scarlett2_min_firmware_version_ctl_info(
2731 struct snd_kcontrol *kctl,
2732 struct snd_ctl_elem_info *uinfo)
2734 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2740 static const struct snd_kcontrol_new scarlett2_min_firmware_version_ctl = {
2741 .iface = SNDRV_CTL_ELEM_IFACE_CARD,
2742 .access = SNDRV_CTL_ELEM_ACCESS_READ,
2744 .info = scarlett2_min_firmware_version_ctl_info,
2745 .get = scarlett2_min_firmware_version_ctl_get
2748 static int scarlett2_add_min_firmware_version_ctl(
2749 struct usb_mixer_interface *mixer)
2751 return scarlett2_add_new_ctl(mixer, &scarlett2_min_firmware_version_ctl,
2752 0, 0, "Minimum Firmware Version", NULL);
2755 /*** Sync Control ***/
2757 /* Update sync control after receiving notification that the status
2760 static int scarlett2_update_sync(struct usb_mixer_interface *mixer)
2762 struct scarlett2_data *private = mixer->private_data;
2764 private->sync_updated = 0;
2765 return scarlett2_usb_get_sync_status(mixer, &private->sync);
2768 static int scarlett2_sync_ctl_info(struct snd_kcontrol *kctl,
2769 struct snd_ctl_elem_info *uinfo)
2771 static const char *texts[2] = {
2772 "Unlocked", "Locked"
2774 return snd_ctl_enum_info(uinfo, 1, 2, texts);
2777 static int scarlett2_sync_ctl_get(struct snd_kcontrol *kctl,
2778 struct snd_ctl_elem_value *ucontrol)
2780 struct usb_mixer_elem_info *elem = kctl->private_data;
2781 struct usb_mixer_interface *mixer = elem->head.mixer;
2782 struct scarlett2_data *private = mixer->private_data;
2785 mutex_lock(&private->data_mutex);
2787 if (private->hwdep_in_use) {
2792 if (private->sync_updated) {
2793 err = scarlett2_update_sync(mixer);
2797 ucontrol->value.enumerated.item[0] = private->sync;
2800 mutex_unlock(&private->data_mutex);
2804 static const struct snd_kcontrol_new scarlett2_sync_ctl = {
2805 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2806 .access = SNDRV_CTL_ELEM_ACCESS_READ,
2808 .info = scarlett2_sync_ctl_info,
2809 .get = scarlett2_sync_ctl_get
2812 static int scarlett2_add_sync_ctl(struct usb_mixer_interface *mixer)
2814 struct scarlett2_data *private = mixer->private_data;
2816 /* devices without a mixer also don't support reporting sync status */
2817 if (!scarlett2_has_mixer(private))
2820 return scarlett2_add_new_ctl(mixer, &scarlett2_sync_ctl,
2821 0, 1, "Sync Status", &private->sync_ctl);
2824 /*** Autogain Switch and Status Controls ***/
2826 /* Forward declarations as phantom power and autogain can disable each other */
2827 static int scarlett2_check_input_phantom_updated(struct usb_mixer_interface *);
2828 static int scarlett2_phantom_is_switching(struct scarlett2_data *, int);
2830 /* Set the access mode of a control to read-only (val = 0) or
2831 * read-write (val = 1).
2833 static void scarlett2_set_ctl_access(struct snd_kcontrol *kctl, int val)
2836 kctl->vd[0].access |= SNDRV_CTL_ELEM_ACCESS_WRITE;
2838 kctl->vd[0].access &= ~SNDRV_CTL_ELEM_ACCESS_WRITE;
2841 /* Check if autogain is running on any input */
2842 static int scarlett2_autogain_is_running(struct scarlett2_data *private)
2846 /* autogain_status[] is 0 if autogain is running */
2847 for (i = 0; i < private->info->gain_input_count; i++)
2848 if (!private->autogain_status[i])
2854 static int scarlett2_update_autogain(struct usb_mixer_interface *mixer)
2856 struct scarlett2_data *private = mixer->private_data;
2857 const struct scarlett2_device_info *info = private->info;
2859 u8 raw_autogain_status[SCARLETT2_INPUT_GAIN_MAX];
2861 private->autogain_updated = 0;
2863 if (!info->gain_input_count)
2866 err = scarlett2_usb_get_config(
2867 mixer, SCARLETT2_CONFIG_AUTOGAIN_SWITCH,
2868 info->gain_input_count, private->autogain_switch);
2871 err = scarlett2_usb_get_config(
2872 mixer, SCARLETT2_CONFIG_AUTOGAIN_STATUS,
2873 info->gain_input_count, raw_autogain_status);
2877 /* Translate autogain_switch and raw_autogain_status into
2880 * When autogain_switch[] is set, the status is the first
2881 * element in scarlett2_autogain_status_texts[] (Running). The
2882 * subsequent elements correspond to the status value from the
2883 * device (raw_autogain_status[]) + 1. The last element is
2884 * "Invalid", in case the device reports a status outside the
2885 * range of scarlett2_autogain_status_texts[].
2887 for (i = 0; i < info->gain_input_count; i++)
2888 if (private->autogain_switch[i])
2889 private->autogain_status[i] = 0;
2890 else if (raw_autogain_status[i] <
2891 ARRAY_SIZE(scarlett2_autogain_status_texts) - 1)
2892 private->autogain_status[i] =
2893 raw_autogain_status[i] + 1;
2895 private->autogain_status[i] =
2896 ARRAY_SIZE(scarlett2_autogain_status_texts) - 1;
2901 /* Update access mode for controls affected by autogain */
2902 static void scarlett2_autogain_update_access(struct usb_mixer_interface *mixer)
2904 struct scarlett2_data *private = mixer->private_data;
2905 const struct scarlett2_device_info *info = private->info;
2906 int val = !scarlett2_autogain_is_running(private);
2909 scarlett2_set_ctl_access(private->input_select_ctl, val);
2910 for (i = 0; i < info->gain_input_count / 2; i++)
2911 scarlett2_set_ctl_access(private->input_link_ctls[i], val);
2912 for (i = 0; i < info->gain_input_count; i++) {
2913 scarlett2_set_ctl_access(private->input_gain_ctls[i], val);
2914 scarlett2_set_ctl_access(private->safe_ctls[i], val);
2916 for (i = 0; i < info->level_input_count; i++)
2917 scarlett2_set_ctl_access(private->level_ctls[i], val);
2918 for (i = 0; i < info->air_input_count; i++)
2919 scarlett2_set_ctl_access(private->air_ctls[i], val);
2920 for (i = 0; i < info->phantom_count; i++)
2921 scarlett2_set_ctl_access(private->phantom_ctls[i], val);
2924 /* Notify of access mode change for all controls read-only while
2927 static void scarlett2_autogain_notify_access(struct usb_mixer_interface *mixer)
2929 struct snd_card *card = mixer->chip->card;
2930 struct scarlett2_data *private = mixer->private_data;
2931 const struct scarlett2_device_info *info = private->info;
2934 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_INFO,
2935 &private->input_select_ctl->id);
2936 for (i = 0; i < info->gain_input_count / 2; i++)
2937 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_INFO,
2938 &private->input_link_ctls[i]->id);
2939 for (i = 0; i < info->gain_input_count; i++) {
2940 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_INFO,
2941 &private->input_gain_ctls[i]->id);
2942 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_INFO,
2943 &private->safe_ctls[i]->id);
2945 for (i = 0; i < info->level_input_count; i++)
2946 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_INFO,
2947 &private->level_ctls[i]->id);
2948 for (i = 0; i < info->air_input_count; i++)
2949 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_INFO,
2950 &private->air_ctls[i]->id);
2951 for (i = 0; i < info->phantom_count; i++)
2952 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_INFO,
2953 &private->phantom_ctls[i]->id);
2956 /* Call scarlett2_update_autogain() and
2957 * scarlett2_autogain_update_access() if autogain_updated is set.
2959 static int scarlett2_check_autogain_updated(
2960 struct usb_mixer_interface *mixer)
2962 struct scarlett2_data *private = mixer->private_data;
2965 if (!private->autogain_updated)
2968 err = scarlett2_update_autogain(mixer);
2972 scarlett2_autogain_update_access(mixer);
2977 /* If autogain_updated is set when a *_ctl_put() function for a
2978 * control that is meant to be read-only while autogain is running,
2979 * update the autogain status and access mode of affected controls.
2980 * Return -EPERM if autogain is running.
2982 static int scarlett2_check_put_during_autogain(
2983 struct usb_mixer_interface *mixer)
2985 int err = scarlett2_check_autogain_updated(mixer);
2990 if (scarlett2_autogain_is_running(mixer->private_data))
2996 static int scarlett2_autogain_switch_ctl_info(
2997 struct snd_kcontrol *kctl, struct snd_ctl_elem_info *uinfo)
2999 struct usb_mixer_elem_info *elem = kctl->private_data;
3000 struct usb_mixer_interface *mixer = elem->head.mixer;
3001 struct scarlett2_data *private = mixer->private_data;
3004 mutex_lock(&private->data_mutex);
3006 err = scarlett2_check_input_phantom_updated(mixer);
3010 err = snd_ctl_boolean_mono_info(kctl, uinfo);
3013 mutex_unlock(&private->data_mutex);
3017 static int scarlett2_autogain_switch_ctl_get(
3018 struct snd_kcontrol *kctl, struct snd_ctl_elem_value *ucontrol)
3020 struct usb_mixer_elem_info *elem = kctl->private_data;
3021 struct usb_mixer_interface *mixer = elem->head.mixer;
3022 struct scarlett2_data *private = mixer->private_data;
3025 mutex_lock(&private->data_mutex);
3027 if (private->hwdep_in_use) {
3032 err = scarlett2_check_autogain_updated(mixer);
3036 ucontrol->value.enumerated.item[0] =
3037 private->autogain_switch[elem->control];
3040 mutex_unlock(&private->data_mutex);
3044 static int scarlett2_autogain_status_ctl_get(
3045 struct snd_kcontrol *kctl, struct snd_ctl_elem_value *ucontrol)
3047 struct usb_mixer_elem_info *elem = kctl->private_data;
3048 struct usb_mixer_interface *mixer = elem->head.mixer;
3049 struct scarlett2_data *private = mixer->private_data;
3052 mutex_lock(&private->data_mutex);
3054 if (private->hwdep_in_use) {
3059 err = scarlett2_check_autogain_updated(mixer);
3063 ucontrol->value.enumerated.item[0] =
3064 private->autogain_status[elem->control];
3067 mutex_unlock(&private->data_mutex);
3071 static int scarlett2_autogain_switch_ctl_put(
3072 struct snd_kcontrol *kctl, struct snd_ctl_elem_value *ucontrol)
3074 struct usb_mixer_elem_info *elem = kctl->private_data;
3075 struct usb_mixer_interface *mixer = elem->head.mixer;
3076 struct scarlett2_data *private = mixer->private_data;
3078 int index = elem->control;
3081 mutex_lock(&private->data_mutex);
3083 if (private->hwdep_in_use) {
3088 err = scarlett2_check_input_phantom_updated(mixer);
3092 if (scarlett2_phantom_is_switching(private, index)) {
3097 oval = private->autogain_switch[index];
3098 val = !!ucontrol->value.integer.value[0];
3103 private->autogain_switch[index] = val;
3105 /* Send switch change to the device */
3106 err = scarlett2_usb_set_config(
3107 mixer, SCARLETT2_CONFIG_AUTOGAIN_SWITCH, index, val);
3111 scarlett2_autogain_update_access(mixer);
3112 scarlett2_autogain_notify_access(mixer);
3115 mutex_unlock(&private->data_mutex);
3119 static int scarlett2_autogain_status_ctl_info(
3120 struct snd_kcontrol *kctl, struct snd_ctl_elem_info *uinfo)
3122 return snd_ctl_enum_info(
3124 ARRAY_SIZE(scarlett2_autogain_status_texts),
3125 scarlett2_autogain_status_texts);
3128 static const struct snd_kcontrol_new scarlett2_autogain_switch_ctl = {
3129 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3131 .info = scarlett2_autogain_switch_ctl_info,
3132 .get = scarlett2_autogain_switch_ctl_get,
3133 .put = scarlett2_autogain_switch_ctl_put
3136 static const struct snd_kcontrol_new scarlett2_autogain_status_ctl = {
3137 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3138 .access = SNDRV_CTL_ELEM_ACCESS_READ,
3140 .info = scarlett2_autogain_status_ctl_info,
3141 .get = scarlett2_autogain_status_ctl_get,
3144 /*** Input Select Control ***/
3146 static int scarlett2_update_input_select(struct usb_mixer_interface *mixer)
3148 struct scarlett2_data *private = mixer->private_data;
3149 const struct scarlett2_device_info *info = private->info;
3150 int link_count = info->gain_input_count / 2;
3153 private->input_select_updated = 0;
3158 err = scarlett2_usb_get_config(
3159 mixer, SCARLETT2_CONFIG_INPUT_SELECT_SWITCH,
3160 1, &private->input_select_switch);
3164 err = scarlett2_usb_get_config(
3165 mixer, SCARLETT2_CONFIG_INPUT_LINK_SWITCH,
3166 link_count, private->input_link_switch);
3170 /* simplified because no model yet has link_count > 1 */
3171 if (private->input_link_switch[0])
3172 private->input_select_switch = 0;
3177 static int scarlett2_input_select_ctl_get(
3178 struct snd_kcontrol *kctl, struct snd_ctl_elem_value *ucontrol)
3180 struct usb_mixer_elem_info *elem = kctl->private_data;
3181 struct usb_mixer_interface *mixer = elem->head.mixer;
3182 struct scarlett2_data *private = mixer->private_data;
3185 mutex_lock(&private->data_mutex);
3187 if (private->hwdep_in_use) {
3192 if (private->input_select_updated) {
3193 err = scarlett2_update_input_select(mixer);
3197 ucontrol->value.enumerated.item[0] = private->input_select_switch;
3200 mutex_unlock(&private->data_mutex);
3204 static int scarlett2_input_select_ctl_put(
3205 struct snd_kcontrol *kctl, struct snd_ctl_elem_value *ucontrol)
3207 struct usb_mixer_elem_info *elem = kctl->private_data;
3208 struct usb_mixer_interface *mixer = elem->head.mixer;
3209 struct scarlett2_data *private = mixer->private_data;
3212 int max_val = private->input_link_switch[0] ? 0 : 1;
3214 mutex_lock(&private->data_mutex);
3216 if (private->hwdep_in_use) {
3221 err = scarlett2_check_put_during_autogain(mixer);
3225 oval = private->input_select_switch;
3226 val = ucontrol->value.integer.value[0];
3230 else if (val > max_val)
3236 private->input_select_switch = val;
3238 /* Send switch change to the device if inputs not linked */
3239 if (!private->input_link_switch[0])
3240 err = scarlett2_usb_set_config(
3241 mixer, SCARLETT2_CONFIG_INPUT_SELECT_SWITCH,
3247 mutex_unlock(&private->data_mutex);
3251 static int scarlett2_input_select_ctl_info(
3252 struct snd_kcontrol *kctl, struct snd_ctl_elem_info *uinfo)
3254 struct usb_mixer_elem_info *elem = kctl->private_data;
3255 struct usb_mixer_interface *mixer = elem->head.mixer;
3256 struct scarlett2_data *private = mixer->private_data;
3258 int inputs = private->info->gain_input_count;
3261 char **values = kcalloc(inputs, sizeof(char *), GFP_KERNEL);
3266 mutex_lock(&private->data_mutex);
3268 if (private->hwdep_in_use) {
3273 err = scarlett2_check_autogain_updated(mixer);
3277 /* Loop through each input
3278 * Linked inputs have one value for the pair
3280 for (i = 0, j = 0; i < inputs; i++) {
3281 if (private->input_link_switch[i / 2]) {
3282 values[j++] = kasprintf(
3283 GFP_KERNEL, "Input %d-%d", i + 1, i + 2);
3286 values[j++] = kasprintf(
3287 GFP_KERNEL, "Input %d", i + 1);
3291 err = snd_ctl_enum_info(uinfo, 1, j,
3292 (const char * const *)values);
3295 mutex_unlock(&private->data_mutex);
3297 for (i = 0; i < inputs; i++)
3304 static const struct snd_kcontrol_new scarlett2_input_select_ctl = {
3305 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3307 .info = scarlett2_input_select_ctl_info,
3308 .get = scarlett2_input_select_ctl_get,
3309 .put = scarlett2_input_select_ctl_put,
3312 /*** Input Link Switch Controls ***/
3314 /* snd_ctl_boolean_mono_info() with autogain-updated check
3315 * (for controls that are read-only while autogain is running)
3317 static int scarlett2_autogain_disables_ctl_info(struct snd_kcontrol *kctl,
3318 struct snd_ctl_elem_info *uinfo)
3320 struct usb_mixer_elem_info *elem = kctl->private_data;
3321 struct usb_mixer_interface *mixer = elem->head.mixer;
3322 struct scarlett2_data *private = mixer->private_data;
3325 mutex_lock(&private->data_mutex);
3327 if (private->hwdep_in_use) {
3332 err = scarlett2_check_autogain_updated(mixer);
3336 err = snd_ctl_boolean_mono_info(kctl, uinfo);
3339 mutex_unlock(&private->data_mutex);
3343 static int scarlett2_input_link_ctl_get(
3344 struct snd_kcontrol *kctl, struct snd_ctl_elem_value *ucontrol)
3346 struct usb_mixer_elem_info *elem = kctl->private_data;
3347 struct usb_mixer_interface *mixer = elem->head.mixer;
3348 struct scarlett2_data *private = mixer->private_data;
3351 mutex_lock(&private->data_mutex);
3353 if (private->hwdep_in_use) {
3358 if (private->input_select_updated) {
3359 err = scarlett2_update_input_select(mixer);
3363 ucontrol->value.enumerated.item[0] =
3364 private->input_link_switch[elem->control];
3367 mutex_unlock(&private->data_mutex);
3371 static int scarlett2_input_link_ctl_put(
3372 struct snd_kcontrol *kctl, struct snd_ctl_elem_value *ucontrol)
3374 struct usb_mixer_elem_info *elem = kctl->private_data;
3375 struct usb_mixer_interface *mixer = elem->head.mixer;
3376 struct scarlett2_data *private = mixer->private_data;
3378 int index = elem->control;
3381 mutex_lock(&private->data_mutex);
3383 if (private->hwdep_in_use) {
3388 err = scarlett2_check_put_during_autogain(mixer);
3392 oval = private->input_link_switch[index];
3393 val = !!ucontrol->value.integer.value[0];
3398 private->input_link_switch[index] = val;
3400 /* Notify of change in input select options available */
3401 snd_ctl_notify(mixer->chip->card,
3402 SNDRV_CTL_EVENT_MASK_VALUE | SNDRV_CTL_EVENT_MASK_INFO,
3403 &private->input_select_ctl->id);
3404 private->input_select_updated = 1;
3406 /* Send switch change to the device
3407 * Link for channels 1-2 is at index 1
3408 * No device yet has more than 2 channels linked
3410 err = scarlett2_usb_set_config(
3411 mixer, SCARLETT2_CONFIG_INPUT_LINK_SWITCH, index + 1, val);
3416 mutex_unlock(&private->data_mutex);
3420 static const struct snd_kcontrol_new scarlett2_input_link_ctl = {
3421 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3423 .info = scarlett2_autogain_disables_ctl_info,
3424 .get = scarlett2_input_link_ctl_get,
3425 .put = scarlett2_input_link_ctl_put
3428 /*** Input Gain Controls ***/
3430 static int scarlett2_update_input_gain(struct usb_mixer_interface *mixer)
3432 struct scarlett2_data *private = mixer->private_data;
3433 const struct scarlett2_device_info *info = private->info;
3435 private->input_gain_updated = 0;
3437 if (!info->gain_input_count)
3440 return scarlett2_usb_get_config(
3441 mixer, SCARLETT2_CONFIG_INPUT_GAIN,
3442 info->gain_input_count, private->gain);
3445 static int scarlett2_input_gain_ctl_info(struct snd_kcontrol *kctl,
3446 struct snd_ctl_elem_info *uinfo)
3448 struct usb_mixer_elem_info *elem = kctl->private_data;
3449 struct usb_mixer_interface *mixer = elem->head.mixer;
3450 struct scarlett2_data *private = mixer->private_data;
3453 mutex_lock(&private->data_mutex);
3455 if (private->hwdep_in_use) {
3460 err = scarlett2_check_autogain_updated(mixer);
3464 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
3465 uinfo->count = elem->channels;
3466 uinfo->value.integer.min = 0;
3467 uinfo->value.integer.max = SCARLETT2_GAIN_BIAS;
3468 uinfo->value.integer.step = 1;
3471 mutex_unlock(&private->data_mutex);
3475 static int scarlett2_input_gain_ctl_get(struct snd_kcontrol *kctl,
3476 struct snd_ctl_elem_value *ucontrol)
3478 struct usb_mixer_elem_info *elem = kctl->private_data;
3479 struct usb_mixer_interface *mixer = elem->head.mixer;
3480 struct scarlett2_data *private = mixer->private_data;
3483 mutex_lock(&private->data_mutex);
3485 if (private->hwdep_in_use) {
3490 if (private->input_gain_updated) {
3491 err = scarlett2_update_input_gain(mixer);
3495 ucontrol->value.integer.value[0] =
3496 private->gain[elem->control];
3499 mutex_unlock(&private->data_mutex);
3503 static int scarlett2_input_gain_ctl_put(struct snd_kcontrol *kctl,
3504 struct snd_ctl_elem_value *ucontrol)
3506 struct usb_mixer_elem_info *elem = kctl->private_data;
3507 struct usb_mixer_interface *mixer = elem->head.mixer;
3508 struct scarlett2_data *private = mixer->private_data;
3510 int index = elem->control;
3513 mutex_lock(&private->data_mutex);
3515 if (private->hwdep_in_use) {
3520 err = scarlett2_check_put_during_autogain(mixer);
3524 oval = private->gain[index];
3525 val = ucontrol->value.integer.value[0];
3530 private->gain[index] = val;
3532 /* Send gain change to the device */
3533 err = scarlett2_usb_set_config(mixer, SCARLETT2_CONFIG_INPUT_GAIN,
3539 mutex_unlock(&private->data_mutex);
3543 static const DECLARE_TLV_DB_MINMAX(
3544 db_scale_scarlett2_gain, -SCARLETT2_GAIN_BIAS * 100, 0
3547 static const struct snd_kcontrol_new scarlett2_input_gain_ctl = {
3548 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3549 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE |
3550 SNDRV_CTL_ELEM_ACCESS_TLV_READ,
3552 .info = scarlett2_input_gain_ctl_info,
3553 .get = scarlett2_input_gain_ctl_get,
3554 .put = scarlett2_input_gain_ctl_put,
3555 .private_value = 0, /* max value */
3556 .tlv = { .p = db_scale_scarlett2_gain }
3559 /*** Safe Controls ***/
3561 static int scarlett2_update_input_safe(struct usb_mixer_interface *mixer)
3563 struct scarlett2_data *private = mixer->private_data;
3564 const struct scarlett2_device_info *info = private->info;
3566 private->input_safe_updated = 0;
3568 if (!info->gain_input_count)
3571 return scarlett2_usb_get_config(
3572 mixer, SCARLETT2_CONFIG_SAFE_SWITCH,
3573 info->gain_input_count, private->safe_switch);
3576 static int scarlett2_safe_ctl_get(struct snd_kcontrol *kctl,
3577 struct snd_ctl_elem_value *ucontrol)
3579 struct usb_mixer_elem_info *elem = kctl->private_data;
3580 struct usb_mixer_interface *mixer = elem->head.mixer;
3581 struct scarlett2_data *private = mixer->private_data;
3584 mutex_lock(&private->data_mutex);
3586 if (private->hwdep_in_use) {
3591 if (private->input_safe_updated) {
3592 err = scarlett2_update_input_safe(mixer);
3596 ucontrol->value.integer.value[0] =
3597 private->safe_switch[elem->control];
3600 mutex_unlock(&private->data_mutex);
3604 static int scarlett2_safe_ctl_put(struct snd_kcontrol *kctl,
3605 struct snd_ctl_elem_value *ucontrol)
3607 struct usb_mixer_elem_info *elem = kctl->private_data;
3608 struct usb_mixer_interface *mixer = elem->head.mixer;
3609 struct scarlett2_data *private = mixer->private_data;
3611 int index = elem->control;
3614 mutex_lock(&private->data_mutex);
3616 if (private->hwdep_in_use) {
3621 err = scarlett2_check_put_during_autogain(mixer);
3625 oval = private->safe_switch[index];
3626 val = !!ucontrol->value.integer.value[0];
3631 private->safe_switch[index] = val;
3633 /* Send switch change to the device */
3634 err = scarlett2_usb_set_config(mixer, SCARLETT2_CONFIG_SAFE_SWITCH,
3640 mutex_unlock(&private->data_mutex);
3644 static const struct snd_kcontrol_new scarlett2_safe_ctl = {
3645 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3647 .info = scarlett2_autogain_disables_ctl_info,
3648 .get = scarlett2_safe_ctl_get,
3649 .put = scarlett2_safe_ctl_put,
3652 /*** PCM Input Control ***/
3654 static int scarlett2_update_pcm_input_switch(struct usb_mixer_interface *mixer)
3656 struct scarlett2_data *private = mixer->private_data;
3659 private->pcm_input_switch_updated = 0;
3661 err = scarlett2_usb_get_config(
3662 mixer, SCARLETT2_CONFIG_PCM_INPUT_SWITCH,
3663 1, &private->pcm_input_switch);
3670 static int scarlett2_pcm_input_switch_ctl_get(
3671 struct snd_kcontrol *kctl, struct snd_ctl_elem_value *ucontrol)
3673 struct usb_mixer_elem_info *elem = kctl->private_data;
3674 struct usb_mixer_interface *mixer = elem->head.mixer;
3675 struct scarlett2_data *private = elem->head.mixer->private_data;
3678 mutex_lock(&private->data_mutex);
3680 if (private->pcm_input_switch_updated) {
3681 err = scarlett2_update_pcm_input_switch(mixer);
3685 ucontrol->value.enumerated.item[0] = private->pcm_input_switch;
3688 mutex_unlock(&private->data_mutex);
3692 static int scarlett2_pcm_input_switch_ctl_put(
3693 struct snd_kcontrol *kctl, struct snd_ctl_elem_value *ucontrol)
3695 struct usb_mixer_elem_info *elem = kctl->private_data;
3696 struct usb_mixer_interface *mixer = elem->head.mixer;
3697 struct scarlett2_data *private = mixer->private_data;
3699 int oval, val, err = 0;
3701 mutex_lock(&private->data_mutex);
3703 if (private->hwdep_in_use) {
3708 oval = private->pcm_input_switch;
3709 val = !!ucontrol->value.integer.value[0];
3714 private->pcm_input_switch = val;
3716 /* Send switch change to the device */
3717 err = scarlett2_usb_set_config(
3718 mixer, SCARLETT2_CONFIG_PCM_INPUT_SWITCH,
3724 mutex_unlock(&private->data_mutex);
3728 static int scarlett2_pcm_input_switch_ctl_info(
3729 struct snd_kcontrol *kctl, struct snd_ctl_elem_info *uinfo)
3731 static const char *const values[2] = {
3735 return snd_ctl_enum_info(
3736 uinfo, 1, 2, values);
3739 static const struct snd_kcontrol_new scarlett2_pcm_input_switch_ctl = {
3740 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3742 .info = scarlett2_pcm_input_switch_ctl_info,
3743 .get = scarlett2_pcm_input_switch_ctl_get,
3744 .put = scarlett2_pcm_input_switch_ctl_put
3747 /*** Analogue Line Out Volume Controls ***/
3749 /* Update hardware volume controls after receiving notification that
3752 static int scarlett2_update_volumes(struct usb_mixer_interface *mixer)
3754 struct scarlett2_data *private = mixer->private_data;
3758 private->vol_updated = 0;
3760 if (scarlett2_has_config_item(private,
3761 SCARLETT2_CONFIG_MASTER_VOLUME)) {
3762 err = scarlett2_usb_get_config(
3763 mixer, SCARLETT2_CONFIG_MASTER_VOLUME,
3768 private->master_vol = clamp(vol + SCARLETT2_VOLUME_BIAS,
3769 0, SCARLETT2_VOLUME_BIAS);
3771 if (scarlett2_has_config_item(private,
3772 SCARLETT2_CONFIG_SW_HW_SWITCH))
3773 for (i = 0; i < private->num_line_out; i++)
3774 if (private->vol_sw_hw_switch[i])
3775 private->vol[i] = private->master_vol;
3778 if (scarlett2_has_config_item(private,
3779 SCARLETT2_CONFIG_HEADPHONE_VOLUME)) {
3780 err = scarlett2_usb_get_config(
3781 mixer, SCARLETT2_CONFIG_HEADPHONE_VOLUME,
3786 private->headphone_vol = clamp(vol + SCARLETT2_VOLUME_BIAS,
3787 0, SCARLETT2_VOLUME_BIAS);
3793 static int scarlett2_volume_ctl_info(struct snd_kcontrol *kctl,
3794 struct snd_ctl_elem_info *uinfo)
3796 struct usb_mixer_elem_info *elem = kctl->private_data;
3798 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
3799 uinfo->count = elem->channels;
3800 uinfo->value.integer.min = 0;
3801 uinfo->value.integer.max = SCARLETT2_VOLUME_BIAS;
3802 uinfo->value.integer.step = 1;
3806 static int scarlett2_master_volume_ctl_get(struct snd_kcontrol *kctl,
3807 struct snd_ctl_elem_value *ucontrol)
3809 struct usb_mixer_elem_info *elem = kctl->private_data;
3810 struct usb_mixer_interface *mixer = elem->head.mixer;
3811 struct scarlett2_data *private = mixer->private_data;
3814 mutex_lock(&private->data_mutex);
3816 if (private->hwdep_in_use) {
3821 if (private->vol_updated) {
3822 err = scarlett2_update_volumes(mixer);
3826 ucontrol->value.integer.value[0] = private->master_vol;
3829 mutex_unlock(&private->data_mutex);
3833 static int scarlett2_headphone_volume_ctl_get(
3834 struct snd_kcontrol *kctl,
3835 struct snd_ctl_elem_value *ucontrol)
3837 struct usb_mixer_elem_info *elem = kctl->private_data;
3838 struct usb_mixer_interface *mixer = elem->head.mixer;
3839 struct scarlett2_data *private = mixer->private_data;
3842 mutex_lock(&private->data_mutex);
3844 if (private->hwdep_in_use) {
3849 if (private->vol_updated) {
3850 err = scarlett2_update_volumes(mixer);
3854 ucontrol->value.integer.value[0] = private->headphone_vol;
3857 mutex_unlock(&private->data_mutex);
3861 static int line_out_remap(struct scarlett2_data *private, int index)
3863 const struct scarlett2_device_info *info = private->info;
3865 if (!info->line_out_remap_enable)
3868 if (index >= private->num_line_out)
3871 return info->line_out_remap[index];
3874 static int scarlett2_volume_ctl_get(struct snd_kcontrol *kctl,
3875 struct snd_ctl_elem_value *ucontrol)
3877 struct usb_mixer_elem_info *elem = kctl->private_data;
3878 struct usb_mixer_interface *mixer = elem->head.mixer;
3879 struct scarlett2_data *private = mixer->private_data;
3880 int index = line_out_remap(private, elem->control);
3883 mutex_lock(&private->data_mutex);
3885 if (private->hwdep_in_use) {
3890 if (private->vol_updated) {
3891 err = scarlett2_update_volumes(mixer);
3895 ucontrol->value.integer.value[0] = private->vol[index];
3898 mutex_unlock(&private->data_mutex);
3902 static int scarlett2_volume_ctl_put(struct snd_kcontrol *kctl,
3903 struct snd_ctl_elem_value *ucontrol)
3905 struct usb_mixer_elem_info *elem = kctl->private_data;
3906 struct usb_mixer_interface *mixer = elem->head.mixer;
3907 struct scarlett2_data *private = mixer->private_data;
3908 int index = line_out_remap(private, elem->control);
3909 int oval, val, err = 0;
3911 mutex_lock(&private->data_mutex);
3913 if (private->hwdep_in_use) {
3918 oval = private->vol[index];
3919 val = ucontrol->value.integer.value[0];
3924 private->vol[index] = val;
3925 err = scarlett2_usb_set_config(mixer, SCARLETT2_CONFIG_LINE_OUT_VOLUME,
3926 index, val - SCARLETT2_VOLUME_BIAS);
3931 mutex_unlock(&private->data_mutex);
3935 static const DECLARE_TLV_DB_MINMAX(
3936 db_scale_scarlett2_volume, -SCARLETT2_VOLUME_BIAS * 100, 0
3939 static const struct snd_kcontrol_new scarlett2_master_volume_ctl = {
3940 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3941 .access = SNDRV_CTL_ELEM_ACCESS_READ |
3942 SNDRV_CTL_ELEM_ACCESS_TLV_READ,
3944 .info = scarlett2_volume_ctl_info,
3945 .get = scarlett2_master_volume_ctl_get,
3946 .private_value = 0, /* max value */
3947 .tlv = { .p = db_scale_scarlett2_volume }
3950 static const struct snd_kcontrol_new scarlett2_headphone_volume_ctl = {
3951 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3952 .access = SNDRV_CTL_ELEM_ACCESS_READ |
3953 SNDRV_CTL_ELEM_ACCESS_TLV_READ,
3955 .info = scarlett2_volume_ctl_info,
3956 .get = scarlett2_headphone_volume_ctl_get,
3957 .private_value = 0, /* max value */
3958 .tlv = { .p = db_scale_scarlett2_volume }
3961 static const struct snd_kcontrol_new scarlett2_line_out_volume_ctl = {
3962 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3963 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE |
3964 SNDRV_CTL_ELEM_ACCESS_TLV_READ,
3966 .info = scarlett2_volume_ctl_info,
3967 .get = scarlett2_volume_ctl_get,
3968 .put = scarlett2_volume_ctl_put,
3969 .private_value = 0, /* max value */
3970 .tlv = { .p = db_scale_scarlett2_volume }
3973 /*** Mute Switch Controls ***/
3975 static int scarlett2_update_dim_mute(struct usb_mixer_interface *mixer)
3977 struct scarlett2_data *private = mixer->private_data;
3981 private->dim_mute_updated = 0;
3983 if (!scarlett2_has_config_item(private, SCARLETT2_CONFIG_SW_HW_SWITCH))
3986 err = scarlett2_usb_get_config(
3987 mixer, SCARLETT2_CONFIG_DIM_MUTE,
3988 SCARLETT2_DIM_MUTE_COUNT, private->dim_mute);
3992 for (i = 0; i < SCARLETT2_DIM_MUTE_COUNT; i++)
3993 private->dim_mute[i] = !!private->dim_mute[i];
3995 mute = private->dim_mute[SCARLETT2_BUTTON_MUTE];
3997 for (i = 0; i < private->num_line_out; i++)
3998 if (private->vol_sw_hw_switch[i])
3999 private->mute_switch[i] = mute;
4004 static int scarlett2_mute_ctl_get(struct snd_kcontrol *kctl,
4005 struct snd_ctl_elem_value *ucontrol)
4007 struct usb_mixer_elem_info *elem = kctl->private_data;
4008 struct usb_mixer_interface *mixer = elem->head.mixer;
4009 struct scarlett2_data *private = mixer->private_data;
4010 int index = line_out_remap(private, elem->control);
4013 mutex_lock(&private->data_mutex);
4015 if (private->hwdep_in_use) {
4020 if (private->dim_mute_updated) {
4021 err = scarlett2_update_dim_mute(mixer);
4025 ucontrol->value.integer.value[0] = private->mute_switch[index];
4028 mutex_unlock(&private->data_mutex);
4032 static int scarlett2_mute_ctl_put(struct snd_kcontrol *kctl,
4033 struct snd_ctl_elem_value *ucontrol)
4035 struct usb_mixer_elem_info *elem = kctl->private_data;
4036 struct usb_mixer_interface *mixer = elem->head.mixer;
4037 struct scarlett2_data *private = mixer->private_data;
4038 int index = line_out_remap(private, elem->control);
4039 int oval, val, err = 0;
4041 mutex_lock(&private->data_mutex);
4043 if (private->hwdep_in_use) {
4048 oval = private->mute_switch[index];
4049 val = !!ucontrol->value.integer.value[0];
4054 private->mute_switch[index] = val;
4056 /* Send mute change to the device */
4057 err = scarlett2_usb_set_config(mixer, SCARLETT2_CONFIG_MUTE_SWITCH,
4063 mutex_unlock(&private->data_mutex);
4067 static const struct snd_kcontrol_new scarlett2_mute_ctl = {
4068 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
4070 .info = snd_ctl_boolean_mono_info,
4071 .get = scarlett2_mute_ctl_get,
4072 .put = scarlett2_mute_ctl_put,
4075 /*** HW/SW Volume Switch Controls ***/
4077 static void scarlett2_sw_hw_ctl_ro(struct scarlett2_data *private, int index)
4079 private->sw_hw_ctls[index]->vd[0].access &=
4080 ~SNDRV_CTL_ELEM_ACCESS_WRITE;
4083 static void scarlett2_sw_hw_ctl_rw(struct scarlett2_data *private, int index)
4085 private->sw_hw_ctls[index]->vd[0].access |=
4086 SNDRV_CTL_ELEM_ACCESS_WRITE;
4089 static int scarlett2_sw_hw_enum_ctl_info(struct snd_kcontrol *kctl,
4090 struct snd_ctl_elem_info *uinfo)
4092 static const char *const values[2] = {
4096 return snd_ctl_enum_info(uinfo, 1, 2, values);
4099 static int scarlett2_sw_hw_enum_ctl_get(struct snd_kcontrol *kctl,
4100 struct snd_ctl_elem_value *ucontrol)
4102 struct usb_mixer_elem_info *elem = kctl->private_data;
4103 struct scarlett2_data *private = elem->head.mixer->private_data;
4104 int index = line_out_remap(private, elem->control);
4106 ucontrol->value.enumerated.item[0] = private->vol_sw_hw_switch[index];
4110 static void scarlett2_vol_ctl_set_writable(struct usb_mixer_interface *mixer,
4111 int index, int value)
4113 struct scarlett2_data *private = mixer->private_data;
4114 struct snd_card *card = mixer->chip->card;
4116 /* Set/Clear write bits */
4118 private->vol_ctls[index]->vd[0].access |=
4119 SNDRV_CTL_ELEM_ACCESS_WRITE;
4120 private->mute_ctls[index]->vd[0].access |=
4121 SNDRV_CTL_ELEM_ACCESS_WRITE;
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;
4129 /* Notify of write bit and possible value change */
4130 snd_ctl_notify(card,
4131 SNDRV_CTL_EVENT_MASK_VALUE | SNDRV_CTL_EVENT_MASK_INFO,
4132 &private->vol_ctls[index]->id);
4133 snd_ctl_notify(card,
4134 SNDRV_CTL_EVENT_MASK_VALUE | SNDRV_CTL_EVENT_MASK_INFO,
4135 &private->mute_ctls[index]->id);
4138 static int scarlett2_sw_hw_change(struct usb_mixer_interface *mixer,
4139 int ctl_index, int val)
4141 struct scarlett2_data *private = mixer->private_data;
4142 int index = line_out_remap(private, ctl_index);
4145 private->vol_sw_hw_switch[index] = val;
4147 /* Change access mode to RO (hardware controlled volume)
4148 * or RW (software controlled volume)
4150 scarlett2_vol_ctl_set_writable(mixer, ctl_index, !val);
4152 /* Reset volume/mute to master volume/mute */
4153 private->vol[index] = private->master_vol;
4154 private->mute_switch[index] = private->dim_mute[SCARLETT2_BUTTON_MUTE];
4156 /* Set SW volume to current HW volume */
4157 err = scarlett2_usb_set_config(
4158 mixer, SCARLETT2_CONFIG_LINE_OUT_VOLUME,
4159 index, private->master_vol - SCARLETT2_VOLUME_BIAS);
4163 /* Set SW mute to current HW mute */
4164 err = scarlett2_usb_set_config(
4165 mixer, SCARLETT2_CONFIG_MUTE_SWITCH,
4166 index, private->dim_mute[SCARLETT2_BUTTON_MUTE]);
4170 /* Send SW/HW switch change to the device */
4171 return scarlett2_usb_set_config(mixer, SCARLETT2_CONFIG_SW_HW_SWITCH,
4175 static int scarlett2_sw_hw_enum_ctl_put(struct snd_kcontrol *kctl,
4176 struct snd_ctl_elem_value *ucontrol)
4178 struct usb_mixer_elem_info *elem = kctl->private_data;
4179 struct usb_mixer_interface *mixer = elem->head.mixer;
4180 struct scarlett2_data *private = mixer->private_data;
4181 int ctl_index = elem->control;
4182 int index = line_out_remap(private, ctl_index);
4183 int oval, val, err = 0;
4185 mutex_lock(&private->data_mutex);
4187 if (private->hwdep_in_use) {
4192 oval = private->vol_sw_hw_switch[index];
4193 val = !!ucontrol->value.enumerated.item[0];
4198 err = scarlett2_sw_hw_change(mixer, ctl_index, val);
4203 mutex_unlock(&private->data_mutex);
4207 static const struct snd_kcontrol_new scarlett2_sw_hw_enum_ctl = {
4208 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
4210 .info = scarlett2_sw_hw_enum_ctl_info,
4211 .get = scarlett2_sw_hw_enum_ctl_get,
4212 .put = scarlett2_sw_hw_enum_ctl_put,
4215 /*** Line Level/Instrument Level Switch Controls ***/
4217 static int scarlett2_update_input_level(struct usb_mixer_interface *mixer)
4219 struct scarlett2_data *private = mixer->private_data;
4220 const struct scarlett2_device_info *info = private->info;
4222 private->input_level_updated = 0;
4224 if (!info->level_input_count)
4227 return scarlett2_usb_get_config(
4228 mixer, SCARLETT2_CONFIG_LEVEL_SWITCH,
4229 info->level_input_count + info->level_input_first,
4230 private->level_switch);
4233 static int scarlett2_level_enum_ctl_info(struct snd_kcontrol *kctl,
4234 struct snd_ctl_elem_info *uinfo)
4236 static const char *const values[2] = {
4239 struct usb_mixer_elem_info *elem = kctl->private_data;
4240 struct usb_mixer_interface *mixer = elem->head.mixer;
4241 struct scarlett2_data *private = mixer->private_data;
4244 mutex_lock(&private->data_mutex);
4246 if (private->hwdep_in_use) {
4251 err = scarlett2_check_autogain_updated(mixer);
4255 err = snd_ctl_enum_info(uinfo, 1, 2, values);
4258 mutex_unlock(&private->data_mutex);
4262 static int scarlett2_level_enum_ctl_get(struct snd_kcontrol *kctl,
4263 struct snd_ctl_elem_value *ucontrol)
4265 struct usb_mixer_elem_info *elem = kctl->private_data;
4266 struct usb_mixer_interface *mixer = elem->head.mixer;
4267 struct scarlett2_data *private = mixer->private_data;
4268 const struct scarlett2_device_info *info = private->info;
4270 int index = elem->control + info->level_input_first;
4273 mutex_lock(&private->data_mutex);
4275 if (private->hwdep_in_use) {
4280 if (private->input_level_updated) {
4281 err = scarlett2_update_input_level(mixer);
4285 ucontrol->value.enumerated.item[0] = scarlett2_decode_muteable(
4286 private->level_switch[index]);
4289 mutex_unlock(&private->data_mutex);
4293 static int scarlett2_level_enum_ctl_put(struct snd_kcontrol *kctl,
4294 struct snd_ctl_elem_value *ucontrol)
4296 struct usb_mixer_elem_info *elem = kctl->private_data;
4297 struct usb_mixer_interface *mixer = elem->head.mixer;
4298 struct scarlett2_data *private = mixer->private_data;
4299 const struct scarlett2_device_info *info = private->info;
4301 int index = elem->control + info->level_input_first;
4304 mutex_lock(&private->data_mutex);
4306 if (private->hwdep_in_use) {
4311 err = scarlett2_check_put_during_autogain(mixer);
4315 oval = private->level_switch[index];
4316 val = !!ucontrol->value.enumerated.item[0];
4321 private->level_switch[index] = val;
4323 /* To set the Gen 4 muteable controls, bit 1 gets set instead */
4324 if (private->config_set->items[SCARLETT2_CONFIG_LEVEL_SWITCH].mute)
4325 val = (!val) | 0x02;
4327 /* Send switch change to the device */
4328 err = scarlett2_usb_set_config(mixer, SCARLETT2_CONFIG_LEVEL_SWITCH,
4334 mutex_unlock(&private->data_mutex);
4338 static const struct snd_kcontrol_new scarlett2_level_enum_ctl = {
4339 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
4341 .info = scarlett2_level_enum_ctl_info,
4342 .get = scarlett2_level_enum_ctl_get,
4343 .put = scarlett2_level_enum_ctl_put,
4346 /*** Pad Switch Controls ***/
4348 static int scarlett2_update_input_pad(struct usb_mixer_interface *mixer)
4350 struct scarlett2_data *private = mixer->private_data;
4351 const struct scarlett2_device_info *info = private->info;
4353 private->input_pad_updated = 0;
4355 if (!info->pad_input_count)
4358 return scarlett2_usb_get_config(
4359 mixer, SCARLETT2_CONFIG_PAD_SWITCH,
4360 info->pad_input_count, private->pad_switch);
4363 static int scarlett2_pad_ctl_get(struct snd_kcontrol *kctl,
4364 struct snd_ctl_elem_value *ucontrol)
4366 struct usb_mixer_elem_info *elem = kctl->private_data;
4367 struct usb_mixer_interface *mixer = elem->head.mixer;
4368 struct scarlett2_data *private = mixer->private_data;
4371 mutex_lock(&private->data_mutex);
4373 if (private->hwdep_in_use) {
4378 if (private->input_pad_updated) {
4379 err = scarlett2_update_input_pad(mixer);
4383 ucontrol->value.integer.value[0] =
4384 private->pad_switch[elem->control];
4387 mutex_unlock(&private->data_mutex);
4391 static int scarlett2_pad_ctl_put(struct snd_kcontrol *kctl,
4392 struct snd_ctl_elem_value *ucontrol)
4394 struct usb_mixer_elem_info *elem = kctl->private_data;
4395 struct usb_mixer_interface *mixer = elem->head.mixer;
4396 struct scarlett2_data *private = mixer->private_data;
4398 int index = elem->control;
4399 int oval, val, err = 0;
4401 mutex_lock(&private->data_mutex);
4403 if (private->hwdep_in_use) {
4408 oval = private->pad_switch[index];
4409 val = !!ucontrol->value.integer.value[0];
4414 private->pad_switch[index] = val;
4416 /* Send switch change to the device */
4417 err = scarlett2_usb_set_config(mixer, SCARLETT2_CONFIG_PAD_SWITCH,
4423 mutex_unlock(&private->data_mutex);
4427 static const struct snd_kcontrol_new scarlett2_pad_ctl = {
4428 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
4430 .info = snd_ctl_boolean_mono_info,
4431 .get = scarlett2_pad_ctl_get,
4432 .put = scarlett2_pad_ctl_put,
4435 /*** Air Switch Controls ***/
4437 static int scarlett2_update_input_air(struct usb_mixer_interface *mixer)
4439 struct scarlett2_data *private = mixer->private_data;
4440 const struct scarlett2_device_info *info = private->info;
4442 private->input_air_updated = 0;
4444 if (!info->air_input_count)
4447 return scarlett2_usb_get_config(
4448 mixer, SCARLETT2_CONFIG_AIR_SWITCH,
4449 info->air_input_count, private->air_switch);
4452 static int scarlett2_air_ctl_get(struct snd_kcontrol *kctl,
4453 struct snd_ctl_elem_value *ucontrol)
4455 struct usb_mixer_elem_info *elem = kctl->private_data;
4456 struct usb_mixer_interface *mixer = elem->head.mixer;
4457 struct scarlett2_data *private = mixer->private_data;
4460 mutex_lock(&private->data_mutex);
4462 if (private->hwdep_in_use) {
4467 if (private->input_air_updated) {
4468 err = scarlett2_update_input_air(mixer);
4472 ucontrol->value.integer.value[0] = private->air_switch[elem->control];
4475 mutex_unlock(&private->data_mutex);
4479 static int scarlett2_air_ctl_put(struct snd_kcontrol *kctl,
4480 struct snd_ctl_elem_value *ucontrol)
4482 struct usb_mixer_elem_info *elem = kctl->private_data;
4483 struct usb_mixer_interface *mixer = elem->head.mixer;
4484 struct scarlett2_data *private = mixer->private_data;
4486 int index = elem->control;
4489 mutex_lock(&private->data_mutex);
4491 if (private->hwdep_in_use) {
4496 err = scarlett2_check_put_during_autogain(mixer);
4500 oval = private->air_switch[index];
4501 val = ucontrol->value.integer.value[0];
4506 private->air_switch[index] = val;
4508 /* Send switch change to the device */
4509 err = scarlett2_usb_set_config(mixer, SCARLETT2_CONFIG_AIR_SWITCH,
4515 mutex_unlock(&private->data_mutex);
4519 static int scarlett2_air_with_drive_ctl_info(
4520 struct snd_kcontrol *kctl, struct snd_ctl_elem_info *uinfo)
4522 static const char *const values[3] = {
4523 "Off", "Presence", "Presence + Drive"
4525 struct usb_mixer_elem_info *elem = kctl->private_data;
4526 struct usb_mixer_interface *mixer = elem->head.mixer;
4527 struct scarlett2_data *private = mixer->private_data;
4530 mutex_lock(&private->data_mutex);
4532 if (private->hwdep_in_use) {
4537 err = scarlett2_check_autogain_updated(mixer);
4541 err = snd_ctl_enum_info(uinfo, 1, 3, values);
4544 mutex_unlock(&private->data_mutex);
4548 static const struct snd_kcontrol_new scarlett2_air_ctl[2] = {
4550 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
4552 .info = snd_ctl_boolean_mono_info,
4553 .get = scarlett2_air_ctl_get,
4554 .put = scarlett2_air_ctl_put,
4557 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
4559 .info = scarlett2_air_with_drive_ctl_info,
4560 .get = scarlett2_air_ctl_get,
4561 .put = scarlett2_air_ctl_put,
4565 /*** Phantom Switch Controls ***/
4567 static int scarlett2_update_input_phantom(struct usb_mixer_interface *mixer)
4569 struct scarlett2_data *private = mixer->private_data;
4570 const struct scarlett2_device_info *info = private->info;
4573 private->input_phantom_updated = 0;
4575 if (!info->phantom_count)
4578 err = scarlett2_usb_get_config(
4579 mixer, SCARLETT2_CONFIG_PHANTOM_SWITCH,
4580 info->phantom_count, private->phantom_switch);
4584 if (scarlett2_has_config_item(private,
4585 SCARLETT2_CONFIG_PHANTOM_PERSISTENCE)) {
4586 err = scarlett2_usb_get_config(
4587 mixer, SCARLETT2_CONFIG_PHANTOM_PERSISTENCE,
4588 1, &private->phantom_persistence);
4596 /* Check if phantom power on the given input is currently changing state */
4597 static int scarlett2_phantom_is_switching(
4598 struct scarlett2_data *private, int line_num)
4600 const struct scarlett2_device_info *info = private->info;
4601 int index = line_num / info->inputs_per_phantom;
4603 return !!(private->phantom_switch[index] & 0x02);
4606 /* Update autogain controls' access mode when phantom power changes state */
4607 static void scarlett2_phantom_update_access(struct usb_mixer_interface *mixer)
4609 struct scarlett2_data *private = mixer->private_data;
4610 const struct scarlett2_device_info *info = private->info;
4613 /* Disable autogain controls if phantom power is changing state */
4614 for (i = 0; i < info->gain_input_count; i++) {
4615 int val = !scarlett2_phantom_is_switching(private, i);
4617 scarlett2_set_ctl_access(private->autogain_ctls[i], val);
4621 /* Notify of access mode change for autogain which can't be enabled
4622 * while phantom power is changing.
4624 static void scarlett2_phantom_notify_access(struct usb_mixer_interface *mixer)
4626 struct snd_card *card = mixer->chip->card;
4627 struct scarlett2_data *private = mixer->private_data;
4628 const struct scarlett2_device_info *info = private->info;
4631 for (i = 0; i < info->gain_input_count; i++)
4632 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_INFO,
4633 &private->autogain_ctls[i]->id);
4636 /* Call scarlett2_update_input_phantom() and
4637 * scarlett2_phantom_update_access() if input_phantom_updated is set.
4639 static int scarlett2_check_input_phantom_updated(
4640 struct usb_mixer_interface *mixer)
4642 struct scarlett2_data *private = mixer->private_data;
4645 if (!private->input_phantom_updated)
4648 err = scarlett2_update_input_phantom(mixer);
4652 scarlett2_phantom_update_access(mixer);
4657 static int scarlett2_phantom_ctl_get(struct snd_kcontrol *kctl,
4658 struct snd_ctl_elem_value *ucontrol)
4660 struct usb_mixer_elem_info *elem = kctl->private_data;
4661 struct usb_mixer_interface *mixer = elem->head.mixer;
4662 struct scarlett2_data *private = mixer->private_data;
4665 mutex_lock(&private->data_mutex);
4667 if (private->hwdep_in_use) {
4672 err = scarlett2_check_input_phantom_updated(mixer);
4676 ucontrol->value.integer.value[0] = scarlett2_decode_muteable(
4677 private->phantom_switch[elem->control]);
4680 mutex_unlock(&private->data_mutex);
4684 static int scarlett2_phantom_ctl_put(struct snd_kcontrol *kctl,
4685 struct snd_ctl_elem_value *ucontrol)
4687 struct usb_mixer_elem_info *elem = kctl->private_data;
4688 struct usb_mixer_interface *mixer = elem->head.mixer;
4689 struct scarlett2_data *private = mixer->private_data;
4690 const struct scarlett2_device_info *info = private->info;
4692 int index = elem->control;
4695 mutex_lock(&private->data_mutex);
4697 if (private->hwdep_in_use) {
4702 err = scarlett2_check_put_during_autogain(mixer);
4706 oval = private->phantom_switch[index];
4707 val = !!ucontrol->value.integer.value[0];
4712 private->phantom_switch[index] = val;
4714 /* To set the Gen 4 muteable controls, bit 1 gets set */
4715 if (private->config_set->items[SCARLETT2_CONFIG_PHANTOM_SWITCH].mute)
4716 val = (!val) | 0x02;
4718 /* Send switch change to the device */
4719 err = scarlett2_usb_set_config(mixer, SCARLETT2_CONFIG_PHANTOM_SWITCH,
4720 index + info->phantom_first, val);
4724 scarlett2_phantom_update_access(mixer);
4725 scarlett2_phantom_notify_access(mixer);
4728 mutex_unlock(&private->data_mutex);
4732 static const struct snd_kcontrol_new scarlett2_phantom_ctl = {
4733 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
4735 .info = scarlett2_autogain_disables_ctl_info,
4736 .get = scarlett2_phantom_ctl_get,
4737 .put = scarlett2_phantom_ctl_put,
4740 /*** Phantom Persistence Control ***/
4742 static int scarlett2_phantom_persistence_ctl_get(
4743 struct snd_kcontrol *kctl, struct snd_ctl_elem_value *ucontrol)
4745 struct usb_mixer_elem_info *elem = kctl->private_data;
4746 struct scarlett2_data *private = elem->head.mixer->private_data;
4748 ucontrol->value.integer.value[0] = private->phantom_persistence;
4752 static int scarlett2_phantom_persistence_ctl_put(
4753 struct snd_kcontrol *kctl, struct snd_ctl_elem_value *ucontrol)
4755 struct usb_mixer_elem_info *elem = kctl->private_data;
4756 struct usb_mixer_interface *mixer = elem->head.mixer;
4757 struct scarlett2_data *private = mixer->private_data;
4759 int index = elem->control;
4760 int oval, val, err = 0;
4762 mutex_lock(&private->data_mutex);
4764 if (private->hwdep_in_use) {
4769 oval = private->phantom_persistence;
4770 val = !!ucontrol->value.integer.value[0];
4775 private->phantom_persistence = val;
4777 /* Send switch change to the device */
4778 err = scarlett2_usb_set_config(
4779 mixer, SCARLETT2_CONFIG_PHANTOM_PERSISTENCE, index, val);
4784 mutex_unlock(&private->data_mutex);
4788 static const struct snd_kcontrol_new scarlett2_phantom_persistence_ctl = {
4789 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
4791 .info = snd_ctl_boolean_mono_info,
4792 .get = scarlett2_phantom_persistence_ctl_get,
4793 .put = scarlett2_phantom_persistence_ctl_put,
4796 /*** Speaker Switching Control ***/
4798 static int scarlett2_update_monitor_other(struct usb_mixer_interface *mixer)
4800 struct scarlett2_data *private = mixer->private_data;
4801 const struct scarlett2_device_info *info = private->info;
4804 /* monitor_other_enable[0] enables speaker switching
4805 * monitor_other_enable[1] enables talkback
4807 u8 monitor_other_enable[2];
4809 /* monitor_other_switch[0] activates the alternate speakers
4810 * monitor_other_switch[1] activates talkback
4812 u8 monitor_other_switch[2];
4814 private->monitor_other_updated = 0;
4816 /* if it doesn't do speaker switching then it also doesn't do
4819 if (!info->has_speaker_switching)
4822 err = scarlett2_usb_get_config(
4823 mixer, SCARLETT2_CONFIG_MONITOR_OTHER_ENABLE,
4824 2, monitor_other_enable);
4828 err = scarlett2_usb_get_config(
4829 mixer, SCARLETT2_CONFIG_MONITOR_OTHER_SWITCH,
4830 2, monitor_other_switch);
4834 if (!monitor_other_enable[0])
4835 private->speaker_switching_switch = 0;
4837 private->speaker_switching_switch = monitor_other_switch[0] + 1;
4839 if (info->has_talkback) {
4843 if (!monitor_other_enable[1])
4844 private->talkback_switch = 0;
4846 private->talkback_switch = monitor_other_switch[1] + 1;
4848 err = scarlett2_usb_get_config(mixer,
4849 SCARLETT2_CONFIG_TALKBACK_MAP,
4853 for (i = 0; i < private->num_mix_out; i++, bitmap >>= 1)
4854 private->talkback_map[i] = bitmap & 1;
4860 static int scarlett2_speaker_switch_enum_ctl_info(
4861 struct snd_kcontrol *kctl, struct snd_ctl_elem_info *uinfo)
4863 static const char *const values[3] = {
4864 "Off", "Main", "Alt"
4867 return snd_ctl_enum_info(uinfo, 1, 3, values);
4870 static int scarlett2_speaker_switch_enum_ctl_get(
4871 struct snd_kcontrol *kctl, struct snd_ctl_elem_value *ucontrol)
4873 struct usb_mixer_elem_info *elem = kctl->private_data;
4874 struct usb_mixer_interface *mixer = elem->head.mixer;
4875 struct scarlett2_data *private = mixer->private_data;
4878 mutex_lock(&private->data_mutex);
4880 if (private->hwdep_in_use) {
4885 if (private->monitor_other_updated) {
4886 err = scarlett2_update_monitor_other(mixer);
4890 ucontrol->value.enumerated.item[0] = private->speaker_switching_switch;
4893 mutex_unlock(&private->data_mutex);
4897 /* when speaker switching gets enabled, switch the main/alt speakers
4898 * to HW volume and disable those controls
4900 static int scarlett2_speaker_switch_enable(struct usb_mixer_interface *mixer)
4902 struct snd_card *card = mixer->chip->card;
4903 struct scarlett2_data *private = mixer->private_data;
4906 for (i = 0; i < 4; i++) {
4907 int index = line_out_remap(private, i);
4909 /* switch the main/alt speakers to HW volume */
4910 if (!private->vol_sw_hw_switch[index]) {
4911 err = scarlett2_sw_hw_change(private->mixer, i, 1);
4916 /* disable the line out SW/HW switch */
4917 scarlett2_sw_hw_ctl_ro(private, i);
4918 snd_ctl_notify(card,
4919 SNDRV_CTL_EVENT_MASK_VALUE |
4920 SNDRV_CTL_EVENT_MASK_INFO,
4921 &private->sw_hw_ctls[i]->id);
4924 /* when the next monitor-other notify comes in, update the mux
4927 private->speaker_switching_switched = 1;
4932 /* when speaker switching gets disabled, reenable the hw/sw controls
4933 * and invalidate the routing
4935 static void scarlett2_speaker_switch_disable(struct usb_mixer_interface *mixer)
4937 struct snd_card *card = mixer->chip->card;
4938 struct scarlett2_data *private = mixer->private_data;
4941 /* enable the line out SW/HW switch */
4942 for (i = 0; i < 4; i++) {
4943 scarlett2_sw_hw_ctl_rw(private, i);
4944 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_INFO,
4945 &private->sw_hw_ctls[i]->id);
4948 /* when the next monitor-other notify comes in, update the mux
4951 private->speaker_switching_switched = 1;
4954 static int scarlett2_speaker_switch_enum_ctl_put(
4955 struct snd_kcontrol *kctl, struct snd_ctl_elem_value *ucontrol)
4957 struct usb_mixer_elem_info *elem = kctl->private_data;
4958 struct usb_mixer_interface *mixer = elem->head.mixer;
4959 struct scarlett2_data *private = mixer->private_data;
4961 int oval, val, err = 0;
4963 mutex_lock(&private->data_mutex);
4965 if (private->hwdep_in_use) {
4970 oval = private->speaker_switching_switch;
4971 val = min(ucontrol->value.enumerated.item[0], 2U);
4976 private->speaker_switching_switch = val;
4978 /* enable/disable speaker switching */
4979 err = scarlett2_usb_set_config(
4980 mixer, SCARLETT2_CONFIG_MONITOR_OTHER_ENABLE,
4985 /* if speaker switching is enabled, select main or alt */
4986 err = scarlett2_usb_set_config(
4987 mixer, SCARLETT2_CONFIG_MONITOR_OTHER_SWITCH,
4992 /* update controls if speaker switching gets enabled or disabled */
4994 err = scarlett2_speaker_switch_enable(mixer);
4995 else if (oval && !val)
4996 scarlett2_speaker_switch_disable(mixer);
5002 mutex_unlock(&private->data_mutex);
5006 static const struct snd_kcontrol_new scarlett2_speaker_switch_enum_ctl = {
5007 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
5009 .info = scarlett2_speaker_switch_enum_ctl_info,
5010 .get = scarlett2_speaker_switch_enum_ctl_get,
5011 .put = scarlett2_speaker_switch_enum_ctl_put,
5014 static int scarlett2_add_speaker_switch_ctl(struct usb_mixer_interface *mixer)
5016 struct scarlett2_data *private = mixer->private_data;
5017 const struct scarlett2_device_info *info = private->info;
5019 if (!info->has_speaker_switching)
5022 return scarlett2_add_new_ctl(
5023 mixer, &scarlett2_speaker_switch_enum_ctl,
5024 0, 1, "Speaker Switching Playback Enum",
5025 &private->speaker_switching_ctl);
5028 /*** Talkback and Talkback Map Controls ***/
5030 static int scarlett2_talkback_enum_ctl_info(
5031 struct snd_kcontrol *kctl, struct snd_ctl_elem_info *uinfo)
5033 static const char *const values[3] = {
5034 "Disabled", "Off", "On"
5037 return snd_ctl_enum_info(uinfo, 1, 3, values);
5040 static int scarlett2_talkback_enum_ctl_get(
5041 struct snd_kcontrol *kctl, struct snd_ctl_elem_value *ucontrol)
5043 struct usb_mixer_elem_info *elem = kctl->private_data;
5044 struct usb_mixer_interface *mixer = elem->head.mixer;
5045 struct scarlett2_data *private = mixer->private_data;
5048 mutex_lock(&private->data_mutex);
5050 if (private->hwdep_in_use) {
5055 if (private->monitor_other_updated) {
5056 err = scarlett2_update_monitor_other(mixer);
5060 ucontrol->value.enumerated.item[0] = private->talkback_switch;
5063 mutex_unlock(&private->data_mutex);
5067 static int scarlett2_talkback_enum_ctl_put(
5068 struct snd_kcontrol *kctl, struct snd_ctl_elem_value *ucontrol)
5070 struct usb_mixer_elem_info *elem = kctl->private_data;
5071 struct usb_mixer_interface *mixer = elem->head.mixer;
5072 struct scarlett2_data *private = mixer->private_data;
5074 int oval, val, err = 0;
5076 mutex_lock(&private->data_mutex);
5078 if (private->hwdep_in_use) {
5083 oval = private->talkback_switch;
5084 val = min(ucontrol->value.enumerated.item[0], 2U);
5089 private->talkback_switch = val;
5091 /* enable/disable talkback */
5092 err = scarlett2_usb_set_config(
5093 mixer, SCARLETT2_CONFIG_MONITOR_OTHER_ENABLE,
5098 /* if talkback is enabled, select main or alt */
5099 err = scarlett2_usb_set_config(
5100 mixer, SCARLETT2_CONFIG_MONITOR_OTHER_SWITCH,
5106 mutex_unlock(&private->data_mutex);
5110 static const struct snd_kcontrol_new scarlett2_talkback_enum_ctl = {
5111 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
5113 .info = scarlett2_talkback_enum_ctl_info,
5114 .get = scarlett2_talkback_enum_ctl_get,
5115 .put = scarlett2_talkback_enum_ctl_put,
5118 static int scarlett2_talkback_map_ctl_get(
5119 struct snd_kcontrol *kctl, struct snd_ctl_elem_value *ucontrol)
5121 struct usb_mixer_elem_info *elem = kctl->private_data;
5122 struct usb_mixer_interface *mixer = elem->head.mixer;
5123 struct scarlett2_data *private = mixer->private_data;
5124 int index = elem->control;
5126 ucontrol->value.integer.value[0] = private->talkback_map[index];
5131 static int scarlett2_talkback_map_ctl_put(
5132 struct snd_kcontrol *kctl, struct snd_ctl_elem_value *ucontrol)
5134 struct usb_mixer_elem_info *elem = kctl->private_data;
5135 struct usb_mixer_interface *mixer = elem->head.mixer;
5136 struct scarlett2_data *private = mixer->private_data;
5137 int index = elem->control;
5138 int oval, val, err = 0, i;
5141 mutex_lock(&private->data_mutex);
5143 if (private->hwdep_in_use) {
5148 oval = private->talkback_map[index];
5149 val = !!ucontrol->value.integer.value[0];
5154 private->talkback_map[index] = val;
5156 for (i = 0; i < private->num_mix_out; i++)
5157 bitmap |= private->talkback_map[i] << i;
5159 /* Send updated bitmap to the device */
5160 err = scarlett2_usb_set_config(mixer, SCARLETT2_CONFIG_TALKBACK_MAP,
5166 mutex_unlock(&private->data_mutex);
5170 static const struct snd_kcontrol_new scarlett2_talkback_map_ctl = {
5171 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
5173 .info = snd_ctl_boolean_mono_info,
5174 .get = scarlett2_talkback_map_ctl_get,
5175 .put = scarlett2_talkback_map_ctl_put,
5178 static int scarlett2_add_talkback_ctls(struct usb_mixer_interface *mixer)
5180 struct scarlett2_data *private = mixer->private_data;
5181 const struct scarlett2_device_info *info = private->info;
5183 char s[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
5185 if (!info->has_talkback)
5188 err = scarlett2_add_new_ctl(
5189 mixer, &scarlett2_talkback_enum_ctl,
5190 0, 1, "Talkback Playback Enum",
5191 &private->talkback_ctl);
5195 for (i = 0; i < private->num_mix_out; i++) {
5196 snprintf(s, sizeof(s),
5197 "Talkback Mix %c Playback Switch", i + 'A');
5198 err = scarlett2_add_new_ctl(mixer, &scarlett2_talkback_map_ctl,
5207 /*** Dim/Mute Controls ***/
5209 static int scarlett2_dim_mute_ctl_get(struct snd_kcontrol *kctl,
5210 struct snd_ctl_elem_value *ucontrol)
5212 struct usb_mixer_elem_info *elem = kctl->private_data;
5213 struct usb_mixer_interface *mixer = elem->head.mixer;
5214 struct scarlett2_data *private = mixer->private_data;
5217 mutex_lock(&private->data_mutex);
5219 if (private->hwdep_in_use) {
5224 if (private->dim_mute_updated) {
5225 err = scarlett2_update_dim_mute(mixer);
5229 ucontrol->value.integer.value[0] = private->dim_mute[elem->control];
5232 mutex_unlock(&private->data_mutex);
5236 static int scarlett2_dim_mute_ctl_put(struct snd_kcontrol *kctl,
5237 struct snd_ctl_elem_value *ucontrol)
5239 struct usb_mixer_elem_info *elem = kctl->private_data;
5240 struct usb_mixer_interface *mixer = elem->head.mixer;
5241 struct scarlett2_data *private = mixer->private_data;
5242 int index = elem->control;
5243 int oval, val, err = 0, i;
5245 mutex_lock(&private->data_mutex);
5247 if (private->hwdep_in_use) {
5252 oval = private->dim_mute[index];
5253 val = !!ucontrol->value.integer.value[0];
5258 private->dim_mute[index] = val;
5260 /* Send switch change to the device */
5261 err = scarlett2_usb_set_config(mixer, SCARLETT2_CONFIG_DIM_MUTE,
5266 if (index == SCARLETT2_BUTTON_MUTE)
5267 for (i = 0; i < private->num_line_out; i++) {
5268 int line_index = line_out_remap(private, i);
5270 if (private->vol_sw_hw_switch[line_index]) {
5271 private->mute_switch[line_index] = val;
5272 snd_ctl_notify(mixer->chip->card,
5273 SNDRV_CTL_EVENT_MASK_VALUE,
5274 &private->mute_ctls[i]->id);
5279 mutex_unlock(&private->data_mutex);
5283 static const struct snd_kcontrol_new scarlett2_dim_mute_ctl = {
5284 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
5286 .info = snd_ctl_boolean_mono_info,
5287 .get = scarlett2_dim_mute_ctl_get,
5288 .put = scarlett2_dim_mute_ctl_put
5291 /*** Create the analogue output controls ***/
5293 static int scarlett2_add_line_out_ctls(struct usb_mixer_interface *mixer)
5295 struct scarlett2_data *private = mixer->private_data;
5296 const struct scarlett2_device_info *info = private->info;
5298 char s[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
5300 /* Add R/O HW volume control */
5301 if (scarlett2_has_config_item(private,
5302 SCARLETT2_CONFIG_MASTER_VOLUME)) {
5303 snprintf(s, sizeof(s), "Master HW Playback Volume");
5304 err = scarlett2_add_new_ctl(mixer,
5305 &scarlett2_master_volume_ctl,
5306 0, 1, s, &private->master_vol_ctl);
5311 /* Add R/O headphone volume control */
5312 if (scarlett2_has_config_item(private,
5313 SCARLETT2_CONFIG_HEADPHONE_VOLUME)) {
5314 snprintf(s, sizeof(s), "Headphone Playback Volume");
5315 err = scarlett2_add_new_ctl(mixer,
5316 &scarlett2_headphone_volume_ctl,
5318 &private->headphone_vol_ctl);
5323 /* Remaining controls are only applicable if the device
5324 * has per-channel line-out volume controls.
5326 if (!scarlett2_has_config_item(private,
5327 SCARLETT2_CONFIG_LINE_OUT_VOLUME))
5330 /* Add volume controls */
5331 for (i = 0; i < private->num_line_out; i++) {
5332 int index = line_out_remap(private, i);
5335 if (info->line_out_descrs[i])
5336 snprintf(s, sizeof(s),
5337 "Line %02d (%s) Playback Volume",
5338 i + 1, info->line_out_descrs[i]);
5340 snprintf(s, sizeof(s),
5341 "Line %02d Playback Volume",
5343 err = scarlett2_add_new_ctl(mixer,
5344 &scarlett2_line_out_volume_ctl,
5345 i, 1, s, &private->vol_ctls[i]);
5350 snprintf(s, sizeof(s),
5351 "Line %02d Mute Playback Switch",
5353 err = scarlett2_add_new_ctl(mixer,
5354 &scarlett2_mute_ctl,
5356 &private->mute_ctls[i]);
5361 if (scarlett2_has_config_item(private,
5362 SCARLETT2_CONFIG_SW_HW_SWITCH)) {
5364 /* Make the fader and mute controls read-only if the
5365 * SW/HW switch is set to HW
5367 if (private->vol_sw_hw_switch[index])
5368 scarlett2_vol_ctl_set_writable(mixer, i, 0);
5370 scnprintf(s, sizeof(s),
5371 "Line Out %02d Volume Control Playback Enum",
5373 err = scarlett2_add_new_ctl(mixer,
5374 &scarlett2_sw_hw_enum_ctl,
5376 &private->sw_hw_ctls[i]);
5380 /* Make the switch read-only if the line is
5381 * involved in speaker switching
5383 if (private->speaker_switching_switch && i < 4)
5384 scarlett2_sw_hw_ctl_ro(private, i);
5388 /* Add dim/mute controls */
5389 if (scarlett2_has_config_item(private, SCARLETT2_CONFIG_DIM_MUTE))
5390 for (i = 0; i < SCARLETT2_DIM_MUTE_COUNT; i++) {
5391 err = scarlett2_add_new_ctl(
5392 mixer, &scarlett2_dim_mute_ctl,
5393 i, 1, scarlett2_dim_mute_names[i],
5394 &private->dim_mute_ctls[i]);
5402 /*** Create the analogue input controls ***/
5404 static int scarlett2_add_line_in_ctls(struct usb_mixer_interface *mixer)
5406 struct scarlett2_data *private = mixer->private_data;
5407 const struct scarlett2_device_info *info = private->info;
5409 char s[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
5410 const char *fmt = "Line In %d %s Capture %s";
5411 const char *fmt2 = "Line In %d-%d %s Capture %s";
5413 /* Add input level (line/inst) controls */
5414 for (i = 0; i < info->level_input_count; i++) {
5415 scnprintf(s, sizeof(s), fmt, i + 1 + info->level_input_first,
5417 err = scarlett2_add_new_ctl(mixer, &scarlett2_level_enum_ctl,
5418 i, 1, s, &private->level_ctls[i]);
5423 /* Add input pad controls */
5424 for (i = 0; i < info->pad_input_count; i++) {
5425 scnprintf(s, sizeof(s), fmt, i + 1, "Pad", "Switch");
5426 err = scarlett2_add_new_ctl(mixer, &scarlett2_pad_ctl,
5427 i, 1, s, &private->pad_ctls[i]);
5432 /* Add input air controls */
5433 for (i = 0; i < info->air_input_count; i++) {
5434 scnprintf(s, sizeof(s), fmt, i + 1 + info->air_input_first,
5435 "Air", info->air_option ? "Enum" : "Switch");
5436 err = scarlett2_add_new_ctl(
5437 mixer, &scarlett2_air_ctl[info->air_option],
5438 i, 1, s, &private->air_ctls[i]);
5443 /* Add input phantom controls */
5444 if (info->inputs_per_phantom == 1) {
5445 for (i = 0; i < info->phantom_count; i++) {
5446 scnprintf(s, sizeof(s), fmt,
5447 i + 1 + info->phantom_first,
5448 "Phantom Power", "Switch");
5449 err = scarlett2_add_new_ctl(
5450 mixer, &scarlett2_phantom_ctl,
5451 i, 1, s, &private->phantom_ctls[i]);
5455 } else if (info->inputs_per_phantom > 1) {
5456 for (i = 0; i < info->phantom_count; i++) {
5457 int from = i * info->inputs_per_phantom + 1;
5458 int to = (i + 1) * info->inputs_per_phantom;
5460 scnprintf(s, sizeof(s), fmt2, from, to,
5461 "Phantom Power", "Switch");
5462 err = scarlett2_add_new_ctl(
5463 mixer, &scarlett2_phantom_ctl,
5464 i, 1, s, &private->phantom_ctls[i]);
5469 if (info->phantom_count &&
5470 scarlett2_has_config_item(private,
5471 SCARLETT2_CONFIG_PHANTOM_PERSISTENCE)) {
5472 err = scarlett2_add_new_ctl(
5473 mixer, &scarlett2_phantom_persistence_ctl, 0, 1,
5474 "Phantom Power Persistence Capture Switch", NULL);
5479 /* Add software-controllable input gain controls */
5480 if (info->gain_input_count) {
5481 err = scarlett2_add_new_ctl(
5482 mixer, &scarlett2_input_select_ctl, 0, 1,
5483 "Input Select Capture Enum",
5484 &private->input_select_ctl);
5488 for (i = 0; i < info->gain_input_count; i++) {
5490 scnprintf(s, sizeof(s),
5491 "Line In %d-%d Link Capture Switch",
5493 err = scarlett2_add_new_ctl(
5494 mixer, &scarlett2_input_link_ctl,
5496 &private->input_link_ctls[i / 2]);
5501 scnprintf(s, sizeof(s), fmt, i + 1,
5503 err = scarlett2_add_new_ctl(
5504 mixer, &scarlett2_input_gain_ctl,
5505 i, 1, s, &private->input_gain_ctls[i]);
5509 scnprintf(s, sizeof(s), fmt, i + 1,
5510 "Autogain", "Switch");
5511 err = scarlett2_add_new_ctl(
5512 mixer, &scarlett2_autogain_switch_ctl,
5513 i, 1, s, &private->autogain_ctls[i]);
5517 scnprintf(s, sizeof(s), fmt, i + 1,
5518 "Autogain Status", "Enum");
5519 err = scarlett2_add_new_ctl(
5520 mixer, &scarlett2_autogain_status_ctl,
5521 i, 1, s, &private->autogain_status_ctls[i]);
5523 scnprintf(s, sizeof(s), fmt, i + 1,
5525 err = scarlett2_add_new_ctl(
5526 mixer, &scarlett2_safe_ctl,
5527 i, 1, s, &private->safe_ctls[i]);
5533 /* Add PCM Input Switch control */
5534 if (scarlett2_has_config_item(private,
5535 SCARLETT2_CONFIG_PCM_INPUT_SWITCH)) {
5536 err = scarlett2_add_new_ctl(
5537 mixer, &scarlett2_pcm_input_switch_ctl, 0, 1,
5538 "PCM Input Capture Switch",
5539 &private->pcm_input_switch_ctl);
5547 /*** Mixer Volume Controls ***/
5549 static int scarlett2_update_mix(struct usb_mixer_interface *mixer)
5551 struct scarlett2_data *private = mixer->private_data;
5554 private->mix_updated = 0;
5556 for (i = 0; i < private->num_mix_out; i++) {
5557 err = scarlett2_usb_get_mix(mixer, i);
5565 static int scarlett2_mixer_ctl_info(struct snd_kcontrol *kctl,
5566 struct snd_ctl_elem_info *uinfo)
5568 struct usb_mixer_elem_info *elem = kctl->private_data;
5570 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
5571 uinfo->count = elem->channels;
5572 uinfo->value.integer.min = 0;
5573 uinfo->value.integer.max = SCARLETT2_MIXER_MAX_VALUE;
5574 uinfo->value.integer.step = 1;
5578 static int scarlett2_mixer_ctl_get(struct snd_kcontrol *kctl,
5579 struct snd_ctl_elem_value *ucontrol)
5581 struct usb_mixer_elem_info *elem = kctl->private_data;
5582 struct usb_mixer_interface *mixer = elem->head.mixer;
5583 struct scarlett2_data *private = mixer->private_data;
5586 mutex_lock(&private->data_mutex);
5588 if (private->hwdep_in_use) {
5593 if (private->mix_updated) {
5594 err = scarlett2_update_mix(mixer);
5598 ucontrol->value.integer.value[0] = private->mix[elem->control];
5601 mutex_unlock(&private->data_mutex);
5605 static int scarlett2_mixer_ctl_put(struct snd_kcontrol *kctl,
5606 struct snd_ctl_elem_value *ucontrol)
5608 struct usb_mixer_elem_info *elem = kctl->private_data;
5609 struct usb_mixer_interface *mixer = elem->head.mixer;
5610 struct scarlett2_data *private = mixer->private_data;
5611 int oval, val, mix_num, err = 0;
5612 int index = elem->control;
5614 mutex_lock(&private->data_mutex);
5616 if (private->hwdep_in_use) {
5621 oval = private->mix[index];
5622 val = clamp(ucontrol->value.integer.value[0],
5623 0L, (long)SCARLETT2_MIXER_MAX_VALUE);
5624 mix_num = index / private->num_mix_in;
5629 private->mix[index] = val;
5630 err = scarlett2_usb_set_mix(mixer, mix_num);
5635 mutex_unlock(&private->data_mutex);
5639 static const DECLARE_TLV_DB_MINMAX(
5640 db_scale_scarlett2_mixer,
5641 SCARLETT2_MIXER_MIN_DB * 100,
5642 SCARLETT2_MIXER_MAX_DB * 100
5645 static const struct snd_kcontrol_new scarlett2_mixer_ctl = {
5646 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
5647 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE |
5648 SNDRV_CTL_ELEM_ACCESS_TLV_READ,
5650 .info = scarlett2_mixer_ctl_info,
5651 .get = scarlett2_mixer_ctl_get,
5652 .put = scarlett2_mixer_ctl_put,
5653 .private_value = SCARLETT2_MIXER_MAX_DB, /* max value */
5654 .tlv = { .p = db_scale_scarlett2_mixer }
5657 static int scarlett2_add_mixer_ctls(struct usb_mixer_interface *mixer)
5659 struct scarlett2_data *private = mixer->private_data;
5662 char s[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
5664 for (i = 0, index = 0; i < private->num_mix_out; i++)
5665 for (j = 0; j < private->num_mix_in; j++, index++) {
5666 snprintf(s, sizeof(s),
5667 "Mix %c Input %02d Playback Volume",
5669 err = scarlett2_add_new_ctl(mixer, &scarlett2_mixer_ctl,
5671 &private->mix_ctls[index]);
5679 /*** Direct Monitor Control ***/
5681 static int scarlett2_update_direct_monitor(struct usb_mixer_interface *mixer)
5683 struct scarlett2_data *private = mixer->private_data;
5685 private->direct_monitor_updated = 0;
5687 if (!private->info->direct_monitor)
5690 return scarlett2_usb_get_config(
5691 mixer, SCARLETT2_CONFIG_DIRECT_MONITOR,
5692 1, &private->direct_monitor_switch);
5695 static int scarlett2_update_monitor_mix(struct usb_mixer_interface *mixer)
5697 struct scarlett2_data *private = mixer->private_data;
5699 u16 mix_values[SCARLETT2_MONITOR_MIX_MAX];
5701 if (!private->num_monitor_mix_ctls)
5704 err = scarlett2_usb_get_config(
5705 mixer, SCARLETT2_CONFIG_DIRECT_MONITOR_GAIN,
5706 private->num_monitor_mix_ctls, mix_values);
5710 for (i = 0; i < private->num_monitor_mix_ctls; i++)
5711 private->monitor_mix[i] = scarlett2_mixer_value_to_db(
5717 static int scarlett2_direct_monitor_ctl_get(
5718 struct snd_kcontrol *kctl, struct snd_ctl_elem_value *ucontrol)
5720 struct usb_mixer_elem_info *elem = kctl->private_data;
5721 struct usb_mixer_interface *mixer = elem->head.mixer;
5722 struct scarlett2_data *private = mixer->private_data;
5725 mutex_lock(&private->data_mutex);
5727 if (private->hwdep_in_use) {
5732 if (private->direct_monitor_updated) {
5733 err = scarlett2_update_direct_monitor(mixer);
5737 ucontrol->value.enumerated.item[0] = private->direct_monitor_switch;
5740 mutex_unlock(&private->data_mutex);
5744 static int scarlett2_direct_monitor_ctl_put(
5745 struct snd_kcontrol *kctl, struct snd_ctl_elem_value *ucontrol)
5747 struct usb_mixer_elem_info *elem = kctl->private_data;
5748 struct usb_mixer_interface *mixer = elem->head.mixer;
5749 struct scarlett2_data *private = mixer->private_data;
5751 int index = elem->control;
5752 int oval, val, err = 0;
5754 mutex_lock(&private->data_mutex);
5756 if (private->hwdep_in_use) {
5761 oval = private->direct_monitor_switch;
5762 val = min(ucontrol->value.enumerated.item[0], 2U);
5767 private->direct_monitor_switch = val;
5769 /* Send switch change to the device */
5770 err = scarlett2_usb_set_config(
5771 mixer, SCARLETT2_CONFIG_DIRECT_MONITOR, index, val);
5776 mutex_unlock(&private->data_mutex);
5780 static int scarlett2_direct_monitor_stereo_enum_ctl_info(
5781 struct snd_kcontrol *kctl, struct snd_ctl_elem_info *uinfo)
5783 static const char *const values[3] = {
5784 "Off", "Mono", "Stereo"
5787 return snd_ctl_enum_info(uinfo, 1, 3, values);
5790 /* Direct Monitor for Solo is mono-only and only needs a boolean control
5791 * Direct Monitor for 2i2 is selectable between Off/Mono/Stereo
5793 static const struct snd_kcontrol_new scarlett2_direct_monitor_ctl[2] = {
5795 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
5797 .info = snd_ctl_boolean_mono_info,
5798 .get = scarlett2_direct_monitor_ctl_get,
5799 .put = scarlett2_direct_monitor_ctl_put,
5802 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
5804 .info = scarlett2_direct_monitor_stereo_enum_ctl_info,
5805 .get = scarlett2_direct_monitor_ctl_get,
5806 .put = scarlett2_direct_monitor_ctl_put,
5810 static int scarlett2_monitor_mix_ctl_get(struct snd_kcontrol *kctl,
5811 struct snd_ctl_elem_value *ucontrol)
5813 struct usb_mixer_elem_info *elem = kctl->private_data;
5814 struct scarlett2_data *private = elem->head.mixer->private_data;
5816 ucontrol->value.integer.value[0] = private->monitor_mix[elem->control];
5821 static int scarlett2_monitor_mix_ctl_put(struct snd_kcontrol *kctl,
5822 struct snd_ctl_elem_value *ucontrol)
5824 struct usb_mixer_elem_info *elem = kctl->private_data;
5825 struct usb_mixer_interface *mixer = elem->head.mixer;
5826 struct scarlett2_data *private = mixer->private_data;
5827 int oval, val, err = 0;
5828 int index = elem->control;
5830 mutex_lock(&private->data_mutex);
5832 if (private->hwdep_in_use) {
5837 oval = private->monitor_mix[index];
5838 val = clamp(ucontrol->value.integer.value[0],
5839 0L, (long)SCARLETT2_MIXER_MAX_VALUE);
5844 private->monitor_mix[index] = val;
5845 err = scarlett2_usb_set_config(
5846 mixer, SCARLETT2_CONFIG_DIRECT_MONITOR_GAIN,
5847 index, scarlett2_mixer_values[val]);
5852 mutex_unlock(&private->data_mutex);
5856 static const struct snd_kcontrol_new scarlett2_monitor_mix_ctl = {
5857 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
5858 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE |
5859 SNDRV_CTL_ELEM_ACCESS_TLV_READ,
5861 .info = scarlett2_mixer_ctl_info,
5862 .get = scarlett2_monitor_mix_ctl_get,
5863 .put = scarlett2_monitor_mix_ctl_put,
5864 .private_value = SCARLETT2_MIXER_MAX_DB, /* max value */
5865 .tlv = { .p = db_scale_scarlett2_mixer }
5868 static int scarlett2_add_direct_monitor_ctls(struct usb_mixer_interface *mixer)
5870 struct scarlett2_data *private = mixer->private_data;
5871 const struct scarlett2_device_info *info = private->info;
5873 int err, i, j, k, index;
5875 if (!info->direct_monitor)
5878 s = info->direct_monitor == 1
5879 ? "Direct Monitor Playback Switch"
5880 : "Direct Monitor Playback Enum";
5882 err = scarlett2_add_new_ctl(
5883 mixer, &scarlett2_direct_monitor_ctl[info->direct_monitor - 1],
5884 0, 1, s, &private->direct_monitor_ctl);
5888 if (!private->num_monitor_mix_ctls)
5891 /* 1 or 2 direct monitor selections (Mono & Stereo) */
5892 for (i = 0, index = 0; i < info->direct_monitor; i++) {
5893 const char * const format =
5894 "Monitor %sMix %c Input %02d Playback Volume";
5895 const char *mix_type;
5897 if (info->direct_monitor == 1)
5904 /* 2 Mix outputs, A/Left & B/Right */
5905 for (j = 0; j < 2; j++)
5908 for (k = 0; k < private->num_mix_in; k++, index++) {
5909 char name[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
5911 scnprintf(name, sizeof(name), format,
5912 mix_type, 'A' + j, k + 1);
5914 err = scarlett2_add_new_ctl(
5915 mixer, &scarlett2_monitor_mix_ctl,
5916 index, 1, name, NULL);
5925 /*** Mux Source Selection Controls ***/
5927 static int scarlett2_mux_src_enum_ctl_info(struct snd_kcontrol *kctl,
5928 struct snd_ctl_elem_info *uinfo)
5930 struct usb_mixer_elem_info *elem = kctl->private_data;
5931 struct scarlett2_data *private = elem->head.mixer->private_data;
5932 const struct scarlett2_device_info *info = private->info;
5933 const int (*port_count)[SCARLETT2_PORT_DIRNS] = info->port_count;
5934 unsigned int item = uinfo->value.enumerated.item;
5935 int items = private->num_mux_srcs;
5938 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
5939 uinfo->count = elem->channels;
5940 uinfo->value.enumerated.items = items;
5943 item = uinfo->value.enumerated.item = items - 1;
5946 port_type < SCARLETT2_PORT_TYPE_COUNT;
5948 if (item < port_count[port_type][SCARLETT2_PORT_IN]) {
5949 const struct scarlett2_port *port =
5950 &scarlett2_ports[port_type];
5952 if (port_type == SCARLETT2_PORT_TYPE_MIX &&
5953 item >= private->num_mix_out)
5954 sprintf(uinfo->value.enumerated.name,
5955 port->dsp_src_descr,
5956 item - private->num_mix_out + 1);
5958 sprintf(uinfo->value.enumerated.name,
5960 item + port->src_num_offset);
5964 item -= port_count[port_type][SCARLETT2_PORT_IN];
5970 static int scarlett2_mux_src_enum_ctl_get(struct snd_kcontrol *kctl,
5971 struct snd_ctl_elem_value *ucontrol)
5973 struct usb_mixer_elem_info *elem = kctl->private_data;
5974 struct usb_mixer_interface *mixer = elem->head.mixer;
5975 struct scarlett2_data *private = mixer->private_data;
5976 int index = line_out_remap(private, elem->control);
5979 mutex_lock(&private->data_mutex);
5981 if (private->hwdep_in_use) {
5986 if (private->mux_updated) {
5987 err = scarlett2_usb_get_mux(mixer);
5991 ucontrol->value.enumerated.item[0] = private->mux[index];
5994 mutex_unlock(&private->data_mutex);
5998 static int scarlett2_mux_src_enum_ctl_put(struct snd_kcontrol *kctl,
5999 struct snd_ctl_elem_value *ucontrol)
6001 struct usb_mixer_elem_info *elem = kctl->private_data;
6002 struct usb_mixer_interface *mixer = elem->head.mixer;
6003 struct scarlett2_data *private = mixer->private_data;
6004 int index = line_out_remap(private, elem->control);
6005 int oval, val, err = 0;
6007 mutex_lock(&private->data_mutex);
6009 if (private->hwdep_in_use) {
6014 oval = private->mux[index];
6015 val = min(ucontrol->value.enumerated.item[0],
6016 private->num_mux_srcs - 1U);
6021 private->mux[index] = val;
6022 err = scarlett2_usb_set_mux(mixer);
6027 mutex_unlock(&private->data_mutex);
6031 static const struct snd_kcontrol_new scarlett2_mux_src_enum_ctl = {
6032 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
6034 .info = scarlett2_mux_src_enum_ctl_info,
6035 .get = scarlett2_mux_src_enum_ctl_get,
6036 .put = scarlett2_mux_src_enum_ctl_put,
6039 static int scarlett2_add_mux_enums(struct usb_mixer_interface *mixer)
6041 struct scarlett2_data *private = mixer->private_data;
6042 const struct scarlett2_device_info *info = private->info;
6043 const int (*port_count)[SCARLETT2_PORT_DIRNS] = info->port_count;
6044 int port_type, channel, i;
6046 for (i = 0, port_type = 0;
6047 port_type < SCARLETT2_PORT_TYPE_COUNT;
6050 channel < port_count[port_type][SCARLETT2_PORT_OUT];
6053 char s[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
6054 int channel_num = channel + 1;
6055 const struct scarlett2_port *port =
6056 &scarlett2_ports[port_type];
6057 const char *descr = port->dst_descr;
6059 if (port_type == SCARLETT2_PORT_TYPE_MIX &&
6060 channel >= private->num_mix_in) {
6061 channel_num -= private->num_mix_in;
6062 descr = port->dsp_dst_descr;
6065 snprintf(s, sizeof(s) - 5, descr, channel_num);
6068 err = scarlett2_add_new_ctl(mixer,
6069 &scarlett2_mux_src_enum_ctl,
6071 &private->mux_ctls[i]);
6080 /*** Meter Controls ***/
6082 static int scarlett2_meter_ctl_info(struct snd_kcontrol *kctl,
6083 struct snd_ctl_elem_info *uinfo)
6085 struct usb_mixer_elem_info *elem = kctl->private_data;
6087 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
6088 uinfo->count = elem->channels;
6089 uinfo->value.integer.min = 0;
6090 uinfo->value.integer.max = 4095;
6091 uinfo->value.integer.step = 1;
6095 static int scarlett2_meter_ctl_get(struct snd_kcontrol *kctl,
6096 struct snd_ctl_elem_value *ucontrol)
6098 struct usb_mixer_elem_info *elem = kctl->private_data;
6099 struct usb_mixer_interface *mixer = elem->head.mixer;
6100 struct scarlett2_data *private = mixer->private_data;
6101 u8 *meter_level_map = private->meter_level_map;
6102 u16 meter_levels[SCARLETT2_MAX_METERS];
6105 mutex_lock(&private->data_mutex);
6107 if (private->hwdep_in_use) {
6112 err = scarlett2_usb_get_meter_levels(mixer, elem->channels,
6117 /* copy & translate from meter_levels[] using meter_level_map[] */
6118 for (i = 0; i < elem->channels; i++) {
6119 int idx = meter_level_map[i];
6125 value = meter_levels[idx];
6127 ucontrol->value.integer.value[i] = value;
6131 mutex_unlock(&private->data_mutex);
6136 static const struct snd_kcontrol_new scarlett2_meter_ctl = {
6137 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
6138 .access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE,
6140 .info = scarlett2_meter_ctl_info,
6141 .get = scarlett2_meter_ctl_get
6144 static int scarlett2_add_meter_ctl(struct usb_mixer_interface *mixer)
6146 struct scarlett2_data *private = mixer->private_data;
6148 /* devices without a mixer also don't support reporting levels */
6149 if (!scarlett2_has_mixer(private))
6152 return scarlett2_add_new_ctl(mixer, &scarlett2_meter_ctl,
6153 0, private->num_mux_dsts,
6154 "Level Meter", NULL);
6157 /*** MSD Controls ***/
6159 static int scarlett2_msd_ctl_get(struct snd_kcontrol *kctl,
6160 struct snd_ctl_elem_value *ucontrol)
6162 struct usb_mixer_elem_info *elem = kctl->private_data;
6163 struct scarlett2_data *private = elem->head.mixer->private_data;
6165 ucontrol->value.integer.value[0] = private->msd_switch;
6169 static int scarlett2_msd_ctl_put(struct snd_kcontrol *kctl,
6170 struct snd_ctl_elem_value *ucontrol)
6172 struct usb_mixer_elem_info *elem = kctl->private_data;
6173 struct usb_mixer_interface *mixer = elem->head.mixer;
6174 struct scarlett2_data *private = mixer->private_data;
6176 int oval, val, err = 0;
6178 mutex_lock(&private->data_mutex);
6180 if (private->hwdep_in_use) {
6185 oval = private->msd_switch;
6186 val = !!ucontrol->value.integer.value[0];
6191 private->msd_switch = val;
6193 /* Send switch change to the device */
6194 err = scarlett2_usb_set_config(mixer, SCARLETT2_CONFIG_MSD_SWITCH,
6200 mutex_unlock(&private->data_mutex);
6204 static const struct snd_kcontrol_new scarlett2_msd_ctl = {
6205 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
6207 .info = snd_ctl_boolean_mono_info,
6208 .get = scarlett2_msd_ctl_get,
6209 .put = scarlett2_msd_ctl_put,
6212 static int scarlett2_add_msd_ctl(struct usb_mixer_interface *mixer)
6214 struct scarlett2_data *private = mixer->private_data;
6216 if (!scarlett2_has_config_item(private, SCARLETT2_CONFIG_MSD_SWITCH))
6219 /* If MSD mode is off, hide the switch by default */
6220 if (!private->msd_switch && !(mixer->chip->setup & SCARLETT2_MSD_ENABLE))
6223 /* Add MSD control */
6224 return scarlett2_add_new_ctl(mixer, &scarlett2_msd_ctl,
6225 0, 1, "MSD Mode Switch", NULL);
6228 /*** Standalone Control ***/
6230 static int scarlett2_standalone_ctl_get(struct snd_kcontrol *kctl,
6231 struct snd_ctl_elem_value *ucontrol)
6233 struct usb_mixer_elem_info *elem = kctl->private_data;
6234 struct scarlett2_data *private = elem->head.mixer->private_data;
6236 ucontrol->value.integer.value[0] = private->standalone_switch;
6240 static int scarlett2_standalone_ctl_put(struct snd_kcontrol *kctl,
6241 struct snd_ctl_elem_value *ucontrol)
6243 struct usb_mixer_elem_info *elem = kctl->private_data;
6244 struct usb_mixer_interface *mixer = elem->head.mixer;
6245 struct scarlett2_data *private = mixer->private_data;
6247 int oval, val, err = 0;
6249 mutex_lock(&private->data_mutex);
6251 if (private->hwdep_in_use) {
6256 oval = private->standalone_switch;
6257 val = !!ucontrol->value.integer.value[0];
6262 private->standalone_switch = val;
6264 /* Send switch change to the device */
6265 err = scarlett2_usb_set_config(mixer,
6266 SCARLETT2_CONFIG_STANDALONE_SWITCH,
6272 mutex_unlock(&private->data_mutex);
6276 static const struct snd_kcontrol_new scarlett2_standalone_ctl = {
6277 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
6279 .info = snd_ctl_boolean_mono_info,
6280 .get = scarlett2_standalone_ctl_get,
6281 .put = scarlett2_standalone_ctl_put,
6284 static int scarlett2_add_standalone_ctl(struct usb_mixer_interface *mixer)
6286 struct scarlett2_data *private = mixer->private_data;
6288 if (!scarlett2_has_config_item(private,
6289 SCARLETT2_CONFIG_STANDALONE_SWITCH))
6292 /* Add standalone control */
6293 return scarlett2_add_new_ctl(mixer, &scarlett2_standalone_ctl,
6294 0, 1, "Standalone Switch", NULL);
6297 /*** Power Status ***/
6299 static int scarlett2_update_power_status(struct usb_mixer_interface *mixer)
6301 struct scarlett2_data *private = mixer->private_data;
6303 u8 power_ext, power_low;
6305 private->power_status_updated = 0;
6307 err = scarlett2_usb_get_config(mixer, SCARLETT2_CONFIG_POWER_EXT,
6312 err = scarlett2_usb_get_config(mixer, SCARLETT2_CONFIG_POWER_LOW,
6318 private->power_status = SCARLETT2_POWER_STATUS_FAIL;
6320 private->power_status = SCARLETT2_POWER_STATUS_EXT;
6322 private->power_status = SCARLETT2_POWER_STATUS_BUS;
6327 static int scarlett2_power_status_ctl_get(struct snd_kcontrol *kctl,
6328 struct snd_ctl_elem_value *ucontrol)
6330 struct usb_mixer_elem_info *elem = kctl->private_data;
6331 struct usb_mixer_interface *mixer = elem->head.mixer;
6332 struct scarlett2_data *private = mixer->private_data;
6335 mutex_lock(&private->data_mutex);
6337 if (private->power_status_updated) {
6338 err = scarlett2_update_power_status(mixer);
6342 ucontrol->value.integer.value[0] = private->power_status;
6345 mutex_unlock(&private->data_mutex);
6349 static int scarlett2_power_status_ctl_info(
6350 struct snd_kcontrol *kctl, struct snd_ctl_elem_info *uinfo)
6352 static const char *const values[3] = {
6353 "External", "Bus", "Fail"
6356 return snd_ctl_enum_info(uinfo, 1, 3, values);
6359 static const struct snd_kcontrol_new scarlett2_power_status_ctl = {
6360 .iface = SNDRV_CTL_ELEM_IFACE_CARD,
6361 .access = SNDRV_CTL_ELEM_ACCESS_READ,
6363 .info = scarlett2_power_status_ctl_info,
6364 .get = scarlett2_power_status_ctl_get,
6367 static int scarlett2_add_power_status_ctl(struct usb_mixer_interface *mixer)
6369 struct scarlett2_data *private = mixer->private_data;
6371 if (!scarlett2_has_config_item(private,
6372 SCARLETT2_CONFIG_POWER_EXT))
6375 /* Add power status control */
6376 return scarlett2_add_new_ctl(mixer, &scarlett2_power_status_ctl,
6377 0, 1, "Power Status Card Enum",
6378 &private->power_status_ctl);
6381 /*** Cleanup/Suspend Callbacks ***/
6383 static void scarlett2_private_free(struct usb_mixer_interface *mixer)
6385 struct scarlett2_data *private = mixer->private_data;
6387 cancel_delayed_work_sync(&private->work);
6389 mixer->private_data = NULL;
6392 static void scarlett2_private_suspend(struct usb_mixer_interface *mixer)
6394 struct scarlett2_data *private = mixer->private_data;
6396 if (cancel_delayed_work_sync(&private->work))
6397 scarlett2_config_save(private->mixer);
6400 /*** Initialisation ***/
6402 static void scarlett2_count_io(struct scarlett2_data *private)
6404 const struct scarlett2_device_info *info = private->info;
6405 const int (*port_count)[SCARLETT2_PORT_DIRNS] = info->port_count;
6406 int port_type, srcs = 0, dsts = 0;
6408 /* Count the number of mux sources and destinations */
6410 port_type < SCARLETT2_PORT_TYPE_COUNT;
6412 srcs += port_count[port_type][SCARLETT2_PORT_IN];
6413 dsts += port_count[port_type][SCARLETT2_PORT_OUT];
6416 private->num_mux_srcs = srcs;
6417 private->num_mux_dsts = dsts;
6419 /* Mixer inputs are mux outputs and vice versa.
6420 * Scarlett Gen 4 DSP I/O uses SCARLETT2_PORT_TYPE_MIX but
6421 * doesn't have mixer controls.
6423 private->num_mix_in =
6424 port_count[SCARLETT2_PORT_TYPE_MIX][SCARLETT2_PORT_OUT] -
6427 private->num_mix_out =
6428 port_count[SCARLETT2_PORT_TYPE_MIX][SCARLETT2_PORT_IN] -
6431 /* Number of analogue line outputs */
6432 private->num_line_out =
6433 port_count[SCARLETT2_PORT_TYPE_ANALOGUE][SCARLETT2_PORT_OUT];
6435 /* Number of monitor mix controls */
6436 private->num_monitor_mix_ctls =
6437 info->direct_monitor * 2 * private->num_mix_in;
6440 /* Look through the interface descriptors for the Focusrite Control
6441 * interface (bInterfaceClass = 255 Vendor Specific Class) and set
6442 * bInterfaceNumber, bEndpointAddress, wMaxPacketSize, and bInterval
6445 static int scarlett2_find_fc_interface(struct usb_device *dev,
6446 struct scarlett2_data *private)
6448 struct usb_host_config *config = dev->actconfig;
6451 for (i = 0; i < config->desc.bNumInterfaces; i++) {
6452 struct usb_interface *intf = config->interface[i];
6453 struct usb_interface_descriptor *desc =
6454 &intf->altsetting[0].desc;
6455 struct usb_endpoint_descriptor *epd;
6457 if (desc->bInterfaceClass != 255)
6460 epd = get_endpoint(intf->altsetting, 0);
6461 private->bInterfaceNumber = desc->bInterfaceNumber;
6462 private->bEndpointAddress = epd->bEndpointAddress &
6463 USB_ENDPOINT_NUMBER_MASK;
6464 private->wMaxPacketSize = le16_to_cpu(epd->wMaxPacketSize);
6465 private->bInterval = epd->bInterval;
6472 /* Initialise private data */
6473 static int scarlett2_init_private(struct usb_mixer_interface *mixer,
6474 const struct scarlett2_device_entry *entry)
6476 struct scarlett2_data *private =
6477 kzalloc(sizeof(struct scarlett2_data), GFP_KERNEL);
6482 mutex_init(&private->usb_mutex);
6483 mutex_init(&private->data_mutex);
6484 INIT_DELAYED_WORK(&private->work, scarlett2_config_save_work);
6486 mixer->private_data = private;
6487 mixer->private_free = scarlett2_private_free;
6488 mixer->private_suspend = scarlett2_private_suspend;
6490 private->info = entry->info;
6491 private->config_set = entry->info->config_set;
6492 private->series_name = entry->series_name;
6493 scarlett2_count_io(private);
6494 private->scarlett2_seq = 0;
6495 private->mixer = mixer;
6497 return scarlett2_find_fc_interface(mixer->chip->dev, private);
6500 /* Cargo cult proprietary initialisation sequence */
6501 static int scarlett2_usb_init(struct usb_mixer_interface *mixer)
6503 struct usb_device *dev = mixer->chip->dev;
6504 struct scarlett2_data *private = mixer->private_data;
6509 if (usb_pipe_type_check(dev, usb_sndctrlpipe(dev, 0)))
6513 err = scarlett2_usb_rx(dev, private->bInterfaceNumber,
6514 SCARLETT2_USB_CMD_INIT,
6515 step0_buf, sizeof(step0_buf));
6520 private->scarlett2_seq = 1;
6521 err = scarlett2_usb(mixer, SCARLETT2_USB_INIT_1, NULL, 0, NULL, 0);
6526 private->scarlett2_seq = 1;
6527 err = scarlett2_usb(mixer, SCARLETT2_USB_INIT_2,
6529 step2_buf, sizeof(step2_buf));
6533 /* extract 4-byte firmware version from step2_buf[8] */
6534 private->firmware_version = le32_to_cpu(*(__le32 *)(step2_buf + 8));
6535 usb_audio_info(mixer->chip,
6536 "Firmware version %d\n",
6537 private->firmware_version);
6542 /* Get the flash segment numbers for the App_Settings and App_Upgrade
6543 * segments and put them in the private data
6545 static int scarlett2_get_flash_segment_nums(struct usb_mixer_interface *mixer)
6547 struct scarlett2_data *private = mixer->private_data;
6554 } __packed flash_info;
6560 } __packed segment_info;
6562 err = scarlett2_usb(mixer, SCARLETT2_USB_INFO_FLASH,
6564 &flash_info, sizeof(flash_info));
6568 count = le32_to_cpu(flash_info.count);
6570 /* sanity check count */
6571 if (count < SCARLETT2_SEGMENT_NUM_MIN ||
6572 count > SCARLETT2_SEGMENT_NUM_MAX + 1) {
6573 usb_audio_err(mixer->chip,
6574 "invalid flash segment count: %d\n", count);
6578 for (i = 0; i < count; i++) {
6579 __le32 segment_num_req = cpu_to_le32(i);
6580 int flash_segment_id;
6582 err = scarlett2_usb(mixer, SCARLETT2_USB_INFO_SEGMENT,
6583 &segment_num_req, sizeof(segment_num_req),
6584 &segment_info, sizeof(segment_info));
6586 usb_audio_err(mixer->chip,
6587 "failed to get flash segment info %d: %d\n",
6592 if (!strncmp(segment_info.name,
6593 SCARLETT2_SEGMENT_SETTINGS_NAME, 16))
6594 flash_segment_id = SCARLETT2_SEGMENT_ID_SETTINGS;
6595 else if (!strncmp(segment_info.name,
6596 SCARLETT2_SEGMENT_FIRMWARE_NAME, 16))
6597 flash_segment_id = SCARLETT2_SEGMENT_ID_FIRMWARE;
6601 private->flash_segment_nums[flash_segment_id] = i;
6602 private->flash_segment_blocks[flash_segment_id] =
6603 le32_to_cpu(segment_info.size) /
6604 SCARLETT2_FLASH_BLOCK_SIZE;
6607 /* segment 0 is App_Gold and we never want to touch that, so
6608 * use 0 as the "not-found" value
6610 if (!private->flash_segment_nums[SCARLETT2_SEGMENT_ID_SETTINGS]) {
6611 usb_audio_err(mixer->chip,
6612 "failed to find flash segment %s\n",
6613 SCARLETT2_SEGMENT_SETTINGS_NAME);
6616 if (!private->flash_segment_nums[SCARLETT2_SEGMENT_ID_FIRMWARE]) {
6617 usb_audio_err(mixer->chip,
6618 "failed to find flash segment %s\n",
6619 SCARLETT2_SEGMENT_FIRMWARE_NAME);
6626 /* Read configuration from the interface on start */
6627 static int scarlett2_read_configs(struct usb_mixer_interface *mixer)
6629 struct scarlett2_data *private = mixer->private_data;
6630 const struct scarlett2_device_info *info = private->info;
6633 if (scarlett2_has_config_item(private, SCARLETT2_CONFIG_MSD_SWITCH)) {
6634 err = scarlett2_usb_get_config(
6635 mixer, SCARLETT2_CONFIG_MSD_SWITCH,
6636 1, &private->msd_switch);
6641 if (private->firmware_version < info->min_firmware_version) {
6642 usb_audio_err(mixer->chip,
6643 "Focusrite %s firmware version %d is too old; "
6645 private->series_name,
6646 private->firmware_version,
6647 info->min_firmware_version);
6651 /* no other controls are created if MSD mode is on */
6652 if (private->msd_switch)
6655 err = scarlett2_update_input_level(mixer);
6659 err = scarlett2_update_input_pad(mixer);
6663 err = scarlett2_update_input_air(mixer);
6667 err = scarlett2_update_input_phantom(mixer);
6671 err = scarlett2_update_direct_monitor(mixer);
6675 /* the rest of the configuration is for devices with a mixer */
6676 if (!scarlett2_has_mixer(private))
6679 err = scarlett2_update_monitor_mix(mixer);
6683 err = scarlett2_update_monitor_other(mixer);
6687 if (scarlett2_has_config_item(private,
6688 SCARLETT2_CONFIG_STANDALONE_SWITCH)) {
6689 err = scarlett2_usb_get_config(
6690 mixer, SCARLETT2_CONFIG_STANDALONE_SWITCH,
6691 1, &private->standalone_switch);
6696 if (scarlett2_has_config_item(private,
6697 SCARLETT2_CONFIG_POWER_EXT)) {
6698 err = scarlett2_update_power_status(mixer);
6703 err = scarlett2_update_sync(mixer);
6707 if (scarlett2_has_config_item(private,
6708 SCARLETT2_CONFIG_LINE_OUT_VOLUME)) {
6709 s16 sw_vol[SCARLETT2_ANALOGUE_MAX];
6711 /* read SW line out volume */
6712 err = scarlett2_usb_get_config(
6713 mixer, SCARLETT2_CONFIG_LINE_OUT_VOLUME,
6714 private->num_line_out, &sw_vol);
6718 for (i = 0; i < private->num_line_out; i++)
6719 private->vol[i] = clamp(
6720 sw_vol[i] + SCARLETT2_VOLUME_BIAS,
6721 0, SCARLETT2_VOLUME_BIAS);
6724 err = scarlett2_usb_get_config(
6725 mixer, SCARLETT2_CONFIG_MUTE_SWITCH,
6726 private->num_line_out, &private->mute_switch);
6730 for (i = 0; i < private->num_line_out; i++)
6731 private->mute_switch[i] =
6732 !!private->mute_switch[i];
6734 /* read SW/HW switches */
6735 if (scarlett2_has_config_item(private,
6736 SCARLETT2_CONFIG_SW_HW_SWITCH)) {
6737 err = scarlett2_usb_get_config(
6738 mixer, SCARLETT2_CONFIG_SW_HW_SWITCH,
6739 private->num_line_out,
6740 &private->vol_sw_hw_switch);
6744 for (i = 0; i < private->num_line_out; i++)
6745 private->vol_sw_hw_switch[i] =
6746 !!private->vol_sw_hw_switch[i];
6750 err = scarlett2_update_volumes(mixer);
6754 err = scarlett2_update_dim_mute(mixer);
6758 err = scarlett2_update_input_select(mixer);
6762 err = scarlett2_update_input_gain(mixer);
6766 err = scarlett2_update_autogain(mixer);
6770 err = scarlett2_update_input_safe(mixer);
6774 if (scarlett2_has_config_item(private,
6775 SCARLETT2_CONFIG_PCM_INPUT_SWITCH)) {
6776 err = scarlett2_update_pcm_input_switch(mixer);
6781 err = scarlett2_update_mix(mixer);
6785 return scarlett2_usb_get_mux(mixer);
6788 /* Notify on sync change */
6789 static void scarlett2_notify_sync(struct usb_mixer_interface *mixer)
6791 struct scarlett2_data *private = mixer->private_data;
6793 private->sync_updated = 1;
6795 snd_ctl_notify(mixer->chip->card, SNDRV_CTL_EVENT_MASK_VALUE,
6796 &private->sync_ctl->id);
6799 /* Notify on monitor change (Gen 2/3) */
6800 static void scarlett2_notify_monitor(struct usb_mixer_interface *mixer)
6802 struct snd_card *card = mixer->chip->card;
6803 struct scarlett2_data *private = mixer->private_data;
6806 if (!scarlett2_has_config_item(private, SCARLETT2_CONFIG_SW_HW_SWITCH))
6809 private->vol_updated = 1;
6811 snd_ctl_notify(mixer->chip->card, SNDRV_CTL_EVENT_MASK_VALUE,
6812 &private->master_vol_ctl->id);
6814 for (i = 0; i < private->num_line_out; i++)
6815 if (private->vol_sw_hw_switch[line_out_remap(private, i)])
6816 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE,
6817 &private->vol_ctls[i]->id);
6820 /* Notify on volume change (Gen 4) */
6821 static void scarlett2_notify_volume(struct usb_mixer_interface *mixer)
6823 struct scarlett2_data *private = mixer->private_data;
6825 private->vol_updated = 1;
6827 snd_ctl_notify(mixer->chip->card, SNDRV_CTL_EVENT_MASK_VALUE,
6828 &private->master_vol_ctl->id);
6829 snd_ctl_notify(mixer->chip->card, SNDRV_CTL_EVENT_MASK_VALUE,
6830 &private->headphone_vol_ctl->id);
6833 /* Notify on dim/mute change */
6834 static void scarlett2_notify_dim_mute(struct usb_mixer_interface *mixer)
6836 struct snd_card *card = mixer->chip->card;
6837 struct scarlett2_data *private = mixer->private_data;
6840 if (!scarlett2_has_config_item(private, SCARLETT2_CONFIG_SW_HW_SWITCH))
6843 private->dim_mute_updated = 1;
6845 for (i = 0; i < SCARLETT2_DIM_MUTE_COUNT; i++)
6846 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE,
6847 &private->dim_mute_ctls[i]->id);
6849 for (i = 0; i < private->num_line_out; i++)
6850 if (private->vol_sw_hw_switch[line_out_remap(private, i)])
6851 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE,
6852 &private->mute_ctls[i]->id);
6855 /* Notify on input level switch change */
6856 static void scarlett2_notify_input_level(struct usb_mixer_interface *mixer)
6858 struct snd_card *card = mixer->chip->card;
6859 struct scarlett2_data *private = mixer->private_data;
6860 const struct scarlett2_device_info *info = private->info;
6863 private->input_level_updated = 1;
6865 for (i = 0; i < info->level_input_count; i++)
6866 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE,
6867 &private->level_ctls[i]->id);
6870 /* Notify on input pad switch change */
6871 static void scarlett2_notify_input_pad(struct usb_mixer_interface *mixer)
6873 struct snd_card *card = mixer->chip->card;
6874 struct scarlett2_data *private = mixer->private_data;
6875 const struct scarlett2_device_info *info = private->info;
6878 private->input_pad_updated = 1;
6880 for (i = 0; i < info->pad_input_count; i++)
6881 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE,
6882 &private->pad_ctls[i]->id);
6885 /* Notify on input air switch change */
6886 static void scarlett2_notify_input_air(struct usb_mixer_interface *mixer)
6888 struct snd_card *card = mixer->chip->card;
6889 struct scarlett2_data *private = mixer->private_data;
6890 const struct scarlett2_device_info *info = private->info;
6893 private->input_air_updated = 1;
6895 for (i = 0; i < info->air_input_count; i++)
6896 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE,
6897 &private->air_ctls[i]->id);
6900 /* Notify on input phantom switch change */
6901 static void scarlett2_notify_input_phantom(struct usb_mixer_interface *mixer)
6903 struct snd_card *card = mixer->chip->card;
6904 struct scarlett2_data *private = mixer->private_data;
6905 const struct scarlett2_device_info *info = private->info;
6908 private->input_phantom_updated = 1;
6910 for (i = 0; i < info->phantom_count; i++)
6911 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE,
6912 &private->phantom_ctls[i]->id);
6914 scarlett2_phantom_notify_access(mixer);
6917 /* Notify on "input other" change (level/pad/air/phantom) */
6918 static void scarlett2_notify_input_other(struct usb_mixer_interface *mixer)
6920 scarlett2_notify_input_level(mixer);
6921 scarlett2_notify_input_pad(mixer);
6922 scarlett2_notify_input_air(mixer);
6923 scarlett2_notify_input_phantom(mixer);
6926 /* Notify on input select change */
6927 static void scarlett2_notify_input_select(struct usb_mixer_interface *mixer)
6929 struct snd_card *card = mixer->chip->card;
6930 struct scarlett2_data *private = mixer->private_data;
6931 const struct scarlett2_device_info *info = private->info;
6934 if (!info->gain_input_count)
6937 private->input_select_updated = 1;
6939 snd_ctl_notify(card,
6940 SNDRV_CTL_EVENT_MASK_VALUE | SNDRV_CTL_EVENT_MASK_INFO,
6941 &private->input_select_ctl->id);
6943 for (i = 0; i < info->gain_input_count / 2; i++)
6944 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE,
6945 &private->input_link_ctls[i]->id);
6948 /* Notify on input gain change */
6949 static void scarlett2_notify_input_gain(struct usb_mixer_interface *mixer)
6951 struct snd_card *card = mixer->chip->card;
6952 struct scarlett2_data *private = mixer->private_data;
6953 const struct scarlett2_device_info *info = private->info;
6956 if (!info->gain_input_count)
6959 private->input_gain_updated = 1;
6961 for (i = 0; i < info->gain_input_count; i++)
6962 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE,
6963 &private->input_gain_ctls[i]->id);
6966 /* Notify on autogain change */
6967 static void scarlett2_notify_autogain(struct usb_mixer_interface *mixer)
6969 struct snd_card *card = mixer->chip->card;
6970 struct scarlett2_data *private = mixer->private_data;
6971 const struct scarlett2_device_info *info = private->info;
6974 if (!info->gain_input_count)
6977 private->autogain_updated = 1;
6979 for (i = 0; i < info->gain_input_count; i++) {
6980 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE,
6981 &private->autogain_ctls[i]->id);
6982 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE,
6983 &private->autogain_status_ctls[i]->id);
6986 scarlett2_autogain_notify_access(mixer);
6989 /* Notify on input safe switch change */
6990 static void scarlett2_notify_input_safe(struct usb_mixer_interface *mixer)
6992 struct snd_card *card = mixer->chip->card;
6993 struct scarlett2_data *private = mixer->private_data;
6994 const struct scarlett2_device_info *info = private->info;
6997 if (!info->gain_input_count)
7000 private->input_safe_updated = 1;
7002 for (i = 0; i < info->gain_input_count; i++)
7003 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE,
7004 &private->safe_ctls[i]->id);
7007 /* Notify on "monitor other" change (speaker switching, talkback) */
7008 static void scarlett2_notify_monitor_other(struct usb_mixer_interface *mixer)
7010 struct snd_card *card = mixer->chip->card;
7011 struct scarlett2_data *private = mixer->private_data;
7012 const struct scarlett2_device_info *info = private->info;
7014 private->monitor_other_updated = 1;
7016 if (info->has_speaker_switching)
7017 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE,
7018 &private->speaker_switching_ctl->id);
7020 if (info->has_talkback)
7021 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE,
7022 &private->talkback_ctl->id);
7024 /* if speaker switching was recently enabled or disabled,
7025 * invalidate the dim/mute and mux enum controls
7027 if (private->speaker_switching_switched) {
7030 scarlett2_notify_dim_mute(mixer);
7032 private->speaker_switching_switched = 0;
7033 private->mux_updated = 1;
7035 for (i = 0; i < private->num_mux_dsts; i++)
7036 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE,
7037 &private->mux_ctls[i]->id);
7041 /* Notify on direct monitor switch change */
7042 static void scarlett2_notify_direct_monitor(struct usb_mixer_interface *mixer)
7044 struct snd_card *card = mixer->chip->card;
7045 struct scarlett2_data *private = mixer->private_data;
7046 int count = private->num_mix_in * private->num_mix_out;
7049 private->direct_monitor_updated = 1;
7051 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE,
7052 &private->direct_monitor_ctl->id);
7054 if (!scarlett2_has_mixer(private))
7057 private->mix_updated = 1;
7059 /* Notify of change to the mix controls */
7060 for (i = 0; i < count; i++)
7061 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE,
7062 &private->mix_ctls[i]->id);
7065 /* Notify on power change */
7066 static void scarlett2_notify_power_status(struct usb_mixer_interface *mixer)
7068 struct snd_card *card = mixer->chip->card;
7069 struct scarlett2_data *private = mixer->private_data;
7071 private->power_status_updated = 1;
7073 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE,
7074 &private->power_status_ctl->id);
7077 /* Notify on mux change */
7078 static void scarlett2_notify_mux(struct usb_mixer_interface *mixer)
7080 struct snd_card *card = mixer->chip->card;
7081 struct scarlett2_data *private = mixer->private_data;
7084 private->mux_updated = 1;
7086 for (i = 0; i < private->num_mux_dsts; i++)
7087 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE,
7088 &private->mux_ctls[i]->id);
7091 /* Notify on PCM input switch change */
7092 static void scarlett2_notify_pcm_input_switch(struct usb_mixer_interface *mixer)
7094 struct snd_card *card = mixer->chip->card;
7095 struct scarlett2_data *private = mixer->private_data;
7097 private->pcm_input_switch_updated = 1;
7099 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE,
7100 &private->pcm_input_switch_ctl->id);
7102 scarlett2_notify_mux(mixer);
7105 /* Interrupt callback */
7106 static void scarlett2_notify(struct urb *urb)
7108 struct usb_mixer_interface *mixer = urb->context;
7109 int len = urb->actual_length;
7110 int ustatus = urb->status;
7112 struct scarlett2_data *private = mixer->private_data;
7113 const struct scarlett2_notification *notifications =
7114 private->config_set->notifications;
7116 if (ustatus != 0 || len != 8)
7119 data = le32_to_cpu(*(__le32 *)urb->transfer_buffer);
7121 while (data && notifications->mask) {
7122 if (data & notifications->mask) {
7123 data &= ~notifications->mask;
7124 if (notifications->func)
7125 notifications->func(mixer);
7131 usb_audio_warn(mixer->chip,
7132 "%s: Unhandled notification: 0x%08x\n",
7136 if (ustatus != -ENOENT &&
7137 ustatus != -ECONNRESET &&
7138 ustatus != -ESHUTDOWN) {
7139 urb->dev = mixer->chip->dev;
7140 usb_submit_urb(urb, GFP_ATOMIC);
7144 static int scarlett2_init_notify(struct usb_mixer_interface *mixer)
7146 struct usb_device *dev = mixer->chip->dev;
7147 struct scarlett2_data *private = mixer->private_data;
7148 unsigned int pipe = usb_rcvintpipe(dev, private->bEndpointAddress);
7149 void *transfer_buffer;
7152 usb_audio_err(mixer->chip,
7153 "%s: mixer urb already in use!\n", __func__);
7157 if (usb_pipe_type_check(dev, pipe))
7160 mixer->urb = usb_alloc_urb(0, GFP_KERNEL);
7164 transfer_buffer = kmalloc(private->wMaxPacketSize, GFP_KERNEL);
7165 if (!transfer_buffer)
7168 usb_fill_int_urb(mixer->urb, dev, pipe,
7169 transfer_buffer, private->wMaxPacketSize,
7170 scarlett2_notify, mixer, private->bInterval);
7172 return usb_submit_urb(mixer->urb, GFP_KERNEL);
7175 static const struct scarlett2_device_entry *get_scarlett2_device_entry(
7176 struct usb_mixer_interface *mixer)
7178 const struct scarlett2_device_entry *entry = scarlett2_devices;
7180 /* Find entry in scarlett2_devices */
7181 while (entry->usb_id && entry->usb_id != mixer->chip->usb_id)
7189 static int snd_scarlett2_controls_create(
7190 struct usb_mixer_interface *mixer,
7191 const struct scarlett2_device_entry *entry)
7193 struct scarlett2_data *private;
7196 /* Initialise private data */
7197 err = scarlett2_init_private(mixer, entry);
7201 private = mixer->private_data;
7203 /* Send proprietary USB initialisation sequence */
7204 err = scarlett2_usb_init(mixer);
7208 /* Get the upgrade & settings flash segment numbers */
7209 err = scarlett2_get_flash_segment_nums(mixer);
7213 /* Add firmware version control */
7214 err = scarlett2_add_firmware_version_ctl(mixer);
7218 /* Add minimum firmware version control */
7219 err = scarlett2_add_min_firmware_version_ctl(mixer);
7223 /* Read volume levels and controls from the interface */
7224 err = scarlett2_read_configs(mixer);
7228 /* Create the MSD control */
7229 err = scarlett2_add_msd_ctl(mixer);
7233 /* If MSD mode is enabled, or if the firmware version is too
7234 * old, don't create any other controls
7236 if (private->msd_switch ||
7237 private->firmware_version < private->info->min_firmware_version)
7240 /* Create the analogue output controls */
7241 err = scarlett2_add_line_out_ctls(mixer);
7245 /* Create the analogue input controls */
7246 err = scarlett2_add_line_in_ctls(mixer);
7250 /* Create the input, output, and mixer mux input selections */
7251 err = scarlett2_add_mux_enums(mixer);
7255 /* Create the matrix mixer controls */
7256 err = scarlett2_add_mixer_ctls(mixer);
7260 /* Create the level meter controls */
7261 err = scarlett2_add_meter_ctl(mixer);
7265 /* Create the sync control */
7266 err = scarlett2_add_sync_ctl(mixer);
7270 /* Create the direct monitor control(s) */
7271 err = scarlett2_add_direct_monitor_ctls(mixer);
7275 /* Create the speaker switching control */
7276 err = scarlett2_add_speaker_switch_ctl(mixer);
7280 /* Create the talkback controls */
7281 err = scarlett2_add_talkback_ctls(mixer);
7285 /* Create the standalone control */
7286 err = scarlett2_add_standalone_ctl(mixer);
7290 /* Create the power status control */
7291 err = scarlett2_add_power_status_ctl(mixer);
7295 /* Set the access mode of controls disabled during
7296 * autogain/phantom power switching.
7298 if (private->info->gain_input_count) {
7299 scarlett2_autogain_update_access(mixer);
7300 scarlett2_phantom_update_access(mixer);
7303 /* Set up the interrupt polling */
7304 err = scarlett2_init_notify(mixer);
7311 /*** hwdep interface ***/
7313 /* Set private->hwdep_in_use; prevents access to the ALSA controls
7314 * while doing a config erase/firmware upgrade.
7316 static void scarlett2_lock(struct scarlett2_data *private)
7318 mutex_lock(&private->data_mutex);
7319 private->hwdep_in_use = 1;
7320 mutex_unlock(&private->data_mutex);
7323 /* Call SCARLETT2_USB_GET_ERASE to get the erase progress */
7324 static int scarlett2_get_erase_progress(struct usb_mixer_interface *mixer)
7326 struct scarlett2_data *private = mixer->private_data;
7327 int segment_id, segment_num, err;
7333 } __packed erase_req;
7335 segment_id = private->selected_flash_segment_id;
7336 segment_num = private->flash_segment_nums[segment_id];
7338 if (segment_num < SCARLETT2_SEGMENT_NUM_MIN ||
7339 segment_num > SCARLETT2_SEGMENT_NUM_MAX)
7342 /* Send the erase progress request */
7343 erase_req.segment_num = cpu_to_le32(segment_num);
7346 err = scarlett2_usb(mixer, SCARLETT2_USB_GET_ERASE,
7347 &erase_req, sizeof(erase_req),
7348 &erase_resp, sizeof(erase_resp));
7355 /* Repeatedly call scarlett2_get_erase_progress() until it returns
7356 * 0xff (erase complete) or we've waited 10 seconds (it usually takes
7359 static int scarlett2_wait_for_erase(struct usb_mixer_interface *mixer)
7363 for (i = 0; i < 100; i++) {
7364 err = scarlett2_get_erase_progress(mixer);
7377 /* Reboot the device; wait for the erase to complete if one is in
7380 static int scarlett2_reboot(struct usb_mixer_interface *mixer)
7382 struct scarlett2_data *private = mixer->private_data;
7384 if (private->flash_write_state ==
7385 SCARLETT2_FLASH_WRITE_STATE_ERASING) {
7386 int err = scarlett2_wait_for_erase(mixer);
7392 return scarlett2_usb(mixer, SCARLETT2_USB_REBOOT, NULL, 0, NULL, 0);
7395 /* Select a flash segment for erasing (and possibly writing to) */
7396 static int scarlett2_ioctl_select_flash_segment(
7397 struct usb_mixer_interface *mixer,
7400 struct scarlett2_data *private = mixer->private_data;
7401 int segment_id, segment_num;
7403 if (get_user(segment_id, (int __user *)arg))
7406 /* Check the segment ID and segment number */
7407 if (segment_id < 0 || segment_id >= SCARLETT2_SEGMENT_ID_COUNT)
7410 segment_num = private->flash_segment_nums[segment_id];
7411 if (segment_num < SCARLETT2_SEGMENT_NUM_MIN ||
7412 segment_num > SCARLETT2_SEGMENT_NUM_MAX) {
7413 usb_audio_err(mixer->chip,
7414 "%s: invalid segment number %d\n",
7415 __func__, segment_id);
7419 /* If erasing, wait for it to complete */
7420 if (private->flash_write_state == SCARLETT2_FLASH_WRITE_STATE_ERASING) {
7421 int err = scarlett2_wait_for_erase(mixer);
7427 /* Save the selected segment ID and set the state to SELECTED */
7428 private->selected_flash_segment_id = segment_id;
7429 private->flash_write_state = SCARLETT2_FLASH_WRITE_STATE_SELECTED;
7434 /* Erase the previously-selected flash segment */
7435 static int scarlett2_ioctl_erase_flash_segment(
7436 struct usb_mixer_interface *mixer)
7438 struct scarlett2_data *private = mixer->private_data;
7439 int segment_id, segment_num, err;
7444 } __packed erase_req;
7446 if (private->flash_write_state != SCARLETT2_FLASH_WRITE_STATE_SELECTED)
7449 segment_id = private->selected_flash_segment_id;
7450 segment_num = private->flash_segment_nums[segment_id];
7452 if (segment_num < SCARLETT2_SEGMENT_NUM_MIN ||
7453 segment_num > SCARLETT2_SEGMENT_NUM_MAX)
7456 /* Prevent access to ALSA controls that access the device from
7459 scarlett2_lock(private);
7461 /* Send the erase request */
7462 erase_req.segment_num = cpu_to_le32(segment_num);
7465 err = scarlett2_usb(mixer, SCARLETT2_USB_ERASE_SEGMENT,
7466 &erase_req, sizeof(erase_req),
7471 /* On success, change the state from SELECTED to ERASING */
7472 private->flash_write_state = SCARLETT2_FLASH_WRITE_STATE_ERASING;
7477 /* Get the erase progress from the device */
7478 static int scarlett2_ioctl_get_erase_progress(
7479 struct usb_mixer_interface *mixer,
7482 struct scarlett2_data *private = mixer->private_data;
7483 struct scarlett2_flash_segment_erase_progress progress;
7484 int segment_id, segment_num, err;
7490 } __packed erase_req;
7492 /* Check that we're erasing */
7493 if (private->flash_write_state != SCARLETT2_FLASH_WRITE_STATE_ERASING)
7496 segment_id = private->selected_flash_segment_id;
7497 segment_num = private->flash_segment_nums[segment_id];
7499 if (segment_num < SCARLETT2_SEGMENT_NUM_MIN ||
7500 segment_num > SCARLETT2_SEGMENT_NUM_MAX)
7503 /* Send the erase progress request */
7504 erase_req.segment_num = cpu_to_le32(segment_num);
7507 err = scarlett2_usb(mixer, SCARLETT2_USB_GET_ERASE,
7508 &erase_req, sizeof(erase_req),
7509 &erase_resp, sizeof(erase_resp));
7513 progress.progress = erase_resp;
7514 progress.num_blocks = private->flash_segment_blocks[segment_id];
7516 if (copy_to_user((void __user *)arg, &progress, sizeof(progress)))
7519 /* If the erase is complete, change the state from ERASING to
7522 if (progress.progress == 0xff)
7523 private->flash_write_state = SCARLETT2_FLASH_WRITE_STATE_WRITE;
7528 static int scarlett2_hwdep_open(struct snd_hwdep *hw, struct file *file)
7530 struct usb_mixer_interface *mixer = hw->private_data;
7531 struct scarlett2_data *private = mixer->private_data;
7533 /* If erasing, wait for it to complete */
7534 if (private->flash_write_state ==
7535 SCARLETT2_FLASH_WRITE_STATE_ERASING) {
7536 int err = scarlett2_wait_for_erase(mixer);
7542 /* Set the state to IDLE */
7543 private->flash_write_state = SCARLETT2_FLASH_WRITE_STATE_IDLE;
7548 static int scarlett2_hwdep_ioctl(struct snd_hwdep *hw, struct file *file,
7549 unsigned int cmd, unsigned long arg)
7551 struct usb_mixer_interface *mixer = hw->private_data;
7555 case SCARLETT2_IOCTL_PVERSION:
7556 return put_user(SCARLETT2_HWDEP_VERSION,
7557 (int __user *)arg) ? -EFAULT : 0;
7559 case SCARLETT2_IOCTL_REBOOT:
7560 return scarlett2_reboot(mixer);
7562 case SCARLETT2_IOCTL_SELECT_FLASH_SEGMENT:
7563 return scarlett2_ioctl_select_flash_segment(mixer, arg);
7565 case SCARLETT2_IOCTL_ERASE_FLASH_SEGMENT:
7566 return scarlett2_ioctl_erase_flash_segment(mixer);
7568 case SCARLETT2_IOCTL_GET_ERASE_PROGRESS:
7569 return scarlett2_ioctl_get_erase_progress(mixer, arg);
7572 return -ENOIOCTLCMD;
7576 static long scarlett2_hwdep_write(struct snd_hwdep *hw,
7577 const char __user *buf,
7578 long count, loff_t *offset)
7580 struct usb_mixer_interface *mixer = hw->private_data;
7581 struct scarlett2_data *private = mixer->private_data;
7582 int segment_id, segment_num, err, len;
7585 /* SCARLETT2_USB_WRITE_SEGMENT request data */
7593 /* Calculate the maximum permitted in data[] */
7594 const size_t max_data_size = SCARLETT2_FLASH_WRITE_MAX -
7595 offsetof(typeof(*req), data);
7597 /* If erasing, wait for it to complete */
7598 if (private->flash_write_state ==
7599 SCARLETT2_FLASH_WRITE_STATE_ERASING) {
7600 err = scarlett2_wait_for_erase(mixer);
7603 private->flash_write_state = SCARLETT2_FLASH_WRITE_STATE_WRITE;
7605 /* Check that an erase has been done & completed */
7606 } else if (private->flash_write_state !=
7607 SCARLETT2_FLASH_WRITE_STATE_WRITE) {
7611 /* Check that we're writing to the upgrade firmware */
7612 segment_id = private->selected_flash_segment_id;
7613 if (segment_id != SCARLETT2_SEGMENT_ID_FIRMWARE)
7616 segment_num = private->flash_segment_nums[segment_id];
7617 if (segment_num < SCARLETT2_SEGMENT_NUM_MIN ||
7618 segment_num > SCARLETT2_SEGMENT_NUM_MAX)
7621 /* Validate the offset and count */
7622 flash_size = private->flash_segment_blocks[segment_id] *
7623 SCARLETT2_FLASH_BLOCK_SIZE;
7625 if (count < 0 || *offset < 0 || *offset + count >= flash_size)
7631 /* Limit the *req size to SCARLETT2_FLASH_WRITE_MAX */
7632 if (count > max_data_size)
7633 count = max_data_size;
7635 /* Create and send the request */
7636 len = struct_size(req, data, count);
7637 req = kzalloc(len, GFP_KERNEL);
7641 req->segment_num = cpu_to_le32(segment_num);
7642 req->offset = cpu_to_le32(*offset);
7645 if (copy_from_user(req->data, buf, count)) {
7650 err = scarlett2_usb(mixer, SCARLETT2_USB_WRITE_SEGMENT,
7663 static int scarlett2_hwdep_release(struct snd_hwdep *hw, struct file *file)
7665 struct usb_mixer_interface *mixer = hw->private_data;
7666 struct scarlett2_data *private = mixer->private_data;
7668 /* Return from the SELECTED or WRITE state to IDLE.
7669 * The ERASING state is left as-is, and checked on next open.
7672 private->hwdep_in_use &&
7673 private->flash_write_state != SCARLETT2_FLASH_WRITE_STATE_ERASING)
7674 private->flash_write_state = SCARLETT2_FLASH_WRITE_STATE_IDLE;
7679 static int scarlett2_hwdep_init(struct usb_mixer_interface *mixer)
7681 struct snd_hwdep *hw;
7684 err = snd_hwdep_new(mixer->chip->card, "Focusrite Control", 0, &hw);
7688 hw->private_data = mixer;
7690 hw->ops.open = scarlett2_hwdep_open;
7691 hw->ops.ioctl = scarlett2_hwdep_ioctl;
7692 hw->ops.write = scarlett2_hwdep_write;
7693 hw->ops.release = scarlett2_hwdep_release;
7698 int snd_scarlett2_init(struct usb_mixer_interface *mixer)
7700 struct snd_usb_audio *chip = mixer->chip;
7701 const struct scarlett2_device_entry *entry;
7704 /* only use UAC_VERSION_2 */
7705 if (!mixer->protocol)
7708 /* find entry in scarlett2_devices */
7709 entry = get_scarlett2_device_entry(mixer);
7711 usb_audio_err(mixer->chip,
7712 "%s: missing device entry for %04x:%04x\n",
7714 USB_ID_VENDOR(chip->usb_id),
7715 USB_ID_PRODUCT(chip->usb_id));
7719 if (chip->setup & SCARLETT2_DISABLE) {
7720 usb_audio_info(chip,
7721 "Focusrite %s Mixer Driver disabled "
7722 "by modprobe options (snd_usb_audio "
7723 "vid=0x%04x pid=0x%04x device_setup=%d)\n",
7725 USB_ID_VENDOR(chip->usb_id),
7726 USB_ID_PRODUCT(chip->usb_id),
7731 usb_audio_info(chip,
7732 "Focusrite %s Mixer Driver enabled (pid=0x%04x); "
7733 "report any issues to "
7734 "https://github.com/geoffreybennett/scarlett-gen2/issues",
7736 USB_ID_PRODUCT(chip->usb_id));
7738 err = snd_scarlett2_controls_create(mixer, entry);
7740 usb_audio_err(mixer->chip,
7741 "Error initialising %s Mixer Driver: %d",
7747 err = scarlett2_hwdep_init(mixer);
7749 usb_audio_err(mixer->chip,
7750 "Error creating %s hwdep device: %d",