mixer_scarlett_gen2.c (113857B)
1// SPDX-License-Identifier: GPL-2.0 2/* 3 * Focusrite Scarlett Gen 2/3 Driver for ALSA 4 * 5 * Supported models: 6 * - 6i6/18i8/18i20 Gen 2 7 * - Solo/2i2/4i4/8i6/18i8/18i20 Gen 3 8 * 9 * Copyright (c) 2018-2022 by Geoffrey D. Bennett <g at b4.vu> 10 * Copyright (c) 2020-2021 by Vladimir Sadovnikov <sadko4u@gmail.com> 11 * 12 * Based on the Scarlett (Gen 1) Driver for ALSA: 13 * 14 * Copyright (c) 2013 by Tobias Hoffmann 15 * Copyright (c) 2013 by Robin Gareus <robin at gareus.org> 16 * Copyright (c) 2002 by Takashi Iwai <tiwai at suse.de> 17 * Copyright (c) 2014 by Chris J Arges <chris.j.arges at canonical.com> 18 * 19 * Many codes borrowed from audio.c by 20 * Alan Cox (alan at lxorguk.ukuu.org.uk) 21 * Thomas Sailer (sailer at ife.ee.ethz.ch) 22 * 23 * Code cleanup: 24 * David Henningsson <david.henningsson at canonical.com> 25 */ 26 27/* The protocol was reverse engineered by looking at the communication 28 * between Focusrite Control 2.3.4 and the Focusrite(R) Scarlett 18i20 29 * (firmware 1083) using usbmon in July-August 2018. 30 * 31 * Scarlett 18i8 support added in April 2019. 32 * 33 * Scarlett 6i6 support added in June 2019 (thanks to Martin Wittmann 34 * for providing usbmon output and testing). 35 * 36 * Scarlett 4i4/8i6 Gen 3 support added in May 2020 (thanks to Laurent 37 * Debricon for donating a 4i4 and to Fredrik Unger for providing 8i6 38 * usbmon output and testing). 39 * 40 * Scarlett 18i8/18i20 Gen 3 support added in June 2020 (thanks to 41 * Darren Jaeckel, Alex Sedlack, and Clovis Lunel for providing usbmon 42 * output, protocol traces and testing). 43 * 44 * Support for loading mixer volume and mux configuration from the 45 * interface during driver initialisation added in May 2021 (thanks to 46 * Vladimir Sadovnikov for figuring out how). 47 * 48 * Support for Solo/2i2 Gen 3 added in May 2021 (thanks to Alexander 49 * Vorona for 2i2 protocol traces). 50 * 51 * Support for phantom power, direct monitoring, speaker switching, 52 * and talkback added in May-June 2021. 53 * 54 * This ALSA mixer gives access to (model-dependent): 55 * - input, output, mixer-matrix muxes 56 * - mixer-matrix gain stages 57 * - gain/volume/mute controls 58 * - level meters 59 * - line/inst level, pad, and air controls 60 * - phantom power, direct monitor, speaker switching, and talkback 61 * controls 62 * - disable/enable MSD mode 63 * - disable/enable standalone mode 64 * 65 * <ditaa> 66 * /--------------\ 18chn 20chn /--------------\ 67 * | Hardware in +--+------\ /-------------+--+ ALSA PCM out | 68 * \--------------/ | | | | \--------------/ 69 * | | | /-----\ | 70 * | | | | | | 71 * | v v v | | 72 * | +---------------+ | | 73 * | \ Matrix Mux / | | 74 * | +-----+-----+ | | 75 * | | | | 76 * | |18chn | | 77 * | | | | 78 * | | 10chn| | 79 * | v | | 80 * | +------------+ | | 81 * | | Mixer | | | 82 * | | Matrix | | | 83 * | | | | | 84 * | | 18x10 Gain | | | 85 * | | stages | | | 86 * | +-----+------+ | | 87 * | | | | 88 * |18chn |10chn | |20chn 89 * | | | | 90 * | +----------/ | 91 * | | | 92 * v v v 93 * =========================== 94 * +---------------+ +--—------------+ 95 * \ Output Mux / \ Capture Mux / 96 * +---+---+---+ +-----+-----+ 97 * | | | 98 * 10chn| | |18chn 99 * | | | 100 * /--------------\ | | | /--------------\ 101 * | S/PDIF, ADAT |<--/ |10chn \-->| ALSA PCM in | 102 * | Hardware out | | \--------------/ 103 * \--------------/ | 104 * v 105 * +-------------+ Software gain per channel. 106 * | Master Gain |<-- 18i20 only: Switch per channel 107 * +------+------+ to select HW or SW gain control. 108 * | 109 * |10chn 110 * /--------------\ | 111 * | Analogue |<------/ 112 * | Hardware out | 113 * \--------------/ 114 * </ditaa> 115 * 116 * Gen 3 devices have a Mass Storage Device (MSD) mode where a small 117 * disk with registration and driver download information is presented 118 * to the host. To access the full functionality of the device without 119 * proprietary software, MSD mode can be disabled by: 120 * - holding down the 48V button for five seconds while powering on 121 * the device, or 122 * - using this driver and alsamixer to change the "MSD Mode" setting 123 * to Off and power-cycling the device 124 */ 125 126#include <linux/slab.h> 127#include <linux/usb.h> 128#include <linux/moduleparam.h> 129 130#include <sound/control.h> 131#include <sound/tlv.h> 132 133#include "usbaudio.h" 134#include "mixer.h" 135#include "helper.h" 136 137#include "mixer_scarlett_gen2.h" 138 139/* device_setup value to enable */ 140#define SCARLETT2_ENABLE 0x01 141 142/* device_setup value to allow turning MSD mode back on */ 143#define SCARLETT2_MSD_ENABLE 0x02 144 145/* some gui mixers can't handle negative ctl values */ 146#define SCARLETT2_VOLUME_BIAS 127 147 148/* mixer range from -80dB to +6dB in 0.5dB steps */ 149#define SCARLETT2_MIXER_MIN_DB -80 150#define SCARLETT2_MIXER_BIAS (-SCARLETT2_MIXER_MIN_DB * 2) 151#define SCARLETT2_MIXER_MAX_DB 6 152#define SCARLETT2_MIXER_MAX_VALUE \ 153 ((SCARLETT2_MIXER_MAX_DB - SCARLETT2_MIXER_MIN_DB) * 2) 154#define SCARLETT2_MIXER_VALUE_COUNT (SCARLETT2_MIXER_MAX_VALUE + 1) 155 156/* map from (dB + 80) * 2 to mixer value 157 * for dB in 0 .. 172: int(8192 * pow(10, ((dB - 160) / 2 / 20))) 158 */ 159static const u16 scarlett2_mixer_values[SCARLETT2_MIXER_VALUE_COUNT] = { 160 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 161 2, 2, 3, 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 6, 6, 6, 7, 7, 8, 8, 162 9, 9, 10, 10, 11, 12, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 163 23, 24, 25, 27, 29, 30, 32, 34, 36, 38, 41, 43, 46, 48, 51, 164 54, 57, 61, 65, 68, 73, 77, 81, 86, 91, 97, 103, 109, 115, 165 122, 129, 137, 145, 154, 163, 173, 183, 194, 205, 217, 230, 166 244, 259, 274, 290, 307, 326, 345, 365, 387, 410, 434, 460, 167 487, 516, 547, 579, 614, 650, 689, 730, 773, 819, 867, 919, 168 973, 1031, 1092, 1157, 1225, 1298, 1375, 1456, 1543, 1634, 169 1731, 1833, 1942, 2057, 2179, 2308, 2445, 2590, 2744, 2906, 170 3078, 3261, 3454, 3659, 3876, 4105, 4349, 4606, 4879, 5168, 171 5475, 5799, 6143, 6507, 6892, 7301, 7733, 8192, 8677, 9191, 172 9736, 10313, 10924, 11571, 12257, 12983, 13752, 14567, 15430, 173 16345 174}; 175 176/* Maximum number of analogue outputs */ 177#define SCARLETT2_ANALOGUE_MAX 10 178 179/* Maximum number of level and pad switches */ 180#define SCARLETT2_LEVEL_SWITCH_MAX 2 181#define SCARLETT2_PAD_SWITCH_MAX 8 182#define SCARLETT2_AIR_SWITCH_MAX 8 183#define SCARLETT2_PHANTOM_SWITCH_MAX 2 184 185/* Maximum number of inputs to the mixer */ 186#define SCARLETT2_INPUT_MIX_MAX 25 187 188/* Maximum number of outputs from the mixer */ 189#define SCARLETT2_OUTPUT_MIX_MAX 12 190 191/* Maximum size of the data in the USB mux assignment message: 192 * 20 inputs, 20 outputs, 25 matrix inputs, 12 spare 193 */ 194#define SCARLETT2_MUX_MAX 77 195 196/* Maximum number of meters (sum of output port counts) */ 197#define SCARLETT2_MAX_METERS 65 198 199/* There are three different sets of configuration parameters across 200 * the devices 201 */ 202enum { 203 SCARLETT2_CONFIG_SET_NO_MIXER = 0, 204 SCARLETT2_CONFIG_SET_GEN_2 = 1, 205 SCARLETT2_CONFIG_SET_GEN_3 = 2, 206 SCARLETT2_CONFIG_SET_COUNT = 3 207}; 208 209/* Hardware port types: 210 * - None (no input to mux) 211 * - Analogue I/O 212 * - S/PDIF I/O 213 * - ADAT I/O 214 * - Mixer I/O 215 * - PCM I/O 216 */ 217enum { 218 SCARLETT2_PORT_TYPE_NONE = 0, 219 SCARLETT2_PORT_TYPE_ANALOGUE = 1, 220 SCARLETT2_PORT_TYPE_SPDIF = 2, 221 SCARLETT2_PORT_TYPE_ADAT = 3, 222 SCARLETT2_PORT_TYPE_MIX = 4, 223 SCARLETT2_PORT_TYPE_PCM = 5, 224 SCARLETT2_PORT_TYPE_COUNT = 6, 225}; 226 227/* I/O count of each port type kept in struct scarlett2_ports */ 228enum { 229 SCARLETT2_PORT_IN = 0, 230 SCARLETT2_PORT_OUT = 1, 231 SCARLETT2_PORT_DIRNS = 2, 232}; 233 234/* Dim/Mute buttons on the 18i20 */ 235enum { 236 SCARLETT2_BUTTON_MUTE = 0, 237 SCARLETT2_BUTTON_DIM = 1, 238 SCARLETT2_DIM_MUTE_COUNT = 2, 239}; 240 241static const char *const scarlett2_dim_mute_names[SCARLETT2_DIM_MUTE_COUNT] = { 242 "Mute Playback Switch", "Dim Playback Switch" 243}; 244 245/* Description of each hardware port type: 246 * - id: hardware ID of this port type 247 * - src_descr: printf format string for mux input selections 248 * - src_num_offset: added to channel number for the fprintf 249 * - dst_descr: printf format string for mixer controls 250 */ 251struct scarlett2_port { 252 u16 id; 253 const char * const src_descr; 254 int src_num_offset; 255 const char * const dst_descr; 256}; 257 258static const struct scarlett2_port scarlett2_ports[SCARLETT2_PORT_TYPE_COUNT] = { 259 [SCARLETT2_PORT_TYPE_NONE] = { 260 .id = 0x000, 261 .src_descr = "Off" 262 }, 263 [SCARLETT2_PORT_TYPE_ANALOGUE] = { 264 .id = 0x080, 265 .src_descr = "Analogue %d", 266 .src_num_offset = 1, 267 .dst_descr = "Analogue Output %02d Playback" 268 }, 269 [SCARLETT2_PORT_TYPE_SPDIF] = { 270 .id = 0x180, 271 .src_descr = "S/PDIF %d", 272 .src_num_offset = 1, 273 .dst_descr = "S/PDIF Output %d Playback" 274 }, 275 [SCARLETT2_PORT_TYPE_ADAT] = { 276 .id = 0x200, 277 .src_descr = "ADAT %d", 278 .src_num_offset = 1, 279 .dst_descr = "ADAT Output %d Playback" 280 }, 281 [SCARLETT2_PORT_TYPE_MIX] = { 282 .id = 0x300, 283 .src_descr = "Mix %c", 284 .src_num_offset = 'A', 285 .dst_descr = "Mixer Input %02d Capture" 286 }, 287 [SCARLETT2_PORT_TYPE_PCM] = { 288 .id = 0x600, 289 .src_descr = "PCM %d", 290 .src_num_offset = 1, 291 .dst_descr = "PCM %02d Capture" 292 }, 293}; 294 295/* Number of mux tables: one for each band of sample rates 296 * (44.1/48kHz, 88.2/96kHz, and 176.4/176kHz) 297 */ 298#define SCARLETT2_MUX_TABLES 3 299 300/* Maximum number of entries in a mux table */ 301#define SCARLETT2_MAX_MUX_ENTRIES 10 302 303/* One entry within mux_assignment defines the port type and range of 304 * ports to add to the set_mux message. The end of the list is marked 305 * with count == 0. 306 */ 307struct scarlett2_mux_entry { 308 u8 port_type; 309 u8 start; 310 u8 count; 311}; 312 313struct scarlett2_device_info { 314 u32 usb_id; /* USB device identifier */ 315 316 /* Gen 3 devices have an internal MSD mode switch that needs 317 * to be disabled in order to access the full functionality of 318 * the device. 319 */ 320 u8 has_msd_mode; 321 322 /* which set of configuration parameters the device uses */ 323 u8 config_set; 324 325 /* line out hw volume is sw controlled */ 326 u8 line_out_hw_vol; 327 328 /* support for main/alt speaker switching */ 329 u8 has_speaker_switching; 330 331 /* support for talkback microphone */ 332 u8 has_talkback; 333 334 /* the number of analogue inputs with a software switchable 335 * level control that can be set to line or instrument 336 */ 337 u8 level_input_count; 338 339 /* the first input with a level control (0-based) */ 340 u8 level_input_first; 341 342 /* the number of analogue inputs with a software switchable 343 * 10dB pad control 344 */ 345 u8 pad_input_count; 346 347 /* the number of analogue inputs with a software switchable 348 * "air" control 349 */ 350 u8 air_input_count; 351 352 /* the number of phantom (48V) software switchable controls */ 353 u8 phantom_count; 354 355 /* the number of inputs each phantom switch controls */ 356 u8 inputs_per_phantom; 357 358 /* the number of direct monitor options 359 * (0 = none, 1 = mono only, 2 = mono/stereo) 360 */ 361 u8 direct_monitor; 362 363 /* remap analogue outputs; 18i8 Gen 3 has "line 3/4" connected 364 * internally to the analogue 7/8 outputs 365 */ 366 u8 line_out_remap_enable; 367 u8 line_out_remap[SCARLETT2_ANALOGUE_MAX]; 368 369 /* additional description for the line out volume controls */ 370 const char * const line_out_descrs[SCARLETT2_ANALOGUE_MAX]; 371 372 /* number of sources/destinations of each port type */ 373 const int port_count[SCARLETT2_PORT_TYPE_COUNT][SCARLETT2_PORT_DIRNS]; 374 375 /* layout/order of the entries in the set_mux message */ 376 struct scarlett2_mux_entry mux_assignment[SCARLETT2_MUX_TABLES] 377 [SCARLETT2_MAX_MUX_ENTRIES]; 378}; 379 380struct scarlett2_data { 381 struct usb_mixer_interface *mixer; 382 struct mutex usb_mutex; /* prevent sending concurrent USB requests */ 383 struct mutex data_mutex; /* lock access to this data */ 384 struct delayed_work work; 385 const struct scarlett2_device_info *info; 386 __u8 bInterfaceNumber; 387 __u8 bEndpointAddress; 388 __u16 wMaxPacketSize; 389 __u8 bInterval; 390 int num_mux_srcs; 391 int num_mux_dsts; 392 u16 scarlett2_seq; 393 u8 sync_updated; 394 u8 vol_updated; 395 u8 input_other_updated; 396 u8 monitor_other_updated; 397 u8 mux_updated; 398 u8 speaker_switching_switched; 399 u8 sync; 400 u8 master_vol; 401 u8 vol[SCARLETT2_ANALOGUE_MAX]; 402 u8 vol_sw_hw_switch[SCARLETT2_ANALOGUE_MAX]; 403 u8 mute_switch[SCARLETT2_ANALOGUE_MAX]; 404 u8 level_switch[SCARLETT2_LEVEL_SWITCH_MAX]; 405 u8 pad_switch[SCARLETT2_PAD_SWITCH_MAX]; 406 u8 dim_mute[SCARLETT2_DIM_MUTE_COUNT]; 407 u8 air_switch[SCARLETT2_AIR_SWITCH_MAX]; 408 u8 phantom_switch[SCARLETT2_PHANTOM_SWITCH_MAX]; 409 u8 phantom_persistence; 410 u8 direct_monitor_switch; 411 u8 speaker_switching_switch; 412 u8 talkback_switch; 413 u8 talkback_map[SCARLETT2_OUTPUT_MIX_MAX]; 414 u8 msd_switch; 415 u8 standalone_switch; 416 struct snd_kcontrol *sync_ctl; 417 struct snd_kcontrol *master_vol_ctl; 418 struct snd_kcontrol *vol_ctls[SCARLETT2_ANALOGUE_MAX]; 419 struct snd_kcontrol *sw_hw_ctls[SCARLETT2_ANALOGUE_MAX]; 420 struct snd_kcontrol *mute_ctls[SCARLETT2_ANALOGUE_MAX]; 421 struct snd_kcontrol *dim_mute_ctls[SCARLETT2_DIM_MUTE_COUNT]; 422 struct snd_kcontrol *level_ctls[SCARLETT2_LEVEL_SWITCH_MAX]; 423 struct snd_kcontrol *pad_ctls[SCARLETT2_PAD_SWITCH_MAX]; 424 struct snd_kcontrol *air_ctls[SCARLETT2_AIR_SWITCH_MAX]; 425 struct snd_kcontrol *phantom_ctls[SCARLETT2_PHANTOM_SWITCH_MAX]; 426 struct snd_kcontrol *mux_ctls[SCARLETT2_MUX_MAX]; 427 struct snd_kcontrol *direct_monitor_ctl; 428 struct snd_kcontrol *speaker_switching_ctl; 429 struct snd_kcontrol *talkback_ctl; 430 u8 mux[SCARLETT2_MUX_MAX]; 431 u8 mix[SCARLETT2_INPUT_MIX_MAX * SCARLETT2_OUTPUT_MIX_MAX]; 432}; 433 434/*** Model-specific data ***/ 435 436static const struct scarlett2_device_info s6i6_gen2_info = { 437 .usb_id = USB_ID(0x1235, 0x8203), 438 439 .config_set = SCARLETT2_CONFIG_SET_GEN_2, 440 .level_input_count = 2, 441 .pad_input_count = 2, 442 443 .line_out_descrs = { 444 "Headphones 1 L", 445 "Headphones 1 R", 446 "Headphones 2 L", 447 "Headphones 2 R", 448 }, 449 450 .port_count = { 451 [SCARLETT2_PORT_TYPE_NONE] = { 1, 0 }, 452 [SCARLETT2_PORT_TYPE_ANALOGUE] = { 4, 4 }, 453 [SCARLETT2_PORT_TYPE_SPDIF] = { 2, 2 }, 454 [SCARLETT2_PORT_TYPE_MIX] = { 10, 18 }, 455 [SCARLETT2_PORT_TYPE_PCM] = { 6, 6 }, 456 }, 457 458 .mux_assignment = { { 459 { SCARLETT2_PORT_TYPE_PCM, 0, 6 }, 460 { SCARLETT2_PORT_TYPE_ANALOGUE, 0, 4 }, 461 { SCARLETT2_PORT_TYPE_SPDIF, 0, 2 }, 462 { SCARLETT2_PORT_TYPE_MIX, 0, 18 }, 463 { SCARLETT2_PORT_TYPE_NONE, 0, 8 }, 464 { 0, 0, 0 }, 465 }, { 466 { SCARLETT2_PORT_TYPE_PCM, 0, 6 }, 467 { SCARLETT2_PORT_TYPE_ANALOGUE, 0, 4 }, 468 { SCARLETT2_PORT_TYPE_SPDIF, 0, 2 }, 469 { SCARLETT2_PORT_TYPE_MIX, 0, 18 }, 470 { SCARLETT2_PORT_TYPE_NONE, 0, 8 }, 471 { 0, 0, 0 }, 472 }, { 473 { SCARLETT2_PORT_TYPE_PCM, 0, 6 }, 474 { SCARLETT2_PORT_TYPE_ANALOGUE, 0, 4 }, 475 { SCARLETT2_PORT_TYPE_SPDIF, 0, 2 }, 476 { SCARLETT2_PORT_TYPE_MIX, 0, 18 }, 477 { SCARLETT2_PORT_TYPE_NONE, 0, 8 }, 478 { 0, 0, 0 }, 479 } }, 480}; 481 482static const struct scarlett2_device_info s18i8_gen2_info = { 483 .usb_id = USB_ID(0x1235, 0x8204), 484 485 .config_set = SCARLETT2_CONFIG_SET_GEN_2, 486 .level_input_count = 2, 487 .pad_input_count = 4, 488 489 .line_out_descrs = { 490 "Monitor L", 491 "Monitor R", 492 "Headphones 1 L", 493 "Headphones 1 R", 494 "Headphones 2 L", 495 "Headphones 2 R", 496 }, 497 498 .port_count = { 499 [SCARLETT2_PORT_TYPE_NONE] = { 1, 0 }, 500 [SCARLETT2_PORT_TYPE_ANALOGUE] = { 8, 6 }, 501 [SCARLETT2_PORT_TYPE_SPDIF] = { 2, 2 }, 502 [SCARLETT2_PORT_TYPE_ADAT] = { 8, 0 }, 503 [SCARLETT2_PORT_TYPE_MIX] = { 10, 18 }, 504 [SCARLETT2_PORT_TYPE_PCM] = { 8, 18 }, 505 }, 506 507 .mux_assignment = { { 508 { SCARLETT2_PORT_TYPE_PCM, 0, 18 }, 509 { SCARLETT2_PORT_TYPE_ANALOGUE, 0, 6 }, 510 { SCARLETT2_PORT_TYPE_SPDIF, 0, 2 }, 511 { SCARLETT2_PORT_TYPE_MIX, 0, 18 }, 512 { SCARLETT2_PORT_TYPE_NONE, 0, 8 }, 513 { 0, 0, 0 }, 514 }, { 515 { SCARLETT2_PORT_TYPE_PCM, 0, 14 }, 516 { SCARLETT2_PORT_TYPE_ANALOGUE, 0, 6 }, 517 { SCARLETT2_PORT_TYPE_SPDIF, 0, 2 }, 518 { SCARLETT2_PORT_TYPE_MIX, 0, 18 }, 519 { SCARLETT2_PORT_TYPE_NONE, 0, 8 }, 520 { 0, 0, 0 }, 521 }, { 522 { SCARLETT2_PORT_TYPE_PCM, 0, 10 }, 523 { SCARLETT2_PORT_TYPE_ANALOGUE, 0, 6 }, 524 { SCARLETT2_PORT_TYPE_SPDIF, 0, 2 }, 525 { SCARLETT2_PORT_TYPE_MIX, 0, 18 }, 526 { SCARLETT2_PORT_TYPE_NONE, 0, 4 }, 527 { 0, 0, 0 }, 528 } }, 529}; 530 531static const struct scarlett2_device_info s18i20_gen2_info = { 532 .usb_id = USB_ID(0x1235, 0x8201), 533 534 .config_set = SCARLETT2_CONFIG_SET_GEN_2, 535 .line_out_hw_vol = 1, 536 537 .line_out_descrs = { 538 "Monitor L", 539 "Monitor R", 540 NULL, 541 NULL, 542 NULL, 543 NULL, 544 "Headphones 1 L", 545 "Headphones 1 R", 546 "Headphones 2 L", 547 "Headphones 2 R", 548 }, 549 550 .port_count = { 551 [SCARLETT2_PORT_TYPE_NONE] = { 1, 0 }, 552 [SCARLETT2_PORT_TYPE_ANALOGUE] = { 8, 10 }, 553 [SCARLETT2_PORT_TYPE_SPDIF] = { 2, 2 }, 554 [SCARLETT2_PORT_TYPE_ADAT] = { 8, 8 }, 555 [SCARLETT2_PORT_TYPE_MIX] = { 10, 18 }, 556 [SCARLETT2_PORT_TYPE_PCM] = { 20, 18 }, 557 }, 558 559 .mux_assignment = { { 560 { SCARLETT2_PORT_TYPE_PCM, 0, 18 }, 561 { SCARLETT2_PORT_TYPE_ANALOGUE, 0, 10 }, 562 { SCARLETT2_PORT_TYPE_SPDIF, 0, 2 }, 563 { SCARLETT2_PORT_TYPE_ADAT, 0, 8 }, 564 { SCARLETT2_PORT_TYPE_MIX, 0, 18 }, 565 { SCARLETT2_PORT_TYPE_NONE, 0, 8 }, 566 { 0, 0, 0 }, 567 }, { 568 { SCARLETT2_PORT_TYPE_PCM, 0, 14 }, 569 { SCARLETT2_PORT_TYPE_ANALOGUE, 0, 10 }, 570 { SCARLETT2_PORT_TYPE_SPDIF, 0, 2 }, 571 { SCARLETT2_PORT_TYPE_ADAT, 0, 4 }, 572 { SCARLETT2_PORT_TYPE_MIX, 0, 18 }, 573 { SCARLETT2_PORT_TYPE_NONE, 0, 8 }, 574 { 0, 0, 0 }, 575 }, { 576 { SCARLETT2_PORT_TYPE_PCM, 0, 10 }, 577 { SCARLETT2_PORT_TYPE_ANALOGUE, 0, 10 }, 578 { SCARLETT2_PORT_TYPE_SPDIF, 0, 2 }, 579 { SCARLETT2_PORT_TYPE_MIX, 0, 18 }, 580 { SCARLETT2_PORT_TYPE_NONE, 0, 6 }, 581 { 0, 0, 0 }, 582 } }, 583}; 584 585static const struct scarlett2_device_info solo_gen3_info = { 586 .usb_id = USB_ID(0x1235, 0x8211), 587 588 .has_msd_mode = 1, 589 .config_set = SCARLETT2_CONFIG_SET_NO_MIXER, 590 .level_input_count = 1, 591 .level_input_first = 1, 592 .air_input_count = 1, 593 .phantom_count = 1, 594 .inputs_per_phantom = 1, 595 .direct_monitor = 1, 596}; 597 598static const struct scarlett2_device_info s2i2_gen3_info = { 599 .usb_id = USB_ID(0x1235, 0x8210), 600 601 .has_msd_mode = 1, 602 .config_set = SCARLETT2_CONFIG_SET_NO_MIXER, 603 .level_input_count = 2, 604 .air_input_count = 2, 605 .phantom_count = 1, 606 .inputs_per_phantom = 2, 607 .direct_monitor = 2, 608}; 609 610static const struct scarlett2_device_info s4i4_gen3_info = { 611 .usb_id = USB_ID(0x1235, 0x8212), 612 613 .has_msd_mode = 1, 614 .config_set = SCARLETT2_CONFIG_SET_GEN_3, 615 .level_input_count = 2, 616 .pad_input_count = 2, 617 .air_input_count = 2, 618 .phantom_count = 1, 619 .inputs_per_phantom = 2, 620 621 .line_out_descrs = { 622 "Monitor L", 623 "Monitor R", 624 "Headphones L", 625 "Headphones R", 626 }, 627 628 .port_count = { 629 [SCARLETT2_PORT_TYPE_NONE] = { 1, 0 }, 630 [SCARLETT2_PORT_TYPE_ANALOGUE] = { 4, 4 }, 631 [SCARLETT2_PORT_TYPE_MIX] = { 6, 8 }, 632 [SCARLETT2_PORT_TYPE_PCM] = { 4, 6 }, 633 }, 634 635 .mux_assignment = { { 636 { SCARLETT2_PORT_TYPE_PCM, 0, 6 }, 637 { SCARLETT2_PORT_TYPE_ANALOGUE, 0, 4 }, 638 { SCARLETT2_PORT_TYPE_MIX, 0, 8 }, 639 { SCARLETT2_PORT_TYPE_NONE, 0, 16 }, 640 { 0, 0, 0 }, 641 }, { 642 { SCARLETT2_PORT_TYPE_PCM, 0, 6 }, 643 { SCARLETT2_PORT_TYPE_ANALOGUE, 0, 4 }, 644 { SCARLETT2_PORT_TYPE_MIX, 0, 8 }, 645 { SCARLETT2_PORT_TYPE_NONE, 0, 16 }, 646 { 0, 0, 0 }, 647 }, { 648 { SCARLETT2_PORT_TYPE_PCM, 0, 6 }, 649 { SCARLETT2_PORT_TYPE_ANALOGUE, 0, 4 }, 650 { SCARLETT2_PORT_TYPE_MIX, 0, 8 }, 651 { SCARLETT2_PORT_TYPE_NONE, 0, 16 }, 652 { 0, 0, 0 }, 653 } }, 654}; 655 656static const struct scarlett2_device_info s8i6_gen3_info = { 657 .usb_id = USB_ID(0x1235, 0x8213), 658 659 .has_msd_mode = 1, 660 .config_set = SCARLETT2_CONFIG_SET_GEN_3, 661 .level_input_count = 2, 662 .pad_input_count = 2, 663 .air_input_count = 2, 664 .phantom_count = 1, 665 .inputs_per_phantom = 2, 666 667 .line_out_descrs = { 668 "Headphones 1 L", 669 "Headphones 1 R", 670 "Headphones 2 L", 671 "Headphones 2 R", 672 }, 673 674 .port_count = { 675 [SCARLETT2_PORT_TYPE_NONE] = { 1, 0 }, 676 [SCARLETT2_PORT_TYPE_ANALOGUE] = { 6, 4 }, 677 [SCARLETT2_PORT_TYPE_SPDIF] = { 2, 2 }, 678 [SCARLETT2_PORT_TYPE_MIX] = { 8, 8 }, 679 [SCARLETT2_PORT_TYPE_PCM] = { 6, 10 }, 680 }, 681 682 .mux_assignment = { { 683 { SCARLETT2_PORT_TYPE_PCM, 0, 8 }, 684 { SCARLETT2_PORT_TYPE_ANALOGUE, 0, 4 }, 685 { SCARLETT2_PORT_TYPE_SPDIF, 0, 2 }, 686 { SCARLETT2_PORT_TYPE_PCM, 8, 2 }, 687 { SCARLETT2_PORT_TYPE_MIX, 0, 8 }, 688 { SCARLETT2_PORT_TYPE_NONE, 0, 18 }, 689 { 0, 0, 0 }, 690 }, { 691 { SCARLETT2_PORT_TYPE_PCM, 0, 8 }, 692 { SCARLETT2_PORT_TYPE_ANALOGUE, 0, 4 }, 693 { SCARLETT2_PORT_TYPE_SPDIF, 0, 2 }, 694 { SCARLETT2_PORT_TYPE_PCM, 8, 2 }, 695 { SCARLETT2_PORT_TYPE_MIX, 0, 8 }, 696 { SCARLETT2_PORT_TYPE_NONE, 0, 18 }, 697 { 0, 0, 0 }, 698 }, { 699 { SCARLETT2_PORT_TYPE_PCM, 0, 8 }, 700 { SCARLETT2_PORT_TYPE_ANALOGUE, 0, 4 }, 701 { SCARLETT2_PORT_TYPE_SPDIF, 0, 2 }, 702 { SCARLETT2_PORT_TYPE_PCM, 8, 2 }, 703 { SCARLETT2_PORT_TYPE_MIX, 0, 8 }, 704 { SCARLETT2_PORT_TYPE_NONE, 0, 18 }, 705 { 0, 0, 0 }, 706 } }, 707}; 708 709static const struct scarlett2_device_info s18i8_gen3_info = { 710 .usb_id = USB_ID(0x1235, 0x8214), 711 712 .has_msd_mode = 1, 713 .config_set = SCARLETT2_CONFIG_SET_GEN_3, 714 .line_out_hw_vol = 1, 715 .has_speaker_switching = 1, 716 .level_input_count = 2, 717 .pad_input_count = 4, 718 .air_input_count = 4, 719 .phantom_count = 2, 720 .inputs_per_phantom = 2, 721 722 .line_out_remap_enable = 1, 723 .line_out_remap = { 0, 1, 6, 7, 2, 3, 4, 5 }, 724 725 .line_out_descrs = { 726 "Monitor L", 727 "Monitor R", 728 "Alt Monitor L", 729 "Alt Monitor R", 730 "Headphones 1 L", 731 "Headphones 1 R", 732 "Headphones 2 L", 733 "Headphones 2 R", 734 }, 735 736 .port_count = { 737 [SCARLETT2_PORT_TYPE_NONE] = { 1, 0 }, 738 [SCARLETT2_PORT_TYPE_ANALOGUE] = { 8, 8 }, 739 [SCARLETT2_PORT_TYPE_SPDIF] = { 2, 2 }, 740 [SCARLETT2_PORT_TYPE_ADAT] = { 8, 0 }, 741 [SCARLETT2_PORT_TYPE_MIX] = { 10, 20 }, 742 [SCARLETT2_PORT_TYPE_PCM] = { 8, 20 }, 743 }, 744 745 .mux_assignment = { { 746 { SCARLETT2_PORT_TYPE_PCM, 0, 10 }, 747 { SCARLETT2_PORT_TYPE_PCM, 12, 8 }, 748 { SCARLETT2_PORT_TYPE_ANALOGUE, 0, 2 }, 749 { SCARLETT2_PORT_TYPE_ANALOGUE, 6, 2 }, 750 { SCARLETT2_PORT_TYPE_ANALOGUE, 2, 4 }, 751 { SCARLETT2_PORT_TYPE_SPDIF, 0, 2 }, 752 { SCARLETT2_PORT_TYPE_PCM, 10, 2 }, 753 { SCARLETT2_PORT_TYPE_MIX, 0, 20 }, 754 { SCARLETT2_PORT_TYPE_NONE, 0, 10 }, 755 { 0, 0, 0 }, 756 }, { 757 { SCARLETT2_PORT_TYPE_PCM, 0, 10 }, 758 { SCARLETT2_PORT_TYPE_PCM, 12, 4 }, 759 { SCARLETT2_PORT_TYPE_ANALOGUE, 0, 2 }, 760 { SCARLETT2_PORT_TYPE_ANALOGUE, 6, 2 }, 761 { SCARLETT2_PORT_TYPE_ANALOGUE, 2, 4 }, 762 { SCARLETT2_PORT_TYPE_SPDIF, 0, 2 }, 763 { SCARLETT2_PORT_TYPE_PCM, 10, 2 }, 764 { SCARLETT2_PORT_TYPE_MIX, 0, 20 }, 765 { SCARLETT2_PORT_TYPE_NONE, 0, 10 }, 766 { 0, 0, 0 }, 767 }, { 768 { SCARLETT2_PORT_TYPE_PCM, 0, 10 }, 769 { SCARLETT2_PORT_TYPE_ANALOGUE, 0, 2 }, 770 { SCARLETT2_PORT_TYPE_ANALOGUE, 6, 2 }, 771 { SCARLETT2_PORT_TYPE_ANALOGUE, 2, 4 }, 772 { SCARLETT2_PORT_TYPE_SPDIF, 0, 2 }, 773 { SCARLETT2_PORT_TYPE_MIX, 0, 20 }, 774 { SCARLETT2_PORT_TYPE_NONE, 0, 10 }, 775 { 0, 0, 0 }, 776 } }, 777}; 778 779static const struct scarlett2_device_info s18i20_gen3_info = { 780 .usb_id = USB_ID(0x1235, 0x8215), 781 782 .has_msd_mode = 1, 783 .config_set = SCARLETT2_CONFIG_SET_GEN_3, 784 .line_out_hw_vol = 1, 785 .has_speaker_switching = 1, 786 .has_talkback = 1, 787 .level_input_count = 2, 788 .pad_input_count = 8, 789 .air_input_count = 8, 790 .phantom_count = 2, 791 .inputs_per_phantom = 4, 792 793 .line_out_descrs = { 794 "Monitor 1 L", 795 "Monitor 1 R", 796 "Monitor 2 L", 797 "Monitor 2 R", 798 NULL, 799 NULL, 800 "Headphones 1 L", 801 "Headphones 1 R", 802 "Headphones 2 L", 803 "Headphones 2 R", 804 }, 805 806 .port_count = { 807 [SCARLETT2_PORT_TYPE_NONE] = { 1, 0 }, 808 [SCARLETT2_PORT_TYPE_ANALOGUE] = { 9, 10 }, 809 [SCARLETT2_PORT_TYPE_SPDIF] = { 2, 2 }, 810 [SCARLETT2_PORT_TYPE_ADAT] = { 8, 8 }, 811 [SCARLETT2_PORT_TYPE_MIX] = { 12, 25 }, 812 [SCARLETT2_PORT_TYPE_PCM] = { 20, 20 }, 813 }, 814 815 .mux_assignment = { { 816 { SCARLETT2_PORT_TYPE_PCM, 0, 8 }, 817 { SCARLETT2_PORT_TYPE_PCM, 10, 10 }, 818 { SCARLETT2_PORT_TYPE_ANALOGUE, 0, 10 }, 819 { SCARLETT2_PORT_TYPE_SPDIF, 0, 2 }, 820 { SCARLETT2_PORT_TYPE_ADAT, 0, 8 }, 821 { SCARLETT2_PORT_TYPE_PCM, 8, 2 }, 822 { SCARLETT2_PORT_TYPE_MIX, 0, 25 }, 823 { SCARLETT2_PORT_TYPE_NONE, 0, 12 }, 824 { 0, 0, 0 }, 825 }, { 826 { SCARLETT2_PORT_TYPE_PCM, 0, 8 }, 827 { SCARLETT2_PORT_TYPE_PCM, 10, 8 }, 828 { SCARLETT2_PORT_TYPE_ANALOGUE, 0, 10 }, 829 { SCARLETT2_PORT_TYPE_SPDIF, 0, 2 }, 830 { SCARLETT2_PORT_TYPE_ADAT, 0, 8 }, 831 { SCARLETT2_PORT_TYPE_PCM, 8, 2 }, 832 { SCARLETT2_PORT_TYPE_MIX, 0, 25 }, 833 { SCARLETT2_PORT_TYPE_NONE, 0, 10 }, 834 { 0, 0, 0 }, 835 }, { 836 { SCARLETT2_PORT_TYPE_PCM, 0, 10 }, 837 { SCARLETT2_PORT_TYPE_ANALOGUE, 0, 10 }, 838 { SCARLETT2_PORT_TYPE_SPDIF, 0, 2 }, 839 { SCARLETT2_PORT_TYPE_NONE, 0, 24 }, 840 { 0, 0, 0 }, 841 } }, 842}; 843 844static const struct scarlett2_device_info *scarlett2_devices[] = { 845 /* Supported Gen 2 devices */ 846 &s6i6_gen2_info, 847 &s18i8_gen2_info, 848 &s18i20_gen2_info, 849 850 /* Supported Gen 3 devices */ 851 &solo_gen3_info, 852 &s2i2_gen3_info, 853 &s4i4_gen3_info, 854 &s8i6_gen3_info, 855 &s18i8_gen3_info, 856 &s18i20_gen3_info, 857 858 /* End of list */ 859 NULL 860}; 861 862/* get the starting port index number for a given port type/direction */ 863static int scarlett2_get_port_start_num( 864 const int port_count[][SCARLETT2_PORT_DIRNS], 865 int direction, int port_type) 866{ 867 int i, num = 0; 868 869 for (i = 0; i < port_type; i++) 870 num += port_count[i][direction]; 871 872 return num; 873} 874 875/*** USB Interactions ***/ 876 877/* Notifications from the interface */ 878#define SCARLETT2_USB_NOTIFY_SYNC 0x00000008 879#define SCARLETT2_USB_NOTIFY_DIM_MUTE 0x00200000 880#define SCARLETT2_USB_NOTIFY_MONITOR 0x00400000 881#define SCARLETT2_USB_NOTIFY_INPUT_OTHER 0x00800000 882#define SCARLETT2_USB_NOTIFY_MONITOR_OTHER 0x01000000 883 884/* Commands for sending/receiving requests/responses */ 885#define SCARLETT2_USB_CMD_INIT 0 886#define SCARLETT2_USB_CMD_REQ 2 887#define SCARLETT2_USB_CMD_RESP 3 888 889#define SCARLETT2_USB_INIT_1 0x00000000 890#define SCARLETT2_USB_INIT_2 0x00000002 891#define SCARLETT2_USB_GET_METER 0x00001001 892#define SCARLETT2_USB_GET_MIX 0x00002001 893#define SCARLETT2_USB_SET_MIX 0x00002002 894#define SCARLETT2_USB_GET_MUX 0x00003001 895#define SCARLETT2_USB_SET_MUX 0x00003002 896#define SCARLETT2_USB_GET_SYNC 0x00006004 897#define SCARLETT2_USB_GET_DATA 0x00800000 898#define SCARLETT2_USB_SET_DATA 0x00800001 899#define SCARLETT2_USB_DATA_CMD 0x00800002 900 901#define SCARLETT2_USB_CONFIG_SAVE 6 902 903#define SCARLETT2_USB_VOLUME_STATUS_OFFSET 0x31 904#define SCARLETT2_USB_METER_LEVELS_GET_MAGIC 1 905 906/* volume status is read together (matches scarlett2_config_items[1]) */ 907struct scarlett2_usb_volume_status { 908 /* dim/mute buttons */ 909 u8 dim_mute[SCARLETT2_DIM_MUTE_COUNT]; 910 911 u8 pad1; 912 913 /* software volume setting */ 914 s16 sw_vol[SCARLETT2_ANALOGUE_MAX]; 915 916 /* actual volume of output inc. dim (-18dB) */ 917 s16 hw_vol[SCARLETT2_ANALOGUE_MAX]; 918 919 /* internal mute buttons */ 920 u8 mute_switch[SCARLETT2_ANALOGUE_MAX]; 921 922 /* sw (0) or hw (1) controlled */ 923 u8 sw_hw_switch[SCARLETT2_ANALOGUE_MAX]; 924 925 u8 pad3[6]; 926 927 /* front panel volume knob */ 928 s16 master_vol; 929} __packed; 930 931/* Configuration parameters that can be read and written */ 932enum { 933 SCARLETT2_CONFIG_DIM_MUTE = 0, 934 SCARLETT2_CONFIG_LINE_OUT_VOLUME = 1, 935 SCARLETT2_CONFIG_MUTE_SWITCH = 2, 936 SCARLETT2_CONFIG_SW_HW_SWITCH = 3, 937 SCARLETT2_CONFIG_LEVEL_SWITCH = 4, 938 SCARLETT2_CONFIG_PAD_SWITCH = 5, 939 SCARLETT2_CONFIG_MSD_SWITCH = 6, 940 SCARLETT2_CONFIG_AIR_SWITCH = 7, 941 SCARLETT2_CONFIG_STANDALONE_SWITCH = 8, 942 SCARLETT2_CONFIG_PHANTOM_SWITCH = 9, 943 SCARLETT2_CONFIG_PHANTOM_PERSISTENCE = 10, 944 SCARLETT2_CONFIG_DIRECT_MONITOR = 11, 945 SCARLETT2_CONFIG_MONITOR_OTHER_SWITCH = 12, 946 SCARLETT2_CONFIG_MONITOR_OTHER_ENABLE = 13, 947 SCARLETT2_CONFIG_TALKBACK_MAP = 14, 948 SCARLETT2_CONFIG_COUNT = 15 949}; 950 951/* Location, size, and activation command number for the configuration 952 * parameters. Size is in bits and may be 1, 8, or 16. 953 */ 954struct scarlett2_config { 955 u8 offset; 956 u8 size; 957 u8 activate; 958}; 959 960static const struct scarlett2_config 961 scarlett2_config_items[SCARLETT2_CONFIG_SET_COUNT] 962 [SCARLETT2_CONFIG_COUNT] = 963 964/* Devices without a mixer (Gen 3 Solo and 2i2) */ 965{ { 966 [SCARLETT2_CONFIG_MSD_SWITCH] = { 967 .offset = 0x04, .size = 8, .activate = 6 }, 968 969 [SCARLETT2_CONFIG_PHANTOM_PERSISTENCE] = { 970 .offset = 0x05, .size = 8, .activate = 6 }, 971 972 [SCARLETT2_CONFIG_PHANTOM_SWITCH] = { 973 .offset = 0x06, .size = 8, .activate = 3 }, 974 975 [SCARLETT2_CONFIG_DIRECT_MONITOR] = { 976 .offset = 0x07, .size = 8, .activate = 4 }, 977 978 [SCARLETT2_CONFIG_LEVEL_SWITCH] = { 979 .offset = 0x08, .size = 1, .activate = 7 }, 980 981 [SCARLETT2_CONFIG_AIR_SWITCH] = { 982 .offset = 0x09, .size = 1, .activate = 8 }, 983 984/* Gen 2 devices: 6i6, 18i8, 18i20 */ 985}, { 986 [SCARLETT2_CONFIG_DIM_MUTE] = { 987 .offset = 0x31, .size = 8, .activate = 2 }, 988 989 [SCARLETT2_CONFIG_LINE_OUT_VOLUME] = { 990 .offset = 0x34, .size = 16, .activate = 1 }, 991 992 [SCARLETT2_CONFIG_MUTE_SWITCH] = { 993 .offset = 0x5c, .size = 8, .activate = 1 }, 994 995 [SCARLETT2_CONFIG_SW_HW_SWITCH] = { 996 .offset = 0x66, .size = 8, .activate = 3 }, 997 998 [SCARLETT2_CONFIG_LEVEL_SWITCH] = { 999 .offset = 0x7c, .size = 8, .activate = 7 }, 1000 1001 [SCARLETT2_CONFIG_PAD_SWITCH] = { 1002 .offset = 0x84, .size = 8, .activate = 8 }, 1003 1004 [SCARLETT2_CONFIG_STANDALONE_SWITCH] = { 1005 .offset = 0x8d, .size = 8, .activate = 6 }, 1006 1007/* Gen 3 devices: 4i4, 8i6, 18i8, 18i20 */ 1008}, { 1009 [SCARLETT2_CONFIG_DIM_MUTE] = { 1010 .offset = 0x31, .size = 8, .activate = 2 }, 1011 1012 [SCARLETT2_CONFIG_LINE_OUT_VOLUME] = { 1013 .offset = 0x34, .size = 16, .activate = 1 }, 1014 1015 [SCARLETT2_CONFIG_MUTE_SWITCH] = { 1016 .offset = 0x5c, .size = 8, .activate = 1 }, 1017 1018 [SCARLETT2_CONFIG_SW_HW_SWITCH] = { 1019 .offset = 0x66, .size = 8, .activate = 3 }, 1020 1021 [SCARLETT2_CONFIG_LEVEL_SWITCH] = { 1022 .offset = 0x7c, .size = 8, .activate = 7 }, 1023 1024 [SCARLETT2_CONFIG_PAD_SWITCH] = { 1025 .offset = 0x84, .size = 8, .activate = 8 }, 1026 1027 [SCARLETT2_CONFIG_AIR_SWITCH] = { 1028 .offset = 0x8c, .size = 8, .activate = 8 }, 1029 1030 [SCARLETT2_CONFIG_STANDALONE_SWITCH] = { 1031 .offset = 0x95, .size = 8, .activate = 6 }, 1032 1033 [SCARLETT2_CONFIG_PHANTOM_SWITCH] = { 1034 .offset = 0x9c, .size = 1, .activate = 8 }, 1035 1036 [SCARLETT2_CONFIG_MSD_SWITCH] = { 1037 .offset = 0x9d, .size = 8, .activate = 6 }, 1038 1039 [SCARLETT2_CONFIG_PHANTOM_PERSISTENCE] = { 1040 .offset = 0x9e, .size = 8, .activate = 6 }, 1041 1042 [SCARLETT2_CONFIG_MONITOR_OTHER_SWITCH] = { 1043 .offset = 0x9f, .size = 1, .activate = 10 }, 1044 1045 [SCARLETT2_CONFIG_MONITOR_OTHER_ENABLE] = { 1046 .offset = 0xa0, .size = 1, .activate = 10 }, 1047 1048 [SCARLETT2_CONFIG_TALKBACK_MAP] = { 1049 .offset = 0xb0, .size = 16, .activate = 10 }, 1050} }; 1051 1052/* proprietary request/response format */ 1053struct scarlett2_usb_packet { 1054 __le32 cmd; 1055 __le16 size; 1056 __le16 seq; 1057 __le32 error; 1058 __le32 pad; 1059 u8 data[]; 1060}; 1061 1062static void scarlett2_fill_request_header(struct scarlett2_data *private, 1063 struct scarlett2_usb_packet *req, 1064 u32 cmd, u16 req_size) 1065{ 1066 /* sequence must go up by 1 for each request */ 1067 u16 seq = private->scarlett2_seq++; 1068 1069 req->cmd = cpu_to_le32(cmd); 1070 req->size = cpu_to_le16(req_size); 1071 req->seq = cpu_to_le16(seq); 1072 req->error = 0; 1073 req->pad = 0; 1074} 1075 1076static int scarlett2_usb_tx(struct usb_device *dev, int interface, 1077 void *buf, u16 size) 1078{ 1079 return snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0), 1080 SCARLETT2_USB_CMD_REQ, 1081 USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_OUT, 1082 0, interface, buf, size); 1083} 1084 1085static int scarlett2_usb_rx(struct usb_device *dev, int interface, 1086 u32 usb_req, void *buf, u16 size) 1087{ 1088 return snd_usb_ctl_msg(dev, usb_rcvctrlpipe(dev, 0), 1089 usb_req, 1090 USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_IN, 1091 0, interface, buf, size); 1092} 1093 1094/* Send a proprietary format request to the Scarlett interface */ 1095static int scarlett2_usb( 1096 struct usb_mixer_interface *mixer, u32 cmd, 1097 void *req_data, u16 req_size, void *resp_data, u16 resp_size) 1098{ 1099 struct scarlett2_data *private = mixer->private_data; 1100 struct usb_device *dev = mixer->chip->dev; 1101 struct scarlett2_usb_packet *req, *resp = NULL; 1102 size_t req_buf_size = struct_size(req, data, req_size); 1103 size_t resp_buf_size = struct_size(resp, data, resp_size); 1104 int err; 1105 1106 req = kmalloc(req_buf_size, GFP_KERNEL); 1107 if (!req) { 1108 err = -ENOMEM; 1109 goto error; 1110 } 1111 1112 resp = kmalloc(resp_buf_size, GFP_KERNEL); 1113 if (!resp) { 1114 err = -ENOMEM; 1115 goto error; 1116 } 1117 1118 mutex_lock(&private->usb_mutex); 1119 1120 /* build request message and send it */ 1121 1122 scarlett2_fill_request_header(private, req, cmd, req_size); 1123 1124 if (req_size) 1125 memcpy(req->data, req_data, req_size); 1126 1127 err = scarlett2_usb_tx(dev, private->bInterfaceNumber, 1128 req, req_buf_size); 1129 1130 if (err != req_buf_size) { 1131 usb_audio_err( 1132 mixer->chip, 1133 "Scarlett Gen 2/3 USB request result cmd %x was %d\n", 1134 cmd, err); 1135 err = -EINVAL; 1136 goto unlock; 1137 } 1138 1139 /* send a second message to get the response */ 1140 1141 err = scarlett2_usb_rx(dev, private->bInterfaceNumber, 1142 SCARLETT2_USB_CMD_RESP, 1143 resp, resp_buf_size); 1144 1145 /* validate the response */ 1146 1147 if (err != resp_buf_size) { 1148 usb_audio_err( 1149 mixer->chip, 1150 "Scarlett Gen 2/3 USB response result cmd %x was %d " 1151 "expected %zu\n", 1152 cmd, err, resp_buf_size); 1153 err = -EINVAL; 1154 goto unlock; 1155 } 1156 1157 /* cmd/seq/size should match except when initialising 1158 * seq sent = 1, response = 0 1159 */ 1160 if (resp->cmd != req->cmd || 1161 (resp->seq != req->seq && 1162 (le16_to_cpu(req->seq) != 1 || resp->seq != 0)) || 1163 resp_size != le16_to_cpu(resp->size) || 1164 resp->error || 1165 resp->pad) { 1166 usb_audio_err( 1167 mixer->chip, 1168 "Scarlett Gen 2/3 USB invalid response; " 1169 "cmd tx/rx %d/%d seq %d/%d size %d/%d " 1170 "error %d pad %d\n", 1171 le32_to_cpu(req->cmd), le32_to_cpu(resp->cmd), 1172 le16_to_cpu(req->seq), le16_to_cpu(resp->seq), 1173 resp_size, le16_to_cpu(resp->size), 1174 le32_to_cpu(resp->error), 1175 le32_to_cpu(resp->pad)); 1176 err = -EINVAL; 1177 goto unlock; 1178 } 1179 1180 if (resp_data && resp_size > 0) 1181 memcpy(resp_data, resp->data, resp_size); 1182 1183unlock: 1184 mutex_unlock(&private->usb_mutex); 1185error: 1186 kfree(req); 1187 kfree(resp); 1188 return err; 1189} 1190 1191/* Send a USB message to get data; result placed in *buf */ 1192static int scarlett2_usb_get( 1193 struct usb_mixer_interface *mixer, 1194 int offset, void *buf, int size) 1195{ 1196 struct { 1197 __le32 offset; 1198 __le32 size; 1199 } __packed req; 1200 1201 req.offset = cpu_to_le32(offset); 1202 req.size = cpu_to_le32(size); 1203 return scarlett2_usb(mixer, SCARLETT2_USB_GET_DATA, 1204 &req, sizeof(req), buf, size); 1205} 1206 1207/* Send a USB message to get configuration parameters; result placed in *buf */ 1208static int scarlett2_usb_get_config( 1209 struct usb_mixer_interface *mixer, 1210 int config_item_num, int count, void *buf) 1211{ 1212 struct scarlett2_data *private = mixer->private_data; 1213 const struct scarlett2_device_info *info = private->info; 1214 const struct scarlett2_config *config_item = 1215 &scarlett2_config_items[info->config_set][config_item_num]; 1216 int size, err, i; 1217 u8 *buf_8; 1218 u8 value; 1219 1220 /* For byte-sized parameters, retrieve directly into buf */ 1221 if (config_item->size >= 8) { 1222 size = config_item->size / 8 * count; 1223 err = scarlett2_usb_get(mixer, config_item->offset, buf, size); 1224 if (err < 0) 1225 return err; 1226 if (size == 2) { 1227 u16 *buf_16 = buf; 1228 1229 for (i = 0; i < count; i++, buf_16++) 1230 *buf_16 = le16_to_cpu(*(__le16 *)buf_16); 1231 } 1232 return 0; 1233 } 1234 1235 /* For bit-sized parameters, retrieve into value */ 1236 err = scarlett2_usb_get(mixer, config_item->offset, &value, 1); 1237 if (err < 0) 1238 return err; 1239 1240 /* then unpack from value into buf[] */ 1241 buf_8 = buf; 1242 for (i = 0; i < 8 && i < count; i++, value >>= 1) 1243 *buf_8++ = value & 1; 1244 1245 return 0; 1246} 1247 1248/* Send SCARLETT2_USB_DATA_CMD SCARLETT2_USB_CONFIG_SAVE */ 1249static void scarlett2_config_save(struct usb_mixer_interface *mixer) 1250{ 1251 __le32 req = cpu_to_le32(SCARLETT2_USB_CONFIG_SAVE); 1252 1253 scarlett2_usb(mixer, SCARLETT2_USB_DATA_CMD, 1254 &req, sizeof(u32), 1255 NULL, 0); 1256} 1257 1258/* Delayed work to save config */ 1259static void scarlett2_config_save_work(struct work_struct *work) 1260{ 1261 struct scarlett2_data *private = 1262 container_of(work, struct scarlett2_data, work.work); 1263 1264 scarlett2_config_save(private->mixer); 1265} 1266 1267/* Send a USB message to set a SCARLETT2_CONFIG_* parameter */ 1268static int scarlett2_usb_set_config( 1269 struct usb_mixer_interface *mixer, 1270 int config_item_num, int index, int value) 1271{ 1272 struct scarlett2_data *private = mixer->private_data; 1273 const struct scarlett2_device_info *info = private->info; 1274 const struct scarlett2_config *config_item = 1275 &scarlett2_config_items[info->config_set][config_item_num]; 1276 struct { 1277 __le32 offset; 1278 __le32 bytes; 1279 __le32 value; 1280 } __packed req; 1281 __le32 req2; 1282 int offset, size; 1283 int err; 1284 1285 /* Cancel any pending NVRAM save */ 1286 cancel_delayed_work_sync(&private->work); 1287 1288 /* Convert config_item->size in bits to size in bytes and 1289 * calculate offset 1290 */ 1291 if (config_item->size >= 8) { 1292 size = config_item->size / 8; 1293 offset = config_item->offset + index * size; 1294 1295 /* If updating a bit, retrieve the old value, set/clear the 1296 * bit as needed, and update value 1297 */ 1298 } else { 1299 u8 tmp; 1300 1301 size = 1; 1302 offset = config_item->offset; 1303 1304 scarlett2_usb_get(mixer, offset, &tmp, 1); 1305 if (value) 1306 tmp |= (1 << index); 1307 else 1308 tmp &= ~(1 << index); 1309 1310 value = tmp; 1311 } 1312 1313 /* Send the configuration parameter data */ 1314 req.offset = cpu_to_le32(offset); 1315 req.bytes = cpu_to_le32(size); 1316 req.value = cpu_to_le32(value); 1317 err = scarlett2_usb(mixer, SCARLETT2_USB_SET_DATA, 1318 &req, sizeof(u32) * 2 + size, 1319 NULL, 0); 1320 if (err < 0) 1321 return err; 1322 1323 /* Activate the change */ 1324 req2 = cpu_to_le32(config_item->activate); 1325 err = scarlett2_usb(mixer, SCARLETT2_USB_DATA_CMD, 1326 &req2, sizeof(req2), NULL, 0); 1327 if (err < 0) 1328 return err; 1329 1330 /* Schedule the change to be written to NVRAM */ 1331 if (config_item->activate != SCARLETT2_USB_CONFIG_SAVE) 1332 schedule_delayed_work(&private->work, msecs_to_jiffies(2000)); 1333 1334 return 0; 1335} 1336 1337/* Send a USB message to get sync status; result placed in *sync */ 1338static int scarlett2_usb_get_sync_status( 1339 struct usb_mixer_interface *mixer, 1340 u8 *sync) 1341{ 1342 __le32 data; 1343 int err; 1344 1345 err = scarlett2_usb(mixer, SCARLETT2_USB_GET_SYNC, 1346 NULL, 0, &data, sizeof(data)); 1347 if (err < 0) 1348 return err; 1349 1350 *sync = !!data; 1351 return 0; 1352} 1353 1354/* Send a USB message to get volume status; result placed in *buf */ 1355static int scarlett2_usb_get_volume_status( 1356 struct usb_mixer_interface *mixer, 1357 struct scarlett2_usb_volume_status *buf) 1358{ 1359 return scarlett2_usb_get(mixer, SCARLETT2_USB_VOLUME_STATUS_OFFSET, 1360 buf, sizeof(*buf)); 1361} 1362 1363/* Send a USB message to get the volumes for all inputs of one mix 1364 * and put the values into private->mix[] 1365 */ 1366static int scarlett2_usb_get_mix(struct usb_mixer_interface *mixer, 1367 int mix_num) 1368{ 1369 struct scarlett2_data *private = mixer->private_data; 1370 const struct scarlett2_device_info *info = private->info; 1371 1372 int num_mixer_in = 1373 info->port_count[SCARLETT2_PORT_TYPE_MIX][SCARLETT2_PORT_OUT]; 1374 int err, i, j, k; 1375 1376 struct { 1377 __le16 mix_num; 1378 __le16 count; 1379 } __packed req; 1380 1381 __le16 data[SCARLETT2_INPUT_MIX_MAX]; 1382 1383 req.mix_num = cpu_to_le16(mix_num); 1384 req.count = cpu_to_le16(num_mixer_in); 1385 1386 err = scarlett2_usb(mixer, SCARLETT2_USB_GET_MIX, 1387 &req, sizeof(req), 1388 data, num_mixer_in * sizeof(u16)); 1389 if (err < 0) 1390 return err; 1391 1392 for (i = 0, j = mix_num * num_mixer_in; i < num_mixer_in; i++, j++) { 1393 u16 mixer_value = le16_to_cpu(data[i]); 1394 1395 for (k = 0; k < SCARLETT2_MIXER_VALUE_COUNT; k++) 1396 if (scarlett2_mixer_values[k] >= mixer_value) 1397 break; 1398 if (k == SCARLETT2_MIXER_VALUE_COUNT) 1399 k = SCARLETT2_MIXER_MAX_VALUE; 1400 private->mix[j] = k; 1401 } 1402 1403 return 0; 1404} 1405 1406/* Send a USB message to set the volumes for all inputs of one mix 1407 * (values obtained from private->mix[]) 1408 */ 1409static int scarlett2_usb_set_mix(struct usb_mixer_interface *mixer, 1410 int mix_num) 1411{ 1412 struct scarlett2_data *private = mixer->private_data; 1413 const struct scarlett2_device_info *info = private->info; 1414 1415 struct { 1416 __le16 mix_num; 1417 __le16 data[SCARLETT2_INPUT_MIX_MAX]; 1418 } __packed req; 1419 1420 int i, j; 1421 int num_mixer_in = 1422 info->port_count[SCARLETT2_PORT_TYPE_MIX][SCARLETT2_PORT_OUT]; 1423 1424 req.mix_num = cpu_to_le16(mix_num); 1425 1426 for (i = 0, j = mix_num * num_mixer_in; i < num_mixer_in; i++, j++) 1427 req.data[i] = cpu_to_le16( 1428 scarlett2_mixer_values[private->mix[j]] 1429 ); 1430 1431 return scarlett2_usb(mixer, SCARLETT2_USB_SET_MIX, 1432 &req, (num_mixer_in + 1) * sizeof(u16), 1433 NULL, 0); 1434} 1435 1436/* Convert a port number index (per info->port_count) to a hardware ID */ 1437static u32 scarlett2_mux_src_num_to_id( 1438 const int port_count[][SCARLETT2_PORT_DIRNS], int num) 1439{ 1440 int port_type; 1441 1442 for (port_type = 0; 1443 port_type < SCARLETT2_PORT_TYPE_COUNT; 1444 port_type++) { 1445 if (num < port_count[port_type][SCARLETT2_PORT_IN]) 1446 return scarlett2_ports[port_type].id | num; 1447 num -= port_count[port_type][SCARLETT2_PORT_IN]; 1448 } 1449 1450 /* Oops */ 1451 return 0; 1452} 1453 1454/* Convert a hardware ID to a port number index */ 1455static u32 scarlett2_mux_id_to_num( 1456 const int port_count[][SCARLETT2_PORT_DIRNS], int direction, u32 id) 1457{ 1458 int port_type; 1459 int port_num = 0; 1460 1461 for (port_type = 0; 1462 port_type < SCARLETT2_PORT_TYPE_COUNT; 1463 port_type++) { 1464 int base = scarlett2_ports[port_type].id; 1465 int count = port_count[port_type][direction]; 1466 1467 if (id >= base && id < base + count) 1468 return port_num + id - base; 1469 port_num += count; 1470 } 1471 1472 /* Oops */ 1473 return -1; 1474} 1475 1476/* Convert one mux entry from the interface and load into private->mux[] */ 1477static void scarlett2_usb_populate_mux(struct scarlett2_data *private, 1478 u32 mux_entry) 1479{ 1480 const struct scarlett2_device_info *info = private->info; 1481 const int (*port_count)[SCARLETT2_PORT_DIRNS] = info->port_count; 1482 1483 int dst_idx, src_idx; 1484 1485 dst_idx = scarlett2_mux_id_to_num(port_count, SCARLETT2_PORT_OUT, 1486 mux_entry & 0xFFF); 1487 if (dst_idx < 0) 1488 return; 1489 1490 if (dst_idx >= private->num_mux_dsts) { 1491 usb_audio_err(private->mixer->chip, 1492 "BUG: scarlett2_mux_id_to_num(%06x, OUT): %d >= %d", 1493 mux_entry, dst_idx, private->num_mux_dsts); 1494 return; 1495 } 1496 1497 src_idx = scarlett2_mux_id_to_num(port_count, SCARLETT2_PORT_IN, 1498 mux_entry >> 12); 1499 if (src_idx < 0) 1500 return; 1501 1502 if (src_idx >= private->num_mux_srcs) { 1503 usb_audio_err(private->mixer->chip, 1504 "BUG: scarlett2_mux_id_to_num(%06x, IN): %d >= %d", 1505 mux_entry, src_idx, private->num_mux_srcs); 1506 return; 1507 } 1508 1509 private->mux[dst_idx] = src_idx; 1510} 1511 1512/* Send USB message to get mux inputs and then populate private->mux[] */ 1513static int scarlett2_usb_get_mux(struct usb_mixer_interface *mixer) 1514{ 1515 struct scarlett2_data *private = mixer->private_data; 1516 int count = private->num_mux_dsts; 1517 int err, i; 1518 1519 struct { 1520 __le16 num; 1521 __le16 count; 1522 } __packed req; 1523 1524 __le32 data[SCARLETT2_MUX_MAX]; 1525 1526 private->mux_updated = 0; 1527 1528 req.num = 0; 1529 req.count = cpu_to_le16(count); 1530 1531 err = scarlett2_usb(mixer, SCARLETT2_USB_GET_MUX, 1532 &req, sizeof(req), 1533 data, count * sizeof(u32)); 1534 if (err < 0) 1535 return err; 1536 1537 for (i = 0; i < count; i++) 1538 scarlett2_usb_populate_mux(private, le32_to_cpu(data[i])); 1539 1540 return 0; 1541} 1542 1543/* Send USB messages to set mux inputs */ 1544static int scarlett2_usb_set_mux(struct usb_mixer_interface *mixer) 1545{ 1546 struct scarlett2_data *private = mixer->private_data; 1547 const struct scarlett2_device_info *info = private->info; 1548 const int (*port_count)[SCARLETT2_PORT_DIRNS] = info->port_count; 1549 int table; 1550 1551 struct { 1552 __le16 pad; 1553 __le16 num; 1554 __le32 data[SCARLETT2_MUX_MAX]; 1555 } __packed req; 1556 1557 req.pad = 0; 1558 1559 /* set mux settings for each rate */ 1560 for (table = 0; table < SCARLETT2_MUX_TABLES; table++) { 1561 const struct scarlett2_mux_entry *entry; 1562 1563 /* i counts over the output array */ 1564 int i = 0, err; 1565 1566 req.num = cpu_to_le16(table); 1567 1568 /* loop through each entry */ 1569 for (entry = info->mux_assignment[table]; 1570 entry->count; 1571 entry++) { 1572 int j; 1573 int port_type = entry->port_type; 1574 int port_idx = entry->start; 1575 int mux_idx = scarlett2_get_port_start_num(port_count, 1576 SCARLETT2_PORT_OUT, port_type) + port_idx; 1577 int dst_id = scarlett2_ports[port_type].id + port_idx; 1578 1579 /* Empty slots */ 1580 if (!dst_id) { 1581 for (j = 0; j < entry->count; j++) 1582 req.data[i++] = 0; 1583 continue; 1584 } 1585 1586 /* Non-empty mux slots use the lower 12 bits 1587 * for the destination and next 12 bits for 1588 * the source 1589 */ 1590 for (j = 0; j < entry->count; j++) { 1591 int src_id = scarlett2_mux_src_num_to_id( 1592 port_count, private->mux[mux_idx++]); 1593 req.data[i++] = cpu_to_le32(dst_id | 1594 src_id << 12); 1595 dst_id++; 1596 } 1597 } 1598 1599 err = scarlett2_usb(mixer, SCARLETT2_USB_SET_MUX, 1600 &req, (i + 1) * sizeof(u32), 1601 NULL, 0); 1602 if (err < 0) 1603 return err; 1604 } 1605 1606 return 0; 1607} 1608 1609/* Send USB message to get meter levels */ 1610static int scarlett2_usb_get_meter_levels(struct usb_mixer_interface *mixer, 1611 u16 num_meters, u16 *levels) 1612{ 1613 struct { 1614 __le16 pad; 1615 __le16 num_meters; 1616 __le32 magic; 1617 } __packed req; 1618 u32 resp[SCARLETT2_MAX_METERS]; 1619 int i, err; 1620 1621 req.pad = 0; 1622 req.num_meters = cpu_to_le16(num_meters); 1623 req.magic = cpu_to_le32(SCARLETT2_USB_METER_LEVELS_GET_MAGIC); 1624 err = scarlett2_usb(mixer, SCARLETT2_USB_GET_METER, 1625 &req, sizeof(req), resp, num_meters * sizeof(u32)); 1626 if (err < 0) 1627 return err; 1628 1629 /* copy, convert to u16 */ 1630 for (i = 0; i < num_meters; i++) 1631 levels[i] = resp[i]; 1632 1633 return 0; 1634} 1635 1636/*** Control Functions ***/ 1637 1638/* helper function to create a new control */ 1639static int scarlett2_add_new_ctl(struct usb_mixer_interface *mixer, 1640 const struct snd_kcontrol_new *ncontrol, 1641 int index, int channels, const char *name, 1642 struct snd_kcontrol **kctl_return) 1643{ 1644 struct snd_kcontrol *kctl; 1645 struct usb_mixer_elem_info *elem; 1646 int err; 1647 1648 elem = kzalloc(sizeof(*elem), GFP_KERNEL); 1649 if (!elem) 1650 return -ENOMEM; 1651 1652 /* We set USB_MIXER_BESPOKEN type, so that the core USB mixer code 1653 * ignores them for resume and other operations. 1654 * Also, the head.id field is set to 0, as we don't use this field. 1655 */ 1656 elem->head.mixer = mixer; 1657 elem->control = index; 1658 elem->head.id = 0; 1659 elem->channels = channels; 1660 elem->val_type = USB_MIXER_BESPOKEN; 1661 1662 kctl = snd_ctl_new1(ncontrol, elem); 1663 if (!kctl) { 1664 kfree(elem); 1665 return -ENOMEM; 1666 } 1667 kctl->private_free = snd_usb_mixer_elem_free; 1668 1669 strscpy(kctl->id.name, name, sizeof(kctl->id.name)); 1670 1671 err = snd_usb_mixer_add_control(&elem->head, kctl); 1672 if (err < 0) 1673 return err; 1674 1675 if (kctl_return) 1676 *kctl_return = kctl; 1677 1678 return 0; 1679} 1680 1681/*** Sync Control ***/ 1682 1683/* Update sync control after receiving notification that the status 1684 * has changed 1685 */ 1686static int scarlett2_update_sync(struct usb_mixer_interface *mixer) 1687{ 1688 struct scarlett2_data *private = mixer->private_data; 1689 1690 private->sync_updated = 0; 1691 return scarlett2_usb_get_sync_status(mixer, &private->sync); 1692} 1693 1694static int scarlett2_sync_ctl_info(struct snd_kcontrol *kctl, 1695 struct snd_ctl_elem_info *uinfo) 1696{ 1697 static const char *texts[2] = { 1698 "Unlocked", "Locked" 1699 }; 1700 return snd_ctl_enum_info(uinfo, 1, 2, texts); 1701} 1702 1703static int scarlett2_sync_ctl_get(struct snd_kcontrol *kctl, 1704 struct snd_ctl_elem_value *ucontrol) 1705{ 1706 struct usb_mixer_elem_info *elem = kctl->private_data; 1707 struct usb_mixer_interface *mixer = elem->head.mixer; 1708 struct scarlett2_data *private = mixer->private_data; 1709 1710 mutex_lock(&private->data_mutex); 1711 if (private->sync_updated) 1712 scarlett2_update_sync(mixer); 1713 ucontrol->value.enumerated.item[0] = private->sync; 1714 mutex_unlock(&private->data_mutex); 1715 1716 return 0; 1717} 1718 1719static const struct snd_kcontrol_new scarlett2_sync_ctl = { 1720 .iface = SNDRV_CTL_ELEM_IFACE_MIXER, 1721 .access = SNDRV_CTL_ELEM_ACCESS_READ, 1722 .name = "", 1723 .info = scarlett2_sync_ctl_info, 1724 .get = scarlett2_sync_ctl_get 1725}; 1726 1727static int scarlett2_add_sync_ctl(struct usb_mixer_interface *mixer) 1728{ 1729 struct scarlett2_data *private = mixer->private_data; 1730 1731 /* devices without a mixer also don't support reporting sync status */ 1732 if (private->info->config_set == SCARLETT2_CONFIG_SET_NO_MIXER) 1733 return 0; 1734 1735 return scarlett2_add_new_ctl(mixer, &scarlett2_sync_ctl, 1736 0, 1, "Sync Status", &private->sync_ctl); 1737} 1738 1739/*** Analogue Line Out Volume Controls ***/ 1740 1741/* Update hardware volume controls after receiving notification that 1742 * they have changed 1743 */ 1744static int scarlett2_update_volumes(struct usb_mixer_interface *mixer) 1745{ 1746 struct scarlett2_data *private = mixer->private_data; 1747 const struct scarlett2_device_info *info = private->info; 1748 const int (*port_count)[SCARLETT2_PORT_DIRNS] = info->port_count; 1749 struct scarlett2_usb_volume_status volume_status; 1750 int num_line_out = 1751 port_count[SCARLETT2_PORT_TYPE_ANALOGUE][SCARLETT2_PORT_OUT]; 1752 int err, i; 1753 int mute; 1754 1755 private->vol_updated = 0; 1756 1757 err = scarlett2_usb_get_volume_status(mixer, &volume_status); 1758 if (err < 0) 1759 return err; 1760 1761 private->master_vol = clamp( 1762 volume_status.master_vol + SCARLETT2_VOLUME_BIAS, 1763 0, SCARLETT2_VOLUME_BIAS); 1764 1765 if (info->line_out_hw_vol) 1766 for (i = 0; i < SCARLETT2_DIM_MUTE_COUNT; i++) 1767 private->dim_mute[i] = !!volume_status.dim_mute[i]; 1768 1769 mute = private->dim_mute[SCARLETT2_BUTTON_MUTE]; 1770 1771 for (i = 0; i < num_line_out; i++) 1772 if (private->vol_sw_hw_switch[i]) { 1773 private->vol[i] = private->master_vol; 1774 private->mute_switch[i] = mute; 1775 } 1776 1777 return 0; 1778} 1779 1780static int scarlett2_volume_ctl_info(struct snd_kcontrol *kctl, 1781 struct snd_ctl_elem_info *uinfo) 1782{ 1783 struct usb_mixer_elem_info *elem = kctl->private_data; 1784 1785 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER; 1786 uinfo->count = elem->channels; 1787 uinfo->value.integer.min = 0; 1788 uinfo->value.integer.max = SCARLETT2_VOLUME_BIAS; 1789 uinfo->value.integer.step = 1; 1790 return 0; 1791} 1792 1793static int scarlett2_master_volume_ctl_get(struct snd_kcontrol *kctl, 1794 struct snd_ctl_elem_value *ucontrol) 1795{ 1796 struct usb_mixer_elem_info *elem = kctl->private_data; 1797 struct usb_mixer_interface *mixer = elem->head.mixer; 1798 struct scarlett2_data *private = mixer->private_data; 1799 1800 mutex_lock(&private->data_mutex); 1801 if (private->vol_updated) 1802 scarlett2_update_volumes(mixer); 1803 mutex_unlock(&private->data_mutex); 1804 1805 ucontrol->value.integer.value[0] = private->master_vol; 1806 return 0; 1807} 1808 1809static int line_out_remap(struct scarlett2_data *private, int index) 1810{ 1811 const struct scarlett2_device_info *info = private->info; 1812 1813 if (!info->line_out_remap_enable) 1814 return index; 1815 return info->line_out_remap[index]; 1816} 1817 1818static int scarlett2_volume_ctl_get(struct snd_kcontrol *kctl, 1819 struct snd_ctl_elem_value *ucontrol) 1820{ 1821 struct usb_mixer_elem_info *elem = kctl->private_data; 1822 struct usb_mixer_interface *mixer = elem->head.mixer; 1823 struct scarlett2_data *private = mixer->private_data; 1824 int index = line_out_remap(private, elem->control); 1825 1826 mutex_lock(&private->data_mutex); 1827 if (private->vol_updated) 1828 scarlett2_update_volumes(mixer); 1829 mutex_unlock(&private->data_mutex); 1830 1831 ucontrol->value.integer.value[0] = private->vol[index]; 1832 return 0; 1833} 1834 1835static int scarlett2_volume_ctl_put(struct snd_kcontrol *kctl, 1836 struct snd_ctl_elem_value *ucontrol) 1837{ 1838 struct usb_mixer_elem_info *elem = kctl->private_data; 1839 struct usb_mixer_interface *mixer = elem->head.mixer; 1840 struct scarlett2_data *private = mixer->private_data; 1841 int index = line_out_remap(private, elem->control); 1842 int oval, val, err = 0; 1843 1844 mutex_lock(&private->data_mutex); 1845 1846 oval = private->vol[index]; 1847 val = ucontrol->value.integer.value[0]; 1848 1849 if (oval == val) 1850 goto unlock; 1851 1852 private->vol[index] = val; 1853 err = scarlett2_usb_set_config(mixer, SCARLETT2_CONFIG_LINE_OUT_VOLUME, 1854 index, val - SCARLETT2_VOLUME_BIAS); 1855 if (err == 0) 1856 err = 1; 1857 1858unlock: 1859 mutex_unlock(&private->data_mutex); 1860 return err; 1861} 1862 1863static const DECLARE_TLV_DB_MINMAX( 1864 db_scale_scarlett2_gain, -SCARLETT2_VOLUME_BIAS * 100, 0 1865); 1866 1867static const struct snd_kcontrol_new scarlett2_master_volume_ctl = { 1868 .iface = SNDRV_CTL_ELEM_IFACE_MIXER, 1869 .access = SNDRV_CTL_ELEM_ACCESS_READ | 1870 SNDRV_CTL_ELEM_ACCESS_TLV_READ, 1871 .name = "", 1872 .info = scarlett2_volume_ctl_info, 1873 .get = scarlett2_master_volume_ctl_get, 1874 .private_value = 0, /* max value */ 1875 .tlv = { .p = db_scale_scarlett2_gain } 1876}; 1877 1878static const struct snd_kcontrol_new scarlett2_line_out_volume_ctl = { 1879 .iface = SNDRV_CTL_ELEM_IFACE_MIXER, 1880 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE | 1881 SNDRV_CTL_ELEM_ACCESS_TLV_READ, 1882 .name = "", 1883 .info = scarlett2_volume_ctl_info, 1884 .get = scarlett2_volume_ctl_get, 1885 .put = scarlett2_volume_ctl_put, 1886 .private_value = 0, /* max value */ 1887 .tlv = { .p = db_scale_scarlett2_gain } 1888}; 1889 1890/*** Mute Switch Controls ***/ 1891 1892static int scarlett2_mute_ctl_get(struct snd_kcontrol *kctl, 1893 struct snd_ctl_elem_value *ucontrol) 1894{ 1895 struct usb_mixer_elem_info *elem = kctl->private_data; 1896 struct usb_mixer_interface *mixer = elem->head.mixer; 1897 struct scarlett2_data *private = mixer->private_data; 1898 int index = line_out_remap(private, elem->control); 1899 1900 mutex_lock(&private->data_mutex); 1901 if (private->vol_updated) 1902 scarlett2_update_volumes(mixer); 1903 mutex_unlock(&private->data_mutex); 1904 1905 ucontrol->value.integer.value[0] = private->mute_switch[index]; 1906 return 0; 1907} 1908 1909static int scarlett2_mute_ctl_put(struct snd_kcontrol *kctl, 1910 struct snd_ctl_elem_value *ucontrol) 1911{ 1912 struct usb_mixer_elem_info *elem = kctl->private_data; 1913 struct usb_mixer_interface *mixer = elem->head.mixer; 1914 struct scarlett2_data *private = mixer->private_data; 1915 int index = line_out_remap(private, elem->control); 1916 int oval, val, err = 0; 1917 1918 mutex_lock(&private->data_mutex); 1919 1920 oval = private->mute_switch[index]; 1921 val = !!ucontrol->value.integer.value[0]; 1922 1923 if (oval == val) 1924 goto unlock; 1925 1926 private->mute_switch[index] = val; 1927 1928 /* Send mute change to the device */ 1929 err = scarlett2_usb_set_config(mixer, SCARLETT2_CONFIG_MUTE_SWITCH, 1930 index, val); 1931 if (err == 0) 1932 err = 1; 1933 1934unlock: 1935 mutex_unlock(&private->data_mutex); 1936 return err; 1937} 1938 1939static const struct snd_kcontrol_new scarlett2_mute_ctl = { 1940 .iface = SNDRV_CTL_ELEM_IFACE_MIXER, 1941 .name = "", 1942 .info = snd_ctl_boolean_mono_info, 1943 .get = scarlett2_mute_ctl_get, 1944 .put = scarlett2_mute_ctl_put, 1945}; 1946 1947/*** HW/SW Volume Switch Controls ***/ 1948 1949static void scarlett2_sw_hw_ctl_ro(struct scarlett2_data *private, int index) 1950{ 1951 private->sw_hw_ctls[index]->vd[0].access &= 1952 ~SNDRV_CTL_ELEM_ACCESS_WRITE; 1953} 1954 1955static void scarlett2_sw_hw_ctl_rw(struct scarlett2_data *private, int index) 1956{ 1957 private->sw_hw_ctls[index]->vd[0].access |= 1958 SNDRV_CTL_ELEM_ACCESS_WRITE; 1959} 1960 1961static int scarlett2_sw_hw_enum_ctl_info(struct snd_kcontrol *kctl, 1962 struct snd_ctl_elem_info *uinfo) 1963{ 1964 static const char *const values[2] = { 1965 "SW", "HW" 1966 }; 1967 1968 return snd_ctl_enum_info(uinfo, 1, 2, values); 1969} 1970 1971static int scarlett2_sw_hw_enum_ctl_get(struct snd_kcontrol *kctl, 1972 struct snd_ctl_elem_value *ucontrol) 1973{ 1974 struct usb_mixer_elem_info *elem = kctl->private_data; 1975 struct scarlett2_data *private = elem->head.mixer->private_data; 1976 int index = line_out_remap(private, elem->control); 1977 1978 ucontrol->value.enumerated.item[0] = private->vol_sw_hw_switch[index]; 1979 return 0; 1980} 1981 1982static void scarlett2_vol_ctl_set_writable(struct usb_mixer_interface *mixer, 1983 int index, int value) 1984{ 1985 struct scarlett2_data *private = mixer->private_data; 1986 struct snd_card *card = mixer->chip->card; 1987 1988 /* Set/Clear write bits */ 1989 if (value) { 1990 private->vol_ctls[index]->vd[0].access |= 1991 SNDRV_CTL_ELEM_ACCESS_WRITE; 1992 private->mute_ctls[index]->vd[0].access |= 1993 SNDRV_CTL_ELEM_ACCESS_WRITE; 1994 } else { 1995 private->vol_ctls[index]->vd[0].access &= 1996 ~SNDRV_CTL_ELEM_ACCESS_WRITE; 1997 private->mute_ctls[index]->vd[0].access &= 1998 ~SNDRV_CTL_ELEM_ACCESS_WRITE; 1999 } 2000 2001 /* Notify of write bit and possible value change */ 2002 snd_ctl_notify(card, 2003 SNDRV_CTL_EVENT_MASK_VALUE | SNDRV_CTL_EVENT_MASK_INFO, 2004 &private->vol_ctls[index]->id); 2005 snd_ctl_notify(card, 2006 SNDRV_CTL_EVENT_MASK_VALUE | SNDRV_CTL_EVENT_MASK_INFO, 2007 &private->mute_ctls[index]->id); 2008} 2009 2010static int scarlett2_sw_hw_change(struct usb_mixer_interface *mixer, 2011 int ctl_index, int val) 2012{ 2013 struct scarlett2_data *private = mixer->private_data; 2014 int index = line_out_remap(private, ctl_index); 2015 int err; 2016 2017 private->vol_sw_hw_switch[index] = val; 2018 2019 /* Change access mode to RO (hardware controlled volume) 2020 * or RW (software controlled volume) 2021 */ 2022 scarlett2_vol_ctl_set_writable(mixer, ctl_index, !val); 2023 2024 /* Reset volume/mute to master volume/mute */ 2025 private->vol[index] = private->master_vol; 2026 private->mute_switch[index] = private->dim_mute[SCARLETT2_BUTTON_MUTE]; 2027 2028 /* Set SW volume to current HW volume */ 2029 err = scarlett2_usb_set_config( 2030 mixer, SCARLETT2_CONFIG_LINE_OUT_VOLUME, 2031 index, private->master_vol - SCARLETT2_VOLUME_BIAS); 2032 if (err < 0) 2033 return err; 2034 2035 /* Set SW mute to current HW mute */ 2036 err = scarlett2_usb_set_config( 2037 mixer, SCARLETT2_CONFIG_MUTE_SWITCH, 2038 index, private->dim_mute[SCARLETT2_BUTTON_MUTE]); 2039 if (err < 0) 2040 return err; 2041 2042 /* Send SW/HW switch change to the device */ 2043 return scarlett2_usb_set_config(mixer, SCARLETT2_CONFIG_SW_HW_SWITCH, 2044 index, val); 2045} 2046 2047static int scarlett2_sw_hw_enum_ctl_put(struct snd_kcontrol *kctl, 2048 struct snd_ctl_elem_value *ucontrol) 2049{ 2050 struct usb_mixer_elem_info *elem = kctl->private_data; 2051 struct usb_mixer_interface *mixer = elem->head.mixer; 2052 struct scarlett2_data *private = mixer->private_data; 2053 int ctl_index = elem->control; 2054 int index = line_out_remap(private, ctl_index); 2055 int oval, val, err = 0; 2056 2057 mutex_lock(&private->data_mutex); 2058 2059 oval = private->vol_sw_hw_switch[index]; 2060 val = !!ucontrol->value.enumerated.item[0]; 2061 2062 if (oval == val) 2063 goto unlock; 2064 2065 err = scarlett2_sw_hw_change(mixer, ctl_index, val); 2066 if (err == 0) 2067 err = 1; 2068 2069unlock: 2070 mutex_unlock(&private->data_mutex); 2071 return err; 2072} 2073 2074static const struct snd_kcontrol_new scarlett2_sw_hw_enum_ctl = { 2075 .iface = SNDRV_CTL_ELEM_IFACE_MIXER, 2076 .name = "", 2077 .info = scarlett2_sw_hw_enum_ctl_info, 2078 .get = scarlett2_sw_hw_enum_ctl_get, 2079 .put = scarlett2_sw_hw_enum_ctl_put, 2080}; 2081 2082/*** Line Level/Instrument Level Switch Controls ***/ 2083 2084static int scarlett2_update_input_other(struct usb_mixer_interface *mixer) 2085{ 2086 struct scarlett2_data *private = mixer->private_data; 2087 const struct scarlett2_device_info *info = private->info; 2088 2089 private->input_other_updated = 0; 2090 2091 if (info->level_input_count) { 2092 int err = scarlett2_usb_get_config( 2093 mixer, SCARLETT2_CONFIG_LEVEL_SWITCH, 2094 info->level_input_count + info->level_input_first, 2095 private->level_switch); 2096 if (err < 0) 2097 return err; 2098 } 2099 2100 if (info->pad_input_count) { 2101 int err = scarlett2_usb_get_config( 2102 mixer, SCARLETT2_CONFIG_PAD_SWITCH, 2103 info->pad_input_count, private->pad_switch); 2104 if (err < 0) 2105 return err; 2106 } 2107 2108 if (info->air_input_count) { 2109 int err = scarlett2_usb_get_config( 2110 mixer, SCARLETT2_CONFIG_AIR_SWITCH, 2111 info->air_input_count, private->air_switch); 2112 if (err < 0) 2113 return err; 2114 } 2115 2116 if (info->phantom_count) { 2117 int err = scarlett2_usb_get_config( 2118 mixer, SCARLETT2_CONFIG_PHANTOM_SWITCH, 2119 info->phantom_count, private->phantom_switch); 2120 if (err < 0) 2121 return err; 2122 2123 err = scarlett2_usb_get_config( 2124 mixer, SCARLETT2_CONFIG_PHANTOM_PERSISTENCE, 2125 1, &private->phantom_persistence); 2126 if (err < 0) 2127 return err; 2128 } 2129 2130 return 0; 2131} 2132 2133static int scarlett2_level_enum_ctl_info(struct snd_kcontrol *kctl, 2134 struct snd_ctl_elem_info *uinfo) 2135{ 2136 static const char *const values[2] = { 2137 "Line", "Inst" 2138 }; 2139 2140 return snd_ctl_enum_info(uinfo, 1, 2, values); 2141} 2142 2143static int scarlett2_level_enum_ctl_get(struct snd_kcontrol *kctl, 2144 struct snd_ctl_elem_value *ucontrol) 2145{ 2146 struct usb_mixer_elem_info *elem = kctl->private_data; 2147 struct usb_mixer_interface *mixer = elem->head.mixer; 2148 struct scarlett2_data *private = mixer->private_data; 2149 const struct scarlett2_device_info *info = private->info; 2150 2151 int index = elem->control + info->level_input_first; 2152 2153 mutex_lock(&private->data_mutex); 2154 if (private->input_other_updated) 2155 scarlett2_update_input_other(mixer); 2156 ucontrol->value.enumerated.item[0] = private->level_switch[index]; 2157 mutex_unlock(&private->data_mutex); 2158 2159 return 0; 2160} 2161 2162static int scarlett2_level_enum_ctl_put(struct snd_kcontrol *kctl, 2163 struct snd_ctl_elem_value *ucontrol) 2164{ 2165 struct usb_mixer_elem_info *elem = kctl->private_data; 2166 struct usb_mixer_interface *mixer = elem->head.mixer; 2167 struct scarlett2_data *private = mixer->private_data; 2168 const struct scarlett2_device_info *info = private->info; 2169 2170 int index = elem->control + info->level_input_first; 2171 int oval, val, err = 0; 2172 2173 mutex_lock(&private->data_mutex); 2174 2175 oval = private->level_switch[index]; 2176 val = !!ucontrol->value.enumerated.item[0]; 2177 2178 if (oval == val) 2179 goto unlock; 2180 2181 private->level_switch[index] = val; 2182 2183 /* Send switch change to the device */ 2184 err = scarlett2_usb_set_config(mixer, SCARLETT2_CONFIG_LEVEL_SWITCH, 2185 index, val); 2186 if (err == 0) 2187 err = 1; 2188 2189unlock: 2190 mutex_unlock(&private->data_mutex); 2191 return err; 2192} 2193 2194static const struct snd_kcontrol_new scarlett2_level_enum_ctl = { 2195 .iface = SNDRV_CTL_ELEM_IFACE_MIXER, 2196 .name = "", 2197 .info = scarlett2_level_enum_ctl_info, 2198 .get = scarlett2_level_enum_ctl_get, 2199 .put = scarlett2_level_enum_ctl_put, 2200}; 2201 2202/*** Pad Switch Controls ***/ 2203 2204static int scarlett2_pad_ctl_get(struct snd_kcontrol *kctl, 2205 struct snd_ctl_elem_value *ucontrol) 2206{ 2207 struct usb_mixer_elem_info *elem = kctl->private_data; 2208 struct usb_mixer_interface *mixer = elem->head.mixer; 2209 struct scarlett2_data *private = mixer->private_data; 2210 2211 mutex_lock(&private->data_mutex); 2212 if (private->input_other_updated) 2213 scarlett2_update_input_other(mixer); 2214 ucontrol->value.integer.value[0] = 2215 private->pad_switch[elem->control]; 2216 mutex_unlock(&private->data_mutex); 2217 2218 return 0; 2219} 2220 2221static int scarlett2_pad_ctl_put(struct snd_kcontrol *kctl, 2222 struct snd_ctl_elem_value *ucontrol) 2223{ 2224 struct usb_mixer_elem_info *elem = kctl->private_data; 2225 struct usb_mixer_interface *mixer = elem->head.mixer; 2226 struct scarlett2_data *private = mixer->private_data; 2227 2228 int index = elem->control; 2229 int oval, val, err = 0; 2230 2231 mutex_lock(&private->data_mutex); 2232 2233 oval = private->pad_switch[index]; 2234 val = !!ucontrol->value.integer.value[0]; 2235 2236 if (oval == val) 2237 goto unlock; 2238 2239 private->pad_switch[index] = val; 2240 2241 /* Send switch change to the device */ 2242 err = scarlett2_usb_set_config(mixer, SCARLETT2_CONFIG_PAD_SWITCH, 2243 index, val); 2244 if (err == 0) 2245 err = 1; 2246 2247unlock: 2248 mutex_unlock(&private->data_mutex); 2249 return err; 2250} 2251 2252static const struct snd_kcontrol_new scarlett2_pad_ctl = { 2253 .iface = SNDRV_CTL_ELEM_IFACE_MIXER, 2254 .name = "", 2255 .info = snd_ctl_boolean_mono_info, 2256 .get = scarlett2_pad_ctl_get, 2257 .put = scarlett2_pad_ctl_put, 2258}; 2259 2260/*** Air Switch Controls ***/ 2261 2262static int scarlett2_air_ctl_get(struct snd_kcontrol *kctl, 2263 struct snd_ctl_elem_value *ucontrol) 2264{ 2265 struct usb_mixer_elem_info *elem = kctl->private_data; 2266 struct usb_mixer_interface *mixer = elem->head.mixer; 2267 struct scarlett2_data *private = mixer->private_data; 2268 2269 mutex_lock(&private->data_mutex); 2270 if (private->input_other_updated) 2271 scarlett2_update_input_other(mixer); 2272 ucontrol->value.integer.value[0] = private->air_switch[elem->control]; 2273 mutex_unlock(&private->data_mutex); 2274 2275 return 0; 2276} 2277 2278static int scarlett2_air_ctl_put(struct snd_kcontrol *kctl, 2279 struct snd_ctl_elem_value *ucontrol) 2280{ 2281 struct usb_mixer_elem_info *elem = kctl->private_data; 2282 struct usb_mixer_interface *mixer = elem->head.mixer; 2283 struct scarlett2_data *private = mixer->private_data; 2284 2285 int index = elem->control; 2286 int oval, val, err = 0; 2287 2288 mutex_lock(&private->data_mutex); 2289 2290 oval = private->air_switch[index]; 2291 val = !!ucontrol->value.integer.value[0]; 2292 2293 if (oval == val) 2294 goto unlock; 2295 2296 private->air_switch[index] = val; 2297 2298 /* Send switch change to the device */ 2299 err = scarlett2_usb_set_config(mixer, SCARLETT2_CONFIG_AIR_SWITCH, 2300 index, val); 2301 if (err == 0) 2302 err = 1; 2303 2304unlock: 2305 mutex_unlock(&private->data_mutex); 2306 return err; 2307} 2308 2309static const struct snd_kcontrol_new scarlett2_air_ctl = { 2310 .iface = SNDRV_CTL_ELEM_IFACE_MIXER, 2311 .name = "", 2312 .info = snd_ctl_boolean_mono_info, 2313 .get = scarlett2_air_ctl_get, 2314 .put = scarlett2_air_ctl_put, 2315}; 2316 2317/*** Phantom Switch Controls ***/ 2318 2319static int scarlett2_phantom_ctl_get(struct snd_kcontrol *kctl, 2320 struct snd_ctl_elem_value *ucontrol) 2321{ 2322 struct usb_mixer_elem_info *elem = kctl->private_data; 2323 struct usb_mixer_interface *mixer = elem->head.mixer; 2324 struct scarlett2_data *private = mixer->private_data; 2325 2326 mutex_lock(&private->data_mutex); 2327 if (private->input_other_updated) 2328 scarlett2_update_input_other(mixer); 2329 ucontrol->value.integer.value[0] = 2330 private->phantom_switch[elem->control]; 2331 mutex_unlock(&private->data_mutex); 2332 2333 return 0; 2334} 2335 2336static int scarlett2_phantom_ctl_put(struct snd_kcontrol *kctl, 2337 struct snd_ctl_elem_value *ucontrol) 2338{ 2339 struct usb_mixer_elem_info *elem = kctl->private_data; 2340 struct usb_mixer_interface *mixer = elem->head.mixer; 2341 struct scarlett2_data *private = mixer->private_data; 2342 2343 int index = elem->control; 2344 int oval, val, err = 0; 2345 2346 mutex_lock(&private->data_mutex); 2347 2348 oval = private->phantom_switch[index]; 2349 val = !!ucontrol->value.integer.value[0]; 2350 2351 if (oval == val) 2352 goto unlock; 2353 2354 private->phantom_switch[index] = val; 2355 2356 /* Send switch change to the device */ 2357 err = scarlett2_usb_set_config(mixer, SCARLETT2_CONFIG_PHANTOM_SWITCH, 2358 index, val); 2359 if (err == 0) 2360 err = 1; 2361 2362unlock: 2363 mutex_unlock(&private->data_mutex); 2364 return err; 2365} 2366 2367static const struct snd_kcontrol_new scarlett2_phantom_ctl = { 2368 .iface = SNDRV_CTL_ELEM_IFACE_MIXER, 2369 .name = "", 2370 .info = snd_ctl_boolean_mono_info, 2371 .get = scarlett2_phantom_ctl_get, 2372 .put = scarlett2_phantom_ctl_put, 2373}; 2374 2375/*** Phantom Persistence Control ***/ 2376 2377static int scarlett2_phantom_persistence_ctl_get( 2378 struct snd_kcontrol *kctl, struct snd_ctl_elem_value *ucontrol) 2379{ 2380 struct usb_mixer_elem_info *elem = kctl->private_data; 2381 struct scarlett2_data *private = elem->head.mixer->private_data; 2382 2383 ucontrol->value.integer.value[0] = private->phantom_persistence; 2384 return 0; 2385} 2386 2387static int scarlett2_phantom_persistence_ctl_put( 2388 struct snd_kcontrol *kctl, struct snd_ctl_elem_value *ucontrol) 2389{ 2390 struct usb_mixer_elem_info *elem = kctl->private_data; 2391 struct usb_mixer_interface *mixer = elem->head.mixer; 2392 struct scarlett2_data *private = mixer->private_data; 2393 2394 int index = elem->control; 2395 int oval, val, err = 0; 2396 2397 mutex_lock(&private->data_mutex); 2398 2399 oval = private->phantom_persistence; 2400 val = !!ucontrol->value.integer.value[0]; 2401 2402 if (oval == val) 2403 goto unlock; 2404 2405 private->phantom_persistence = val; 2406 2407 /* Send switch change to the device */ 2408 err = scarlett2_usb_set_config( 2409 mixer, SCARLETT2_CONFIG_PHANTOM_PERSISTENCE, index, val); 2410 if (err == 0) 2411 err = 1; 2412 2413unlock: 2414 mutex_unlock(&private->data_mutex); 2415 return err; 2416} 2417 2418static const struct snd_kcontrol_new scarlett2_phantom_persistence_ctl = { 2419 .iface = SNDRV_CTL_ELEM_IFACE_MIXER, 2420 .name = "", 2421 .info = snd_ctl_boolean_mono_info, 2422 .get = scarlett2_phantom_persistence_ctl_get, 2423 .put = scarlett2_phantom_persistence_ctl_put, 2424}; 2425 2426/*** Direct Monitor Control ***/ 2427 2428static int scarlett2_update_monitor_other(struct usb_mixer_interface *mixer) 2429{ 2430 struct scarlett2_data *private = mixer->private_data; 2431 const struct scarlett2_device_info *info = private->info; 2432 int err; 2433 2434 /* monitor_other_enable[0] enables speaker switching 2435 * monitor_other_enable[1] enables talkback 2436 */ 2437 u8 monitor_other_enable[2]; 2438 2439 /* monitor_other_switch[0] activates the alternate speakers 2440 * monitor_other_switch[1] activates talkback 2441 */ 2442 u8 monitor_other_switch[2]; 2443 2444 private->monitor_other_updated = 0; 2445 2446 if (info->direct_monitor) 2447 return scarlett2_usb_get_config( 2448 mixer, SCARLETT2_CONFIG_DIRECT_MONITOR, 2449 1, &private->direct_monitor_switch); 2450 2451 /* if it doesn't do speaker switching then it also doesn't do 2452 * talkback 2453 */ 2454 if (!info->has_speaker_switching) 2455 return 0; 2456 2457 err = scarlett2_usb_get_config( 2458 mixer, SCARLETT2_CONFIG_MONITOR_OTHER_ENABLE, 2459 2, monitor_other_enable); 2460 if (err < 0) 2461 return err; 2462 2463 err = scarlett2_usb_get_config( 2464 mixer, SCARLETT2_CONFIG_MONITOR_OTHER_SWITCH, 2465 2, monitor_other_switch); 2466 if (err < 0) 2467 return err; 2468 2469 if (!monitor_other_enable[0]) 2470 private->speaker_switching_switch = 0; 2471 else 2472 private->speaker_switching_switch = monitor_other_switch[0] + 1; 2473 2474 if (info->has_talkback) { 2475 const int (*port_count)[SCARLETT2_PORT_DIRNS] = 2476 info->port_count; 2477 int num_mixes = 2478 port_count[SCARLETT2_PORT_TYPE_MIX][SCARLETT2_PORT_IN]; 2479 u16 bitmap; 2480 int i; 2481 2482 if (!monitor_other_enable[1]) 2483 private->talkback_switch = 0; 2484 else 2485 private->talkback_switch = monitor_other_switch[1] + 1; 2486 2487 err = scarlett2_usb_get_config(mixer, 2488 SCARLETT2_CONFIG_TALKBACK_MAP, 2489 1, &bitmap); 2490 if (err < 0) 2491 return err; 2492 for (i = 0; i < num_mixes; i++, bitmap >>= 1) 2493 private->talkback_map[i] = bitmap & 1; 2494 } 2495 2496 return 0; 2497} 2498 2499static int scarlett2_direct_monitor_ctl_get( 2500 struct snd_kcontrol *kctl, struct snd_ctl_elem_value *ucontrol) 2501{ 2502 struct usb_mixer_elem_info *elem = kctl->private_data; 2503 struct usb_mixer_interface *mixer = elem->head.mixer; 2504 struct scarlett2_data *private = elem->head.mixer->private_data; 2505 2506 mutex_lock(&private->data_mutex); 2507 if (private->monitor_other_updated) 2508 scarlett2_update_monitor_other(mixer); 2509 ucontrol->value.enumerated.item[0] = private->direct_monitor_switch; 2510 mutex_unlock(&private->data_mutex); 2511 2512 return 0; 2513} 2514 2515static int scarlett2_direct_monitor_ctl_put( 2516 struct snd_kcontrol *kctl, struct snd_ctl_elem_value *ucontrol) 2517{ 2518 struct usb_mixer_elem_info *elem = kctl->private_data; 2519 struct usb_mixer_interface *mixer = elem->head.mixer; 2520 struct scarlett2_data *private = mixer->private_data; 2521 2522 int index = elem->control; 2523 int oval, val, err = 0; 2524 2525 mutex_lock(&private->data_mutex); 2526 2527 oval = private->direct_monitor_switch; 2528 val = min(ucontrol->value.enumerated.item[0], 2U); 2529 2530 if (oval == val) 2531 goto unlock; 2532 2533 private->direct_monitor_switch = val; 2534 2535 /* Send switch change to the device */ 2536 err = scarlett2_usb_set_config( 2537 mixer, SCARLETT2_CONFIG_DIRECT_MONITOR, index, val); 2538 if (err == 0) 2539 err = 1; 2540 2541unlock: 2542 mutex_unlock(&private->data_mutex); 2543 return err; 2544} 2545 2546static int scarlett2_direct_monitor_stereo_enum_ctl_info( 2547 struct snd_kcontrol *kctl, struct snd_ctl_elem_info *uinfo) 2548{ 2549 static const char *const values[3] = { 2550 "Off", "Mono", "Stereo" 2551 }; 2552 2553 return snd_ctl_enum_info(uinfo, 1, 3, values); 2554} 2555 2556/* Direct Monitor for Solo is mono-only and only needs a boolean control 2557 * Direct Monitor for 2i2 is selectable between Off/Mono/Stereo 2558 */ 2559static const struct snd_kcontrol_new scarlett2_direct_monitor_ctl[2] = { 2560 { 2561 .iface = SNDRV_CTL_ELEM_IFACE_MIXER, 2562 .name = "", 2563 .info = snd_ctl_boolean_mono_info, 2564 .get = scarlett2_direct_monitor_ctl_get, 2565 .put = scarlett2_direct_monitor_ctl_put, 2566 }, 2567 { 2568 .iface = SNDRV_CTL_ELEM_IFACE_MIXER, 2569 .name = "", 2570 .info = scarlett2_direct_monitor_stereo_enum_ctl_info, 2571 .get = scarlett2_direct_monitor_ctl_get, 2572 .put = scarlett2_direct_monitor_ctl_put, 2573 } 2574}; 2575 2576static int scarlett2_add_direct_monitor_ctl(struct usb_mixer_interface *mixer) 2577{ 2578 struct scarlett2_data *private = mixer->private_data; 2579 const struct scarlett2_device_info *info = private->info; 2580 const char *s; 2581 2582 if (!info->direct_monitor) 2583 return 0; 2584 2585 s = info->direct_monitor == 1 2586 ? "Direct Monitor Playback Switch" 2587 : "Direct Monitor Playback Enum"; 2588 2589 return scarlett2_add_new_ctl( 2590 mixer, &scarlett2_direct_monitor_ctl[info->direct_monitor - 1], 2591 0, 1, s, &private->direct_monitor_ctl); 2592} 2593 2594/*** Speaker Switching Control ***/ 2595 2596static int scarlett2_speaker_switch_enum_ctl_info( 2597 struct snd_kcontrol *kctl, struct snd_ctl_elem_info *uinfo) 2598{ 2599 static const char *const values[3] = { 2600 "Off", "Main", "Alt" 2601 }; 2602 2603 return snd_ctl_enum_info(uinfo, 1, 3, values); 2604} 2605 2606static int scarlett2_speaker_switch_enum_ctl_get( 2607 struct snd_kcontrol *kctl, struct snd_ctl_elem_value *ucontrol) 2608{ 2609 struct usb_mixer_elem_info *elem = kctl->private_data; 2610 struct usb_mixer_interface *mixer = elem->head.mixer; 2611 struct scarlett2_data *private = mixer->private_data; 2612 2613 mutex_lock(&private->data_mutex); 2614 if (private->monitor_other_updated) 2615 scarlett2_update_monitor_other(mixer); 2616 ucontrol->value.enumerated.item[0] = private->speaker_switching_switch; 2617 mutex_unlock(&private->data_mutex); 2618 2619 return 0; 2620} 2621 2622/* when speaker switching gets enabled, switch the main/alt speakers 2623 * to HW volume and disable those controls 2624 */ 2625static int scarlett2_speaker_switch_enable(struct usb_mixer_interface *mixer) 2626{ 2627 struct snd_card *card = mixer->chip->card; 2628 struct scarlett2_data *private = mixer->private_data; 2629 int i, err; 2630 2631 for (i = 0; i < 4; i++) { 2632 int index = line_out_remap(private, i); 2633 2634 /* switch the main/alt speakers to HW volume */ 2635 if (!private->vol_sw_hw_switch[index]) { 2636 err = scarlett2_sw_hw_change(private->mixer, i, 1); 2637 if (err < 0) 2638 return err; 2639 } 2640 2641 /* disable the line out SW/HW switch */ 2642 scarlett2_sw_hw_ctl_ro(private, i); 2643 snd_ctl_notify(card, 2644 SNDRV_CTL_EVENT_MASK_VALUE | 2645 SNDRV_CTL_EVENT_MASK_INFO, 2646 &private->sw_hw_ctls[i]->id); 2647 } 2648 2649 /* when the next monitor-other notify comes in, update the mux 2650 * configuration 2651 */ 2652 private->speaker_switching_switched = 1; 2653 2654 return 0; 2655} 2656 2657/* when speaker switching gets disabled, reenable the hw/sw controls 2658 * and invalidate the routing 2659 */ 2660static void scarlett2_speaker_switch_disable(struct usb_mixer_interface *mixer) 2661{ 2662 struct snd_card *card = mixer->chip->card; 2663 struct scarlett2_data *private = mixer->private_data; 2664 int i; 2665 2666 /* enable the line out SW/HW switch */ 2667 for (i = 0; i < 4; i++) { 2668 scarlett2_sw_hw_ctl_rw(private, i); 2669 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_INFO, 2670 &private->sw_hw_ctls[i]->id); 2671 } 2672 2673 /* when the next monitor-other notify comes in, update the mux 2674 * configuration 2675 */ 2676 private->speaker_switching_switched = 1; 2677} 2678 2679static int scarlett2_speaker_switch_enum_ctl_put( 2680 struct snd_kcontrol *kctl, struct snd_ctl_elem_value *ucontrol) 2681{ 2682 struct usb_mixer_elem_info *elem = kctl->private_data; 2683 struct usb_mixer_interface *mixer = elem->head.mixer; 2684 struct scarlett2_data *private = mixer->private_data; 2685 2686 int oval, val, err = 0; 2687 2688 mutex_lock(&private->data_mutex); 2689 2690 oval = private->speaker_switching_switch; 2691 val = min(ucontrol->value.enumerated.item[0], 2U); 2692 2693 if (oval == val) 2694 goto unlock; 2695 2696 private->speaker_switching_switch = val; 2697 2698 /* enable/disable speaker switching */ 2699 err = scarlett2_usb_set_config( 2700 mixer, SCARLETT2_CONFIG_MONITOR_OTHER_ENABLE, 2701 0, !!val); 2702 if (err < 0) 2703 goto unlock; 2704 2705 /* if speaker switching is enabled, select main or alt */ 2706 err = scarlett2_usb_set_config( 2707 mixer, SCARLETT2_CONFIG_MONITOR_OTHER_SWITCH, 2708 0, val == 2); 2709 if (err < 0) 2710 goto unlock; 2711 2712 /* update controls if speaker switching gets enabled or disabled */ 2713 if (!oval && val) 2714 err = scarlett2_speaker_switch_enable(mixer); 2715 else if (oval && !val) 2716 scarlett2_speaker_switch_disable(mixer); 2717 2718 if (err == 0) 2719 err = 1; 2720 2721unlock: 2722 mutex_unlock(&private->data_mutex); 2723 return err; 2724} 2725 2726static const struct snd_kcontrol_new scarlett2_speaker_switch_enum_ctl = { 2727 .iface = SNDRV_CTL_ELEM_IFACE_MIXER, 2728 .name = "", 2729 .info = scarlett2_speaker_switch_enum_ctl_info, 2730 .get = scarlett2_speaker_switch_enum_ctl_get, 2731 .put = scarlett2_speaker_switch_enum_ctl_put, 2732}; 2733 2734static int scarlett2_add_speaker_switch_ctl( 2735 struct usb_mixer_interface *mixer) 2736{ 2737 struct scarlett2_data *private = mixer->private_data; 2738 const struct scarlett2_device_info *info = private->info; 2739 2740 if (!info->has_speaker_switching) 2741 return 0; 2742 2743 return scarlett2_add_new_ctl( 2744 mixer, &scarlett2_speaker_switch_enum_ctl, 2745 0, 1, "Speaker Switching Playback Enum", 2746 &private->speaker_switching_ctl); 2747} 2748 2749/*** Talkback and Talkback Map Controls ***/ 2750 2751static int scarlett2_talkback_enum_ctl_info( 2752 struct snd_kcontrol *kctl, struct snd_ctl_elem_info *uinfo) 2753{ 2754 static const char *const values[3] = { 2755 "Disabled", "Off", "On" 2756 }; 2757 2758 return snd_ctl_enum_info(uinfo, 1, 3, values); 2759} 2760 2761static int scarlett2_talkback_enum_ctl_get( 2762 struct snd_kcontrol *kctl, struct snd_ctl_elem_value *ucontrol) 2763{ 2764 struct usb_mixer_elem_info *elem = kctl->private_data; 2765 struct usb_mixer_interface *mixer = elem->head.mixer; 2766 struct scarlett2_data *private = mixer->private_data; 2767 2768 mutex_lock(&private->data_mutex); 2769 if (private->monitor_other_updated) 2770 scarlett2_update_monitor_other(mixer); 2771 ucontrol->value.enumerated.item[0] = private->talkback_switch; 2772 mutex_unlock(&private->data_mutex); 2773 2774 return 0; 2775} 2776 2777static int scarlett2_talkback_enum_ctl_put( 2778 struct snd_kcontrol *kctl, struct snd_ctl_elem_value *ucontrol) 2779{ 2780 struct usb_mixer_elem_info *elem = kctl->private_data; 2781 struct usb_mixer_interface *mixer = elem->head.mixer; 2782 struct scarlett2_data *private = mixer->private_data; 2783 2784 int oval, val, err = 0; 2785 2786 mutex_lock(&private->data_mutex); 2787 2788 oval = private->talkback_switch; 2789 val = min(ucontrol->value.enumerated.item[0], 2U); 2790 2791 if (oval == val) 2792 goto unlock; 2793 2794 private->talkback_switch = val; 2795 2796 /* enable/disable talkback */ 2797 err = scarlett2_usb_set_config( 2798 mixer, SCARLETT2_CONFIG_MONITOR_OTHER_ENABLE, 2799 1, !!val); 2800 if (err < 0) 2801 goto unlock; 2802 2803 /* if talkback is enabled, select main or alt */ 2804 err = scarlett2_usb_set_config( 2805 mixer, SCARLETT2_CONFIG_MONITOR_OTHER_SWITCH, 2806 1, val == 2); 2807 if (err == 0) 2808 err = 1; 2809 2810unlock: 2811 mutex_unlock(&private->data_mutex); 2812 return err; 2813} 2814 2815static const struct snd_kcontrol_new scarlett2_talkback_enum_ctl = { 2816 .iface = SNDRV_CTL_ELEM_IFACE_MIXER, 2817 .name = "", 2818 .info = scarlett2_talkback_enum_ctl_info, 2819 .get = scarlett2_talkback_enum_ctl_get, 2820 .put = scarlett2_talkback_enum_ctl_put, 2821}; 2822 2823static int scarlett2_talkback_map_ctl_get( 2824 struct snd_kcontrol *kctl, struct snd_ctl_elem_value *ucontrol) 2825{ 2826 struct usb_mixer_elem_info *elem = kctl->private_data; 2827 struct usb_mixer_interface *mixer = elem->head.mixer; 2828 struct scarlett2_data *private = mixer->private_data; 2829 int index = elem->control; 2830 2831 ucontrol->value.integer.value[0] = private->talkback_map[index]; 2832 2833 return 0; 2834} 2835 2836static int scarlett2_talkback_map_ctl_put( 2837 struct snd_kcontrol *kctl, struct snd_ctl_elem_value *ucontrol) 2838{ 2839 struct usb_mixer_elem_info *elem = kctl->private_data; 2840 struct usb_mixer_interface *mixer = elem->head.mixer; 2841 struct scarlett2_data *private = mixer->private_data; 2842 const int (*port_count)[SCARLETT2_PORT_DIRNS] = 2843 private->info->port_count; 2844 int num_mixes = port_count[SCARLETT2_PORT_TYPE_MIX][SCARLETT2_PORT_IN]; 2845 2846 int index = elem->control; 2847 int oval, val, err = 0, i; 2848 u16 bitmap = 0; 2849 2850 mutex_lock(&private->data_mutex); 2851 2852 oval = private->talkback_map[index]; 2853 val = !!ucontrol->value.integer.value[0]; 2854 2855 if (oval == val) 2856 goto unlock; 2857 2858 private->talkback_map[index] = val; 2859 2860 for (i = 0; i < num_mixes; i++) 2861 bitmap |= private->talkback_map[i] << i; 2862 2863 /* Send updated bitmap to the device */ 2864 err = scarlett2_usb_set_config(mixer, SCARLETT2_CONFIG_TALKBACK_MAP, 2865 0, bitmap); 2866 if (err == 0) 2867 err = 1; 2868 2869unlock: 2870 mutex_unlock(&private->data_mutex); 2871 return err; 2872} 2873 2874static const struct snd_kcontrol_new scarlett2_talkback_map_ctl = { 2875 .iface = SNDRV_CTL_ELEM_IFACE_MIXER, 2876 .name = "", 2877 .info = snd_ctl_boolean_mono_info, 2878 .get = scarlett2_talkback_map_ctl_get, 2879 .put = scarlett2_talkback_map_ctl_put, 2880}; 2881 2882static int scarlett2_add_talkback_ctls( 2883 struct usb_mixer_interface *mixer) 2884{ 2885 struct scarlett2_data *private = mixer->private_data; 2886 const struct scarlett2_device_info *info = private->info; 2887 const int (*port_count)[SCARLETT2_PORT_DIRNS] = info->port_count; 2888 int num_mixes = port_count[SCARLETT2_PORT_TYPE_MIX][SCARLETT2_PORT_IN]; 2889 int err, i; 2890 char s[SNDRV_CTL_ELEM_ID_NAME_MAXLEN]; 2891 2892 if (!info->has_talkback) 2893 return 0; 2894 2895 err = scarlett2_add_new_ctl( 2896 mixer, &scarlett2_talkback_enum_ctl, 2897 0, 1, "Talkback Playback Enum", 2898 &private->talkback_ctl); 2899 if (err < 0) 2900 return err; 2901 2902 for (i = 0; i < num_mixes; i++) { 2903 snprintf(s, sizeof(s), 2904 "Talkback Mix %c Playback Switch", i + 'A'); 2905 err = scarlett2_add_new_ctl(mixer, &scarlett2_talkback_map_ctl, 2906 i, 1, s, NULL); 2907 if (err < 0) 2908 return err; 2909 } 2910 2911 return 0; 2912} 2913 2914/*** Dim/Mute Controls ***/ 2915 2916static int scarlett2_dim_mute_ctl_get(struct snd_kcontrol *kctl, 2917 struct snd_ctl_elem_value *ucontrol) 2918{ 2919 struct usb_mixer_elem_info *elem = kctl->private_data; 2920 struct usb_mixer_interface *mixer = elem->head.mixer; 2921 struct scarlett2_data *private = mixer->private_data; 2922 2923 mutex_lock(&private->data_mutex); 2924 if (private->vol_updated) 2925 scarlett2_update_volumes(mixer); 2926 mutex_unlock(&private->data_mutex); 2927 2928 ucontrol->value.integer.value[0] = private->dim_mute[elem->control]; 2929 return 0; 2930} 2931 2932static int scarlett2_dim_mute_ctl_put(struct snd_kcontrol *kctl, 2933 struct snd_ctl_elem_value *ucontrol) 2934{ 2935 struct usb_mixer_elem_info *elem = kctl->private_data; 2936 struct usb_mixer_interface *mixer = elem->head.mixer; 2937 struct scarlett2_data *private = mixer->private_data; 2938 const struct scarlett2_device_info *info = private->info; 2939 const int (*port_count)[SCARLETT2_PORT_DIRNS] = info->port_count; 2940 int num_line_out = 2941 port_count[SCARLETT2_PORT_TYPE_ANALOGUE][SCARLETT2_PORT_OUT]; 2942 2943 int index = elem->control; 2944 int oval, val, err = 0, i; 2945 2946 mutex_lock(&private->data_mutex); 2947 2948 oval = private->dim_mute[index]; 2949 val = !!ucontrol->value.integer.value[0]; 2950 2951 if (oval == val) 2952 goto unlock; 2953 2954 private->dim_mute[index] = val; 2955 2956 /* Send switch change to the device */ 2957 err = scarlett2_usb_set_config(mixer, SCARLETT2_CONFIG_DIM_MUTE, 2958 index, val); 2959 if (err == 0) 2960 err = 1; 2961 2962 if (index == SCARLETT2_BUTTON_MUTE) 2963 for (i = 0; i < num_line_out; i++) { 2964 int line_index = line_out_remap(private, i); 2965 2966 if (private->vol_sw_hw_switch[line_index]) { 2967 private->mute_switch[line_index] = val; 2968 snd_ctl_notify(mixer->chip->card, 2969 SNDRV_CTL_EVENT_MASK_VALUE, 2970 &private->mute_ctls[i]->id); 2971 } 2972 } 2973 2974unlock: 2975 mutex_unlock(&private->data_mutex); 2976 return err; 2977} 2978 2979static const struct snd_kcontrol_new scarlett2_dim_mute_ctl = { 2980 .iface = SNDRV_CTL_ELEM_IFACE_MIXER, 2981 .name = "", 2982 .info = snd_ctl_boolean_mono_info, 2983 .get = scarlett2_dim_mute_ctl_get, 2984 .put = scarlett2_dim_mute_ctl_put 2985}; 2986 2987/*** Create the analogue output controls ***/ 2988 2989static int scarlett2_add_line_out_ctls(struct usb_mixer_interface *mixer) 2990{ 2991 struct scarlett2_data *private = mixer->private_data; 2992 const struct scarlett2_device_info *info = private->info; 2993 const int (*port_count)[SCARLETT2_PORT_DIRNS] = info->port_count; 2994 int num_line_out = 2995 port_count[SCARLETT2_PORT_TYPE_ANALOGUE][SCARLETT2_PORT_OUT]; 2996 int err, i; 2997 char s[SNDRV_CTL_ELEM_ID_NAME_MAXLEN]; 2998 2999 /* Add R/O HW volume control */ 3000 if (info->line_out_hw_vol) { 3001 snprintf(s, sizeof(s), "Master HW Playback Volume"); 3002 err = scarlett2_add_new_ctl(mixer, 3003 &scarlett2_master_volume_ctl, 3004 0, 1, s, &private->master_vol_ctl); 3005 if (err < 0) 3006 return err; 3007 } 3008 3009 /* Add volume controls */ 3010 for (i = 0; i < num_line_out; i++) { 3011 int index = line_out_remap(private, i); 3012 3013 /* Fader */ 3014 if (info->line_out_descrs[i]) 3015 snprintf(s, sizeof(s), 3016 "Line %02d (%s) Playback Volume", 3017 i + 1, info->line_out_descrs[i]); 3018 else 3019 snprintf(s, sizeof(s), 3020 "Line %02d Playback Volume", 3021 i + 1); 3022 err = scarlett2_add_new_ctl(mixer, 3023 &scarlett2_line_out_volume_ctl, 3024 i, 1, s, &private->vol_ctls[i]); 3025 if (err < 0) 3026 return err; 3027 3028 /* Mute Switch */ 3029 snprintf(s, sizeof(s), 3030 "Line %02d Mute Playback Switch", 3031 i + 1); 3032 err = scarlett2_add_new_ctl(mixer, 3033 &scarlett2_mute_ctl, 3034 i, 1, s, 3035 &private->mute_ctls[i]); 3036 if (err < 0) 3037 return err; 3038 3039 /* Make the fader and mute controls read-only if the 3040 * SW/HW switch is set to HW 3041 */ 3042 if (private->vol_sw_hw_switch[index]) 3043 scarlett2_vol_ctl_set_writable(mixer, i, 0); 3044 3045 /* SW/HW Switch */ 3046 if (info->line_out_hw_vol) { 3047 snprintf(s, sizeof(s), 3048 "Line Out %02d Volume Control Playback Enum", 3049 i + 1); 3050 err = scarlett2_add_new_ctl(mixer, 3051 &scarlett2_sw_hw_enum_ctl, 3052 i, 1, s, 3053 &private->sw_hw_ctls[i]); 3054 if (err < 0) 3055 return err; 3056 3057 /* Make the switch read-only if the line is 3058 * involved in speaker switching 3059 */ 3060 if (private->speaker_switching_switch && i < 4) 3061 scarlett2_sw_hw_ctl_ro(private, i); 3062 } 3063 } 3064 3065 /* Add dim/mute controls */ 3066 if (info->line_out_hw_vol) 3067 for (i = 0; i < SCARLETT2_DIM_MUTE_COUNT; i++) { 3068 err = scarlett2_add_new_ctl( 3069 mixer, &scarlett2_dim_mute_ctl, 3070 i, 1, scarlett2_dim_mute_names[i], 3071 &private->dim_mute_ctls[i]); 3072 if (err < 0) 3073 return err; 3074 } 3075 3076 return 0; 3077} 3078 3079/*** Create the analogue input controls ***/ 3080 3081static int scarlett2_add_line_in_ctls(struct usb_mixer_interface *mixer) 3082{ 3083 struct scarlett2_data *private = mixer->private_data; 3084 const struct scarlett2_device_info *info = private->info; 3085 int err, i; 3086 char s[SNDRV_CTL_ELEM_ID_NAME_MAXLEN]; 3087 const char *fmt = "Line In %d %s Capture %s"; 3088 const char *fmt2 = "Line In %d-%d %s Capture %s"; 3089 3090 /* Add input level (line/inst) controls */ 3091 for (i = 0; i < info->level_input_count; i++) { 3092 snprintf(s, sizeof(s), fmt, i + 1 + info->level_input_first, 3093 "Level", "Enum"); 3094 err = scarlett2_add_new_ctl(mixer, &scarlett2_level_enum_ctl, 3095 i, 1, s, &private->level_ctls[i]); 3096 if (err < 0) 3097 return err; 3098 } 3099 3100 /* Add input pad controls */ 3101 for (i = 0; i < info->pad_input_count; i++) { 3102 snprintf(s, sizeof(s), fmt, i + 1, "Pad", "Switch"); 3103 err = scarlett2_add_new_ctl(mixer, &scarlett2_pad_ctl, 3104 i, 1, s, &private->pad_ctls[i]); 3105 if (err < 0) 3106 return err; 3107 } 3108 3109 /* Add input air controls */ 3110 for (i = 0; i < info->air_input_count; i++) { 3111 snprintf(s, sizeof(s), fmt, i + 1, "Air", "Switch"); 3112 err = scarlett2_add_new_ctl(mixer, &scarlett2_air_ctl, 3113 i, 1, s, &private->air_ctls[i]); 3114 if (err < 0) 3115 return err; 3116 } 3117 3118 /* Add input phantom controls */ 3119 if (info->inputs_per_phantom == 1) { 3120 for (i = 0; i < info->phantom_count; i++) { 3121 snprintf(s, sizeof(s), fmt, i + 1, 3122 "Phantom Power", "Switch"); 3123 err = scarlett2_add_new_ctl( 3124 mixer, &scarlett2_phantom_ctl, 3125 i, 1, s, &private->phantom_ctls[i]); 3126 if (err < 0) 3127 return err; 3128 } 3129 } else if (info->inputs_per_phantom > 1) { 3130 for (i = 0; i < info->phantom_count; i++) { 3131 int from = i * info->inputs_per_phantom + 1; 3132 int to = (i + 1) * info->inputs_per_phantom; 3133 3134 snprintf(s, sizeof(s), fmt2, from, to, 3135 "Phantom Power", "Switch"); 3136 err = scarlett2_add_new_ctl( 3137 mixer, &scarlett2_phantom_ctl, 3138 i, 1, s, &private->phantom_ctls[i]); 3139 if (err < 0) 3140 return err; 3141 } 3142 } 3143 if (info->phantom_count) { 3144 err = scarlett2_add_new_ctl( 3145 mixer, &scarlett2_phantom_persistence_ctl, 0, 1, 3146 "Phantom Power Persistence Capture Switch", NULL); 3147 if (err < 0) 3148 return err; 3149 } 3150 3151 return 0; 3152} 3153 3154/*** Mixer Volume Controls ***/ 3155 3156static int scarlett2_mixer_ctl_info(struct snd_kcontrol *kctl, 3157 struct snd_ctl_elem_info *uinfo) 3158{ 3159 struct usb_mixer_elem_info *elem = kctl->private_data; 3160 3161 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER; 3162 uinfo->count = elem->channels; 3163 uinfo->value.integer.min = 0; 3164 uinfo->value.integer.max = SCARLETT2_MIXER_MAX_VALUE; 3165 uinfo->value.integer.step = 1; 3166 return 0; 3167} 3168 3169static int scarlett2_mixer_ctl_get(struct snd_kcontrol *kctl, 3170 struct snd_ctl_elem_value *ucontrol) 3171{ 3172 struct usb_mixer_elem_info *elem = kctl->private_data; 3173 struct scarlett2_data *private = elem->head.mixer->private_data; 3174 3175 ucontrol->value.integer.value[0] = private->mix[elem->control]; 3176 return 0; 3177} 3178 3179static int scarlett2_mixer_ctl_put(struct snd_kcontrol *kctl, 3180 struct snd_ctl_elem_value *ucontrol) 3181{ 3182 struct usb_mixer_elem_info *elem = kctl->private_data; 3183 struct usb_mixer_interface *mixer = elem->head.mixer; 3184 struct scarlett2_data *private = mixer->private_data; 3185 const struct scarlett2_device_info *info = private->info; 3186 const int (*port_count)[SCARLETT2_PORT_DIRNS] = info->port_count; 3187 int oval, val, num_mixer_in, mix_num, err = 0; 3188 int index = elem->control; 3189 3190 mutex_lock(&private->data_mutex); 3191 3192 oval = private->mix[index]; 3193 val = ucontrol->value.integer.value[0]; 3194 num_mixer_in = port_count[SCARLETT2_PORT_TYPE_MIX][SCARLETT2_PORT_OUT]; 3195 mix_num = index / num_mixer_in; 3196 3197 if (oval == val) 3198 goto unlock; 3199 3200 private->mix[index] = val; 3201 err = scarlett2_usb_set_mix(mixer, mix_num); 3202 if (err == 0) 3203 err = 1; 3204 3205unlock: 3206 mutex_unlock(&private->data_mutex); 3207 return err; 3208} 3209 3210static const DECLARE_TLV_DB_MINMAX( 3211 db_scale_scarlett2_mixer, 3212 SCARLETT2_MIXER_MIN_DB * 100, 3213 SCARLETT2_MIXER_MAX_DB * 100 3214); 3215 3216static const struct snd_kcontrol_new scarlett2_mixer_ctl = { 3217 .iface = SNDRV_CTL_ELEM_IFACE_MIXER, 3218 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE | 3219 SNDRV_CTL_ELEM_ACCESS_TLV_READ, 3220 .name = "", 3221 .info = scarlett2_mixer_ctl_info, 3222 .get = scarlett2_mixer_ctl_get, 3223 .put = scarlett2_mixer_ctl_put, 3224 .private_value = SCARLETT2_MIXER_MAX_DB, /* max value */ 3225 .tlv = { .p = db_scale_scarlett2_mixer } 3226}; 3227 3228static int scarlett2_add_mixer_ctls(struct usb_mixer_interface *mixer) 3229{ 3230 struct scarlett2_data *private = mixer->private_data; 3231 const struct scarlett2_device_info *info = private->info; 3232 const int (*port_count)[SCARLETT2_PORT_DIRNS] = info->port_count; 3233 int err, i, j; 3234 int index; 3235 char s[SNDRV_CTL_ELEM_ID_NAME_MAXLEN]; 3236 3237 int num_inputs = 3238 port_count[SCARLETT2_PORT_TYPE_MIX][SCARLETT2_PORT_OUT]; 3239 int num_outputs = 3240 port_count[SCARLETT2_PORT_TYPE_MIX][SCARLETT2_PORT_IN]; 3241 3242 for (i = 0, index = 0; i < num_outputs; i++) 3243 for (j = 0; j < num_inputs; j++, index++) { 3244 snprintf(s, sizeof(s), 3245 "Mix %c Input %02d Playback Volume", 3246 'A' + i, j + 1); 3247 err = scarlett2_add_new_ctl(mixer, &scarlett2_mixer_ctl, 3248 index, 1, s, NULL); 3249 if (err < 0) 3250 return err; 3251 } 3252 3253 return 0; 3254} 3255 3256/*** Mux Source Selection Controls ***/ 3257 3258static int scarlett2_mux_src_enum_ctl_info(struct snd_kcontrol *kctl, 3259 struct snd_ctl_elem_info *uinfo) 3260{ 3261 struct usb_mixer_elem_info *elem = kctl->private_data; 3262 struct scarlett2_data *private = elem->head.mixer->private_data; 3263 const struct scarlett2_device_info *info = private->info; 3264 const int (*port_count)[SCARLETT2_PORT_DIRNS] = info->port_count; 3265 unsigned int item = uinfo->value.enumerated.item; 3266 int items = private->num_mux_srcs; 3267 int port_type; 3268 3269 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED; 3270 uinfo->count = elem->channels; 3271 uinfo->value.enumerated.items = items; 3272 3273 if (item >= items) 3274 item = uinfo->value.enumerated.item = items - 1; 3275 3276 for (port_type = 0; 3277 port_type < SCARLETT2_PORT_TYPE_COUNT; 3278 port_type++) { 3279 if (item < port_count[port_type][SCARLETT2_PORT_IN]) { 3280 const struct scarlett2_port *port = 3281 &scarlett2_ports[port_type]; 3282 3283 sprintf(uinfo->value.enumerated.name, 3284 port->src_descr, item + port->src_num_offset); 3285 return 0; 3286 } 3287 item -= port_count[port_type][SCARLETT2_PORT_IN]; 3288 } 3289 3290 return -EINVAL; 3291} 3292 3293static int scarlett2_mux_src_enum_ctl_get(struct snd_kcontrol *kctl, 3294 struct snd_ctl_elem_value *ucontrol) 3295{ 3296 struct usb_mixer_elem_info *elem = kctl->private_data; 3297 struct usb_mixer_interface *mixer = elem->head.mixer; 3298 struct scarlett2_data *private = mixer->private_data; 3299 const struct scarlett2_device_info *info = private->info; 3300 const int (*port_count)[SCARLETT2_PORT_DIRNS] = info->port_count; 3301 int line_out_count = 3302 port_count[SCARLETT2_PORT_TYPE_ANALOGUE][SCARLETT2_PORT_OUT]; 3303 int index = elem->control; 3304 3305 if (index < line_out_count) 3306 index = line_out_remap(private, index); 3307 3308 mutex_lock(&private->data_mutex); 3309 if (private->mux_updated) 3310 scarlett2_usb_get_mux(mixer); 3311 ucontrol->value.enumerated.item[0] = private->mux[index]; 3312 mutex_unlock(&private->data_mutex); 3313 3314 return 0; 3315} 3316 3317static int scarlett2_mux_src_enum_ctl_put(struct snd_kcontrol *kctl, 3318 struct snd_ctl_elem_value *ucontrol) 3319{ 3320 struct usb_mixer_elem_info *elem = kctl->private_data; 3321 struct usb_mixer_interface *mixer = elem->head.mixer; 3322 struct scarlett2_data *private = mixer->private_data; 3323 const struct scarlett2_device_info *info = private->info; 3324 const int (*port_count)[SCARLETT2_PORT_DIRNS] = info->port_count; 3325 int line_out_count = 3326 port_count[SCARLETT2_PORT_TYPE_ANALOGUE][SCARLETT2_PORT_OUT]; 3327 int index = elem->control; 3328 int oval, val, err = 0; 3329 3330 if (index < line_out_count) 3331 index = line_out_remap(private, index); 3332 3333 mutex_lock(&private->data_mutex); 3334 3335 oval = private->mux[index]; 3336 val = min(ucontrol->value.enumerated.item[0], 3337 private->num_mux_srcs - 1U); 3338 3339 if (oval == val) 3340 goto unlock; 3341 3342 private->mux[index] = val; 3343 err = scarlett2_usb_set_mux(mixer); 3344 if (err == 0) 3345 err = 1; 3346 3347unlock: 3348 mutex_unlock(&private->data_mutex); 3349 return err; 3350} 3351 3352static const struct snd_kcontrol_new scarlett2_mux_src_enum_ctl = { 3353 .iface = SNDRV_CTL_ELEM_IFACE_MIXER, 3354 .name = "", 3355 .info = scarlett2_mux_src_enum_ctl_info, 3356 .get = scarlett2_mux_src_enum_ctl_get, 3357 .put = scarlett2_mux_src_enum_ctl_put, 3358}; 3359 3360static int scarlett2_add_mux_enums(struct usb_mixer_interface *mixer) 3361{ 3362 struct scarlett2_data *private = mixer->private_data; 3363 const struct scarlett2_device_info *info = private->info; 3364 const int (*port_count)[SCARLETT2_PORT_DIRNS] = info->port_count; 3365 int port_type, channel, i; 3366 3367 for (i = 0, port_type = 0; 3368 port_type < SCARLETT2_PORT_TYPE_COUNT; 3369 port_type++) { 3370 for (channel = 0; 3371 channel < port_count[port_type][SCARLETT2_PORT_OUT]; 3372 channel++, i++) { 3373 int err; 3374 char s[SNDRV_CTL_ELEM_ID_NAME_MAXLEN]; 3375 const char *const descr = 3376 scarlett2_ports[port_type].dst_descr; 3377 3378 snprintf(s, sizeof(s) - 5, descr, channel + 1); 3379 strcat(s, " Enum"); 3380 3381 err = scarlett2_add_new_ctl(mixer, 3382 &scarlett2_mux_src_enum_ctl, 3383 i, 1, s, 3384 &private->mux_ctls[i]); 3385 if (err < 0) 3386 return err; 3387 } 3388 } 3389 3390 return 0; 3391} 3392 3393/*** Meter Controls ***/ 3394 3395static int scarlett2_meter_ctl_info(struct snd_kcontrol *kctl, 3396 struct snd_ctl_elem_info *uinfo) 3397{ 3398 struct usb_mixer_elem_info *elem = kctl->private_data; 3399 3400 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER; 3401 uinfo->count = elem->channels; 3402 uinfo->value.integer.min = 0; 3403 uinfo->value.integer.max = 4095; 3404 uinfo->value.integer.step = 1; 3405 return 0; 3406} 3407 3408static int scarlett2_meter_ctl_get(struct snd_kcontrol *kctl, 3409 struct snd_ctl_elem_value *ucontrol) 3410{ 3411 struct usb_mixer_elem_info *elem = kctl->private_data; 3412 u16 meter_levels[SCARLETT2_MAX_METERS]; 3413 int i, err; 3414 3415 err = scarlett2_usb_get_meter_levels(elem->head.mixer, elem->channels, 3416 meter_levels); 3417 if (err < 0) 3418 return err; 3419 3420 for (i = 0; i < elem->channels; i++) 3421 ucontrol->value.integer.value[i] = meter_levels[i]; 3422 3423 return 0; 3424} 3425 3426static const struct snd_kcontrol_new scarlett2_meter_ctl = { 3427 .iface = SNDRV_CTL_ELEM_IFACE_PCM, 3428 .access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE, 3429 .name = "", 3430 .info = scarlett2_meter_ctl_info, 3431 .get = scarlett2_meter_ctl_get 3432}; 3433 3434static int scarlett2_add_meter_ctl(struct usb_mixer_interface *mixer) 3435{ 3436 struct scarlett2_data *private = mixer->private_data; 3437 3438 /* devices without a mixer also don't support reporting levels */ 3439 if (private->info->config_set == SCARLETT2_CONFIG_SET_NO_MIXER) 3440 return 0; 3441 3442 return scarlett2_add_new_ctl(mixer, &scarlett2_meter_ctl, 3443 0, private->num_mux_dsts, 3444 "Level Meter", NULL); 3445} 3446 3447/*** MSD Controls ***/ 3448 3449static int scarlett2_msd_ctl_get(struct snd_kcontrol *kctl, 3450 struct snd_ctl_elem_value *ucontrol) 3451{ 3452 struct usb_mixer_elem_info *elem = kctl->private_data; 3453 struct scarlett2_data *private = elem->head.mixer->private_data; 3454 3455 ucontrol->value.integer.value[0] = private->msd_switch; 3456 return 0; 3457} 3458 3459static int scarlett2_msd_ctl_put(struct snd_kcontrol *kctl, 3460 struct snd_ctl_elem_value *ucontrol) 3461{ 3462 struct usb_mixer_elem_info *elem = kctl->private_data; 3463 struct usb_mixer_interface *mixer = elem->head.mixer; 3464 struct scarlett2_data *private = mixer->private_data; 3465 3466 int oval, val, err = 0; 3467 3468 mutex_lock(&private->data_mutex); 3469 3470 oval = private->msd_switch; 3471 val = !!ucontrol->value.integer.value[0]; 3472 3473 if (oval == val) 3474 goto unlock; 3475 3476 private->msd_switch = val; 3477 3478 /* Send switch change to the device */ 3479 err = scarlett2_usb_set_config(mixer, SCARLETT2_CONFIG_MSD_SWITCH, 3480 0, val); 3481 if (err == 0) 3482 err = 1; 3483 3484unlock: 3485 mutex_unlock(&private->data_mutex); 3486 return err; 3487} 3488 3489static const struct snd_kcontrol_new scarlett2_msd_ctl = { 3490 .iface = SNDRV_CTL_ELEM_IFACE_MIXER, 3491 .name = "", 3492 .info = snd_ctl_boolean_mono_info, 3493 .get = scarlett2_msd_ctl_get, 3494 .put = scarlett2_msd_ctl_put, 3495}; 3496 3497static int scarlett2_add_msd_ctl(struct usb_mixer_interface *mixer) 3498{ 3499 struct scarlett2_data *private = mixer->private_data; 3500 const struct scarlett2_device_info *info = private->info; 3501 3502 if (!info->has_msd_mode) 3503 return 0; 3504 3505 /* If MSD mode is off, hide the switch by default */ 3506 if (!private->msd_switch && !(mixer->chip->setup & SCARLETT2_MSD_ENABLE)) 3507 return 0; 3508 3509 /* Add MSD control */ 3510 return scarlett2_add_new_ctl(mixer, &scarlett2_msd_ctl, 3511 0, 1, "MSD Mode Switch", NULL); 3512} 3513 3514/*** Standalone Control ***/ 3515 3516static int scarlett2_standalone_ctl_get(struct snd_kcontrol *kctl, 3517 struct snd_ctl_elem_value *ucontrol) 3518{ 3519 struct usb_mixer_elem_info *elem = kctl->private_data; 3520 struct scarlett2_data *private = elem->head.mixer->private_data; 3521 3522 ucontrol->value.integer.value[0] = private->standalone_switch; 3523 return 0; 3524} 3525 3526static int scarlett2_standalone_ctl_put(struct snd_kcontrol *kctl, 3527 struct snd_ctl_elem_value *ucontrol) 3528{ 3529 struct usb_mixer_elem_info *elem = kctl->private_data; 3530 struct usb_mixer_interface *mixer = elem->head.mixer; 3531 struct scarlett2_data *private = mixer->private_data; 3532 3533 int oval, val, err = 0; 3534 3535 mutex_lock(&private->data_mutex); 3536 3537 oval = private->standalone_switch; 3538 val = !!ucontrol->value.integer.value[0]; 3539 3540 if (oval == val) 3541 goto unlock; 3542 3543 private->standalone_switch = val; 3544 3545 /* Send switch change to the device */ 3546 err = scarlett2_usb_set_config(mixer, 3547 SCARLETT2_CONFIG_STANDALONE_SWITCH, 3548 0, val); 3549 if (err == 0) 3550 err = 1; 3551 3552unlock: 3553 mutex_unlock(&private->data_mutex); 3554 return err; 3555} 3556 3557static const struct snd_kcontrol_new scarlett2_standalone_ctl = { 3558 .iface = SNDRV_CTL_ELEM_IFACE_MIXER, 3559 .name = "", 3560 .info = snd_ctl_boolean_mono_info, 3561 .get = scarlett2_standalone_ctl_get, 3562 .put = scarlett2_standalone_ctl_put, 3563}; 3564 3565static int scarlett2_add_standalone_ctl(struct usb_mixer_interface *mixer) 3566{ 3567 struct scarlett2_data *private = mixer->private_data; 3568 3569 if (private->info->config_set == SCARLETT2_CONFIG_SET_NO_MIXER) 3570 return 0; 3571 3572 /* Add standalone control */ 3573 return scarlett2_add_new_ctl(mixer, &scarlett2_standalone_ctl, 3574 0, 1, "Standalone Switch", NULL); 3575} 3576 3577/*** Cleanup/Suspend Callbacks ***/ 3578 3579static void scarlett2_private_free(struct usb_mixer_interface *mixer) 3580{ 3581 struct scarlett2_data *private = mixer->private_data; 3582 3583 cancel_delayed_work_sync(&private->work); 3584 kfree(private); 3585 mixer->private_data = NULL; 3586} 3587 3588static void scarlett2_private_suspend(struct usb_mixer_interface *mixer) 3589{ 3590 struct scarlett2_data *private = mixer->private_data; 3591 3592 if (cancel_delayed_work_sync(&private->work)) 3593 scarlett2_config_save(private->mixer); 3594} 3595 3596/*** Initialisation ***/ 3597 3598static void scarlett2_count_mux_io(struct scarlett2_data *private) 3599{ 3600 const struct scarlett2_device_info *info = private->info; 3601 const int (*port_count)[SCARLETT2_PORT_DIRNS] = info->port_count; 3602 int port_type, srcs = 0, dsts = 0; 3603 3604 for (port_type = 0; 3605 port_type < SCARLETT2_PORT_TYPE_COUNT; 3606 port_type++) { 3607 srcs += port_count[port_type][SCARLETT2_PORT_IN]; 3608 dsts += port_count[port_type][SCARLETT2_PORT_OUT]; 3609 } 3610 3611 private->num_mux_srcs = srcs; 3612 private->num_mux_dsts = dsts; 3613} 3614 3615/* Look through the interface descriptors for the Focusrite Control 3616 * interface (bInterfaceClass = 255 Vendor Specific Class) and set 3617 * bInterfaceNumber, bEndpointAddress, wMaxPacketSize, and bInterval 3618 * in private 3619 */ 3620static int scarlett2_find_fc_interface(struct usb_device *dev, 3621 struct scarlett2_data *private) 3622{ 3623 struct usb_host_config *config = dev->actconfig; 3624 int i; 3625 3626 for (i = 0; i < config->desc.bNumInterfaces; i++) { 3627 struct usb_interface *intf = config->interface[i]; 3628 struct usb_interface_descriptor *desc = 3629 &intf->altsetting[0].desc; 3630 struct usb_endpoint_descriptor *epd; 3631 3632 if (desc->bInterfaceClass != 255) 3633 continue; 3634 3635 epd = get_endpoint(intf->altsetting, 0); 3636 private->bInterfaceNumber = desc->bInterfaceNumber; 3637 private->bEndpointAddress = epd->bEndpointAddress & 3638 USB_ENDPOINT_NUMBER_MASK; 3639 private->wMaxPacketSize = le16_to_cpu(epd->wMaxPacketSize); 3640 private->bInterval = epd->bInterval; 3641 return 0; 3642 } 3643 3644 return -EINVAL; 3645} 3646 3647/* Initialise private data */ 3648static int scarlett2_init_private(struct usb_mixer_interface *mixer, 3649 const struct scarlett2_device_info *info) 3650{ 3651 struct scarlett2_data *private = 3652 kzalloc(sizeof(struct scarlett2_data), GFP_KERNEL); 3653 3654 if (!private) 3655 return -ENOMEM; 3656 3657 mutex_init(&private->usb_mutex); 3658 mutex_init(&private->data_mutex); 3659 INIT_DELAYED_WORK(&private->work, scarlett2_config_save_work); 3660 3661 mixer->private_data = private; 3662 mixer->private_free = scarlett2_private_free; 3663 mixer->private_suspend = scarlett2_private_suspend; 3664 3665 private->info = info; 3666 scarlett2_count_mux_io(private); 3667 private->scarlett2_seq = 0; 3668 private->mixer = mixer; 3669 3670 return scarlett2_find_fc_interface(mixer->chip->dev, private); 3671} 3672 3673/* Cargo cult proprietary initialisation sequence */ 3674static int scarlett2_usb_init(struct usb_mixer_interface *mixer) 3675{ 3676 struct usb_device *dev = mixer->chip->dev; 3677 struct scarlett2_data *private = mixer->private_data; 3678 u8 buf[24]; 3679 int err; 3680 3681 if (usb_pipe_type_check(dev, usb_sndctrlpipe(dev, 0))) 3682 return -EINVAL; 3683 3684 /* step 0 */ 3685 err = scarlett2_usb_rx(dev, private->bInterfaceNumber, 3686 SCARLETT2_USB_CMD_INIT, buf, sizeof(buf)); 3687 if (err < 0) 3688 return err; 3689 3690 /* step 1 */ 3691 private->scarlett2_seq = 1; 3692 err = scarlett2_usb(mixer, SCARLETT2_USB_INIT_1, NULL, 0, NULL, 0); 3693 if (err < 0) 3694 return err; 3695 3696 /* step 2 */ 3697 private->scarlett2_seq = 1; 3698 return scarlett2_usb(mixer, SCARLETT2_USB_INIT_2, NULL, 0, NULL, 84); 3699} 3700 3701/* Read configuration from the interface on start */ 3702static int scarlett2_read_configs(struct usb_mixer_interface *mixer) 3703{ 3704 struct scarlett2_data *private = mixer->private_data; 3705 const struct scarlett2_device_info *info = private->info; 3706 const int (*port_count)[SCARLETT2_PORT_DIRNS] = info->port_count; 3707 int num_line_out = 3708 port_count[SCARLETT2_PORT_TYPE_ANALOGUE][SCARLETT2_PORT_OUT]; 3709 int num_mixer_out = 3710 port_count[SCARLETT2_PORT_TYPE_MIX][SCARLETT2_PORT_IN]; 3711 struct scarlett2_usb_volume_status volume_status; 3712 int err, i; 3713 3714 if (info->has_msd_mode) { 3715 err = scarlett2_usb_get_config( 3716 mixer, SCARLETT2_CONFIG_MSD_SWITCH, 3717 1, &private->msd_switch); 3718 if (err < 0) 3719 return err; 3720 3721 /* no other controls are created if MSD mode is on */ 3722 if (private->msd_switch) 3723 return 0; 3724 } 3725 3726 err = scarlett2_update_input_other(mixer); 3727 if (err < 0) 3728 return err; 3729 3730 err = scarlett2_update_monitor_other(mixer); 3731 if (err < 0) 3732 return err; 3733 3734 /* the rest of the configuration is for devices with a mixer */ 3735 if (info->config_set == SCARLETT2_CONFIG_SET_NO_MIXER) 3736 return 0; 3737 3738 err = scarlett2_usb_get_config( 3739 mixer, SCARLETT2_CONFIG_STANDALONE_SWITCH, 3740 1, &private->standalone_switch); 3741 if (err < 0) 3742 return err; 3743 3744 err = scarlett2_update_sync(mixer); 3745 if (err < 0) 3746 return err; 3747 3748 err = scarlett2_usb_get_volume_status(mixer, &volume_status); 3749 if (err < 0) 3750 return err; 3751 3752 if (info->line_out_hw_vol) 3753 for (i = 0; i < SCARLETT2_DIM_MUTE_COUNT; i++) 3754 private->dim_mute[i] = !!volume_status.dim_mute[i]; 3755 3756 private->master_vol = clamp( 3757 volume_status.master_vol + SCARLETT2_VOLUME_BIAS, 3758 0, SCARLETT2_VOLUME_BIAS); 3759 3760 for (i = 0; i < num_line_out; i++) { 3761 int volume, mute; 3762 3763 private->vol_sw_hw_switch[i] = 3764 info->line_out_hw_vol 3765 && volume_status.sw_hw_switch[i]; 3766 3767 volume = private->vol_sw_hw_switch[i] 3768 ? volume_status.master_vol 3769 : volume_status.sw_vol[i]; 3770 volume = clamp(volume + SCARLETT2_VOLUME_BIAS, 3771 0, SCARLETT2_VOLUME_BIAS); 3772 private->vol[i] = volume; 3773 3774 mute = private->vol_sw_hw_switch[i] 3775 ? private->dim_mute[SCARLETT2_BUTTON_MUTE] 3776 : volume_status.mute_switch[i]; 3777 private->mute_switch[i] = mute; 3778 } 3779 3780 for (i = 0; i < num_mixer_out; i++) { 3781 err = scarlett2_usb_get_mix(mixer, i); 3782 if (err < 0) 3783 return err; 3784 } 3785 3786 return scarlett2_usb_get_mux(mixer); 3787} 3788 3789/* Notify on sync change */ 3790static void scarlett2_notify_sync( 3791 struct usb_mixer_interface *mixer) 3792{ 3793 struct scarlett2_data *private = mixer->private_data; 3794 3795 private->sync_updated = 1; 3796 3797 snd_ctl_notify(mixer->chip->card, SNDRV_CTL_EVENT_MASK_VALUE, 3798 &private->sync_ctl->id); 3799} 3800 3801/* Notify on monitor change */ 3802static void scarlett2_notify_monitor( 3803 struct usb_mixer_interface *mixer) 3804{ 3805 struct snd_card *card = mixer->chip->card; 3806 struct scarlett2_data *private = mixer->private_data; 3807 const struct scarlett2_device_info *info = private->info; 3808 const int (*port_count)[SCARLETT2_PORT_DIRNS] = info->port_count; 3809 int num_line_out = 3810 port_count[SCARLETT2_PORT_TYPE_ANALOGUE][SCARLETT2_PORT_OUT]; 3811 int i; 3812 3813 /* if line_out_hw_vol is 0, there are no controls to update */ 3814 if (!info->line_out_hw_vol) 3815 return; 3816 3817 private->vol_updated = 1; 3818 3819 snd_ctl_notify(mixer->chip->card, SNDRV_CTL_EVENT_MASK_VALUE, 3820 &private->master_vol_ctl->id); 3821 3822 for (i = 0; i < num_line_out; i++) 3823 if (private->vol_sw_hw_switch[line_out_remap(private, i)]) 3824 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE, 3825 &private->vol_ctls[i]->id); 3826} 3827 3828/* Notify on dim/mute change */ 3829static void scarlett2_notify_dim_mute( 3830 struct usb_mixer_interface *mixer) 3831{ 3832 struct snd_card *card = mixer->chip->card; 3833 struct scarlett2_data *private = mixer->private_data; 3834 const struct scarlett2_device_info *info = private->info; 3835 const int (*port_count)[SCARLETT2_PORT_DIRNS] = info->port_count; 3836 int num_line_out = 3837 port_count[SCARLETT2_PORT_TYPE_ANALOGUE][SCARLETT2_PORT_OUT]; 3838 int i; 3839 3840 private->vol_updated = 1; 3841 3842 if (!info->line_out_hw_vol) 3843 return; 3844 3845 for (i = 0; i < SCARLETT2_DIM_MUTE_COUNT; i++) 3846 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE, 3847 &private->dim_mute_ctls[i]->id); 3848 3849 for (i = 0; i < num_line_out; i++) 3850 if (private->vol_sw_hw_switch[line_out_remap(private, i)]) 3851 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE, 3852 &private->mute_ctls[i]->id); 3853} 3854 3855/* Notify on "input other" change (level/pad/air) */ 3856static void scarlett2_notify_input_other( 3857 struct usb_mixer_interface *mixer) 3858{ 3859 struct snd_card *card = mixer->chip->card; 3860 struct scarlett2_data *private = mixer->private_data; 3861 const struct scarlett2_device_info *info = private->info; 3862 int i; 3863 3864 private->input_other_updated = 1; 3865 3866 for (i = 0; i < info->level_input_count; i++) 3867 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE, 3868 &private->level_ctls[i]->id); 3869 for (i = 0; i < info->pad_input_count; i++) 3870 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE, 3871 &private->pad_ctls[i]->id); 3872 for (i = 0; i < info->air_input_count; i++) 3873 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE, 3874 &private->air_ctls[i]->id); 3875 for (i = 0; i < info->phantom_count; i++) 3876 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE, 3877 &private->phantom_ctls[i]->id); 3878} 3879 3880/* Notify on "monitor other" change (direct monitor, speaker 3881 * switching, talkback) 3882 */ 3883static void scarlett2_notify_monitor_other( 3884 struct usb_mixer_interface *mixer) 3885{ 3886 struct snd_card *card = mixer->chip->card; 3887 struct scarlett2_data *private = mixer->private_data; 3888 const struct scarlett2_device_info *info = private->info; 3889 3890 private->monitor_other_updated = 1; 3891 3892 if (info->direct_monitor) { 3893 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE, 3894 &private->direct_monitor_ctl->id); 3895 return; 3896 } 3897 3898 if (info->has_speaker_switching) 3899 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE, 3900 &private->speaker_switching_ctl->id); 3901 3902 if (info->has_talkback) 3903 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE, 3904 &private->talkback_ctl->id); 3905 3906 /* if speaker switching was recently enabled or disabled, 3907 * invalidate the dim/mute and mux enum controls 3908 */ 3909 if (private->speaker_switching_switched) { 3910 int i; 3911 3912 scarlett2_notify_dim_mute(mixer); 3913 3914 private->speaker_switching_switched = 0; 3915 private->mux_updated = 1; 3916 3917 for (i = 0; i < private->num_mux_dsts; i++) 3918 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE, 3919 &private->mux_ctls[i]->id); 3920 } 3921} 3922 3923/* Interrupt callback */ 3924static void scarlett2_notify(struct urb *urb) 3925{ 3926 struct usb_mixer_interface *mixer = urb->context; 3927 int len = urb->actual_length; 3928 int ustatus = urb->status; 3929 u32 data; 3930 3931 if (ustatus != 0 || len != 8) 3932 goto requeue; 3933 3934 data = le32_to_cpu(*(__le32 *)urb->transfer_buffer); 3935 if (data & SCARLETT2_USB_NOTIFY_SYNC) 3936 scarlett2_notify_sync(mixer); 3937 if (data & SCARLETT2_USB_NOTIFY_MONITOR) 3938 scarlett2_notify_monitor(mixer); 3939 if (data & SCARLETT2_USB_NOTIFY_DIM_MUTE) 3940 scarlett2_notify_dim_mute(mixer); 3941 if (data & SCARLETT2_USB_NOTIFY_INPUT_OTHER) 3942 scarlett2_notify_input_other(mixer); 3943 if (data & SCARLETT2_USB_NOTIFY_MONITOR_OTHER) 3944 scarlett2_notify_monitor_other(mixer); 3945 3946requeue: 3947 if (ustatus != -ENOENT && 3948 ustatus != -ECONNRESET && 3949 ustatus != -ESHUTDOWN) { 3950 urb->dev = mixer->chip->dev; 3951 usb_submit_urb(urb, GFP_ATOMIC); 3952 } 3953} 3954 3955static int scarlett2_init_notify(struct usb_mixer_interface *mixer) 3956{ 3957 struct usb_device *dev = mixer->chip->dev; 3958 struct scarlett2_data *private = mixer->private_data; 3959 unsigned int pipe = usb_rcvintpipe(dev, private->bEndpointAddress); 3960 void *transfer_buffer; 3961 3962 if (mixer->urb) { 3963 usb_audio_err(mixer->chip, 3964 "%s: mixer urb already in use!\n", __func__); 3965 return 0; 3966 } 3967 3968 if (usb_pipe_type_check(dev, pipe)) 3969 return -EINVAL; 3970 3971 mixer->urb = usb_alloc_urb(0, GFP_KERNEL); 3972 if (!mixer->urb) 3973 return -ENOMEM; 3974 3975 transfer_buffer = kmalloc(private->wMaxPacketSize, GFP_KERNEL); 3976 if (!transfer_buffer) 3977 return -ENOMEM; 3978 3979 usb_fill_int_urb(mixer->urb, dev, pipe, 3980 transfer_buffer, private->wMaxPacketSize, 3981 scarlett2_notify, mixer, private->bInterval); 3982 3983 return usb_submit_urb(mixer->urb, GFP_KERNEL); 3984} 3985 3986static int snd_scarlett_gen2_controls_create(struct usb_mixer_interface *mixer) 3987{ 3988 const struct scarlett2_device_info **info = scarlett2_devices; 3989 int err; 3990 3991 /* Find device in scarlett2_devices */ 3992 while (*info && (*info)->usb_id != mixer->chip->usb_id) 3993 info++; 3994 if (!*info) 3995 return -EINVAL; 3996 3997 /* Initialise private data */ 3998 err = scarlett2_init_private(mixer, *info); 3999 if (err < 0) 4000 return err; 4001 4002 /* Send proprietary USB initialisation sequence */ 4003 err = scarlett2_usb_init(mixer); 4004 if (err < 0) 4005 return err; 4006 4007 /* Read volume levels and controls from the interface */ 4008 err = scarlett2_read_configs(mixer); 4009 if (err < 0) 4010 return err; 4011 4012 /* Create the MSD control */ 4013 err = scarlett2_add_msd_ctl(mixer); 4014 if (err < 0) 4015 return err; 4016 4017 /* If MSD mode is enabled, don't create any other controls */ 4018 if (((struct scarlett2_data *)mixer->private_data)->msd_switch) 4019 return 0; 4020 4021 /* Create the analogue output controls */ 4022 err = scarlett2_add_line_out_ctls(mixer); 4023 if (err < 0) 4024 return err; 4025 4026 /* Create the analogue input controls */ 4027 err = scarlett2_add_line_in_ctls(mixer); 4028 if (err < 0) 4029 return err; 4030 4031 /* Create the input, output, and mixer mux input selections */ 4032 err = scarlett2_add_mux_enums(mixer); 4033 if (err < 0) 4034 return err; 4035 4036 /* Create the matrix mixer controls */ 4037 err = scarlett2_add_mixer_ctls(mixer); 4038 if (err < 0) 4039 return err; 4040 4041 /* Create the level meter controls */ 4042 err = scarlett2_add_meter_ctl(mixer); 4043 if (err < 0) 4044 return err; 4045 4046 /* Create the sync control */ 4047 err = scarlett2_add_sync_ctl(mixer); 4048 if (err < 0) 4049 return err; 4050 4051 /* Create the direct monitor control */ 4052 err = scarlett2_add_direct_monitor_ctl(mixer); 4053 if (err < 0) 4054 return err; 4055 4056 /* Create the speaker switching control */ 4057 err = scarlett2_add_speaker_switch_ctl(mixer); 4058 if (err < 0) 4059 return err; 4060 4061 /* Create the talkback controls */ 4062 err = scarlett2_add_talkback_ctls(mixer); 4063 if (err < 0) 4064 return err; 4065 4066 /* Create the standalone control */ 4067 err = scarlett2_add_standalone_ctl(mixer); 4068 if (err < 0) 4069 return err; 4070 4071 /* Set up the interrupt polling */ 4072 err = scarlett2_init_notify(mixer); 4073 if (err < 0) 4074 return err; 4075 4076 return 0; 4077} 4078 4079int snd_scarlett_gen2_init(struct usb_mixer_interface *mixer) 4080{ 4081 struct snd_usb_audio *chip = mixer->chip; 4082 int err; 4083 4084 /* only use UAC_VERSION_2 */ 4085 if (!mixer->protocol) 4086 return 0; 4087 4088 if (!(chip->setup & SCARLETT2_ENABLE)) { 4089 usb_audio_info(chip, 4090 "Focusrite Scarlett Gen 2/3 Mixer Driver disabled; " 4091 "use options snd_usb_audio vid=0x%04x pid=0x%04x " 4092 "device_setup=1 to enable and report any issues " 4093 "to g@b4.vu", 4094 USB_ID_VENDOR(chip->usb_id), 4095 USB_ID_PRODUCT(chip->usb_id)); 4096 return 0; 4097 } 4098 4099 usb_audio_info(chip, 4100 "Focusrite Scarlett Gen 2/3 Mixer Driver enabled pid=0x%04x", 4101 USB_ID_PRODUCT(chip->usb_id)); 4102 4103 err = snd_scarlett_gen2_controls_create(mixer); 4104 if (err < 0) 4105 usb_audio_err(mixer->chip, 4106 "Error initialising Scarlett Mixer Driver: %d", 4107 err); 4108 4109 return err; 4110}