xref: /linux/sound/usb/mixer_scarlett.c (revision bf4afc53b77aeaa48b5409da5c8da6bb4eff7f43)
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  */
forte_set_ctl_value(struct usb_mixer_elem_info * elem,int value)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 
forte_get_ctl_value(struct usb_mixer_elem_info * elem,int * value)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 
forte_input_gain_info(struct snd_kcontrol * kctl,struct snd_ctl_elem_info * uinfo)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 
forte_input_gain_get(struct snd_kcontrol * kctl,struct snd_ctl_elem_value * ucontrol)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 
forte_input_gain_put(struct snd_kcontrol * kctl,struct snd_ctl_elem_value * ucontrol)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 
forte_input_gain_resume(struct usb_mixer_elem_list * list)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  */
forte_ctl_enum_get(struct snd_kcontrol * kctl,struct snd_ctl_elem_value * ucontrol)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 
forte_ctl_enum_put(struct snd_kcontrol * kctl,struct snd_ctl_elem_value * ucontrol)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 
forte_ctl_enum_resume(struct usb_mixer_elem_list * list)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  */
forte_ctl_switch_get(struct snd_kcontrol * kctl,struct snd_ctl_elem_value * ucontrol)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 
forte_ctl_switch_put(struct snd_kcontrol * kctl,struct snd_ctl_elem_value * ucontrol)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 
forte_ctl_switch_resume(struct usb_mixer_elem_list * list)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 
scarlett_ctl_switch_info(struct snd_kcontrol * kctl,struct snd_ctl_elem_info * uinfo)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 
scarlett_ctl_switch_get(struct snd_kcontrol * kctl,struct snd_ctl_elem_value * ucontrol)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 
scarlett_ctl_switch_put(struct snd_kcontrol * kctl,struct snd_ctl_elem_value * ucontrol)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 
scarlett_ctl_resume(struct usb_mixer_elem_list * list)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 
scarlett_ctl_info(struct snd_kcontrol * kctl,struct snd_ctl_elem_info * uinfo)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 
scarlett_ctl_get(struct snd_kcontrol * kctl,struct snd_ctl_elem_value * ucontrol)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 
scarlett_ctl_put(struct snd_kcontrol * kctl,struct snd_ctl_elem_value * ucontrol)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 
scarlett_generate_name(int i,char * dst,size_t size,int offsets[])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 
scarlett_ctl_enum_dynamic_info(struct snd_kcontrol * kctl,struct snd_ctl_elem_info * uinfo)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 
scarlett_ctl_enum_info(struct snd_kcontrol * kctl,struct snd_ctl_elem_info * uinfo)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 
scarlett_ctl_enum_get(struct snd_kcontrol * kctl,struct snd_ctl_elem_value * ucontrol)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 
scarlett_ctl_enum_put(struct snd_kcontrol * kctl,struct snd_ctl_elem_value * ucontrol)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 		snd_usb_set_cur_mix_value(elem, 0, 0, val);
684 		return 1;
685 	}
686 	return 0;
687 }
688 
scarlett_ctl_enum_resume(struct usb_mixer_elem_list * list)689 static int scarlett_ctl_enum_resume(struct usb_mixer_elem_list *list)
690 {
691 	struct usb_mixer_elem_info *elem = mixer_elem_list_to_info(list);
692 
693 	if (elem->cached)
694 		snd_usb_set_cur_mix_value(elem, 0, 0, *elem->cache_val);
695 	return 0;
696 }
697 
scarlett_ctl_meter_get(struct snd_kcontrol * kctl,struct snd_ctl_elem_value * ucontrol)698 static int scarlett_ctl_meter_get(struct snd_kcontrol *kctl,
699 				  struct snd_ctl_elem_value *ucontrol)
700 {
701 	struct usb_mixer_elem_info *elem = kctl->private_data;
702 	struct snd_usb_audio *chip = elem->head.mixer->chip;
703 	unsigned char buf[2 * MAX_CHANNELS] = {0, };
704 	int wValue = (elem->control << 8) | elem->idx_off;
705 	int idx = snd_usb_ctrl_intf(elem->head.mixer->hostif) | (elem->head.id << 8);
706 	int err;
707 
708 	err = snd_usb_ctl_msg(chip->dev,
709 				usb_rcvctrlpipe(chip->dev, 0),
710 				UAC2_CS_MEM,
711 				USB_RECIP_INTERFACE | USB_TYPE_CLASS |
712 				USB_DIR_IN, wValue, idx, buf, elem->channels);
713 	if (err < 0)
714 		return err;
715 
716 	ucontrol->value.enumerated.item[0] = clamp((int)buf[0], 0, 1);
717 	return 0;
718 }
719 
720 static const struct snd_kcontrol_new usb_scarlett_ctl_switch = {
721 	.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
722 	.name = "",
723 	.info = scarlett_ctl_switch_info,
724 	.get =  scarlett_ctl_switch_get,
725 	.put =  scarlett_ctl_switch_put,
726 };
727 
728 static const DECLARE_TLV_DB_SCALE(db_scale_scarlett_gain, -12800, 100, 0);
729 
730 static const struct snd_kcontrol_new usb_scarlett_ctl = {
731 	.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
732 	.access = SNDRV_CTL_ELEM_ACCESS_READWRITE |
733 		  SNDRV_CTL_ELEM_ACCESS_TLV_READ,
734 	.name = "",
735 	.info = scarlett_ctl_info,
736 	.get =  scarlett_ctl_get,
737 	.put =  scarlett_ctl_put,
738 	.private_value = 6,  /* max value */
739 	.tlv = { .p = db_scale_scarlett_gain }
740 };
741 
742 static const struct snd_kcontrol_new usb_scarlett_ctl_master = {
743 	.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
744 	.access = SNDRV_CTL_ELEM_ACCESS_READWRITE |
745 		  SNDRV_CTL_ELEM_ACCESS_TLV_READ,
746 	.name = "",
747 	.info = scarlett_ctl_info,
748 	.get =  scarlett_ctl_get,
749 	.put =  scarlett_ctl_put,
750 	.private_value = 6,  /* max value */
751 	.tlv = { .p = db_scale_scarlett_gain }
752 };
753 
754 static const struct snd_kcontrol_new usb_scarlett_ctl_enum = {
755 	.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
756 	.name = "",
757 	.info = scarlett_ctl_enum_info,
758 	.get =  scarlett_ctl_enum_get,
759 	.put =  scarlett_ctl_enum_put,
760 };
761 
762 static const struct snd_kcontrol_new usb_scarlett_ctl_dynamic_enum = {
763 	.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
764 	.name = "",
765 	.info = scarlett_ctl_enum_dynamic_info,
766 	.get =  scarlett_ctl_enum_get,
767 	.put =  scarlett_ctl_enum_put,
768 };
769 
770 static const struct snd_kcontrol_new usb_scarlett_ctl_sync = {
771 	.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
772 	.access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE,
773 	.name = "",
774 	.info = scarlett_ctl_enum_info,
775 	.get =  scarlett_ctl_meter_get,
776 };
777 
778 /* Forte-specific control structures - use bRequest=0x03 instead of 0x01 */
779 static const struct snd_kcontrol_new usb_forte_ctl_enum = {
780 	.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
781 	.name = "",
782 	.info = scarlett_ctl_enum_info,
783 	.get =  forte_ctl_enum_get,
784 	.put =  forte_ctl_enum_put,
785 };
786 
787 static const struct snd_kcontrol_new usb_forte_ctl_switch = {
788 	.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
789 	.name = "",
790 	.info = snd_ctl_boolean_mono_info,
791 	.get =  forte_ctl_switch_get,
792 	.put =  forte_ctl_switch_put,
793 };
794 
795 /* Forte input gain: 0-42 maps to approximately 0-75dB (~179 cB per step) */
796 static const DECLARE_TLV_DB_SCALE(db_scale_forte_input_gain, 0, 179, 0);
797 
798 static const struct snd_kcontrol_new usb_forte_ctl_input_gain = {
799 	.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
800 	.access = SNDRV_CTL_ELEM_ACCESS_READWRITE |
801 		  SNDRV_CTL_ELEM_ACCESS_TLV_READ,
802 	.name = "",
803 	.info = forte_input_gain_info,
804 	.get =  forte_input_gain_get,
805 	.put =  forte_input_gain_put,
806 	.tlv = { .p = db_scale_forte_input_gain }
807 };
808 
add_new_ctl(struct usb_mixer_interface * mixer,const struct snd_kcontrol_new * ncontrol,usb_mixer_elem_resume_func_t resume,int index,int offset,int num,int val_type,int channels,const char * name,const struct scarlett_mixer_elem_enum_info * opt,struct usb_mixer_elem_info ** elem_ret)809 static int add_new_ctl(struct usb_mixer_interface *mixer,
810 		       const struct snd_kcontrol_new *ncontrol,
811 		       usb_mixer_elem_resume_func_t resume,
812 		       int index, int offset, int num,
813 		       int val_type, int channels, const char *name,
814 		       const struct scarlett_mixer_elem_enum_info *opt,
815 		       struct usb_mixer_elem_info **elem_ret
816 )
817 {
818 	struct snd_kcontrol *kctl;
819 	struct usb_mixer_elem_info *elem;
820 	int err;
821 
822 	elem = kzalloc_obj(*elem);
823 	if (!elem)
824 		return -ENOMEM;
825 
826 	elem->head.mixer = mixer;
827 	elem->head.resume = resume;
828 	elem->control = offset;
829 	elem->idx_off = num;
830 	elem->head.id = index;
831 	elem->val_type = val_type;
832 
833 	elem->channels = channels;
834 
835 	/* add scarlett_mixer_elem_enum_info struct */
836 	elem->private_data = (void *)opt;
837 
838 	kctl = snd_ctl_new1(ncontrol, elem);
839 	if (!kctl) {
840 		kfree(elem);
841 		return -ENOMEM;
842 	}
843 	kctl->private_free = snd_usb_mixer_elem_free;
844 
845 	strscpy(kctl->id.name, name, sizeof(kctl->id.name));
846 
847 	err = snd_usb_mixer_add_control(&elem->head, kctl);
848 	if (err < 0)
849 		return err;
850 
851 	if (elem_ret)
852 		*elem_ret = elem;
853 
854 	return 0;
855 }
856 
add_output_ctls(struct usb_mixer_interface * mixer,int index,const char * name,const struct scarlett_device_info * info)857 static int add_output_ctls(struct usb_mixer_interface *mixer,
858 			   int index, const char *name,
859 			   const struct scarlett_device_info *info)
860 {
861 	int err;
862 	char mx[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
863 	struct usb_mixer_elem_info *elem;
864 
865 	/* Add mute switch */
866 	snprintf(mx, sizeof(mx), "Master %d (%s) Playback Switch",
867 		index + 1, name);
868 	err = add_new_ctl(mixer, &usb_scarlett_ctl_switch,
869 			  scarlett_ctl_resume, 0x0a, 0x01,
870 			  2*index+1, USB_MIXER_S16, 2, mx, NULL, &elem);
871 	if (err < 0)
872 		return err;
873 
874 	/* Add volume control and initialize to 0 */
875 	snprintf(mx, sizeof(mx), "Master %d (%s) Playback Volume",
876 		index + 1, name);
877 	err = add_new_ctl(mixer, &usb_scarlett_ctl_master,
878 			  scarlett_ctl_resume, 0x0a, 0x02,
879 			  2*index+1, USB_MIXER_S16, 2, mx, NULL, &elem);
880 	if (err < 0)
881 		return err;
882 
883 	/* Add L/R source routing if device supports it */
884 	if (info->has_output_source_routing) {
885 		/* Add L channel source playback enumeration */
886 		snprintf(mx, sizeof(mx), "Master %dL (%s) Source Playback Enum",
887 			index + 1, name);
888 		err = add_new_ctl(mixer, &usb_scarlett_ctl_dynamic_enum,
889 				  scarlett_ctl_enum_resume, 0x33, 0x00,
890 				  2*index, USB_MIXER_S16, 1, mx, &info->opt_master,
891 				  &elem);
892 		if (err < 0)
893 			return err;
894 
895 		/* Add R channel source playback enumeration */
896 		snprintf(mx, sizeof(mx), "Master %dR (%s) Source Playback Enum",
897 			index + 1, name);
898 		err = add_new_ctl(mixer, &usb_scarlett_ctl_dynamic_enum,
899 				  scarlett_ctl_enum_resume, 0x33, 0x00,
900 				  2*index+1, USB_MIXER_S16, 1, mx, &info->opt_master,
901 				  &elem);
902 		if (err < 0)
903 			return err;
904 	}
905 
906 	return 0;
907 }
908 
909 /********************** device-specific config *************************/
910 
911 static const struct scarlett_device_info forte_info = {
912 	.matrix_in = 6,
913 	.matrix_out = 4,
914 	.input_len = 2,
915 	.output_len = 4,
916 	.has_output_source_routing = false,
917 
918 	.opt_master = {
919 		.start = -1,
920 		.len = 13,
921 		.offsets = {0, 4, 6, 6, 6},
922 		.names = NULL
923 	},
924 
925 	.opt_matrix = {
926 		.start = -1,
927 		.len = 7,
928 		.offsets = {0, 4, 6, 6, 6},
929 		.names = NULL
930 	},
931 
932 	.num_controls = 14,
933 	.controls = {
934 		{ .num = 0, .type = SCARLETT_OUTPUTS, .name = "Line Out" },
935 		{ .num = 1, .type = SCARLETT_OUTPUTS, .name = "Headphone" },
936 		/* Input 1 controls */
937 		{ .num = 1, .type = FORTE_INPUT_GAIN, .name = NULL},
938 		{ .num = 1, .type = FORTE_INPUT_SOURCE, .name = NULL},
939 		{ .num = 1, .type = FORTE_INPUT_HPF, .name = NULL},
940 		{ .num = 1, .type = FORTE_INPUT_PHANTOM, .name = NULL},
941 		{ .num = 1, .type = FORTE_INPUT_PHASE, .name = NULL},
942 		{ .num = 1, .type = FORTE_INPUT_PAD, .name = NULL},
943 		/* Input 2 controls */
944 		{ .num = 2, .type = FORTE_INPUT_GAIN, .name = NULL},
945 		{ .num = 2, .type = FORTE_INPUT_SOURCE, .name = NULL},
946 		{ .num = 2, .type = FORTE_INPUT_HPF, .name = NULL},
947 		{ .num = 2, .type = FORTE_INPUT_PHANTOM, .name = NULL},
948 		{ .num = 2, .type = FORTE_INPUT_PHASE, .name = NULL},
949 		{ .num = 2, .type = FORTE_INPUT_PAD, .name = NULL},
950 	},
951 };
952 
953 /*  untested...  */
954 static const struct scarlett_device_info s6i6_info = {
955 	.matrix_in = 18,
956 	.matrix_out = 8,
957 	.input_len = 6,
958 	.output_len = 6,
959 	.has_output_source_routing = true,
960 
961 	.opt_master = {
962 		.start = -1,
963 		.len = 27,
964 		.offsets = {0, 12, 16, 18, 18},
965 		.names = NULL
966 	},
967 
968 	.opt_matrix = {
969 		.start = -1,
970 		.len = 19,
971 		.offsets = {0, 12, 16, 18, 18},
972 		.names = NULL
973 	},
974 
975 	.num_controls = 9,
976 	.controls = {
977 		{ .num = 0, .type = SCARLETT_OUTPUTS, .name = "Monitor" },
978 		{ .num = 1, .type = SCARLETT_OUTPUTS, .name = "Headphone" },
979 		{ .num = 2, .type = SCARLETT_OUTPUTS, .name = "SPDIF" },
980 		{ .num = 1, .type = SCARLETT_SWITCH_IMPEDANCE, .name = NULL},
981 		{ .num = 1, .type = SCARLETT_SWITCH_PAD, .name = NULL},
982 		{ .num = 2, .type = SCARLETT_SWITCH_IMPEDANCE, .name = NULL},
983 		{ .num = 2, .type = SCARLETT_SWITCH_PAD, .name = NULL},
984 		{ .num = 3, .type = SCARLETT_SWITCH_GAIN, .name = NULL},
985 		{ .num = 4, .type = SCARLETT_SWITCH_GAIN, .name = NULL},
986 	},
987 
988 	.matrix_mux_init = {
989 		12, 13, 14, 15,                 /* Analog -> 1..4 */
990 		16, 17,                          /* SPDIF -> 5,6 */
991 		0, 1, 2, 3, 4, 5, 6, 7,     /* PCM[1..12] -> 7..18 */
992 		8, 9, 10, 11
993 	}
994 };
995 
996 /*  untested...  */
997 static const struct scarlett_device_info s8i6_info = {
998 	.matrix_in = 18,
999 	.matrix_out = 6,
1000 	.input_len = 8,
1001 	.output_len = 6,
1002 	.has_output_source_routing = true,
1003 
1004 	.opt_master = {
1005 		.start = -1,
1006 		.len = 25,
1007 		.offsets = {0, 12, 16, 18, 18},
1008 		.names = NULL
1009 	},
1010 
1011 	.opt_matrix = {
1012 		.start = -1,
1013 		.len = 19,
1014 		.offsets = {0, 12, 16, 18, 18},
1015 		.names = NULL
1016 	},
1017 
1018 	.num_controls = 7,
1019 	.controls = {
1020 		{ .num = 0, .type = SCARLETT_OUTPUTS, .name = "Monitor" },
1021 		{ .num = 1, .type = SCARLETT_OUTPUTS, .name = "Headphone" },
1022 		{ .num = 2, .type = SCARLETT_OUTPUTS, .name = "SPDIF" },
1023 		{ .num = 1, .type = SCARLETT_SWITCH_IMPEDANCE, .name = NULL},
1024 		{ .num = 2, .type = SCARLETT_SWITCH_IMPEDANCE, .name = NULL},
1025 		{ .num = 3, .type = SCARLETT_SWITCH_PAD, .name = NULL},
1026 		{ .num = 4, .type = SCARLETT_SWITCH_PAD, .name = NULL},
1027 	},
1028 
1029 	.matrix_mux_init = {
1030 		12, 13, 14, 15,                 /* Analog -> 1..4 */
1031 		16, 17,                          /* SPDIF -> 5,6 */
1032 		0, 1, 2, 3, 4, 5, 6, 7,     /* PCM[1..12] -> 7..18 */
1033 		8, 9, 10, 11
1034 	}
1035 };
1036 
1037 static const struct scarlett_device_info s18i6_info = {
1038 	.matrix_in = 18,
1039 	.matrix_out = 6,
1040 	.input_len = 18,
1041 	.output_len = 6,
1042 	.has_output_source_routing = true,
1043 
1044 	.opt_master = {
1045 		.start = -1,
1046 		.len = 31,
1047 		.offsets = {0, 6, 14, 16, 24},
1048 		.names = NULL,
1049 	},
1050 
1051 	.opt_matrix = {
1052 		.start = -1,
1053 		.len = 25,
1054 		.offsets = {0, 6, 14, 16, 24},
1055 		.names = NULL,
1056 	},
1057 
1058 	.num_controls = 5,
1059 	.controls = {
1060 		{ .num = 0, .type = SCARLETT_OUTPUTS, .name = "Monitor" },
1061 		{ .num = 1, .type = SCARLETT_OUTPUTS, .name = "Headphone" },
1062 		{ .num = 2, .type = SCARLETT_OUTPUTS, .name = "SPDIF" },
1063 		{ .num = 1, .type = SCARLETT_SWITCH_IMPEDANCE, .name = NULL},
1064 		{ .num = 2, .type = SCARLETT_SWITCH_IMPEDANCE, .name = NULL},
1065 	},
1066 
1067 	.matrix_mux_init = {
1068 		 6,  7,  8,  9, 10, 11, 12, 13, /* Analog -> 1..8 */
1069 		16, 17, 18, 19, 20, 21,     /* ADAT[1..6] -> 9..14 */
1070 		14, 15,                          /* SPDIF -> 15,16 */
1071 		0, 1                          /* PCM[1,2] -> 17,18 */
1072 	}
1073 };
1074 
1075 static const struct scarlett_device_info s18i8_info = {
1076 	.matrix_in = 18,
1077 	.matrix_out = 8,
1078 	.input_len = 18,
1079 	.output_len = 8,
1080 	.has_output_source_routing = true,
1081 
1082 	.opt_master = {
1083 		.start = -1,
1084 		.len = 35,
1085 		.offsets = {0, 8, 16, 18, 26},
1086 		.names = NULL
1087 	},
1088 
1089 	.opt_matrix = {
1090 		.start = -1,
1091 		.len = 27,
1092 		.offsets = {0, 8, 16, 18, 26},
1093 		.names = NULL
1094 	},
1095 
1096 	.num_controls = 10,
1097 	.controls = {
1098 		{ .num = 0, .type = SCARLETT_OUTPUTS, .name = "Monitor" },
1099 		{ .num = 1, .type = SCARLETT_OUTPUTS, .name = "Headphone 1" },
1100 		{ .num = 2, .type = SCARLETT_OUTPUTS, .name = "Headphone 2" },
1101 		{ .num = 3, .type = SCARLETT_OUTPUTS, .name = "SPDIF" },
1102 		{ .num = 1, .type = SCARLETT_SWITCH_IMPEDANCE, .name = NULL},
1103 		{ .num = 1, .type = SCARLETT_SWITCH_PAD, .name = NULL},
1104 		{ .num = 2, .type = SCARLETT_SWITCH_IMPEDANCE, .name = NULL},
1105 		{ .num = 2, .type = SCARLETT_SWITCH_PAD, .name = NULL},
1106 		{ .num = 3, .type = SCARLETT_SWITCH_PAD, .name = NULL},
1107 		{ .num = 4, .type = SCARLETT_SWITCH_PAD, .name = NULL},
1108 	},
1109 
1110 	.matrix_mux_init = {
1111 		 8,  9, 10, 11, 12, 13, 14, 15, /* Analog -> 1..8 */
1112 		18, 19, 20, 21, 22, 23,     /* ADAT[1..6] -> 9..14 */
1113 		16, 17,                          /* SPDIF -> 15,16 */
1114 		0, 1                          /* PCM[1,2] -> 17,18 */
1115 	}
1116 };
1117 
1118 static const struct scarlett_device_info s18i20_info = {
1119 	.matrix_in = 18,
1120 	.matrix_out = 8,
1121 	.input_len = 18,
1122 	.output_len = 20,
1123 	.has_output_source_routing = true,
1124 
1125 	.opt_master = {
1126 		.start = -1,
1127 		.len = 47,
1128 		.offsets = {0, 20, 28, 30, 38},
1129 		.names = NULL
1130 	},
1131 
1132 	.opt_matrix = {
1133 		.start = -1,
1134 		.len = 39,
1135 		.offsets = {0, 20, 28, 30, 38},
1136 		.names = NULL
1137 	},
1138 
1139 	.num_controls = 10,
1140 	.controls = {
1141 		{ .num = 0, .type = SCARLETT_OUTPUTS, .name = "Monitor" },
1142 		{ .num = 1, .type = SCARLETT_OUTPUTS, .name = "Line 3/4" },
1143 		{ .num = 2, .type = SCARLETT_OUTPUTS, .name = "Line 5/6" },
1144 		{ .num = 3, .type = SCARLETT_OUTPUTS, .name = "Line 7/8" },
1145 		{ .num = 4, .type = SCARLETT_OUTPUTS, .name = "Line 9/10" },
1146 		{ .num = 5, .type = SCARLETT_OUTPUTS, .name = "SPDIF" },
1147 		{ .num = 6, .type = SCARLETT_OUTPUTS, .name = "ADAT 1/2" },
1148 		{ .num = 7, .type = SCARLETT_OUTPUTS, .name = "ADAT 3/4" },
1149 		{ .num = 8, .type = SCARLETT_OUTPUTS, .name = "ADAT 5/6" },
1150 		{ .num = 9, .type = SCARLETT_OUTPUTS, .name = "ADAT 7/8" },
1151 		/*{ .num = 1, .type = SCARLETT_SWITCH_IMPEDANCE, .name = NULL},
1152 		{ .num = 1, .type = SCARLETT_SWITCH_PAD, .name = NULL},
1153 		{ .num = 2, .type = SCARLETT_SWITCH_IMPEDANCE, .name = NULL},
1154 		{ .num = 2, .type = SCARLETT_SWITCH_PAD, .name = NULL},
1155 		{ .num = 3, .type = SCARLETT_SWITCH_PAD, .name = NULL},
1156 		{ .num = 4, .type = SCARLETT_SWITCH_PAD, .name = NULL},*/
1157 	},
1158 
1159 	.matrix_mux_init = {
1160 		20, 21, 22, 23, 24, 25, 26, 27, /* Analog -> 1..8 */
1161 		30, 31, 32, 33, 34, 35,     /* ADAT[1..6] -> 9..14 */
1162 		28, 29,                          /* SPDIF -> 15,16 */
1163 		0, 1                          /* PCM[1,2] -> 17,18 */
1164 	}
1165 };
1166 
1167 
scarlett_controls_create_generic(struct usb_mixer_interface * mixer,const struct scarlett_device_info * info)1168 static int scarlett_controls_create_generic(struct usb_mixer_interface *mixer,
1169 	const struct scarlett_device_info *info)
1170 {
1171 	int i, err;
1172 	char mx[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
1173 	const struct scarlett_mixer_control *ctl;
1174 	struct usb_mixer_elem_info *elem;
1175 
1176 	/* create master switch and playback volume */
1177 	err = add_new_ctl(mixer, &usb_scarlett_ctl_switch,
1178 			  scarlett_ctl_resume, 0x0a, 0x01, 0,
1179 			  USB_MIXER_S16, 1, "Master Playback Switch", NULL,
1180 			  &elem);
1181 	if (err < 0)
1182 		return err;
1183 
1184 	err = add_new_ctl(mixer, &usb_scarlett_ctl_master,
1185 			  scarlett_ctl_resume, 0x0a, 0x02, 0,
1186 			  USB_MIXER_S16, 1, "Master Playback Volume", NULL,
1187 			  &elem);
1188 	if (err < 0)
1189 		return err;
1190 
1191 	/* iterate through controls in info struct and create each one */
1192 	for (i = 0; i < info->num_controls; i++) {
1193 		ctl = &info->controls[i];
1194 
1195 		switch (ctl->type) {
1196 		case SCARLETT_OUTPUTS:
1197 			err = add_output_ctls(mixer, ctl->num, ctl->name, info);
1198 			if (err < 0)
1199 				return err;
1200 			break;
1201 		case SCARLETT_SWITCH_IMPEDANCE:
1202 			scnprintf(mx, sizeof(mx),
1203 				  "Input %d Impedance Switch", ctl->num);
1204 			err = add_new_ctl(mixer, &usb_scarlett_ctl_enum,
1205 					  scarlett_ctl_enum_resume, 0x01,
1206 					  0x09, ctl->num, USB_MIXER_S16, 1, mx,
1207 					  &opt_impedance, &elem);
1208 			if (err < 0)
1209 				return err;
1210 			break;
1211 		case SCARLETT_SWITCH_PAD:
1212 			scnprintf(mx, sizeof(mx),
1213 				  "Input %d Pad Switch", ctl->num);
1214 			err = add_new_ctl(mixer, &usb_scarlett_ctl_enum,
1215 					  scarlett_ctl_enum_resume, 0x01,
1216 					  0x0b, ctl->num, USB_MIXER_S16, 1, mx,
1217 					  &opt_pad, &elem);
1218 			if (err < 0)
1219 				return err;
1220 			break;
1221 		case SCARLETT_SWITCH_GAIN:
1222 			scnprintf(mx, sizeof(mx),
1223 				  "Input %d Gain Switch", ctl->num);
1224 			err = add_new_ctl(mixer, &usb_scarlett_ctl_enum,
1225 					  scarlett_ctl_enum_resume, 0x01,
1226 					  0x08, ctl->num, USB_MIXER_S16, 1, mx,
1227 					  &opt_gain, &elem);
1228 			if (err < 0)
1229 				return err;
1230 			break;
1231 		/* Forte input controls - use wIndex 0x3c per wiki docs */
1232 		case FORTE_INPUT_GAIN:
1233 			sprintf(mx, "Line In %d Gain Capture Volume", ctl->num);
1234 			err = add_new_ctl(mixer, &usb_forte_ctl_input_gain,
1235 					  forte_input_gain_resume, 0x3c,
1236 					  0x06, ctl->num - 1, USB_MIXER_S16, 1, mx,
1237 					  NULL, &elem);
1238 			if (err < 0)
1239 				return err;
1240 			break;
1241 		case FORTE_INPUT_SOURCE:
1242 			sprintf(mx, "Input %d Source", ctl->num);
1243 			err = add_new_ctl(mixer, &usb_forte_ctl_enum,
1244 					  forte_ctl_enum_resume, 0x3c,
1245 					  0x07, ctl->num - 1, USB_MIXER_S16, 1, mx,
1246 					  &opt_forte_source, &elem);
1247 			if (err < 0)
1248 				return err;
1249 			break;
1250 		case FORTE_INPUT_HPF:
1251 			sprintf(mx, "Input %d High Pass Filter Switch", ctl->num);
1252 			err = add_new_ctl(mixer, &usb_forte_ctl_switch,
1253 					  forte_ctl_switch_resume, 0x3c,
1254 					  0x08, ctl->num - 1, USB_MIXER_S16, 1, mx,
1255 					  NULL, &elem);
1256 			if (err < 0)
1257 				return err;
1258 			break;
1259 		case FORTE_INPUT_PHANTOM:
1260 			sprintf(mx, "Input %d 48V Phantom Power Switch", ctl->num);
1261 			err = add_new_ctl(mixer, &usb_forte_ctl_switch,
1262 					  forte_ctl_switch_resume, 0x3c,
1263 					  0x09, ctl->num - 1, USB_MIXER_S16, 1, mx,
1264 					  NULL, &elem);
1265 			if (err < 0)
1266 				return err;
1267 			break;
1268 		case FORTE_INPUT_PHASE:
1269 			sprintf(mx, "Input %d Phase Invert Switch", ctl->num);
1270 			err = add_new_ctl(mixer, &usb_forte_ctl_switch,
1271 					  forte_ctl_switch_resume, 0x3c,
1272 					  0x0a, ctl->num - 1, USB_MIXER_S16, 1, mx,
1273 					  NULL, &elem);
1274 			if (err < 0)
1275 				return err;
1276 			break;
1277 		case FORTE_INPUT_PAD:
1278 			sprintf(mx, "Input %d Pad Switch", ctl->num);
1279 			err = add_new_ctl(mixer, &usb_forte_ctl_switch,
1280 					  forte_ctl_switch_resume, 0x3c,
1281 					  0x0b, ctl->num - 1, USB_MIXER_S16, 1, mx,
1282 					  NULL, &elem);
1283 			if (err < 0)
1284 				return err;
1285 			break;
1286 		}
1287 	}
1288 
1289 	return 0;
1290 }
1291 
1292 /*
1293  * Create and initialize a mixer for the Focusrite(R) Scarlett
1294  */
snd_scarlett_controls_create(struct usb_mixer_interface * mixer)1295 int snd_scarlett_controls_create(struct usb_mixer_interface *mixer)
1296 {
1297 	int err, i, o;
1298 	char mx[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
1299 	const struct scarlett_device_info *info;
1300 	struct usb_mixer_elem_info *elem;
1301 	static char sample_rate_buffer[4] = { '\x80', '\xbb', '\x00', '\x00' };
1302 
1303 	/* only use UAC_VERSION_2 */
1304 	if (!mixer->protocol)
1305 		return 0;
1306 
1307 	switch (mixer->chip->usb_id) {
1308 	case USB_ID(0x1235, 0x8012):
1309 		info = &s6i6_info;
1310 		break;
1311 	case USB_ID(0x1235, 0x8002):
1312 		info = &s8i6_info;
1313 		break;
1314 	case USB_ID(0x1235, 0x8004):
1315 		info = &s18i6_info;
1316 		break;
1317 	case USB_ID(0x1235, 0x8014):
1318 		info = &s18i8_info;
1319 		break;
1320 	case USB_ID(0x1235, 0x800c):
1321 		info = &s18i20_info;
1322 		break;
1323 	default: /* device not (yet) supported */
1324 		return -EINVAL;
1325 	}
1326 
1327 	/* generic function to create controls */
1328 	err = scarlett_controls_create_generic(mixer, info);
1329 	if (err < 0)
1330 		return err;
1331 
1332 	/* setup matrix controls */
1333 	for (i = 0; i < info->matrix_in; i++) {
1334 		snprintf(mx, sizeof(mx), "Matrix %02d Input Playback Route",
1335 			 i+1);
1336 		err = add_new_ctl(mixer, &usb_scarlett_ctl_dynamic_enum,
1337 				  scarlett_ctl_enum_resume, 0x32,
1338 				  0x06, i, USB_MIXER_S16, 1, mx,
1339 				  &info->opt_matrix, &elem);
1340 		if (err < 0)
1341 			return err;
1342 
1343 		for (o = 0; o < info->matrix_out; o++) {
1344 			scnprintf(mx, sizeof(mx),
1345 				  "Matrix %02d Mix %c Playback Volume", i+1,
1346 				  o+'A');
1347 			err = add_new_ctl(mixer, &usb_scarlett_ctl,
1348 					  scarlett_ctl_resume, 0x3c, 0x00,
1349 					  (i << 3) + (o & 0x07), USB_MIXER_S16,
1350 					  1, mx, NULL, &elem);
1351 			if (err < 0)
1352 				return err;
1353 
1354 		}
1355 	}
1356 
1357 	for (i = 0; i < info->input_len; i++) {
1358 		snprintf(mx, sizeof(mx), "Input Source %02d Capture Route",
1359 			 i+1);
1360 		err = add_new_ctl(mixer, &usb_scarlett_ctl_dynamic_enum,
1361 				  scarlett_ctl_enum_resume, 0x34,
1362 				  0x00, i, USB_MIXER_S16, 1, mx,
1363 				  &info->opt_master, &elem);
1364 		if (err < 0)
1365 			return err;
1366 	}
1367 
1368 	/* val_len == 1 needed here */
1369 	err = add_new_ctl(mixer, &usb_scarlett_ctl_enum,
1370 			  scarlett_ctl_enum_resume, 0x28, 0x01, 0,
1371 			  USB_MIXER_U8, 1, "Sample Clock Source",
1372 			  &opt_clock, &elem);
1373 	if (err < 0)
1374 		return err;
1375 
1376 	/* val_len == 1 and UAC2_CS_MEM */
1377 	err = add_new_ctl(mixer, &usb_scarlett_ctl_sync, NULL, 0x3c, 0x00, 2,
1378 			  USB_MIXER_U8, 1, "Sample Clock Sync Status",
1379 			  &opt_sync, &elem);
1380 	if (err < 0)
1381 		return err;
1382 
1383 	/* initialize sampling rate to 48000 */
1384 	err = snd_usb_ctl_msg(mixer->chip->dev,
1385 		usb_sndctrlpipe(mixer->chip->dev, 0), UAC2_CS_CUR,
1386 		USB_RECIP_INTERFACE | USB_TYPE_CLASS |
1387 		USB_DIR_OUT, 0x0100, snd_usb_ctrl_intf(mixer->hostif) |
1388 		(0x29 << 8), sample_rate_buffer, 4);
1389 	if (err < 0)
1390 		return err;
1391 
1392 	return err;
1393 }
1394 
1395 /*
1396  * Create and initialize a mixer for the Focusrite(R) Forte
1397  */
snd_forte_controls_create(struct usb_mixer_interface * mixer)1398 int snd_forte_controls_create(struct usb_mixer_interface *mixer)
1399 {
1400 	int err, i, o;
1401 	char mx[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
1402 	const struct scarlett_device_info *info;
1403 	struct usb_mixer_elem_info *elem;
1404 
1405 	/* only use UAC_VERSION_2 */
1406 	if (!mixer->protocol)
1407 		return 0;
1408 
1409 	switch (mixer->chip->usb_id) {
1410 	case USB_ID(0x1235, 0x8010):
1411 		info = &forte_info;
1412 		break;
1413 	default: /* device not (yet) supported */
1414 		return -EINVAL;
1415 	}
1416 
1417 	/* generic function to create controls */
1418 	err = scarlett_controls_create_generic(mixer, info);
1419 	if (err < 0)
1420 		return err;
1421 
1422 	/* setup matrix controls */
1423 	for (i = 0; i < info->matrix_in; i++) {
1424 		snprintf(mx, sizeof(mx), "Matrix %02d Input Playback Route",
1425 			 i+1);
1426 		err = add_new_ctl(mixer, &usb_scarlett_ctl_dynamic_enum,
1427 				  scarlett_ctl_enum_resume, 0x32,
1428 				  0x06, i, USB_MIXER_S16, 1, mx,
1429 				  &info->opt_matrix, &elem);
1430 		if (err < 0)
1431 			return err;
1432 
1433 		for (o = 0; o < info->matrix_out; o++) {
1434 			sprintf(mx, "Matrix %02d Mix %c Playback Volume", i+1,
1435 				o+'A');
1436 			err = add_new_ctl(mixer, &usb_scarlett_ctl,
1437 					  scarlett_ctl_resume, 0x3c, 0x01,
1438 					  (i << 2) + (o & 0x03), USB_MIXER_S16,
1439 					  1, mx, NULL, &elem);
1440 			if (err < 0)
1441 				return err;
1442 
1443 		}
1444 	}
1445 
1446 	/* val_len == 1 and UAC2_CS_MEM */
1447 	err = add_new_ctl(mixer, &usb_scarlett_ctl_sync, NULL, 0x3c, 0x00, 2,
1448 			  USB_MIXER_U8, 1, "Sample Clock Sync Status",
1449 			  &opt_sync, &elem);
1450 	if (err < 0)
1451 		return err;
1452 
1453 	return err;
1454 }
1455