1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Scarlett Driver for ALSA
5 * Copyright (c) 2013 by Tobias Hoffmann
6 * Copyright (c) 2013 by Robin Gareus <robin at gareus.org>
7 * Copyright (c) 2002 by Takashi Iwai <tiwai at suse.de>
8 * Copyright (c) 2014 by Chris J Arges <chris.j.arges at canonical.com>
10 * Many codes borrowed from audio.c by
11 * Alan Cox (alan at lxorguk.ukuu.org.uk)
12 * Thomas Sailer (sailer at ife.ee.ethz.ch)
15 * David Henningsson <david.henningsson at canonical.com>
19 * Rewritten and extended to support more models, e.g. Scarlett 18i8.
21 * Auto-detection via UAC2 is not feasible to properly discover the vast
22 * majority of features. It's related to both Linux/ALSA's UAC2 as well as
23 * Focusrite's implementation of it. Eventually quirks may be sufficient but
24 * right now it's a major headache to work arount these things.
26 * NB. Neither the OSX nor the win driver provided by Focusrite performs
27 * discovery, they seem to operate the same as this driver.
30 /* Mixer Interface for the Focusrite Scarlett 18i6 audio interface.
32 * The protocol was reverse engineered by looking at communication between
33 * Scarlett MixControl (v 1.2.128.0) and the Focusrite(R) Scarlett 18i6
34 * (firmware v305) using wireshark and usbmon in January 2013.
35 * Extended in July 2013.
37 * this mixer gives complete access to all features of the device:
38 * - change Impedance of inputs (Line-in, Mic / Instrument, Hi-Z)
39 * - select clock source
40 * - dynamic input to mixer-matrix assignment
41 * - 18 x 6 mixer-matrix gain stages
42 * - bus routing & volume control
43 * - automatic re-initialization on connect if device was power-cycled
45 * USB URB commands overview (bRequest = 0x01 = UAC2_CS_CUR)
47 * 0x01 Analog Input line/instrument impedance switch, wValue=0x0901 +
48 * channel, data=Line/Inst (2bytes)
49 * pad (-10dB) switch, wValue=0x0b01 + channel, data=Off/On (2bytes)
50 * ?? wValue=0x0803/04, ?? (2bytes)
51 * 0x0a Master Volume, wValue=0x0200+bus[0:all + only 1..4?] data(2bytes)
52 * Bus Mute/Unmute wValue=0x0100+bus[0:all + only 1..4?], data(2bytes)
53 * 0x28 Clock source, wValue=0x0100, data={1:int,2:spdif,3:adat} (1byte)
54 * 0x29 Set Sample-rate, wValue=0x0100, data=sample-rate(4bytes)
55 * 0x32 Mixer mux, wValue=0x0600 + mixer-channel, data=input-to-connect(2bytes)
56 * 0x33 Output mux, wValue=bus, data=input-to-connect(2bytes)
57 * 0x34 Capture mux, wValue=0...18, data=input-to-connect(2bytes)
58 * 0x3c Matrix Mixer gains, wValue=mixer-node data=gain(2bytes)
59 * ?? [sometimes](4bytes, e.g 0x000003be 0x000003bf ...03ff)
61 * USB reads: (i.e. actually issued by original software)
62 * 0x01 wValue=0x0901+channel (1byte!!), wValue=0x0b01+channed (1byte!!)
63 * 0x29 wValue=0x0100 sample-rate(4bytes)
64 * wValue=0x0200 ?? 1byte (only once)
65 * 0x2a wValue=0x0100 ?? 4bytes, sample-rate2 ??
67 * USB reads with bRequest = 0x03 = UAC2_CS_MEM
68 * 0x3c wValue=0x0002 1byte: sync status (locked=1)
69 * wValue=0x0000 18*2byte: peak meter (inputs)
70 * wValue=0x0001 8(?)*2byte: peak meter (mix)
71 * wValue=0x0003 6*2byte: peak meter (pcm/daw)
73 * USB write with bRequest = 0x03
74 * 0x3c Save settings to hardware: wValue=0x005a, data=0xa5
78 * /--------------\ 18chn 6chn /--------------\
79 * | Hardware in +--+-------\ /------+--+ ALSA PCM out |
80 * \--------------/ | | | | \--------------/
83 * | +---------------+ |
98 * | 18chn | 6chn | 6chn
100 * =========================
101 * +---------------+ +--—------------+
102 * \ Output Mux / \ Capture Mux /
103 * +-----+-----+ +-----+-----+
112 * | (3 stereo pairs) |
113 * /--------------\ | | /--------------\
114 * | Hardware out |<--/ \-->| ALSA PCM in |
115 * \--------------/ \--------------/
120 #include <linux/slab.h>
121 #include <linux/usb.h>
122 #include <linux/usb/audio-v2.h>
124 #include <sound/core.h>
125 #include <sound/control.h>
126 #include <sound/tlv.h>
128 #include "usbaudio.h"
133 #include "mixer_scarlett.h"
135 /* some gui mixers can't handle negative ctl values */
136 #define SND_SCARLETT_LEVEL_BIAS 128
137 #define SND_SCARLETT_MATRIX_IN_MAX 18
138 #define SND_SCARLETT_CONTROLS_MAX 10
139 #define SND_SCARLETT_OFFSETS_MAX 5
143 SCARLETT_SWITCH_IMPEDANCE
,
148 SCARLETT_OFFSET_PCM
= 0,
149 SCARLETT_OFFSET_ANALOG
= 1,
150 SCARLETT_OFFSET_SPDIF
= 2,
151 SCARLETT_OFFSET_ADAT
= 3,
152 SCARLETT_OFFSET_MIX
= 4,
155 struct scarlett_mixer_elem_enum_info
{
158 int offsets
[SND_SCARLETT_OFFSETS_MAX
];
159 char const * const *names
;
162 struct scarlett_mixer_control
{
168 struct scarlett_device_info
{
174 struct scarlett_mixer_elem_enum_info opt_master
;
175 struct scarlett_mixer_elem_enum_info opt_matrix
;
177 /* initial values for matrix mux */
178 int matrix_mux_init
[SND_SCARLETT_MATRIX_IN_MAX
];
180 int num_controls
; /* number of items in controls */
181 const struct scarlett_mixer_control controls
[SND_SCARLETT_CONTROLS_MAX
];
184 /********************** Enum Strings *************************/
186 static const struct scarlett_mixer_elem_enum_info opt_pad
= {
190 .names
= (char const * const []){
195 static const struct scarlett_mixer_elem_enum_info opt_impedance
= {
199 .names
= (char const * const []){
204 static const struct scarlett_mixer_elem_enum_info opt_clock
= {
208 .names
= (char const * const []){
209 "Internal", "SPDIF", "ADAT"
213 static const struct scarlett_mixer_elem_enum_info opt_sync
= {
217 .names
= (char const * const []){
222 static int scarlett_ctl_switch_info(struct snd_kcontrol
*kctl
,
223 struct snd_ctl_elem_info
*uinfo
)
225 struct usb_mixer_elem_info
*elem
= kctl
->private_data
;
227 uinfo
->type
= SNDRV_CTL_ELEM_TYPE_BOOLEAN
;
228 uinfo
->count
= elem
->channels
;
229 uinfo
->value
.integer
.min
= 0;
230 uinfo
->value
.integer
.max
= 1;
234 static int scarlett_ctl_switch_get(struct snd_kcontrol
*kctl
,
235 struct snd_ctl_elem_value
*ucontrol
)
237 struct usb_mixer_elem_info
*elem
= kctl
->private_data
;
240 for (i
= 0; i
< elem
->channels
; i
++) {
241 err
= snd_usb_get_cur_mix_value(elem
, i
, i
, &val
);
245 val
= !val
; /* invert mute logic for mixer */
246 ucontrol
->value
.integer
.value
[i
] = val
;
252 static int scarlett_ctl_switch_put(struct snd_kcontrol
*kctl
,
253 struct snd_ctl_elem_value
*ucontrol
)
255 struct usb_mixer_elem_info
*elem
= kctl
->private_data
;
259 for (i
= 0; i
< elem
->channels
; i
++) {
260 err
= snd_usb_get_cur_mix_value(elem
, i
, i
, &oval
);
264 val
= ucontrol
->value
.integer
.value
[i
];
267 err
= snd_usb_set_cur_mix_value(elem
, i
, i
, val
);
278 static int scarlett_ctl_resume(struct usb_mixer_elem_list
*list
)
280 struct usb_mixer_elem_info
*elem
= mixer_elem_list_to_info(list
);
283 for (i
= 0; i
< elem
->channels
; i
++)
284 if (elem
->cached
& (1 << i
))
285 snd_usb_set_cur_mix_value(elem
, i
, i
,
290 static int scarlett_ctl_info(struct snd_kcontrol
*kctl
,
291 struct snd_ctl_elem_info
*uinfo
)
293 struct usb_mixer_elem_info
*elem
= kctl
->private_data
;
295 uinfo
->type
= SNDRV_CTL_ELEM_TYPE_INTEGER
;
296 uinfo
->count
= elem
->channels
;
297 uinfo
->value
.integer
.min
= 0;
298 uinfo
->value
.integer
.max
= (int)kctl
->private_value
+
299 SND_SCARLETT_LEVEL_BIAS
;
300 uinfo
->value
.integer
.step
= 1;
304 static int scarlett_ctl_get(struct snd_kcontrol
*kctl
,
305 struct snd_ctl_elem_value
*ucontrol
)
307 struct usb_mixer_elem_info
*elem
= kctl
->private_data
;
310 for (i
= 0; i
< elem
->channels
; i
++) {
311 err
= snd_usb_get_cur_mix_value(elem
, i
, i
, &val
);
315 val
= clamp(val
/ 256, -128, (int)kctl
->private_value
) +
316 SND_SCARLETT_LEVEL_BIAS
;
317 ucontrol
->value
.integer
.value
[i
] = val
;
323 static int scarlett_ctl_put(struct snd_kcontrol
*kctl
,
324 struct snd_ctl_elem_value
*ucontrol
)
326 struct usb_mixer_elem_info
*elem
= kctl
->private_data
;
330 for (i
= 0; i
< elem
->channels
; i
++) {
331 err
= snd_usb_get_cur_mix_value(elem
, i
, i
, &oval
);
335 val
= ucontrol
->value
.integer
.value
[i
] -
336 SND_SCARLETT_LEVEL_BIAS
;
339 err
= snd_usb_set_cur_mix_value(elem
, i
, i
, val
);
350 static void scarlett_generate_name(int i
, char *dst
, int offsets
[])
352 if (i
> offsets
[SCARLETT_OFFSET_MIX
])
353 sprintf(dst
, "Mix %c",
354 'A'+(i
- offsets
[SCARLETT_OFFSET_MIX
] - 1));
355 else if (i
> offsets
[SCARLETT_OFFSET_ADAT
])
356 sprintf(dst
, "ADAT %d", i
- offsets
[SCARLETT_OFFSET_ADAT
]);
357 else if (i
> offsets
[SCARLETT_OFFSET_SPDIF
])
358 sprintf(dst
, "SPDIF %d", i
- offsets
[SCARLETT_OFFSET_SPDIF
]);
359 else if (i
> offsets
[SCARLETT_OFFSET_ANALOG
])
360 sprintf(dst
, "Analog %d", i
- offsets
[SCARLETT_OFFSET_ANALOG
]);
361 else if (i
> offsets
[SCARLETT_OFFSET_PCM
])
362 sprintf(dst
, "PCM %d", i
- offsets
[SCARLETT_OFFSET_PCM
]);
367 static int scarlett_ctl_enum_dynamic_info(struct snd_kcontrol
*kctl
,
368 struct snd_ctl_elem_info
*uinfo
)
370 struct usb_mixer_elem_info
*elem
= kctl
->private_data
;
371 struct scarlett_mixer_elem_enum_info
*opt
= elem
->private_data
;
372 unsigned int items
= opt
->len
;
374 uinfo
->type
= SNDRV_CTL_ELEM_TYPE_ENUMERATED
;
375 uinfo
->count
= elem
->channels
;
376 uinfo
->value
.enumerated
.items
= items
;
378 if (uinfo
->value
.enumerated
.item
>= items
)
379 uinfo
->value
.enumerated
.item
= items
- 1;
381 /* generate name dynamically based on item number and offset info */
382 scarlett_generate_name(uinfo
->value
.enumerated
.item
,
383 uinfo
->value
.enumerated
.name
,
389 static int scarlett_ctl_enum_info(struct snd_kcontrol
*kctl
,
390 struct snd_ctl_elem_info
*uinfo
)
392 struct usb_mixer_elem_info
*elem
= kctl
->private_data
;
393 struct scarlett_mixer_elem_enum_info
*opt
= elem
->private_data
;
395 return snd_ctl_enum_info(uinfo
, elem
->channels
, opt
->len
,
396 (const char * const *)opt
->names
);
399 static int scarlett_ctl_enum_get(struct snd_kcontrol
*kctl
,
400 struct snd_ctl_elem_value
*ucontrol
)
402 struct usb_mixer_elem_info
*elem
= kctl
->private_data
;
403 struct scarlett_mixer_elem_enum_info
*opt
= elem
->private_data
;
406 err
= snd_usb_get_cur_mix_value(elem
, 0, 0, &val
);
410 val
= clamp(val
- opt
->start
, 0, opt
->len
-1);
412 ucontrol
->value
.enumerated
.item
[0] = val
;
417 static int scarlett_ctl_enum_put(struct snd_kcontrol
*kctl
,
418 struct snd_ctl_elem_value
*ucontrol
)
420 struct usb_mixer_elem_info
*elem
= kctl
->private_data
;
421 struct scarlett_mixer_elem_enum_info
*opt
= elem
->private_data
;
424 err
= snd_usb_get_cur_mix_value(elem
, 0, 0, &oval
);
428 val
= ucontrol
->value
.integer
.value
[0];
429 val
= val
+ opt
->start
;
431 snd_usb_set_cur_mix_value(elem
, 0, 0, val
);
437 static int scarlett_ctl_enum_resume(struct usb_mixer_elem_list
*list
)
439 struct usb_mixer_elem_info
*elem
= mixer_elem_list_to_info(list
);
442 snd_usb_set_cur_mix_value(elem
, 0, 0, *elem
->cache_val
);
446 static int scarlett_ctl_meter_get(struct snd_kcontrol
*kctl
,
447 struct snd_ctl_elem_value
*ucontrol
)
449 struct usb_mixer_elem_info
*elem
= kctl
->private_data
;
450 struct snd_usb_audio
*chip
= elem
->head
.mixer
->chip
;
451 unsigned char buf
[2 * MAX_CHANNELS
] = {0, };
452 int wValue
= (elem
->control
<< 8) | elem
->idx_off
;
453 int idx
= snd_usb_ctrl_intf(chip
) | (elem
->head
.id
<< 8);
456 err
= snd_usb_ctl_msg(chip
->dev
,
457 usb_rcvctrlpipe(chip
->dev
, 0),
459 USB_RECIP_INTERFACE
| USB_TYPE_CLASS
|
460 USB_DIR_IN
, wValue
, idx
, buf
, elem
->channels
);
464 ucontrol
->value
.enumerated
.item
[0] = clamp((int)buf
[0], 0, 1);
468 static const struct snd_kcontrol_new usb_scarlett_ctl_switch
= {
469 .iface
= SNDRV_CTL_ELEM_IFACE_MIXER
,
471 .info
= scarlett_ctl_switch_info
,
472 .get
= scarlett_ctl_switch_get
,
473 .put
= scarlett_ctl_switch_put
,
476 static const DECLARE_TLV_DB_SCALE(db_scale_scarlett_gain
, -12800, 100, 0);
478 static const struct snd_kcontrol_new usb_scarlett_ctl
= {
479 .iface
= SNDRV_CTL_ELEM_IFACE_MIXER
,
480 .access
= SNDRV_CTL_ELEM_ACCESS_READWRITE
|
481 SNDRV_CTL_ELEM_ACCESS_TLV_READ
,
483 .info
= scarlett_ctl_info
,
484 .get
= scarlett_ctl_get
,
485 .put
= scarlett_ctl_put
,
486 .private_value
= 6, /* max value */
487 .tlv
= { .p
= db_scale_scarlett_gain
}
490 static const struct snd_kcontrol_new usb_scarlett_ctl_master
= {
491 .iface
= SNDRV_CTL_ELEM_IFACE_MIXER
,
492 .access
= SNDRV_CTL_ELEM_ACCESS_READWRITE
|
493 SNDRV_CTL_ELEM_ACCESS_TLV_READ
,
495 .info
= scarlett_ctl_info
,
496 .get
= scarlett_ctl_get
,
497 .put
= scarlett_ctl_put
,
498 .private_value
= 6, /* max value */
499 .tlv
= { .p
= db_scale_scarlett_gain
}
502 static const struct snd_kcontrol_new usb_scarlett_ctl_enum
= {
503 .iface
= SNDRV_CTL_ELEM_IFACE_MIXER
,
505 .info
= scarlett_ctl_enum_info
,
506 .get
= scarlett_ctl_enum_get
,
507 .put
= scarlett_ctl_enum_put
,
510 static const struct snd_kcontrol_new usb_scarlett_ctl_dynamic_enum
= {
511 .iface
= SNDRV_CTL_ELEM_IFACE_MIXER
,
513 .info
= scarlett_ctl_enum_dynamic_info
,
514 .get
= scarlett_ctl_enum_get
,
515 .put
= scarlett_ctl_enum_put
,
518 static const struct snd_kcontrol_new usb_scarlett_ctl_sync
= {
519 .iface
= SNDRV_CTL_ELEM_IFACE_MIXER
,
520 .access
= SNDRV_CTL_ELEM_ACCESS_READ
| SNDRV_CTL_ELEM_ACCESS_VOLATILE
,
522 .info
= scarlett_ctl_enum_info
,
523 .get
= scarlett_ctl_meter_get
,
526 static int add_new_ctl(struct usb_mixer_interface
*mixer
,
527 const struct snd_kcontrol_new
*ncontrol
,
528 usb_mixer_elem_resume_func_t resume
,
529 int index
, int offset
, int num
,
530 int val_type
, int channels
, const char *name
,
531 const struct scarlett_mixer_elem_enum_info
*opt
,
532 struct usb_mixer_elem_info
**elem_ret
535 struct snd_kcontrol
*kctl
;
536 struct usb_mixer_elem_info
*elem
;
539 elem
= kzalloc(sizeof(*elem
), GFP_KERNEL
);
543 elem
->head
.mixer
= mixer
;
544 elem
->head
.resume
= resume
;
545 elem
->control
= offset
;
547 elem
->head
.id
= index
;
548 elem
->val_type
= val_type
;
550 elem
->channels
= channels
;
552 /* add scarlett_mixer_elem_enum_info struct */
553 elem
->private_data
= (void *)opt
;
555 kctl
= snd_ctl_new1(ncontrol
, elem
);
560 kctl
->private_free
= snd_usb_mixer_elem_free
;
562 strlcpy(kctl
->id
.name
, name
, sizeof(kctl
->id
.name
));
564 err
= snd_usb_mixer_add_control(&elem
->head
, kctl
);
574 static int add_output_ctls(struct usb_mixer_interface
*mixer
,
575 int index
, const char *name
,
576 const struct scarlett_device_info
*info
)
579 char mx
[SNDRV_CTL_ELEM_ID_NAME_MAXLEN
];
580 struct usb_mixer_elem_info
*elem
;
582 /* Add mute switch */
583 snprintf(mx
, sizeof(mx
), "Master %d (%s) Playback Switch",
585 err
= add_new_ctl(mixer
, &usb_scarlett_ctl_switch
,
586 scarlett_ctl_resume
, 0x0a, 0x01,
587 2*index
+1, USB_MIXER_S16
, 2, mx
, NULL
, &elem
);
591 /* Add volume control and initialize to 0 */
592 snprintf(mx
, sizeof(mx
), "Master %d (%s) Playback Volume",
594 err
= add_new_ctl(mixer
, &usb_scarlett_ctl_master
,
595 scarlett_ctl_resume
, 0x0a, 0x02,
596 2*index
+1, USB_MIXER_S16
, 2, mx
, NULL
, &elem
);
600 /* Add L channel source playback enumeration */
601 snprintf(mx
, sizeof(mx
), "Master %dL (%s) Source Playback Enum",
603 err
= add_new_ctl(mixer
, &usb_scarlett_ctl_dynamic_enum
,
604 scarlett_ctl_enum_resume
, 0x33, 0x00,
605 2*index
, USB_MIXER_S16
, 1, mx
, &info
->opt_master
,
610 /* Add R channel source playback enumeration */
611 snprintf(mx
, sizeof(mx
), "Master %dR (%s) Source Playback Enum",
613 err
= add_new_ctl(mixer
, &usb_scarlett_ctl_dynamic_enum
,
614 scarlett_ctl_enum_resume
, 0x33, 0x00,
615 2*index
+1, USB_MIXER_S16
, 1, mx
, &info
->opt_master
,
623 /********************** device-specific config *************************/
626 static struct scarlett_device_info s6i6_info
= {
635 .offsets
= {0, 12, 16, 18, 18},
642 .offsets
= {0, 12, 16, 18, 18},
648 { .num
= 0, .type
= SCARLETT_OUTPUTS
, .name
= "Monitor" },
649 { .num
= 1, .type
= SCARLETT_OUTPUTS
, .name
= "Headphone" },
650 { .num
= 2, .type
= SCARLETT_OUTPUTS
, .name
= "SPDIF" },
651 { .num
= 1, .type
= SCARLETT_SWITCH_IMPEDANCE
, .name
= NULL
},
652 { .num
= 1, .type
= SCARLETT_SWITCH_PAD
, .name
= NULL
},
653 { .num
= 2, .type
= SCARLETT_SWITCH_IMPEDANCE
, .name
= NULL
},
654 { .num
= 2, .type
= SCARLETT_SWITCH_PAD
, .name
= NULL
},
655 { .num
= 3, .type
= SCARLETT_SWITCH_PAD
, .name
= NULL
},
656 { .num
= 4, .type
= SCARLETT_SWITCH_PAD
, .name
= NULL
},
660 12, 13, 14, 15, /* Analog -> 1..4 */
661 16, 17, /* SPDIF -> 5,6 */
662 0, 1, 2, 3, 4, 5, 6, 7, /* PCM[1..12] -> 7..18 */
668 static struct scarlett_device_info s8i6_info
= {
677 .offsets
= {0, 12, 16, 18, 18},
684 .offsets
= {0, 12, 16, 18, 18},
690 { .num
= 0, .type
= SCARLETT_OUTPUTS
, .name
= "Monitor" },
691 { .num
= 1, .type
= SCARLETT_OUTPUTS
, .name
= "Headphone" },
692 { .num
= 2, .type
= SCARLETT_OUTPUTS
, .name
= "SPDIF" },
693 { .num
= 1, .type
= SCARLETT_SWITCH_IMPEDANCE
, .name
= NULL
},
694 { .num
= 2, .type
= SCARLETT_SWITCH_IMPEDANCE
, .name
= NULL
},
695 { .num
= 3, .type
= SCARLETT_SWITCH_PAD
, .name
= NULL
},
696 { .num
= 4, .type
= SCARLETT_SWITCH_PAD
, .name
= NULL
},
700 12, 13, 14, 15, /* Analog -> 1..4 */
701 16, 17, /* SPDIF -> 5,6 */
702 0, 1, 2, 3, 4, 5, 6, 7, /* PCM[1..12] -> 7..18 */
707 static struct scarlett_device_info s18i6_info
= {
716 .offsets
= {0, 6, 14, 16, 24},
723 .offsets
= {0, 6, 14, 16, 24},
729 { .num
= 0, .type
= SCARLETT_OUTPUTS
, .name
= "Monitor" },
730 { .num
= 1, .type
= SCARLETT_OUTPUTS
, .name
= "Headphone" },
731 { .num
= 2, .type
= SCARLETT_OUTPUTS
, .name
= "SPDIF" },
732 { .num
= 1, .type
= SCARLETT_SWITCH_IMPEDANCE
, .name
= NULL
},
733 { .num
= 2, .type
= SCARLETT_SWITCH_IMPEDANCE
, .name
= NULL
},
737 6, 7, 8, 9, 10, 11, 12, 13, /* Analog -> 1..8 */
738 16, 17, 18, 19, 20, 21, /* ADAT[1..6] -> 9..14 */
739 14, 15, /* SPDIF -> 15,16 */
740 0, 1 /* PCM[1,2] -> 17,18 */
744 static struct scarlett_device_info s18i8_info
= {
753 .offsets
= {0, 8, 16, 18, 26},
760 .offsets
= {0, 8, 16, 18, 26},
766 { .num
= 0, .type
= SCARLETT_OUTPUTS
, .name
= "Monitor" },
767 { .num
= 1, .type
= SCARLETT_OUTPUTS
, .name
= "Headphone 1" },
768 { .num
= 2, .type
= SCARLETT_OUTPUTS
, .name
= "Headphone 2" },
769 { .num
= 3, .type
= SCARLETT_OUTPUTS
, .name
= "SPDIF" },
770 { .num
= 1, .type
= SCARLETT_SWITCH_IMPEDANCE
, .name
= NULL
},
771 { .num
= 1, .type
= SCARLETT_SWITCH_PAD
, .name
= NULL
},
772 { .num
= 2, .type
= SCARLETT_SWITCH_IMPEDANCE
, .name
= NULL
},
773 { .num
= 2, .type
= SCARLETT_SWITCH_PAD
, .name
= NULL
},
774 { .num
= 3, .type
= SCARLETT_SWITCH_PAD
, .name
= NULL
},
775 { .num
= 4, .type
= SCARLETT_SWITCH_PAD
, .name
= NULL
},
779 8, 9, 10, 11, 12, 13, 14, 15, /* Analog -> 1..8 */
780 18, 19, 20, 21, 22, 23, /* ADAT[1..6] -> 9..14 */
781 16, 17, /* SPDIF -> 15,16 */
782 0, 1 /* PCM[1,2] -> 17,18 */
786 static struct scarlett_device_info s18i20_info
= {
795 .offsets
= {0, 20, 28, 30, 38},
802 .offsets
= {0, 20, 28, 30, 38},
808 { .num
= 0, .type
= SCARLETT_OUTPUTS
, .name
= "Monitor" },
809 { .num
= 1, .type
= SCARLETT_OUTPUTS
, .name
= "Line 3/4" },
810 { .num
= 2, .type
= SCARLETT_OUTPUTS
, .name
= "Line 5/6" },
811 { .num
= 3, .type
= SCARLETT_OUTPUTS
, .name
= "Line 7/8" },
812 { .num
= 4, .type
= SCARLETT_OUTPUTS
, .name
= "Line 9/10" },
813 { .num
= 5, .type
= SCARLETT_OUTPUTS
, .name
= "SPDIF" },
814 { .num
= 6, .type
= SCARLETT_OUTPUTS
, .name
= "ADAT 1/2" },
815 { .num
= 7, .type
= SCARLETT_OUTPUTS
, .name
= "ADAT 3/4" },
816 { .num
= 8, .type
= SCARLETT_OUTPUTS
, .name
= "ADAT 5/6" },
817 { .num
= 9, .type
= SCARLETT_OUTPUTS
, .name
= "ADAT 7/8" },
818 /*{ .num = 1, .type = SCARLETT_SWITCH_IMPEDANCE, .name = NULL},
819 { .num = 1, .type = SCARLETT_SWITCH_PAD, .name = NULL},
820 { .num = 2, .type = SCARLETT_SWITCH_IMPEDANCE, .name = NULL},
821 { .num = 2, .type = SCARLETT_SWITCH_PAD, .name = NULL},
822 { .num = 3, .type = SCARLETT_SWITCH_PAD, .name = NULL},
823 { .num = 4, .type = SCARLETT_SWITCH_PAD, .name = NULL},*/
827 20, 21, 22, 23, 24, 25, 26, 27, /* Analog -> 1..8 */
828 30, 31, 32, 33, 34, 35, /* ADAT[1..6] -> 9..14 */
829 28, 29, /* SPDIF -> 15,16 */
830 0, 1 /* PCM[1,2] -> 17,18 */
835 static int scarlett_controls_create_generic(struct usb_mixer_interface
*mixer
,
836 struct scarlett_device_info
*info
)
839 char mx
[SNDRV_CTL_ELEM_ID_NAME_MAXLEN
];
840 const struct scarlett_mixer_control
*ctl
;
841 struct usb_mixer_elem_info
*elem
;
843 /* create master switch and playback volume */
844 err
= add_new_ctl(mixer
, &usb_scarlett_ctl_switch
,
845 scarlett_ctl_resume
, 0x0a, 0x01, 0,
846 USB_MIXER_S16
, 1, "Master Playback Switch", NULL
,
851 err
= add_new_ctl(mixer
, &usb_scarlett_ctl_master
,
852 scarlett_ctl_resume
, 0x0a, 0x02, 0,
853 USB_MIXER_S16
, 1, "Master Playback Volume", NULL
,
858 /* iterate through controls in info struct and create each one */
859 for (i
= 0; i
< info
->num_controls
; i
++) {
860 ctl
= &info
->controls
[i
];
863 case SCARLETT_OUTPUTS
:
864 err
= add_output_ctls(mixer
, ctl
->num
, ctl
->name
, info
);
868 case SCARLETT_SWITCH_IMPEDANCE
:
869 sprintf(mx
, "Input %d Impedance Switch", ctl
->num
);
870 err
= add_new_ctl(mixer
, &usb_scarlett_ctl_enum
,
871 scarlett_ctl_enum_resume
, 0x01,
872 0x09, ctl
->num
, USB_MIXER_S16
, 1, mx
,
873 &opt_impedance
, &elem
);
877 case SCARLETT_SWITCH_PAD
:
878 sprintf(mx
, "Input %d Pad Switch", ctl
->num
);
879 err
= add_new_ctl(mixer
, &usb_scarlett_ctl_enum
,
880 scarlett_ctl_enum_resume
, 0x01,
881 0x0b, ctl
->num
, USB_MIXER_S16
, 1, mx
,
893 * Create and initialize a mixer for the Focusrite(R) Scarlett
895 int snd_scarlett_controls_create(struct usb_mixer_interface
*mixer
)
898 char mx
[SNDRV_CTL_ELEM_ID_NAME_MAXLEN
];
899 struct scarlett_device_info
*info
;
900 struct usb_mixer_elem_info
*elem
;
901 static char sample_rate_buffer
[4] = { '\x80', '\xbb', '\x00', '\x00' };
903 /* only use UAC_VERSION_2 */
904 if (!mixer
->protocol
)
907 switch (mixer
->chip
->usb_id
) {
908 case USB_ID(0x1235, 0x8012):
911 case USB_ID(0x1235, 0x8002):
914 case USB_ID(0x1235, 0x8004):
917 case USB_ID(0x1235, 0x8014):
920 case USB_ID(0x1235, 0x800c):
923 default: /* device not (yet) supported */
927 /* generic function to create controls */
928 err
= scarlett_controls_create_generic(mixer
, info
);
932 /* setup matrix controls */
933 for (i
= 0; i
< info
->matrix_in
; i
++) {
934 snprintf(mx
, sizeof(mx
), "Matrix %02d Input Playback Route",
936 err
= add_new_ctl(mixer
, &usb_scarlett_ctl_dynamic_enum
,
937 scarlett_ctl_enum_resume
, 0x32,
938 0x06, i
, USB_MIXER_S16
, 1, mx
,
939 &info
->opt_matrix
, &elem
);
943 for (o
= 0; o
< info
->matrix_out
; o
++) {
944 sprintf(mx
, "Matrix %02d Mix %c Playback Volume", i
+1,
946 err
= add_new_ctl(mixer
, &usb_scarlett_ctl
,
947 scarlett_ctl_resume
, 0x3c, 0x00,
948 (i
<< 3) + (o
& 0x07), USB_MIXER_S16
,
956 for (i
= 0; i
< info
->input_len
; i
++) {
957 snprintf(mx
, sizeof(mx
), "Input Source %02d Capture Route",
959 err
= add_new_ctl(mixer
, &usb_scarlett_ctl_dynamic_enum
,
960 scarlett_ctl_enum_resume
, 0x34,
961 0x00, i
, USB_MIXER_S16
, 1, mx
,
962 &info
->opt_master
, &elem
);
967 /* val_len == 1 needed here */
968 err
= add_new_ctl(mixer
, &usb_scarlett_ctl_enum
,
969 scarlett_ctl_enum_resume
, 0x28, 0x01, 0,
970 USB_MIXER_U8
, 1, "Sample Clock Source",
975 /* val_len == 1 and UAC2_CS_MEM */
976 err
= add_new_ctl(mixer
, &usb_scarlett_ctl_sync
, NULL
, 0x3c, 0x00, 2,
977 USB_MIXER_U8
, 1, "Sample Clock Sync Status",
982 /* initialize sampling rate to 48000 */
983 err
= snd_usb_ctl_msg(mixer
->chip
->dev
,
984 usb_sndctrlpipe(mixer
->chip
->dev
, 0), UAC2_CS_CUR
,
985 USB_RECIP_INTERFACE
| USB_TYPE_CLASS
|
986 USB_DIR_OUT
, 0x0100, snd_usb_ctrl_intf(mixer
->chip
) |
987 (0x29 << 8), sample_rate_buffer
, 4);