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