xref: /linux/sound/usb/mixer_s1810c.c (revision 9e4e86a604dfd06402933467578c4b79f5412b2c)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Presonus Studio 1810c driver for ALSA
4  * Copyright (C) 2019 Nick Kossifidis <mickflemm@gmail.com>
5  *
6  * Based on reverse engineering of the communication protocol
7  * between the windows driver / Univeral Control (UC) program
8  * and the device, through usbmon.
9  *
10  * For now this bypasses the mixer, with all channels split,
11  * so that the software can mix with greater flexibility.
12  * It also adds controls for the 4 buttons on the front of
13  * the device.
14  */
15 
16 #include <linux/usb.h>
17 #include <linux/usb/audio-v2.h>
18 #include <linux/slab.h>
19 #include <sound/core.h>
20 #include <sound/control.h>
21 
22 #include "usbaudio.h"
23 #include "mixer.h"
24 #include "mixer_quirks.h"
25 #include "helper.h"
26 #include "mixer_s1810c.h"
27 
28 /*
29  * DISCLAIMER: These are just guesses based on the
30  * dumps I got.
31  *
32  * It seems like a selects between
33  * device (0), mixer (0x64) and output (0x65)
34  *
35  * For mixer (0x64):
36  *  * b selects an input channel (see below).
37  *  * c selects an output channel pair (see below).
38  *  * d selects left (0) or right (1) of that pair.
39  *  * e level : see MIXER_LEVEL_* defines below.
40  *	Also used for setting volume levels
41  *	in which case b is also set so I guess
42  *	this way it is possible to set the volume
43  *	level from the specified input to the
44  *	specified output.
45  *
46  * IN Channels:
47  * 0  - 7  Mic/Inst/Line (Analog inputs)
48  * 8  - 9  S/PDIF
49  * 10 - 17 ADAT
50  * 18 - 35 DAW (Inputs from the host)
51  *
52  * OUT Channels (pairs):
53  * 0 -> Main out
54  * 1 -> Line1/2
55  * 2 -> Line3/4
56  * 3 -> S/PDIF
57  * 4 -> ADAT?
58  *
59  * For device (0):
60  *  * b and c are not used, at least not on the
61  *    dumps I got.
62  *  * d sets the control id to be modified
63  *    (see below).
64  *  * e sets the setting for that control.
65  *    (so for the switches I was interested
66  *    in it's 0/1)
67  *
68  * For output (0x65):
69  *   * b is the output channel (see above).
70  *   * c is zero.
71  *   * e I guess the same as with mixer
72  *
73  */
74 /* struct s1810c_ctl_packet - basic vendor request
75  * @selector: device/mixer/output
76  * @b: request-dependant field b
77  * @tag: fixed value identifying type of request
78  * @len: sizeof this struct - 8 (excludes first 2 fields)
79  *	i.e. for basic struct s1810c_ctl_packet: len is 5*4=0x14
80  * @c: request-dependant field c
81  * @d: request-dependant field d
82  * @e: request-dependant field e
83  *
84  * See longer description above. This could be combined
85  * (as a union?) with the longer struct s1810c_state_packet
86  */
87 struct s1810c_ctl_packet {
88 	__le32 selector;
89 	__le32 b;
90 	__le32 tag;
91 	__le32 len;
92 	__le32 c;
93 	__le32 d;
94 	__le32 e;
95 };
96 
97 /* selectors for CMD request
98  */
99 #define SC1810C_SEL_DEVICE 0
100 #define SC1810C_SEL_MIXER 0x64
101 #define SC1810C_SEL_OUTPUT 0x65
102 
103 
104 /* control ids */
105 #define SC1810C_CTL_LINE_SW	0
106 #define SC1810C_CTL_MUTE_SW	1
107 #define SC1824C_CTL_MONO_SW	2
108 #define SC1810C_CTL_AB_SW	3
109 #define SC1810C_CTL_48V_SW	4
110 
111 /* USB Control (vendor) requests
112  */
113 #define SC1810C_CMD_REQ	160
114 #define SC1810C_CMD_REQTYPE \
115 	(USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_OUT)
116 #define SC1810C_CMD_TAG		0x50617269
117 #define SC1810C_CMD_LEN		0x14
118 
119 #define SC1810C_SET_STATE_REQ	161
120 #define SC1810C_SET_STATE_REQTYPE SC1810C_CMD_REQTYPE
121 #define SC1810C_SET_STATE_TAG	0x64656D73
122 #define SC1810C_SET_STATE_LEN	0xF4
123 
124 #define SC1810C_GET_STATE_REQ	162
125 #define SC1810C_GET_STATE_REQTYPE \
126 	(USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN)
127 #define SC1810C_GET_STATE_TAG	SC1810C_SET_STATE_TAG
128 #define SC1810C_GET_STATE_LEN	SC1810C_SET_STATE_LEN
129 
130 /* Mixer levels normally range from 0 (off) to 0x0100 0000 (0 dB).
131  * raw_level = 2^24 * 10^(db_level / 20), thus
132  * -3dB = 0xb53bf0 (technically, half-power -3.01...dB would be 0xb504f3)
133  * -96dB = 0x109
134  * -99dB = 0xBC
135  * PC software sliders cover -96 to +10dB (0x0329 8b08),
136  * but the value 0 (-inf dB) can be used when e.g. Mixer Bypass is enabled.
137  * Unclear what the hardware's maximum value is.
138  *
139  * Note, when a channel is panned to two channels (stereo),
140  * the mixer level is set to slider value (by default -96dB) minus 3dB,
141  * which explains the -99dB value seen in USB captures.
142  */
143 #define MIXER_LEVEL_MUTE 0
144 #define MIXER_LEVEL_N99DB 0xbc
145 #define MIXER_LEVEL_N3DB 0xb53bf0
146 #define MIXER_LEVEL_0DB 0x1000000
147 
148 /*
149  * This packet includes mixer volumes and
150  * various other fields, it's an extended
151  * version of ctl_packet, with a and b
152  * being zero and different tag/length.
153  */
154 struct s1810c_state_packet {
155 	__le32 fields[63];
156 };
157 
158 /* indices into s1810c_state_packet.fields[]
159  */
160 #define SC1810C_STATE_TAG_IDX	2
161 #define SC1810C_STATE_LEN_IDX	3
162 
163 #define SC1810C_STATE_48V_SW	58
164 #define SC1810C_STATE_LINE_SW	59
165 #define SC1810C_STATE_MUTE_SW	60
166 #define SC1824C_STATE_MONO_SW	61
167 #define SC1810C_STATE_AB_SW	62
168 
169 struct s1810_mixer_state {
170 	uint16_t seqnum;
171 	struct mutex usb_mutex;
172 	struct mutex data_mutex;
173 };
174 
175 static int
snd_s1810c_send_ctl_packet(struct usb_device * dev,u32 sel,u32 b,u32 c,u32 d,u32 e)176 snd_s1810c_send_ctl_packet(struct usb_device *dev, u32 sel,
177 			   u32 b, u32 c, u32 d, u32 e)
178 {
179 	struct s1810c_ctl_packet pkt = { 0 };
180 	int ret = 0;
181 
182 	pkt.tag = __cpu_to_le32(SC1810C_CMD_TAG);
183 	pkt.len = __cpu_to_le32(SC1810C_CMD_LEN);
184 
185 	pkt.selector = __cpu_to_le32(sel);
186 	pkt.b = __cpu_to_le32(b);
187 	pkt.c = __cpu_to_le32(c);
188 	pkt.d = __cpu_to_le32(d);
189 	pkt.e = __cpu_to_le32(e);
190 
191 	ret = snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0),
192 			      SC1810C_CMD_REQ,
193 			      SC1810C_CMD_REQTYPE, 0, 0, &pkt, sizeof(pkt));
194 	if (ret < 0) {
195 		dev_warn(&dev->dev, "could not send ctl packet\n");
196 		return ret;
197 	}
198 	return 0;
199 }
200 
201 /*
202  * When opening Universal Control the program periodically
203  * sends and receives state packets for syncinc state between
204  * the device and the host.
205  *
206  * Note that if we send only the request to get data back we'll
207  * get an error, we need to first send an empty state packet and
208  * then ask to receive a filled. Their seqnumbers must also match.
209  */
210 static int
snd_sc1810c_get_status_field(struct usb_device * dev,u32 * field,int field_idx,uint16_t * seqnum)211 snd_sc1810c_get_status_field(struct usb_device *dev,
212 			     u32 *field, int field_idx, uint16_t *seqnum)
213 {
214 	struct s1810c_state_packet pkt_out = { { 0 } };
215 	struct s1810c_state_packet pkt_in = { { 0 } };
216 	int ret = 0;
217 
218 	pkt_out.fields[SC1810C_STATE_TAG_IDX] = __cpu_to_le32(SC1810C_SET_STATE_TAG);
219 	pkt_out.fields[SC1810C_STATE_LEN_IDX] = __cpu_to_le32(SC1810C_SET_STATE_LEN);
220 	ret = snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0),
221 			      SC1810C_SET_STATE_REQ,
222 			      SC1810C_SET_STATE_REQTYPE,
223 			      (*seqnum), 0, &pkt_out, sizeof(pkt_out));
224 	if (ret < 0) {
225 		dev_warn(&dev->dev, "could not send state packet (%d)\n", ret);
226 		return ret;
227 	}
228 
229 	ret = snd_usb_ctl_msg(dev, usb_rcvctrlpipe(dev, 0),
230 			      SC1810C_GET_STATE_REQ,
231 			      SC1810C_GET_STATE_REQTYPE,
232 			      (*seqnum), 0, &pkt_in, sizeof(pkt_in));
233 	if (ret < 0) {
234 		dev_warn(&dev->dev, "could not get state field %u (%d)\n",
235 			 field_idx, ret);
236 		return ret;
237 	}
238 
239 	(*field) = __le32_to_cpu(pkt_in.fields[field_idx]);
240 	(*seqnum)++;
241 	return 0;
242 }
243 
244 /*
245  * This is what I got when bypassing the mixer with
246  * all channels split. I'm not 100% sure of what's going
247  * on, I could probably clean this up based on my observations
248  * but I prefer to keep the same behavior as the windows driver.
249  */
snd_s1810c_init_mixer_maps(struct snd_usb_audio * chip)250 static int snd_s1810c_init_mixer_maps(struct snd_usb_audio *chip)
251 {
252 	u32 a, b, c, e, n, off, left, right;
253 	struct usb_device *dev = chip->dev;
254 
255 	switch (chip->usb_id) {
256 	case USB_ID(0x194f, 0x010c): /* 1810c */
257 		/* Set initial volume levels ? */
258 		a = SC1810C_SEL_MIXER;
259 		e = MIXER_LEVEL_N99DB;
260 		for (n = 0; n < 2; n++) {
261 			off = n * 18;
262 			for (b = off; b < 18 + off; b++) {
263 				/* This channel to all outputs ? */
264 				for (c = 0; c <= 8; c++) {
265 					snd_s1810c_send_ctl_packet(dev, a, b, c, 0, e);
266 					snd_s1810c_send_ctl_packet(dev, a, b, c, 1, e);
267 				}
268 				/* This channel to main output (again) */
269 				snd_s1810c_send_ctl_packet(dev, a, b, 0, 0, e);
270 				snd_s1810c_send_ctl_packet(dev, a, b, 0, 1, e);
271 			}
272 			/*
273 			 * I noticed on UC that DAW channels have different
274 			 * initial volumes, so this makes sense.
275 			 */
276 			e = MIXER_LEVEL_N3DB;
277 		}
278 
279 		/* Connect analog outputs ? */
280 		a = SC1810C_SEL_OUTPUT;
281 		for (b = 1; b < 3; b++) {
282 			snd_s1810c_send_ctl_packet(dev, a, b, 0, 0, MIXER_LEVEL_0DB);
283 			snd_s1810c_send_ctl_packet(dev, a, b, 0, 1, MIXER_LEVEL_0DB);
284 		}
285 		snd_s1810c_send_ctl_packet(dev, a, 0, 0, 0, MIXER_LEVEL_0DB);
286 		snd_s1810c_send_ctl_packet(dev, a, 0, 0, 1, MIXER_LEVEL_0DB);
287 
288 		/* Set initial volume levels for S/PDIF mappings ? */
289 		a = SC1810C_SEL_MIXER;
290 		e = MIXER_LEVEL_N99DB;
291 		c = 3;
292 		for (n = 0; n < 2; n++) {
293 			off = n * 18;
294 			for (b = off; b < 18 + off; b++) {
295 				snd_s1810c_send_ctl_packet(dev, a, b, c, 0, e);
296 				snd_s1810c_send_ctl_packet(dev, a, b, c, 1, e);
297 			}
298 			e = MIXER_LEVEL_N3DB;
299 		}
300 
301 		/* Connect S/PDIF output ? */
302 		a = SC1810C_SEL_OUTPUT;
303 		snd_s1810c_send_ctl_packet(dev, a, 3, 0, 0, MIXER_LEVEL_0DB);
304 		snd_s1810c_send_ctl_packet(dev, a, 3, 0, 1, MIXER_LEVEL_0DB);
305 
306 		/* Connect all outputs (again) ? */
307 		a = SC1810C_SEL_OUTPUT;
308 		for (b = 0; b < 4; b++) {
309 			snd_s1810c_send_ctl_packet(dev, a, b, 0, 0, MIXER_LEVEL_0DB);
310 			snd_s1810c_send_ctl_packet(dev, a, b, 0, 1, MIXER_LEVEL_0DB);
311 		}
312 
313 		/* Basic routing to get sound out of the device */
314 		a = SC1810C_SEL_MIXER;
315 		for (c = 0; c < 4; c++) {
316 			for (b = 0; b < 36; b++) {
317 				if ((c == 0 && b == 18) ||	/* DAW1/2 -> Main */
318 					(c == 1 && b == 20) ||	/* DAW3/4 -> Line3/4 */
319 					(c == 2 && b == 22) ||	/* DAW4/5 -> Line5/6 */
320 					(c == 3 && b == 24)) {	/* DAW5/6 -> S/PDIF */
321 					/* Left */
322 					snd_s1810c_send_ctl_packet(dev,
323 							a, b, c, 0, MIXER_LEVEL_0DB);
324 					snd_s1810c_send_ctl_packet(dev,
325 							a, b, c, 1, MIXER_LEVEL_MUTE);
326 					b++;
327 					/* Right */
328 					snd_s1810c_send_ctl_packet(dev,
329 							a, b, c, 0, MIXER_LEVEL_MUTE);
330 					snd_s1810c_send_ctl_packet(dev,
331 							a, b, c, 1, MIXER_LEVEL_0DB);
332 				} else {
333 					/* Leave the rest disconnected */
334 					snd_s1810c_send_ctl_packet(dev,
335 							a, b, c, 0, MIXER_LEVEL_MUTE);
336 					snd_s1810c_send_ctl_packet(dev,
337 							a, b, c, 1, MIXER_LEVEL_MUTE);
338 				}
339 			}
340 		}
341 
342 		/* Set initial volume levels for S/PDIF (again) ? */
343 		a = SC1810C_SEL_MIXER;
344 		e = MIXER_LEVEL_N99DB;
345 		c = 3;
346 		for (n = 0; n < 2; n++) {
347 			off = n * 18;
348 			for (b = off; b < 18 + off; b++) {
349 				snd_s1810c_send_ctl_packet(dev, a, b, c, 0, e);
350 				snd_s1810c_send_ctl_packet(dev, a, b, c, 1, e);
351 			}
352 			e = MIXER_LEVEL_N3DB;
353 		}
354 
355 		/* Connect S/PDIF outputs (again) ? */
356 		a = SC1810C_SEL_OUTPUT;
357 		snd_s1810c_send_ctl_packet(dev, a, 3, 0, 0, MIXER_LEVEL_0DB);
358 		snd_s1810c_send_ctl_packet(dev, a, 3, 0, 1, MIXER_LEVEL_0DB);
359 
360 		/* Again ? */
361 		snd_s1810c_send_ctl_packet(dev, a, 3, 0, 0, MIXER_LEVEL_0DB);
362 		snd_s1810c_send_ctl_packet(dev, a, 3, 0, 1, MIXER_LEVEL_0DB);
363 		break;
364 
365 	case USB_ID(0x194f, 0x0107): /* 1824 */
366 	case USB_ID(0x194f, 0x010d): /* 1824c */
367 		/* Set all output faders to unity gain */
368 		a = SC1810C_SEL_OUTPUT;
369 		c = 0x00;
370 		for (b = 0; b < 9; b++) {
371 			snd_s1810c_send_ctl_packet(dev, a, b, c, 0, MIXER_LEVEL_0DB);
372 			snd_s1810c_send_ctl_packet(dev, a, b, c, 1, MIXER_LEVEL_0DB);
373 		}
374 
375 		/* Set
376 		 * Daw 1 -> Line out 1 (left), Daw 2 -> Line out 2 (right)
377 		 * Daw 3 -> Line out 3, (left) Daw 4 -> Line out 4 (right)
378 		 * Daw 5 -> Line out 5, (left) Daw 6 -> Line out 6 (right)
379 		 * Daw 7 -> Line out 7, (left) Daw 8 -> Line out 8 (right)
380 		 * Daw 9 -> SPDIF out 1, (left) Daw 10 -> SPDIF out 2 (right)
381 		 * Daw 11 -> ADAT out 1, (left) Daw 12 -> ADAT out 2 (right)
382 		 * Daw 13 -> ADAT out 3, (left) Daw 14 -> ADAT out 4 (right)
383 		 * Daw 15 -> ADAT out 5, (left) Daw 16 -> ADAT out 6 (right)
384 		 * Daw 17 -> ADAT out 7, (left) Daw 18 -> ADAT out 8 (right)
385 		 * Everything else muted
386 		 */
387 		a = SC1810C_SEL_MIXER;
388 		/* The first Daw channel is channel 18 */
389 		left = 18;
390 
391 		for (c = 0; c < 9; c++) {
392 			right = left + 1;
393 
394 			for (b = 0; b < 36; b++) {
395 				if (b == left) {
396 					snd_s1810c_send_ctl_packet(dev,
397 							a, b, c, 0, MIXER_LEVEL_0DB);
398 					snd_s1810c_send_ctl_packet(dev,
399 							a, b, c, 1, MIXER_LEVEL_MUTE);
400 				} else if (b == right) {
401 					snd_s1810c_send_ctl_packet(dev,
402 							a, b, c, 0, MIXER_LEVEL_MUTE);
403 					snd_s1810c_send_ctl_packet(dev,
404 							a, b, c, 1, MIXER_LEVEL_0DB);
405 				} else {
406 					snd_s1810c_send_ctl_packet(dev,
407 							a, b, c, 0, MIXER_LEVEL_MUTE);
408 					snd_s1810c_send_ctl_packet(dev,
409 							a, b, c, 1, MIXER_LEVEL_MUTE);
410 				}
411 			}
412 			left += 2;
413 		}
414 		break;
415 	}
416 	return 0;
417 }
418 
419 /*
420  * Sync state with the device and retrieve the requested field,
421  * whose index is specified in (kctl->private_value & 0xFF),
422  * from the received fields array.
423  */
424 static int
snd_s1810c_get_switch_state(struct usb_mixer_interface * mixer,struct snd_kcontrol * kctl,u32 * state)425 snd_s1810c_get_switch_state(struct usb_mixer_interface *mixer,
426 			    struct snd_kcontrol *kctl, u32 *state)
427 {
428 	struct snd_usb_audio *chip = mixer->chip;
429 	struct s1810_mixer_state *private = mixer->private_data;
430 	u32 field = 0;
431 	u32 ctl_idx = (u32) (kctl->private_value & 0xFF);
432 	int ret;
433 
434 	guard(mutex)(&private->usb_mutex);
435 	ret = snd_sc1810c_get_status_field(chip->dev, &field,
436 					   ctl_idx, &private->seqnum);
437 	if (ret < 0)
438 		return ret;
439 
440 	*state = field;
441 	return ret;
442 }
443 
444 /*
445  * Send a control packet to the device for the control id
446  * specified in (kctl->private_value >> 8) with value
447  * specified in (kctl->private_value >> 16).
448  */
449 static int
snd_s1810c_set_switch_state(struct usb_mixer_interface * mixer,struct snd_kcontrol * kctl)450 snd_s1810c_set_switch_state(struct usb_mixer_interface *mixer,
451 			    struct snd_kcontrol *kctl)
452 {
453 	struct snd_usb_audio *chip = mixer->chip;
454 	struct s1810_mixer_state *private = mixer->private_data;
455 	u32 pval = (u32) kctl->private_value;
456 	u32 ctl_id = (pval >> 8) & 0xFF;
457 	u32 ctl_val = (pval >> 16) & 0x1;
458 
459 	guard(mutex)(&private->usb_mutex);
460 	return snd_s1810c_send_ctl_packet(chip->dev, 0, 0, 0, ctl_id, ctl_val);
461 }
462 
463 /* Generic get/set/init functions for switch controls */
464 
465 static int
snd_s1810c_switch_get(struct snd_kcontrol * kctl,struct snd_ctl_elem_value * ctl_elem)466 snd_s1810c_switch_get(struct snd_kcontrol *kctl,
467 		      struct snd_ctl_elem_value *ctl_elem)
468 {
469 	struct usb_mixer_elem_list *list = snd_kcontrol_chip(kctl);
470 	struct usb_mixer_interface *mixer = list->mixer;
471 	struct s1810_mixer_state *private = mixer->private_data;
472 	u32 pval = (u32) kctl->private_value;
473 	u32 ctl_idx = pval & 0xFF;
474 	u32 state = 0;
475 	int ret;
476 
477 	guard(mutex)(&private->data_mutex);
478 	ret = snd_s1810c_get_switch_state(mixer, kctl, &state);
479 	if (ret < 0)
480 		return ret;
481 
482 	switch (ctl_idx) {
483 	case SC1810C_STATE_LINE_SW:
484 	case SC1810C_STATE_AB_SW:
485 		ctl_elem->value.enumerated.item[0] = (int)state;
486 		break;
487 	default:
488 		ctl_elem->value.integer.value[0] = (long)state;
489 	}
490 
491 	return 0;
492 }
493 
494 static int
snd_s1810c_switch_set(struct snd_kcontrol * kctl,struct snd_ctl_elem_value * ctl_elem)495 snd_s1810c_switch_set(struct snd_kcontrol *kctl,
496 		      struct snd_ctl_elem_value *ctl_elem)
497 {
498 	struct usb_mixer_elem_list *list = snd_kcontrol_chip(kctl);
499 	struct usb_mixer_interface *mixer = list->mixer;
500 	struct s1810_mixer_state *private = mixer->private_data;
501 	u32 pval = (u32) kctl->private_value;
502 	u32 ctl_idx = pval & 0xFF;
503 	u32 curval = 0;
504 	u32 newval = 0;
505 	int ret = 0;
506 
507 	guard(mutex)(&private->data_mutex);
508 	ret = snd_s1810c_get_switch_state(mixer, kctl, &curval);
509 	if (ret < 0)
510 		return ret;
511 
512 	switch (ctl_idx) {
513 	case SC1810C_STATE_LINE_SW:
514 	case SC1810C_STATE_AB_SW:
515 		newval = (u32) ctl_elem->value.enumerated.item[0];
516 		break;
517 	default:
518 		newval = (u32) ctl_elem->value.integer.value[0];
519 	}
520 
521 	if (curval == newval)
522 		return 0;
523 
524 	kctl->private_value &= ~(0x1 << 16);
525 	kctl->private_value |= (unsigned int)(newval & 0x1) << 16;
526 	ret = snd_s1810c_set_switch_state(mixer, kctl);
527 
528 	return (ret < 0) ? 0 : 1;
529 }
530 
531 static int
snd_s1810c_switch_init(struct usb_mixer_interface * mixer,const struct snd_kcontrol_new * new_kctl)532 snd_s1810c_switch_init(struct usb_mixer_interface *mixer,
533 		       const struct snd_kcontrol_new *new_kctl)
534 {
535 	struct snd_kcontrol *kctl;
536 	struct usb_mixer_elem_info *elem;
537 
538 	elem = kzalloc_obj(struct usb_mixer_elem_info);
539 	if (!elem)
540 		return -ENOMEM;
541 
542 	elem->head.mixer = mixer;
543 	elem->control = 0;
544 	elem->head.id = 0;
545 	elem->channels = 1;
546 
547 	kctl = snd_ctl_new1(new_kctl, elem);
548 	if (!kctl) {
549 		kfree(elem);
550 		return -ENOMEM;
551 	}
552 	kctl->private_free = snd_usb_mixer_elem_free;
553 
554 	return snd_usb_mixer_add_control(&elem->head, kctl);
555 }
556 
557 static int
snd_s1810c_line_sw_info(struct snd_kcontrol * kctl,struct snd_ctl_elem_info * uinfo)558 snd_s1810c_line_sw_info(struct snd_kcontrol *kctl,
559 			struct snd_ctl_elem_info *uinfo)
560 {
561 	static const char *const texts[2] = {
562 		"Preamp On (Mic/Inst)",
563 		"Preamp Off (Line in)"
564 	};
565 
566 	return snd_ctl_enum_info(uinfo, 1, ARRAY_SIZE(texts), texts);
567 }
568 
569 static const struct snd_kcontrol_new snd_s1810c_line_sw = {
570 	.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
571 	.name = "Line 1/2 Source Type",
572 	.info = snd_s1810c_line_sw_info,
573 	.get = snd_s1810c_switch_get,
574 	.put = snd_s1810c_switch_set,
575 	.private_value = (SC1810C_STATE_LINE_SW | SC1810C_CTL_LINE_SW << 8)
576 };
577 
578 static const struct snd_kcontrol_new snd_s1810c_mute_sw = {
579 	.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
580 	.name = "Mute Main Out Switch",
581 	.info = snd_ctl_boolean_mono_info,
582 	.get = snd_s1810c_switch_get,
583 	.put = snd_s1810c_switch_set,
584 	.private_value = (SC1810C_STATE_MUTE_SW | SC1810C_CTL_MUTE_SW << 8)
585 };
586 
587 static const struct snd_kcontrol_new snd_s1824c_mono_sw = {
588 	.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
589 	.name = "Mono Main Out Switch",
590 	.info = snd_ctl_boolean_mono_info,
591 	.get = snd_s1810c_switch_get,
592 	.put = snd_s1810c_switch_set,
593 	.private_value = (SC1824C_STATE_MONO_SW | SC1824C_CTL_MONO_SW << 8)
594 };
595 
596 static const struct snd_kcontrol_new snd_s1810c_48v_sw = {
597 	.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
598 	.name = "48V Phantom Power On Mic Inputs Switch",
599 	.info = snd_ctl_boolean_mono_info,
600 	.get = snd_s1810c_switch_get,
601 	.put = snd_s1810c_switch_set,
602 	.private_value = (SC1810C_STATE_48V_SW | SC1810C_CTL_48V_SW << 8)
603 };
604 
605 static int
snd_s1810c_ab_sw_info(struct snd_kcontrol * kctl,struct snd_ctl_elem_info * uinfo)606 snd_s1810c_ab_sw_info(struct snd_kcontrol *kctl,
607 		      struct snd_ctl_elem_info *uinfo)
608 {
609 	static const char *const texts[2] = {
610 		"1/2",
611 		"3/4"
612 	};
613 
614 	return snd_ctl_enum_info(uinfo, 1, ARRAY_SIZE(texts), texts);
615 }
616 
617 static const struct snd_kcontrol_new snd_s1810c_ab_sw = {
618 	.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
619 	.name = "Headphone 1 Source Route",
620 	.info = snd_s1810c_ab_sw_info,
621 	.get = snd_s1810c_switch_get,
622 	.put = snd_s1810c_switch_set,
623 	.private_value = (SC1810C_STATE_AB_SW | SC1810C_CTL_AB_SW << 8)
624 };
625 
snd_sc1810_mixer_state_free(struct usb_mixer_interface * mixer)626 static void snd_sc1810_mixer_state_free(struct usb_mixer_interface *mixer)
627 {
628 	struct s1810_mixer_state *private = mixer->private_data;
629 	kfree(private);
630 	mixer->private_data = NULL;
631 }
632 
633 /* Entry point, called from mixer_quirks.c */
snd_sc1810_init_mixer(struct usb_mixer_interface * mixer)634 int snd_sc1810_init_mixer(struct usb_mixer_interface *mixer)
635 {
636 	struct s1810_mixer_state *private = NULL;
637 	struct snd_usb_audio *chip = mixer->chip;
638 	struct usb_device *dev = chip->dev;
639 	int ret = 0;
640 
641 	/* Run this only once */
642 	if (!list_empty(&chip->mixer_list))
643 		return 0;
644 
645 	ret = snd_s1810c_init_mixer_maps(chip);
646 	if (ret < 0)
647 		return ret;
648 
649 	private = kzalloc_obj(struct s1810_mixer_state);
650 	if (!private)
651 		return -ENOMEM;
652 
653 	mutex_init(&private->usb_mutex);
654 	mutex_init(&private->data_mutex);
655 
656 	mixer->private_data = private;
657 	mixer->private_free = snd_sc1810_mixer_state_free;
658 
659 	private->seqnum = 1;
660 
661 	ret = snd_s1810c_switch_init(mixer, &snd_s1810c_line_sw);
662 	if (ret < 0)
663 		return ret;
664 
665 	ret = snd_s1810c_switch_init(mixer, &snd_s1810c_mute_sw);
666 	if (ret < 0)
667 		return ret;
668 
669 	ret = snd_s1810c_switch_init(mixer, &snd_s1810c_48v_sw);
670 	if (ret < 0)
671 		return ret;
672 
673 	switch (chip->usb_id) {
674 	case USB_ID(0x194f, 0x010c): /* Presonus Studio 1810c */
675 		dev_info(&dev->dev,
676 			 "Presonus Studio 1810c, device_setup: %u\n", chip->setup);
677 		if (chip->setup == 1)
678 			dev_info(&dev->dev, "(8out/18in @ 48kHz)\n");
679 		else if (chip->setup == 2)
680 			dev_info(&dev->dev, "(6out/8in @ 192kHz)\n");
681 		else
682 			dev_info(&dev->dev, "(8out/14in @ 96kHz)\n");
683 
684 		ret = snd_s1810c_switch_init(mixer, &snd_s1810c_ab_sw);
685 		if (ret < 0)
686 			return ret;
687 
688 		break;
689 	case USB_ID(0x194f, 0x0107): /* Presonus Studio 1824 */
690 	case USB_ID(0x194f, 0x010d): /* Presonus Studio 1824c */
691 		ret = snd_s1810c_switch_init(mixer, &snd_s1824c_mono_sw);
692 		if (ret < 0)
693 			return ret;
694 
695 		break;
696 	}
697 
698 	return ret;
699 }
700