1 // SPDX-License-Identifier: GPL-2.0
3 * Presonus Studio 1810c driver for ALSA
4 * Copyright (C) 2019 Nick Kossifidis <mickflemm@gmail.com>
6 * Based on reverse engineering of the communication protocol
7 * between the windows driver / Univeral Control (UC) program
8 * and the device, through usbmon.
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
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>
24 #include "mixer_quirks.h"
26 #include "mixer_s1810c.h"
28 #define SC1810C_CMD_REQ 160
29 #define SC1810C_CMD_REQTYPE \
30 (USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_OUT)
31 #define SC1810C_CMD_F1 0x50617269
32 #define SC1810C_CMD_F2 0x14
35 * DISCLAIMER: These are just guesses based on the
38 * It seems like a selects between
39 * device (0), mixer (0x64) and output (0x65)
42 * * b selects an input channel (see below).
43 * * c selects an output channel pair (see below).
44 * * d selects left (0) or right (1) of that pair.
45 * * e 0-> disconnect, 0x01000000-> connect,
46 * 0x0109-> used for stereo-linking channels,
47 * e is also used for setting volume levels
48 * in which case b is also set so I guess
49 * this way it is possible to set the volume
50 * level from the specified input to the
54 * 0 - 7 Mic/Inst/Line (Analog inputs)
57 * 18 - 35 DAW (Inputs from the host)
59 * OUT Channels (pairs):
67 * * b and c are not used, at least not on the
69 * * d sets the control id to be modified
71 * * e sets the setting for that control.
72 * (so for the switches I was interested
76 * * b is the output channel (see above).
78 * * e I guess the same as with mixer except 0x0109
79 * which I didn't see in my dumps.
81 * The two fixed fields have the same values for
82 * mixer and output but a different set for device.
84 struct s1810c_ctl_packet {
94 #define SC1810C_CTL_LINE_SW 0
95 #define SC1810C_CTL_MUTE_SW 1
96 #define SC1810C_CTL_AB_SW 3
97 #define SC1810C_CTL_48V_SW 4
99 #define SC1810C_SET_STATE_REQ 161
100 #define SC1810C_SET_STATE_REQTYPE SC1810C_CMD_REQTYPE
101 #define SC1810C_SET_STATE_F1 0x64656D73
102 #define SC1810C_SET_STATE_F2 0xF4
104 #define SC1810C_GET_STATE_REQ 162
105 #define SC1810C_GET_STATE_REQTYPE \
106 (USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN)
107 #define SC1810C_GET_STATE_F1 SC1810C_SET_STATE_F1
108 #define SC1810C_GET_STATE_F2 SC1810C_SET_STATE_F2
110 #define SC1810C_STATE_F1_IDX 2
111 #define SC1810C_STATE_F2_IDX 3
114 * This packet includes mixer volumes and
115 * various other fields, it's an extended
116 * version of ctl_packet, with a and b
117 * being zero and different f1/f2.
119 struct s1810c_state_packet {
123 #define SC1810C_STATE_48V_SW 58
124 #define SC1810C_STATE_LINE_SW 59
125 #define SC1810C_STATE_MUTE_SW 60
126 #define SC1810C_STATE_AB_SW 62
128 struct s1810_mixer_state {
130 struct mutex usb_mutex;
131 struct mutex data_mutex;
135 snd_s1810c_send_ctl_packet(struct usb_device *dev, u32 a,
136 u32 b, u32 c, u32 d, u32 e)
138 struct s1810c_ctl_packet pkt = { 0 };
141 pkt.fixed1 = SC1810C_CMD_F1;
142 pkt.fixed2 = SC1810C_CMD_F2;
149 * Value for settings 0/1 for this
150 * output channel is always 0 (probably because
151 * there is no ADAT output on 1810c)
153 pkt.e = (c == 4) ? 0 : e;
155 ret = snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0),
157 SC1810C_CMD_REQTYPE, 0, 0, &pkt, sizeof(pkt));
159 dev_warn(&dev->dev, "could not send ctl packet\n");
166 * When opening Universal Control the program periodically
167 * sends and receives state packets for syncinc state between
168 * the device and the host.
170 * Note that if we send only the request to get data back we'll
171 * get an error, we need to first send an empty state packet and
172 * then ask to receive a filled. Their seqnumbers must also match.
175 snd_sc1810c_get_status_field(struct usb_device *dev,
176 u32 *field, int field_idx, uint16_t *seqnum)
178 struct s1810c_state_packet pkt_out = { { 0 } };
179 struct s1810c_state_packet pkt_in = { { 0 } };
182 pkt_out.fields[SC1810C_STATE_F1_IDX] = SC1810C_SET_STATE_F1;
183 pkt_out.fields[SC1810C_STATE_F2_IDX] = SC1810C_SET_STATE_F2;
184 ret = snd_usb_ctl_msg(dev, usb_rcvctrlpipe(dev, 0),
185 SC1810C_SET_STATE_REQ,
186 SC1810C_SET_STATE_REQTYPE,
187 (*seqnum), 0, &pkt_out, sizeof(pkt_out));
189 dev_warn(&dev->dev, "could not send state packet (%d)\n", ret);
193 ret = snd_usb_ctl_msg(dev, usb_rcvctrlpipe(dev, 0),
194 SC1810C_GET_STATE_REQ,
195 SC1810C_GET_STATE_REQTYPE,
196 (*seqnum), 0, &pkt_in, sizeof(pkt_in));
198 dev_warn(&dev->dev, "could not get state field %u (%d)\n",
203 (*field) = pkt_in.fields[field_idx];
209 * This is what I got when bypassing the mixer with
210 * all channels split. I'm not 100% sure of what's going
211 * on, I could probably clean this up based on my observations
212 * but I prefer to keep the same behavior as the windows driver.
214 static int snd_s1810c_init_mixer_maps(struct snd_usb_audio *chip)
216 u32 a, b, c, e, n, off;
217 struct usb_device *dev = chip->dev;
219 /* Set initial volume levels ? */
222 for (n = 0; n < 2; n++) {
224 for (b = off; b < 18 + off; b++) {
225 /* This channel to all outputs ? */
226 for (c = 0; c <= 8; c++) {
227 snd_s1810c_send_ctl_packet(dev, a, b, c, 0, e);
228 snd_s1810c_send_ctl_packet(dev, a, b, c, 1, e);
230 /* This channel to main output (again) */
231 snd_s1810c_send_ctl_packet(dev, a, b, 0, 0, e);
232 snd_s1810c_send_ctl_packet(dev, a, b, 0, 1, e);
235 * I noticed on UC that DAW channels have different
236 * initial volumes, so this makes sense.
241 /* Connect analog outputs ? */
244 for (b = 1; b < 3; b++) {
245 snd_s1810c_send_ctl_packet(dev, a, b, 0, 0, e);
246 snd_s1810c_send_ctl_packet(dev, a, b, 0, 1, e);
248 snd_s1810c_send_ctl_packet(dev, a, 0, 0, 0, e);
249 snd_s1810c_send_ctl_packet(dev, a, 0, 0, 1, e);
251 /* Set initial volume levels for S/PDIF mappings ? */
255 for (n = 0; n < 2; n++) {
257 for (b = off; b < 18 + off; b++) {
258 snd_s1810c_send_ctl_packet(dev, a, b, c, 0, e);
259 snd_s1810c_send_ctl_packet(dev, a, b, c, 1, e);
264 /* Connect S/PDIF output ? */
267 snd_s1810c_send_ctl_packet(dev, a, 3, 0, 0, e);
268 snd_s1810c_send_ctl_packet(dev, a, 3, 0, 1, e);
270 /* Connect all outputs (again) ? */
273 for (b = 0; b < 4; b++) {
274 snd_s1810c_send_ctl_packet(dev, a, b, 0, 0, e);
275 snd_s1810c_send_ctl_packet(dev, a, b, 0, 1, e);
278 /* Basic routing to get sound out of the device */
281 for (c = 0; c < 4; c++) {
282 for (b = 0; b < 36; b++) {
283 if ((c == 0 && b == 18) || /* DAW1/2 -> Main */
284 (c == 1 && b == 20) || /* DAW3/4 -> Line3/4 */
285 (c == 2 && b == 22) || /* DAW4/5 -> Line5/6 */
286 (c == 3 && b == 24)) { /* DAW5/6 -> S/PDIF */
288 snd_s1810c_send_ctl_packet(dev, a, b, c, 0, e);
289 snd_s1810c_send_ctl_packet(dev, a, b, c, 1, 0);
292 snd_s1810c_send_ctl_packet(dev, a, b, c, 0, 0);
293 snd_s1810c_send_ctl_packet(dev, a, b, c, 1, e);
295 /* Leave the rest disconnected */
296 snd_s1810c_send_ctl_packet(dev, a, b, c, 0, 0);
297 snd_s1810c_send_ctl_packet(dev, a, b, c, 1, 0);
302 /* Set initial volume levels for S/PDIF (again) ? */
306 for (n = 0; n < 2; n++) {
308 for (b = off; b < 18 + off; b++) {
309 snd_s1810c_send_ctl_packet(dev, a, b, c, 0, e);
310 snd_s1810c_send_ctl_packet(dev, a, b, c, 1, e);
315 /* Connect S/PDIF outputs (again) ? */
318 snd_s1810c_send_ctl_packet(dev, a, 3, 0, 0, e);
319 snd_s1810c_send_ctl_packet(dev, a, 3, 0, 1, e);
322 snd_s1810c_send_ctl_packet(dev, a, 3, 0, 0, e);
323 snd_s1810c_send_ctl_packet(dev, a, 3, 0, 1, e);
329 * Sync state with the device and retrieve the requested field,
330 * whose index is specified in (kctl->private_value & 0xFF),
331 * from the received fields array.
334 snd_s1810c_get_switch_state(struct usb_mixer_interface *mixer,
335 struct snd_kcontrol *kctl, u32 *state)
337 struct snd_usb_audio *chip = mixer->chip;
338 struct s1810_mixer_state *private = mixer->private_data;
340 u32 ctl_idx = (u32) (kctl->private_value & 0xFF);
343 mutex_lock(&private->usb_mutex);
344 ret = snd_sc1810c_get_status_field(chip->dev, &field,
345 ctl_idx, &private->seqnum);
351 mutex_unlock(&private->usb_mutex);
352 return ret ? ret : 0;
356 * Send a control packet to the device for the control id
357 * specified in (kctl->private_value >> 8) with value
358 * specified in (kctl->private_value >> 16).
361 snd_s1810c_set_switch_state(struct usb_mixer_interface *mixer,
362 struct snd_kcontrol *kctl)
364 struct snd_usb_audio *chip = mixer->chip;
365 struct s1810_mixer_state *private = mixer->private_data;
366 u32 pval = (u32) kctl->private_value;
367 u32 ctl_id = (pval >> 8) & 0xFF;
368 u32 ctl_val = (pval >> 16) & 0x1;
371 mutex_lock(&private->usb_mutex);
372 ret = snd_s1810c_send_ctl_packet(chip->dev, 0, 0, 0, ctl_id, ctl_val);
373 mutex_unlock(&private->usb_mutex);
377 /* Generic get/set/init functions for switch controls */
380 snd_s1810c_switch_get(struct snd_kcontrol *kctl,
381 struct snd_ctl_elem_value *ctl_elem)
383 struct usb_mixer_elem_list *list = snd_kcontrol_chip(kctl);
384 struct usb_mixer_interface *mixer = list->mixer;
385 struct s1810_mixer_state *private = mixer->private_data;
386 u32 pval = (u32) kctl->private_value;
387 u32 ctl_idx = pval & 0xFF;
391 mutex_lock(&private->data_mutex);
392 ret = snd_s1810c_get_switch_state(mixer, kctl, &state);
397 case SC1810C_STATE_LINE_SW:
398 case SC1810C_STATE_AB_SW:
399 ctl_elem->value.enumerated.item[0] = (int)state;
402 ctl_elem->value.integer.value[0] = (long)state;
406 mutex_unlock(&private->data_mutex);
407 return (ret < 0) ? ret : 0;
411 snd_s1810c_switch_set(struct snd_kcontrol *kctl,
412 struct snd_ctl_elem_value *ctl_elem)
414 struct usb_mixer_elem_list *list = snd_kcontrol_chip(kctl);
415 struct usb_mixer_interface *mixer = list->mixer;
416 struct s1810_mixer_state *private = mixer->private_data;
417 u32 pval = (u32) kctl->private_value;
418 u32 ctl_idx = pval & 0xFF;
423 mutex_lock(&private->data_mutex);
424 ret = snd_s1810c_get_switch_state(mixer, kctl, &curval);
429 case SC1810C_STATE_LINE_SW:
430 case SC1810C_STATE_AB_SW:
431 newval = (u32) ctl_elem->value.enumerated.item[0];
434 newval = (u32) ctl_elem->value.integer.value[0];
437 if (curval == newval)
440 kctl->private_value &= ~(0x1 << 16);
441 kctl->private_value |= (unsigned int)(newval & 0x1) << 16;
442 ret = snd_s1810c_set_switch_state(mixer, kctl);
445 mutex_unlock(&private->data_mutex);
446 return (ret < 0) ? 0 : 1;
450 snd_s1810c_switch_init(struct usb_mixer_interface *mixer,
451 const struct snd_kcontrol_new *new_kctl)
453 struct snd_kcontrol *kctl;
454 struct usb_mixer_elem_info *elem;
456 elem = kzalloc(sizeof(struct usb_mixer_elem_info), GFP_KERNEL);
460 elem->head.mixer = mixer;
465 kctl = snd_ctl_new1(new_kctl, elem);
470 kctl->private_free = snd_usb_mixer_elem_free;
472 return snd_usb_mixer_add_control(&elem->head, kctl);
476 snd_s1810c_line_sw_info(struct snd_kcontrol *kctl,
477 struct snd_ctl_elem_info *uinfo)
479 static const char *const texts[2] = {
480 "Preamp On (Mic/Inst)",
481 "Preamp Off (Line in)"
484 return snd_ctl_enum_info(uinfo, 1, ARRAY_SIZE(texts), texts);
487 static const struct snd_kcontrol_new snd_s1810c_line_sw = {
488 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
489 .name = "Line 1/2 Source Type",
490 .info = snd_s1810c_line_sw_info,
491 .get = snd_s1810c_switch_get,
492 .put = snd_s1810c_switch_set,
493 .private_value = (SC1810C_STATE_LINE_SW | SC1810C_CTL_LINE_SW << 8)
496 static const struct snd_kcontrol_new snd_s1810c_mute_sw = {
497 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
498 .name = "Mute Main Out Switch",
499 .info = snd_ctl_boolean_mono_info,
500 .get = snd_s1810c_switch_get,
501 .put = snd_s1810c_switch_set,
502 .private_value = (SC1810C_STATE_MUTE_SW | SC1810C_CTL_MUTE_SW << 8)
505 static const struct snd_kcontrol_new snd_s1810c_48v_sw = {
506 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
507 .name = "48V Phantom Power On Mic Inputs Switch",
508 .info = snd_ctl_boolean_mono_info,
509 .get = snd_s1810c_switch_get,
510 .put = snd_s1810c_switch_set,
511 .private_value = (SC1810C_STATE_48V_SW | SC1810C_CTL_48V_SW << 8)
515 snd_s1810c_ab_sw_info(struct snd_kcontrol *kctl,
516 struct snd_ctl_elem_info *uinfo)
518 static const char *const texts[2] = {
523 return snd_ctl_enum_info(uinfo, 1, ARRAY_SIZE(texts), texts);
526 static const struct snd_kcontrol_new snd_s1810c_ab_sw = {
527 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
528 .name = "Headphone 1 Source Route",
529 .info = snd_s1810c_ab_sw_info,
530 .get = snd_s1810c_switch_get,
531 .put = snd_s1810c_switch_set,
532 .private_value = (SC1810C_STATE_AB_SW | SC1810C_CTL_AB_SW << 8)
535 static void snd_sc1810_mixer_state_free(struct usb_mixer_interface *mixer)
537 struct s1810_mixer_state *private = mixer->private_data;
539 mixer->private_data = NULL;
542 /* Entry point, called from mixer_quirks.c */
543 int snd_sc1810_init_mixer(struct usb_mixer_interface *mixer)
545 struct s1810_mixer_state *private = NULL;
546 struct snd_usb_audio *chip = mixer->chip;
547 struct usb_device *dev = chip->dev;
550 /* Run this only once */
551 if (!list_empty(&chip->mixer_list))
555 "Presonus Studio 1810c, device_setup: %u\n", chip->setup);
556 if (chip->setup == 1)
557 dev_info(&dev->dev, "(8out/18in @ 48kHz)\n");
558 else if (chip->setup == 2)
559 dev_info(&dev->dev, "(6out/8in @ 192kHz)\n");
561 dev_info(&dev->dev, "(8out/14in @ 96kHz)\n");
563 ret = snd_s1810c_init_mixer_maps(chip);
567 private = kzalloc(sizeof(struct s1810_mixer_state), GFP_KERNEL);
571 mutex_init(&private->usb_mutex);
572 mutex_init(&private->data_mutex);
574 mixer->private_data = private;
575 mixer->private_free = snd_sc1810_mixer_state_free;
579 ret = snd_s1810c_switch_init(mixer, &snd_s1810c_line_sw);
583 ret = snd_s1810c_switch_init(mixer, &snd_s1810c_mute_sw);
587 ret = snd_s1810c_switch_init(mixer, &snd_s1810c_48v_sw);
591 ret = snd_s1810c_switch_init(mixer, &snd_s1810c_ab_sw);