xref: /linux/sound/usb/mixer_scarlett.c (revision 9611c0ce215a66770ccbe5c126bf57ba8c31bcad)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  *   Scarlett Driver for ALSA
4  *
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>
9  *
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)
13  *
14  *   Code cleanup:
15  *   David Henningsson <david.henningsson at canonical.com>
16  */
17 
18 /*
19  * Rewritten and extended to support more models, e.g. Scarlett 18i8.
20  *
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 around these things.
25  *
26  * NB. Neither the OSX nor the win driver provided by Focusrite performs
27  * discovery, they seem to operate the same as this driver.
28  */
29 
30 /* Mixer Interface for the Focusrite Scarlett 18i6 audio interface.
31  *
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.
36  *
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
44  *
45  * USB URB commands overview (bRequest = 0x01 = UAC2_CS_CUR)
46  * wIndex
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)
60  *
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 ??
66  *
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)
72  *
73  * USB write with bRequest = 0x03
74  * 0x3c Save settings to hardware: wValue=0x005a, data=0xa5
75  *
76  *
77  * <ditaa>
78  *  /--------------\    18chn            6chn    /--------------\
79  *  | Hardware  in +--+-------\        /------+--+ ALSA PCM out |
80  *  \--------------/  |       |        |      |  \--------------/
81  *                    |       |        |      |
82  *                    |       v        v      |
83  *                    |   +---------------+   |
84  *                    |    \ Matrix  Mux /    |
85  *                    |     +-----+-----+     |
86  *                    |           |           |
87  *                    |           | 18chn     |
88  *                    |           v           |
89  *                    |     +-----------+     |
90  *                    |     | Mixer     |     |
91  *                    |     |    Matrix |     |
92  *                    |     |           |     |
93  *                    |     | 18x6 Gain |     |
94  *                    |     |   stages  |     |
95  *                    |     +-----+-----+     |
96  *                    |           |           |
97  *                    |           |           |
98  *                    | 18chn     | 6chn      | 6chn
99  *                    v           v           v
100  *                    =========================
101  *             +---------------+     +--—------------+
102  *              \ Output  Mux /       \ Capture Mux /
103  *               +-----+-----+         +-----+-----+
104  *                     |                     |
105  *                     | 6chn                |
106  *                     v                     |
107  *              +-------------+              |
108  *              | Master Gain |              |
109  *              +------+------+              |
110  *                     |                     |
111  *                     | 6chn                | 18chn
112  *                     | (3 stereo pairs)    |
113  *  /--------------\   |                     |   /--------------\
114  *  | Hardware out |<--/                     \-->| ALSA PCM  in |
115  *  \--------------/                             \--------------/
116  * </ditaa>
117  *
118  */
119 
120 #include <linux/slab.h>
121 #include <linux/usb.h>
122 #include <linux/usb/audio-v2.h>
123 
124 #include <sound/core.h>
125 #include <sound/control.h>
126 #include <sound/tlv.h>
127 
128 #include "usbaudio.h"
129 #include "mixer.h"
130 #include "helper.h"
131 #include "power.h"
132 
133 #include "mixer_scarlett.h"
134 
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 14
139 #define SND_SCARLETT_OFFSETS_MAX 5
140 
141 enum {
142 	SCARLETT_OUTPUTS,
143 	SCARLETT_SWITCH_IMPEDANCE,
144 	SCARLETT_SWITCH_PAD,
145 	SCARLETT_SWITCH_GAIN,
146 	FORTE_INPUT_SOURCE,     /* mic/line/instrument selection */
147 	FORTE_INPUT_HPF,        /* high pass filter */
148 	FORTE_INPUT_PHANTOM,    /* 48V phantom power */
149 	FORTE_INPUT_PHASE,      /* phase invert */
150 	FORTE_INPUT_PAD,        /* pad */
151 	FORTE_INPUT_GAIN,       /* preamp gain 0-42 (~0-75dB for mic) */
152 };
153 
154 enum {
155 	SCARLETT_OFFSET_PCM = 0,
156 	SCARLETT_OFFSET_ANALOG = 1,
157 	SCARLETT_OFFSET_SPDIF = 2,
158 	SCARLETT_OFFSET_ADAT = 3,
159 	SCARLETT_OFFSET_MIX = 4,
160 };
161 
162 struct scarlett_mixer_elem_enum_info {
163 	int start;
164 	int len;
165 	int offsets[SND_SCARLETT_OFFSETS_MAX];
166 	char const * const *names;
167 };
168 
169 struct scarlett_mixer_control {
170 	unsigned char num;
171 	unsigned char type;
172 	const char *name;
173 };
174 
175 struct scarlett_device_info {
176 	int matrix_in;
177 	int matrix_out;
178 	int input_len;
179 	int output_len;
180 
181 	bool has_output_source_routing;
182 
183 	struct scarlett_mixer_elem_enum_info opt_master;
184 	struct scarlett_mixer_elem_enum_info opt_matrix;
185 
186 	/* initial values for matrix mux */
187 	int matrix_mux_init[SND_SCARLETT_MATRIX_IN_MAX];
188 
189 	int num_controls;	/* number of items in controls */
190 	const struct scarlett_mixer_control controls[SND_SCARLETT_CONTROLS_MAX];
191 };
192 
193 /********************** Enum Strings *************************/
194 
195 static const struct scarlett_mixer_elem_enum_info opt_pad = {
196 	.start = 0,
197 	.len = 2,
198 	.offsets = {},
199 	.names = (char const * const []){
200 		"0dB", "-10dB"
201 	}
202 };
203 
204 static const struct scarlett_mixer_elem_enum_info opt_gain = {
205 	.start = 0,
206 	.len = 2,
207 	.offsets = {},
208 	.names = (char const * const []){
209 		"Lo", "Hi"
210 	}
211 };
212 
213 static const struct scarlett_mixer_elem_enum_info opt_impedance = {
214 	.start = 0,
215 	.len = 2,
216 	.offsets = {},
217 	.names = (char const * const []){
218 		"Line", "Hi-Z"
219 	}
220 };
221 
222 static const struct scarlett_mixer_elem_enum_info opt_clock = {
223 	.start = 1,
224 	.len = 3,
225 	.offsets = {},
226 	.names = (char const * const []){
227 		"Internal", "SPDIF", "ADAT"
228 	}
229 };
230 
231 static const struct scarlett_mixer_elem_enum_info opt_sync = {
232 	.start = 0,
233 	.len = 2,
234 	.offsets = {},
235 	.names = (char const * const []){
236 		"No Lock", "Locked"
237 	}
238 };
239 
240 /* Forte-specific input control options */
241 static const struct scarlett_mixer_elem_enum_info opt_forte_source = {
242 	.start = 0,
243 	.len = 3,
244 	.offsets = {},
245 	.names = (char const * const []){
246 		"Mic", "Line", "Inst"
247 	}
248 };
249 
250 /*
251  * Forte-specific USB control functions
252  * Forte input controls use bRequest=0x03 (UAC2_CS_MEM) instead of 0x01
253  * wValue = (control_code << 8) | channel
254  * wIndex = interface | (0x3c << 8) like Scarlett meter/matrix controls
255  */
256 static int forte_set_ctl_value(struct usb_mixer_elem_info *elem, int value)
257 {
258 	struct snd_usb_audio *chip = elem->head.mixer->chip;
259 	unsigned char buf[2];
260 	int wValue = elem->control;  /* Just control code, NO shift, NO channel */
261 	int idx = snd_usb_ctrl_intf(elem->head.mixer->hostif) | (elem->head.id << 8);
262 	int err;
263 
264 	/* Wiki format: "2 bytes, chan 0,1 and value"
265 	 * Data order: [channel, value] - channel FIRST per wiki
266 	 */
267 	buf[0] = elem->idx_off;  /* Channel: 0 or 1 */
268 	buf[1] = value & 0xff;   /* Value: 0-2 for source, 0-1 for switches */
269 
270 	err = snd_usb_lock_shutdown(chip);
271 	if (err < 0)
272 		return -EIO;
273 
274 	err = snd_usb_ctl_msg(chip->dev,
275 			      usb_sndctrlpipe(chip->dev, 0),
276 			      UAC2_CS_MEM,  /* bRequest = 0x03 */
277 			      USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_OUT,
278 			      wValue, idx, buf, 2);
279 
280 	snd_usb_unlock_shutdown(chip);
281 
282 	if (err < 0) {
283 		usb_audio_err(chip, "forte_set FAILED: req=0x03 wVal=0x%04x wIdx=0x%04x buf=[%02x,%02x] err=%d\n",
284 			      wValue, idx, buf[0], buf[1], err);
285 		return err;
286 	}
287 	return 0;
288 }
289 
290 static int forte_get_ctl_value(struct usb_mixer_elem_info *elem, int *value)
291 {
292 	/* Device may not support reading input controls.
293 	 * Return cached value or default to avoid blocking module load.
294 	 */
295 	if (elem->cached)
296 		*value = elem->cache_val[0];
297 	else
298 		*value = 0;  /* Default: first option */
299 
300 	return 0;
301 }
302 
303 /*
304  * Forte Input Gain control functions
305  * Gain range is 0-42 (0x00-0x2a) which maps to approximately:
306  * - Mic: 0 to +75dB (~1.8dB per step)
307  * - Instrument: +14 to +68dB
308  * - Line: -12 to +42dB
309  * We use a TLV scale of 0 to 7500 centidB (0 to 75dB) in ~179 cB steps
310  */
311 #define FORTE_INPUT_GAIN_MAX 42
312 
313 static int forte_input_gain_info(struct snd_kcontrol *kctl,
314 				 struct snd_ctl_elem_info *uinfo)
315 {
316 	uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
317 	uinfo->count = 1;
318 	uinfo->value.integer.min = 0;
319 	uinfo->value.integer.max = FORTE_INPUT_GAIN_MAX;
320 	uinfo->value.integer.step = 1;
321 	return 0;
322 }
323 
324 static int forte_input_gain_get(struct snd_kcontrol *kctl,
325 				struct snd_ctl_elem_value *ucontrol)
326 {
327 	struct usb_mixer_elem_info *elem = kctl->private_data;
328 	int err, val;
329 
330 	/* Use Forte-specific USB command with UAC2_CS_MEM */
331 	err = forte_get_ctl_value(elem, &val);
332 	if (err < 0)
333 		return err;
334 
335 	ucontrol->value.integer.value[0] = clamp(val, 0, FORTE_INPUT_GAIN_MAX);
336 	return 0;
337 }
338 
339 static int forte_input_gain_put(struct snd_kcontrol *kctl,
340 				struct snd_ctl_elem_value *ucontrol)
341 {
342 	struct usb_mixer_elem_info *elem = kctl->private_data;
343 	int err, oval, val;
344 
345 	/* Read current value */
346 	err = forte_get_ctl_value(elem, &oval);
347 	if (err < 0)
348 		return err;
349 
350 	val = clamp((int)ucontrol->value.integer.value[0], 0, FORTE_INPUT_GAIN_MAX);
351 	if (oval != val) {
352 		/* Use Forte-specific USB command */
353 		err = forte_set_ctl_value(elem, val);
354 		if (err < 0)
355 			return err;
356 		elem->cached |= 1;
357 		elem->cache_val[0] = val;
358 		return 1;
359 	}
360 	return 0;
361 }
362 
363 static int forte_input_gain_resume(struct usb_mixer_elem_list *list)
364 {
365 	struct usb_mixer_elem_info *elem = mixer_elem_list_to_info(list);
366 
367 	if (elem->cached)
368 		forte_set_ctl_value(elem, *elem->cache_val);
369 	return 0;
370 }
371 
372 /*
373  * Forte-specific enum control functions (for Source selection)
374  * Uses bRequest=0x03 (UAC2_CS_MEM) instead of standard 0x01
375  */
376 static int forte_ctl_enum_get(struct snd_kcontrol *kctl,
377 			      struct snd_ctl_elem_value *ucontrol)
378 {
379 	struct usb_mixer_elem_info *elem = kctl->private_data;
380 	struct scarlett_mixer_elem_enum_info *opt = elem->private_data;
381 	int err, val;
382 
383 	err = forte_get_ctl_value(elem, &val);
384 	if (err < 0)
385 		return err;
386 
387 	val = clamp(val - opt->start, 0, opt->len - 1);
388 	ucontrol->value.enumerated.item[0] = val;
389 	return 0;
390 }
391 
392 static int forte_ctl_enum_put(struct snd_kcontrol *kctl,
393 			      struct snd_ctl_elem_value *ucontrol)
394 {
395 	struct usb_mixer_elem_info *elem = kctl->private_data;
396 	struct scarlett_mixer_elem_enum_info *opt = elem->private_data;
397 	int err, oval, val;
398 
399 	err = forte_get_ctl_value(elem, &oval);
400 	if (err < 0)
401 		return err;
402 
403 	val = ucontrol->value.integer.value[0] + opt->start;
404 	if (val != oval) {
405 		err = forte_set_ctl_value(elem, val);
406 		if (err < 0)
407 			return err;
408 		elem->cached |= 1;
409 		elem->cache_val[0] = val;
410 		return 1;
411 	}
412 	return 0;
413 }
414 
415 static int forte_ctl_enum_resume(struct usb_mixer_elem_list *list)
416 {
417 	struct usb_mixer_elem_info *elem = mixer_elem_list_to_info(list);
418 
419 	if (elem->cached)
420 		forte_set_ctl_value(elem, *elem->cache_val);
421 	return 0;
422 }
423 
424 /*
425  * Forte-specific switch control functions (for HPF, 48V, Phase, Pad)
426  * Uses bRequest=0x03 (UAC2_CS_MEM) instead of standard 0x01
427  */
428 static int forte_ctl_switch_get(struct snd_kcontrol *kctl,
429 				struct snd_ctl_elem_value *ucontrol)
430 {
431 	struct usb_mixer_elem_info *elem = kctl->private_data;
432 	int err, val;
433 
434 	err = forte_get_ctl_value(elem, &val);
435 	if (err < 0)
436 		return err;
437 
438 	ucontrol->value.integer.value[0] = val ? 1 : 0;
439 	return 0;
440 }
441 
442 static int forte_ctl_switch_put(struct snd_kcontrol *kctl,
443 				struct snd_ctl_elem_value *ucontrol)
444 {
445 	struct usb_mixer_elem_info *elem = kctl->private_data;
446 	int err, oval, val;
447 
448 	err = forte_get_ctl_value(elem, &oval);
449 	if (err < 0)
450 		return err;
451 
452 	val = ucontrol->value.integer.value[0] ? 1 : 0;
453 	if (val != oval) {
454 		err = forte_set_ctl_value(elem, val);
455 		if (err < 0)
456 			return err;
457 		elem->cached |= 1;
458 		elem->cache_val[0] = val;
459 		return 1;
460 	}
461 	return 0;
462 }
463 
464 static int forte_ctl_switch_resume(struct usb_mixer_elem_list *list)
465 {
466 	struct usb_mixer_elem_info *elem = mixer_elem_list_to_info(list);
467 
468 	if (elem->cached)
469 		forte_set_ctl_value(elem, *elem->cache_val);
470 	return 0;
471 }
472 
473 static int scarlett_ctl_switch_info(struct snd_kcontrol *kctl,
474 		struct snd_ctl_elem_info *uinfo)
475 {
476 	struct usb_mixer_elem_info *elem = kctl->private_data;
477 
478 	uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
479 	uinfo->count = elem->channels;
480 	uinfo->value.integer.min = 0;
481 	uinfo->value.integer.max = 1;
482 	return 0;
483 }
484 
485 static int scarlett_ctl_switch_get(struct snd_kcontrol *kctl,
486 		struct snd_ctl_elem_value *ucontrol)
487 {
488 	struct usb_mixer_elem_info *elem = kctl->private_data;
489 	int i, err, val;
490 
491 	for (i = 0; i < elem->channels; i++) {
492 		err = snd_usb_get_cur_mix_value(elem, i, i, &val);
493 		if (err < 0)
494 			return err;
495 
496 		val = !val; /* invert mute logic for mixer */
497 		ucontrol->value.integer.value[i] = val;
498 	}
499 
500 	return 0;
501 }
502 
503 static int scarlett_ctl_switch_put(struct snd_kcontrol *kctl,
504 		struct snd_ctl_elem_value *ucontrol)
505 {
506 	struct usb_mixer_elem_info *elem = kctl->private_data;
507 	int i, changed = 0;
508 	int err, oval, val;
509 
510 	for (i = 0; i < elem->channels; i++) {
511 		err = snd_usb_get_cur_mix_value(elem, i, i, &oval);
512 		if (err < 0)
513 			return err;
514 
515 		val = ucontrol->value.integer.value[i];
516 		val = !val;
517 		if (oval != val) {
518 			err = snd_usb_set_cur_mix_value(elem, i, i, val);
519 			if (err < 0)
520 				return err;
521 
522 			changed = 1;
523 		}
524 	}
525 
526 	return changed;
527 }
528 
529 static int scarlett_ctl_resume(struct usb_mixer_elem_list *list)
530 {
531 	struct usb_mixer_elem_info *elem = mixer_elem_list_to_info(list);
532 	int i;
533 
534 	for (i = 0; i < elem->channels; i++)
535 		if (elem->cached & (1 << i))
536 			snd_usb_set_cur_mix_value(elem, i, i,
537 						  elem->cache_val[i]);
538 	return 0;
539 }
540 
541 static int scarlett_ctl_info(struct snd_kcontrol *kctl,
542 			     struct snd_ctl_elem_info *uinfo)
543 {
544 	struct usb_mixer_elem_info *elem = kctl->private_data;
545 
546 	uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
547 	uinfo->count = elem->channels;
548 	uinfo->value.integer.min = 0;
549 	uinfo->value.integer.max = (int)kctl->private_value +
550 		SND_SCARLETT_LEVEL_BIAS;
551 	uinfo->value.integer.step = 1;
552 	return 0;
553 }
554 
555 static int scarlett_ctl_get(struct snd_kcontrol *kctl,
556 			    struct snd_ctl_elem_value *ucontrol)
557 {
558 	struct usb_mixer_elem_info *elem = kctl->private_data;
559 	int i, err, val;
560 
561 	for (i = 0; i < elem->channels; i++) {
562 		err = snd_usb_get_cur_mix_value(elem, i, i, &val);
563 		if (err < 0)
564 			return err;
565 
566 		val = clamp(val / 256, -128, (int)kctl->private_value) +
567 				    SND_SCARLETT_LEVEL_BIAS;
568 		ucontrol->value.integer.value[i] = val;
569 	}
570 
571 	return 0;
572 }
573 
574 static int scarlett_ctl_put(struct snd_kcontrol *kctl,
575 			    struct snd_ctl_elem_value *ucontrol)
576 {
577 	struct usb_mixer_elem_info *elem = kctl->private_data;
578 	int i, changed = 0;
579 	int err, oval, val;
580 
581 	for (i = 0; i < elem->channels; i++) {
582 		err = snd_usb_get_cur_mix_value(elem, i, i, &oval);
583 		if (err < 0)
584 			return err;
585 
586 		val = ucontrol->value.integer.value[i] -
587 			SND_SCARLETT_LEVEL_BIAS;
588 		val = val * 256;
589 		if (oval != val) {
590 			err = snd_usb_set_cur_mix_value(elem, i, i, val);
591 			if (err < 0)
592 				return err;
593 
594 			changed = 1;
595 		}
596 	}
597 
598 	return changed;
599 }
600 
601 static void scarlett_generate_name(int i, char *dst, size_t size, int offsets[])
602 {
603 	if (i > offsets[SCARLETT_OFFSET_MIX])
604 		scnprintf(dst, size, "Mix %c",
605 			  'A'+(i - offsets[SCARLETT_OFFSET_MIX] - 1));
606 	else if (i > offsets[SCARLETT_OFFSET_ADAT])
607 		scnprintf(dst, size, "ADAT %d", i - offsets[SCARLETT_OFFSET_ADAT]);
608 	else if (i > offsets[SCARLETT_OFFSET_SPDIF])
609 		scnprintf(dst, size, "SPDIF %d", i - offsets[SCARLETT_OFFSET_SPDIF]);
610 	else if (i > offsets[SCARLETT_OFFSET_ANALOG])
611 		scnprintf(dst, size, "Analog %d", i - offsets[SCARLETT_OFFSET_ANALOG]);
612 	else if (i > offsets[SCARLETT_OFFSET_PCM])
613 		scnprintf(dst, size, "PCM %d", i - offsets[SCARLETT_OFFSET_PCM]);
614 	else
615 		scnprintf(dst, size, "Off");
616 }
617 
618 static int scarlett_ctl_enum_dynamic_info(struct snd_kcontrol *kctl,
619 					  struct snd_ctl_elem_info *uinfo)
620 {
621 	struct usb_mixer_elem_info *elem = kctl->private_data;
622 	struct scarlett_mixer_elem_enum_info *opt = elem->private_data;
623 	unsigned int items = opt->len;
624 
625 	uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
626 	uinfo->count = elem->channels;
627 	uinfo->value.enumerated.items = items;
628 
629 	if (uinfo->value.enumerated.item >= items)
630 		uinfo->value.enumerated.item = items - 1;
631 
632 	/* generate name dynamically based on item number and offset info */
633 	scarlett_generate_name(uinfo->value.enumerated.item,
634 			       uinfo->value.enumerated.name,
635 			       sizeof(uinfo->value.enumerated.name),
636 			       opt->offsets);
637 
638 	return 0;
639 }
640 
641 static int scarlett_ctl_enum_info(struct snd_kcontrol *kctl,
642 				  struct snd_ctl_elem_info *uinfo)
643 {
644 	struct usb_mixer_elem_info *elem = kctl->private_data;
645 	struct scarlett_mixer_elem_enum_info *opt = elem->private_data;
646 
647 	return snd_ctl_enum_info(uinfo, elem->channels, opt->len,
648 				 (const char * const *)opt->names);
649 }
650 
651 static int scarlett_ctl_enum_get(struct snd_kcontrol *kctl,
652 				 struct snd_ctl_elem_value *ucontrol)
653 {
654 	struct usb_mixer_elem_info *elem = kctl->private_data;
655 	struct scarlett_mixer_elem_enum_info *opt = elem->private_data;
656 	int err, val;
657 
658 	err = snd_usb_get_cur_mix_value(elem, 0, 0, &val);
659 	if (err < 0)
660 		return err;
661 
662 	val = clamp(val - opt->start, 0, opt->len-1);
663 
664 	ucontrol->value.enumerated.item[0] = val;
665 
666 	return 0;
667 }
668 
669 static int scarlett_ctl_enum_put(struct snd_kcontrol *kctl,
670 				 struct snd_ctl_elem_value *ucontrol)
671 {
672 	struct usb_mixer_elem_info *elem = kctl->private_data;
673 	struct scarlett_mixer_elem_enum_info *opt = elem->private_data;
674 	int err, oval, val;
675 
676 	err = snd_usb_get_cur_mix_value(elem, 0, 0, &oval);
677 	if (err < 0)
678 		return err;
679 
680 	val = ucontrol->value.integer.value[0];
681 	val = val + opt->start;
682 	if (val != oval) {
683 		err = snd_usb_set_cur_mix_value(elem, 0, 0, val);
684 		if (err < 0)
685 			return err;
686 		return 1;
687 	}
688 	return 0;
689 }
690 
691 static int scarlett_ctl_enum_resume(struct usb_mixer_elem_list *list)
692 {
693 	struct usb_mixer_elem_info *elem = mixer_elem_list_to_info(list);
694 
695 	if (elem->cached)
696 		snd_usb_set_cur_mix_value(elem, 0, 0, *elem->cache_val);
697 	return 0;
698 }
699 
700 static int scarlett_ctl_meter_get(struct snd_kcontrol *kctl,
701 				  struct snd_ctl_elem_value *ucontrol)
702 {
703 	struct usb_mixer_elem_info *elem = kctl->private_data;
704 	struct snd_usb_audio *chip = elem->head.mixer->chip;
705 	unsigned char buf[2 * MAX_CHANNELS] = {0, };
706 	int wValue = (elem->control << 8) | elem->idx_off;
707 	int idx = snd_usb_ctrl_intf(elem->head.mixer->hostif) | (elem->head.id << 8);
708 	int err;
709 
710 	err = snd_usb_ctl_msg(chip->dev,
711 				usb_rcvctrlpipe(chip->dev, 0),
712 				UAC2_CS_MEM,
713 				USB_RECIP_INTERFACE | USB_TYPE_CLASS |
714 				USB_DIR_IN, wValue, idx, buf, elem->channels);
715 	if (err < 0)
716 		return err;
717 
718 	ucontrol->value.enumerated.item[0] = clamp((int)buf[0], 0, 1);
719 	return 0;
720 }
721 
722 static const struct snd_kcontrol_new usb_scarlett_ctl_switch = {
723 	.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
724 	.name = "",
725 	.info = scarlett_ctl_switch_info,
726 	.get =  scarlett_ctl_switch_get,
727 	.put =  scarlett_ctl_switch_put,
728 };
729 
730 static const DECLARE_TLV_DB_SCALE(db_scale_scarlett_gain, -12800, 100, 0);
731 
732 static const struct snd_kcontrol_new usb_scarlett_ctl = {
733 	.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
734 	.access = SNDRV_CTL_ELEM_ACCESS_READWRITE |
735 		  SNDRV_CTL_ELEM_ACCESS_TLV_READ,
736 	.name = "",
737 	.info = scarlett_ctl_info,
738 	.get =  scarlett_ctl_get,
739 	.put =  scarlett_ctl_put,
740 	.private_value = 6,  /* max value */
741 	.tlv = { .p = db_scale_scarlett_gain }
742 };
743 
744 static const struct snd_kcontrol_new usb_scarlett_ctl_master = {
745 	.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
746 	.access = SNDRV_CTL_ELEM_ACCESS_READWRITE |
747 		  SNDRV_CTL_ELEM_ACCESS_TLV_READ,
748 	.name = "",
749 	.info = scarlett_ctl_info,
750 	.get =  scarlett_ctl_get,
751 	.put =  scarlett_ctl_put,
752 	.private_value = 6,  /* max value */
753 	.tlv = { .p = db_scale_scarlett_gain }
754 };
755 
756 static const struct snd_kcontrol_new usb_scarlett_ctl_enum = {
757 	.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
758 	.name = "",
759 	.info = scarlett_ctl_enum_info,
760 	.get =  scarlett_ctl_enum_get,
761 	.put =  scarlett_ctl_enum_put,
762 };
763 
764 static const struct snd_kcontrol_new usb_scarlett_ctl_dynamic_enum = {
765 	.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
766 	.name = "",
767 	.info = scarlett_ctl_enum_dynamic_info,
768 	.get =  scarlett_ctl_enum_get,
769 	.put =  scarlett_ctl_enum_put,
770 };
771 
772 static const struct snd_kcontrol_new usb_scarlett_ctl_sync = {
773 	.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
774 	.access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE,
775 	.name = "",
776 	.info = scarlett_ctl_enum_info,
777 	.get =  scarlett_ctl_meter_get,
778 };
779 
780 /* Forte-specific control structures - use bRequest=0x03 instead of 0x01 */
781 static const struct snd_kcontrol_new usb_forte_ctl_enum = {
782 	.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
783 	.name = "",
784 	.info = scarlett_ctl_enum_info,
785 	.get =  forte_ctl_enum_get,
786 	.put =  forte_ctl_enum_put,
787 };
788 
789 static const struct snd_kcontrol_new usb_forte_ctl_switch = {
790 	.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
791 	.name = "",
792 	.info = snd_ctl_boolean_mono_info,
793 	.get =  forte_ctl_switch_get,
794 	.put =  forte_ctl_switch_put,
795 };
796 
797 /* Forte input gain: 0-42 maps to approximately 0-75dB (~179 cB per step) */
798 static const DECLARE_TLV_DB_SCALE(db_scale_forte_input_gain, 0, 179, 0);
799 
800 static const struct snd_kcontrol_new usb_forte_ctl_input_gain = {
801 	.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
802 	.access = SNDRV_CTL_ELEM_ACCESS_READWRITE |
803 		  SNDRV_CTL_ELEM_ACCESS_TLV_READ,
804 	.name = "",
805 	.info = forte_input_gain_info,
806 	.get =  forte_input_gain_get,
807 	.put =  forte_input_gain_put,
808 	.tlv = { .p = db_scale_forte_input_gain }
809 };
810 
811 static int add_new_ctl(struct usb_mixer_interface *mixer,
812 		       const struct snd_kcontrol_new *ncontrol,
813 		       usb_mixer_elem_resume_func_t resume,
814 		       int index, int offset, int num,
815 		       int val_type, int channels, const char *name,
816 		       const struct scarlett_mixer_elem_enum_info *opt,
817 		       struct usb_mixer_elem_info **elem_ret
818 )
819 {
820 	struct snd_kcontrol *kctl;
821 	struct usb_mixer_elem_info *elem;
822 	int err;
823 
824 	elem = kzalloc_obj(*elem);
825 	if (!elem)
826 		return -ENOMEM;
827 
828 	elem->head.mixer = mixer;
829 	elem->head.resume = resume;
830 	elem->control = offset;
831 	elem->idx_off = num;
832 	elem->head.id = index;
833 	elem->val_type = val_type;
834 
835 	elem->channels = channels;
836 
837 	/* add scarlett_mixer_elem_enum_info struct */
838 	elem->private_data = (void *)opt;
839 
840 	kctl = snd_ctl_new1(ncontrol, elem);
841 	if (!kctl) {
842 		kfree(elem);
843 		return -ENOMEM;
844 	}
845 	kctl->private_free = snd_usb_mixer_elem_free;
846 
847 	strscpy(kctl->id.name, name, sizeof(kctl->id.name));
848 
849 	err = snd_usb_mixer_add_control(&elem->head, kctl);
850 	if (err < 0)
851 		return err;
852 
853 	if (elem_ret)
854 		*elem_ret = elem;
855 
856 	return 0;
857 }
858 
859 static int add_output_ctls(struct usb_mixer_interface *mixer,
860 			   int index, const char *name,
861 			   const struct scarlett_device_info *info)
862 {
863 	int err;
864 	char mx[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
865 	struct usb_mixer_elem_info *elem;
866 
867 	/* Add mute switch */
868 	snprintf(mx, sizeof(mx), "Master %d (%s) Playback Switch",
869 		index + 1, name);
870 	err = add_new_ctl(mixer, &usb_scarlett_ctl_switch,
871 			  scarlett_ctl_resume, 0x0a, 0x01,
872 			  2*index+1, USB_MIXER_S16, 2, mx, NULL, &elem);
873 	if (err < 0)
874 		return err;
875 
876 	/* Add volume control and initialize to 0 */
877 	snprintf(mx, sizeof(mx), "Master %d (%s) Playback Volume",
878 		index + 1, name);
879 	err = add_new_ctl(mixer, &usb_scarlett_ctl_master,
880 			  scarlett_ctl_resume, 0x0a, 0x02,
881 			  2*index+1, USB_MIXER_S16, 2, mx, NULL, &elem);
882 	if (err < 0)
883 		return err;
884 
885 	/* Add L/R source routing if device supports it */
886 	if (info->has_output_source_routing) {
887 		/* Add L channel source playback enumeration */
888 		snprintf(mx, sizeof(mx), "Master %dL (%s) Source Playback Enum",
889 			index + 1, name);
890 		err = add_new_ctl(mixer, &usb_scarlett_ctl_dynamic_enum,
891 				  scarlett_ctl_enum_resume, 0x33, 0x00,
892 				  2*index, USB_MIXER_S16, 1, mx, &info->opt_master,
893 				  &elem);
894 		if (err < 0)
895 			return err;
896 
897 		/* Add R channel source playback enumeration */
898 		snprintf(mx, sizeof(mx), "Master %dR (%s) Source Playback Enum",
899 			index + 1, name);
900 		err = add_new_ctl(mixer, &usb_scarlett_ctl_dynamic_enum,
901 				  scarlett_ctl_enum_resume, 0x33, 0x00,
902 				  2*index+1, USB_MIXER_S16, 1, mx, &info->opt_master,
903 				  &elem);
904 		if (err < 0)
905 			return err;
906 	}
907 
908 	return 0;
909 }
910 
911 /********************** device-specific config *************************/
912 
913 static const struct scarlett_device_info forte_info = {
914 	.matrix_in = 6,
915 	.matrix_out = 4,
916 	.input_len = 2,
917 	.output_len = 4,
918 	.has_output_source_routing = false,
919 
920 	.opt_master = {
921 		.start = -1,
922 		.len = 13,
923 		.offsets = {0, 4, 6, 6, 6},
924 		.names = NULL
925 	},
926 
927 	.opt_matrix = {
928 		.start = -1,
929 		.len = 7,
930 		.offsets = {0, 4, 6, 6, 6},
931 		.names = NULL
932 	},
933 
934 	.num_controls = 14,
935 	.controls = {
936 		{ .num = 0, .type = SCARLETT_OUTPUTS, .name = "Line Out" },
937 		{ .num = 1, .type = SCARLETT_OUTPUTS, .name = "Headphone" },
938 		/* Input 1 controls */
939 		{ .num = 1, .type = FORTE_INPUT_GAIN, .name = NULL},
940 		{ .num = 1, .type = FORTE_INPUT_SOURCE, .name = NULL},
941 		{ .num = 1, .type = FORTE_INPUT_HPF, .name = NULL},
942 		{ .num = 1, .type = FORTE_INPUT_PHANTOM, .name = NULL},
943 		{ .num = 1, .type = FORTE_INPUT_PHASE, .name = NULL},
944 		{ .num = 1, .type = FORTE_INPUT_PAD, .name = NULL},
945 		/* Input 2 controls */
946 		{ .num = 2, .type = FORTE_INPUT_GAIN, .name = NULL},
947 		{ .num = 2, .type = FORTE_INPUT_SOURCE, .name = NULL},
948 		{ .num = 2, .type = FORTE_INPUT_HPF, .name = NULL},
949 		{ .num = 2, .type = FORTE_INPUT_PHANTOM, .name = NULL},
950 		{ .num = 2, .type = FORTE_INPUT_PHASE, .name = NULL},
951 		{ .num = 2, .type = FORTE_INPUT_PAD, .name = NULL},
952 	},
953 };
954 
955 /*  untested...  */
956 static const struct scarlett_device_info s6i6_info = {
957 	.matrix_in = 18,
958 	.matrix_out = 8,
959 	.input_len = 6,
960 	.output_len = 6,
961 	.has_output_source_routing = true,
962 
963 	.opt_master = {
964 		.start = -1,
965 		.len = 27,
966 		.offsets = {0, 12, 16, 18, 18},
967 		.names = NULL
968 	},
969 
970 	.opt_matrix = {
971 		.start = -1,
972 		.len = 19,
973 		.offsets = {0, 12, 16, 18, 18},
974 		.names = NULL
975 	},
976 
977 	.num_controls = 9,
978 	.controls = {
979 		{ .num = 0, .type = SCARLETT_OUTPUTS, .name = "Monitor" },
980 		{ .num = 1, .type = SCARLETT_OUTPUTS, .name = "Headphone" },
981 		{ .num = 2, .type = SCARLETT_OUTPUTS, .name = "SPDIF" },
982 		{ .num = 1, .type = SCARLETT_SWITCH_IMPEDANCE, .name = NULL},
983 		{ .num = 1, .type = SCARLETT_SWITCH_PAD, .name = NULL},
984 		{ .num = 2, .type = SCARLETT_SWITCH_IMPEDANCE, .name = NULL},
985 		{ .num = 2, .type = SCARLETT_SWITCH_PAD, .name = NULL},
986 		{ .num = 3, .type = SCARLETT_SWITCH_GAIN, .name = NULL},
987 		{ .num = 4, .type = SCARLETT_SWITCH_GAIN, .name = NULL},
988 	},
989 
990 	.matrix_mux_init = {
991 		12, 13, 14, 15,                 /* Analog -> 1..4 */
992 		16, 17,                          /* SPDIF -> 5,6 */
993 		0, 1, 2, 3, 4, 5, 6, 7,     /* PCM[1..12] -> 7..18 */
994 		8, 9, 10, 11
995 	}
996 };
997 
998 /*  untested...  */
999 static const struct scarlett_device_info s8i6_info = {
1000 	.matrix_in = 18,
1001 	.matrix_out = 6,
1002 	.input_len = 8,
1003 	.output_len = 6,
1004 	.has_output_source_routing = true,
1005 
1006 	.opt_master = {
1007 		.start = -1,
1008 		.len = 25,
1009 		.offsets = {0, 12, 16, 18, 18},
1010 		.names = NULL
1011 	},
1012 
1013 	.opt_matrix = {
1014 		.start = -1,
1015 		.len = 19,
1016 		.offsets = {0, 12, 16, 18, 18},
1017 		.names = NULL
1018 	},
1019 
1020 	.num_controls = 7,
1021 	.controls = {
1022 		{ .num = 0, .type = SCARLETT_OUTPUTS, .name = "Monitor" },
1023 		{ .num = 1, .type = SCARLETT_OUTPUTS, .name = "Headphone" },
1024 		{ .num = 2, .type = SCARLETT_OUTPUTS, .name = "SPDIF" },
1025 		{ .num = 1, .type = SCARLETT_SWITCH_IMPEDANCE, .name = NULL},
1026 		{ .num = 2, .type = SCARLETT_SWITCH_IMPEDANCE, .name = NULL},
1027 		{ .num = 3, .type = SCARLETT_SWITCH_PAD, .name = NULL},
1028 		{ .num = 4, .type = SCARLETT_SWITCH_PAD, .name = NULL},
1029 	},
1030 
1031 	.matrix_mux_init = {
1032 		12, 13, 14, 15,                 /* Analog -> 1..4 */
1033 		16, 17,                          /* SPDIF -> 5,6 */
1034 		0, 1, 2, 3, 4, 5, 6, 7,     /* PCM[1..12] -> 7..18 */
1035 		8, 9, 10, 11
1036 	}
1037 };
1038 
1039 static const struct scarlett_device_info s18i6_info = {
1040 	.matrix_in = 18,
1041 	.matrix_out = 6,
1042 	.input_len = 18,
1043 	.output_len = 6,
1044 	.has_output_source_routing = true,
1045 
1046 	.opt_master = {
1047 		.start = -1,
1048 		.len = 31,
1049 		.offsets = {0, 6, 14, 16, 24},
1050 		.names = NULL,
1051 	},
1052 
1053 	.opt_matrix = {
1054 		.start = -1,
1055 		.len = 25,
1056 		.offsets = {0, 6, 14, 16, 24},
1057 		.names = NULL,
1058 	},
1059 
1060 	.num_controls = 5,
1061 	.controls = {
1062 		{ .num = 0, .type = SCARLETT_OUTPUTS, .name = "Monitor" },
1063 		{ .num = 1, .type = SCARLETT_OUTPUTS, .name = "Headphone" },
1064 		{ .num = 2, .type = SCARLETT_OUTPUTS, .name = "SPDIF" },
1065 		{ .num = 1, .type = SCARLETT_SWITCH_IMPEDANCE, .name = NULL},
1066 		{ .num = 2, .type = SCARLETT_SWITCH_IMPEDANCE, .name = NULL},
1067 	},
1068 
1069 	.matrix_mux_init = {
1070 		 6,  7,  8,  9, 10, 11, 12, 13, /* Analog -> 1..8 */
1071 		16, 17, 18, 19, 20, 21,     /* ADAT[1..6] -> 9..14 */
1072 		14, 15,                          /* SPDIF -> 15,16 */
1073 		0, 1                          /* PCM[1,2] -> 17,18 */
1074 	}
1075 };
1076 
1077 static const struct scarlett_device_info s18i8_info = {
1078 	.matrix_in = 18,
1079 	.matrix_out = 8,
1080 	.input_len = 18,
1081 	.output_len = 8,
1082 	.has_output_source_routing = true,
1083 
1084 	.opt_master = {
1085 		.start = -1,
1086 		.len = 35,
1087 		.offsets = {0, 8, 16, 18, 26},
1088 		.names = NULL
1089 	},
1090 
1091 	.opt_matrix = {
1092 		.start = -1,
1093 		.len = 27,
1094 		.offsets = {0, 8, 16, 18, 26},
1095 		.names = NULL
1096 	},
1097 
1098 	.num_controls = 10,
1099 	.controls = {
1100 		{ .num = 0, .type = SCARLETT_OUTPUTS, .name = "Monitor" },
1101 		{ .num = 1, .type = SCARLETT_OUTPUTS, .name = "Headphone 1" },
1102 		{ .num = 2, .type = SCARLETT_OUTPUTS, .name = "Headphone 2" },
1103 		{ .num = 3, .type = SCARLETT_OUTPUTS, .name = "SPDIF" },
1104 		{ .num = 1, .type = SCARLETT_SWITCH_IMPEDANCE, .name = NULL},
1105 		{ .num = 1, .type = SCARLETT_SWITCH_PAD, .name = NULL},
1106 		{ .num = 2, .type = SCARLETT_SWITCH_IMPEDANCE, .name = NULL},
1107 		{ .num = 2, .type = SCARLETT_SWITCH_PAD, .name = NULL},
1108 		{ .num = 3, .type = SCARLETT_SWITCH_PAD, .name = NULL},
1109 		{ .num = 4, .type = SCARLETT_SWITCH_PAD, .name = NULL},
1110 	},
1111 
1112 	.matrix_mux_init = {
1113 		 8,  9, 10, 11, 12, 13, 14, 15, /* Analog -> 1..8 */
1114 		18, 19, 20, 21, 22, 23,     /* ADAT[1..6] -> 9..14 */
1115 		16, 17,                          /* SPDIF -> 15,16 */
1116 		0, 1                          /* PCM[1,2] -> 17,18 */
1117 	}
1118 };
1119 
1120 static const struct scarlett_device_info s18i20_info = {
1121 	.matrix_in = 18,
1122 	.matrix_out = 8,
1123 	.input_len = 18,
1124 	.output_len = 20,
1125 	.has_output_source_routing = true,
1126 
1127 	.opt_master = {
1128 		.start = -1,
1129 		.len = 47,
1130 		.offsets = {0, 20, 28, 30, 38},
1131 		.names = NULL
1132 	},
1133 
1134 	.opt_matrix = {
1135 		.start = -1,
1136 		.len = 39,
1137 		.offsets = {0, 20, 28, 30, 38},
1138 		.names = NULL
1139 	},
1140 
1141 	.num_controls = 10,
1142 	.controls = {
1143 		{ .num = 0, .type = SCARLETT_OUTPUTS, .name = "Monitor" },
1144 		{ .num = 1, .type = SCARLETT_OUTPUTS, .name = "Line 3/4" },
1145 		{ .num = 2, .type = SCARLETT_OUTPUTS, .name = "Line 5/6" },
1146 		{ .num = 3, .type = SCARLETT_OUTPUTS, .name = "Line 7/8" },
1147 		{ .num = 4, .type = SCARLETT_OUTPUTS, .name = "Line 9/10" },
1148 		{ .num = 5, .type = SCARLETT_OUTPUTS, .name = "SPDIF" },
1149 		{ .num = 6, .type = SCARLETT_OUTPUTS, .name = "ADAT 1/2" },
1150 		{ .num = 7, .type = SCARLETT_OUTPUTS, .name = "ADAT 3/4" },
1151 		{ .num = 8, .type = SCARLETT_OUTPUTS, .name = "ADAT 5/6" },
1152 		{ .num = 9, .type = SCARLETT_OUTPUTS, .name = "ADAT 7/8" },
1153 		/*{ .num = 1, .type = SCARLETT_SWITCH_IMPEDANCE, .name = NULL},
1154 		{ .num = 1, .type = SCARLETT_SWITCH_PAD, .name = NULL},
1155 		{ .num = 2, .type = SCARLETT_SWITCH_IMPEDANCE, .name = NULL},
1156 		{ .num = 2, .type = SCARLETT_SWITCH_PAD, .name = NULL},
1157 		{ .num = 3, .type = SCARLETT_SWITCH_PAD, .name = NULL},
1158 		{ .num = 4, .type = SCARLETT_SWITCH_PAD, .name = NULL},*/
1159 	},
1160 
1161 	.matrix_mux_init = {
1162 		20, 21, 22, 23, 24, 25, 26, 27, /* Analog -> 1..8 */
1163 		30, 31, 32, 33, 34, 35,     /* ADAT[1..6] -> 9..14 */
1164 		28, 29,                          /* SPDIF -> 15,16 */
1165 		0, 1                          /* PCM[1,2] -> 17,18 */
1166 	}
1167 };
1168 
1169 
1170 static int scarlett_controls_create_generic(struct usb_mixer_interface *mixer,
1171 	const struct scarlett_device_info *info)
1172 {
1173 	int i, err;
1174 	char mx[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
1175 	const struct scarlett_mixer_control *ctl;
1176 	struct usb_mixer_elem_info *elem;
1177 
1178 	/* create master switch and playback volume */
1179 	err = add_new_ctl(mixer, &usb_scarlett_ctl_switch,
1180 			  scarlett_ctl_resume, 0x0a, 0x01, 0,
1181 			  USB_MIXER_S16, 1, "Master Playback Switch", NULL,
1182 			  &elem);
1183 	if (err < 0)
1184 		return err;
1185 
1186 	err = add_new_ctl(mixer, &usb_scarlett_ctl_master,
1187 			  scarlett_ctl_resume, 0x0a, 0x02, 0,
1188 			  USB_MIXER_S16, 1, "Master Playback Volume", NULL,
1189 			  &elem);
1190 	if (err < 0)
1191 		return err;
1192 
1193 	/* iterate through controls in info struct and create each one */
1194 	for (i = 0; i < info->num_controls; i++) {
1195 		ctl = &info->controls[i];
1196 
1197 		switch (ctl->type) {
1198 		case SCARLETT_OUTPUTS:
1199 			err = add_output_ctls(mixer, ctl->num, ctl->name, info);
1200 			if (err < 0)
1201 				return err;
1202 			break;
1203 		case SCARLETT_SWITCH_IMPEDANCE:
1204 			scnprintf(mx, sizeof(mx),
1205 				  "Input %d Impedance Switch", ctl->num);
1206 			err = add_new_ctl(mixer, &usb_scarlett_ctl_enum,
1207 					  scarlett_ctl_enum_resume, 0x01,
1208 					  0x09, ctl->num, USB_MIXER_S16, 1, mx,
1209 					  &opt_impedance, &elem);
1210 			if (err < 0)
1211 				return err;
1212 			break;
1213 		case SCARLETT_SWITCH_PAD:
1214 			scnprintf(mx, sizeof(mx),
1215 				  "Input %d Pad Switch", ctl->num);
1216 			err = add_new_ctl(mixer, &usb_scarlett_ctl_enum,
1217 					  scarlett_ctl_enum_resume, 0x01,
1218 					  0x0b, ctl->num, USB_MIXER_S16, 1, mx,
1219 					  &opt_pad, &elem);
1220 			if (err < 0)
1221 				return err;
1222 			break;
1223 		case SCARLETT_SWITCH_GAIN:
1224 			scnprintf(mx, sizeof(mx),
1225 				  "Input %d Gain Switch", ctl->num);
1226 			err = add_new_ctl(mixer, &usb_scarlett_ctl_enum,
1227 					  scarlett_ctl_enum_resume, 0x01,
1228 					  0x08, ctl->num, USB_MIXER_S16, 1, mx,
1229 					  &opt_gain, &elem);
1230 			if (err < 0)
1231 				return err;
1232 			break;
1233 		/* Forte input controls - use wIndex 0x3c per wiki docs */
1234 		case FORTE_INPUT_GAIN:
1235 			sprintf(mx, "Line In %d Gain Capture Volume", ctl->num);
1236 			err = add_new_ctl(mixer, &usb_forte_ctl_input_gain,
1237 					  forte_input_gain_resume, 0x3c,
1238 					  0x06, ctl->num - 1, USB_MIXER_S16, 1, mx,
1239 					  NULL, &elem);
1240 			if (err < 0)
1241 				return err;
1242 			break;
1243 		case FORTE_INPUT_SOURCE:
1244 			sprintf(mx, "Input %d Source", ctl->num);
1245 			err = add_new_ctl(mixer, &usb_forte_ctl_enum,
1246 					  forte_ctl_enum_resume, 0x3c,
1247 					  0x07, ctl->num - 1, USB_MIXER_S16, 1, mx,
1248 					  &opt_forte_source, &elem);
1249 			if (err < 0)
1250 				return err;
1251 			break;
1252 		case FORTE_INPUT_HPF:
1253 			sprintf(mx, "Input %d High Pass Filter Switch", ctl->num);
1254 			err = add_new_ctl(mixer, &usb_forte_ctl_switch,
1255 					  forte_ctl_switch_resume, 0x3c,
1256 					  0x08, ctl->num - 1, USB_MIXER_S16, 1, mx,
1257 					  NULL, &elem);
1258 			if (err < 0)
1259 				return err;
1260 			break;
1261 		case FORTE_INPUT_PHANTOM:
1262 			sprintf(mx, "Input %d 48V Phantom Power Switch", ctl->num);
1263 			err = add_new_ctl(mixer, &usb_forte_ctl_switch,
1264 					  forte_ctl_switch_resume, 0x3c,
1265 					  0x09, ctl->num - 1, USB_MIXER_S16, 1, mx,
1266 					  NULL, &elem);
1267 			if (err < 0)
1268 				return err;
1269 			break;
1270 		case FORTE_INPUT_PHASE:
1271 			sprintf(mx, "Input %d Phase Invert Switch", ctl->num);
1272 			err = add_new_ctl(mixer, &usb_forte_ctl_switch,
1273 					  forte_ctl_switch_resume, 0x3c,
1274 					  0x0a, ctl->num - 1, USB_MIXER_S16, 1, mx,
1275 					  NULL, &elem);
1276 			if (err < 0)
1277 				return err;
1278 			break;
1279 		case FORTE_INPUT_PAD:
1280 			sprintf(mx, "Input %d Pad Switch", ctl->num);
1281 			err = add_new_ctl(mixer, &usb_forte_ctl_switch,
1282 					  forte_ctl_switch_resume, 0x3c,
1283 					  0x0b, ctl->num - 1, USB_MIXER_S16, 1, mx,
1284 					  NULL, &elem);
1285 			if (err < 0)
1286 				return err;
1287 			break;
1288 		}
1289 	}
1290 
1291 	return 0;
1292 }
1293 
1294 /*
1295  * Create and initialize a mixer for the Focusrite(R) Scarlett
1296  */
1297 int snd_scarlett_controls_create(struct usb_mixer_interface *mixer)
1298 {
1299 	int err, i, o;
1300 	char mx[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
1301 	const struct scarlett_device_info *info;
1302 	struct usb_mixer_elem_info *elem;
1303 	static char sample_rate_buffer[4] = { '\x80', '\xbb', '\x00', '\x00' };
1304 
1305 	/* only use UAC_VERSION_2 */
1306 	if (!mixer->protocol)
1307 		return 0;
1308 
1309 	switch (mixer->chip->usb_id) {
1310 	case USB_ID(0x1235, 0x8012):
1311 		info = &s6i6_info;
1312 		break;
1313 	case USB_ID(0x1235, 0x8002):
1314 		info = &s8i6_info;
1315 		break;
1316 	case USB_ID(0x1235, 0x8004):
1317 		info = &s18i6_info;
1318 		break;
1319 	case USB_ID(0x1235, 0x8014):
1320 		info = &s18i8_info;
1321 		break;
1322 	case USB_ID(0x1235, 0x800c):
1323 		info = &s18i20_info;
1324 		break;
1325 	default: /* device not (yet) supported */
1326 		return -EINVAL;
1327 	}
1328 
1329 	/* generic function to create controls */
1330 	err = scarlett_controls_create_generic(mixer, info);
1331 	if (err < 0)
1332 		return err;
1333 
1334 	/* setup matrix controls */
1335 	for (i = 0; i < info->matrix_in; i++) {
1336 		snprintf(mx, sizeof(mx), "Matrix %02d Input Playback Route",
1337 			 i+1);
1338 		err = add_new_ctl(mixer, &usb_scarlett_ctl_dynamic_enum,
1339 				  scarlett_ctl_enum_resume, 0x32,
1340 				  0x06, i, USB_MIXER_S16, 1, mx,
1341 				  &info->opt_matrix, &elem);
1342 		if (err < 0)
1343 			return err;
1344 
1345 		for (o = 0; o < info->matrix_out; o++) {
1346 			scnprintf(mx, sizeof(mx),
1347 				  "Matrix %02d Mix %c Playback Volume", i+1,
1348 				  o+'A');
1349 			err = add_new_ctl(mixer, &usb_scarlett_ctl,
1350 					  scarlett_ctl_resume, 0x3c, 0x00,
1351 					  (i << 3) + (o & 0x07), USB_MIXER_S16,
1352 					  1, mx, NULL, &elem);
1353 			if (err < 0)
1354 				return err;
1355 
1356 		}
1357 	}
1358 
1359 	for (i = 0; i < info->input_len; i++) {
1360 		snprintf(mx, sizeof(mx), "Input Source %02d Capture Route",
1361 			 i+1);
1362 		err = add_new_ctl(mixer, &usb_scarlett_ctl_dynamic_enum,
1363 				  scarlett_ctl_enum_resume, 0x34,
1364 				  0x00, i, USB_MIXER_S16, 1, mx,
1365 				  &info->opt_master, &elem);
1366 		if (err < 0)
1367 			return err;
1368 	}
1369 
1370 	/* val_len == 1 needed here */
1371 	err = add_new_ctl(mixer, &usb_scarlett_ctl_enum,
1372 			  scarlett_ctl_enum_resume, 0x28, 0x01, 0,
1373 			  USB_MIXER_U8, 1, "Sample Clock Source",
1374 			  &opt_clock, &elem);
1375 	if (err < 0)
1376 		return err;
1377 
1378 	/* val_len == 1 and UAC2_CS_MEM */
1379 	err = add_new_ctl(mixer, &usb_scarlett_ctl_sync, NULL, 0x3c, 0x00, 2,
1380 			  USB_MIXER_U8, 1, "Sample Clock Sync Status",
1381 			  &opt_sync, &elem);
1382 	if (err < 0)
1383 		return err;
1384 
1385 	/* initialize sampling rate to 48000 */
1386 	err = snd_usb_ctl_msg(mixer->chip->dev,
1387 		usb_sndctrlpipe(mixer->chip->dev, 0), UAC2_CS_CUR,
1388 		USB_RECIP_INTERFACE | USB_TYPE_CLASS |
1389 		USB_DIR_OUT, 0x0100, snd_usb_ctrl_intf(mixer->hostif) |
1390 		(0x29 << 8), sample_rate_buffer, 4);
1391 	if (err < 0)
1392 		return err;
1393 
1394 	return err;
1395 }
1396 
1397 /*
1398  * Create and initialize a mixer for the Focusrite(R) Forte
1399  */
1400 int snd_forte_controls_create(struct usb_mixer_interface *mixer)
1401 {
1402 	int err, i, o;
1403 	char mx[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
1404 	const struct scarlett_device_info *info;
1405 	struct usb_mixer_elem_info *elem;
1406 
1407 	/* only use UAC_VERSION_2 */
1408 	if (!mixer->protocol)
1409 		return 0;
1410 
1411 	switch (mixer->chip->usb_id) {
1412 	case USB_ID(0x1235, 0x8010):
1413 		info = &forte_info;
1414 		break;
1415 	default: /* device not (yet) supported */
1416 		return -EINVAL;
1417 	}
1418 
1419 	/* generic function to create controls */
1420 	err = scarlett_controls_create_generic(mixer, info);
1421 	if (err < 0)
1422 		return err;
1423 
1424 	/* setup matrix controls */
1425 	for (i = 0; i < info->matrix_in; i++) {
1426 		snprintf(mx, sizeof(mx), "Matrix %02d Input Playback Route",
1427 			 i+1);
1428 		err = add_new_ctl(mixer, &usb_scarlett_ctl_dynamic_enum,
1429 				  scarlett_ctl_enum_resume, 0x32,
1430 				  0x06, i, USB_MIXER_S16, 1, mx,
1431 				  &info->opt_matrix, &elem);
1432 		if (err < 0)
1433 			return err;
1434 
1435 		for (o = 0; o < info->matrix_out; o++) {
1436 			sprintf(mx, "Matrix %02d Mix %c Playback Volume", i+1,
1437 				o+'A');
1438 			err = add_new_ctl(mixer, &usb_scarlett_ctl,
1439 					  scarlett_ctl_resume, 0x3c, 0x01,
1440 					  (i << 2) + (o & 0x03), USB_MIXER_S16,
1441 					  1, mx, NULL, &elem);
1442 			if (err < 0)
1443 				return err;
1444 
1445 		}
1446 	}
1447 
1448 	/* val_len == 1 and UAC2_CS_MEM */
1449 	err = add_new_ctl(mixer, &usb_scarlett_ctl_sync, NULL, 0x3c, 0x00, 2,
1450 			  USB_MIXER_U8, 1, "Sample Clock Sync Status",
1451 			  &opt_sync, &elem);
1452 	if (err < 0)
1453 		return err;
1454 
1455 	return err;
1456 }
1457