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 /* Autogain Status Values */
289 SCARLETT2_AUTOGAIN_STATUS_STOPPED,
290 SCARLETT2_AUTOGAIN_STATUS_RUNNING,
291 SCARLETT2_AUTOGAIN_STATUS_FAILED,
292 SCARLETT2_AUTOGAIN_STATUS_CANCELLED,
293 SCARLETT2_AUTOGAIN_STATUS_UNKNOWN,
294 SCARLETT2_AUTOGAIN_STATUS_COUNT
297 /* Power Status Values */
299 SCARLETT2_POWER_STATUS_EXT,
300 SCARLETT2_POWER_STATUS_BUS,
301 SCARLETT2_POWER_STATUS_FAIL,
302 SCARLETT2_POWER_STATUS_COUNT
305 /* Notification callback functions */
306 struct scarlett2_notification {
308 void (*func)(struct usb_mixer_interface *mixer);
311 static void scarlett2_notify_sync(struct usb_mixer_interface *mixer);
312 static void scarlett2_notify_dim_mute(struct usb_mixer_interface *mixer);
313 static void scarlett2_notify_monitor(struct usb_mixer_interface *mixer);
314 static void scarlett2_notify_volume(struct usb_mixer_interface *mixer);
315 static void scarlett2_notify_input_level(struct usb_mixer_interface *mixer);
316 static void scarlett2_notify_input_pad(struct usb_mixer_interface *mixer);
317 static void scarlett2_notify_input_air(struct usb_mixer_interface *mixer);
318 static void scarlett2_notify_input_phantom(struct usb_mixer_interface *mixer);
319 static void scarlett2_notify_input_other(struct usb_mixer_interface *mixer);
320 static void scarlett2_notify_input_select(struct usb_mixer_interface *mixer);
321 static void scarlett2_notify_input_gain(struct usb_mixer_interface *mixer);
322 static void scarlett2_notify_autogain(struct usb_mixer_interface *mixer);
323 static void scarlett2_notify_input_safe(struct usb_mixer_interface *mixer);
324 static void scarlett2_notify_monitor_other(struct usb_mixer_interface *mixer);
325 static void scarlett2_notify_direct_monitor(struct usb_mixer_interface *mixer);
326 static void scarlett2_notify_power_status(struct usb_mixer_interface *mixer);
327 static void scarlett2_notify_pcm_input_switch(
328 struct usb_mixer_interface *mixer);
330 /* Arrays of notification callback functions */
332 static const struct scarlett2_notification scarlett2_notifications[] = {
333 { 0x00000001, NULL }, /* ack, gets ignored */
334 { 0x00000008, scarlett2_notify_sync },
335 { 0x00200000, scarlett2_notify_dim_mute },
336 { 0x00400000, scarlett2_notify_monitor },
337 { 0x00800000, scarlett2_notify_input_other },
338 { 0x01000000, scarlett2_notify_monitor_other },
342 static const struct scarlett2_notification scarlett3a_notifications[] = {
343 { 0x00000001, NULL }, /* ack, gets ignored */
344 { 0x00800000, scarlett2_notify_input_other },
345 { 0x01000000, scarlett2_notify_direct_monitor },
349 static const struct scarlett2_notification scarlett4_solo_notifications[] = {
350 { 0x00000001, NULL }, /* ack, gets ignored */
351 { 0x00000008, scarlett2_notify_sync },
352 { 0x00400000, scarlett2_notify_input_air },
353 { 0x00800000, scarlett2_notify_direct_monitor },
354 { 0x01000000, scarlett2_notify_input_level },
355 { 0x02000000, scarlett2_notify_input_phantom },
356 { 0x04000000, scarlett2_notify_pcm_input_switch },
360 static const struct scarlett2_notification scarlett4_2i2_notifications[] = {
361 { 0x00000001, NULL }, /* ack, gets ignored */
362 { 0x00000008, scarlett2_notify_sync },
363 { 0x00200000, scarlett2_notify_input_safe },
364 { 0x00400000, scarlett2_notify_autogain },
365 { 0x00800000, scarlett2_notify_input_air },
366 { 0x01000000, scarlett2_notify_direct_monitor },
367 { 0x02000000, scarlett2_notify_input_select },
368 { 0x04000000, scarlett2_notify_input_level },
369 { 0x08000000, scarlett2_notify_input_phantom },
370 { 0x10000000, NULL }, /* power status, ignored */
371 { 0x40000000, scarlett2_notify_input_gain },
372 { 0x80000000, NULL }, /* power status, ignored */
376 static const struct scarlett2_notification scarlett4_4i4_notifications[] = {
377 { 0x00000001, NULL }, /* ack, gets ignored */
378 { 0x00000008, scarlett2_notify_sync },
379 { 0x00200000, scarlett2_notify_input_safe },
380 { 0x00400000, scarlett2_notify_autogain },
381 { 0x00800000, scarlett2_notify_input_air },
382 { 0x01000000, scarlett2_notify_input_select },
383 { 0x02000000, scarlett2_notify_input_level },
384 { 0x04000000, scarlett2_notify_input_phantom },
385 { 0x08000000, scarlett2_notify_power_status }, /* power external */
386 { 0x20000000, scarlett2_notify_input_gain },
387 { 0x40000000, scarlett2_notify_power_status }, /* power status */
388 { 0x80000000, scarlett2_notify_volume },
392 /* Configuration parameters that can be read and written */
394 SCARLETT2_CONFIG_DIM_MUTE,
395 SCARLETT2_CONFIG_LINE_OUT_VOLUME,
396 SCARLETT2_CONFIG_MUTE_SWITCH,
397 SCARLETT2_CONFIG_SW_HW_SWITCH,
398 SCARLETT2_CONFIG_MASTER_VOLUME,
399 SCARLETT2_CONFIG_HEADPHONE_VOLUME,
400 SCARLETT2_CONFIG_LEVEL_SWITCH,
401 SCARLETT2_CONFIG_PAD_SWITCH,
402 SCARLETT2_CONFIG_MSD_SWITCH,
403 SCARLETT2_CONFIG_AIR_SWITCH,
404 SCARLETT2_CONFIG_STANDALONE_SWITCH,
405 SCARLETT2_CONFIG_PHANTOM_SWITCH,
406 SCARLETT2_CONFIG_PHANTOM_PERSISTENCE,
407 SCARLETT2_CONFIG_DIRECT_MONITOR,
408 SCARLETT2_CONFIG_MONITOR_OTHER_SWITCH,
409 SCARLETT2_CONFIG_MONITOR_OTHER_ENABLE,
410 SCARLETT2_CONFIG_TALKBACK_MAP,
411 SCARLETT2_CONFIG_AUTOGAIN_SWITCH,
412 SCARLETT2_CONFIG_AUTOGAIN_STATUS,
413 SCARLETT2_CONFIG_INPUT_GAIN,
414 SCARLETT2_CONFIG_SAFE_SWITCH,
415 SCARLETT2_CONFIG_INPUT_SELECT_SWITCH,
416 SCARLETT2_CONFIG_INPUT_LINK_SWITCH,
417 SCARLETT2_CONFIG_POWER_EXT,
418 SCARLETT2_CONFIG_POWER_STATUS,
419 SCARLETT2_CONFIG_PCM_INPUT_SWITCH,
420 SCARLETT2_CONFIG_DIRECT_MONITOR_GAIN,
421 SCARLETT2_CONFIG_COUNT
424 /* Location, size, and activation command number for the configuration
425 * parameters. Size is in bits and may be 0, 1, 8, or 16.
427 * A size of 0 indicates that the parameter is a byte-sized Scarlett
428 * Gen 4 configuration which is written through the gen4_write_addr
429 * location (but still read through the given offset location).
431 * Some Gen 4 configuration parameters are written with 0x02 for a
432 * desired value of 0x01, and 0x03 for 0x00. These are indicated with
433 * mute set to 1. 0x02 and 0x03 are temporary values while the device
434 * makes the change and the channel and/or corresponding DSP channel
437 struct scarlett2_config {
444 struct scarlett2_config_set {
445 const struct scarlett2_notification *notifications;
447 const struct scarlett2_config items[SCARLETT2_CONFIG_COUNT];
450 /* Gen 2 devices without SW/HW volume switch: 6i6, 18i8 */
452 static const struct scarlett2_config_set scarlett2_config_set_gen2a = {
453 .notifications = scarlett2_notifications,
455 [SCARLETT2_CONFIG_LINE_OUT_VOLUME] = {
456 .offset = 0x34, .size = 16, .activate = 1 },
458 [SCARLETT2_CONFIG_MUTE_SWITCH] = {
459 .offset = 0x5c, .size = 8, .activate = 1 },
461 [SCARLETT2_CONFIG_LEVEL_SWITCH] = {
462 .offset = 0x7c, .size = 8, .activate = 7 },
464 [SCARLETT2_CONFIG_PAD_SWITCH] = {
465 .offset = 0x84, .size = 8, .activate = 8 },
467 [SCARLETT2_CONFIG_STANDALONE_SWITCH] = {
468 .offset = 0x8d, .size = 8, .activate = 6 },
472 /* Gen 2 devices with SW/HW volume switch: 18i20 */
474 static const struct scarlett2_config_set scarlett2_config_set_gen2b = {
475 .notifications = scarlett2_notifications,
477 [SCARLETT2_CONFIG_DIM_MUTE] = {
478 .offset = 0x31, .size = 8, .activate = 2 },
480 [SCARLETT2_CONFIG_LINE_OUT_VOLUME] = {
481 .offset = 0x34, .size = 16, .activate = 1 },
483 [SCARLETT2_CONFIG_MUTE_SWITCH] = {
484 .offset = 0x5c, .size = 8, .activate = 1 },
486 [SCARLETT2_CONFIG_SW_HW_SWITCH] = {
487 .offset = 0x66, .size = 8, .activate = 3 },
489 [SCARLETT2_CONFIG_MASTER_VOLUME] = {
490 .offset = 0x76, .size = 16 },
492 [SCARLETT2_CONFIG_LEVEL_SWITCH] = {
493 .offset = 0x7c, .size = 8, .activate = 7 },
495 [SCARLETT2_CONFIG_PAD_SWITCH] = {
496 .offset = 0x84, .size = 8, .activate = 8 },
498 [SCARLETT2_CONFIG_STANDALONE_SWITCH] = {
499 .offset = 0x8d, .size = 8, .activate = 6 },
503 /* Gen 3 devices without a mixer (Solo and 2i2) */
504 static const struct scarlett2_config_set scarlett2_config_set_gen3a = {
505 .notifications = scarlett3a_notifications,
507 [SCARLETT2_CONFIG_MSD_SWITCH] = {
508 .offset = 0x04, .size = 8, .activate = 6 },
510 [SCARLETT2_CONFIG_PHANTOM_PERSISTENCE] = {
511 .offset = 0x05, .size = 8, .activate = 6 },
513 [SCARLETT2_CONFIG_PHANTOM_SWITCH] = {
514 .offset = 0x06, .size = 8, .activate = 3 },
516 [SCARLETT2_CONFIG_DIRECT_MONITOR] = {
517 .offset = 0x07, .size = 8, .activate = 4 },
519 [SCARLETT2_CONFIG_LEVEL_SWITCH] = {
520 .offset = 0x08, .size = 1, .activate = 7 },
522 [SCARLETT2_CONFIG_AIR_SWITCH] = {
523 .offset = 0x09, .size = 1, .activate = 8 },
527 /* Gen 3 devices without SW/HW volume switch: 4i4, 8i6 */
528 static const struct scarlett2_config_set scarlett2_config_set_gen3b = {
529 .notifications = scarlett2_notifications,
531 [SCARLETT2_CONFIG_LINE_OUT_VOLUME] = {
532 .offset = 0x34, .size = 16, .activate = 1 },
534 [SCARLETT2_CONFIG_MUTE_SWITCH] = {
535 .offset = 0x5c, .size = 8, .activate = 1 },
537 [SCARLETT2_CONFIG_LEVEL_SWITCH] = {
538 .offset = 0x7c, .size = 8, .activate = 7 },
540 [SCARLETT2_CONFIG_PAD_SWITCH] = {
541 .offset = 0x84, .size = 8, .activate = 8 },
543 [SCARLETT2_CONFIG_AIR_SWITCH] = {
544 .offset = 0x8c, .size = 8, .activate = 8 },
546 [SCARLETT2_CONFIG_STANDALONE_SWITCH] = {
547 .offset = 0x95, .size = 8, .activate = 6 },
549 [SCARLETT2_CONFIG_PHANTOM_SWITCH] = {
550 .offset = 0x9c, .size = 1, .activate = 8 },
552 [SCARLETT2_CONFIG_MSD_SWITCH] = {
553 .offset = 0x9d, .size = 8, .activate = 6 },
555 [SCARLETT2_CONFIG_PHANTOM_PERSISTENCE] = {
556 .offset = 0x9e, .size = 8, .activate = 6 },
560 /* Gen 3 devices with SW/HW volume switch: 18i8, 18i20 */
561 static const struct scarlett2_config_set scarlett2_config_set_gen3c = {
562 .notifications = scarlett2_notifications,
564 [SCARLETT2_CONFIG_DIM_MUTE] = {
565 .offset = 0x31, .size = 8, .activate = 2 },
567 [SCARLETT2_CONFIG_LINE_OUT_VOLUME] = {
568 .offset = 0x34, .size = 16, .activate = 1 },
570 [SCARLETT2_CONFIG_MUTE_SWITCH] = {
571 .offset = 0x5c, .size = 8, .activate = 1 },
573 [SCARLETT2_CONFIG_SW_HW_SWITCH] = {
574 .offset = 0x66, .size = 8, .activate = 3 },
576 [SCARLETT2_CONFIG_MASTER_VOLUME] = {
577 .offset = 0x76, .size = 16 },
579 [SCARLETT2_CONFIG_LEVEL_SWITCH] = {
580 .offset = 0x7c, .size = 8, .activate = 7 },
582 [SCARLETT2_CONFIG_PAD_SWITCH] = {
583 .offset = 0x84, .size = 8, .activate = 8 },
585 [SCARLETT2_CONFIG_AIR_SWITCH] = {
586 .offset = 0x8c, .size = 8, .activate = 8 },
588 [SCARLETT2_CONFIG_STANDALONE_SWITCH] = {
589 .offset = 0x95, .size = 8, .activate = 6 },
591 [SCARLETT2_CONFIG_PHANTOM_SWITCH] = {
592 .offset = 0x9c, .size = 1, .activate = 8 },
594 [SCARLETT2_CONFIG_MSD_SWITCH] = {
595 .offset = 0x9d, .size = 8, .activate = 6 },
597 [SCARLETT2_CONFIG_PHANTOM_PERSISTENCE] = {
598 .offset = 0x9e, .size = 8, .activate = 6 },
600 [SCARLETT2_CONFIG_MONITOR_OTHER_SWITCH] = {
601 .offset = 0x9f, .size = 1, .activate = 10 },
603 [SCARLETT2_CONFIG_MONITOR_OTHER_ENABLE] = {
604 .offset = 0xa0, .size = 1, .activate = 10 },
606 [SCARLETT2_CONFIG_TALKBACK_MAP] = {
607 .offset = 0xb0, .size = 16, .activate = 10 },
612 static const struct scarlett2_config_set scarlett2_config_set_gen4_solo = {
613 .notifications = scarlett4_solo_notifications,
614 .gen4_write_addr = 0xd8,
616 [SCARLETT2_CONFIG_MSD_SWITCH] = {
617 .offset = 0x47, .size = 8, .activate = 4 },
619 [SCARLETT2_CONFIG_DIRECT_MONITOR] = {
620 .offset = 0x108, .activate = 12 },
622 [SCARLETT2_CONFIG_PHANTOM_SWITCH] = {
623 .offset = 0x46, .activate = 9, .mute = 1 },
625 [SCARLETT2_CONFIG_LEVEL_SWITCH] = {
626 .offset = 0x3d, .activate = 10, .mute = 1 },
628 [SCARLETT2_CONFIG_AIR_SWITCH] = {
629 .offset = 0x3e, .activate = 11 },
631 [SCARLETT2_CONFIG_PCM_INPUT_SWITCH] = {
632 .offset = 0x206, .activate = 25 },
634 [SCARLETT2_CONFIG_DIRECT_MONITOR_GAIN] = {
635 .offset = 0x232, .size = 16, .activate = 26 }
640 static const struct scarlett2_config_set scarlett2_config_set_gen4_2i2 = {
641 .notifications = scarlett4_2i2_notifications,
642 .gen4_write_addr = 0xfc,
644 [SCARLETT2_CONFIG_MSD_SWITCH] = {
645 .offset = 0x49, .size = 8, .activate = 4 }, // 0x41 ??
647 [SCARLETT2_CONFIG_DIRECT_MONITOR] = {
648 .offset = 0x14a, .activate = 16 },
650 [SCARLETT2_CONFIG_AUTOGAIN_SWITCH] = {
651 .offset = 0x135, .activate = 10 },
653 [SCARLETT2_CONFIG_AUTOGAIN_STATUS] = {
656 [SCARLETT2_CONFIG_PHANTOM_SWITCH] = {
657 .offset = 0x48, .activate = 11, .mute = 1 },
659 [SCARLETT2_CONFIG_INPUT_GAIN] = {
660 .offset = 0x4b, .activate = 12 },
662 [SCARLETT2_CONFIG_LEVEL_SWITCH] = {
663 .offset = 0x3c, .activate = 13, .mute = 1 },
665 [SCARLETT2_CONFIG_SAFE_SWITCH] = {
666 .offset = 0x147, .activate = 14 },
668 [SCARLETT2_CONFIG_AIR_SWITCH] = {
669 .offset = 0x3e, .activate = 15 },
671 [SCARLETT2_CONFIG_INPUT_SELECT_SWITCH] = {
672 .offset = 0x14b, .activate = 17 },
674 [SCARLETT2_CONFIG_INPUT_LINK_SWITCH] = {
675 .offset = 0x14e, .activate = 18 },
677 [SCARLETT2_CONFIG_DIRECT_MONITOR_GAIN] = {
678 .offset = 0x2a0, .size = 16, .activate = 36 }
683 static const struct scarlett2_config_set scarlett2_config_set_gen4_4i4 = {
684 .notifications = scarlett4_4i4_notifications,
685 .gen4_write_addr = 0x130,
687 [SCARLETT2_CONFIG_MSD_SWITCH] = {
688 .offset = 0x5c, .size = 8, .activate = 4 },
690 [SCARLETT2_CONFIG_AUTOGAIN_SWITCH] = {
691 .offset = 0x13e, .activate = 10 },
693 [SCARLETT2_CONFIG_AUTOGAIN_STATUS] = {
696 [SCARLETT2_CONFIG_PHANTOM_SWITCH] = {
697 .offset = 0x5a, .activate = 11, .mute = 1 },
699 [SCARLETT2_CONFIG_INPUT_GAIN] = {
700 .offset = 0x5e, .activate = 12 },
702 [SCARLETT2_CONFIG_LEVEL_SWITCH] = {
703 .offset = 0x4e, .activate = 13, .mute = 1 },
705 [SCARLETT2_CONFIG_SAFE_SWITCH] = {
706 .offset = 0x150, .activate = 14 },
708 [SCARLETT2_CONFIG_AIR_SWITCH] = {
709 .offset = 0x50, .activate = 15 },
711 [SCARLETT2_CONFIG_INPUT_SELECT_SWITCH] = {
712 .offset = 0x153, .activate = 16 },
714 [SCARLETT2_CONFIG_INPUT_LINK_SWITCH] = {
715 .offset = 0x156, .activate = 17 },
717 [SCARLETT2_CONFIG_MASTER_VOLUME] = {
718 .offset = 0x32, .size = 16 },
720 [SCARLETT2_CONFIG_HEADPHONE_VOLUME] = {
721 .offset = 0x3a, .size = 16 },
723 [SCARLETT2_CONFIG_POWER_EXT] = {
726 [SCARLETT2_CONFIG_POWER_STATUS] = {
731 /* Clarett USB and Clarett+ devices: 2Pre, 4Pre, 8Pre */
732 static const struct scarlett2_config_set scarlett2_config_set_clarett = {
733 .notifications = scarlett2_notifications,
735 [SCARLETT2_CONFIG_DIM_MUTE] = {
736 .offset = 0x31, .size = 8, .activate = 2 },
738 [SCARLETT2_CONFIG_LINE_OUT_VOLUME] = {
739 .offset = 0x34, .size = 16, .activate = 1 },
741 [SCARLETT2_CONFIG_MUTE_SWITCH] = {
742 .offset = 0x5c, .size = 8, .activate = 1 },
744 [SCARLETT2_CONFIG_SW_HW_SWITCH] = {
745 .offset = 0x66, .size = 8, .activate = 3 },
747 [SCARLETT2_CONFIG_MASTER_VOLUME] = {
748 .offset = 0x76, .size = 16 },
750 [SCARLETT2_CONFIG_LEVEL_SWITCH] = {
751 .offset = 0x7c, .size = 8, .activate = 7 },
753 [SCARLETT2_CONFIG_AIR_SWITCH] = {
754 .offset = 0x95, .size = 8, .activate = 8 },
756 [SCARLETT2_CONFIG_STANDALONE_SWITCH] = {
757 .offset = 0x8d, .size = 8, .activate = 6 },
761 /* Description of each hardware port type:
762 * - id: hardware ID of this port type
763 * - src_descr: printf format string for mux input selections
764 * - src_num_offset: added to channel number for the fprintf
765 * - dst_descr: printf format string for mixer controls
767 struct scarlett2_port {
769 const char * const src_descr;
771 const char * const dst_descr;
772 const char * const dsp_src_descr;
773 const char * const dsp_dst_descr;
776 static const struct scarlett2_port scarlett2_ports[SCARLETT2_PORT_TYPE_COUNT] = {
777 [SCARLETT2_PORT_TYPE_NONE] = {
781 [SCARLETT2_PORT_TYPE_ANALOGUE] = {
783 .src_descr = "Analogue %d",
785 .dst_descr = "Analogue Output %02d Playback"
787 [SCARLETT2_PORT_TYPE_SPDIF] = {
789 .src_descr = "S/PDIF %d",
791 .dst_descr = "S/PDIF Output %d Playback"
793 [SCARLETT2_PORT_TYPE_ADAT] = {
795 .src_descr = "ADAT %d",
797 .dst_descr = "ADAT Output %d Playback"
799 [SCARLETT2_PORT_TYPE_MIX] = {
801 .src_descr = "Mix %c",
802 .src_num_offset = 'A',
803 .dst_descr = "Mixer Input %02d Capture",
804 .dsp_src_descr = "DSP %d",
805 .dsp_dst_descr = "DSP Input %d Capture"
807 [SCARLETT2_PORT_TYPE_PCM] = {
809 .src_descr = "PCM %d",
811 .dst_descr = "PCM %02d Capture"
815 /* Number of mux tables: one for each band of sample rates
816 * (44.1/48kHz, 88.2/96kHz, and 176.4/176kHz)
818 #define SCARLETT2_MUX_TABLES 3
820 /* Maximum number of entries in a mux table */
821 #define SCARLETT2_MAX_MUX_ENTRIES 10
823 /* One entry within mux_assignment defines the port type and range of
824 * ports to add to the set_mux message. The end of the list is marked
827 struct scarlett2_mux_entry {
833 /* Maximum number of entries in a mux table */
834 #define SCARLETT2_MAX_METER_ENTRIES 9
836 /* One entry within meter_assignment defines the range of mux outputs
837 * that consecutive meter entries are mapped to. The end of the list
838 * is marked with count == 0.
840 struct scarlett2_meter_entry {
845 struct scarlett2_device_info {
846 /* which set of configuration parameters the device uses */
847 const struct scarlett2_config_set *config_set;
849 /* minimum firmware version required */
850 u16 min_firmware_version;
852 /* support for main/alt speaker switching */
853 u8 has_speaker_switching;
855 /* support for talkback microphone */
858 /* the number of analogue inputs with a software switchable
859 * level control that can be set to line or instrument
861 u8 level_input_count;
863 /* the first input with a level control (0-based) */
864 u8 level_input_first;
866 /* the number of analogue inputs with a software switchable
871 /* the number of analogue inputs with a software switchable
876 /* the first input with an air control (0-based) */
879 /* number of additional air options
880 * 0 for air presence only (Gen 3)
881 * 1 for air presence+drive (Gen 4)
885 /* the number of phantom (48V) software switchable controls */
888 /* the first input with phantom power control (0-based) */
891 /* the number of inputs each phantom switch controls */
892 u8 inputs_per_phantom;
894 /* the number of inputs with software-controllable gain */
897 /* the number of direct monitor options
898 * (0 = none, 1 = mono only, 2 = mono/stereo)
902 /* the number of DSP channels */
905 /* remap analogue outputs; 18i8 Gen 3 has "line 3/4" connected
906 * internally to the analogue 7/8 outputs
908 u8 line_out_remap_enable;
909 u8 line_out_remap[SCARLETT2_ANALOGUE_MAX];
910 u8 line_out_unmap[SCARLETT2_ANALOGUE_MAX];
912 /* additional description for the line out volume controls */
913 const char * const line_out_descrs[SCARLETT2_ANALOGUE_MAX];
915 /* number of sources/destinations of each port type */
916 const int port_count[SCARLETT2_PORT_TYPE_COUNT][SCARLETT2_PORT_DIRNS];
918 /* layout/order of the entries in the set_mux message */
919 struct scarlett2_mux_entry mux_assignment[SCARLETT2_MUX_TABLES]
920 [SCARLETT2_MAX_MUX_ENTRIES];
922 /* map from meter level order returned by
923 * SCARLETT2_USB_GET_METER to index into mux[] entries (same
924 * as the order returned by scarlett2_meter_ctl_get())
926 struct scarlett2_meter_entry meter_map[SCARLETT2_MAX_METER_ENTRIES];
929 struct scarlett2_data {
930 struct usb_mixer_interface *mixer;
931 struct mutex usb_mutex; /* prevent sending concurrent USB requests */
932 struct mutex data_mutex; /* lock access to this data */
934 u8 selected_flash_segment_id;
935 u8 flash_write_state;
936 struct delayed_work work;
937 const struct scarlett2_device_info *info;
938 const struct scarlett2_config_set *config_set;
939 const char *series_name;
940 __u8 bInterfaceNumber;
941 __u8 bEndpointAddress;
942 __u16 wMaxPacketSize;
949 u8 num_monitor_mix_ctls;
950 u32 firmware_version;
951 u8 flash_segment_nums[SCARLETT2_SEGMENT_ID_COUNT];
952 u8 flash_segment_blocks[SCARLETT2_SEGMENT_ID_COUNT];
957 u8 input_level_updated;
958 u8 input_pad_updated;
959 u8 input_air_updated;
960 u8 input_phantom_updated;
961 u8 input_select_updated;
962 u8 input_gain_updated;
964 u8 input_safe_updated;
965 u8 pcm_input_switch_updated;
966 u8 monitor_other_updated;
967 u8 direct_monitor_updated;
970 u8 speaker_switching_switched;
971 u8 power_status_updated;
975 u8 vol[SCARLETT2_ANALOGUE_MAX];
976 u8 vol_sw_hw_switch[SCARLETT2_ANALOGUE_MAX];
977 u8 mute_switch[SCARLETT2_ANALOGUE_MAX];
978 u8 level_switch[SCARLETT2_LEVEL_SWITCH_MAX];
979 u8 pad_switch[SCARLETT2_PAD_SWITCH_MAX];
980 u8 dim_mute[SCARLETT2_DIM_MUTE_COUNT];
981 u8 air_switch[SCARLETT2_AIR_SWITCH_MAX];
982 u8 phantom_switch[SCARLETT2_PHANTOM_SWITCH_MAX];
983 u8 phantom_persistence;
984 u8 input_select_switch;
985 u8 input_link_switch[SCARLETT2_INPUT_GAIN_MAX / 2];
986 u8 gain[SCARLETT2_INPUT_GAIN_MAX];
987 u8 autogain_switch[SCARLETT2_INPUT_GAIN_MAX];
988 u8 autogain_status[SCARLETT2_INPUT_GAIN_MAX];
989 u8 safe_switch[SCARLETT2_INPUT_GAIN_MAX];
991 u8 direct_monitor_switch;
992 u8 speaker_switching_switch;
994 u8 talkback_map[SCARLETT2_OUTPUT_MIX_MAX];
996 u8 standalone_switch;
998 u8 meter_level_map[SCARLETT2_MAX_METERS];
999 struct snd_kcontrol *sync_ctl;
1000 struct snd_kcontrol *master_vol_ctl;
1001 struct snd_kcontrol *headphone_vol_ctl;
1002 struct snd_kcontrol *vol_ctls[SCARLETT2_ANALOGUE_MAX];
1003 struct snd_kcontrol *sw_hw_ctls[SCARLETT2_ANALOGUE_MAX];
1004 struct snd_kcontrol *mute_ctls[SCARLETT2_ANALOGUE_MAX];
1005 struct snd_kcontrol *dim_mute_ctls[SCARLETT2_DIM_MUTE_COUNT];
1006 struct snd_kcontrol *level_ctls[SCARLETT2_LEVEL_SWITCH_MAX];
1007 struct snd_kcontrol *pad_ctls[SCARLETT2_PAD_SWITCH_MAX];
1008 struct snd_kcontrol *air_ctls[SCARLETT2_AIR_SWITCH_MAX];
1009 struct snd_kcontrol *phantom_ctls[SCARLETT2_PHANTOM_SWITCH_MAX];
1010 struct snd_kcontrol *input_select_ctl;
1011 struct snd_kcontrol *input_link_ctls[SCARLETT2_INPUT_GAIN_MAX / 2];
1012 struct snd_kcontrol *input_gain_ctls[SCARLETT2_INPUT_GAIN_MAX];
1013 struct snd_kcontrol *autogain_ctls[SCARLETT2_INPUT_GAIN_MAX];
1014 struct snd_kcontrol *autogain_status_ctls[SCARLETT2_INPUT_GAIN_MAX];
1015 struct snd_kcontrol *safe_ctls[SCARLETT2_INPUT_GAIN_MAX];
1016 struct snd_kcontrol *pcm_input_switch_ctl;
1017 struct snd_kcontrol *mux_ctls[SCARLETT2_MUX_MAX];
1018 struct snd_kcontrol *mix_ctls[SCARLETT2_MIX_MAX];
1019 struct snd_kcontrol *direct_monitor_ctl;
1020 struct snd_kcontrol *speaker_switching_ctl;
1021 struct snd_kcontrol *talkback_ctl;
1022 struct snd_kcontrol *power_status_ctl;
1023 u8 mux[SCARLETT2_MUX_MAX];
1024 u8 mix[SCARLETT2_MIX_MAX];
1025 u8 monitor_mix[SCARLETT2_MONITOR_MIX_MAX];
1028 /*** Model-specific data ***/
1030 static const struct scarlett2_device_info s6i6_gen2_info = {
1031 .config_set = &scarlett2_config_set_gen2a,
1032 .level_input_count = 2,
1033 .pad_input_count = 2,
1035 .line_out_descrs = {
1043 [SCARLETT2_PORT_TYPE_NONE] = { 1, 0 },
1044 [SCARLETT2_PORT_TYPE_ANALOGUE] = { 4, 4 },
1045 [SCARLETT2_PORT_TYPE_SPDIF] = { 2, 2 },
1046 [SCARLETT2_PORT_TYPE_MIX] = { 10, 18 },
1047 [SCARLETT2_PORT_TYPE_PCM] = { 6, 6 },
1050 .mux_assignment = { {
1051 { SCARLETT2_PORT_TYPE_PCM, 0, 6 },
1052 { SCARLETT2_PORT_TYPE_ANALOGUE, 0, 4 },
1053 { SCARLETT2_PORT_TYPE_SPDIF, 0, 2 },
1054 { SCARLETT2_PORT_TYPE_MIX, 0, 18 },
1055 { SCARLETT2_PORT_TYPE_NONE, 0, 8 },
1058 { SCARLETT2_PORT_TYPE_PCM, 0, 6 },
1059 { SCARLETT2_PORT_TYPE_ANALOGUE, 0, 4 },
1060 { SCARLETT2_PORT_TYPE_SPDIF, 0, 2 },
1061 { SCARLETT2_PORT_TYPE_MIX, 0, 18 },
1062 { SCARLETT2_PORT_TYPE_NONE, 0, 8 },
1065 { SCARLETT2_PORT_TYPE_PCM, 0, 6 },
1066 { SCARLETT2_PORT_TYPE_ANALOGUE, 0, 4 },
1067 { SCARLETT2_PORT_TYPE_SPDIF, 0, 2 },
1068 { SCARLETT2_PORT_TYPE_MIX, 0, 18 },
1069 { SCARLETT2_PORT_TYPE_NONE, 0, 8 },
1080 static const struct scarlett2_device_info s18i8_gen2_info = {
1081 .config_set = &scarlett2_config_set_gen2a,
1082 .level_input_count = 2,
1083 .pad_input_count = 4,
1085 .line_out_descrs = {
1095 [SCARLETT2_PORT_TYPE_NONE] = { 1, 0 },
1096 [SCARLETT2_PORT_TYPE_ANALOGUE] = { 8, 6 },
1097 [SCARLETT2_PORT_TYPE_SPDIF] = { 2, 2 },
1098 [SCARLETT2_PORT_TYPE_ADAT] = { 8, 0 },
1099 [SCARLETT2_PORT_TYPE_MIX] = { 10, 18 },
1100 [SCARLETT2_PORT_TYPE_PCM] = { 8, 18 },
1103 .mux_assignment = { {
1104 { SCARLETT2_PORT_TYPE_PCM, 0, 18 },
1105 { SCARLETT2_PORT_TYPE_ANALOGUE, 0, 6 },
1106 { SCARLETT2_PORT_TYPE_SPDIF, 0, 2 },
1107 { SCARLETT2_PORT_TYPE_MIX, 0, 18 },
1108 { SCARLETT2_PORT_TYPE_NONE, 0, 8 },
1111 { SCARLETT2_PORT_TYPE_PCM, 0, 14 },
1112 { SCARLETT2_PORT_TYPE_ANALOGUE, 0, 6 },
1113 { SCARLETT2_PORT_TYPE_SPDIF, 0, 2 },
1114 { SCARLETT2_PORT_TYPE_MIX, 0, 18 },
1115 { SCARLETT2_PORT_TYPE_NONE, 0, 8 },
1118 { SCARLETT2_PORT_TYPE_PCM, 0, 10 },
1119 { SCARLETT2_PORT_TYPE_ANALOGUE, 0, 6 },
1120 { SCARLETT2_PORT_TYPE_SPDIF, 0, 2 },
1121 { SCARLETT2_PORT_TYPE_MIX, 0, 18 },
1122 { SCARLETT2_PORT_TYPE_NONE, 0, 4 },
1133 static const struct scarlett2_device_info s18i20_gen2_info = {
1134 .config_set = &scarlett2_config_set_gen2b,
1136 .line_out_descrs = {
1150 [SCARLETT2_PORT_TYPE_NONE] = { 1, 0 },
1151 [SCARLETT2_PORT_TYPE_ANALOGUE] = { 8, 10 },
1152 [SCARLETT2_PORT_TYPE_SPDIF] = { 2, 2 },
1153 [SCARLETT2_PORT_TYPE_ADAT] = { 8, 8 },
1154 [SCARLETT2_PORT_TYPE_MIX] = { 10, 18 },
1155 [SCARLETT2_PORT_TYPE_PCM] = { 20, 18 },
1158 .mux_assignment = { {
1159 { SCARLETT2_PORT_TYPE_PCM, 0, 18 },
1160 { SCARLETT2_PORT_TYPE_ANALOGUE, 0, 10 },
1161 { SCARLETT2_PORT_TYPE_SPDIF, 0, 2 },
1162 { SCARLETT2_PORT_TYPE_ADAT, 0, 8 },
1163 { SCARLETT2_PORT_TYPE_MIX, 0, 18 },
1164 { SCARLETT2_PORT_TYPE_NONE, 0, 8 },
1167 { SCARLETT2_PORT_TYPE_PCM, 0, 14 },
1168 { SCARLETT2_PORT_TYPE_ANALOGUE, 0, 10 },
1169 { SCARLETT2_PORT_TYPE_SPDIF, 0, 2 },
1170 { SCARLETT2_PORT_TYPE_ADAT, 0, 4 },
1171 { SCARLETT2_PORT_TYPE_MIX, 0, 18 },
1172 { SCARLETT2_PORT_TYPE_NONE, 0, 8 },
1175 { SCARLETT2_PORT_TYPE_PCM, 0, 10 },
1176 { SCARLETT2_PORT_TYPE_ANALOGUE, 0, 10 },
1177 { SCARLETT2_PORT_TYPE_SPDIF, 0, 2 },
1178 { SCARLETT2_PORT_TYPE_MIX, 0, 18 },
1179 { SCARLETT2_PORT_TYPE_NONE, 0, 6 },
1190 static const struct scarlett2_device_info solo_gen3_info = {
1191 .config_set = &scarlett2_config_set_gen3a,
1192 .level_input_count = 1,
1193 .level_input_first = 1,
1194 .air_input_count = 1,
1196 .inputs_per_phantom = 1,
1197 .direct_monitor = 1,
1200 static const struct scarlett2_device_info s2i2_gen3_info = {
1201 .config_set = &scarlett2_config_set_gen3a,
1202 .level_input_count = 2,
1203 .air_input_count = 2,
1205 .inputs_per_phantom = 2,
1206 .direct_monitor = 2,
1209 static const struct scarlett2_device_info s4i4_gen3_info = {
1210 .config_set = &scarlett2_config_set_gen3b,
1211 .level_input_count = 2,
1212 .pad_input_count = 2,
1213 .air_input_count = 2,
1215 .inputs_per_phantom = 2,
1217 .line_out_descrs = {
1225 [SCARLETT2_PORT_TYPE_NONE] = { 1, 0 },
1226 [SCARLETT2_PORT_TYPE_ANALOGUE] = { 4, 4 },
1227 [SCARLETT2_PORT_TYPE_MIX] = { 6, 8 },
1228 [SCARLETT2_PORT_TYPE_PCM] = { 4, 6 },
1231 .mux_assignment = { {
1232 { SCARLETT2_PORT_TYPE_PCM, 0, 6 },
1233 { SCARLETT2_PORT_TYPE_ANALOGUE, 0, 4 },
1234 { SCARLETT2_PORT_TYPE_MIX, 0, 8 },
1235 { SCARLETT2_PORT_TYPE_NONE, 0, 16 },
1238 { SCARLETT2_PORT_TYPE_PCM, 0, 6 },
1239 { SCARLETT2_PORT_TYPE_ANALOGUE, 0, 4 },
1240 { SCARLETT2_PORT_TYPE_MIX, 0, 8 },
1241 { SCARLETT2_PORT_TYPE_NONE, 0, 16 },
1244 { SCARLETT2_PORT_TYPE_PCM, 0, 6 },
1245 { SCARLETT2_PORT_TYPE_ANALOGUE, 0, 4 },
1246 { SCARLETT2_PORT_TYPE_MIX, 0, 8 },
1247 { SCARLETT2_PORT_TYPE_NONE, 0, 16 },
1258 static const struct scarlett2_device_info s8i6_gen3_info = {
1259 .config_set = &scarlett2_config_set_gen3b,
1260 .level_input_count = 2,
1261 .pad_input_count = 2,
1262 .air_input_count = 2,
1264 .inputs_per_phantom = 2,
1266 .line_out_descrs = {
1274 [SCARLETT2_PORT_TYPE_NONE] = { 1, 0 },
1275 [SCARLETT2_PORT_TYPE_ANALOGUE] = { 6, 4 },
1276 [SCARLETT2_PORT_TYPE_SPDIF] = { 2, 2 },
1277 [SCARLETT2_PORT_TYPE_MIX] = { 8, 8 },
1278 [SCARLETT2_PORT_TYPE_PCM] = { 6, 10 },
1281 .mux_assignment = { {
1282 { SCARLETT2_PORT_TYPE_PCM, 0, 8 },
1283 { SCARLETT2_PORT_TYPE_ANALOGUE, 0, 4 },
1284 { SCARLETT2_PORT_TYPE_SPDIF, 0, 2 },
1285 { SCARLETT2_PORT_TYPE_PCM, 8, 2 },
1286 { SCARLETT2_PORT_TYPE_MIX, 0, 8 },
1287 { SCARLETT2_PORT_TYPE_NONE, 0, 18 },
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 },
1316 static const struct scarlett2_device_info s18i8_gen3_info = {
1317 .config_set = &scarlett2_config_set_gen3c,
1318 .has_speaker_switching = 1,
1319 .level_input_count = 2,
1320 .pad_input_count = 4,
1321 .air_input_count = 4,
1323 .inputs_per_phantom = 2,
1325 .line_out_remap_enable = 1,
1326 .line_out_remap = { 0, 1, 6, 7, 2, 3, 4, 5 },
1327 .line_out_unmap = { 0, 1, 4, 5, 6, 7, 2, 3 },
1329 .line_out_descrs = {
1341 [SCARLETT2_PORT_TYPE_NONE] = { 1, 0 },
1342 [SCARLETT2_PORT_TYPE_ANALOGUE] = { 8, 8 },
1343 [SCARLETT2_PORT_TYPE_SPDIF] = { 2, 2 },
1344 [SCARLETT2_PORT_TYPE_ADAT] = { 8, 0 },
1345 [SCARLETT2_PORT_TYPE_MIX] = { 10, 20 },
1346 [SCARLETT2_PORT_TYPE_PCM] = { 8, 20 },
1349 .mux_assignment = { {
1350 { SCARLETT2_PORT_TYPE_PCM, 0, 10 },
1351 { SCARLETT2_PORT_TYPE_PCM, 12, 8 },
1352 { SCARLETT2_PORT_TYPE_ANALOGUE, 0, 2 },
1353 { SCARLETT2_PORT_TYPE_ANALOGUE, 6, 2 },
1354 { SCARLETT2_PORT_TYPE_ANALOGUE, 2, 4 },
1355 { SCARLETT2_PORT_TYPE_SPDIF, 0, 2 },
1356 { SCARLETT2_PORT_TYPE_PCM, 10, 2 },
1357 { SCARLETT2_PORT_TYPE_MIX, 0, 20 },
1358 { SCARLETT2_PORT_TYPE_NONE, 0, 10 },
1361 { SCARLETT2_PORT_TYPE_PCM, 0, 10 },
1362 { SCARLETT2_PORT_TYPE_PCM, 12, 4 },
1363 { SCARLETT2_PORT_TYPE_ANALOGUE, 0, 2 },
1364 { SCARLETT2_PORT_TYPE_ANALOGUE, 6, 2 },
1365 { SCARLETT2_PORT_TYPE_ANALOGUE, 2, 4 },
1366 { SCARLETT2_PORT_TYPE_SPDIF, 0, 2 },
1367 { SCARLETT2_PORT_TYPE_PCM, 10, 2 },
1368 { SCARLETT2_PORT_TYPE_MIX, 0, 20 },
1369 { SCARLETT2_PORT_TYPE_NONE, 0, 10 },
1372 { SCARLETT2_PORT_TYPE_PCM, 0, 10 },
1373 { SCARLETT2_PORT_TYPE_ANALOGUE, 0, 2 },
1374 { SCARLETT2_PORT_TYPE_ANALOGUE, 6, 2 },
1375 { SCARLETT2_PORT_TYPE_ANALOGUE, 2, 4 },
1376 { SCARLETT2_PORT_TYPE_SPDIF, 0, 2 },
1377 { SCARLETT2_PORT_TYPE_MIX, 0, 20 },
1378 { SCARLETT2_PORT_TYPE_NONE, 0, 10 },
1395 static const struct scarlett2_device_info s18i20_gen3_info = {
1396 .config_set = &scarlett2_config_set_gen3c,
1397 .has_speaker_switching = 1,
1399 .level_input_count = 2,
1400 .pad_input_count = 8,
1401 .air_input_count = 8,
1403 .inputs_per_phantom = 4,
1405 .line_out_descrs = {
1419 [SCARLETT2_PORT_TYPE_NONE] = { 1, 0 },
1420 [SCARLETT2_PORT_TYPE_ANALOGUE] = { 9, 10 },
1421 [SCARLETT2_PORT_TYPE_SPDIF] = { 2, 2 },
1422 [SCARLETT2_PORT_TYPE_ADAT] = { 8, 8 },
1423 [SCARLETT2_PORT_TYPE_MIX] = { 12, 25 },
1424 [SCARLETT2_PORT_TYPE_PCM] = { 20, 20 },
1427 .mux_assignment = { {
1428 { SCARLETT2_PORT_TYPE_PCM, 0, 8 },
1429 { SCARLETT2_PORT_TYPE_PCM, 10, 10 },
1430 { SCARLETT2_PORT_TYPE_ANALOGUE, 0, 10 },
1431 { SCARLETT2_PORT_TYPE_SPDIF, 0, 2 },
1432 { SCARLETT2_PORT_TYPE_ADAT, 0, 8 },
1433 { SCARLETT2_PORT_TYPE_PCM, 8, 2 },
1434 { SCARLETT2_PORT_TYPE_MIX, 0, 25 },
1435 { SCARLETT2_PORT_TYPE_NONE, 0, 12 },
1438 { SCARLETT2_PORT_TYPE_PCM, 0, 8 },
1439 { SCARLETT2_PORT_TYPE_PCM, 10, 8 },
1440 { SCARLETT2_PORT_TYPE_ANALOGUE, 0, 10 },
1441 { SCARLETT2_PORT_TYPE_SPDIF, 0, 2 },
1442 { SCARLETT2_PORT_TYPE_ADAT, 0, 8 },
1443 { SCARLETT2_PORT_TYPE_PCM, 8, 2 },
1444 { SCARLETT2_PORT_TYPE_MIX, 0, 25 },
1445 { SCARLETT2_PORT_TYPE_NONE, 0, 10 },
1448 { SCARLETT2_PORT_TYPE_PCM, 0, 10 },
1449 { SCARLETT2_PORT_TYPE_ANALOGUE, 0, 10 },
1450 { SCARLETT2_PORT_TYPE_SPDIF, 0, 2 },
1451 { SCARLETT2_PORT_TYPE_NONE, 0, 24 },
1465 static const struct scarlett2_device_info solo_gen4_info = {
1466 .config_set = &scarlett2_config_set_gen4_solo,
1467 .min_firmware_version = 2115,
1469 .level_input_count = 1,
1470 .air_input_count = 1,
1471 .air_input_first = 1,
1475 .inputs_per_phantom = 1,
1476 .direct_monitor = 1,
1480 [SCARLETT2_PORT_TYPE_NONE] = { 1, 0 },
1481 [SCARLETT2_PORT_TYPE_ANALOGUE] = { 2, 2 },
1482 [SCARLETT2_PORT_TYPE_MIX] = { 8, 6 },
1483 [SCARLETT2_PORT_TYPE_PCM] = { 2, 4 },
1486 .mux_assignment = { {
1487 { SCARLETT2_PORT_TYPE_MIX, 4, 2 },
1488 { SCARLETT2_PORT_TYPE_MIX, 2, 2 },
1489 { SCARLETT2_PORT_TYPE_PCM, 0, 4 },
1490 { SCARLETT2_PORT_TYPE_MIX, 0, 2 },
1491 { SCARLETT2_PORT_TYPE_ANALOGUE, 0, 2 },
1494 { SCARLETT2_PORT_TYPE_MIX, 4, 2 },
1495 { SCARLETT2_PORT_TYPE_MIX, 2, 2 },
1496 { SCARLETT2_PORT_TYPE_PCM, 0, 4 },
1497 { SCARLETT2_PORT_TYPE_MIX, 0, 2 },
1498 { SCARLETT2_PORT_TYPE_ANALOGUE, 0, 2 },
1501 { SCARLETT2_PORT_TYPE_MIX, 4, 2 },
1502 { SCARLETT2_PORT_TYPE_MIX, 2, 2 },
1503 { SCARLETT2_PORT_TYPE_PCM, 0, 4 },
1504 { SCARLETT2_PORT_TYPE_MIX, 0, 2 },
1505 { SCARLETT2_PORT_TYPE_ANALOGUE, 0, 2 },
1519 static const struct scarlett2_device_info s2i2_gen4_info = {
1520 .config_set = &scarlett2_config_set_gen4_2i2,
1521 .min_firmware_version = 2115,
1523 .level_input_count = 2,
1524 .air_input_count = 2,
1527 .inputs_per_phantom = 2,
1528 .gain_input_count = 2,
1529 .direct_monitor = 2,
1533 [SCARLETT2_PORT_TYPE_NONE] = { 1, 0 },
1534 [SCARLETT2_PORT_TYPE_ANALOGUE] = { 2, 2 },
1535 [SCARLETT2_PORT_TYPE_MIX] = { 6, 6 },
1536 [SCARLETT2_PORT_TYPE_PCM] = { 2, 4 },
1539 .mux_assignment = { {
1540 { SCARLETT2_PORT_TYPE_MIX, 4, 2 },
1541 { SCARLETT2_PORT_TYPE_MIX, 2, 2 },
1542 { SCARLETT2_PORT_TYPE_PCM, 0, 4 },
1543 { SCARLETT2_PORT_TYPE_MIX, 0, 2 },
1544 { SCARLETT2_PORT_TYPE_ANALOGUE, 0, 2 },
1547 { SCARLETT2_PORT_TYPE_MIX, 4, 2 },
1548 { SCARLETT2_PORT_TYPE_MIX, 2, 2 },
1549 { SCARLETT2_PORT_TYPE_PCM, 0, 4 },
1550 { SCARLETT2_PORT_TYPE_MIX, 0, 2 },
1551 { SCARLETT2_PORT_TYPE_ANALOGUE, 0, 2 },
1554 { SCARLETT2_PORT_TYPE_MIX, 4, 2 },
1555 { SCARLETT2_PORT_TYPE_MIX, 2, 2 },
1556 { SCARLETT2_PORT_TYPE_PCM, 0, 4 },
1557 { SCARLETT2_PORT_TYPE_MIX, 0, 2 },
1558 { SCARLETT2_PORT_TYPE_ANALOGUE, 0, 2 },
1572 static const struct scarlett2_device_info s4i4_gen4_info = {
1573 .config_set = &scarlett2_config_set_gen4_4i4,
1574 .min_firmware_version = 2089,
1576 .level_input_count = 2,
1577 .air_input_count = 2,
1580 .inputs_per_phantom = 1,
1581 .gain_input_count = 2,
1585 [SCARLETT2_PORT_TYPE_NONE] = { 1, 0 },
1586 [SCARLETT2_PORT_TYPE_ANALOGUE] = { 4, 6 },
1587 [SCARLETT2_PORT_TYPE_MIX] = { 8, 12 },
1588 [SCARLETT2_PORT_TYPE_PCM] = { 6, 6 },
1591 .mux_assignment = { {
1592 { SCARLETT2_PORT_TYPE_MIX, 10, 2 },
1593 { SCARLETT2_PORT_TYPE_PCM, 0, 6 },
1594 { SCARLETT2_PORT_TYPE_MIX, 0, 10 },
1595 { SCARLETT2_PORT_TYPE_ANALOGUE, 0, 6 },
1598 { SCARLETT2_PORT_TYPE_MIX, 10, 2 },
1599 { SCARLETT2_PORT_TYPE_PCM, 0, 6 },
1600 { SCARLETT2_PORT_TYPE_MIX, 0, 10 },
1601 { SCARLETT2_PORT_TYPE_ANALOGUE, 0, 6 },
1604 { SCARLETT2_PORT_TYPE_MIX, 10, 2 },
1605 { SCARLETT2_PORT_TYPE_PCM, 0, 6 },
1606 { SCARLETT2_PORT_TYPE_MIX, 0, 10 },
1607 { SCARLETT2_PORT_TYPE_ANALOGUE, 0, 6 },
1619 static const struct scarlett2_device_info clarett_2pre_info = {
1620 .config_set = &scarlett2_config_set_clarett,
1621 .level_input_count = 2,
1622 .air_input_count = 2,
1624 .line_out_descrs = {
1632 [SCARLETT2_PORT_TYPE_NONE] = { 1, 0 },
1633 [SCARLETT2_PORT_TYPE_ANALOGUE] = { 2, 4 },
1634 [SCARLETT2_PORT_TYPE_SPDIF] = { 2, 0 },
1635 [SCARLETT2_PORT_TYPE_ADAT] = { 8, 0 },
1636 [SCARLETT2_PORT_TYPE_MIX] = { 10, 18 },
1637 [SCARLETT2_PORT_TYPE_PCM] = { 4, 12 },
1640 .mux_assignment = { {
1641 { SCARLETT2_PORT_TYPE_PCM, 0, 12 },
1642 { SCARLETT2_PORT_TYPE_ANALOGUE, 0, 4 },
1643 { SCARLETT2_PORT_TYPE_MIX, 0, 18 },
1644 { SCARLETT2_PORT_TYPE_NONE, 0, 8 },
1647 { SCARLETT2_PORT_TYPE_PCM, 0, 8 },
1648 { SCARLETT2_PORT_TYPE_ANALOGUE, 0, 4 },
1649 { SCARLETT2_PORT_TYPE_MIX, 0, 18 },
1650 { SCARLETT2_PORT_TYPE_NONE, 0, 8 },
1653 { SCARLETT2_PORT_TYPE_PCM, 0, 2 },
1654 { SCARLETT2_PORT_TYPE_ANALOGUE, 0, 4 },
1655 { SCARLETT2_PORT_TYPE_NONE, 0, 26 },
1666 static const struct scarlett2_device_info clarett_4pre_info = {
1667 .config_set = &scarlett2_config_set_clarett,
1668 .level_input_count = 2,
1669 .air_input_count = 4,
1671 .line_out_descrs = {
1681 [SCARLETT2_PORT_TYPE_NONE] = { 1, 0 },
1682 [SCARLETT2_PORT_TYPE_ANALOGUE] = { 8, 6 },
1683 [SCARLETT2_PORT_TYPE_SPDIF] = { 2, 2 },
1684 [SCARLETT2_PORT_TYPE_ADAT] = { 8, 0 },
1685 [SCARLETT2_PORT_TYPE_MIX] = { 10, 18 },
1686 [SCARLETT2_PORT_TYPE_PCM] = { 8, 18 },
1689 .mux_assignment = { {
1690 { SCARLETT2_PORT_TYPE_PCM, 0, 18 },
1691 { SCARLETT2_PORT_TYPE_ANALOGUE, 0, 6 },
1692 { SCARLETT2_PORT_TYPE_SPDIF, 0, 2 },
1693 { SCARLETT2_PORT_TYPE_MIX, 0, 18 },
1694 { SCARLETT2_PORT_TYPE_NONE, 0, 8 },
1697 { SCARLETT2_PORT_TYPE_PCM, 0, 14 },
1698 { SCARLETT2_PORT_TYPE_ANALOGUE, 0, 6 },
1699 { SCARLETT2_PORT_TYPE_SPDIF, 0, 2 },
1700 { SCARLETT2_PORT_TYPE_MIX, 0, 18 },
1701 { SCARLETT2_PORT_TYPE_NONE, 0, 8 },
1704 { SCARLETT2_PORT_TYPE_PCM, 0, 12 },
1705 { SCARLETT2_PORT_TYPE_ANALOGUE, 0, 6 },
1706 { SCARLETT2_PORT_TYPE_SPDIF, 0, 2 },
1707 { SCARLETT2_PORT_TYPE_NONE, 0, 24 },
1718 static const struct scarlett2_device_info clarett_8pre_info = {
1719 .config_set = &scarlett2_config_set_clarett,
1720 .level_input_count = 2,
1721 .air_input_count = 8,
1723 .line_out_descrs = {
1737 [SCARLETT2_PORT_TYPE_NONE] = { 1, 0 },
1738 [SCARLETT2_PORT_TYPE_ANALOGUE] = { 8, 10 },
1739 [SCARLETT2_PORT_TYPE_SPDIF] = { 2, 2 },
1740 [SCARLETT2_PORT_TYPE_ADAT] = { 8, 8 },
1741 [SCARLETT2_PORT_TYPE_MIX] = { 10, 18 },
1742 [SCARLETT2_PORT_TYPE_PCM] = { 20, 18 },
1745 .mux_assignment = { {
1746 { SCARLETT2_PORT_TYPE_PCM, 0, 18 },
1747 { SCARLETT2_PORT_TYPE_ANALOGUE, 0, 10 },
1748 { SCARLETT2_PORT_TYPE_SPDIF, 0, 2 },
1749 { SCARLETT2_PORT_TYPE_ADAT, 0, 8 },
1750 { SCARLETT2_PORT_TYPE_MIX, 0, 18 },
1751 { SCARLETT2_PORT_TYPE_NONE, 0, 8 },
1754 { SCARLETT2_PORT_TYPE_PCM, 0, 14 },
1755 { SCARLETT2_PORT_TYPE_ANALOGUE, 0, 10 },
1756 { SCARLETT2_PORT_TYPE_SPDIF, 0, 2 },
1757 { SCARLETT2_PORT_TYPE_ADAT, 0, 4 },
1758 { SCARLETT2_PORT_TYPE_MIX, 0, 18 },
1759 { SCARLETT2_PORT_TYPE_NONE, 0, 8 },
1762 { SCARLETT2_PORT_TYPE_PCM, 0, 12 },
1763 { SCARLETT2_PORT_TYPE_ANALOGUE, 0, 10 },
1764 { SCARLETT2_PORT_TYPE_SPDIF, 0, 2 },
1765 { SCARLETT2_PORT_TYPE_NONE, 0, 22 },
1776 struct scarlett2_device_entry {
1777 const u32 usb_id; /* USB device identifier */
1778 const struct scarlett2_device_info *info;
1779 const char *series_name;
1782 static const struct scarlett2_device_entry scarlett2_devices[] = {
1783 /* Supported Gen 2 devices */
1784 { USB_ID(0x1235, 0x8203), &s6i6_gen2_info, "Scarlett Gen 2" },
1785 { USB_ID(0x1235, 0x8204), &s18i8_gen2_info, "Scarlett Gen 2" },
1786 { USB_ID(0x1235, 0x8201), &s18i20_gen2_info, "Scarlett Gen 2" },
1788 /* Supported Gen 3 devices */
1789 { USB_ID(0x1235, 0x8211), &solo_gen3_info, "Scarlett Gen 3" },
1790 { USB_ID(0x1235, 0x8210), &s2i2_gen3_info, "Scarlett Gen 3" },
1791 { USB_ID(0x1235, 0x8212), &s4i4_gen3_info, "Scarlett Gen 3" },
1792 { USB_ID(0x1235, 0x8213), &s8i6_gen3_info, "Scarlett Gen 3" },
1793 { USB_ID(0x1235, 0x8214), &s18i8_gen3_info, "Scarlett Gen 3" },
1794 { USB_ID(0x1235, 0x8215), &s18i20_gen3_info, "Scarlett Gen 3" },
1796 /* Supported Gen 4 devices */
1797 { USB_ID(0x1235, 0x8218), &solo_gen4_info, "Scarlett Gen 4" },
1798 { USB_ID(0x1235, 0x8219), &s2i2_gen4_info, "Scarlett Gen 4" },
1799 { USB_ID(0x1235, 0x821a), &s4i4_gen4_info, "Scarlett Gen 4" },
1801 /* Supported Clarett USB/Clarett+ devices */
1802 { USB_ID(0x1235, 0x8206), &clarett_2pre_info, "Clarett USB" },
1803 { USB_ID(0x1235, 0x8207), &clarett_4pre_info, "Clarett USB" },
1804 { USB_ID(0x1235, 0x8208), &clarett_8pre_info, "Clarett USB" },
1805 { USB_ID(0x1235, 0x820a), &clarett_2pre_info, "Clarett+" },
1806 { USB_ID(0x1235, 0x820b), &clarett_4pre_info, "Clarett+" },
1807 { USB_ID(0x1235, 0x820c), &clarett_8pre_info, "Clarett+" },
1813 /* get the starting port index number for a given port type/direction */
1814 static int scarlett2_get_port_start_num(
1815 const int port_count[][SCARLETT2_PORT_DIRNS],
1816 int direction, int port_type)
1820 for (i = 0; i < port_type; i++)
1821 num += port_count[i][direction];
1826 /*** USB Interactions ***/
1828 /* Commands for sending/receiving requests/responses */
1829 #define SCARLETT2_USB_CMD_INIT 0
1830 #define SCARLETT2_USB_CMD_REQ 2
1831 #define SCARLETT2_USB_CMD_RESP 3
1833 #define SCARLETT2_USB_INIT_1 0x00000000
1834 #define SCARLETT2_USB_INIT_2 0x00000002
1835 #define SCARLETT2_USB_REBOOT 0x00000003
1836 #define SCARLETT2_USB_GET_METER 0x00001001
1837 #define SCARLETT2_USB_GET_MIX 0x00002001
1838 #define SCARLETT2_USB_SET_MIX 0x00002002
1839 #define SCARLETT2_USB_GET_MUX 0x00003001
1840 #define SCARLETT2_USB_SET_MUX 0x00003002
1841 #define SCARLETT2_USB_INFO_FLASH 0x00004000
1842 #define SCARLETT2_USB_INFO_SEGMENT 0x00004001
1843 #define SCARLETT2_USB_ERASE_SEGMENT 0x00004002
1844 #define SCARLETT2_USB_GET_ERASE 0x00004003
1845 #define SCARLETT2_USB_WRITE_SEGMENT 0x00004004
1846 #define SCARLETT2_USB_GET_SYNC 0x00006004
1847 #define SCARLETT2_USB_GET_DATA 0x00800000
1848 #define SCARLETT2_USB_SET_DATA 0x00800001
1849 #define SCARLETT2_USB_DATA_CMD 0x00800002
1851 #define SCARLETT2_USB_CONFIG_SAVE 6
1853 #define SCARLETT2_USB_METER_LEVELS_GET_MAGIC 1
1855 #define SCARLETT2_FLASH_BLOCK_SIZE 4096
1856 #define SCARLETT2_FLASH_WRITE_MAX 1024
1857 #define SCARLETT2_SEGMENT_NUM_MIN 1
1858 #define SCARLETT2_SEGMENT_NUM_MAX 4
1860 #define SCARLETT2_SEGMENT_SETTINGS_NAME "App_Settings"
1861 #define SCARLETT2_SEGMENT_FIRMWARE_NAME "App_Upgrade"
1863 /* proprietary request/response format */
1864 struct scarlett2_usb_packet {
1873 static void scarlett2_fill_request_header(struct scarlett2_data *private,
1874 struct scarlett2_usb_packet *req,
1875 u32 cmd, u16 req_size)
1877 /* sequence must go up by 1 for each request */
1878 u16 seq = private->scarlett2_seq++;
1880 req->cmd = cpu_to_le32(cmd);
1881 req->size = cpu_to_le16(req_size);
1882 req->seq = cpu_to_le16(seq);
1887 static int scarlett2_usb_tx(struct usb_device *dev, int interface,
1888 void *buf, u16 size)
1890 return snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0),
1891 SCARLETT2_USB_CMD_REQ,
1892 USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_OUT,
1893 0, interface, buf, size);
1896 static int scarlett2_usb_rx(struct usb_device *dev, int interface,
1897 u32 usb_req, void *buf, u16 size)
1899 return snd_usb_ctl_msg(dev, usb_rcvctrlpipe(dev, 0),
1901 USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_IN,
1902 0, interface, buf, size);
1905 /* Send a proprietary format request to the Scarlett interface */
1906 static int scarlett2_usb(
1907 struct usb_mixer_interface *mixer, u32 cmd,
1908 void *req_data, u16 req_size, void *resp_data, u16 resp_size)
1910 struct scarlett2_data *private = mixer->private_data;
1911 struct usb_device *dev = mixer->chip->dev;
1912 struct scarlett2_usb_packet *req, *resp = NULL;
1913 size_t req_buf_size = struct_size(req, data, req_size);
1914 size_t resp_buf_size = struct_size(resp, data, resp_size);
1917 req = kmalloc(req_buf_size, GFP_KERNEL);
1923 resp = kmalloc(resp_buf_size, GFP_KERNEL);
1929 mutex_lock(&private->usb_mutex);
1931 /* build request message and send it */
1933 scarlett2_fill_request_header(private, req, cmd, req_size);
1936 memcpy(req->data, req_data, req_size);
1938 err = scarlett2_usb_tx(dev, private->bInterfaceNumber,
1941 if (err != req_buf_size) {
1944 "%s USB request result cmd %x was %d\n",
1945 private->series_name, cmd, err);
1950 /* send a second message to get the response */
1952 err = scarlett2_usb_rx(dev, private->bInterfaceNumber,
1953 SCARLETT2_USB_CMD_RESP,
1954 resp, resp_buf_size);
1956 /* validate the response */
1958 if (err != resp_buf_size) {
1960 /* ESHUTDOWN and EPROTO are valid responses to a
1963 if (cmd == SCARLETT2_USB_REBOOT &&
1964 (err == -ESHUTDOWN || err == -EPROTO)) {
1971 "%s USB response result cmd %x was %d expected %zu\n",
1972 private->series_name, cmd, err, resp_buf_size);
1977 /* cmd/seq/size should match except when initialising
1978 * seq sent = 1, response = 0
1980 if (resp->cmd != req->cmd ||
1981 (resp->seq != req->seq &&
1982 (le16_to_cpu(req->seq) != 1 || resp->seq != 0)) ||
1983 resp_size != le16_to_cpu(resp->size) ||
1988 "%s USB invalid response; "
1989 "cmd tx/rx %d/%d seq %d/%d size %d/%d "
1990 "error %d pad %d\n",
1991 private->series_name,
1992 le32_to_cpu(req->cmd), le32_to_cpu(resp->cmd),
1993 le16_to_cpu(req->seq), le16_to_cpu(resp->seq),
1994 resp_size, le16_to_cpu(resp->size),
1995 le32_to_cpu(resp->error),
1996 le32_to_cpu(resp->pad));
2001 if (resp_data && resp_size > 0)
2002 memcpy(resp_data, resp->data, resp_size);
2005 mutex_unlock(&private->usb_mutex);
2012 /* Send a USB message to get data; result placed in *buf */
2013 static int scarlett2_usb_get(
2014 struct usb_mixer_interface *mixer,
2015 int offset, void *buf, int size)
2022 req.offset = cpu_to_le32(offset);
2023 req.size = cpu_to_le32(size);
2024 return scarlett2_usb(mixer, SCARLETT2_USB_GET_DATA,
2025 &req, sizeof(req), buf, size);
2028 /* Return true if the given configuration item is present in the
2029 * configuration set used by this device.
2031 static int scarlett2_has_config_item(
2032 struct scarlett2_data *private, int config_item_num)
2034 return !!private->config_set->items[config_item_num].offset;
2037 /* Send a USB message to get configuration parameters; result placed in *buf */
2038 static int scarlett2_usb_get_config(
2039 struct usb_mixer_interface *mixer,
2040 int config_item_num, int count, void *buf)
2042 struct scarlett2_data *private = mixer->private_data;
2043 const struct scarlett2_config *config_item =
2044 &private->config_set->items[config_item_num];
2049 /* Check that the configuration item is present in the
2050 * configuration set used by this device
2052 if (!config_item->offset)
2055 /* Gen 4 style parameters are always 1 byte */
2056 size = config_item->size ? config_item->size : 8;
2058 /* For byte-sized parameters, retrieve directly into buf */
2060 size = size / 8 * count;
2061 err = scarlett2_usb_get(mixer, config_item->offset, buf, size);
2067 for (i = 0; i < count; i++, buf_16++)
2068 *buf_16 = le16_to_cpu(*(__le16 *)buf_16);
2073 /* For bit-sized parameters, retrieve into value */
2074 err = scarlett2_usb_get(mixer, config_item->offset, &value, 1);
2078 /* then unpack from value into buf[] */
2080 for (i = 0; i < 8 && i < count; i++, value >>= 1)
2081 *buf_8++ = value & 1;
2086 /* Send a SCARLETT2_USB_SET_DATA command.
2087 * offset: location in the device's data space
2088 * size: size in bytes of the value (1, 2, 4)
2090 static int scarlett2_usb_set_data(
2091 struct usb_mixer_interface *mixer,
2092 int offset, int size, int value)
2094 struct scarlett2_data *private = mixer->private_data;
2101 req.offset = cpu_to_le32(offset);
2102 req.size = cpu_to_le32(size);
2103 req.value = cpu_to_le32(value);
2104 return scarlett2_usb(private->mixer, SCARLETT2_USB_SET_DATA,
2105 &req, sizeof(u32) * 2 + size, NULL, 0);
2108 /* Send a SCARLETT2_USB_DATA_CMD command.
2109 * Configuration changes require activation with this after they have
2110 * been uploaded by a previous SCARLETT2_USB_SET_DATA.
2111 * The value for activate needed is determined by the configuration
2114 static int scarlett2_usb_activate_config(
2115 struct usb_mixer_interface *mixer, int activate)
2119 req = cpu_to_le32(activate);
2120 return scarlett2_usb(mixer, SCARLETT2_USB_DATA_CMD,
2121 &req, sizeof(req), NULL, 0);
2124 /* Send USB messages to set a SCARLETT2_CONFIG_* parameter */
2125 static int scarlett2_usb_set_config(
2126 struct usb_mixer_interface *mixer,
2127 int config_item_num, int index, int value)
2129 struct scarlett2_data *private = mixer->private_data;
2130 const struct scarlett2_config_set *config_set = private->config_set;
2131 const struct scarlett2_config *config_item =
2132 &config_set->items[config_item_num];
2136 /* Check that the configuration item is present in the
2137 * configuration set used by this device
2139 if (!config_item->offset)
2142 /* Gen 4 style writes are selected with size = 0;
2143 * these are only byte-sized values written through a shared
2144 * location, different to the read address
2146 if (!config_item->size) {
2147 if (!config_set->gen4_write_addr)
2150 /* Place index in gen4_write_addr + 1 */
2151 err = scarlett2_usb_set_data(
2152 mixer, config_set->gen4_write_addr + 1, 1, index);
2156 /* Place value in gen4_write_addr */
2157 err = scarlett2_usb_set_data(
2158 mixer, config_set->gen4_write_addr, 1, value);
2162 /* Request the interface do the write */
2163 return scarlett2_usb_activate_config(
2164 mixer, config_item->activate);
2167 /* Not-Gen 4 style needs NVRAM save, supports
2168 * bit-modification, and writing is done to the same place
2169 * that the value can be read from
2172 /* Cancel any pending NVRAM save */
2173 cancel_delayed_work_sync(&private->work);
2175 /* Convert config_item->size in bits to size in bytes and
2178 if (config_item->size >= 8) {
2179 size = config_item->size / 8;
2180 offset = config_item->offset + index * size;
2182 /* If updating a bit, retrieve the old value, set/clear the
2183 * bit as needed, and update value
2189 offset = config_item->offset;
2191 err = scarlett2_usb_get(mixer, offset, &tmp, 1);
2196 tmp |= (1 << index);
2198 tmp &= ~(1 << index);
2203 /* Send the configuration parameter data */
2204 err = scarlett2_usb_set_data(mixer, offset, size, value);
2208 /* Activate the change */
2209 err = scarlett2_usb_activate_config(mixer, config_item->activate);
2213 /* Gen 2 style writes to Gen 4 devices don't need saving */
2214 if (config_set->gen4_write_addr)
2217 /* Schedule the change to be written to NVRAM */
2218 if (config_item->activate != SCARLETT2_USB_CONFIG_SAVE)
2219 schedule_delayed_work(&private->work, msecs_to_jiffies(2000));
2224 /* Send SCARLETT2_USB_DATA_CMD SCARLETT2_USB_CONFIG_SAVE */
2225 static void scarlett2_config_save(struct usb_mixer_interface *mixer)
2229 err = scarlett2_usb_activate_config(mixer, SCARLETT2_USB_CONFIG_SAVE);
2231 usb_audio_err(mixer->chip, "config save failed: %d\n", err);
2234 /* Delayed work to save config */
2235 static void scarlett2_config_save_work(struct work_struct *work)
2237 struct scarlett2_data *private =
2238 container_of(work, struct scarlett2_data, work.work);
2240 scarlett2_config_save(private->mixer);
2243 /* Send a USB message to get sync status; result placed in *sync */
2244 static int scarlett2_usb_get_sync_status(
2245 struct usb_mixer_interface *mixer,
2251 err = scarlett2_usb(mixer, SCARLETT2_USB_GET_SYNC,
2252 NULL, 0, &data, sizeof(data));
2260 /* Return true if the device has a mixer that we can control */
2261 static int scarlett2_has_mixer(struct scarlett2_data *private)
2263 return !!private->info->mux_assignment[0][0].count;
2266 /* Map from mixer value to (db + 80) * 2
2267 * (reverse of scarlett2_mixer_values[])
2269 static int scarlett2_mixer_value_to_db(int value)
2273 for (i = 0; i < SCARLETT2_MIXER_VALUE_COUNT; i++)
2274 if (scarlett2_mixer_values[i] >= value)
2276 return SCARLETT2_MIXER_MAX_VALUE;
2279 /* Send a USB message to get the volumes for all inputs of one mix
2280 * and put the values into private->mix[]
2282 static int scarlett2_usb_get_mix(struct usb_mixer_interface *mixer,
2285 struct scarlett2_data *private = mixer->private_data;
2287 int num_mixer_in = private->num_mix_in;
2295 __le16 data[SCARLETT2_INPUT_MIX_MAX];
2297 req.mix_num = cpu_to_le16(mix_num);
2298 req.count = cpu_to_le16(num_mixer_in);
2300 err = scarlett2_usb(mixer, SCARLETT2_USB_GET_MIX,
2302 data, num_mixer_in * sizeof(u16));
2306 for (i = 0, j = mix_num * num_mixer_in; i < num_mixer_in; i++, j++)
2307 private->mix[j] = scarlett2_mixer_value_to_db(
2308 le16_to_cpu(data[i]));
2313 /* Send a USB message to set the volumes for all inputs of one mix
2314 * (values obtained from private->mix[])
2316 static int scarlett2_usb_set_mix(struct usb_mixer_interface *mixer,
2319 struct scarlett2_data *private = mixer->private_data;
2323 __le16 data[SCARLETT2_INPUT_MIX_MAX];
2327 int num_mixer_in = private->num_mix_in;
2329 req.mix_num = cpu_to_le16(mix_num);
2331 for (i = 0, j = mix_num * num_mixer_in; i < num_mixer_in; i++, j++)
2332 req.data[i] = cpu_to_le16(
2333 scarlett2_mixer_values[private->mix[j]]
2336 return scarlett2_usb(mixer, SCARLETT2_USB_SET_MIX,
2337 &req, (num_mixer_in + 1) * sizeof(u16),
2341 /* Convert a port number index (per info->port_count) to a hardware ID */
2342 static u32 scarlett2_mux_src_num_to_id(
2343 const int port_count[][SCARLETT2_PORT_DIRNS], int num)
2348 port_type < SCARLETT2_PORT_TYPE_COUNT;
2350 if (num < port_count[port_type][SCARLETT2_PORT_IN])
2351 return scarlett2_ports[port_type].id | num;
2352 num -= port_count[port_type][SCARLETT2_PORT_IN];
2359 /* Convert a hardware ID to a port number index */
2360 static u32 scarlett2_mux_id_to_num(
2361 const int port_count[][SCARLETT2_PORT_DIRNS], int direction, u32 id)
2367 port_type < SCARLETT2_PORT_TYPE_COUNT;
2369 int base = scarlett2_ports[port_type].id;
2370 int count = port_count[port_type][direction];
2372 if (id >= base && id < base + count)
2373 return port_num + id - base;
2381 /* Convert one mux entry from the interface and load into private->mux[] */
2382 static void scarlett2_usb_populate_mux(struct scarlett2_data *private,
2385 const struct scarlett2_device_info *info = private->info;
2386 const int (*port_count)[SCARLETT2_PORT_DIRNS] = info->port_count;
2388 int dst_idx, src_idx;
2390 dst_idx = scarlett2_mux_id_to_num(port_count, SCARLETT2_PORT_OUT,
2395 if (dst_idx >= private->num_mux_dsts) {
2396 usb_audio_err(private->mixer->chip,
2397 "BUG: scarlett2_mux_id_to_num(%06x, OUT): %d >= %d",
2398 mux_entry, dst_idx, private->num_mux_dsts);
2402 src_idx = scarlett2_mux_id_to_num(port_count, SCARLETT2_PORT_IN,
2407 if (src_idx >= private->num_mux_srcs) {
2408 usb_audio_err(private->mixer->chip,
2409 "BUG: scarlett2_mux_id_to_num(%06x, IN): %d >= %d",
2410 mux_entry, src_idx, private->num_mux_srcs);
2414 private->mux[dst_idx] = src_idx;
2417 /* Update the meter level map
2419 * The meter level data from the interface (SCARLETT2_USB_GET_METER
2420 * request) is returned in mux_assignment order, but to avoid exposing
2421 * that to userspace, scarlett2_meter_ctl_get() rearranges the data
2422 * into scarlett2_ports order using the meter_level_map[] array which
2423 * is set up by this function.
2425 * In addition, the meter level data values returned from the
2426 * interface are invalid for destinations where:
2428 * - the source is "Off"; therefore we set those values to zero (map
2431 * - the source is assigned to a previous (with respect to the
2432 * mux_assignment order) destination; therefore we set those values
2433 * to the value previously reported for that source
2435 static void scarlett2_update_meter_level_map(struct scarlett2_data *private)
2437 const struct scarlett2_device_info *info = private->info;
2438 const struct scarlett2_meter_entry *entry;
2440 /* sources already assigned to a destination
2441 * value is 255 for None, otherwise the value of i
2442 * (index into array returned by
2443 * scarlett2_usb_get_meter_levels())
2445 u8 seen_src[SCARLETT2_MAX_SRCS] = { 1 };
2446 u8 seen_src_value[SCARLETT2_MAX_SRCS] = { 255 };
2448 /* index in meter_map[] order */
2451 /* go through the meter_map[] entries */
2452 for (entry = info->meter_map;
2456 /* fill in each meter_level_map[] entry */
2459 for (j = 0, mux_idx = entry->start;
2461 i++, j++, mux_idx++) {
2463 /* convert mux_idx using line_out_unmap[] */
2465 info->line_out_remap_enable &&
2466 mux_idx < private->num_line_out
2467 ) ? info->line_out_unmap[mux_idx]
2470 /* check which source is connected, and if
2471 * that source is already connected elsewhere,
2472 * use that existing connection's destination
2473 * for this meter entry instead
2475 int mux_src = private->mux[mux_idx];
2477 if (!seen_src[mux_src]) {
2478 seen_src[mux_src] = 1;
2479 seen_src_value[mux_src] = i;
2481 private->meter_level_map[map_mux_idx] =
2482 seen_src_value[mux_src];
2487 /* Send USB message to get mux inputs and then populate private->mux[] */
2488 static int scarlett2_usb_get_mux(struct usb_mixer_interface *mixer)
2490 struct scarlett2_data *private = mixer->private_data;
2491 int count = private->num_mux_dsts;
2499 __le32 data[SCARLETT2_MUX_MAX];
2501 private->mux_updated = 0;
2504 req.count = cpu_to_le16(count);
2506 err = scarlett2_usb(mixer, SCARLETT2_USB_GET_MUX,
2508 data, count * sizeof(u32));
2512 for (i = 0; i < count; i++)
2513 scarlett2_usb_populate_mux(private, le32_to_cpu(data[i]));
2515 scarlett2_update_meter_level_map(private);
2520 /* Send USB messages to set mux inputs */
2521 static int scarlett2_usb_set_mux(struct usb_mixer_interface *mixer)
2523 struct scarlett2_data *private = mixer->private_data;
2524 const struct scarlett2_device_info *info = private->info;
2525 const int (*port_count)[SCARLETT2_PORT_DIRNS] = info->port_count;
2531 __le32 data[SCARLETT2_MUX_MAX];
2536 /* set mux settings for each rate */
2537 for (table = 0; table < SCARLETT2_MUX_TABLES; table++) {
2538 const struct scarlett2_mux_entry *entry;
2540 /* i counts over the output array */
2543 req.num = cpu_to_le16(table);
2545 /* loop through each entry */
2546 for (entry = info->mux_assignment[table];
2550 int port_type = entry->port_type;
2551 int port_idx = entry->start;
2552 int mux_idx = scarlett2_get_port_start_num(port_count,
2553 SCARLETT2_PORT_OUT, port_type) + port_idx;
2554 int dst_id = scarlett2_ports[port_type].id + port_idx;
2558 for (j = 0; j < entry->count; j++)
2563 /* Non-empty mux slots use the lower 12 bits
2564 * for the destination and next 12 bits for
2567 for (j = 0; j < entry->count; j++) {
2568 int src_id = scarlett2_mux_src_num_to_id(
2569 port_count, private->mux[mux_idx++]);
2570 req.data[i++] = cpu_to_le32(dst_id |
2576 err = scarlett2_usb(mixer, SCARLETT2_USB_SET_MUX,
2577 &req, (i + 1) * sizeof(u32),
2583 scarlett2_update_meter_level_map(private);
2588 /* Send USB message to get meter levels */
2589 static int scarlett2_usb_get_meter_levels(struct usb_mixer_interface *mixer,
2590 u16 num_meters, u16 *levels)
2597 __le32 resp[SCARLETT2_MAX_METERS];
2601 req.num_meters = cpu_to_le16(num_meters);
2602 req.magic = cpu_to_le32(SCARLETT2_USB_METER_LEVELS_GET_MAGIC);
2603 err = scarlett2_usb(mixer, SCARLETT2_USB_GET_METER,
2604 &req, sizeof(req), resp, num_meters * sizeof(u32));
2608 /* copy, convert to u16 */
2609 for (i = 0; i < num_meters; i++)
2610 levels[i] = le32_to_cpu(resp[i]);
2615 /* For config items with mute=1, xor bits 0 & 1 together to get the
2616 * current/next state. This won't have any effect on values which are
2619 static uint8_t scarlett2_decode_muteable(uint8_t v)
2621 return (v ^ (v >> 1)) & 1;
2624 /*** Control Functions ***/
2626 /* helper function to create a new control */
2627 static int scarlett2_add_new_ctl(struct usb_mixer_interface *mixer,
2628 const struct snd_kcontrol_new *ncontrol,
2629 int index, int channels, const char *name,
2630 struct snd_kcontrol **kctl_return)
2632 struct snd_kcontrol *kctl;
2633 struct usb_mixer_elem_info *elem;
2636 elem = kzalloc(sizeof(*elem), GFP_KERNEL);
2640 /* We set USB_MIXER_BESPOKEN type, so that the core USB mixer code
2641 * ignores them for resume and other operations.
2642 * Also, the head.id field is set to 0, as we don't use this field.
2644 elem->head.mixer = mixer;
2645 elem->control = index;
2647 elem->channels = channels;
2648 elem->val_type = USB_MIXER_BESPOKEN;
2650 kctl = snd_ctl_new1(ncontrol, elem);
2655 kctl->private_free = snd_usb_mixer_elem_free;
2657 strscpy(kctl->id.name, name, sizeof(kctl->id.name));
2659 err = snd_usb_mixer_add_control(&elem->head, kctl);
2664 *kctl_return = kctl;
2669 /*** Firmware Version Control ***/
2671 static int scarlett2_firmware_version_ctl_get(
2672 struct snd_kcontrol *kctl,
2673 struct snd_ctl_elem_value *ucontrol)
2675 struct usb_mixer_elem_info *elem = kctl->private_data;
2676 struct scarlett2_data *private = elem->head.mixer->private_data;
2678 ucontrol->value.integer.value[0] = private->firmware_version;
2683 static int scarlett2_firmware_version_ctl_info(
2684 struct snd_kcontrol *kctl,
2685 struct snd_ctl_elem_info *uinfo)
2687 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2693 static const struct snd_kcontrol_new scarlett2_firmware_version_ctl = {
2694 .iface = SNDRV_CTL_ELEM_IFACE_CARD,
2695 .access = SNDRV_CTL_ELEM_ACCESS_READ,
2697 .info = scarlett2_firmware_version_ctl_info,
2698 .get = scarlett2_firmware_version_ctl_get
2701 static int scarlett2_add_firmware_version_ctl(
2702 struct usb_mixer_interface *mixer)
2704 return scarlett2_add_new_ctl(mixer, &scarlett2_firmware_version_ctl,
2705 0, 0, "Firmware Version", NULL);
2708 /*** Minimum Firmware Version Control ***/
2710 static int scarlett2_min_firmware_version_ctl_get(
2711 struct snd_kcontrol *kctl,
2712 struct snd_ctl_elem_value *ucontrol)
2714 struct usb_mixer_elem_info *elem = kctl->private_data;
2715 struct scarlett2_data *private = elem->head.mixer->private_data;
2717 ucontrol->value.integer.value[0] = private->info->min_firmware_version;
2722 static int scarlett2_min_firmware_version_ctl_info(
2723 struct snd_kcontrol *kctl,
2724 struct snd_ctl_elem_info *uinfo)
2726 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2732 static const struct snd_kcontrol_new scarlett2_min_firmware_version_ctl = {
2733 .iface = SNDRV_CTL_ELEM_IFACE_CARD,
2734 .access = SNDRV_CTL_ELEM_ACCESS_READ,
2736 .info = scarlett2_min_firmware_version_ctl_info,
2737 .get = scarlett2_min_firmware_version_ctl_get
2740 static int scarlett2_add_min_firmware_version_ctl(
2741 struct usb_mixer_interface *mixer)
2743 return scarlett2_add_new_ctl(mixer, &scarlett2_min_firmware_version_ctl,
2744 0, 0, "Minimum Firmware Version", NULL);
2747 /*** Sync Control ***/
2749 /* Update sync control after receiving notification that the status
2752 static int scarlett2_update_sync(struct usb_mixer_interface *mixer)
2754 struct scarlett2_data *private = mixer->private_data;
2756 private->sync_updated = 0;
2757 return scarlett2_usb_get_sync_status(mixer, &private->sync);
2760 static int scarlett2_sync_ctl_info(struct snd_kcontrol *kctl,
2761 struct snd_ctl_elem_info *uinfo)
2763 static const char *texts[2] = {
2764 "Unlocked", "Locked"
2766 return snd_ctl_enum_info(uinfo, 1, 2, texts);
2769 static int scarlett2_sync_ctl_get(struct snd_kcontrol *kctl,
2770 struct snd_ctl_elem_value *ucontrol)
2772 struct usb_mixer_elem_info *elem = kctl->private_data;
2773 struct usb_mixer_interface *mixer = elem->head.mixer;
2774 struct scarlett2_data *private = mixer->private_data;
2777 mutex_lock(&private->data_mutex);
2779 if (private->hwdep_in_use) {
2784 if (private->sync_updated) {
2785 err = scarlett2_update_sync(mixer);
2789 ucontrol->value.enumerated.item[0] = private->sync;
2792 mutex_unlock(&private->data_mutex);
2796 static const struct snd_kcontrol_new scarlett2_sync_ctl = {
2797 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2798 .access = SNDRV_CTL_ELEM_ACCESS_READ,
2800 .info = scarlett2_sync_ctl_info,
2801 .get = scarlett2_sync_ctl_get
2804 static int scarlett2_add_sync_ctl(struct usb_mixer_interface *mixer)
2806 struct scarlett2_data *private = mixer->private_data;
2808 /* devices without a mixer also don't support reporting sync status */
2809 if (!scarlett2_has_mixer(private))
2812 return scarlett2_add_new_ctl(mixer, &scarlett2_sync_ctl,
2813 0, 1, "Sync Status", &private->sync_ctl);
2816 /*** Autogain Switch and Status Controls ***/
2818 /* Forward declarations as phantom power and autogain can disable each other */
2819 static int scarlett2_check_input_phantom_updated(struct usb_mixer_interface *);
2820 static int scarlett2_phantom_is_switching(struct scarlett2_data *, int);
2822 /* Set the access mode of a control to read-only (val = 0) or
2823 * read-write (val = 1).
2825 static void scarlett2_set_ctl_access(struct snd_kcontrol *kctl, int val)
2828 kctl->vd[0].access |= SNDRV_CTL_ELEM_ACCESS_WRITE;
2830 kctl->vd[0].access &= ~SNDRV_CTL_ELEM_ACCESS_WRITE;
2833 /* Check if autogain is running on any input */
2834 static int scarlett2_autogain_is_running(struct scarlett2_data *private)
2838 for (i = 0; i < private->info->gain_input_count; i++)
2839 if (private->autogain_status[i] ==
2840 SCARLETT2_AUTOGAIN_STATUS_RUNNING)
2846 static int scarlett2_update_autogain(struct usb_mixer_interface *mixer)
2848 struct scarlett2_data *private = mixer->private_data;
2849 const struct scarlett2_device_info *info = private->info;
2851 u8 raw_autogain_status[SCARLETT2_INPUT_GAIN_MAX];
2853 private->autogain_updated = 0;
2855 if (!info->gain_input_count)
2858 err = scarlett2_usb_get_config(
2859 mixer, SCARLETT2_CONFIG_AUTOGAIN_SWITCH,
2860 info->gain_input_count, private->autogain_switch);
2863 err = scarlett2_usb_get_config(
2864 mixer, SCARLETT2_CONFIG_AUTOGAIN_STATUS,
2865 info->gain_input_count, raw_autogain_status);
2869 /* Translate autogain_switch and raw_autogain_status into
2872 for (i = 0; i < info->gain_input_count; i++)
2873 if (private->autogain_switch[i])
2874 private->autogain_status[i] =
2875 SCARLETT2_AUTOGAIN_STATUS_RUNNING;
2876 else if (raw_autogain_status[i] == 0)
2877 private->autogain_status[i] =
2878 SCARLETT2_AUTOGAIN_STATUS_STOPPED;
2879 else if (raw_autogain_status[i] >= 2 &&
2880 raw_autogain_status[i] <= 5)
2881 private->autogain_status[i] =
2882 SCARLETT2_AUTOGAIN_STATUS_FAILED;
2883 else if (raw_autogain_status[i] == 6)
2884 private->autogain_status[i] =
2885 SCARLETT2_AUTOGAIN_STATUS_CANCELLED;
2887 private->autogain_status[i] =
2888 SCARLETT2_AUTOGAIN_STATUS_UNKNOWN;
2893 /* Update access mode for controls affected by autogain */
2894 static void scarlett2_autogain_update_access(struct usb_mixer_interface *mixer)
2896 struct scarlett2_data *private = mixer->private_data;
2897 const struct scarlett2_device_info *info = private->info;
2898 int val = !scarlett2_autogain_is_running(private);
2901 scarlett2_set_ctl_access(private->input_select_ctl, val);
2902 for (i = 0; i < info->gain_input_count / 2; i++)
2903 scarlett2_set_ctl_access(private->input_link_ctls[i], val);
2904 for (i = 0; i < info->gain_input_count; i++) {
2905 scarlett2_set_ctl_access(private->input_gain_ctls[i], val);
2906 scarlett2_set_ctl_access(private->safe_ctls[i], val);
2908 for (i = 0; i < info->level_input_count; i++)
2909 scarlett2_set_ctl_access(private->level_ctls[i], val);
2910 for (i = 0; i < info->air_input_count; i++)
2911 scarlett2_set_ctl_access(private->air_ctls[i], val);
2912 for (i = 0; i < info->phantom_count; i++)
2913 scarlett2_set_ctl_access(private->phantom_ctls[i], val);
2916 /* Notify of access mode change for all controls read-only while
2919 static void scarlett2_autogain_notify_access(struct usb_mixer_interface *mixer)
2921 struct snd_card *card = mixer->chip->card;
2922 struct scarlett2_data *private = mixer->private_data;
2923 const struct scarlett2_device_info *info = private->info;
2926 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_INFO,
2927 &private->input_select_ctl->id);
2928 for (i = 0; i < info->gain_input_count / 2; i++)
2929 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_INFO,
2930 &private->input_link_ctls[i]->id);
2931 for (i = 0; i < info->gain_input_count; i++) {
2932 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_INFO,
2933 &private->input_gain_ctls[i]->id);
2934 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_INFO,
2935 &private->safe_ctls[i]->id);
2937 for (i = 0; i < info->level_input_count; i++)
2938 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_INFO,
2939 &private->level_ctls[i]->id);
2940 for (i = 0; i < info->air_input_count; i++)
2941 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_INFO,
2942 &private->air_ctls[i]->id);
2943 for (i = 0; i < info->phantom_count; i++)
2944 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_INFO,
2945 &private->phantom_ctls[i]->id);
2948 /* Call scarlett2_update_autogain() and
2949 * scarlett2_autogain_update_access() if autogain_updated is set.
2951 static int scarlett2_check_autogain_updated(
2952 struct usb_mixer_interface *mixer)
2954 struct scarlett2_data *private = mixer->private_data;
2957 if (!private->autogain_updated)
2960 err = scarlett2_update_autogain(mixer);
2964 scarlett2_autogain_update_access(mixer);
2969 /* If autogain_updated is set when a *_ctl_put() function for a
2970 * control that is meant to be read-only while autogain is running,
2971 * update the autogain status and access mode of affected controls.
2972 * Return -EPERM if autogain is running.
2974 static int scarlett2_check_put_during_autogain(
2975 struct usb_mixer_interface *mixer)
2977 int err = scarlett2_check_autogain_updated(mixer);
2982 if (scarlett2_autogain_is_running(mixer->private_data))
2988 static int scarlett2_autogain_switch_ctl_info(
2989 struct snd_kcontrol *kctl, struct snd_ctl_elem_info *uinfo)
2991 struct usb_mixer_elem_info *elem = kctl->private_data;
2992 struct usb_mixer_interface *mixer = elem->head.mixer;
2993 struct scarlett2_data *private = mixer->private_data;
2996 mutex_lock(&private->data_mutex);
2998 err = scarlett2_check_input_phantom_updated(mixer);
3002 err = snd_ctl_boolean_mono_info(kctl, uinfo);
3005 mutex_unlock(&private->data_mutex);
3009 static int scarlett2_autogain_switch_ctl_get(
3010 struct snd_kcontrol *kctl, struct snd_ctl_elem_value *ucontrol)
3012 struct usb_mixer_elem_info *elem = kctl->private_data;
3013 struct usb_mixer_interface *mixer = elem->head.mixer;
3014 struct scarlett2_data *private = mixer->private_data;
3017 mutex_lock(&private->data_mutex);
3019 if (private->hwdep_in_use) {
3024 err = scarlett2_check_autogain_updated(mixer);
3028 ucontrol->value.enumerated.item[0] =
3029 private->autogain_switch[elem->control];
3032 mutex_unlock(&private->data_mutex);
3036 static int scarlett2_autogain_status_ctl_get(
3037 struct snd_kcontrol *kctl, struct snd_ctl_elem_value *ucontrol)
3039 struct usb_mixer_elem_info *elem = kctl->private_data;
3040 struct usb_mixer_interface *mixer = elem->head.mixer;
3041 struct scarlett2_data *private = mixer->private_data;
3044 mutex_lock(&private->data_mutex);
3046 if (private->hwdep_in_use) {
3051 err = scarlett2_check_autogain_updated(mixer);
3055 ucontrol->value.enumerated.item[0] =
3056 private->autogain_status[elem->control];
3059 mutex_unlock(&private->data_mutex);
3063 static int scarlett2_autogain_switch_ctl_put(
3064 struct snd_kcontrol *kctl, struct snd_ctl_elem_value *ucontrol)
3066 struct usb_mixer_elem_info *elem = kctl->private_data;
3067 struct usb_mixer_interface *mixer = elem->head.mixer;
3068 struct scarlett2_data *private = mixer->private_data;
3070 int index = elem->control;
3073 mutex_lock(&private->data_mutex);
3075 if (private->hwdep_in_use) {
3080 err = scarlett2_check_input_phantom_updated(mixer);
3084 if (scarlett2_phantom_is_switching(private, index)) {
3089 oval = private->autogain_switch[index];
3090 val = !!ucontrol->value.integer.value[0];
3095 private->autogain_switch[index] = val;
3097 /* Send switch change to the device */
3098 err = scarlett2_usb_set_config(
3099 mixer, SCARLETT2_CONFIG_AUTOGAIN_SWITCH, index, val);
3103 scarlett2_autogain_update_access(mixer);
3104 scarlett2_autogain_notify_access(mixer);
3107 mutex_unlock(&private->data_mutex);
3111 static int scarlett2_autogain_status_ctl_info(
3112 struct snd_kcontrol *kctl, struct snd_ctl_elem_info *uinfo)
3114 static const char *const values[SCARLETT2_AUTOGAIN_STATUS_COUNT] = {
3115 "Stopped", "Running", "Failed", "Cancelled", "Unknown"
3118 return snd_ctl_enum_info(
3119 uinfo, 1, SCARLETT2_AUTOGAIN_STATUS_COUNT, values);
3122 static const struct snd_kcontrol_new scarlett2_autogain_switch_ctl = {
3123 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3125 .info = scarlett2_autogain_switch_ctl_info,
3126 .get = scarlett2_autogain_switch_ctl_get,
3127 .put = scarlett2_autogain_switch_ctl_put
3130 static const struct snd_kcontrol_new scarlett2_autogain_status_ctl = {
3131 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3132 .access = SNDRV_CTL_ELEM_ACCESS_READ,
3134 .info = scarlett2_autogain_status_ctl_info,
3135 .get = scarlett2_autogain_status_ctl_get,
3138 /*** Input Select Control ***/
3140 static int scarlett2_update_input_select(struct usb_mixer_interface *mixer)
3142 struct scarlett2_data *private = mixer->private_data;
3143 const struct scarlett2_device_info *info = private->info;
3144 int link_count = info->gain_input_count / 2;
3147 private->input_select_updated = 0;
3152 err = scarlett2_usb_get_config(
3153 mixer, SCARLETT2_CONFIG_INPUT_SELECT_SWITCH,
3154 1, &private->input_select_switch);
3158 err = scarlett2_usb_get_config(
3159 mixer, SCARLETT2_CONFIG_INPUT_LINK_SWITCH,
3160 link_count, private->input_link_switch);
3164 /* simplified because no model yet has link_count > 1 */
3165 if (private->input_link_switch[0])
3166 private->input_select_switch = 0;
3171 static int scarlett2_input_select_ctl_get(
3172 struct snd_kcontrol *kctl, struct snd_ctl_elem_value *ucontrol)
3174 struct usb_mixer_elem_info *elem = kctl->private_data;
3175 struct usb_mixer_interface *mixer = elem->head.mixer;
3176 struct scarlett2_data *private = mixer->private_data;
3179 mutex_lock(&private->data_mutex);
3181 if (private->hwdep_in_use) {
3186 if (private->input_select_updated) {
3187 err = scarlett2_update_input_select(mixer);
3191 ucontrol->value.enumerated.item[0] = private->input_select_switch;
3194 mutex_unlock(&private->data_mutex);
3198 static int scarlett2_input_select_ctl_put(
3199 struct snd_kcontrol *kctl, struct snd_ctl_elem_value *ucontrol)
3201 struct usb_mixer_elem_info *elem = kctl->private_data;
3202 struct usb_mixer_interface *mixer = elem->head.mixer;
3203 struct scarlett2_data *private = mixer->private_data;
3206 int max_val = private->input_link_switch[0] ? 0 : 1;
3208 mutex_lock(&private->data_mutex);
3210 if (private->hwdep_in_use) {
3215 err = scarlett2_check_put_during_autogain(mixer);
3219 oval = private->input_select_switch;
3220 val = ucontrol->value.integer.value[0];
3224 else if (val > max_val)
3230 private->input_select_switch = val;
3232 /* Send switch change to the device if inputs not linked */
3233 if (!private->input_link_switch[0])
3234 err = scarlett2_usb_set_config(
3235 mixer, SCARLETT2_CONFIG_INPUT_SELECT_SWITCH,
3241 mutex_unlock(&private->data_mutex);
3245 static int scarlett2_input_select_ctl_info(
3246 struct snd_kcontrol *kctl, struct snd_ctl_elem_info *uinfo)
3248 struct usb_mixer_elem_info *elem = kctl->private_data;
3249 struct usb_mixer_interface *mixer = elem->head.mixer;
3250 struct scarlett2_data *private = mixer->private_data;
3252 int inputs = private->info->gain_input_count;
3255 char **values = kcalloc(inputs, sizeof(char *), GFP_KERNEL);
3260 mutex_lock(&private->data_mutex);
3262 if (private->hwdep_in_use) {
3267 err = scarlett2_check_autogain_updated(mixer);
3271 /* Loop through each input
3272 * Linked inputs have one value for the pair
3274 for (i = 0, j = 0; i < inputs; i++) {
3275 if (private->input_link_switch[i / 2]) {
3276 values[j++] = kasprintf(
3277 GFP_KERNEL, "Input %d-%d", i + 1, i + 2);
3280 values[j++] = kasprintf(
3281 GFP_KERNEL, "Input %d", i + 1);
3285 err = snd_ctl_enum_info(uinfo, 1, j,
3286 (const char * const *)values);
3289 mutex_unlock(&private->data_mutex);
3291 for (i = 0; i < inputs; i++)
3298 static const struct snd_kcontrol_new scarlett2_input_select_ctl = {
3299 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3301 .info = scarlett2_input_select_ctl_info,
3302 .get = scarlett2_input_select_ctl_get,
3303 .put = scarlett2_input_select_ctl_put,
3306 /*** Input Link Switch Controls ***/
3308 /* snd_ctl_boolean_mono_info() with autogain-updated check
3309 * (for controls that are read-only while autogain is running)
3311 static int scarlett2_autogain_disables_ctl_info(struct snd_kcontrol *kctl,
3312 struct snd_ctl_elem_info *uinfo)
3314 struct usb_mixer_elem_info *elem = kctl->private_data;
3315 struct usb_mixer_interface *mixer = elem->head.mixer;
3316 struct scarlett2_data *private = mixer->private_data;
3319 mutex_lock(&private->data_mutex);
3321 if (private->hwdep_in_use) {
3326 err = scarlett2_check_autogain_updated(mixer);
3330 err = snd_ctl_boolean_mono_info(kctl, uinfo);
3333 mutex_unlock(&private->data_mutex);
3337 static int scarlett2_input_link_ctl_get(
3338 struct snd_kcontrol *kctl, struct snd_ctl_elem_value *ucontrol)
3340 struct usb_mixer_elem_info *elem = kctl->private_data;
3341 struct usb_mixer_interface *mixer = elem->head.mixer;
3342 struct scarlett2_data *private = mixer->private_data;
3345 mutex_lock(&private->data_mutex);
3347 if (private->hwdep_in_use) {
3352 if (private->input_select_updated) {
3353 err = scarlett2_update_input_select(mixer);
3357 ucontrol->value.enumerated.item[0] =
3358 private->input_link_switch[elem->control];
3361 mutex_unlock(&private->data_mutex);
3365 static int scarlett2_input_link_ctl_put(
3366 struct snd_kcontrol *kctl, struct snd_ctl_elem_value *ucontrol)
3368 struct usb_mixer_elem_info *elem = kctl->private_data;
3369 struct usb_mixer_interface *mixer = elem->head.mixer;
3370 struct scarlett2_data *private = mixer->private_data;
3372 int index = elem->control;
3375 mutex_lock(&private->data_mutex);
3377 if (private->hwdep_in_use) {
3382 err = scarlett2_check_put_during_autogain(mixer);
3386 oval = private->input_link_switch[index];
3387 val = !!ucontrol->value.integer.value[0];
3392 private->input_link_switch[index] = val;
3394 /* Notify of change in input select options available */
3395 snd_ctl_notify(mixer->chip->card,
3396 SNDRV_CTL_EVENT_MASK_VALUE | SNDRV_CTL_EVENT_MASK_INFO,
3397 &private->input_select_ctl->id);
3398 private->input_select_updated = 1;
3400 /* Send switch change to the device
3401 * Link for channels 1-2 is at index 1
3402 * No device yet has more than 2 channels linked
3404 err = scarlett2_usb_set_config(
3405 mixer, SCARLETT2_CONFIG_INPUT_LINK_SWITCH, index + 1, val);
3410 mutex_unlock(&private->data_mutex);
3414 static const struct snd_kcontrol_new scarlett2_input_link_ctl = {
3415 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3417 .info = scarlett2_autogain_disables_ctl_info,
3418 .get = scarlett2_input_link_ctl_get,
3419 .put = scarlett2_input_link_ctl_put
3422 /*** Input Gain Controls ***/
3424 static int scarlett2_update_input_gain(struct usb_mixer_interface *mixer)
3426 struct scarlett2_data *private = mixer->private_data;
3427 const struct scarlett2_device_info *info = private->info;
3429 private->input_gain_updated = 0;
3431 if (!info->gain_input_count)
3434 return scarlett2_usb_get_config(
3435 mixer, SCARLETT2_CONFIG_INPUT_GAIN,
3436 info->gain_input_count, private->gain);
3439 static int scarlett2_input_gain_ctl_info(struct snd_kcontrol *kctl,
3440 struct snd_ctl_elem_info *uinfo)
3442 struct usb_mixer_elem_info *elem = kctl->private_data;
3443 struct usb_mixer_interface *mixer = elem->head.mixer;
3444 struct scarlett2_data *private = mixer->private_data;
3447 mutex_lock(&private->data_mutex);
3449 if (private->hwdep_in_use) {
3454 err = scarlett2_check_autogain_updated(mixer);
3458 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
3459 uinfo->count = elem->channels;
3460 uinfo->value.integer.min = 0;
3461 uinfo->value.integer.max = SCARLETT2_GAIN_BIAS;
3462 uinfo->value.integer.step = 1;
3465 mutex_unlock(&private->data_mutex);
3469 static int scarlett2_input_gain_ctl_get(struct snd_kcontrol *kctl,
3470 struct snd_ctl_elem_value *ucontrol)
3472 struct usb_mixer_elem_info *elem = kctl->private_data;
3473 struct usb_mixer_interface *mixer = elem->head.mixer;
3474 struct scarlett2_data *private = mixer->private_data;
3477 mutex_lock(&private->data_mutex);
3479 if (private->hwdep_in_use) {
3484 if (private->input_gain_updated) {
3485 err = scarlett2_update_input_gain(mixer);
3489 ucontrol->value.integer.value[0] =
3490 private->gain[elem->control];
3493 mutex_unlock(&private->data_mutex);
3497 static int scarlett2_input_gain_ctl_put(struct snd_kcontrol *kctl,
3498 struct snd_ctl_elem_value *ucontrol)
3500 struct usb_mixer_elem_info *elem = kctl->private_data;
3501 struct usb_mixer_interface *mixer = elem->head.mixer;
3502 struct scarlett2_data *private = mixer->private_data;
3504 int index = elem->control;
3507 mutex_lock(&private->data_mutex);
3509 if (private->hwdep_in_use) {
3514 err = scarlett2_check_put_during_autogain(mixer);
3518 oval = private->gain[index];
3519 val = ucontrol->value.integer.value[0];
3524 private->gain[index] = val;
3526 /* Send gain change to the device */
3527 err = scarlett2_usb_set_config(mixer, SCARLETT2_CONFIG_INPUT_GAIN,
3533 mutex_unlock(&private->data_mutex);
3537 static const DECLARE_TLV_DB_MINMAX(
3538 db_scale_scarlett2_gain, -SCARLETT2_GAIN_BIAS * 100, 0
3541 static const struct snd_kcontrol_new scarlett2_input_gain_ctl = {
3542 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3543 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE |
3544 SNDRV_CTL_ELEM_ACCESS_TLV_READ,
3546 .info = scarlett2_input_gain_ctl_info,
3547 .get = scarlett2_input_gain_ctl_get,
3548 .put = scarlett2_input_gain_ctl_put,
3549 .private_value = 0, /* max value */
3550 .tlv = { .p = db_scale_scarlett2_gain }
3553 /*** Safe Controls ***/
3555 static int scarlett2_update_input_safe(struct usb_mixer_interface *mixer)
3557 struct scarlett2_data *private = mixer->private_data;
3558 const struct scarlett2_device_info *info = private->info;
3560 private->input_safe_updated = 0;
3562 if (!info->gain_input_count)
3565 return scarlett2_usb_get_config(
3566 mixer, SCARLETT2_CONFIG_SAFE_SWITCH,
3567 info->gain_input_count, private->safe_switch);
3570 static int scarlett2_safe_ctl_get(struct snd_kcontrol *kctl,
3571 struct snd_ctl_elem_value *ucontrol)
3573 struct usb_mixer_elem_info *elem = kctl->private_data;
3574 struct usb_mixer_interface *mixer = elem->head.mixer;
3575 struct scarlett2_data *private = mixer->private_data;
3578 mutex_lock(&private->data_mutex);
3580 if (private->hwdep_in_use) {
3585 if (private->input_safe_updated) {
3586 err = scarlett2_update_input_safe(mixer);
3590 ucontrol->value.integer.value[0] =
3591 private->safe_switch[elem->control];
3594 mutex_unlock(&private->data_mutex);
3598 static int scarlett2_safe_ctl_put(struct snd_kcontrol *kctl,
3599 struct snd_ctl_elem_value *ucontrol)
3601 struct usb_mixer_elem_info *elem = kctl->private_data;
3602 struct usb_mixer_interface *mixer = elem->head.mixer;
3603 struct scarlett2_data *private = mixer->private_data;
3605 int index = elem->control;
3608 mutex_lock(&private->data_mutex);
3610 if (private->hwdep_in_use) {
3615 err = scarlett2_check_put_during_autogain(mixer);
3619 oval = private->safe_switch[index];
3620 val = !!ucontrol->value.integer.value[0];
3625 private->safe_switch[index] = val;
3627 /* Send switch change to the device */
3628 err = scarlett2_usb_set_config(mixer, SCARLETT2_CONFIG_SAFE_SWITCH,
3634 mutex_unlock(&private->data_mutex);
3638 static const struct snd_kcontrol_new scarlett2_safe_ctl = {
3639 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3641 .info = scarlett2_autogain_disables_ctl_info,
3642 .get = scarlett2_safe_ctl_get,
3643 .put = scarlett2_safe_ctl_put,
3646 /*** PCM Input Control ***/
3648 static int scarlett2_update_pcm_input_switch(struct usb_mixer_interface *mixer)
3650 struct scarlett2_data *private = mixer->private_data;
3653 private->pcm_input_switch_updated = 0;
3655 err = scarlett2_usb_get_config(
3656 mixer, SCARLETT2_CONFIG_PCM_INPUT_SWITCH,
3657 1, &private->pcm_input_switch);
3664 static int scarlett2_pcm_input_switch_ctl_get(
3665 struct snd_kcontrol *kctl, struct snd_ctl_elem_value *ucontrol)
3667 struct usb_mixer_elem_info *elem = kctl->private_data;
3668 struct usb_mixer_interface *mixer = elem->head.mixer;
3669 struct scarlett2_data *private = elem->head.mixer->private_data;
3672 mutex_lock(&private->data_mutex);
3674 if (private->pcm_input_switch_updated) {
3675 err = scarlett2_update_pcm_input_switch(mixer);
3679 ucontrol->value.enumerated.item[0] = private->pcm_input_switch;
3682 mutex_unlock(&private->data_mutex);
3686 static int scarlett2_pcm_input_switch_ctl_put(
3687 struct snd_kcontrol *kctl, struct snd_ctl_elem_value *ucontrol)
3689 struct usb_mixer_elem_info *elem = kctl->private_data;
3690 struct usb_mixer_interface *mixer = elem->head.mixer;
3691 struct scarlett2_data *private = mixer->private_data;
3693 int oval, val, err = 0;
3695 mutex_lock(&private->data_mutex);
3697 if (private->hwdep_in_use) {
3702 oval = private->pcm_input_switch;
3703 val = !!ucontrol->value.integer.value[0];
3708 private->pcm_input_switch = val;
3710 /* Send switch change to the device */
3711 err = scarlett2_usb_set_config(
3712 mixer, SCARLETT2_CONFIG_PCM_INPUT_SWITCH,
3718 mutex_unlock(&private->data_mutex);
3722 static int scarlett2_pcm_input_switch_ctl_info(
3723 struct snd_kcontrol *kctl, struct snd_ctl_elem_info *uinfo)
3725 static const char *const values[2] = {
3729 return snd_ctl_enum_info(
3730 uinfo, 1, 2, values);
3733 static const struct snd_kcontrol_new scarlett2_pcm_input_switch_ctl = {
3734 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3736 .info = scarlett2_pcm_input_switch_ctl_info,
3737 .get = scarlett2_pcm_input_switch_ctl_get,
3738 .put = scarlett2_pcm_input_switch_ctl_put
3741 /*** Analogue Line Out Volume Controls ***/
3743 /* Update hardware volume controls after receiving notification that
3746 static int scarlett2_update_volumes(struct usb_mixer_interface *mixer)
3748 struct scarlett2_data *private = mixer->private_data;
3752 private->vol_updated = 0;
3754 if (scarlett2_has_config_item(private,
3755 SCARLETT2_CONFIG_MASTER_VOLUME)) {
3756 err = scarlett2_usb_get_config(
3757 mixer, SCARLETT2_CONFIG_MASTER_VOLUME,
3762 private->master_vol = clamp(vol + SCARLETT2_VOLUME_BIAS,
3763 0, SCARLETT2_VOLUME_BIAS);
3765 if (scarlett2_has_config_item(private,
3766 SCARLETT2_CONFIG_SW_HW_SWITCH))
3767 for (i = 0; i < private->num_line_out; i++)
3768 if (private->vol_sw_hw_switch[i])
3769 private->vol[i] = private->master_vol;
3772 if (scarlett2_has_config_item(private,
3773 SCARLETT2_CONFIG_HEADPHONE_VOLUME)) {
3774 err = scarlett2_usb_get_config(
3775 mixer, SCARLETT2_CONFIG_HEADPHONE_VOLUME,
3780 private->headphone_vol = clamp(vol + SCARLETT2_VOLUME_BIAS,
3781 0, SCARLETT2_VOLUME_BIAS);
3787 static int scarlett2_volume_ctl_info(struct snd_kcontrol *kctl,
3788 struct snd_ctl_elem_info *uinfo)
3790 struct usb_mixer_elem_info *elem = kctl->private_data;
3792 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
3793 uinfo->count = elem->channels;
3794 uinfo->value.integer.min = 0;
3795 uinfo->value.integer.max = SCARLETT2_VOLUME_BIAS;
3796 uinfo->value.integer.step = 1;
3800 static int scarlett2_master_volume_ctl_get(struct snd_kcontrol *kctl,
3801 struct snd_ctl_elem_value *ucontrol)
3803 struct usb_mixer_elem_info *elem = kctl->private_data;
3804 struct usb_mixer_interface *mixer = elem->head.mixer;
3805 struct scarlett2_data *private = mixer->private_data;
3808 mutex_lock(&private->data_mutex);
3810 if (private->hwdep_in_use) {
3815 if (private->vol_updated) {
3816 err = scarlett2_update_volumes(mixer);
3820 ucontrol->value.integer.value[0] = private->master_vol;
3823 mutex_unlock(&private->data_mutex);
3827 static int scarlett2_headphone_volume_ctl_get(
3828 struct snd_kcontrol *kctl,
3829 struct snd_ctl_elem_value *ucontrol)
3831 struct usb_mixer_elem_info *elem = kctl->private_data;
3832 struct usb_mixer_interface *mixer = elem->head.mixer;
3833 struct scarlett2_data *private = mixer->private_data;
3836 mutex_lock(&private->data_mutex);
3838 if (private->hwdep_in_use) {
3843 if (private->vol_updated) {
3844 err = scarlett2_update_volumes(mixer);
3848 ucontrol->value.integer.value[0] = private->headphone_vol;
3851 mutex_unlock(&private->data_mutex);
3855 static int line_out_remap(struct scarlett2_data *private, int index)
3857 const struct scarlett2_device_info *info = private->info;
3859 if (!info->line_out_remap_enable)
3862 if (index >= private->num_line_out)
3865 return info->line_out_remap[index];
3868 static int scarlett2_volume_ctl_get(struct snd_kcontrol *kctl,
3869 struct snd_ctl_elem_value *ucontrol)
3871 struct usb_mixer_elem_info *elem = kctl->private_data;
3872 struct usb_mixer_interface *mixer = elem->head.mixer;
3873 struct scarlett2_data *private = mixer->private_data;
3874 int index = line_out_remap(private, elem->control);
3877 mutex_lock(&private->data_mutex);
3879 if (private->hwdep_in_use) {
3884 if (private->vol_updated) {
3885 err = scarlett2_update_volumes(mixer);
3889 ucontrol->value.integer.value[0] = private->vol[index];
3892 mutex_unlock(&private->data_mutex);
3896 static int scarlett2_volume_ctl_put(struct snd_kcontrol *kctl,
3897 struct snd_ctl_elem_value *ucontrol)
3899 struct usb_mixer_elem_info *elem = kctl->private_data;
3900 struct usb_mixer_interface *mixer = elem->head.mixer;
3901 struct scarlett2_data *private = mixer->private_data;
3902 int index = line_out_remap(private, elem->control);
3903 int oval, val, err = 0;
3905 mutex_lock(&private->data_mutex);
3907 if (private->hwdep_in_use) {
3912 oval = private->vol[index];
3913 val = ucontrol->value.integer.value[0];
3918 private->vol[index] = val;
3919 err = scarlett2_usb_set_config(mixer, SCARLETT2_CONFIG_LINE_OUT_VOLUME,
3920 index, val - SCARLETT2_VOLUME_BIAS);
3925 mutex_unlock(&private->data_mutex);
3929 static const DECLARE_TLV_DB_MINMAX(
3930 db_scale_scarlett2_volume, -SCARLETT2_VOLUME_BIAS * 100, 0
3933 static const struct snd_kcontrol_new scarlett2_master_volume_ctl = {
3934 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3935 .access = SNDRV_CTL_ELEM_ACCESS_READ |
3936 SNDRV_CTL_ELEM_ACCESS_TLV_READ,
3938 .info = scarlett2_volume_ctl_info,
3939 .get = scarlett2_master_volume_ctl_get,
3940 .private_value = 0, /* max value */
3941 .tlv = { .p = db_scale_scarlett2_volume }
3944 static const struct snd_kcontrol_new scarlett2_headphone_volume_ctl = {
3945 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3946 .access = SNDRV_CTL_ELEM_ACCESS_READ |
3947 SNDRV_CTL_ELEM_ACCESS_TLV_READ,
3949 .info = scarlett2_volume_ctl_info,
3950 .get = scarlett2_headphone_volume_ctl_get,
3951 .private_value = 0, /* max value */
3952 .tlv = { .p = db_scale_scarlett2_volume }
3955 static const struct snd_kcontrol_new scarlett2_line_out_volume_ctl = {
3956 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3957 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE |
3958 SNDRV_CTL_ELEM_ACCESS_TLV_READ,
3960 .info = scarlett2_volume_ctl_info,
3961 .get = scarlett2_volume_ctl_get,
3962 .put = scarlett2_volume_ctl_put,
3963 .private_value = 0, /* max value */
3964 .tlv = { .p = db_scale_scarlett2_volume }
3967 /*** Mute Switch Controls ***/
3969 static int scarlett2_update_dim_mute(struct usb_mixer_interface *mixer)
3971 struct scarlett2_data *private = mixer->private_data;
3975 private->dim_mute_updated = 0;
3977 if (!scarlett2_has_config_item(private, SCARLETT2_CONFIG_SW_HW_SWITCH))
3980 err = scarlett2_usb_get_config(
3981 mixer, SCARLETT2_CONFIG_DIM_MUTE,
3982 SCARLETT2_DIM_MUTE_COUNT, private->dim_mute);
3986 for (i = 0; i < SCARLETT2_DIM_MUTE_COUNT; i++)
3987 private->dim_mute[i] = !!private->dim_mute[i];
3989 mute = private->dim_mute[SCARLETT2_BUTTON_MUTE];
3991 for (i = 0; i < private->num_line_out; i++)
3992 if (private->vol_sw_hw_switch[i])
3993 private->mute_switch[i] = mute;
3998 static int scarlett2_mute_ctl_get(struct snd_kcontrol *kctl,
3999 struct snd_ctl_elem_value *ucontrol)
4001 struct usb_mixer_elem_info *elem = kctl->private_data;
4002 struct usb_mixer_interface *mixer = elem->head.mixer;
4003 struct scarlett2_data *private = mixer->private_data;
4004 int index = line_out_remap(private, elem->control);
4007 mutex_lock(&private->data_mutex);
4009 if (private->hwdep_in_use) {
4014 if (private->dim_mute_updated) {
4015 err = scarlett2_update_dim_mute(mixer);
4019 ucontrol->value.integer.value[0] = private->mute_switch[index];
4022 mutex_unlock(&private->data_mutex);
4026 static int scarlett2_mute_ctl_put(struct snd_kcontrol *kctl,
4027 struct snd_ctl_elem_value *ucontrol)
4029 struct usb_mixer_elem_info *elem = kctl->private_data;
4030 struct usb_mixer_interface *mixer = elem->head.mixer;
4031 struct scarlett2_data *private = mixer->private_data;
4032 int index = line_out_remap(private, elem->control);
4033 int oval, val, err = 0;
4035 mutex_lock(&private->data_mutex);
4037 if (private->hwdep_in_use) {
4042 oval = private->mute_switch[index];
4043 val = !!ucontrol->value.integer.value[0];
4048 private->mute_switch[index] = val;
4050 /* Send mute change to the device */
4051 err = scarlett2_usb_set_config(mixer, SCARLETT2_CONFIG_MUTE_SWITCH,
4057 mutex_unlock(&private->data_mutex);
4061 static const struct snd_kcontrol_new scarlett2_mute_ctl = {
4062 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
4064 .info = snd_ctl_boolean_mono_info,
4065 .get = scarlett2_mute_ctl_get,
4066 .put = scarlett2_mute_ctl_put,
4069 /*** HW/SW Volume Switch Controls ***/
4071 static void scarlett2_sw_hw_ctl_ro(struct scarlett2_data *private, int index)
4073 private->sw_hw_ctls[index]->vd[0].access &=
4074 ~SNDRV_CTL_ELEM_ACCESS_WRITE;
4077 static void scarlett2_sw_hw_ctl_rw(struct scarlett2_data *private, int index)
4079 private->sw_hw_ctls[index]->vd[0].access |=
4080 SNDRV_CTL_ELEM_ACCESS_WRITE;
4083 static int scarlett2_sw_hw_enum_ctl_info(struct snd_kcontrol *kctl,
4084 struct snd_ctl_elem_info *uinfo)
4086 static const char *const values[2] = {
4090 return snd_ctl_enum_info(uinfo, 1, 2, values);
4093 static int scarlett2_sw_hw_enum_ctl_get(struct snd_kcontrol *kctl,
4094 struct snd_ctl_elem_value *ucontrol)
4096 struct usb_mixer_elem_info *elem = kctl->private_data;
4097 struct scarlett2_data *private = elem->head.mixer->private_data;
4098 int index = line_out_remap(private, elem->control);
4100 ucontrol->value.enumerated.item[0] = private->vol_sw_hw_switch[index];
4104 static void scarlett2_vol_ctl_set_writable(struct usb_mixer_interface *mixer,
4105 int index, int value)
4107 struct scarlett2_data *private = mixer->private_data;
4108 struct snd_card *card = mixer->chip->card;
4110 /* Set/Clear write bits */
4112 private->vol_ctls[index]->vd[0].access |=
4113 SNDRV_CTL_ELEM_ACCESS_WRITE;
4114 private->mute_ctls[index]->vd[0].access |=
4115 SNDRV_CTL_ELEM_ACCESS_WRITE;
4117 private->vol_ctls[index]->vd[0].access &=
4118 ~SNDRV_CTL_ELEM_ACCESS_WRITE;
4119 private->mute_ctls[index]->vd[0].access &=
4120 ~SNDRV_CTL_ELEM_ACCESS_WRITE;
4123 /* Notify of write bit and possible value change */
4124 snd_ctl_notify(card,
4125 SNDRV_CTL_EVENT_MASK_VALUE | SNDRV_CTL_EVENT_MASK_INFO,
4126 &private->vol_ctls[index]->id);
4127 snd_ctl_notify(card,
4128 SNDRV_CTL_EVENT_MASK_VALUE | SNDRV_CTL_EVENT_MASK_INFO,
4129 &private->mute_ctls[index]->id);
4132 static int scarlett2_sw_hw_change(struct usb_mixer_interface *mixer,
4133 int ctl_index, int val)
4135 struct scarlett2_data *private = mixer->private_data;
4136 int index = line_out_remap(private, ctl_index);
4139 private->vol_sw_hw_switch[index] = val;
4141 /* Change access mode to RO (hardware controlled volume)
4142 * or RW (software controlled volume)
4144 scarlett2_vol_ctl_set_writable(mixer, ctl_index, !val);
4146 /* Reset volume/mute to master volume/mute */
4147 private->vol[index] = private->master_vol;
4148 private->mute_switch[index] = private->dim_mute[SCARLETT2_BUTTON_MUTE];
4150 /* Set SW volume to current HW volume */
4151 err = scarlett2_usb_set_config(
4152 mixer, SCARLETT2_CONFIG_LINE_OUT_VOLUME,
4153 index, private->master_vol - SCARLETT2_VOLUME_BIAS);
4157 /* Set SW mute to current HW mute */
4158 err = scarlett2_usb_set_config(
4159 mixer, SCARLETT2_CONFIG_MUTE_SWITCH,
4160 index, private->dim_mute[SCARLETT2_BUTTON_MUTE]);
4164 /* Send SW/HW switch change to the device */
4165 return scarlett2_usb_set_config(mixer, SCARLETT2_CONFIG_SW_HW_SWITCH,
4169 static int scarlett2_sw_hw_enum_ctl_put(struct snd_kcontrol *kctl,
4170 struct snd_ctl_elem_value *ucontrol)
4172 struct usb_mixer_elem_info *elem = kctl->private_data;
4173 struct usb_mixer_interface *mixer = elem->head.mixer;
4174 struct scarlett2_data *private = mixer->private_data;
4175 int ctl_index = elem->control;
4176 int index = line_out_remap(private, ctl_index);
4177 int oval, val, err = 0;
4179 mutex_lock(&private->data_mutex);
4181 if (private->hwdep_in_use) {
4186 oval = private->vol_sw_hw_switch[index];
4187 val = !!ucontrol->value.enumerated.item[0];
4192 err = scarlett2_sw_hw_change(mixer, ctl_index, val);
4197 mutex_unlock(&private->data_mutex);
4201 static const struct snd_kcontrol_new scarlett2_sw_hw_enum_ctl = {
4202 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
4204 .info = scarlett2_sw_hw_enum_ctl_info,
4205 .get = scarlett2_sw_hw_enum_ctl_get,
4206 .put = scarlett2_sw_hw_enum_ctl_put,
4209 /*** Line Level/Instrument Level Switch Controls ***/
4211 static int scarlett2_update_input_level(struct usb_mixer_interface *mixer)
4213 struct scarlett2_data *private = mixer->private_data;
4214 const struct scarlett2_device_info *info = private->info;
4216 private->input_level_updated = 0;
4218 if (!info->level_input_count)
4221 return scarlett2_usb_get_config(
4222 mixer, SCARLETT2_CONFIG_LEVEL_SWITCH,
4223 info->level_input_count + info->level_input_first,
4224 private->level_switch);
4227 static int scarlett2_level_enum_ctl_info(struct snd_kcontrol *kctl,
4228 struct snd_ctl_elem_info *uinfo)
4230 static const char *const values[2] = {
4233 struct usb_mixer_elem_info *elem = kctl->private_data;
4234 struct usb_mixer_interface *mixer = elem->head.mixer;
4235 struct scarlett2_data *private = mixer->private_data;
4238 mutex_lock(&private->data_mutex);
4240 if (private->hwdep_in_use) {
4245 err = scarlett2_check_autogain_updated(mixer);
4249 err = snd_ctl_enum_info(uinfo, 1, 2, values);
4252 mutex_unlock(&private->data_mutex);
4256 static int scarlett2_level_enum_ctl_get(struct snd_kcontrol *kctl,
4257 struct snd_ctl_elem_value *ucontrol)
4259 struct usb_mixer_elem_info *elem = kctl->private_data;
4260 struct usb_mixer_interface *mixer = elem->head.mixer;
4261 struct scarlett2_data *private = mixer->private_data;
4262 const struct scarlett2_device_info *info = private->info;
4264 int index = elem->control + info->level_input_first;
4267 mutex_lock(&private->data_mutex);
4269 if (private->hwdep_in_use) {
4274 if (private->input_level_updated) {
4275 err = scarlett2_update_input_level(mixer);
4279 ucontrol->value.enumerated.item[0] = scarlett2_decode_muteable(
4280 private->level_switch[index]);
4283 mutex_unlock(&private->data_mutex);
4287 static int scarlett2_level_enum_ctl_put(struct snd_kcontrol *kctl,
4288 struct snd_ctl_elem_value *ucontrol)
4290 struct usb_mixer_elem_info *elem = kctl->private_data;
4291 struct usb_mixer_interface *mixer = elem->head.mixer;
4292 struct scarlett2_data *private = mixer->private_data;
4293 const struct scarlett2_device_info *info = private->info;
4295 int index = elem->control + info->level_input_first;
4298 mutex_lock(&private->data_mutex);
4300 if (private->hwdep_in_use) {
4305 err = scarlett2_check_put_during_autogain(mixer);
4309 oval = private->level_switch[index];
4310 val = !!ucontrol->value.enumerated.item[0];
4315 private->level_switch[index] = val;
4317 /* To set the Gen 4 muteable controls, bit 1 gets set instead */
4318 if (private->config_set->items[SCARLETT2_CONFIG_LEVEL_SWITCH].mute)
4319 val = (!val) | 0x02;
4321 /* Send switch change to the device */
4322 err = scarlett2_usb_set_config(mixer, SCARLETT2_CONFIG_LEVEL_SWITCH,
4328 mutex_unlock(&private->data_mutex);
4332 static const struct snd_kcontrol_new scarlett2_level_enum_ctl = {
4333 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
4335 .info = scarlett2_level_enum_ctl_info,
4336 .get = scarlett2_level_enum_ctl_get,
4337 .put = scarlett2_level_enum_ctl_put,
4340 /*** Pad Switch Controls ***/
4342 static int scarlett2_update_input_pad(struct usb_mixer_interface *mixer)
4344 struct scarlett2_data *private = mixer->private_data;
4345 const struct scarlett2_device_info *info = private->info;
4347 private->input_pad_updated = 0;
4349 if (!info->pad_input_count)
4352 return scarlett2_usb_get_config(
4353 mixer, SCARLETT2_CONFIG_PAD_SWITCH,
4354 info->pad_input_count, private->pad_switch);
4357 static int scarlett2_pad_ctl_get(struct snd_kcontrol *kctl,
4358 struct snd_ctl_elem_value *ucontrol)
4360 struct usb_mixer_elem_info *elem = kctl->private_data;
4361 struct usb_mixer_interface *mixer = elem->head.mixer;
4362 struct scarlett2_data *private = mixer->private_data;
4365 mutex_lock(&private->data_mutex);
4367 if (private->hwdep_in_use) {
4372 if (private->input_pad_updated) {
4373 err = scarlett2_update_input_pad(mixer);
4377 ucontrol->value.integer.value[0] =
4378 private->pad_switch[elem->control];
4381 mutex_unlock(&private->data_mutex);
4385 static int scarlett2_pad_ctl_put(struct snd_kcontrol *kctl,
4386 struct snd_ctl_elem_value *ucontrol)
4388 struct usb_mixer_elem_info *elem = kctl->private_data;
4389 struct usb_mixer_interface *mixer = elem->head.mixer;
4390 struct scarlett2_data *private = mixer->private_data;
4392 int index = elem->control;
4393 int oval, val, err = 0;
4395 mutex_lock(&private->data_mutex);
4397 if (private->hwdep_in_use) {
4402 oval = private->pad_switch[index];
4403 val = !!ucontrol->value.integer.value[0];
4408 private->pad_switch[index] = val;
4410 /* Send switch change to the device */
4411 err = scarlett2_usb_set_config(mixer, SCARLETT2_CONFIG_PAD_SWITCH,
4417 mutex_unlock(&private->data_mutex);
4421 static const struct snd_kcontrol_new scarlett2_pad_ctl = {
4422 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
4424 .info = snd_ctl_boolean_mono_info,
4425 .get = scarlett2_pad_ctl_get,
4426 .put = scarlett2_pad_ctl_put,
4429 /*** Air Switch Controls ***/
4431 static int scarlett2_update_input_air(struct usb_mixer_interface *mixer)
4433 struct scarlett2_data *private = mixer->private_data;
4434 const struct scarlett2_device_info *info = private->info;
4436 private->input_air_updated = 0;
4438 if (!info->air_input_count)
4441 return scarlett2_usb_get_config(
4442 mixer, SCARLETT2_CONFIG_AIR_SWITCH,
4443 info->air_input_count, private->air_switch);
4446 static int scarlett2_air_ctl_get(struct snd_kcontrol *kctl,
4447 struct snd_ctl_elem_value *ucontrol)
4449 struct usb_mixer_elem_info *elem = kctl->private_data;
4450 struct usb_mixer_interface *mixer = elem->head.mixer;
4451 struct scarlett2_data *private = mixer->private_data;
4454 mutex_lock(&private->data_mutex);
4456 if (private->hwdep_in_use) {
4461 if (private->input_air_updated) {
4462 err = scarlett2_update_input_air(mixer);
4466 ucontrol->value.integer.value[0] = private->air_switch[elem->control];
4469 mutex_unlock(&private->data_mutex);
4473 static int scarlett2_air_ctl_put(struct snd_kcontrol *kctl,
4474 struct snd_ctl_elem_value *ucontrol)
4476 struct usb_mixer_elem_info *elem = kctl->private_data;
4477 struct usb_mixer_interface *mixer = elem->head.mixer;
4478 struct scarlett2_data *private = mixer->private_data;
4480 int index = elem->control;
4483 mutex_lock(&private->data_mutex);
4485 if (private->hwdep_in_use) {
4490 err = scarlett2_check_put_during_autogain(mixer);
4494 oval = private->air_switch[index];
4495 val = ucontrol->value.integer.value[0];
4500 private->air_switch[index] = val;
4502 /* Send switch change to the device */
4503 err = scarlett2_usb_set_config(mixer, SCARLETT2_CONFIG_AIR_SWITCH,
4509 mutex_unlock(&private->data_mutex);
4513 static int scarlett2_air_with_drive_ctl_info(
4514 struct snd_kcontrol *kctl, struct snd_ctl_elem_info *uinfo)
4516 static const char *const values[3] = {
4517 "Off", "Presence", "Presence + Drive"
4519 struct usb_mixer_elem_info *elem = kctl->private_data;
4520 struct usb_mixer_interface *mixer = elem->head.mixer;
4521 struct scarlett2_data *private = mixer->private_data;
4524 mutex_lock(&private->data_mutex);
4526 if (private->hwdep_in_use) {
4531 err = scarlett2_check_autogain_updated(mixer);
4535 err = snd_ctl_enum_info(uinfo, 1, 3, values);
4538 mutex_unlock(&private->data_mutex);
4542 static const struct snd_kcontrol_new scarlett2_air_ctl[2] = {
4544 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
4546 .info = snd_ctl_boolean_mono_info,
4547 .get = scarlett2_air_ctl_get,
4548 .put = scarlett2_air_ctl_put,
4551 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
4553 .info = scarlett2_air_with_drive_ctl_info,
4554 .get = scarlett2_air_ctl_get,
4555 .put = scarlett2_air_ctl_put,
4559 /*** Phantom Switch Controls ***/
4561 static int scarlett2_update_input_phantom(struct usb_mixer_interface *mixer)
4563 struct scarlett2_data *private = mixer->private_data;
4564 const struct scarlett2_device_info *info = private->info;
4567 private->input_phantom_updated = 0;
4569 if (!info->phantom_count)
4572 err = scarlett2_usb_get_config(
4573 mixer, SCARLETT2_CONFIG_PHANTOM_SWITCH,
4574 info->phantom_count, private->phantom_switch);
4578 if (scarlett2_has_config_item(private,
4579 SCARLETT2_CONFIG_PHANTOM_PERSISTENCE)) {
4580 err = scarlett2_usb_get_config(
4581 mixer, SCARLETT2_CONFIG_PHANTOM_PERSISTENCE,
4582 1, &private->phantom_persistence);
4590 /* Check if phantom power on the given input is currently changing state */
4591 static int scarlett2_phantom_is_switching(
4592 struct scarlett2_data *private, int line_num)
4594 const struct scarlett2_device_info *info = private->info;
4595 int index = line_num / info->inputs_per_phantom;
4597 return !!(private->phantom_switch[index] & 0x02);
4600 /* Update autogain controls' access mode when phantom power changes state */
4601 static void scarlett2_phantom_update_access(struct usb_mixer_interface *mixer)
4603 struct scarlett2_data *private = mixer->private_data;
4604 const struct scarlett2_device_info *info = private->info;
4607 /* Disable autogain controls if phantom power is changing state */
4608 for (i = 0; i < info->gain_input_count; i++) {
4609 int val = !scarlett2_phantom_is_switching(private, i);
4611 scarlett2_set_ctl_access(private->autogain_ctls[i], val);
4615 /* Notify of access mode change for autogain which can't be enabled
4616 * while phantom power is changing.
4618 static void scarlett2_phantom_notify_access(struct usb_mixer_interface *mixer)
4620 struct snd_card *card = mixer->chip->card;
4621 struct scarlett2_data *private = mixer->private_data;
4622 const struct scarlett2_device_info *info = private->info;
4625 for (i = 0; i < info->gain_input_count; i++)
4626 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_INFO,
4627 &private->autogain_ctls[i]->id);
4630 /* Call scarlett2_update_input_phantom() and
4631 * scarlett2_phantom_update_access() if input_phantom_updated is set.
4633 static int scarlett2_check_input_phantom_updated(
4634 struct usb_mixer_interface *mixer)
4636 struct scarlett2_data *private = mixer->private_data;
4639 if (!private->input_phantom_updated)
4642 err = scarlett2_update_input_phantom(mixer);
4646 scarlett2_phantom_update_access(mixer);
4651 static int scarlett2_phantom_ctl_get(struct snd_kcontrol *kctl,
4652 struct snd_ctl_elem_value *ucontrol)
4654 struct usb_mixer_elem_info *elem = kctl->private_data;
4655 struct usb_mixer_interface *mixer = elem->head.mixer;
4656 struct scarlett2_data *private = mixer->private_data;
4659 mutex_lock(&private->data_mutex);
4661 if (private->hwdep_in_use) {
4666 err = scarlett2_check_input_phantom_updated(mixer);
4670 ucontrol->value.integer.value[0] = scarlett2_decode_muteable(
4671 private->phantom_switch[elem->control]);
4674 mutex_unlock(&private->data_mutex);
4678 static int scarlett2_phantom_ctl_put(struct snd_kcontrol *kctl,
4679 struct snd_ctl_elem_value *ucontrol)
4681 struct usb_mixer_elem_info *elem = kctl->private_data;
4682 struct usb_mixer_interface *mixer = elem->head.mixer;
4683 struct scarlett2_data *private = mixer->private_data;
4684 const struct scarlett2_device_info *info = private->info;
4686 int index = elem->control;
4689 mutex_lock(&private->data_mutex);
4691 if (private->hwdep_in_use) {
4696 err = scarlett2_check_put_during_autogain(mixer);
4700 oval = private->phantom_switch[index];
4701 val = !!ucontrol->value.integer.value[0];
4706 private->phantom_switch[index] = val;
4708 /* To set the Gen 4 muteable controls, bit 1 gets set */
4709 if (private->config_set->items[SCARLETT2_CONFIG_PHANTOM_SWITCH].mute)
4710 val = (!val) | 0x02;
4712 /* Send switch change to the device */
4713 err = scarlett2_usb_set_config(mixer, SCARLETT2_CONFIG_PHANTOM_SWITCH,
4714 index + info->phantom_first, val);
4718 scarlett2_phantom_update_access(mixer);
4719 scarlett2_phantom_notify_access(mixer);
4722 mutex_unlock(&private->data_mutex);
4726 static const struct snd_kcontrol_new scarlett2_phantom_ctl = {
4727 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
4729 .info = scarlett2_autogain_disables_ctl_info,
4730 .get = scarlett2_phantom_ctl_get,
4731 .put = scarlett2_phantom_ctl_put,
4734 /*** Phantom Persistence Control ***/
4736 static int scarlett2_phantom_persistence_ctl_get(
4737 struct snd_kcontrol *kctl, struct snd_ctl_elem_value *ucontrol)
4739 struct usb_mixer_elem_info *elem = kctl->private_data;
4740 struct scarlett2_data *private = elem->head.mixer->private_data;
4742 ucontrol->value.integer.value[0] = private->phantom_persistence;
4746 static int scarlett2_phantom_persistence_ctl_put(
4747 struct snd_kcontrol *kctl, struct snd_ctl_elem_value *ucontrol)
4749 struct usb_mixer_elem_info *elem = kctl->private_data;
4750 struct usb_mixer_interface *mixer = elem->head.mixer;
4751 struct scarlett2_data *private = mixer->private_data;
4753 int index = elem->control;
4754 int oval, val, err = 0;
4756 mutex_lock(&private->data_mutex);
4758 if (private->hwdep_in_use) {
4763 oval = private->phantom_persistence;
4764 val = !!ucontrol->value.integer.value[0];
4769 private->phantom_persistence = val;
4771 /* Send switch change to the device */
4772 err = scarlett2_usb_set_config(
4773 mixer, SCARLETT2_CONFIG_PHANTOM_PERSISTENCE, index, val);
4778 mutex_unlock(&private->data_mutex);
4782 static const struct snd_kcontrol_new scarlett2_phantom_persistence_ctl = {
4783 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
4785 .info = snd_ctl_boolean_mono_info,
4786 .get = scarlett2_phantom_persistence_ctl_get,
4787 .put = scarlett2_phantom_persistence_ctl_put,
4790 /*** Speaker Switching Control ***/
4792 static int scarlett2_update_monitor_other(struct usb_mixer_interface *mixer)
4794 struct scarlett2_data *private = mixer->private_data;
4795 const struct scarlett2_device_info *info = private->info;
4798 /* monitor_other_enable[0] enables speaker switching
4799 * monitor_other_enable[1] enables talkback
4801 u8 monitor_other_enable[2];
4803 /* monitor_other_switch[0] activates the alternate speakers
4804 * monitor_other_switch[1] activates talkback
4806 u8 monitor_other_switch[2];
4808 private->monitor_other_updated = 0;
4810 /* if it doesn't do speaker switching then it also doesn't do
4813 if (!info->has_speaker_switching)
4816 err = scarlett2_usb_get_config(
4817 mixer, SCARLETT2_CONFIG_MONITOR_OTHER_ENABLE,
4818 2, monitor_other_enable);
4822 err = scarlett2_usb_get_config(
4823 mixer, SCARLETT2_CONFIG_MONITOR_OTHER_SWITCH,
4824 2, monitor_other_switch);
4828 if (!monitor_other_enable[0])
4829 private->speaker_switching_switch = 0;
4831 private->speaker_switching_switch = monitor_other_switch[0] + 1;
4833 if (info->has_talkback) {
4837 if (!monitor_other_enable[1])
4838 private->talkback_switch = 0;
4840 private->talkback_switch = monitor_other_switch[1] + 1;
4842 err = scarlett2_usb_get_config(mixer,
4843 SCARLETT2_CONFIG_TALKBACK_MAP,
4847 for (i = 0; i < private->num_mix_out; i++, bitmap >>= 1)
4848 private->talkback_map[i] = bitmap & 1;
4854 static int scarlett2_speaker_switch_enum_ctl_info(
4855 struct snd_kcontrol *kctl, struct snd_ctl_elem_info *uinfo)
4857 static const char *const values[3] = {
4858 "Off", "Main", "Alt"
4861 return snd_ctl_enum_info(uinfo, 1, 3, values);
4864 static int scarlett2_speaker_switch_enum_ctl_get(
4865 struct snd_kcontrol *kctl, struct snd_ctl_elem_value *ucontrol)
4867 struct usb_mixer_elem_info *elem = kctl->private_data;
4868 struct usb_mixer_interface *mixer = elem->head.mixer;
4869 struct scarlett2_data *private = mixer->private_data;
4872 mutex_lock(&private->data_mutex);
4874 if (private->hwdep_in_use) {
4879 if (private->monitor_other_updated) {
4880 err = scarlett2_update_monitor_other(mixer);
4884 ucontrol->value.enumerated.item[0] = private->speaker_switching_switch;
4887 mutex_unlock(&private->data_mutex);
4891 /* when speaker switching gets enabled, switch the main/alt speakers
4892 * to HW volume and disable those controls
4894 static int scarlett2_speaker_switch_enable(struct usb_mixer_interface *mixer)
4896 struct snd_card *card = mixer->chip->card;
4897 struct scarlett2_data *private = mixer->private_data;
4900 for (i = 0; i < 4; i++) {
4901 int index = line_out_remap(private, i);
4903 /* switch the main/alt speakers to HW volume */
4904 if (!private->vol_sw_hw_switch[index]) {
4905 err = scarlett2_sw_hw_change(private->mixer, i, 1);
4910 /* disable the line out SW/HW switch */
4911 scarlett2_sw_hw_ctl_ro(private, i);
4912 snd_ctl_notify(card,
4913 SNDRV_CTL_EVENT_MASK_VALUE |
4914 SNDRV_CTL_EVENT_MASK_INFO,
4915 &private->sw_hw_ctls[i]->id);
4918 /* when the next monitor-other notify comes in, update the mux
4921 private->speaker_switching_switched = 1;
4926 /* when speaker switching gets disabled, reenable the hw/sw controls
4927 * and invalidate the routing
4929 static void scarlett2_speaker_switch_disable(struct usb_mixer_interface *mixer)
4931 struct snd_card *card = mixer->chip->card;
4932 struct scarlett2_data *private = mixer->private_data;
4935 /* enable the line out SW/HW switch */
4936 for (i = 0; i < 4; i++) {
4937 scarlett2_sw_hw_ctl_rw(private, i);
4938 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_INFO,
4939 &private->sw_hw_ctls[i]->id);
4942 /* when the next monitor-other notify comes in, update the mux
4945 private->speaker_switching_switched = 1;
4948 static int scarlett2_speaker_switch_enum_ctl_put(
4949 struct snd_kcontrol *kctl, struct snd_ctl_elem_value *ucontrol)
4951 struct usb_mixer_elem_info *elem = kctl->private_data;
4952 struct usb_mixer_interface *mixer = elem->head.mixer;
4953 struct scarlett2_data *private = mixer->private_data;
4955 int oval, val, err = 0;
4957 mutex_lock(&private->data_mutex);
4959 if (private->hwdep_in_use) {
4964 oval = private->speaker_switching_switch;
4965 val = min(ucontrol->value.enumerated.item[0], 2U);
4970 private->speaker_switching_switch = val;
4972 /* enable/disable speaker switching */
4973 err = scarlett2_usb_set_config(
4974 mixer, SCARLETT2_CONFIG_MONITOR_OTHER_ENABLE,
4979 /* if speaker switching is enabled, select main or alt */
4980 err = scarlett2_usb_set_config(
4981 mixer, SCARLETT2_CONFIG_MONITOR_OTHER_SWITCH,
4986 /* update controls if speaker switching gets enabled or disabled */
4988 err = scarlett2_speaker_switch_enable(mixer);
4989 else if (oval && !val)
4990 scarlett2_speaker_switch_disable(mixer);
4996 mutex_unlock(&private->data_mutex);
5000 static const struct snd_kcontrol_new scarlett2_speaker_switch_enum_ctl = {
5001 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
5003 .info = scarlett2_speaker_switch_enum_ctl_info,
5004 .get = scarlett2_speaker_switch_enum_ctl_get,
5005 .put = scarlett2_speaker_switch_enum_ctl_put,
5008 static int scarlett2_add_speaker_switch_ctl(struct usb_mixer_interface *mixer)
5010 struct scarlett2_data *private = mixer->private_data;
5011 const struct scarlett2_device_info *info = private->info;
5013 if (!info->has_speaker_switching)
5016 return scarlett2_add_new_ctl(
5017 mixer, &scarlett2_speaker_switch_enum_ctl,
5018 0, 1, "Speaker Switching Playback Enum",
5019 &private->speaker_switching_ctl);
5022 /*** Talkback and Talkback Map Controls ***/
5024 static int scarlett2_talkback_enum_ctl_info(
5025 struct snd_kcontrol *kctl, struct snd_ctl_elem_info *uinfo)
5027 static const char *const values[3] = {
5028 "Disabled", "Off", "On"
5031 return snd_ctl_enum_info(uinfo, 1, 3, values);
5034 static int scarlett2_talkback_enum_ctl_get(
5035 struct snd_kcontrol *kctl, struct snd_ctl_elem_value *ucontrol)
5037 struct usb_mixer_elem_info *elem = kctl->private_data;
5038 struct usb_mixer_interface *mixer = elem->head.mixer;
5039 struct scarlett2_data *private = mixer->private_data;
5042 mutex_lock(&private->data_mutex);
5044 if (private->hwdep_in_use) {
5049 if (private->monitor_other_updated) {
5050 err = scarlett2_update_monitor_other(mixer);
5054 ucontrol->value.enumerated.item[0] = private->talkback_switch;
5057 mutex_unlock(&private->data_mutex);
5061 static int scarlett2_talkback_enum_ctl_put(
5062 struct snd_kcontrol *kctl, struct snd_ctl_elem_value *ucontrol)
5064 struct usb_mixer_elem_info *elem = kctl->private_data;
5065 struct usb_mixer_interface *mixer = elem->head.mixer;
5066 struct scarlett2_data *private = mixer->private_data;
5068 int oval, val, err = 0;
5070 mutex_lock(&private->data_mutex);
5072 if (private->hwdep_in_use) {
5077 oval = private->talkback_switch;
5078 val = min(ucontrol->value.enumerated.item[0], 2U);
5083 private->talkback_switch = val;
5085 /* enable/disable talkback */
5086 err = scarlett2_usb_set_config(
5087 mixer, SCARLETT2_CONFIG_MONITOR_OTHER_ENABLE,
5092 /* if talkback is enabled, select main or alt */
5093 err = scarlett2_usb_set_config(
5094 mixer, SCARLETT2_CONFIG_MONITOR_OTHER_SWITCH,
5100 mutex_unlock(&private->data_mutex);
5104 static const struct snd_kcontrol_new scarlett2_talkback_enum_ctl = {
5105 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
5107 .info = scarlett2_talkback_enum_ctl_info,
5108 .get = scarlett2_talkback_enum_ctl_get,
5109 .put = scarlett2_talkback_enum_ctl_put,
5112 static int scarlett2_talkback_map_ctl_get(
5113 struct snd_kcontrol *kctl, struct snd_ctl_elem_value *ucontrol)
5115 struct usb_mixer_elem_info *elem = kctl->private_data;
5116 struct usb_mixer_interface *mixer = elem->head.mixer;
5117 struct scarlett2_data *private = mixer->private_data;
5118 int index = elem->control;
5120 ucontrol->value.integer.value[0] = private->talkback_map[index];
5125 static int scarlett2_talkback_map_ctl_put(
5126 struct snd_kcontrol *kctl, struct snd_ctl_elem_value *ucontrol)
5128 struct usb_mixer_elem_info *elem = kctl->private_data;
5129 struct usb_mixer_interface *mixer = elem->head.mixer;
5130 struct scarlett2_data *private = mixer->private_data;
5131 int index = elem->control;
5132 int oval, val, err = 0, i;
5135 mutex_lock(&private->data_mutex);
5137 if (private->hwdep_in_use) {
5142 oval = private->talkback_map[index];
5143 val = !!ucontrol->value.integer.value[0];
5148 private->talkback_map[index] = val;
5150 for (i = 0; i < private->num_mix_out; i++)
5151 bitmap |= private->talkback_map[i] << i;
5153 /* Send updated bitmap to the device */
5154 err = scarlett2_usb_set_config(mixer, SCARLETT2_CONFIG_TALKBACK_MAP,
5160 mutex_unlock(&private->data_mutex);
5164 static const struct snd_kcontrol_new scarlett2_talkback_map_ctl = {
5165 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
5167 .info = snd_ctl_boolean_mono_info,
5168 .get = scarlett2_talkback_map_ctl_get,
5169 .put = scarlett2_talkback_map_ctl_put,
5172 static int scarlett2_add_talkback_ctls(struct usb_mixer_interface *mixer)
5174 struct scarlett2_data *private = mixer->private_data;
5175 const struct scarlett2_device_info *info = private->info;
5177 char s[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
5179 if (!info->has_talkback)
5182 err = scarlett2_add_new_ctl(
5183 mixer, &scarlett2_talkback_enum_ctl,
5184 0, 1, "Talkback Playback Enum",
5185 &private->talkback_ctl);
5189 for (i = 0; i < private->num_mix_out; i++) {
5190 snprintf(s, sizeof(s),
5191 "Talkback Mix %c Playback Switch", i + 'A');
5192 err = scarlett2_add_new_ctl(mixer, &scarlett2_talkback_map_ctl,
5201 /*** Dim/Mute Controls ***/
5203 static int scarlett2_dim_mute_ctl_get(struct snd_kcontrol *kctl,
5204 struct snd_ctl_elem_value *ucontrol)
5206 struct usb_mixer_elem_info *elem = kctl->private_data;
5207 struct usb_mixer_interface *mixer = elem->head.mixer;
5208 struct scarlett2_data *private = mixer->private_data;
5211 mutex_lock(&private->data_mutex);
5213 if (private->hwdep_in_use) {
5218 if (private->dim_mute_updated) {
5219 err = scarlett2_update_dim_mute(mixer);
5223 ucontrol->value.integer.value[0] = private->dim_mute[elem->control];
5226 mutex_unlock(&private->data_mutex);
5230 static int scarlett2_dim_mute_ctl_put(struct snd_kcontrol *kctl,
5231 struct snd_ctl_elem_value *ucontrol)
5233 struct usb_mixer_elem_info *elem = kctl->private_data;
5234 struct usb_mixer_interface *mixer = elem->head.mixer;
5235 struct scarlett2_data *private = mixer->private_data;
5236 int index = elem->control;
5237 int oval, val, err = 0, i;
5239 mutex_lock(&private->data_mutex);
5241 if (private->hwdep_in_use) {
5246 oval = private->dim_mute[index];
5247 val = !!ucontrol->value.integer.value[0];
5252 private->dim_mute[index] = val;
5254 /* Send switch change to the device */
5255 err = scarlett2_usb_set_config(mixer, SCARLETT2_CONFIG_DIM_MUTE,
5260 if (index == SCARLETT2_BUTTON_MUTE)
5261 for (i = 0; i < private->num_line_out; i++) {
5262 int line_index = line_out_remap(private, i);
5264 if (private->vol_sw_hw_switch[line_index]) {
5265 private->mute_switch[line_index] = val;
5266 snd_ctl_notify(mixer->chip->card,
5267 SNDRV_CTL_EVENT_MASK_VALUE,
5268 &private->mute_ctls[i]->id);
5273 mutex_unlock(&private->data_mutex);
5277 static const struct snd_kcontrol_new scarlett2_dim_mute_ctl = {
5278 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
5280 .info = snd_ctl_boolean_mono_info,
5281 .get = scarlett2_dim_mute_ctl_get,
5282 .put = scarlett2_dim_mute_ctl_put
5285 /*** Create the analogue output controls ***/
5287 static int scarlett2_add_line_out_ctls(struct usb_mixer_interface *mixer)
5289 struct scarlett2_data *private = mixer->private_data;
5290 const struct scarlett2_device_info *info = private->info;
5292 char s[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
5294 /* Add R/O HW volume control */
5295 if (scarlett2_has_config_item(private,
5296 SCARLETT2_CONFIG_MASTER_VOLUME)) {
5297 snprintf(s, sizeof(s), "Master HW Playback Volume");
5298 err = scarlett2_add_new_ctl(mixer,
5299 &scarlett2_master_volume_ctl,
5300 0, 1, s, &private->master_vol_ctl);
5305 /* Add R/O headphone volume control */
5306 if (scarlett2_has_config_item(private,
5307 SCARLETT2_CONFIG_HEADPHONE_VOLUME)) {
5308 snprintf(s, sizeof(s), "Headphone Playback Volume");
5309 err = scarlett2_add_new_ctl(mixer,
5310 &scarlett2_headphone_volume_ctl,
5312 &private->headphone_vol_ctl);
5317 /* Remaining controls are only applicable if the device
5318 * has per-channel line-out volume controls.
5320 if (!scarlett2_has_config_item(private,
5321 SCARLETT2_CONFIG_LINE_OUT_VOLUME))
5324 /* Add volume controls */
5325 for (i = 0; i < private->num_line_out; i++) {
5326 int index = line_out_remap(private, i);
5329 if (info->line_out_descrs[i])
5330 snprintf(s, sizeof(s),
5331 "Line %02d (%s) Playback Volume",
5332 i + 1, info->line_out_descrs[i]);
5334 snprintf(s, sizeof(s),
5335 "Line %02d Playback Volume",
5337 err = scarlett2_add_new_ctl(mixer,
5338 &scarlett2_line_out_volume_ctl,
5339 i, 1, s, &private->vol_ctls[i]);
5344 snprintf(s, sizeof(s),
5345 "Line %02d Mute Playback Switch",
5347 err = scarlett2_add_new_ctl(mixer,
5348 &scarlett2_mute_ctl,
5350 &private->mute_ctls[i]);
5355 if (scarlett2_has_config_item(private,
5356 SCARLETT2_CONFIG_SW_HW_SWITCH)) {
5358 /* Make the fader and mute controls read-only if the
5359 * SW/HW switch is set to HW
5361 if (private->vol_sw_hw_switch[index])
5362 scarlett2_vol_ctl_set_writable(mixer, i, 0);
5364 snprintf(s, sizeof(s),
5365 "Line Out %02d Volume Control Playback Enum",
5367 err = scarlett2_add_new_ctl(mixer,
5368 &scarlett2_sw_hw_enum_ctl,
5370 &private->sw_hw_ctls[i]);
5374 /* Make the switch read-only if the line is
5375 * involved in speaker switching
5377 if (private->speaker_switching_switch && i < 4)
5378 scarlett2_sw_hw_ctl_ro(private, i);
5382 /* Add dim/mute controls */
5383 if (scarlett2_has_config_item(private, SCARLETT2_CONFIG_DIM_MUTE))
5384 for (i = 0; i < SCARLETT2_DIM_MUTE_COUNT; i++) {
5385 err = scarlett2_add_new_ctl(
5386 mixer, &scarlett2_dim_mute_ctl,
5387 i, 1, scarlett2_dim_mute_names[i],
5388 &private->dim_mute_ctls[i]);
5396 /*** Create the analogue input controls ***/
5398 static int scarlett2_add_line_in_ctls(struct usb_mixer_interface *mixer)
5400 struct scarlett2_data *private = mixer->private_data;
5401 const struct scarlett2_device_info *info = private->info;
5403 char s[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
5404 const char *fmt = "Line In %d %s Capture %s";
5405 const char *fmt2 = "Line In %d-%d %s Capture %s";
5407 /* Add input level (line/inst) controls */
5408 for (i = 0; i < info->level_input_count; i++) {
5409 snprintf(s, sizeof(s), fmt, i + 1 + info->level_input_first,
5411 err = scarlett2_add_new_ctl(mixer, &scarlett2_level_enum_ctl,
5412 i, 1, s, &private->level_ctls[i]);
5417 /* Add input pad controls */
5418 for (i = 0; i < info->pad_input_count; i++) {
5419 snprintf(s, sizeof(s), fmt, i + 1, "Pad", "Switch");
5420 err = scarlett2_add_new_ctl(mixer, &scarlett2_pad_ctl,
5421 i, 1, s, &private->pad_ctls[i]);
5426 /* Add input air controls */
5427 for (i = 0; i < info->air_input_count; i++) {
5428 snprintf(s, sizeof(s), fmt, i + 1 + info->air_input_first,
5429 "Air", info->air_option ? "Enum" : "Switch");
5430 err = scarlett2_add_new_ctl(
5431 mixer, &scarlett2_air_ctl[info->air_option],
5432 i, 1, s, &private->air_ctls[i]);
5437 /* Add input phantom controls */
5438 if (info->inputs_per_phantom == 1) {
5439 for (i = 0; i < info->phantom_count; i++) {
5440 scnprintf(s, sizeof(s), fmt,
5441 i + 1 + info->phantom_first,
5442 "Phantom Power", "Switch");
5443 err = scarlett2_add_new_ctl(
5444 mixer, &scarlett2_phantom_ctl,
5445 i, 1, s, &private->phantom_ctls[i]);
5449 } else if (info->inputs_per_phantom > 1) {
5450 for (i = 0; i < info->phantom_count; i++) {
5451 int from = i * info->inputs_per_phantom + 1;
5452 int to = (i + 1) * info->inputs_per_phantom;
5454 scnprintf(s, sizeof(s), fmt2, from, to,
5455 "Phantom Power", "Switch");
5456 err = scarlett2_add_new_ctl(
5457 mixer, &scarlett2_phantom_ctl,
5458 i, 1, s, &private->phantom_ctls[i]);
5463 if (info->phantom_count &&
5464 scarlett2_has_config_item(private,
5465 SCARLETT2_CONFIG_PHANTOM_PERSISTENCE)) {
5466 err = scarlett2_add_new_ctl(
5467 mixer, &scarlett2_phantom_persistence_ctl, 0, 1,
5468 "Phantom Power Persistence Capture Switch", NULL);
5473 /* Add software-controllable input gain controls */
5474 if (info->gain_input_count) {
5475 err = scarlett2_add_new_ctl(
5476 mixer, &scarlett2_input_select_ctl, 0, 1,
5477 "Input Select Capture Enum",
5478 &private->input_select_ctl);
5482 for (i = 0; i < info->gain_input_count; i++) {
5484 snprintf(s, sizeof(s),
5485 "Line In %d-%d Link Capture Switch",
5487 err = scarlett2_add_new_ctl(
5488 mixer, &scarlett2_input_link_ctl,
5490 &private->input_link_ctls[i / 2]);
5495 snprintf(s, sizeof(s), fmt, i + 1,
5497 err = scarlett2_add_new_ctl(
5498 mixer, &scarlett2_input_gain_ctl,
5499 i, 1, s, &private->input_gain_ctls[i]);
5503 snprintf(s, sizeof(s), fmt, i + 1,
5504 "Autogain", "Switch");
5505 err = scarlett2_add_new_ctl(
5506 mixer, &scarlett2_autogain_switch_ctl,
5507 i, 1, s, &private->autogain_ctls[i]);
5511 snprintf(s, sizeof(s), fmt, i + 1,
5512 "Autogain Status", "Enum");
5513 err = scarlett2_add_new_ctl(
5514 mixer, &scarlett2_autogain_status_ctl,
5515 i, 1, s, &private->autogain_status_ctls[i]);
5517 snprintf(s, sizeof(s), fmt, i + 1,
5519 err = scarlett2_add_new_ctl(
5520 mixer, &scarlett2_safe_ctl,
5521 i, 1, s, &private->safe_ctls[i]);
5527 /* Add PCM Input Switch control */
5528 if (scarlett2_has_config_item(private,
5529 SCARLETT2_CONFIG_PCM_INPUT_SWITCH)) {
5530 err = scarlett2_add_new_ctl(
5531 mixer, &scarlett2_pcm_input_switch_ctl, 0, 1,
5532 "PCM Input Capture Switch",
5533 &private->pcm_input_switch_ctl);
5541 /*** Mixer Volume Controls ***/
5543 static int scarlett2_update_mix(struct usb_mixer_interface *mixer)
5545 struct scarlett2_data *private = mixer->private_data;
5548 private->mix_updated = 0;
5550 for (i = 0; i < private->num_mix_out; i++) {
5551 err = scarlett2_usb_get_mix(mixer, i);
5559 static int scarlett2_mixer_ctl_info(struct snd_kcontrol *kctl,
5560 struct snd_ctl_elem_info *uinfo)
5562 struct usb_mixer_elem_info *elem = kctl->private_data;
5564 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
5565 uinfo->count = elem->channels;
5566 uinfo->value.integer.min = 0;
5567 uinfo->value.integer.max = SCARLETT2_MIXER_MAX_VALUE;
5568 uinfo->value.integer.step = 1;
5572 static int scarlett2_mixer_ctl_get(struct snd_kcontrol *kctl,
5573 struct snd_ctl_elem_value *ucontrol)
5575 struct usb_mixer_elem_info *elem = kctl->private_data;
5576 struct usb_mixer_interface *mixer = elem->head.mixer;
5577 struct scarlett2_data *private = mixer->private_data;
5580 mutex_lock(&private->data_mutex);
5582 if (private->hwdep_in_use) {
5587 if (private->mix_updated) {
5588 err = scarlett2_update_mix(mixer);
5592 ucontrol->value.integer.value[0] = private->mix[elem->control];
5595 mutex_unlock(&private->data_mutex);
5599 static int scarlett2_mixer_ctl_put(struct snd_kcontrol *kctl,
5600 struct snd_ctl_elem_value *ucontrol)
5602 struct usb_mixer_elem_info *elem = kctl->private_data;
5603 struct usb_mixer_interface *mixer = elem->head.mixer;
5604 struct scarlett2_data *private = mixer->private_data;
5605 int oval, val, mix_num, err = 0;
5606 int index = elem->control;
5608 mutex_lock(&private->data_mutex);
5610 if (private->hwdep_in_use) {
5615 oval = private->mix[index];
5616 val = clamp(ucontrol->value.integer.value[0],
5617 0L, (long)SCARLETT2_MIXER_MAX_VALUE);
5618 mix_num = index / private->num_mix_in;
5623 private->mix[index] = val;
5624 err = scarlett2_usb_set_mix(mixer, mix_num);
5629 mutex_unlock(&private->data_mutex);
5633 static const DECLARE_TLV_DB_MINMAX(
5634 db_scale_scarlett2_mixer,
5635 SCARLETT2_MIXER_MIN_DB * 100,
5636 SCARLETT2_MIXER_MAX_DB * 100
5639 static const struct snd_kcontrol_new scarlett2_mixer_ctl = {
5640 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
5641 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE |
5642 SNDRV_CTL_ELEM_ACCESS_TLV_READ,
5644 .info = scarlett2_mixer_ctl_info,
5645 .get = scarlett2_mixer_ctl_get,
5646 .put = scarlett2_mixer_ctl_put,
5647 .private_value = SCARLETT2_MIXER_MAX_DB, /* max value */
5648 .tlv = { .p = db_scale_scarlett2_mixer }
5651 static int scarlett2_add_mixer_ctls(struct usb_mixer_interface *mixer)
5653 struct scarlett2_data *private = mixer->private_data;
5656 char s[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
5658 for (i = 0, index = 0; i < private->num_mix_out; i++)
5659 for (j = 0; j < private->num_mix_in; j++, index++) {
5660 snprintf(s, sizeof(s),
5661 "Mix %c Input %02d Playback Volume",
5663 err = scarlett2_add_new_ctl(mixer, &scarlett2_mixer_ctl,
5665 &private->mix_ctls[index]);
5673 /*** Direct Monitor Control ***/
5675 static int scarlett2_update_direct_monitor(struct usb_mixer_interface *mixer)
5677 struct scarlett2_data *private = mixer->private_data;
5679 private->direct_monitor_updated = 0;
5681 if (!private->info->direct_monitor)
5684 return scarlett2_usb_get_config(
5685 mixer, SCARLETT2_CONFIG_DIRECT_MONITOR,
5686 1, &private->direct_monitor_switch);
5689 static int scarlett2_update_monitor_mix(struct usb_mixer_interface *mixer)
5691 struct scarlett2_data *private = mixer->private_data;
5693 u16 mix_values[SCARLETT2_MONITOR_MIX_MAX];
5695 if (!private->num_monitor_mix_ctls)
5698 err = scarlett2_usb_get_config(
5699 mixer, SCARLETT2_CONFIG_DIRECT_MONITOR_GAIN,
5700 private->num_monitor_mix_ctls, mix_values);
5704 for (i = 0; i < private->num_monitor_mix_ctls; i++)
5705 private->monitor_mix[i] = scarlett2_mixer_value_to_db(
5711 static int scarlett2_direct_monitor_ctl_get(
5712 struct snd_kcontrol *kctl, struct snd_ctl_elem_value *ucontrol)
5714 struct usb_mixer_elem_info *elem = kctl->private_data;
5715 struct usb_mixer_interface *mixer = elem->head.mixer;
5716 struct scarlett2_data *private = mixer->private_data;
5719 mutex_lock(&private->data_mutex);
5721 if (private->hwdep_in_use) {
5726 if (private->direct_monitor_updated) {
5727 err = scarlett2_update_direct_monitor(mixer);
5731 ucontrol->value.enumerated.item[0] = private->direct_monitor_switch;
5734 mutex_unlock(&private->data_mutex);
5738 static int scarlett2_direct_monitor_ctl_put(
5739 struct snd_kcontrol *kctl, struct snd_ctl_elem_value *ucontrol)
5741 struct usb_mixer_elem_info *elem = kctl->private_data;
5742 struct usb_mixer_interface *mixer = elem->head.mixer;
5743 struct scarlett2_data *private = mixer->private_data;
5745 int index = elem->control;
5746 int oval, val, err = 0;
5748 mutex_lock(&private->data_mutex);
5750 if (private->hwdep_in_use) {
5755 oval = private->direct_monitor_switch;
5756 val = min(ucontrol->value.enumerated.item[0], 2U);
5761 private->direct_monitor_switch = val;
5763 /* Send switch change to the device */
5764 err = scarlett2_usb_set_config(
5765 mixer, SCARLETT2_CONFIG_DIRECT_MONITOR, index, val);
5770 mutex_unlock(&private->data_mutex);
5774 static int scarlett2_direct_monitor_stereo_enum_ctl_info(
5775 struct snd_kcontrol *kctl, struct snd_ctl_elem_info *uinfo)
5777 static const char *const values[3] = {
5778 "Off", "Mono", "Stereo"
5781 return snd_ctl_enum_info(uinfo, 1, 3, values);
5784 /* Direct Monitor for Solo is mono-only and only needs a boolean control
5785 * Direct Monitor for 2i2 is selectable between Off/Mono/Stereo
5787 static const struct snd_kcontrol_new scarlett2_direct_monitor_ctl[2] = {
5789 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
5791 .info = snd_ctl_boolean_mono_info,
5792 .get = scarlett2_direct_monitor_ctl_get,
5793 .put = scarlett2_direct_monitor_ctl_put,
5796 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
5798 .info = scarlett2_direct_monitor_stereo_enum_ctl_info,
5799 .get = scarlett2_direct_monitor_ctl_get,
5800 .put = scarlett2_direct_monitor_ctl_put,
5804 static int scarlett2_monitor_mix_ctl_get(struct snd_kcontrol *kctl,
5805 struct snd_ctl_elem_value *ucontrol)
5807 struct usb_mixer_elem_info *elem = kctl->private_data;
5808 struct scarlett2_data *private = elem->head.mixer->private_data;
5810 ucontrol->value.integer.value[0] = private->monitor_mix[elem->control];
5815 static int scarlett2_monitor_mix_ctl_put(struct snd_kcontrol *kctl,
5816 struct snd_ctl_elem_value *ucontrol)
5818 struct usb_mixer_elem_info *elem = kctl->private_data;
5819 struct usb_mixer_interface *mixer = elem->head.mixer;
5820 struct scarlett2_data *private = mixer->private_data;
5821 int oval, val, err = 0;
5822 int index = elem->control;
5824 mutex_lock(&private->data_mutex);
5826 if (private->hwdep_in_use) {
5831 oval = private->monitor_mix[index];
5832 val = clamp(ucontrol->value.integer.value[0],
5833 0L, (long)SCARLETT2_MIXER_MAX_VALUE);
5838 private->monitor_mix[index] = val;
5839 err = scarlett2_usb_set_config(
5840 mixer, SCARLETT2_CONFIG_DIRECT_MONITOR_GAIN,
5841 index, scarlett2_mixer_values[val]);
5846 mutex_unlock(&private->data_mutex);
5850 static const struct snd_kcontrol_new scarlett2_monitor_mix_ctl = {
5851 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
5852 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE |
5853 SNDRV_CTL_ELEM_ACCESS_TLV_READ,
5855 .info = scarlett2_mixer_ctl_info,
5856 .get = scarlett2_monitor_mix_ctl_get,
5857 .put = scarlett2_monitor_mix_ctl_put,
5858 .private_value = SCARLETT2_MIXER_MAX_DB, /* max value */
5859 .tlv = { .p = db_scale_scarlett2_mixer }
5862 static int scarlett2_add_direct_monitor_ctls(struct usb_mixer_interface *mixer)
5864 struct scarlett2_data *private = mixer->private_data;
5865 const struct scarlett2_device_info *info = private->info;
5867 int err, i, j, k, index;
5869 if (!info->direct_monitor)
5872 s = info->direct_monitor == 1
5873 ? "Direct Monitor Playback Switch"
5874 : "Direct Monitor Playback Enum";
5876 err = scarlett2_add_new_ctl(
5877 mixer, &scarlett2_direct_monitor_ctl[info->direct_monitor - 1],
5878 0, 1, s, &private->direct_monitor_ctl);
5882 if (!private->num_monitor_mix_ctls)
5885 /* 1 or 2 direct monitor selections (Mono & Stereo) */
5886 for (i = 0, index = 0; i < info->direct_monitor; i++) {
5887 const char * const format =
5888 "Monitor %sMix %c Input %02d Playback Volume";
5889 const char *mix_type;
5891 if (info->direct_monitor == 1)
5898 /* 2 Mix outputs, A/Left & B/Right */
5899 for (j = 0; j < 2; j++)
5902 for (k = 0; k < private->num_mix_in; k++, index++) {
5903 char name[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
5905 snprintf(name, sizeof(name), format,
5906 mix_type, 'A' + j, k + 1);
5908 err = scarlett2_add_new_ctl(
5909 mixer, &scarlett2_monitor_mix_ctl,
5910 index, 1, name, NULL);
5919 /*** Mux Source Selection Controls ***/
5921 static int scarlett2_mux_src_enum_ctl_info(struct snd_kcontrol *kctl,
5922 struct snd_ctl_elem_info *uinfo)
5924 struct usb_mixer_elem_info *elem = kctl->private_data;
5925 struct scarlett2_data *private = elem->head.mixer->private_data;
5926 const struct scarlett2_device_info *info = private->info;
5927 const int (*port_count)[SCARLETT2_PORT_DIRNS] = info->port_count;
5928 unsigned int item = uinfo->value.enumerated.item;
5929 int items = private->num_mux_srcs;
5932 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
5933 uinfo->count = elem->channels;
5934 uinfo->value.enumerated.items = items;
5937 item = uinfo->value.enumerated.item = items - 1;
5940 port_type < SCARLETT2_PORT_TYPE_COUNT;
5942 if (item < port_count[port_type][SCARLETT2_PORT_IN]) {
5943 const struct scarlett2_port *port =
5944 &scarlett2_ports[port_type];
5946 if (port_type == SCARLETT2_PORT_TYPE_MIX &&
5947 item >= private->num_mix_out)
5948 sprintf(uinfo->value.enumerated.name,
5949 port->dsp_src_descr,
5950 item - private->num_mix_out + 1);
5952 sprintf(uinfo->value.enumerated.name,
5954 item + port->src_num_offset);
5958 item -= port_count[port_type][SCARLETT2_PORT_IN];
5964 static int scarlett2_mux_src_enum_ctl_get(struct snd_kcontrol *kctl,
5965 struct snd_ctl_elem_value *ucontrol)
5967 struct usb_mixer_elem_info *elem = kctl->private_data;
5968 struct usb_mixer_interface *mixer = elem->head.mixer;
5969 struct scarlett2_data *private = mixer->private_data;
5970 int index = line_out_remap(private, elem->control);
5973 mutex_lock(&private->data_mutex);
5975 if (private->hwdep_in_use) {
5980 if (private->mux_updated) {
5981 err = scarlett2_usb_get_mux(mixer);
5985 ucontrol->value.enumerated.item[0] = private->mux[index];
5988 mutex_unlock(&private->data_mutex);
5992 static int scarlett2_mux_src_enum_ctl_put(struct snd_kcontrol *kctl,
5993 struct snd_ctl_elem_value *ucontrol)
5995 struct usb_mixer_elem_info *elem = kctl->private_data;
5996 struct usb_mixer_interface *mixer = elem->head.mixer;
5997 struct scarlett2_data *private = mixer->private_data;
5998 int index = line_out_remap(private, elem->control);
5999 int oval, val, err = 0;
6001 mutex_lock(&private->data_mutex);
6003 if (private->hwdep_in_use) {
6008 oval = private->mux[index];
6009 val = min(ucontrol->value.enumerated.item[0],
6010 private->num_mux_srcs - 1U);
6015 private->mux[index] = val;
6016 err = scarlett2_usb_set_mux(mixer);
6021 mutex_unlock(&private->data_mutex);
6025 static const struct snd_kcontrol_new scarlett2_mux_src_enum_ctl = {
6026 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
6028 .info = scarlett2_mux_src_enum_ctl_info,
6029 .get = scarlett2_mux_src_enum_ctl_get,
6030 .put = scarlett2_mux_src_enum_ctl_put,
6033 static int scarlett2_add_mux_enums(struct usb_mixer_interface *mixer)
6035 struct scarlett2_data *private = mixer->private_data;
6036 const struct scarlett2_device_info *info = private->info;
6037 const int (*port_count)[SCARLETT2_PORT_DIRNS] = info->port_count;
6038 int port_type, channel, i;
6040 for (i = 0, port_type = 0;
6041 port_type < SCARLETT2_PORT_TYPE_COUNT;
6044 channel < port_count[port_type][SCARLETT2_PORT_OUT];
6047 char s[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
6048 int channel_num = channel + 1;
6049 const struct scarlett2_port *port =
6050 &scarlett2_ports[port_type];
6051 const char *descr = port->dst_descr;
6053 if (port_type == SCARLETT2_PORT_TYPE_MIX &&
6054 channel >= private->num_mix_in) {
6055 channel_num -= private->num_mix_in;
6056 descr = port->dsp_dst_descr;
6059 snprintf(s, sizeof(s) - 5, descr, channel_num);
6062 err = scarlett2_add_new_ctl(mixer,
6063 &scarlett2_mux_src_enum_ctl,
6065 &private->mux_ctls[i]);
6074 /*** Meter Controls ***/
6076 static int scarlett2_meter_ctl_info(struct snd_kcontrol *kctl,
6077 struct snd_ctl_elem_info *uinfo)
6079 struct usb_mixer_elem_info *elem = kctl->private_data;
6081 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
6082 uinfo->count = elem->channels;
6083 uinfo->value.integer.min = 0;
6084 uinfo->value.integer.max = 4095;
6085 uinfo->value.integer.step = 1;
6089 static int scarlett2_meter_ctl_get(struct snd_kcontrol *kctl,
6090 struct snd_ctl_elem_value *ucontrol)
6092 struct usb_mixer_elem_info *elem = kctl->private_data;
6093 struct usb_mixer_interface *mixer = elem->head.mixer;
6094 struct scarlett2_data *private = mixer->private_data;
6095 u8 *meter_level_map = private->meter_level_map;
6096 u16 meter_levels[SCARLETT2_MAX_METERS];
6099 mutex_lock(&private->data_mutex);
6101 if (private->hwdep_in_use) {
6106 err = scarlett2_usb_get_meter_levels(mixer, elem->channels,
6111 /* copy & translate from meter_levels[] using meter_level_map[] */
6112 for (i = 0; i < elem->channels; i++) {
6113 int idx = meter_level_map[i];
6119 value = meter_levels[idx];
6121 ucontrol->value.integer.value[i] = value;
6125 mutex_unlock(&private->data_mutex);
6130 static const struct snd_kcontrol_new scarlett2_meter_ctl = {
6131 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
6132 .access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE,
6134 .info = scarlett2_meter_ctl_info,
6135 .get = scarlett2_meter_ctl_get
6138 static int scarlett2_add_meter_ctl(struct usb_mixer_interface *mixer)
6140 struct scarlett2_data *private = mixer->private_data;
6142 /* devices without a mixer also don't support reporting levels */
6143 if (!scarlett2_has_mixer(private))
6146 return scarlett2_add_new_ctl(mixer, &scarlett2_meter_ctl,
6147 0, private->num_mux_dsts,
6148 "Level Meter", NULL);
6151 /*** MSD Controls ***/
6153 static int scarlett2_msd_ctl_get(struct snd_kcontrol *kctl,
6154 struct snd_ctl_elem_value *ucontrol)
6156 struct usb_mixer_elem_info *elem = kctl->private_data;
6157 struct scarlett2_data *private = elem->head.mixer->private_data;
6159 ucontrol->value.integer.value[0] = private->msd_switch;
6163 static int scarlett2_msd_ctl_put(struct snd_kcontrol *kctl,
6164 struct snd_ctl_elem_value *ucontrol)
6166 struct usb_mixer_elem_info *elem = kctl->private_data;
6167 struct usb_mixer_interface *mixer = elem->head.mixer;
6168 struct scarlett2_data *private = mixer->private_data;
6170 int oval, val, err = 0;
6172 mutex_lock(&private->data_mutex);
6174 if (private->hwdep_in_use) {
6179 oval = private->msd_switch;
6180 val = !!ucontrol->value.integer.value[0];
6185 private->msd_switch = val;
6187 /* Send switch change to the device */
6188 err = scarlett2_usb_set_config(mixer, SCARLETT2_CONFIG_MSD_SWITCH,
6194 mutex_unlock(&private->data_mutex);
6198 static const struct snd_kcontrol_new scarlett2_msd_ctl = {
6199 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
6201 .info = snd_ctl_boolean_mono_info,
6202 .get = scarlett2_msd_ctl_get,
6203 .put = scarlett2_msd_ctl_put,
6206 static int scarlett2_add_msd_ctl(struct usb_mixer_interface *mixer)
6208 struct scarlett2_data *private = mixer->private_data;
6210 if (!scarlett2_has_config_item(private, SCARLETT2_CONFIG_MSD_SWITCH))
6213 /* If MSD mode is off, hide the switch by default */
6214 if (!private->msd_switch && !(mixer->chip->setup & SCARLETT2_MSD_ENABLE))
6217 /* Add MSD control */
6218 return scarlett2_add_new_ctl(mixer, &scarlett2_msd_ctl,
6219 0, 1, "MSD Mode Switch", NULL);
6222 /*** Standalone Control ***/
6224 static int scarlett2_standalone_ctl_get(struct snd_kcontrol *kctl,
6225 struct snd_ctl_elem_value *ucontrol)
6227 struct usb_mixer_elem_info *elem = kctl->private_data;
6228 struct scarlett2_data *private = elem->head.mixer->private_data;
6230 ucontrol->value.integer.value[0] = private->standalone_switch;
6234 static int scarlett2_standalone_ctl_put(struct snd_kcontrol *kctl,
6235 struct snd_ctl_elem_value *ucontrol)
6237 struct usb_mixer_elem_info *elem = kctl->private_data;
6238 struct usb_mixer_interface *mixer = elem->head.mixer;
6239 struct scarlett2_data *private = mixer->private_data;
6241 int oval, val, err = 0;
6243 mutex_lock(&private->data_mutex);
6245 if (private->hwdep_in_use) {
6250 oval = private->standalone_switch;
6251 val = !!ucontrol->value.integer.value[0];
6256 private->standalone_switch = val;
6258 /* Send switch change to the device */
6259 err = scarlett2_usb_set_config(mixer,
6260 SCARLETT2_CONFIG_STANDALONE_SWITCH,
6266 mutex_unlock(&private->data_mutex);
6270 static const struct snd_kcontrol_new scarlett2_standalone_ctl = {
6271 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
6273 .info = snd_ctl_boolean_mono_info,
6274 .get = scarlett2_standalone_ctl_get,
6275 .put = scarlett2_standalone_ctl_put,
6278 static int scarlett2_add_standalone_ctl(struct usb_mixer_interface *mixer)
6280 struct scarlett2_data *private = mixer->private_data;
6282 if (!scarlett2_has_config_item(private,
6283 SCARLETT2_CONFIG_STANDALONE_SWITCH))
6286 /* Add standalone control */
6287 return scarlett2_add_new_ctl(mixer, &scarlett2_standalone_ctl,
6288 0, 1, "Standalone Switch", NULL);
6291 /*** Power Status ***/
6293 static int scarlett2_update_power_status(struct usb_mixer_interface *mixer)
6295 struct scarlett2_data *private = mixer->private_data;
6300 private->power_status_updated = 0;
6302 err = scarlett2_usb_get_config(mixer, SCARLETT2_CONFIG_POWER_EXT,
6307 err = scarlett2_usb_get_config(mixer, SCARLETT2_CONFIG_POWER_STATUS,
6312 if (power_status > 1)
6313 private->power_status = SCARLETT2_POWER_STATUS_FAIL;
6315 private->power_status = SCARLETT2_POWER_STATUS_EXT;
6317 private->power_status = SCARLETT2_POWER_STATUS_BUS;
6322 static int scarlett2_power_status_ctl_get(struct snd_kcontrol *kctl,
6323 struct snd_ctl_elem_value *ucontrol)
6325 struct usb_mixer_elem_info *elem = kctl->private_data;
6326 struct usb_mixer_interface *mixer = elem->head.mixer;
6327 struct scarlett2_data *private = mixer->private_data;
6330 mutex_lock(&private->data_mutex);
6332 if (private->power_status_updated) {
6333 err = scarlett2_update_power_status(mixer);
6337 ucontrol->value.integer.value[0] = private->power_status;
6340 mutex_unlock(&private->data_mutex);
6344 static int scarlett2_power_status_ctl_info(
6345 struct snd_kcontrol *kctl, struct snd_ctl_elem_info *uinfo)
6347 static const char *const values[3] = {
6348 "External", "Bus", "Fail"
6351 return snd_ctl_enum_info(uinfo, 1, 3, values);
6354 static const struct snd_kcontrol_new scarlett2_power_status_ctl = {
6355 .iface = SNDRV_CTL_ELEM_IFACE_CARD,
6356 .access = SNDRV_CTL_ELEM_ACCESS_READ,
6358 .info = scarlett2_power_status_ctl_info,
6359 .get = scarlett2_power_status_ctl_get,
6362 static int scarlett2_add_power_status_ctl(struct usb_mixer_interface *mixer)
6364 struct scarlett2_data *private = mixer->private_data;
6366 if (!scarlett2_has_config_item(private,
6367 SCARLETT2_CONFIG_POWER_EXT))
6370 /* Add power status control */
6371 return scarlett2_add_new_ctl(mixer, &scarlett2_power_status_ctl,
6372 0, 1, "Power Status Card Enum",
6373 &private->power_status_ctl);
6376 /*** Cleanup/Suspend Callbacks ***/
6378 static void scarlett2_private_free(struct usb_mixer_interface *mixer)
6380 struct scarlett2_data *private = mixer->private_data;
6382 cancel_delayed_work_sync(&private->work);
6384 mixer->private_data = NULL;
6387 static void scarlett2_private_suspend(struct usb_mixer_interface *mixer)
6389 struct scarlett2_data *private = mixer->private_data;
6391 if (cancel_delayed_work_sync(&private->work))
6392 scarlett2_config_save(private->mixer);
6395 /*** Initialisation ***/
6397 static void scarlett2_count_io(struct scarlett2_data *private)
6399 const struct scarlett2_device_info *info = private->info;
6400 const int (*port_count)[SCARLETT2_PORT_DIRNS] = info->port_count;
6401 int port_type, srcs = 0, dsts = 0;
6403 /* Count the number of mux sources and destinations */
6405 port_type < SCARLETT2_PORT_TYPE_COUNT;
6407 srcs += port_count[port_type][SCARLETT2_PORT_IN];
6408 dsts += port_count[port_type][SCARLETT2_PORT_OUT];
6411 private->num_mux_srcs = srcs;
6412 private->num_mux_dsts = dsts;
6414 /* Mixer inputs are mux outputs and vice versa.
6415 * Scarlett Gen 4 DSP I/O uses SCARLETT2_PORT_TYPE_MIX but
6416 * doesn't have mixer controls.
6418 private->num_mix_in =
6419 port_count[SCARLETT2_PORT_TYPE_MIX][SCARLETT2_PORT_OUT] -
6422 private->num_mix_out =
6423 port_count[SCARLETT2_PORT_TYPE_MIX][SCARLETT2_PORT_IN] -
6426 /* Number of analogue line outputs */
6427 private->num_line_out =
6428 port_count[SCARLETT2_PORT_TYPE_ANALOGUE][SCARLETT2_PORT_OUT];
6430 /* Number of monitor mix controls */
6431 private->num_monitor_mix_ctls =
6432 info->direct_monitor * 2 * private->num_mix_in;
6435 /* Look through the interface descriptors for the Focusrite Control
6436 * interface (bInterfaceClass = 255 Vendor Specific Class) and set
6437 * bInterfaceNumber, bEndpointAddress, wMaxPacketSize, and bInterval
6440 static int scarlett2_find_fc_interface(struct usb_device *dev,
6441 struct scarlett2_data *private)
6443 struct usb_host_config *config = dev->actconfig;
6446 for (i = 0; i < config->desc.bNumInterfaces; i++) {
6447 struct usb_interface *intf = config->interface[i];
6448 struct usb_interface_descriptor *desc =
6449 &intf->altsetting[0].desc;
6450 struct usb_endpoint_descriptor *epd;
6452 if (desc->bInterfaceClass != 255)
6455 epd = get_endpoint(intf->altsetting, 0);
6456 private->bInterfaceNumber = desc->bInterfaceNumber;
6457 private->bEndpointAddress = epd->bEndpointAddress &
6458 USB_ENDPOINT_NUMBER_MASK;
6459 private->wMaxPacketSize = le16_to_cpu(epd->wMaxPacketSize);
6460 private->bInterval = epd->bInterval;
6467 /* Initialise private data */
6468 static int scarlett2_init_private(struct usb_mixer_interface *mixer,
6469 const struct scarlett2_device_entry *entry)
6471 struct scarlett2_data *private =
6472 kzalloc(sizeof(struct scarlett2_data), GFP_KERNEL);
6477 mutex_init(&private->usb_mutex);
6478 mutex_init(&private->data_mutex);
6479 INIT_DELAYED_WORK(&private->work, scarlett2_config_save_work);
6481 mixer->private_data = private;
6482 mixer->private_free = scarlett2_private_free;
6483 mixer->private_suspend = scarlett2_private_suspend;
6485 private->info = entry->info;
6486 private->config_set = entry->info->config_set;
6487 private->series_name = entry->series_name;
6488 scarlett2_count_io(private);
6489 private->scarlett2_seq = 0;
6490 private->mixer = mixer;
6492 return scarlett2_find_fc_interface(mixer->chip->dev, private);
6495 /* Cargo cult proprietary initialisation sequence */
6496 static int scarlett2_usb_init(struct usb_mixer_interface *mixer)
6498 struct usb_device *dev = mixer->chip->dev;
6499 struct scarlett2_data *private = mixer->private_data;
6504 if (usb_pipe_type_check(dev, usb_sndctrlpipe(dev, 0)))
6508 err = scarlett2_usb_rx(dev, private->bInterfaceNumber,
6509 SCARLETT2_USB_CMD_INIT,
6510 step0_buf, sizeof(step0_buf));
6515 private->scarlett2_seq = 1;
6516 err = scarlett2_usb(mixer, SCARLETT2_USB_INIT_1, NULL, 0, NULL, 0);
6521 private->scarlett2_seq = 1;
6522 err = scarlett2_usb(mixer, SCARLETT2_USB_INIT_2,
6524 step2_buf, sizeof(step2_buf));
6528 /* extract 4-byte firmware version from step2_buf[8] */
6529 private->firmware_version = le32_to_cpu(*(__le32 *)(step2_buf + 8));
6530 usb_audio_info(mixer->chip,
6531 "Firmware version %d\n",
6532 private->firmware_version);
6537 /* Get the flash segment numbers for the App_Settings and App_Upgrade
6538 * segments and put them in the private data
6540 static int scarlett2_get_flash_segment_nums(struct usb_mixer_interface *mixer)
6542 struct scarlett2_data *private = mixer->private_data;
6549 } __packed flash_info;
6555 } __packed segment_info;
6557 err = scarlett2_usb(mixer, SCARLETT2_USB_INFO_FLASH,
6559 &flash_info, sizeof(flash_info));
6563 count = le32_to_cpu(flash_info.count);
6565 /* sanity check count */
6566 if (count < SCARLETT2_SEGMENT_NUM_MIN ||
6567 count > SCARLETT2_SEGMENT_NUM_MAX + 1) {
6568 usb_audio_err(mixer->chip,
6569 "invalid flash segment count: %d\n", count);
6573 for (i = 0; i < count; i++) {
6574 __le32 segment_num_req = cpu_to_le32(i);
6575 int flash_segment_id;
6577 err = scarlett2_usb(mixer, SCARLETT2_USB_INFO_SEGMENT,
6578 &segment_num_req, sizeof(segment_num_req),
6579 &segment_info, sizeof(segment_info));
6581 usb_audio_err(mixer->chip,
6582 "failed to get flash segment info %d: %d\n",
6587 if (!strncmp(segment_info.name,
6588 SCARLETT2_SEGMENT_SETTINGS_NAME, 16))
6589 flash_segment_id = SCARLETT2_SEGMENT_ID_SETTINGS;
6590 else if (!strncmp(segment_info.name,
6591 SCARLETT2_SEGMENT_FIRMWARE_NAME, 16))
6592 flash_segment_id = SCARLETT2_SEGMENT_ID_FIRMWARE;
6596 private->flash_segment_nums[flash_segment_id] = i;
6597 private->flash_segment_blocks[flash_segment_id] =
6598 le32_to_cpu(segment_info.size) /
6599 SCARLETT2_FLASH_BLOCK_SIZE;
6602 /* segment 0 is App_Gold and we never want to touch that, so
6603 * use 0 as the "not-found" value
6605 if (!private->flash_segment_nums[SCARLETT2_SEGMENT_ID_SETTINGS]) {
6606 usb_audio_err(mixer->chip,
6607 "failed to find flash segment %s\n",
6608 SCARLETT2_SEGMENT_SETTINGS_NAME);
6611 if (!private->flash_segment_nums[SCARLETT2_SEGMENT_ID_FIRMWARE]) {
6612 usb_audio_err(mixer->chip,
6613 "failed to find flash segment %s\n",
6614 SCARLETT2_SEGMENT_FIRMWARE_NAME);
6621 /* Read configuration from the interface on start */
6622 static int scarlett2_read_configs(struct usb_mixer_interface *mixer)
6624 struct scarlett2_data *private = mixer->private_data;
6625 const struct scarlett2_device_info *info = private->info;
6628 if (scarlett2_has_config_item(private, SCARLETT2_CONFIG_MSD_SWITCH)) {
6629 err = scarlett2_usb_get_config(
6630 mixer, SCARLETT2_CONFIG_MSD_SWITCH,
6631 1, &private->msd_switch);
6636 if (private->firmware_version < info->min_firmware_version) {
6637 usb_audio_err(mixer->chip,
6638 "Focusrite %s firmware version %d is too old; "
6640 private->series_name,
6641 private->firmware_version,
6642 info->min_firmware_version);
6646 /* no other controls are created if MSD mode is on */
6647 if (private->msd_switch)
6650 err = scarlett2_update_input_level(mixer);
6654 err = scarlett2_update_input_pad(mixer);
6658 err = scarlett2_update_input_air(mixer);
6662 err = scarlett2_update_input_phantom(mixer);
6666 err = scarlett2_update_direct_monitor(mixer);
6670 /* the rest of the configuration is for devices with a mixer */
6671 if (!scarlett2_has_mixer(private))
6674 err = scarlett2_update_monitor_mix(mixer);
6678 err = scarlett2_update_monitor_other(mixer);
6682 if (scarlett2_has_config_item(private,
6683 SCARLETT2_CONFIG_STANDALONE_SWITCH)) {
6684 err = scarlett2_usb_get_config(
6685 mixer, SCARLETT2_CONFIG_STANDALONE_SWITCH,
6686 1, &private->standalone_switch);
6691 if (scarlett2_has_config_item(private,
6692 SCARLETT2_CONFIG_POWER_EXT)) {
6693 err = scarlett2_update_power_status(mixer);
6698 err = scarlett2_update_sync(mixer);
6702 if (scarlett2_has_config_item(private,
6703 SCARLETT2_CONFIG_LINE_OUT_VOLUME)) {
6704 s16 sw_vol[SCARLETT2_ANALOGUE_MAX];
6706 /* read SW line out volume */
6707 err = scarlett2_usb_get_config(
6708 mixer, SCARLETT2_CONFIG_LINE_OUT_VOLUME,
6709 private->num_line_out, &sw_vol);
6713 for (i = 0; i < private->num_line_out; i++)
6714 private->vol[i] = clamp(
6715 sw_vol[i] + SCARLETT2_VOLUME_BIAS,
6716 0, SCARLETT2_VOLUME_BIAS);
6719 err = scarlett2_usb_get_config(
6720 mixer, SCARLETT2_CONFIG_MUTE_SWITCH,
6721 private->num_line_out, &private->mute_switch);
6725 for (i = 0; i < private->num_line_out; i++)
6726 private->mute_switch[i] =
6727 !!private->mute_switch[i];
6729 /* read SW/HW switches */
6730 if (scarlett2_has_config_item(private,
6731 SCARLETT2_CONFIG_SW_HW_SWITCH)) {
6732 err = scarlett2_usb_get_config(
6733 mixer, SCARLETT2_CONFIG_SW_HW_SWITCH,
6734 private->num_line_out,
6735 &private->vol_sw_hw_switch);
6739 for (i = 0; i < private->num_line_out; i++)
6740 private->vol_sw_hw_switch[i] =
6741 !!private->vol_sw_hw_switch[i];
6745 err = scarlett2_update_volumes(mixer);
6749 err = scarlett2_update_dim_mute(mixer);
6753 err = scarlett2_update_input_select(mixer);
6757 err = scarlett2_update_input_gain(mixer);
6761 err = scarlett2_update_autogain(mixer);
6765 err = scarlett2_update_input_safe(mixer);
6769 if (scarlett2_has_config_item(private,
6770 SCARLETT2_CONFIG_PCM_INPUT_SWITCH)) {
6771 err = scarlett2_update_pcm_input_switch(mixer);
6776 err = scarlett2_update_mix(mixer);
6780 return scarlett2_usb_get_mux(mixer);
6783 /* Notify on sync change */
6784 static void scarlett2_notify_sync(struct usb_mixer_interface *mixer)
6786 struct scarlett2_data *private = mixer->private_data;
6788 private->sync_updated = 1;
6790 snd_ctl_notify(mixer->chip->card, SNDRV_CTL_EVENT_MASK_VALUE,
6791 &private->sync_ctl->id);
6794 /* Notify on monitor change (Gen 2/3) */
6795 static void scarlett2_notify_monitor(struct usb_mixer_interface *mixer)
6797 struct snd_card *card = mixer->chip->card;
6798 struct scarlett2_data *private = mixer->private_data;
6801 if (!scarlett2_has_config_item(private, SCARLETT2_CONFIG_SW_HW_SWITCH))
6804 private->vol_updated = 1;
6806 snd_ctl_notify(mixer->chip->card, SNDRV_CTL_EVENT_MASK_VALUE,
6807 &private->master_vol_ctl->id);
6809 for (i = 0; i < private->num_line_out; i++)
6810 if (private->vol_sw_hw_switch[line_out_remap(private, i)])
6811 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE,
6812 &private->vol_ctls[i]->id);
6815 /* Notify on volume change (Gen 4) */
6816 static void scarlett2_notify_volume(struct usb_mixer_interface *mixer)
6818 struct scarlett2_data *private = mixer->private_data;
6820 private->vol_updated = 1;
6822 snd_ctl_notify(mixer->chip->card, SNDRV_CTL_EVENT_MASK_VALUE,
6823 &private->master_vol_ctl->id);
6824 snd_ctl_notify(mixer->chip->card, SNDRV_CTL_EVENT_MASK_VALUE,
6825 &private->headphone_vol_ctl->id);
6828 /* Notify on dim/mute change */
6829 static void scarlett2_notify_dim_mute(struct usb_mixer_interface *mixer)
6831 struct snd_card *card = mixer->chip->card;
6832 struct scarlett2_data *private = mixer->private_data;
6835 if (!scarlett2_has_config_item(private, SCARLETT2_CONFIG_SW_HW_SWITCH))
6838 private->dim_mute_updated = 1;
6840 for (i = 0; i < SCARLETT2_DIM_MUTE_COUNT; i++)
6841 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE,
6842 &private->dim_mute_ctls[i]->id);
6844 for (i = 0; i < private->num_line_out; i++)
6845 if (private->vol_sw_hw_switch[line_out_remap(private, i)])
6846 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE,
6847 &private->mute_ctls[i]->id);
6850 /* Notify on input level switch change */
6851 static void scarlett2_notify_input_level(struct usb_mixer_interface *mixer)
6853 struct snd_card *card = mixer->chip->card;
6854 struct scarlett2_data *private = mixer->private_data;
6855 const struct scarlett2_device_info *info = private->info;
6858 private->input_level_updated = 1;
6860 for (i = 0; i < info->level_input_count; i++)
6861 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE,
6862 &private->level_ctls[i]->id);
6865 /* Notify on input pad switch change */
6866 static void scarlett2_notify_input_pad(struct usb_mixer_interface *mixer)
6868 struct snd_card *card = mixer->chip->card;
6869 struct scarlett2_data *private = mixer->private_data;
6870 const struct scarlett2_device_info *info = private->info;
6873 private->input_pad_updated = 1;
6875 for (i = 0; i < info->pad_input_count; i++)
6876 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE,
6877 &private->pad_ctls[i]->id);
6880 /* Notify on input air switch change */
6881 static void scarlett2_notify_input_air(struct usb_mixer_interface *mixer)
6883 struct snd_card *card = mixer->chip->card;
6884 struct scarlett2_data *private = mixer->private_data;
6885 const struct scarlett2_device_info *info = private->info;
6888 private->input_air_updated = 1;
6890 for (i = 0; i < info->air_input_count; i++)
6891 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE,
6892 &private->air_ctls[i]->id);
6895 /* Notify on input phantom switch change */
6896 static void scarlett2_notify_input_phantom(struct usb_mixer_interface *mixer)
6898 struct snd_card *card = mixer->chip->card;
6899 struct scarlett2_data *private = mixer->private_data;
6900 const struct scarlett2_device_info *info = private->info;
6903 private->input_phantom_updated = 1;
6905 for (i = 0; i < info->phantom_count; i++)
6906 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE,
6907 &private->phantom_ctls[i]->id);
6909 scarlett2_phantom_notify_access(mixer);
6912 /* Notify on "input other" change (level/pad/air/phantom) */
6913 static void scarlett2_notify_input_other(struct usb_mixer_interface *mixer)
6915 scarlett2_notify_input_level(mixer);
6916 scarlett2_notify_input_pad(mixer);
6917 scarlett2_notify_input_air(mixer);
6918 scarlett2_notify_input_phantom(mixer);
6921 /* Notify on input select change */
6922 static void scarlett2_notify_input_select(struct usb_mixer_interface *mixer)
6924 struct snd_card *card = mixer->chip->card;
6925 struct scarlett2_data *private = mixer->private_data;
6926 const struct scarlett2_device_info *info = private->info;
6929 if (!info->gain_input_count)
6932 private->input_select_updated = 1;
6934 snd_ctl_notify(card,
6935 SNDRV_CTL_EVENT_MASK_VALUE | SNDRV_CTL_EVENT_MASK_INFO,
6936 &private->input_select_ctl->id);
6938 for (i = 0; i < info->gain_input_count / 2; i++)
6939 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE,
6940 &private->input_link_ctls[i]->id);
6943 /* Notify on input gain change */
6944 static void scarlett2_notify_input_gain(struct usb_mixer_interface *mixer)
6946 struct snd_card *card = mixer->chip->card;
6947 struct scarlett2_data *private = mixer->private_data;
6948 const struct scarlett2_device_info *info = private->info;
6951 if (!info->gain_input_count)
6954 private->input_gain_updated = 1;
6956 for (i = 0; i < info->gain_input_count; i++)
6957 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE,
6958 &private->input_gain_ctls[i]->id);
6961 /* Notify on autogain change */
6962 static void scarlett2_notify_autogain(struct usb_mixer_interface *mixer)
6964 struct snd_card *card = mixer->chip->card;
6965 struct scarlett2_data *private = mixer->private_data;
6966 const struct scarlett2_device_info *info = private->info;
6969 if (!info->gain_input_count)
6972 private->autogain_updated = 1;
6974 for (i = 0; i < info->gain_input_count; i++) {
6975 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE,
6976 &private->autogain_ctls[i]->id);
6977 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE,
6978 &private->autogain_status_ctls[i]->id);
6981 scarlett2_autogain_notify_access(mixer);
6984 /* Notify on input safe switch change */
6985 static void scarlett2_notify_input_safe(struct usb_mixer_interface *mixer)
6987 struct snd_card *card = mixer->chip->card;
6988 struct scarlett2_data *private = mixer->private_data;
6989 const struct scarlett2_device_info *info = private->info;
6992 if (!info->gain_input_count)
6995 private->input_safe_updated = 1;
6997 for (i = 0; i < info->gain_input_count; i++)
6998 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE,
6999 &private->safe_ctls[i]->id);
7002 /* Notify on "monitor other" change (speaker switching, talkback) */
7003 static void scarlett2_notify_monitor_other(struct usb_mixer_interface *mixer)
7005 struct snd_card *card = mixer->chip->card;
7006 struct scarlett2_data *private = mixer->private_data;
7007 const struct scarlett2_device_info *info = private->info;
7009 private->monitor_other_updated = 1;
7011 if (info->has_speaker_switching)
7012 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE,
7013 &private->speaker_switching_ctl->id);
7015 if (info->has_talkback)
7016 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE,
7017 &private->talkback_ctl->id);
7019 /* if speaker switching was recently enabled or disabled,
7020 * invalidate the dim/mute and mux enum controls
7022 if (private->speaker_switching_switched) {
7025 scarlett2_notify_dim_mute(mixer);
7027 private->speaker_switching_switched = 0;
7028 private->mux_updated = 1;
7030 for (i = 0; i < private->num_mux_dsts; i++)
7031 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE,
7032 &private->mux_ctls[i]->id);
7036 /* Notify on direct monitor switch change */
7037 static void scarlett2_notify_direct_monitor(struct usb_mixer_interface *mixer)
7039 struct snd_card *card = mixer->chip->card;
7040 struct scarlett2_data *private = mixer->private_data;
7041 int count = private->num_mix_in * private->num_mix_out;
7044 private->direct_monitor_updated = 1;
7046 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE,
7047 &private->direct_monitor_ctl->id);
7049 if (!scarlett2_has_mixer(private))
7052 private->mix_updated = 1;
7054 /* Notify of change to the mix controls */
7055 for (i = 0; i < count; i++)
7056 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE,
7057 &private->mix_ctls[i]->id);
7060 /* Notify on power change */
7061 static void scarlett2_notify_power_status(struct usb_mixer_interface *mixer)
7063 struct snd_card *card = mixer->chip->card;
7064 struct scarlett2_data *private = mixer->private_data;
7066 private->power_status_updated = 1;
7068 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE,
7069 &private->power_status_ctl->id);
7072 /* Notify on mux change */
7073 static void scarlett2_notify_mux(struct usb_mixer_interface *mixer)
7075 struct snd_card *card = mixer->chip->card;
7076 struct scarlett2_data *private = mixer->private_data;
7079 private->mux_updated = 1;
7081 for (i = 0; i < private->num_mux_dsts; i++)
7082 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE,
7083 &private->mux_ctls[i]->id);
7086 /* Notify on PCM input switch change */
7087 static void scarlett2_notify_pcm_input_switch(struct usb_mixer_interface *mixer)
7089 struct snd_card *card = mixer->chip->card;
7090 struct scarlett2_data *private = mixer->private_data;
7092 private->pcm_input_switch_updated = 1;
7094 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE,
7095 &private->pcm_input_switch_ctl->id);
7097 scarlett2_notify_mux(mixer);
7100 /* Interrupt callback */
7101 static void scarlett2_notify(struct urb *urb)
7103 struct usb_mixer_interface *mixer = urb->context;
7104 int len = urb->actual_length;
7105 int ustatus = urb->status;
7107 struct scarlett2_data *private = mixer->private_data;
7108 const struct scarlett2_notification *notifications =
7109 private->config_set->notifications;
7111 if (ustatus != 0 || len != 8)
7114 data = le32_to_cpu(*(__le32 *)urb->transfer_buffer);
7116 while (data && notifications->mask) {
7117 if (data & notifications->mask) {
7118 data &= ~notifications->mask;
7119 if (notifications->func)
7120 notifications->func(mixer);
7126 usb_audio_warn(mixer->chip,
7127 "%s: Unhandled notification: 0x%08x\n",
7131 if (ustatus != -ENOENT &&
7132 ustatus != -ECONNRESET &&
7133 ustatus != -ESHUTDOWN) {
7134 urb->dev = mixer->chip->dev;
7135 usb_submit_urb(urb, GFP_ATOMIC);
7139 static int scarlett2_init_notify(struct usb_mixer_interface *mixer)
7141 struct usb_device *dev = mixer->chip->dev;
7142 struct scarlett2_data *private = mixer->private_data;
7143 unsigned int pipe = usb_rcvintpipe(dev, private->bEndpointAddress);
7144 void *transfer_buffer;
7147 usb_audio_err(mixer->chip,
7148 "%s: mixer urb already in use!\n", __func__);
7152 if (usb_pipe_type_check(dev, pipe))
7155 mixer->urb = usb_alloc_urb(0, GFP_KERNEL);
7159 transfer_buffer = kmalloc(private->wMaxPacketSize, GFP_KERNEL);
7160 if (!transfer_buffer)
7163 usb_fill_int_urb(mixer->urb, dev, pipe,
7164 transfer_buffer, private->wMaxPacketSize,
7165 scarlett2_notify, mixer, private->bInterval);
7167 return usb_submit_urb(mixer->urb, GFP_KERNEL);
7170 static const struct scarlett2_device_entry *get_scarlett2_device_entry(
7171 struct usb_mixer_interface *mixer)
7173 const struct scarlett2_device_entry *entry = scarlett2_devices;
7175 /* Find entry in scarlett2_devices */
7176 while (entry->usb_id && entry->usb_id != mixer->chip->usb_id)
7184 static int snd_scarlett2_controls_create(
7185 struct usb_mixer_interface *mixer,
7186 const struct scarlett2_device_entry *entry)
7188 struct scarlett2_data *private;
7191 /* Initialise private data */
7192 err = scarlett2_init_private(mixer, entry);
7196 private = mixer->private_data;
7198 /* Send proprietary USB initialisation sequence */
7199 err = scarlett2_usb_init(mixer);
7203 /* Get the upgrade & settings flash segment numbers */
7204 err = scarlett2_get_flash_segment_nums(mixer);
7208 /* Add firmware version control */
7209 err = scarlett2_add_firmware_version_ctl(mixer);
7213 /* Add minimum firmware version control */
7214 err = scarlett2_add_min_firmware_version_ctl(mixer);
7218 /* Read volume levels and controls from the interface */
7219 err = scarlett2_read_configs(mixer);
7223 /* Create the MSD control */
7224 err = scarlett2_add_msd_ctl(mixer);
7228 /* If MSD mode is enabled, or if the firmware version is too
7229 * old, don't create any other controls
7231 if (private->msd_switch ||
7232 private->firmware_version < private->info->min_firmware_version)
7235 /* Create the analogue output controls */
7236 err = scarlett2_add_line_out_ctls(mixer);
7240 /* Create the analogue input controls */
7241 err = scarlett2_add_line_in_ctls(mixer);
7245 /* Create the input, output, and mixer mux input selections */
7246 err = scarlett2_add_mux_enums(mixer);
7250 /* Create the matrix mixer controls */
7251 err = scarlett2_add_mixer_ctls(mixer);
7255 /* Create the level meter controls */
7256 err = scarlett2_add_meter_ctl(mixer);
7260 /* Create the sync control */
7261 err = scarlett2_add_sync_ctl(mixer);
7265 /* Create the direct monitor control(s) */
7266 err = scarlett2_add_direct_monitor_ctls(mixer);
7270 /* Create the speaker switching control */
7271 err = scarlett2_add_speaker_switch_ctl(mixer);
7275 /* Create the talkback controls */
7276 err = scarlett2_add_talkback_ctls(mixer);
7280 /* Create the standalone control */
7281 err = scarlett2_add_standalone_ctl(mixer);
7285 /* Create the power status control */
7286 err = scarlett2_add_power_status_ctl(mixer);
7290 /* Set the access mode of controls disabled during
7291 * autogain/phantom power switching.
7293 if (private->info->gain_input_count) {
7294 scarlett2_autogain_update_access(mixer);
7295 scarlett2_phantom_update_access(mixer);
7298 /* Set up the interrupt polling */
7299 err = scarlett2_init_notify(mixer);
7306 /*** hwdep interface ***/
7308 /* Set private->hwdep_in_use; prevents access to the ALSA controls
7309 * while doing a config erase/firmware upgrade.
7311 static void scarlett2_lock(struct scarlett2_data *private)
7313 mutex_lock(&private->data_mutex);
7314 private->hwdep_in_use = 1;
7315 mutex_unlock(&private->data_mutex);
7318 /* Call SCARLETT2_USB_GET_ERASE to get the erase progress */
7319 static int scarlett2_get_erase_progress(struct usb_mixer_interface *mixer)
7321 struct scarlett2_data *private = mixer->private_data;
7322 int segment_id, segment_num, err;
7328 } __packed erase_req;
7330 segment_id = private->selected_flash_segment_id;
7331 segment_num = private->flash_segment_nums[segment_id];
7333 if (segment_num < SCARLETT2_SEGMENT_NUM_MIN ||
7334 segment_num > SCARLETT2_SEGMENT_NUM_MAX)
7337 /* Send the erase progress request */
7338 erase_req.segment_num = cpu_to_le32(segment_num);
7341 err = scarlett2_usb(mixer, SCARLETT2_USB_GET_ERASE,
7342 &erase_req, sizeof(erase_req),
7343 &erase_resp, sizeof(erase_resp));
7350 /* Repeatedly call scarlett2_get_erase_progress() until it returns
7351 * 0xff (erase complete) or we've waited 10 seconds (it usually takes
7354 static int scarlett2_wait_for_erase(struct usb_mixer_interface *mixer)
7358 for (i = 0; i < 100; i++) {
7359 err = scarlett2_get_erase_progress(mixer);
7372 /* Reboot the device; wait for the erase to complete if one is in
7375 static int scarlett2_reboot(struct usb_mixer_interface *mixer)
7377 struct scarlett2_data *private = mixer->private_data;
7379 if (private->flash_write_state ==
7380 SCARLETT2_FLASH_WRITE_STATE_ERASING) {
7381 int err = scarlett2_wait_for_erase(mixer);
7387 return scarlett2_usb(mixer, SCARLETT2_USB_REBOOT, NULL, 0, NULL, 0);
7390 /* Select a flash segment for erasing (and possibly writing to) */
7391 static int scarlett2_ioctl_select_flash_segment(
7392 struct usb_mixer_interface *mixer,
7395 struct scarlett2_data *private = mixer->private_data;
7396 int segment_id, segment_num;
7398 if (get_user(segment_id, (int __user *)arg))
7401 /* Check the segment ID and segment number */
7402 if (segment_id < 0 || segment_id >= SCARLETT2_SEGMENT_ID_COUNT)
7405 segment_num = private->flash_segment_nums[segment_id];
7406 if (segment_num < SCARLETT2_SEGMENT_NUM_MIN ||
7407 segment_num > SCARLETT2_SEGMENT_NUM_MAX) {
7408 usb_audio_err(mixer->chip,
7409 "%s: invalid segment number %d\n",
7410 __func__, segment_id);
7414 /* If erasing, wait for it to complete */
7415 if (private->flash_write_state == SCARLETT2_FLASH_WRITE_STATE_ERASING) {
7416 int err = scarlett2_wait_for_erase(mixer);
7422 /* Save the selected segment ID and set the state to SELECTED */
7423 private->selected_flash_segment_id = segment_id;
7424 private->flash_write_state = SCARLETT2_FLASH_WRITE_STATE_SELECTED;
7429 /* Erase the previously-selected flash segment */
7430 static int scarlett2_ioctl_erase_flash_segment(
7431 struct usb_mixer_interface *mixer)
7433 struct scarlett2_data *private = mixer->private_data;
7434 int segment_id, segment_num, err;
7439 } __packed erase_req;
7441 if (private->flash_write_state != SCARLETT2_FLASH_WRITE_STATE_SELECTED)
7444 segment_id = private->selected_flash_segment_id;
7445 segment_num = private->flash_segment_nums[segment_id];
7447 if (segment_num < SCARLETT2_SEGMENT_NUM_MIN ||
7448 segment_num > SCARLETT2_SEGMENT_NUM_MAX)
7451 /* Prevent access to ALSA controls that access the device from
7454 scarlett2_lock(private);
7456 /* Send the erase request */
7457 erase_req.segment_num = cpu_to_le32(segment_num);
7460 err = scarlett2_usb(mixer, SCARLETT2_USB_ERASE_SEGMENT,
7461 &erase_req, sizeof(erase_req),
7466 /* On success, change the state from SELECTED to ERASING */
7467 private->flash_write_state = SCARLETT2_FLASH_WRITE_STATE_ERASING;
7472 /* Get the erase progress from the device */
7473 static int scarlett2_ioctl_get_erase_progress(
7474 struct usb_mixer_interface *mixer,
7477 struct scarlett2_data *private = mixer->private_data;
7478 struct scarlett2_flash_segment_erase_progress progress;
7479 int segment_id, segment_num, err;
7485 } __packed erase_req;
7487 /* Check that we're erasing */
7488 if (private->flash_write_state != SCARLETT2_FLASH_WRITE_STATE_ERASING)
7491 segment_id = private->selected_flash_segment_id;
7492 segment_num = private->flash_segment_nums[segment_id];
7494 if (segment_num < SCARLETT2_SEGMENT_NUM_MIN ||
7495 segment_num > SCARLETT2_SEGMENT_NUM_MAX)
7498 /* Send the erase progress request */
7499 erase_req.segment_num = cpu_to_le32(segment_num);
7502 err = scarlett2_usb(mixer, SCARLETT2_USB_GET_ERASE,
7503 &erase_req, sizeof(erase_req),
7504 &erase_resp, sizeof(erase_resp));
7508 progress.progress = erase_resp;
7509 progress.num_blocks = private->flash_segment_blocks[segment_id];
7511 if (copy_to_user((void __user *)arg, &progress, sizeof(progress)))
7514 /* If the erase is complete, change the state from ERASING to
7517 if (progress.progress == 0xff)
7518 private->flash_write_state = SCARLETT2_FLASH_WRITE_STATE_WRITE;
7523 static int scarlett2_hwdep_open(struct snd_hwdep *hw, struct file *file)
7525 struct usb_mixer_interface *mixer = hw->private_data;
7526 struct scarlett2_data *private = mixer->private_data;
7528 /* If erasing, wait for it to complete */
7529 if (private->flash_write_state ==
7530 SCARLETT2_FLASH_WRITE_STATE_ERASING) {
7531 int err = scarlett2_wait_for_erase(mixer);
7537 /* Set the state to IDLE */
7538 private->flash_write_state = SCARLETT2_FLASH_WRITE_STATE_IDLE;
7543 static int scarlett2_hwdep_ioctl(struct snd_hwdep *hw, struct file *file,
7544 unsigned int cmd, unsigned long arg)
7546 struct usb_mixer_interface *mixer = hw->private_data;
7550 case SCARLETT2_IOCTL_PVERSION:
7551 return put_user(SCARLETT2_HWDEP_VERSION,
7552 (int __user *)arg) ? -EFAULT : 0;
7554 case SCARLETT2_IOCTL_REBOOT:
7555 return scarlett2_reboot(mixer);
7557 case SCARLETT2_IOCTL_SELECT_FLASH_SEGMENT:
7558 return scarlett2_ioctl_select_flash_segment(mixer, arg);
7560 case SCARLETT2_IOCTL_ERASE_FLASH_SEGMENT:
7561 return scarlett2_ioctl_erase_flash_segment(mixer);
7563 case SCARLETT2_IOCTL_GET_ERASE_PROGRESS:
7564 return scarlett2_ioctl_get_erase_progress(mixer, arg);
7567 return -ENOIOCTLCMD;
7571 static long scarlett2_hwdep_write(struct snd_hwdep *hw,
7572 const char __user *buf,
7573 long count, loff_t *offset)
7575 struct usb_mixer_interface *mixer = hw->private_data;
7576 struct scarlett2_data *private = mixer->private_data;
7577 int segment_id, segment_num, err, len;
7580 /* SCARLETT2_USB_WRITE_SEGMENT request data */
7588 /* Calculate the maximum permitted in data[] */
7589 const size_t max_data_size = SCARLETT2_FLASH_WRITE_MAX -
7590 offsetof(typeof(*req), data);
7592 /* If erasing, wait for it to complete */
7593 if (private->flash_write_state ==
7594 SCARLETT2_FLASH_WRITE_STATE_ERASING) {
7595 err = scarlett2_wait_for_erase(mixer);
7598 private->flash_write_state = SCARLETT2_FLASH_WRITE_STATE_WRITE;
7600 /* Check that an erase has been done & completed */
7601 } else if (private->flash_write_state !=
7602 SCARLETT2_FLASH_WRITE_STATE_WRITE) {
7606 /* Check that we're writing to the upgrade firmware */
7607 segment_id = private->selected_flash_segment_id;
7608 if (segment_id != SCARLETT2_SEGMENT_ID_FIRMWARE)
7611 segment_num = private->flash_segment_nums[segment_id];
7612 if (segment_num < SCARLETT2_SEGMENT_NUM_MIN ||
7613 segment_num > SCARLETT2_SEGMENT_NUM_MAX)
7616 /* Validate the offset and count */
7617 flash_size = private->flash_segment_blocks[segment_id] *
7618 SCARLETT2_FLASH_BLOCK_SIZE;
7620 if (count < 0 || *offset < 0 || *offset + count >= flash_size)
7626 /* Limit the *req size to SCARLETT2_FLASH_WRITE_MAX */
7627 if (count > max_data_size)
7628 count = max_data_size;
7630 /* Create and send the request */
7631 len = struct_size(req, data, count);
7632 req = kzalloc(len, GFP_KERNEL);
7636 req->segment_num = cpu_to_le32(segment_num);
7637 req->offset = cpu_to_le32(*offset);
7640 if (copy_from_user(req->data, buf, count)) {
7645 err = scarlett2_usb(mixer, SCARLETT2_USB_WRITE_SEGMENT,
7658 static int scarlett2_hwdep_release(struct snd_hwdep *hw, struct file *file)
7660 struct usb_mixer_interface *mixer = hw->private_data;
7661 struct scarlett2_data *private = mixer->private_data;
7663 /* Return from the SELECTED or WRITE state to IDLE.
7664 * The ERASING state is left as-is, and checked on next open.
7667 private->hwdep_in_use &&
7668 private->flash_write_state != SCARLETT2_FLASH_WRITE_STATE_ERASING)
7669 private->flash_write_state = SCARLETT2_FLASH_WRITE_STATE_IDLE;
7674 static int scarlett2_hwdep_init(struct usb_mixer_interface *mixer)
7676 struct snd_hwdep *hw;
7679 err = snd_hwdep_new(mixer->chip->card, "Focusrite Control", 0, &hw);
7683 hw->private_data = mixer;
7685 hw->ops.open = scarlett2_hwdep_open;
7686 hw->ops.ioctl = scarlett2_hwdep_ioctl;
7687 hw->ops.write = scarlett2_hwdep_write;
7688 hw->ops.release = scarlett2_hwdep_release;
7693 int snd_scarlett2_init(struct usb_mixer_interface *mixer)
7695 struct snd_usb_audio *chip = mixer->chip;
7696 const struct scarlett2_device_entry *entry;
7699 /* only use UAC_VERSION_2 */
7700 if (!mixer->protocol)
7703 /* find entry in scarlett2_devices */
7704 entry = get_scarlett2_device_entry(mixer);
7706 usb_audio_err(mixer->chip,
7707 "%s: missing device entry for %04x:%04x\n",
7709 USB_ID_VENDOR(chip->usb_id),
7710 USB_ID_PRODUCT(chip->usb_id));
7714 if (chip->setup & SCARLETT2_DISABLE) {
7715 usb_audio_info(chip,
7716 "Focusrite %s Mixer Driver disabled "
7717 "by modprobe options (snd_usb_audio "
7718 "vid=0x%04x pid=0x%04x device_setup=%d)\n",
7720 USB_ID_VENDOR(chip->usb_id),
7721 USB_ID_PRODUCT(chip->usb_id),
7726 usb_audio_info(chip,
7727 "Focusrite %s Mixer Driver enabled (pid=0x%04x); "
7728 "report any issues to "
7729 "https://github.com/geoffreybennett/scarlett-gen2/issues",
7731 USB_ID_PRODUCT(chip->usb_id));
7733 err = snd_scarlett2_controls_create(mixer, entry);
7735 usb_audio_err(mixer->chip,
7736 "Error initialising %s Mixer Driver: %d",
7742 err = scarlett2_hwdep_init(mixer);
7744 usb_audio_err(mixer->chip,
7745 "Error creating %s hwdep device: %d",