1// SPDX-License-Identifier: GPL-2.0
2/*
3 *   Focusrite Scarlett 6i6/18i8/18i20 Gen 2 Driver for ALSA
4 *
5 *   Copyright (c) 2018-2019 by Geoffrey D. Bennett <g at b4.vu>
6 *
7 *   Based on the Scarlett (Gen 1) Driver for ALSA:
8 *
9 *   Copyright (c) 2013 by Tobias Hoffmann
10 *   Copyright (c) 2013 by Robin Gareus <robin at gareus.org>
11 *   Copyright (c) 2002 by Takashi Iwai <tiwai at suse.de>
12 *   Copyright (c) 2014 by Chris J Arges <chris.j.arges at canonical.com>
13 *
14 *   Many codes borrowed from audio.c by
15 *     Alan Cox (alan at lxorguk.ukuu.org.uk)
16 *     Thomas Sailer (sailer at ife.ee.ethz.ch)
17 *
18 *   Code cleanup:
19 *   David Henningsson <david.henningsson at canonical.com>
20 */
21
22/* Mixer Interface for the Focusrite Scarlett 6i6/18i8/18i20 Gen 2 audio
23 * interface. Based on the Gen 1 driver and rewritten.
24 */
25
26/* The protocol was reverse engineered by looking at the communication
27 * between Focusrite Control 2.3.4 and the Focusrite(R) Scarlett 18i20
28 * (firmware 1083) using usbmon in July-August 2018.
29 *
30 * Scarlett 18i8 support added in April 2019.
31 *
32 * Scarlett 6i6 support added in June 2019 (thanks to Martin Wittmann
33 * for providing usbmon output and testing).
34 *
35 * This ALSA mixer gives access to:
36 *  - input, output, mixer-matrix muxes
37 *  - 18x10 mixer-matrix gain stages
38 *  - gain/volume controls
39 *  - level meters
40 *  - line/inst level and pad controls
41 *
42 * <ditaa>
43 *    /--------------\    18chn            20chn     /--------------\
44 *    | Hardware  in +--+------\    /-------------+--+ ALSA PCM out |
45 *    \--------------/  |      |    |             |  \--------------/
46 *                      |      |    |    /-----\  |
47 *                      |      |    |    |     |  |
48 *                      |      v    v    v     |  |
49 *                      |   +---------------+  |  |
50 *                      |    \ Matrix  Mux /   |  |
51 *                      |     +-----+-----+    |  |
52 *                      |           |          |  |
53 *                      |           |18chn     |  |
54 *                      |           |          |  |
55 *                      |           |     10chn|  |
56 *                      |           v          |  |
57 *                      |     +------------+   |  |
58 *                      |     | Mixer      |   |  |
59 *                      |     |     Matrix |   |  |
60 *                      |     |            |   |  |
61 *                      |     | 18x10 Gain |   |  |
62 *                      |     |   stages   |   |  |
63 *                      |     +-----+------+   |  |
64 *                      |           |          |  |
65 *                      |18chn      |10chn     |  |20chn
66 *                      |           |          |  |
67 *                      |           +----------/  |
68 *                      |           |             |
69 *                      v           v             v
70 *                      ===========================
71 *               +---------------+       +--—------------+
72 *                \ Output  Mux /         \ Capture Mux /
73 *                 +---+---+---+           +-----+-----+
74 *                     |   |                     |
75 *                10chn|   |                     |18chn
76 *                     |   |                     |
77 *  /--------------\   |   |                     |   /--------------\
78 *  | S/PDIF, ADAT |<--/   |10chn                \-->| ALSA PCM in  |
79 *  | Hardware out |       |                         \--------------/
80 *  \--------------/       |
81 *                         v
82 *                  +-------------+    Software gain per channel.
83 *                  | Master Gain |<-- 18i20 only: Switch per channel
84 *                  +------+------+    to select HW or SW gain control.
85 *                         |
86 *                         |10chn
87 *  /--------------\       |
88 *  | Analogue     |<------/
89 *  | Hardware out |
90 *  \--------------/
91 * </ditaa>
92 *
93 */
94
95#include <linux/slab.h>
96#include <linux/usb.h>
97#include <linux/moduleparam.h>
98
99#include <sound/control.h>
100#include <sound/tlv.h>
101
102#include "usbaudio.h"
103#include "mixer.h"
104#include "helper.h"
105
106#include "mixer_scarlett_gen2.h"
107
108/* device_setup value to enable */
109#define SCARLETT2_ENABLE 0x01
110
111/* some gui mixers can't handle negative ctl values */
112#define SCARLETT2_VOLUME_BIAS 127
113
114/* mixer range from -80dB to +6dB in 0.5dB steps */
115#define SCARLETT2_MIXER_MIN_DB -80
116#define SCARLETT2_MIXER_BIAS (-SCARLETT2_MIXER_MIN_DB * 2)
117#define SCARLETT2_MIXER_MAX_DB 6
118#define SCARLETT2_MIXER_MAX_VALUE \
119	((SCARLETT2_MIXER_MAX_DB - SCARLETT2_MIXER_MIN_DB) * 2)
120
121/* map from (dB + 80) * 2 to mixer value
122 * for dB in 0 .. 172: int(8192 * pow(10, ((dB - 160) / 2 / 20)))
123 */
124static const u16 scarlett2_mixer_values[173] = {
125	0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2,
126	2, 2, 3, 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 6, 6, 6, 7, 7, 8, 8,
127	9, 9, 10, 10, 11, 12, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21,
128	23, 24, 25, 27, 29, 30, 32, 34, 36, 38, 41, 43, 46, 48, 51,
129	54, 57, 61, 65, 68, 73, 77, 81, 86, 91, 97, 103, 109, 115,
130	122, 129, 137, 145, 154, 163, 173, 183, 194, 205, 217, 230,
131	244, 259, 274, 290, 307, 326, 345, 365, 387, 410, 434, 460,
132	487, 516, 547, 579, 614, 650, 689, 730, 773, 819, 867, 919,
133	973, 1031, 1092, 1157, 1225, 1298, 1375, 1456, 1543, 1634,
134	1731, 1833, 1942, 2057, 2179, 2308, 2445, 2590, 2744, 2906,
135	3078, 3261, 3454, 3659, 3876, 4105, 4349, 4606, 4879, 5168,
136	5475, 5799, 6143, 6507, 6892, 7301, 7733, 8192, 8677, 9191,
137	9736, 10313, 10924, 11571, 12257, 12983, 13752, 14567, 15430,
138	16345
139};
140
141/* Maximum number of analogue outputs */
142#define SCARLETT2_ANALOGUE_MAX 10
143
144/* Maximum number of level and pad switches */
145#define SCARLETT2_LEVEL_SWITCH_MAX 2
146#define SCARLETT2_PAD_SWITCH_MAX 4
147
148/* Maximum number of inputs to the mixer */
149#define SCARLETT2_INPUT_MIX_MAX 18
150
151/* Maximum number of outputs from the mixer */
152#define SCARLETT2_OUTPUT_MIX_MAX 10
153
154/* Maximum size of the data in the USB mux assignment message:
155 * 18 inputs, 20 outputs, 18 matrix inputs, 8 spare
156 */
157#define SCARLETT2_MUX_MAX 64
158
159/* Number of meters:
160 * 18 inputs, 20 outputs, 18 matrix inputs
161 */
162#define SCARLETT2_NUM_METERS 56
163
164/* Hardware port types:
165 * - None (no input to mux)
166 * - Analogue I/O
167 * - S/PDIF I/O
168 * - ADAT I/O
169 * - Mixer I/O
170 * - PCM I/O
171 */
172enum {
173	SCARLETT2_PORT_TYPE_NONE = 0,
174	SCARLETT2_PORT_TYPE_ANALOGUE = 1,
175	SCARLETT2_PORT_TYPE_SPDIF = 2,
176	SCARLETT2_PORT_TYPE_ADAT = 3,
177	SCARLETT2_PORT_TYPE_MIX = 4,
178	SCARLETT2_PORT_TYPE_PCM = 5,
179	SCARLETT2_PORT_TYPE_COUNT = 6,
180};
181
182/* Count of total I/O and number available at each sample rate */
183enum {
184	SCARLETT2_PORT_IN = 0,
185	SCARLETT2_PORT_OUT = 1,
186	SCARLETT2_PORT_OUT_44 = 2,
187	SCARLETT2_PORT_OUT_88 = 3,
188	SCARLETT2_PORT_OUT_176 = 4,
189	SCARLETT2_PORT_DIRECTIONS = 5,
190};
191
192/* Hardware buttons on the 18i20 */
193#define SCARLETT2_BUTTON_MAX 2
194
195static const char *const scarlett2_button_names[SCARLETT2_BUTTON_MAX] = {
196	"Mute", "Dim"
197};
198
199/* Description of each hardware port type:
200 * - id: hardware ID for this port type
201 * - num: number of sources/destinations of this port type
202 * - src_descr: printf format string for mux input selections
203 * - src_num_offset: added to channel number for the fprintf
204 * - dst_descr: printf format string for mixer controls
205 */
206struct scarlett2_ports {
207	u16 id;
208	int num[SCARLETT2_PORT_DIRECTIONS];
209	const char * const src_descr;
210	int src_num_offset;
211	const char * const dst_descr;
212};
213
214struct scarlett2_device_info {
215	u8 line_out_hw_vol; /* line out hw volume is sw controlled */
216	u8 button_count; /* number of buttons */
217	u8 level_input_count; /* inputs with level selectable */
218	u8 pad_input_count; /* inputs with pad selectable */
219	const char * const line_out_descrs[SCARLETT2_ANALOGUE_MAX];
220	struct scarlett2_ports ports[SCARLETT2_PORT_TYPE_COUNT];
221};
222
223struct scarlett2_mixer_data {
224	struct usb_mixer_interface *mixer;
225	struct mutex usb_mutex; /* prevent sending concurrent USB requests */
226	struct mutex data_mutex; /* lock access to this data */
227	struct delayed_work work;
228	const struct scarlett2_device_info *info;
229	int num_mux_srcs;
230	u16 scarlett2_seq;
231	u8 vol_updated;
232	u8 master_vol;
233	u8 vol[SCARLETT2_ANALOGUE_MAX];
234	u8 vol_sw_hw_switch[SCARLETT2_ANALOGUE_MAX];
235	u8 level_switch[SCARLETT2_LEVEL_SWITCH_MAX];
236	u8 pad_switch[SCARLETT2_PAD_SWITCH_MAX];
237	u8 buttons[SCARLETT2_BUTTON_MAX];
238	struct snd_kcontrol *master_vol_ctl;
239	struct snd_kcontrol *vol_ctls[SCARLETT2_ANALOGUE_MAX];
240	struct snd_kcontrol *button_ctls[SCARLETT2_BUTTON_MAX];
241	u8 mux[SCARLETT2_MUX_MAX];
242	u8 mix[SCARLETT2_INPUT_MIX_MAX * SCARLETT2_OUTPUT_MIX_MAX];
243};
244
245/*** Model-specific data ***/
246
247static const struct scarlett2_device_info s6i6_gen2_info = {
248	/* The first two analogue inputs can be switched between line
249	 * and instrument levels.
250	 */
251	.level_input_count = 2,
252
253	/* The first two analogue inputs have an optional pad. */
254	.pad_input_count = 2,
255
256	.line_out_descrs = {
257		"Headphones 1 L",
258		"Headphones 1 R",
259		"Headphones 2 L",
260		"Headphones 2 R",
261	},
262
263	.ports = {
264		[SCARLETT2_PORT_TYPE_NONE] = {
265			.id = 0x000,
266			.num = { 1, 0, 8, 8, 8 },
267			.src_descr = "Off",
268			.src_num_offset = 0,
269		},
270		[SCARLETT2_PORT_TYPE_ANALOGUE] = {
271			.id = 0x080,
272			.num = { 4, 4, 4, 4, 4 },
273			.src_descr = "Analogue %d",
274			.src_num_offset = 1,
275			.dst_descr = "Analogue Output %02d Playback"
276		},
277		[SCARLETT2_PORT_TYPE_SPDIF] = {
278			.id = 0x180,
279			.num = { 2, 2, 2, 2, 2 },
280			.src_descr = "S/PDIF %d",
281			.src_num_offset = 1,
282			.dst_descr = "S/PDIF Output %d Playback"
283		},
284		[SCARLETT2_PORT_TYPE_MIX] = {
285			.id = 0x300,
286			.num = { 10, 18, 18, 18, 18 },
287			.src_descr = "Mix %c",
288			.src_num_offset = 65,
289			.dst_descr = "Mixer Input %02d Capture"
290		},
291		[SCARLETT2_PORT_TYPE_PCM] = {
292			.id = 0x600,
293			.num = { 6, 6, 6, 6, 6 },
294			.src_descr = "PCM %d",
295			.src_num_offset = 1,
296			.dst_descr = "PCM %02d Capture"
297		},
298	},
299};
300
301static const struct scarlett2_device_info s18i8_gen2_info = {
302	/* The first two analogue inputs can be switched between line
303	 * and instrument levels.
304	 */
305	.level_input_count = 2,
306
307	/* The first four analogue inputs have an optional pad. */
308	.pad_input_count = 4,
309
310	.line_out_descrs = {
311		"Monitor L",
312		"Monitor R",
313		"Headphones 1 L",
314		"Headphones 1 R",
315		"Headphones 2 L",
316		"Headphones 2 R",
317	},
318
319	.ports = {
320		[SCARLETT2_PORT_TYPE_NONE] = {
321			.id = 0x000,
322			.num = { 1, 0, 8, 8, 4 },
323			.src_descr = "Off",
324			.src_num_offset = 0,
325		},
326		[SCARLETT2_PORT_TYPE_ANALOGUE] = {
327			.id = 0x080,
328			.num = { 8, 6, 6, 6, 6 },
329			.src_descr = "Analogue %d",
330			.src_num_offset = 1,
331			.dst_descr = "Analogue Output %02d Playback"
332		},
333		[SCARLETT2_PORT_TYPE_SPDIF] = {
334			.id = 0x180,
335			/* S/PDIF outputs aren't available at 192kHz
336			 * but are included in the USB mux I/O
337			 * assignment message anyway
338			 */
339			.num = { 2, 2, 2, 2, 2 },
340			.src_descr = "S/PDIF %d",
341			.src_num_offset = 1,
342			.dst_descr = "S/PDIF Output %d Playback"
343		},
344		[SCARLETT2_PORT_TYPE_ADAT] = {
345			.id = 0x200,
346			.num = { 8, 0, 0, 0, 0 },
347			.src_descr = "ADAT %d",
348			.src_num_offset = 1,
349		},
350		[SCARLETT2_PORT_TYPE_MIX] = {
351			.id = 0x300,
352			.num = { 10, 18, 18, 18, 18 },
353			.src_descr = "Mix %c",
354			.src_num_offset = 65,
355			.dst_descr = "Mixer Input %02d Capture"
356		},
357		[SCARLETT2_PORT_TYPE_PCM] = {
358			.id = 0x600,
359			.num = { 8, 18, 18, 14, 10 },
360			.src_descr = "PCM %d",
361			.src_num_offset = 1,
362			.dst_descr = "PCM %02d Capture"
363		},
364	},
365};
366
367static const struct scarlett2_device_info s18i20_gen2_info = {
368	/* The analogue line outputs on the 18i20 can be switched
369	 * between software and hardware volume control
370	 */
371	.line_out_hw_vol = 1,
372
373	/* Mute and dim buttons */
374	.button_count = 2,
375
376	.line_out_descrs = {
377		"Monitor L",
378		"Monitor R",
379		NULL,
380		NULL,
381		NULL,
382		NULL,
383		"Headphones 1 L",
384		"Headphones 1 R",
385		"Headphones 2 L",
386		"Headphones 2 R",
387	},
388
389	.ports = {
390		[SCARLETT2_PORT_TYPE_NONE] = {
391			.id = 0x000,
392			.num = { 1, 0, 8, 8, 6 },
393			.src_descr = "Off",
394			.src_num_offset = 0,
395		},
396		[SCARLETT2_PORT_TYPE_ANALOGUE] = {
397			.id = 0x080,
398			.num = { 8, 10, 10, 10, 10 },
399			.src_descr = "Analogue %d",
400			.src_num_offset = 1,
401			.dst_descr = "Analogue Output %02d Playback"
402		},
403		[SCARLETT2_PORT_TYPE_SPDIF] = {
404			/* S/PDIF outputs aren't available at 192kHz
405			 * but are included in the USB mux I/O
406			 * assignment message anyway
407			 */
408			.id = 0x180,
409			.num = { 2, 2, 2, 2, 2 },
410			.src_descr = "S/PDIF %d",
411			.src_num_offset = 1,
412			.dst_descr = "S/PDIF Output %d Playback"
413		},
414		[SCARLETT2_PORT_TYPE_ADAT] = {
415			.id = 0x200,
416			.num = { 8, 8, 8, 4, 0 },
417			.src_descr = "ADAT %d",
418			.src_num_offset = 1,
419			.dst_descr = "ADAT Output %d Playback"
420		},
421		[SCARLETT2_PORT_TYPE_MIX] = {
422			.id = 0x300,
423			.num = { 10, 18, 18, 18, 18 },
424			.src_descr = "Mix %c",
425			.src_num_offset = 65,
426			.dst_descr = "Mixer Input %02d Capture"
427		},
428		[SCARLETT2_PORT_TYPE_PCM] = {
429			.id = 0x600,
430			.num = { 20, 18, 18, 14, 10 },
431			.src_descr = "PCM %d",
432			.src_num_offset = 1,
433			.dst_descr = "PCM %02d Capture"
434		},
435	},
436};
437
438/* get the starting port index number for a given port type/direction */
439static int scarlett2_get_port_start_num(const struct scarlett2_ports *ports,
440					int direction, int port_type)
441{
442	int i, num = 0;
443
444	for (i = 0; i < port_type; i++)
445		num += ports[i].num[direction];
446
447	return num;
448}
449
450/*** USB Interactions ***/
451
452/* Vendor-Specific Interface, Endpoint, MaxPacketSize, Interval */
453#define SCARLETT2_USB_VENDOR_SPECIFIC_INTERFACE 5
454#define SCARLETT2_USB_INTERRUPT_ENDPOINT 4
455#define SCARLETT2_USB_INTERRUPT_MAX_DATA 64
456#define SCARLETT2_USB_INTERRUPT_INTERVAL 3
457
458/* Interrupt flags for volume and mute/dim button changes */
459#define SCARLETT2_USB_INTERRUPT_VOL_CHANGE 0x400000
460#define SCARLETT2_USB_INTERRUPT_BUTTON_CHANGE 0x200000
461
462/* Commands for sending/receiving requests/responses */
463#define SCARLETT2_USB_VENDOR_SPECIFIC_CMD_REQ 2
464#define SCARLETT2_USB_VENDOR_SPECIFIC_CMD_RESP 3
465
466#define SCARLETT2_USB_INIT_SEQ 0x00000000
467#define SCARLETT2_USB_GET_METER_LEVELS 0x00001001
468#define SCARLETT2_USB_SET_MIX 0x00002002
469#define SCARLETT2_USB_SET_MUX 0x00003002
470#define SCARLETT2_USB_GET_DATA 0x00800000
471#define SCARLETT2_USB_SET_DATA 0x00800001
472#define SCARLETT2_USB_DATA_CMD 0x00800002
473#define SCARLETT2_USB_CONFIG_SAVE 6
474
475#define SCARLETT2_USB_VOLUME_STATUS_OFFSET 0x31
476#define SCARLETT2_USB_METER_LEVELS_GET_MAGIC 1
477
478/* volume status is read together (matches scarlett2_config_items[]) */
479struct scarlett2_usb_volume_status {
480	/* mute & dim buttons */
481	u8 buttons[SCARLETT2_BUTTON_MAX];
482
483	u8 pad1;
484
485	/* software volume setting */
486	s16 sw_vol[SCARLETT2_ANALOGUE_MAX];
487
488	/* actual volume of output inc. dim (-18dB) */
489	s16 hw_vol[SCARLETT2_ANALOGUE_MAX];
490
491	u8 pad2[SCARLETT2_ANALOGUE_MAX];
492
493	/* sw (0) or hw (1) controlled */
494	u8 sw_hw_switch[SCARLETT2_ANALOGUE_MAX];
495
496	u8 pad3[6];
497
498	/* front panel volume knob */
499	s16 master_vol;
500} __packed;
501
502/* Configuration parameters that can be read and written */
503enum {
504	SCARLETT2_CONFIG_BUTTONS = 0,
505	SCARLETT2_CONFIG_LINE_OUT_VOLUME = 1,
506	SCARLETT2_CONFIG_SW_HW_SWITCH = 2,
507	SCARLETT2_CONFIG_LEVEL_SWITCH = 3,
508	SCARLETT2_CONFIG_PAD_SWITCH = 4,
509	SCARLETT2_CONFIG_COUNT = 5
510};
511
512/* Location, size, and activation command number for the configuration
513 * parameters
514 */
515struct scarlett2_config {
516	u8 offset;
517	u8 size;
518	u8 activate;
519};
520
521static const struct scarlett2_config
522		scarlett2_config_items[SCARLETT2_CONFIG_COUNT] = {
523	/* Mute/Dim Buttons */
524	{
525		.offset = 0x31,
526		.size = 1,
527		.activate = 2
528	},
529
530	/* Line Out Volume */
531	{
532		.offset = 0x34,
533		.size = 2,
534		.activate = 1
535	},
536
537	/* SW/HW Volume Switch */
538	{
539		.offset = 0x66,
540		.size = 1,
541		.activate = 3
542	},
543
544	/* Level Switch */
545	{
546		.offset = 0x7c,
547		.size = 1,
548		.activate = 7
549	},
550
551	/* Pad Switch */
552	{
553		.offset = 0x84,
554		.size = 1,
555		.activate = 8
556	}
557};
558
559/* proprietary request/response format */
560struct scarlett2_usb_packet {
561	__le32 cmd;
562	__le16 size;
563	__le16 seq;
564	__le32 error;
565	__le32 pad;
566	u8 data[];
567};
568
569#define SCARLETT2_USB_PACKET_LEN (sizeof(struct scarlett2_usb_packet))
570
571static void scarlett2_fill_request_header(struct scarlett2_mixer_data *private,
572					  struct scarlett2_usb_packet *req,
573					  u32 cmd, u16 req_size)
574{
575	/* sequence must go up by 1 for each request */
576	u16 seq = private->scarlett2_seq++;
577
578	req->cmd = cpu_to_le32(cmd);
579	req->size = cpu_to_le16(req_size);
580	req->seq = cpu_to_le16(seq);
581	req->error = 0;
582	req->pad = 0;
583}
584
585/* Send a proprietary format request to the Scarlett interface */
586static int scarlett2_usb(
587	struct usb_mixer_interface *mixer, u32 cmd,
588	void *req_data, u16 req_size, void *resp_data, u16 resp_size)
589{
590	struct scarlett2_mixer_data *private = mixer->private_data;
591	u16 req_buf_size = sizeof(struct scarlett2_usb_packet) + req_size;
592	u16 resp_buf_size = sizeof(struct scarlett2_usb_packet) + resp_size;
593	struct scarlett2_usb_packet *req = NULL, *resp = NULL;
594	int err = 0;
595
596	req = kmalloc(req_buf_size, GFP_KERNEL);
597	if (!req) {
598		err = -ENOMEM;
599		goto error;
600	}
601
602	resp = kmalloc(resp_buf_size, GFP_KERNEL);
603	if (!resp) {
604		err = -ENOMEM;
605		goto error;
606	}
607
608	mutex_lock(&private->usb_mutex);
609
610	/* build request message and send it */
611
612	scarlett2_fill_request_header(private, req, cmd, req_size);
613
614	if (req_size)
615		memcpy(req->data, req_data, req_size);
616
617	err = snd_usb_ctl_msg(mixer->chip->dev,
618			usb_sndctrlpipe(mixer->chip->dev, 0),
619			SCARLETT2_USB_VENDOR_SPECIFIC_CMD_REQ,
620			USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_OUT,
621			0,
622			SCARLETT2_USB_VENDOR_SPECIFIC_INTERFACE,
623			req,
624			req_buf_size);
625
626	if (err != req_buf_size) {
627		usb_audio_err(
628			mixer->chip,
629			"Scarlett Gen 2 USB request result cmd %x was %d\n",
630			cmd, err);
631		err = -EINVAL;
632		goto unlock;
633	}
634
635	/* send a second message to get the response */
636
637	err = snd_usb_ctl_msg(mixer->chip->dev,
638			usb_rcvctrlpipe(mixer->chip->dev, 0),
639			SCARLETT2_USB_VENDOR_SPECIFIC_CMD_RESP,
640			USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_IN,
641			0,
642			SCARLETT2_USB_VENDOR_SPECIFIC_INTERFACE,
643			resp,
644			resp_buf_size);
645
646	/* validate the response */
647
648	if (err != resp_buf_size) {
649		usb_audio_err(
650			mixer->chip,
651			"Scarlett Gen 2 USB response result cmd %x was %d\n",
652			cmd, err);
653		err = -EINVAL;
654		goto unlock;
655	}
656
657	if (resp->cmd != req->cmd ||
658	    resp->seq != req->seq ||
659	    resp_size != le16_to_cpu(resp->size) ||
660	    resp->error ||
661	    resp->pad) {
662		usb_audio_err(
663			mixer->chip,
664			"Scarlett Gen 2 USB invalid response; "
665			   "cmd tx/rx %d/%d seq %d/%d size %d/%d "
666			   "error %d pad %d\n",
667			le32_to_cpu(req->cmd), le32_to_cpu(resp->cmd),
668			le16_to_cpu(req->seq), le16_to_cpu(resp->seq),
669			resp_size, le16_to_cpu(resp->size),
670			le32_to_cpu(resp->error),
671			le32_to_cpu(resp->pad));
672		err = -EINVAL;
673		goto unlock;
674	}
675
676	if (resp_size > 0)
677		memcpy(resp_data, resp->data, resp_size);
678
679unlock:
680	mutex_unlock(&private->usb_mutex);
681error:
682	kfree(req);
683	kfree(resp);
684	return err;
685}
686
687/* Send SCARLETT2_USB_DATA_CMD SCARLETT2_USB_CONFIG_SAVE */
688static void scarlett2_config_save(struct usb_mixer_interface *mixer)
689{
690	__le32 req = cpu_to_le32(SCARLETT2_USB_CONFIG_SAVE);
691
692	scarlett2_usb(mixer, SCARLETT2_USB_DATA_CMD,
693		      &req, sizeof(u32),
694		      NULL, 0);
695}
696
697/* Delayed work to save config */
698static void scarlett2_config_save_work(struct work_struct *work)
699{
700	struct scarlett2_mixer_data *private =
701		container_of(work, struct scarlett2_mixer_data, work.work);
702
703	scarlett2_config_save(private->mixer);
704}
705
706/* Send a USB message to set a configuration parameter (volume level,
707 * sw/hw volume switch, line/inst level switch, or pad switch)
708 */
709static int scarlett2_usb_set_config(
710	struct usb_mixer_interface *mixer,
711	int config_item_num, int index, int value)
712{
713	const struct scarlett2_config config_item =
714	       scarlett2_config_items[config_item_num];
715	struct {
716		__le32 offset;
717		__le32 bytes;
718		__le32 value;
719	} __packed req;
720	__le32 req2;
721	int err;
722	struct scarlett2_mixer_data *private = mixer->private_data;
723
724	/* Cancel any pending NVRAM save */
725	cancel_delayed_work_sync(&private->work);
726
727	/* Send the configuration parameter data */
728	req.offset = cpu_to_le32(config_item.offset + index * config_item.size);
729	req.bytes = cpu_to_le32(config_item.size);
730	req.value = cpu_to_le32(value);
731	err = scarlett2_usb(mixer, SCARLETT2_USB_SET_DATA,
732			    &req, sizeof(u32) * 2 + config_item.size,
733			    NULL, 0);
734	if (err < 0)
735		return err;
736
737	/* Activate the change */
738	req2 = cpu_to_le32(config_item.activate);
739	err = scarlett2_usb(mixer, SCARLETT2_USB_DATA_CMD,
740			    &req2, sizeof(req2), NULL, 0);
741	if (err < 0)
742		return err;
743
744	/* Schedule the change to be written to NVRAM */
745	schedule_delayed_work(&private->work, msecs_to_jiffies(2000));
746
747	return 0;
748}
749
750/* Send a USB message to get data; result placed in *buf */
751static int scarlett2_usb_get(
752	struct usb_mixer_interface *mixer,
753	int offset, void *buf, int size)
754{
755	struct {
756		__le32 offset;
757		__le32 size;
758	} __packed req;
759
760	req.offset = cpu_to_le32(offset);
761	req.size = cpu_to_le32(size);
762	return scarlett2_usb(mixer, SCARLETT2_USB_GET_DATA,
763			     &req, sizeof(req), buf, size);
764}
765
766/* Send a USB message to get configuration parameters; result placed in *buf */
767static int scarlett2_usb_get_config(
768	struct usb_mixer_interface *mixer,
769	int config_item_num, int count, void *buf)
770{
771	const struct scarlett2_config config_item =
772	       scarlett2_config_items[config_item_num];
773	int size = config_item.size * count;
774
775	return scarlett2_usb_get(mixer, config_item.offset, buf, size);
776}
777
778/* Send a USB message to get volume status; result placed in *buf */
779static int scarlett2_usb_get_volume_status(
780	struct usb_mixer_interface *mixer,
781	struct scarlett2_usb_volume_status *buf)
782{
783	return scarlett2_usb_get(mixer, SCARLETT2_USB_VOLUME_STATUS_OFFSET,
784				 buf, sizeof(*buf));
785}
786
787/* Send a USB message to set the volumes for all inputs of one mix
788 * (values obtained from private->mix[])
789 */
790static int scarlett2_usb_set_mix(struct usb_mixer_interface *mixer,
791				     int mix_num)
792{
793	struct scarlett2_mixer_data *private = mixer->private_data;
794	const struct scarlett2_device_info *info = private->info;
795
796	struct {
797		__le16 mix_num;
798		__le16 data[SCARLETT2_INPUT_MIX_MAX];
799	} __packed req;
800
801	int i, j;
802	int num_mixer_in =
803		info->ports[SCARLETT2_PORT_TYPE_MIX].num[SCARLETT2_PORT_OUT];
804
805	req.mix_num = cpu_to_le16(mix_num);
806
807	for (i = 0, j = mix_num * num_mixer_in; i < num_mixer_in; i++, j++)
808		req.data[i] = cpu_to_le16(
809			scarlett2_mixer_values[private->mix[j]]
810		);
811
812	return scarlett2_usb(mixer, SCARLETT2_USB_SET_MIX,
813			     &req, (num_mixer_in + 1) * sizeof(u16),
814			     NULL, 0);
815}
816
817/* Convert a port number index (per info->ports) to a hardware ID */
818static u32 scarlett2_mux_src_num_to_id(const struct scarlett2_ports *ports,
819				       int num)
820{
821	int port_type;
822
823	for (port_type = 0;
824	     port_type < SCARLETT2_PORT_TYPE_COUNT;
825	     port_type++) {
826		if (num < ports[port_type].num[SCARLETT2_PORT_IN])
827			return ports[port_type].id | num;
828		num -= ports[port_type].num[SCARLETT2_PORT_IN];
829	}
830
831	/* Oops */
832	return 0;
833}
834
835/* Send USB messages to set mux inputs */
836static int scarlett2_usb_set_mux(struct usb_mixer_interface *mixer)
837{
838	struct scarlett2_mixer_data *private = mixer->private_data;
839	const struct scarlett2_device_info *info = private->info;
840	const struct scarlett2_ports *ports = info->ports;
841	int rate, port_dir_rate;
842
843	static const int assignment_order[SCARLETT2_PORT_TYPE_COUNT] = {
844		SCARLETT2_PORT_TYPE_PCM,
845		SCARLETT2_PORT_TYPE_ANALOGUE,
846		SCARLETT2_PORT_TYPE_SPDIF,
847		SCARLETT2_PORT_TYPE_ADAT,
848		SCARLETT2_PORT_TYPE_MIX,
849		SCARLETT2_PORT_TYPE_NONE,
850	};
851
852	struct {
853		__le16 pad;
854		__le16 num;
855		__le32 data[SCARLETT2_MUX_MAX];
856	} __packed req;
857
858	req.pad = 0;
859
860	/* mux settings for each rate */
861	for (rate = 0, port_dir_rate = SCARLETT2_PORT_OUT_44;
862	     port_dir_rate <= SCARLETT2_PORT_OUT_176;
863	     rate++, port_dir_rate++) {
864		int order_num, i, err;
865
866		req.num = cpu_to_le16(rate);
867
868		for (order_num = 0, i = 0;
869		     order_num < SCARLETT2_PORT_TYPE_COUNT;
870		     order_num++) {
871			int port_type = assignment_order[order_num];
872			int j = scarlett2_get_port_start_num(ports,
873							     SCARLETT2_PORT_OUT,
874							     port_type);
875			int port_id = ports[port_type].id;
876			int channel;
877
878			for (channel = 0;
879			     channel < ports[port_type].num[port_dir_rate];
880			     channel++, i++, j++)
881				/* lower 12 bits for the destination and
882				 * next 12 bits for the source
883				 */
884				req.data[i] = !port_id
885					? 0
886					: cpu_to_le32(
887						port_id |
888						channel |
889						scarlett2_mux_src_num_to_id(
890							ports, private->mux[j]
891						) << 12
892					  );
893
894			/* skip private->mux[j] entries not output */
895			j += ports[port_type].num[SCARLETT2_PORT_OUT] -
896			     ports[port_type].num[port_dir_rate];
897		}
898
899		err = scarlett2_usb(mixer, SCARLETT2_USB_SET_MUX,
900				    &req, (i + 1) * sizeof(u32),
901				    NULL, 0);
902		if (err < 0)
903			return err;
904	}
905
906	return 0;
907}
908
909/* Send USB message to get meter levels */
910static int scarlett2_usb_get_meter_levels(struct usb_mixer_interface *mixer,
911					  u16 *levels)
912{
913	struct {
914		__le16 pad;
915		__le16 num_meters;
916		__le32 magic;
917	} __packed req;
918	u32 resp[SCARLETT2_NUM_METERS];
919	int i, err;
920
921	req.pad = 0;
922	req.num_meters = cpu_to_le16(SCARLETT2_NUM_METERS);
923	req.magic = cpu_to_le32(SCARLETT2_USB_METER_LEVELS_GET_MAGIC);
924	err = scarlett2_usb(mixer, SCARLETT2_USB_GET_METER_LEVELS,
925			    &req, sizeof(req), resp, sizeof(resp));
926	if (err < 0)
927		return err;
928
929	/* copy, convert to u16 */
930	for (i = 0; i < SCARLETT2_NUM_METERS; i++)
931		levels[i] = resp[i];
932
933	return 0;
934}
935
936/*** Control Functions ***/
937
938/* helper function to create a new control */
939static int scarlett2_add_new_ctl(struct usb_mixer_interface *mixer,
940				 const struct snd_kcontrol_new *ncontrol,
941				 int index, int channels, const char *name,
942				 struct snd_kcontrol **kctl_return)
943{
944	struct snd_kcontrol *kctl;
945	struct usb_mixer_elem_info *elem;
946	int err;
947
948	elem = kzalloc(sizeof(*elem), GFP_KERNEL);
949	if (!elem)
950		return -ENOMEM;
951
952	/* We set USB_MIXER_BESPOKEN type, so that the core USB mixer code
953	 * ignores them for resume and other operations.
954	 * Also, the head.id field is set to 0, as we don't use this field.
955	 */
956	elem->head.mixer = mixer;
957	elem->control = index;
958	elem->head.id = 0;
959	elem->channels = channels;
960	elem->val_type = USB_MIXER_BESPOKEN;
961
962	kctl = snd_ctl_new1(ncontrol, elem);
963	if (!kctl) {
964		kfree(elem);
965		return -ENOMEM;
966	}
967	kctl->private_free = snd_usb_mixer_elem_free;
968
969	strlcpy(kctl->id.name, name, sizeof(kctl->id.name));
970
971	err = snd_usb_mixer_add_control(&elem->head, kctl);
972	if (err < 0)
973		return err;
974
975	if (kctl_return)
976		*kctl_return = kctl;
977
978	return 0;
979}
980
981/*** Analogue Line Out Volume Controls ***/
982
983/* Update hardware volume controls after receiving notification that
984 * they have changed
985 */
986static int scarlett2_update_volumes(struct usb_mixer_interface *mixer)
987{
988	struct scarlett2_mixer_data *private = mixer->private_data;
989	const struct scarlett2_ports *ports = private->info->ports;
990	struct scarlett2_usb_volume_status volume_status;
991	int num_line_out =
992		ports[SCARLETT2_PORT_TYPE_ANALOGUE].num[SCARLETT2_PORT_OUT];
993	int err, i;
994
995	private->vol_updated = 0;
996
997	err = scarlett2_usb_get_volume_status(mixer, &volume_status);
998	if (err < 0)
999		return err;
1000
1001	private->master_vol = clamp(
1002		volume_status.master_vol + SCARLETT2_VOLUME_BIAS,
1003		0, SCARLETT2_VOLUME_BIAS);
1004
1005	for (i = 0; i < num_line_out; i++) {
1006		if (private->vol_sw_hw_switch[i])
1007			private->vol[i] = private->master_vol;
1008	}
1009
1010	for (i = 0; i < private->info->button_count; i++)
1011		private->buttons[i] = !!volume_status.buttons[i];
1012
1013	return 0;
1014}
1015
1016static int scarlett2_volume_ctl_info(struct snd_kcontrol *kctl,
1017				     struct snd_ctl_elem_info *uinfo)
1018{
1019	struct usb_mixer_elem_info *elem = kctl->private_data;
1020
1021	uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1022	uinfo->count = elem->channels;
1023	uinfo->value.integer.min = 0;
1024	uinfo->value.integer.max = SCARLETT2_VOLUME_BIAS;
1025	uinfo->value.integer.step = 1;
1026	return 0;
1027}
1028
1029static int scarlett2_master_volume_ctl_get(struct snd_kcontrol *kctl,
1030					   struct snd_ctl_elem_value *ucontrol)
1031{
1032	struct usb_mixer_elem_info *elem = kctl->private_data;
1033	struct usb_mixer_interface *mixer = elem->head.mixer;
1034	struct scarlett2_mixer_data *private = mixer->private_data;
1035
1036	mutex_lock(&private->data_mutex);
1037	if (private->vol_updated)
1038		scarlett2_update_volumes(mixer);
1039	mutex_unlock(&private->data_mutex);
1040
1041	ucontrol->value.integer.value[0] = private->master_vol;
1042	return 0;
1043}
1044
1045static int scarlett2_volume_ctl_get(struct snd_kcontrol *kctl,
1046				    struct snd_ctl_elem_value *ucontrol)
1047{
1048	struct usb_mixer_elem_info *elem = kctl->private_data;
1049	struct usb_mixer_interface *mixer = elem->head.mixer;
1050	struct scarlett2_mixer_data *private = mixer->private_data;
1051	int index = elem->control;
1052
1053	mutex_lock(&private->data_mutex);
1054	if (private->vol_updated)
1055		scarlett2_update_volumes(mixer);
1056	mutex_unlock(&private->data_mutex);
1057
1058	ucontrol->value.integer.value[0] = private->vol[index];
1059	return 0;
1060}
1061
1062static int scarlett2_volume_ctl_put(struct snd_kcontrol *kctl,
1063				    struct snd_ctl_elem_value *ucontrol)
1064{
1065	struct usb_mixer_elem_info *elem = kctl->private_data;
1066	struct usb_mixer_interface *mixer = elem->head.mixer;
1067	struct scarlett2_mixer_data *private = mixer->private_data;
1068	int index = elem->control;
1069	int oval, val, err = 0;
1070
1071	mutex_lock(&private->data_mutex);
1072
1073	oval = private->vol[index];
1074	val = ucontrol->value.integer.value[0];
1075
1076	if (oval == val)
1077		goto unlock;
1078
1079	private->vol[index] = val;
1080	err = scarlett2_usb_set_config(mixer, SCARLETT2_CONFIG_LINE_OUT_VOLUME,
1081				       index, val - SCARLETT2_VOLUME_BIAS);
1082	if (err == 0)
1083		err = 1;
1084
1085unlock:
1086	mutex_unlock(&private->data_mutex);
1087	return err;
1088}
1089
1090static const DECLARE_TLV_DB_MINMAX(
1091	db_scale_scarlett2_gain, -SCARLETT2_VOLUME_BIAS * 100, 0
1092);
1093
1094static const struct snd_kcontrol_new scarlett2_master_volume_ctl = {
1095	.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1096	.access = SNDRV_CTL_ELEM_ACCESS_READ |
1097		  SNDRV_CTL_ELEM_ACCESS_TLV_READ,
1098	.name = "",
1099	.info = scarlett2_volume_ctl_info,
1100	.get  = scarlett2_master_volume_ctl_get,
1101	.private_value = 0, /* max value */
1102	.tlv = { .p = db_scale_scarlett2_gain }
1103};
1104
1105static const struct snd_kcontrol_new scarlett2_line_out_volume_ctl = {
1106	.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1107	.access = SNDRV_CTL_ELEM_ACCESS_READWRITE |
1108		  SNDRV_CTL_ELEM_ACCESS_TLV_READ,
1109	.name = "",
1110	.info = scarlett2_volume_ctl_info,
1111	.get  = scarlett2_volume_ctl_get,
1112	.put  = scarlett2_volume_ctl_put,
1113	.private_value = 0, /* max value */
1114	.tlv = { .p = db_scale_scarlett2_gain }
1115};
1116
1117/*** HW/SW Volume Switch Controls ***/
1118
1119static int scarlett2_sw_hw_enum_ctl_info(struct snd_kcontrol *kctl,
1120					 struct snd_ctl_elem_info *uinfo)
1121{
1122	static const char *const values[2] = {
1123		"SW", "HW"
1124	};
1125
1126	return snd_ctl_enum_info(uinfo, 1, 2, values);
1127}
1128
1129static int scarlett2_sw_hw_enum_ctl_get(struct snd_kcontrol *kctl,
1130					struct snd_ctl_elem_value *ucontrol)
1131{
1132	struct usb_mixer_elem_info *elem = kctl->private_data;
1133	struct scarlett2_mixer_data *private = elem->head.mixer->private_data;
1134
1135	ucontrol->value.enumerated.item[0] =
1136		private->vol_sw_hw_switch[elem->control];
1137	return 0;
1138}
1139
1140static int scarlett2_sw_hw_enum_ctl_put(struct snd_kcontrol *kctl,
1141					struct snd_ctl_elem_value *ucontrol)
1142{
1143	struct usb_mixer_elem_info *elem = kctl->private_data;
1144	struct usb_mixer_interface *mixer = elem->head.mixer;
1145	struct scarlett2_mixer_data *private = mixer->private_data;
1146
1147	int index = elem->control;
1148	int oval, val, err = 0;
1149
1150	mutex_lock(&private->data_mutex);
1151
1152	oval = private->vol_sw_hw_switch[index];
1153	val = !!ucontrol->value.integer.value[0];
1154
1155	if (oval == val)
1156		goto unlock;
1157
1158	private->vol_sw_hw_switch[index] = val;
1159
1160	/* Change access mode to RO (hardware controlled volume)
1161	 * or RW (software controlled volume)
1162	 */
1163	if (val)
1164		private->vol_ctls[index]->vd[0].access &=
1165			~SNDRV_CTL_ELEM_ACCESS_WRITE;
1166	else
1167		private->vol_ctls[index]->vd[0].access |=
1168			SNDRV_CTL_ELEM_ACCESS_WRITE;
1169
1170	/* Reset volume to master volume */
1171	private->vol[index] = private->master_vol;
1172
1173	/* Set SW volume to current HW volume */
1174	err = scarlett2_usb_set_config(
1175		mixer, SCARLETT2_CONFIG_LINE_OUT_VOLUME,
1176		index, private->master_vol - SCARLETT2_VOLUME_BIAS);
1177	if (err < 0)
1178		goto unlock;
1179
1180	/* Notify of RO/RW change */
1181	snd_ctl_notify(mixer->chip->card, SNDRV_CTL_EVENT_MASK_INFO,
1182		       &private->vol_ctls[index]->id);
1183
1184	/* Send SW/HW switch change to the device */
1185	err = scarlett2_usb_set_config(mixer, SCARLETT2_CONFIG_SW_HW_SWITCH,
1186				       index, val);
1187	if (err == 0)
1188		err = 1;
1189
1190unlock:
1191	mutex_unlock(&private->data_mutex);
1192	return err;
1193}
1194
1195static const struct snd_kcontrol_new scarlett2_sw_hw_enum_ctl = {
1196	.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1197	.name = "",
1198	.info = scarlett2_sw_hw_enum_ctl_info,
1199	.get  = scarlett2_sw_hw_enum_ctl_get,
1200	.put  = scarlett2_sw_hw_enum_ctl_put,
1201};
1202
1203/*** Line Level/Instrument Level Switch Controls ***/
1204
1205static int scarlett2_level_enum_ctl_info(struct snd_kcontrol *kctl,
1206					 struct snd_ctl_elem_info *uinfo)
1207{
1208	static const char *const values[2] = {
1209		"Line", "Inst"
1210	};
1211
1212	return snd_ctl_enum_info(uinfo, 1, 2, values);
1213}
1214
1215static int scarlett2_level_enum_ctl_get(struct snd_kcontrol *kctl,
1216					struct snd_ctl_elem_value *ucontrol)
1217{
1218	struct usb_mixer_elem_info *elem = kctl->private_data;
1219	struct scarlett2_mixer_data *private = elem->head.mixer->private_data;
1220
1221	ucontrol->value.enumerated.item[0] =
1222		private->level_switch[elem->control];
1223	return 0;
1224}
1225
1226static int scarlett2_level_enum_ctl_put(struct snd_kcontrol *kctl,
1227					struct snd_ctl_elem_value *ucontrol)
1228{
1229	struct usb_mixer_elem_info *elem = kctl->private_data;
1230	struct usb_mixer_interface *mixer = elem->head.mixer;
1231	struct scarlett2_mixer_data *private = mixer->private_data;
1232
1233	int index = elem->control;
1234	int oval, val, err = 0;
1235
1236	mutex_lock(&private->data_mutex);
1237
1238	oval = private->level_switch[index];
1239	val = !!ucontrol->value.integer.value[0];
1240
1241	if (oval == val)
1242		goto unlock;
1243
1244	private->level_switch[index] = val;
1245
1246	/* Send switch change to the device */
1247	err = scarlett2_usb_set_config(mixer, SCARLETT2_CONFIG_LEVEL_SWITCH,
1248				       index, val);
1249	if (err == 0)
1250		err = 1;
1251
1252unlock:
1253	mutex_unlock(&private->data_mutex);
1254	return err;
1255}
1256
1257static const struct snd_kcontrol_new scarlett2_level_enum_ctl = {
1258	.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1259	.name = "",
1260	.info = scarlett2_level_enum_ctl_info,
1261	.get  = scarlett2_level_enum_ctl_get,
1262	.put  = scarlett2_level_enum_ctl_put,
1263};
1264
1265/*** Pad Switch Controls ***/
1266
1267static int scarlett2_pad_ctl_get(struct snd_kcontrol *kctl,
1268				 struct snd_ctl_elem_value *ucontrol)
1269{
1270	struct usb_mixer_elem_info *elem = kctl->private_data;
1271	struct scarlett2_mixer_data *private = elem->head.mixer->private_data;
1272
1273	ucontrol->value.enumerated.item[0] =
1274		private->pad_switch[elem->control];
1275	return 0;
1276}
1277
1278static int scarlett2_pad_ctl_put(struct snd_kcontrol *kctl,
1279				 struct snd_ctl_elem_value *ucontrol)
1280{
1281	struct usb_mixer_elem_info *elem = kctl->private_data;
1282	struct usb_mixer_interface *mixer = elem->head.mixer;
1283	struct scarlett2_mixer_data *private = mixer->private_data;
1284
1285	int index = elem->control;
1286	int oval, val, err = 0;
1287
1288	mutex_lock(&private->data_mutex);
1289
1290	oval = private->pad_switch[index];
1291	val = !!ucontrol->value.integer.value[0];
1292
1293	if (oval == val)
1294		goto unlock;
1295
1296	private->pad_switch[index] = val;
1297
1298	/* Send switch change to the device */
1299	err = scarlett2_usb_set_config(mixer, SCARLETT2_CONFIG_PAD_SWITCH,
1300				       index, val);
1301	if (err == 0)
1302		err = 1;
1303
1304unlock:
1305	mutex_unlock(&private->data_mutex);
1306	return err;
1307}
1308
1309static const struct snd_kcontrol_new scarlett2_pad_ctl = {
1310	.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1311	.name = "",
1312	.info = snd_ctl_boolean_mono_info,
1313	.get  = scarlett2_pad_ctl_get,
1314	.put  = scarlett2_pad_ctl_put,
1315};
1316
1317/*** Mute/Dim Controls ***/
1318
1319static int scarlett2_button_ctl_get(struct snd_kcontrol *kctl,
1320				    struct snd_ctl_elem_value *ucontrol)
1321{
1322	struct usb_mixer_elem_info *elem = kctl->private_data;
1323	struct usb_mixer_interface *mixer = elem->head.mixer;
1324	struct scarlett2_mixer_data *private = mixer->private_data;
1325
1326	mutex_lock(&private->data_mutex);
1327	if (private->vol_updated)
1328		scarlett2_update_volumes(mixer);
1329	mutex_unlock(&private->data_mutex);
1330
1331	ucontrol->value.enumerated.item[0] = private->buttons[elem->control];
1332	return 0;
1333}
1334
1335static int scarlett2_button_ctl_put(struct snd_kcontrol *kctl,
1336				    struct snd_ctl_elem_value *ucontrol)
1337{
1338	struct usb_mixer_elem_info *elem = kctl->private_data;
1339	struct usb_mixer_interface *mixer = elem->head.mixer;
1340	struct scarlett2_mixer_data *private = mixer->private_data;
1341
1342	int index = elem->control;
1343	int oval, val, err = 0;
1344
1345	mutex_lock(&private->data_mutex);
1346
1347	oval = private->buttons[index];
1348	val = !!ucontrol->value.integer.value[0];
1349
1350	if (oval == val)
1351		goto unlock;
1352
1353	private->buttons[index] = val;
1354
1355	/* Send switch change to the device */
1356	err = scarlett2_usb_set_config(mixer, SCARLETT2_CONFIG_BUTTONS,
1357				       index, val);
1358	if (err == 0)
1359		err = 1;
1360
1361unlock:
1362	mutex_unlock(&private->data_mutex);
1363	return err;
1364}
1365
1366static const struct snd_kcontrol_new scarlett2_button_ctl = {
1367	.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1368	.name = "",
1369	.info = snd_ctl_boolean_mono_info,
1370	.get  = scarlett2_button_ctl_get,
1371	.put  = scarlett2_button_ctl_put
1372};
1373
1374/*** Create the analogue output controls ***/
1375
1376static int scarlett2_add_line_out_ctls(struct usb_mixer_interface *mixer)
1377{
1378	struct scarlett2_mixer_data *private = mixer->private_data;
1379	const struct scarlett2_device_info *info = private->info;
1380	const struct scarlett2_ports *ports = info->ports;
1381	int num_line_out =
1382		ports[SCARLETT2_PORT_TYPE_ANALOGUE].num[SCARLETT2_PORT_OUT];
1383	int err, i;
1384	char s[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
1385
1386	/* Add R/O HW volume control */
1387	if (info->line_out_hw_vol) {
1388		snprintf(s, sizeof(s), "Master HW Playback Volume");
1389		err = scarlett2_add_new_ctl(mixer,
1390					    &scarlett2_master_volume_ctl,
1391					    0, 1, s, &private->master_vol_ctl);
1392		if (err < 0)
1393			return err;
1394	}
1395
1396	/* Add volume controls */
1397	for (i = 0; i < num_line_out; i++) {
1398
1399		/* Fader */
1400		if (info->line_out_descrs[i])
1401			snprintf(s, sizeof(s),
1402				 "Line %02d (%s) Playback Volume",
1403				 i + 1, info->line_out_descrs[i]);
1404		else
1405			snprintf(s, sizeof(s),
1406				 "Line %02d Playback Volume",
1407				 i + 1);
1408		err = scarlett2_add_new_ctl(mixer,
1409					    &scarlett2_line_out_volume_ctl,
1410					    i, 1, s, &private->vol_ctls[i]);
1411		if (err < 0)
1412			return err;
1413
1414		/* Make the fader read-only if the SW/HW switch is set to HW */
1415		if (private->vol_sw_hw_switch[i])
1416			private->vol_ctls[i]->vd[0].access &=
1417				~SNDRV_CTL_ELEM_ACCESS_WRITE;
1418
1419		/* SW/HW Switch */
1420		if (info->line_out_hw_vol) {
1421			snprintf(s, sizeof(s),
1422				 "Line Out %02d Volume Control Playback Enum",
1423				 i + 1);
1424			err = scarlett2_add_new_ctl(mixer,
1425						    &scarlett2_sw_hw_enum_ctl,
1426						    i, 1, s, NULL);
1427			if (err < 0)
1428				return err;
1429		}
1430	}
1431
1432	/* Add HW button controls */
1433	for (i = 0; i < private->info->button_count; i++) {
1434		err = scarlett2_add_new_ctl(mixer, &scarlett2_button_ctl,
1435					    i, 1, scarlett2_button_names[i],
1436					    &private->button_ctls[i]);
1437		if (err < 0)
1438			return err;
1439	}
1440
1441	return 0;
1442}
1443
1444/*** Create the analogue input controls ***/
1445
1446static int scarlett2_add_line_in_ctls(struct usb_mixer_interface *mixer)
1447{
1448	struct scarlett2_mixer_data *private = mixer->private_data;
1449	const struct scarlett2_device_info *info = private->info;
1450	int err, i;
1451	char s[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
1452
1453	/* Add input level (line/inst) controls */
1454	for (i = 0; i < info->level_input_count; i++) {
1455		snprintf(s, sizeof(s), "Line In %d Level Capture Enum", i + 1);
1456		err = scarlett2_add_new_ctl(mixer, &scarlett2_level_enum_ctl,
1457					    i, 1, s, NULL);
1458		if (err < 0)
1459			return err;
1460	}
1461
1462	/* Add input pad controls */
1463	for (i = 0; i < info->pad_input_count; i++) {
1464		snprintf(s, sizeof(s), "Line In %d Pad Capture Switch", i + 1);
1465		err = scarlett2_add_new_ctl(mixer, &scarlett2_pad_ctl,
1466					    i, 1, s, NULL);
1467		if (err < 0)
1468			return err;
1469	}
1470
1471	return 0;
1472}
1473
1474/*** Mixer Volume Controls ***/
1475
1476static int scarlett2_mixer_ctl_info(struct snd_kcontrol *kctl,
1477				    struct snd_ctl_elem_info *uinfo)
1478{
1479	struct usb_mixer_elem_info *elem = kctl->private_data;
1480
1481	uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1482	uinfo->count = elem->channels;
1483	uinfo->value.integer.min = 0;
1484	uinfo->value.integer.max = SCARLETT2_MIXER_MAX_VALUE;
1485	uinfo->value.integer.step = 1;
1486	return 0;
1487}
1488
1489static int scarlett2_mixer_ctl_get(struct snd_kcontrol *kctl,
1490				   struct snd_ctl_elem_value *ucontrol)
1491{
1492	struct usb_mixer_elem_info *elem = kctl->private_data;
1493	struct scarlett2_mixer_data *private = elem->head.mixer->private_data;
1494
1495	ucontrol->value.integer.value[0] = private->mix[elem->control];
1496	return 0;
1497}
1498
1499static int scarlett2_mixer_ctl_put(struct snd_kcontrol *kctl,
1500				   struct snd_ctl_elem_value *ucontrol)
1501{
1502	struct usb_mixer_elem_info *elem = kctl->private_data;
1503	struct usb_mixer_interface *mixer = elem->head.mixer;
1504	struct scarlett2_mixer_data *private = mixer->private_data;
1505	const struct scarlett2_device_info *info = private->info;
1506	const struct scarlett2_ports *ports = info->ports;
1507	int oval, val, num_mixer_in, mix_num, err = 0;
1508
1509	mutex_lock(&private->data_mutex);
1510
1511	oval = private->mix[elem->control];
1512	val = ucontrol->value.integer.value[0];
1513	num_mixer_in = ports[SCARLETT2_PORT_TYPE_MIX].num[SCARLETT2_PORT_OUT];
1514	mix_num = elem->control / num_mixer_in;
1515
1516	if (oval == val)
1517		goto unlock;
1518
1519	private->mix[elem->control] = val;
1520	err = scarlett2_usb_set_mix(mixer, mix_num);
1521	if (err == 0)
1522		err = 1;
1523
1524unlock:
1525	mutex_unlock(&private->data_mutex);
1526	return err;
1527}
1528
1529static const DECLARE_TLV_DB_MINMAX(
1530	db_scale_scarlett2_mixer,
1531	SCARLETT2_MIXER_MIN_DB * 100,
1532	SCARLETT2_MIXER_MAX_DB * 100
1533);
1534
1535static const struct snd_kcontrol_new scarlett2_mixer_ctl = {
1536	.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1537	.access = SNDRV_CTL_ELEM_ACCESS_READWRITE |
1538		  SNDRV_CTL_ELEM_ACCESS_TLV_READ,
1539	.name = "",
1540	.info = scarlett2_mixer_ctl_info,
1541	.get  = scarlett2_mixer_ctl_get,
1542	.put  = scarlett2_mixer_ctl_put,
1543	.private_value = SCARLETT2_MIXER_MAX_DB, /* max value */
1544	.tlv = { .p = db_scale_scarlett2_mixer }
1545};
1546
1547static int scarlett2_add_mixer_ctls(struct usb_mixer_interface *mixer)
1548{
1549	struct scarlett2_mixer_data *private = mixer->private_data;
1550	const struct scarlett2_ports *ports = private->info->ports;
1551	int err, i, j;
1552	int index;
1553	char s[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
1554
1555	int num_inputs = ports[SCARLETT2_PORT_TYPE_MIX].num[SCARLETT2_PORT_OUT];
1556	int num_outputs = ports[SCARLETT2_PORT_TYPE_MIX].num[SCARLETT2_PORT_IN];
1557
1558	for (i = 0, index = 0; i < num_outputs; i++) {
1559		for (j = 0; j < num_inputs; j++, index++) {
1560			snprintf(s, sizeof(s),
1561				 "Mix %c Input %02d Playback Volume",
1562				 'A' + i, j + 1);
1563			err = scarlett2_add_new_ctl(mixer, &scarlett2_mixer_ctl,
1564						    index, 1, s, NULL);
1565			if (err < 0)
1566				return err;
1567		}
1568	}
1569
1570	return 0;
1571}
1572
1573/*** Mux Source Selection Controls ***/
1574
1575static int scarlett2_mux_src_enum_ctl_info(struct snd_kcontrol *kctl,
1576					   struct snd_ctl_elem_info *uinfo)
1577{
1578	struct usb_mixer_elem_info *elem = kctl->private_data;
1579	struct scarlett2_mixer_data *private = elem->head.mixer->private_data;
1580	const struct scarlett2_ports *ports = private->info->ports;
1581	unsigned int item = uinfo->value.enumerated.item;
1582	int items = private->num_mux_srcs;
1583	int port_type;
1584
1585	uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1586	uinfo->count = elem->channels;
1587	uinfo->value.enumerated.items = items;
1588
1589	if (item >= items)
1590		item = uinfo->value.enumerated.item = items - 1;
1591
1592	for (port_type = 0;
1593	     port_type < SCARLETT2_PORT_TYPE_COUNT;
1594	     port_type++) {
1595		if (item < ports[port_type].num[SCARLETT2_PORT_IN]) {
1596			sprintf(uinfo->value.enumerated.name,
1597				ports[port_type].src_descr,
1598				item + ports[port_type].src_num_offset);
1599			return 0;
1600		}
1601		item -= ports[port_type].num[SCARLETT2_PORT_IN];
1602	}
1603
1604	return -EINVAL;
1605}
1606
1607static int scarlett2_mux_src_enum_ctl_get(struct snd_kcontrol *kctl,
1608					  struct snd_ctl_elem_value *ucontrol)
1609{
1610	struct usb_mixer_elem_info *elem = kctl->private_data;
1611	struct scarlett2_mixer_data *private = elem->head.mixer->private_data;
1612
1613	ucontrol->value.enumerated.item[0] = private->mux[elem->control];
1614	return 0;
1615}
1616
1617static int scarlett2_mux_src_enum_ctl_put(struct snd_kcontrol *kctl,
1618					  struct snd_ctl_elem_value *ucontrol)
1619{
1620	struct usb_mixer_elem_info *elem = kctl->private_data;
1621	struct usb_mixer_interface *mixer = elem->head.mixer;
1622	struct scarlett2_mixer_data *private = mixer->private_data;
1623	int index = elem->control;
1624	int oval, val, err = 0;
1625
1626	mutex_lock(&private->data_mutex);
1627
1628	oval = private->mux[index];
1629	val = clamp(ucontrol->value.integer.value[0],
1630		    0L, private->num_mux_srcs - 1L);
1631
1632	if (oval == val)
1633		goto unlock;
1634
1635	private->mux[index] = val;
1636	err = scarlett2_usb_set_mux(mixer);
1637	if (err == 0)
1638		err = 1;
1639
1640unlock:
1641	mutex_unlock(&private->data_mutex);
1642	return err;
1643}
1644
1645static const struct snd_kcontrol_new scarlett2_mux_src_enum_ctl = {
1646	.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1647	.name = "",
1648	.info = scarlett2_mux_src_enum_ctl_info,
1649	.get  = scarlett2_mux_src_enum_ctl_get,
1650	.put  = scarlett2_mux_src_enum_ctl_put,
1651};
1652
1653static int scarlett2_add_mux_enums(struct usb_mixer_interface *mixer)
1654{
1655	struct scarlett2_mixer_data *private = mixer->private_data;
1656	const struct scarlett2_ports *ports = private->info->ports;
1657	int port_type, channel, i;
1658
1659	for (i = 0, port_type = 0;
1660	     port_type < SCARLETT2_PORT_TYPE_COUNT;
1661	     port_type++) {
1662		for (channel = 0;
1663		     channel < ports[port_type].num[SCARLETT2_PORT_OUT];
1664		     channel++, i++) {
1665			int err;
1666			char s[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
1667			const char *const descr = ports[port_type].dst_descr;
1668
1669			snprintf(s, sizeof(s) - 5, descr, channel + 1);
1670			strcat(s, " Enum");
1671
1672			err = scarlett2_add_new_ctl(mixer,
1673						    &scarlett2_mux_src_enum_ctl,
1674						    i, 1, s, NULL);
1675			if (err < 0)
1676				return err;
1677		}
1678	}
1679
1680	return 0;
1681}
1682
1683/*** Meter Controls ***/
1684
1685static int scarlett2_meter_ctl_info(struct snd_kcontrol *kctl,
1686				    struct snd_ctl_elem_info *uinfo)
1687{
1688	struct usb_mixer_elem_info *elem = kctl->private_data;
1689
1690	uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1691	uinfo->count = elem->channels;
1692	uinfo->value.integer.min = 0;
1693	uinfo->value.integer.max = 4095;
1694	uinfo->value.integer.step = 1;
1695	return 0;
1696}
1697
1698static int scarlett2_meter_ctl_get(struct snd_kcontrol *kctl,
1699				   struct snd_ctl_elem_value *ucontrol)
1700{
1701	struct usb_mixer_elem_info *elem = kctl->private_data;
1702	u16 meter_levels[SCARLETT2_NUM_METERS];
1703	int i, err;
1704
1705	err = scarlett2_usb_get_meter_levels(elem->head.mixer, meter_levels);
1706	if (err < 0)
1707		return err;
1708
1709	for (i = 0; i < elem->channels; i++)
1710		ucontrol->value.integer.value[i] = meter_levels[i];
1711
1712	return 0;
1713}
1714
1715static const struct snd_kcontrol_new scarlett2_meter_ctl = {
1716	.iface = SNDRV_CTL_ELEM_IFACE_PCM,
1717	.access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE,
1718	.name = "",
1719	.info = scarlett2_meter_ctl_info,
1720	.get  = scarlett2_meter_ctl_get
1721};
1722
1723static int scarlett2_add_meter_ctl(struct usb_mixer_interface *mixer)
1724{
1725	return scarlett2_add_new_ctl(mixer, &scarlett2_meter_ctl,
1726				     0, SCARLETT2_NUM_METERS,
1727				     "Level Meter", NULL);
1728}
1729
1730/*** Cleanup/Suspend Callbacks ***/
1731
1732static void scarlett2_private_free(struct usb_mixer_interface *mixer)
1733{
1734	struct scarlett2_mixer_data *private = mixer->private_data;
1735
1736	cancel_delayed_work_sync(&private->work);
1737	kfree(private);
1738	mixer->private_data = NULL;
1739}
1740
1741static void scarlett2_private_suspend(struct usb_mixer_interface *mixer)
1742{
1743	struct scarlett2_mixer_data *private = mixer->private_data;
1744
1745	if (cancel_delayed_work_sync(&private->work))
1746		scarlett2_config_save(private->mixer);
1747}
1748
1749/*** Initialisation ***/
1750
1751static int scarlett2_count_mux_srcs(const struct scarlett2_ports *ports)
1752{
1753	int port_type, count = 0;
1754
1755	for (port_type = 0;
1756	     port_type < SCARLETT2_PORT_TYPE_COUNT;
1757	     port_type++)
1758		count += ports[port_type].num[SCARLETT2_PORT_IN];
1759
1760	return count;
1761}
1762
1763/* Default routing connects PCM outputs and inputs to Analogue,
1764 * S/PDIF, then ADAT
1765 */
1766static void scarlett2_init_routing(u8 *mux,
1767				   const struct scarlett2_ports *ports)
1768{
1769	int i, input_num, input_count, port_type;
1770	int output_num, output_count, port_type_connect_num;
1771
1772	static const int connect_order[] = {
1773		SCARLETT2_PORT_TYPE_ANALOGUE,
1774		SCARLETT2_PORT_TYPE_SPDIF,
1775		SCARLETT2_PORT_TYPE_ADAT,
1776		-1
1777	};
1778
1779	/* Assign PCM inputs (routing outputs) */
1780	output_num = scarlett2_get_port_start_num(ports,
1781						  SCARLETT2_PORT_OUT,
1782						  SCARLETT2_PORT_TYPE_PCM);
1783	output_count = ports[SCARLETT2_PORT_TYPE_PCM].num[SCARLETT2_PORT_OUT];
1784
1785	for (port_type = connect_order[port_type_connect_num = 0];
1786	     port_type >= 0;
1787	     port_type = connect_order[++port_type_connect_num]) {
1788		input_num = scarlett2_get_port_start_num(
1789			ports, SCARLETT2_PORT_IN, port_type);
1790		input_count = ports[port_type].num[SCARLETT2_PORT_IN];
1791		for (i = 0;
1792		     i < input_count && output_count;
1793		     i++, output_count--)
1794			mux[output_num++] = input_num++;
1795	}
1796
1797	/* Assign PCM outputs (routing inputs) */
1798	input_num = scarlett2_get_port_start_num(ports,
1799						 SCARLETT2_PORT_IN,
1800						 SCARLETT2_PORT_TYPE_PCM);
1801	input_count = ports[SCARLETT2_PORT_TYPE_PCM].num[SCARLETT2_PORT_IN];
1802
1803	for (port_type = connect_order[port_type_connect_num = 0];
1804	     port_type >= 0;
1805	     port_type = connect_order[++port_type_connect_num]) {
1806		output_num = scarlett2_get_port_start_num(
1807			ports, SCARLETT2_PORT_OUT, port_type);
1808		output_count = ports[port_type].num[SCARLETT2_PORT_OUT];
1809		for (i = 0;
1810		     i < output_count && input_count;
1811		     i++, input_count--)
1812			mux[output_num++] = input_num++;
1813	}
1814}
1815
1816/* Initialise private data, routing, sequence number */
1817static int scarlett2_init_private(struct usb_mixer_interface *mixer,
1818				  const struct scarlett2_device_info *info)
1819{
1820	struct scarlett2_mixer_data *private =
1821		kzalloc(sizeof(struct scarlett2_mixer_data), GFP_KERNEL);
1822
1823	if (!private)
1824		return -ENOMEM;
1825
1826	mutex_init(&private->usb_mutex);
1827	mutex_init(&private->data_mutex);
1828	INIT_DELAYED_WORK(&private->work, scarlett2_config_save_work);
1829	private->info = info;
1830	private->num_mux_srcs = scarlett2_count_mux_srcs(info->ports);
1831	private->scarlett2_seq = 0;
1832	private->mixer = mixer;
1833	mixer->private_data = private;
1834	mixer->private_free = scarlett2_private_free;
1835	mixer->private_suspend = scarlett2_private_suspend;
1836
1837	/* Setup default routing */
1838	scarlett2_init_routing(private->mux, info->ports);
1839
1840	/* Initialise the sequence number used for the proprietary commands */
1841	return scarlett2_usb(mixer, SCARLETT2_USB_INIT_SEQ, NULL, 0, NULL, 0);
1842}
1843
1844/* Read line-in config and line-out volume settings on start */
1845static int scarlett2_read_configs(struct usb_mixer_interface *mixer)
1846{
1847	struct scarlett2_mixer_data *private = mixer->private_data;
1848	const struct scarlett2_device_info *info = private->info;
1849	const struct scarlett2_ports *ports = info->ports;
1850	int num_line_out =
1851		ports[SCARLETT2_PORT_TYPE_ANALOGUE].num[SCARLETT2_PORT_OUT];
1852	u8 level_switches[SCARLETT2_LEVEL_SWITCH_MAX];
1853	u8 pad_switches[SCARLETT2_PAD_SWITCH_MAX];
1854	struct scarlett2_usb_volume_status volume_status;
1855	int err, i;
1856
1857	if (info->level_input_count) {
1858		err = scarlett2_usb_get_config(
1859			mixer,
1860			SCARLETT2_CONFIG_LEVEL_SWITCH,
1861			info->level_input_count,
1862			level_switches);
1863		if (err < 0)
1864			return err;
1865		for (i = 0; i < info->level_input_count; i++)
1866			private->level_switch[i] = level_switches[i];
1867	}
1868
1869	if (info->pad_input_count) {
1870		err = scarlett2_usb_get_config(
1871			mixer,
1872			SCARLETT2_CONFIG_PAD_SWITCH,
1873			info->pad_input_count,
1874			pad_switches);
1875		if (err < 0)
1876			return err;
1877		for (i = 0; i < info->pad_input_count; i++)
1878			private->pad_switch[i] = pad_switches[i];
1879	}
1880
1881	err = scarlett2_usb_get_volume_status(mixer, &volume_status);
1882	if (err < 0)
1883		return err;
1884
1885	private->master_vol = clamp(
1886		volume_status.master_vol + SCARLETT2_VOLUME_BIAS,
1887		0, SCARLETT2_VOLUME_BIAS);
1888
1889	for (i = 0; i < num_line_out; i++) {
1890		int volume;
1891
1892		private->vol_sw_hw_switch[i] =
1893			info->line_out_hw_vol
1894				&& volume_status.sw_hw_switch[i];
1895
1896		volume = private->vol_sw_hw_switch[i]
1897			   ? volume_status.master_vol
1898			   : volume_status.sw_vol[i];
1899		volume = clamp(volume + SCARLETT2_VOLUME_BIAS,
1900			       0, SCARLETT2_VOLUME_BIAS);
1901		private->vol[i] = volume;
1902	}
1903
1904	for (i = 0; i < info->button_count; i++)
1905		private->buttons[i] = !!volume_status.buttons[i];
1906
1907	return 0;
1908}
1909
1910/* Notify on volume change */
1911static void scarlett2_mixer_interrupt_vol_change(
1912	struct usb_mixer_interface *mixer)
1913{
1914	struct scarlett2_mixer_data *private = mixer->private_data;
1915	const struct scarlett2_ports *ports = private->info->ports;
1916	int num_line_out =
1917		ports[SCARLETT2_PORT_TYPE_ANALOGUE].num[SCARLETT2_PORT_OUT];
1918	int i;
1919
1920	private->vol_updated = 1;
1921
1922	snd_ctl_notify(mixer->chip->card, SNDRV_CTL_EVENT_MASK_VALUE,
1923		       &private->master_vol_ctl->id);
1924
1925	for (i = 0; i < num_line_out; i++) {
1926		if (!private->vol_sw_hw_switch[i])
1927			continue;
1928		snd_ctl_notify(mixer->chip->card, SNDRV_CTL_EVENT_MASK_VALUE,
1929			       &private->vol_ctls[i]->id);
1930	}
1931}
1932
1933/* Notify on button change */
1934static void scarlett2_mixer_interrupt_button_change(
1935	struct usb_mixer_interface *mixer)
1936{
1937	struct scarlett2_mixer_data *private = mixer->private_data;
1938	int i;
1939
1940	private->vol_updated = 1;
1941
1942	for (i = 0; i < private->info->button_count; i++)
1943		snd_ctl_notify(mixer->chip->card, SNDRV_CTL_EVENT_MASK_VALUE,
1944			       &private->button_ctls[i]->id);
1945}
1946
1947/* Interrupt callback */
1948static void scarlett2_mixer_interrupt(struct urb *urb)
1949{
1950	struct usb_mixer_interface *mixer = urb->context;
1951	int len = urb->actual_length;
1952	int ustatus = urb->status;
1953	u32 data;
1954
1955	if (ustatus != 0)
1956		goto requeue;
1957
1958	if (len == 8) {
1959		data = le32_to_cpu(*(__le32 *)urb->transfer_buffer);
1960		if (data & SCARLETT2_USB_INTERRUPT_VOL_CHANGE)
1961			scarlett2_mixer_interrupt_vol_change(mixer);
1962		if (data & SCARLETT2_USB_INTERRUPT_BUTTON_CHANGE)
1963			scarlett2_mixer_interrupt_button_change(mixer);
1964	} else {
1965		usb_audio_err(mixer->chip,
1966			      "scarlett mixer interrupt length %d\n", len);
1967	}
1968
1969requeue:
1970	if (ustatus != -ENOENT &&
1971	    ustatus != -ECONNRESET &&
1972	    ustatus != -ESHUTDOWN) {
1973		urb->dev = mixer->chip->dev;
1974		usb_submit_urb(urb, GFP_ATOMIC);
1975	}
1976}
1977
1978static int scarlett2_mixer_status_create(struct usb_mixer_interface *mixer)
1979{
1980	struct usb_device *dev = mixer->chip->dev;
1981	unsigned int pipe = usb_rcvintpipe(dev,
1982					   SCARLETT2_USB_INTERRUPT_ENDPOINT);
1983	void *transfer_buffer;
1984
1985	if (mixer->urb) {
1986		usb_audio_err(mixer->chip,
1987			      "%s: mixer urb already in use!\n", __func__);
1988		return 0;
1989	}
1990
1991	if (usb_pipe_type_check(dev, pipe))
1992		return -EINVAL;
1993
1994	mixer->urb = usb_alloc_urb(0, GFP_KERNEL);
1995	if (!mixer->urb)
1996		return -ENOMEM;
1997
1998	transfer_buffer = kmalloc(SCARLETT2_USB_INTERRUPT_MAX_DATA, GFP_KERNEL);
1999	if (!transfer_buffer)
2000		return -ENOMEM;
2001
2002	usb_fill_int_urb(mixer->urb, dev, pipe,
2003			 transfer_buffer, SCARLETT2_USB_INTERRUPT_MAX_DATA,
2004			 scarlett2_mixer_interrupt, mixer,
2005			 SCARLETT2_USB_INTERRUPT_INTERVAL);
2006
2007	return usb_submit_urb(mixer->urb, GFP_KERNEL);
2008}
2009
2010static int snd_scarlett_gen2_controls_create(struct usb_mixer_interface *mixer,
2011					     const struct scarlett2_device_info *info)
2012{
2013	int err;
2014
2015	/* Initialise private data, routing, sequence number */
2016	err = scarlett2_init_private(mixer, info);
2017	if (err < 0)
2018		return err;
2019
2020	/* Read volume levels and controls from the interface */
2021	err = scarlett2_read_configs(mixer);
2022	if (err < 0)
2023		return err;
2024
2025	/* Create the analogue output controls */
2026	err = scarlett2_add_line_out_ctls(mixer);
2027	if (err < 0)
2028		return err;
2029
2030	/* Create the analogue input controls */
2031	err = scarlett2_add_line_in_ctls(mixer);
2032	if (err < 0)
2033		return err;
2034
2035	/* Create the input, output, and mixer mux input selections */
2036	err = scarlett2_add_mux_enums(mixer);
2037	if (err < 0)
2038		return err;
2039
2040	/* Create the matrix mixer controls */
2041	err = scarlett2_add_mixer_ctls(mixer);
2042	if (err < 0)
2043		return err;
2044
2045	/* Create the level meter controls */
2046	err = scarlett2_add_meter_ctl(mixer);
2047	if (err < 0)
2048		return err;
2049
2050	/* Set up the interrupt polling if there are hardware buttons */
2051	if (info->button_count) {
2052		err = scarlett2_mixer_status_create(mixer);
2053		if (err < 0)
2054			return err;
2055	}
2056
2057	return 0;
2058}
2059
2060int snd_scarlett_gen2_init(struct usb_mixer_interface *mixer)
2061{
2062	struct snd_usb_audio *chip = mixer->chip;
2063	const struct scarlett2_device_info *info;
2064	int err;
2065
2066	/* only use UAC_VERSION_2 */
2067	if (!mixer->protocol)
2068		return 0;
2069
2070	switch (chip->usb_id) {
2071	case USB_ID(0x1235, 0x8203):
2072		info = &s6i6_gen2_info;
2073		break;
2074	case USB_ID(0x1235, 0x8204):
2075		info = &s18i8_gen2_info;
2076		break;
2077	case USB_ID(0x1235, 0x8201):
2078		info = &s18i20_gen2_info;
2079		break;
2080	default: /* device not (yet) supported */
2081		return -EINVAL;
2082	}
2083
2084	if (!(chip->setup & SCARLETT2_ENABLE)) {
2085		usb_audio_info(chip,
2086			"Focusrite Scarlett Gen 2 Mixer Driver disabled; "
2087			"use options snd_usb_audio vid=0x%04x pid=0x%04x "
2088			"device_setup=1 to enable and report any issues "
2089			"to g@b4.vu",
2090			USB_ID_VENDOR(chip->usb_id),
2091			USB_ID_PRODUCT(chip->usb_id));
2092		return 0;
2093	}
2094
2095	usb_audio_info(chip,
2096		"Focusrite Scarlett Gen 2 Mixer Driver enabled pid=0x%04x",
2097		USB_ID_PRODUCT(chip->usb_id));
2098
2099	err = snd_scarlett_gen2_controls_create(mixer, info);
2100	if (err < 0)
2101		usb_audio_err(mixer->chip,
2102			      "Error initialising Scarlett Mixer Driver: %d",
2103			      err);
2104
2105	return err;
2106}
2107