Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /*
0003  * Presonus Studio 1810c driver for ALSA
0004  * Copyright (C) 2019 Nick Kossifidis <mickflemm@gmail.com>
0005  *
0006  * Based on reverse engineering of the communication protocol
0007  * between the windows driver / Univeral Control (UC) program
0008  * and the device, through usbmon.
0009  *
0010  * For now this bypasses the mixer, with all channels split,
0011  * so that the software can mix with greater flexibility.
0012  * It also adds controls for the 4 buttons on the front of
0013  * the device.
0014  */
0015 
0016 #include <linux/usb.h>
0017 #include <linux/usb/audio-v2.h>
0018 #include <linux/slab.h>
0019 #include <sound/core.h>
0020 #include <sound/control.h>
0021 
0022 #include "usbaudio.h"
0023 #include "mixer.h"
0024 #include "mixer_quirks.h"
0025 #include "helper.h"
0026 #include "mixer_s1810c.h"
0027 
0028 #define SC1810C_CMD_REQ 160
0029 #define SC1810C_CMD_REQTYPE \
0030     (USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_OUT)
0031 #define SC1810C_CMD_F1      0x50617269
0032 #define SC1810C_CMD_F2      0x14
0033 
0034 /*
0035  * DISCLAIMER: These are just guesses based on the
0036  * dumps I got.
0037  *
0038  * It seems like a selects between
0039  * device (0), mixer (0x64) and output (0x65)
0040  *
0041  * For mixer (0x64):
0042  *  * b selects an input channel (see below).
0043  *  * c selects an output channel pair (see below).
0044  *  * d selects left (0) or right (1) of that pair.
0045  *  * e 0-> disconnect, 0x01000000-> connect,
0046  *  0x0109-> used for stereo-linking channels,
0047  *  e is also used for setting volume levels
0048  *  in which case b is also set so I guess
0049  *  this way it is possible to set the volume
0050  *  level from the specified input to the
0051  *  specified output.
0052  *
0053  * IN Channels:
0054  * 0  - 7  Mic/Inst/Line (Analog inputs)
0055  * 8  - 9  S/PDIF
0056  * 10 - 17 ADAT
0057  * 18 - 35 DAW (Inputs from the host)
0058  *
0059  * OUT Channels (pairs):
0060  * 0 -> Main out
0061  * 1 -> Line1/2
0062  * 2 -> Line3/4
0063  * 3 -> S/PDIF
0064  * 4 -> ADAT?
0065  *
0066  * For device (0):
0067  *  * b and c are not used, at least not on the
0068  *    dumps I got.
0069  *  * d sets the control id to be modified
0070  *    (see below).
0071  *  * e sets the setting for that control.
0072  *    (so for the switches I was interested
0073  *    in it's 0/1)
0074  *
0075  * For output (0x65):
0076  *   * b is the output channel (see above).
0077  *   * c is zero.
0078  *   * e I guess the same as with mixer except 0x0109
0079  *   which I didn't see in my dumps.
0080  *
0081  * The two fixed fields have the same values for
0082  * mixer and output but a different set for device.
0083  */
0084 struct s1810c_ctl_packet {
0085     u32 a;
0086     u32 b;
0087     u32 fixed1;
0088     u32 fixed2;
0089     u32 c;
0090     u32 d;
0091     u32 e;
0092 };
0093 
0094 #define SC1810C_CTL_LINE_SW 0
0095 #define SC1810C_CTL_MUTE_SW 1
0096 #define SC1810C_CTL_AB_SW   3
0097 #define SC1810C_CTL_48V_SW  4
0098 
0099 #define SC1810C_SET_STATE_REQ   161
0100 #define SC1810C_SET_STATE_REQTYPE SC1810C_CMD_REQTYPE
0101 #define SC1810C_SET_STATE_F1    0x64656D73
0102 #define SC1810C_SET_STATE_F2    0xF4
0103 
0104 #define SC1810C_GET_STATE_REQ   162
0105 #define SC1810C_GET_STATE_REQTYPE \
0106     (USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN)
0107 #define SC1810C_GET_STATE_F1    SC1810C_SET_STATE_F1
0108 #define SC1810C_GET_STATE_F2    SC1810C_SET_STATE_F2
0109 
0110 #define SC1810C_STATE_F1_IDX    2
0111 #define SC1810C_STATE_F2_IDX    3
0112 
0113 /*
0114  * This packet includes mixer volumes and
0115  * various other fields, it's an extended
0116  * version of ctl_packet, with a and b
0117  * being zero and different f1/f2.
0118  */
0119 struct s1810c_state_packet {
0120     u32 fields[63];
0121 };
0122 
0123 #define SC1810C_STATE_48V_SW    58
0124 #define SC1810C_STATE_LINE_SW   59
0125 #define SC1810C_STATE_MUTE_SW   60
0126 #define SC1810C_STATE_AB_SW 62
0127 
0128 struct s1810_mixer_state {
0129     uint16_t seqnum;
0130     struct mutex usb_mutex;
0131     struct mutex data_mutex;
0132 };
0133 
0134 static int
0135 snd_s1810c_send_ctl_packet(struct usb_device *dev, u32 a,
0136                u32 b, u32 c, u32 d, u32 e)
0137 {
0138     struct s1810c_ctl_packet pkt = { 0 };
0139     int ret = 0;
0140 
0141     pkt.fixed1 = SC1810C_CMD_F1;
0142     pkt.fixed2 = SC1810C_CMD_F2;
0143 
0144     pkt.a = a;
0145     pkt.b = b;
0146     pkt.c = c;
0147     pkt.d = d;
0148     /*
0149      * Value for settings 0/1 for this
0150      * output channel is always 0 (probably because
0151      * there is no ADAT output on 1810c)
0152      */
0153     pkt.e = (c == 4) ? 0 : e;
0154 
0155     ret = snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0),
0156                   SC1810C_CMD_REQ,
0157                   SC1810C_CMD_REQTYPE, 0, 0, &pkt, sizeof(pkt));
0158     if (ret < 0) {
0159         dev_warn(&dev->dev, "could not send ctl packet\n");
0160         return ret;
0161     }
0162     return 0;
0163 }
0164 
0165 /*
0166  * When opening Universal Control the program periodically
0167  * sends and receives state packets for syncinc state between
0168  * the device and the host.
0169  *
0170  * Note that if we send only the request to get data back we'll
0171  * get an error, we need to first send an empty state packet and
0172  * then ask to receive a filled. Their seqnumbers must also match.
0173  */
0174 static int
0175 snd_sc1810c_get_status_field(struct usb_device *dev,
0176                  u32 *field, int field_idx, uint16_t *seqnum)
0177 {
0178     struct s1810c_state_packet pkt_out = { { 0 } };
0179     struct s1810c_state_packet pkt_in = { { 0 } };
0180     int ret = 0;
0181 
0182     pkt_out.fields[SC1810C_STATE_F1_IDX] = SC1810C_SET_STATE_F1;
0183     pkt_out.fields[SC1810C_STATE_F2_IDX] = SC1810C_SET_STATE_F2;
0184     ret = snd_usb_ctl_msg(dev, usb_rcvctrlpipe(dev, 0),
0185                   SC1810C_SET_STATE_REQ,
0186                   SC1810C_SET_STATE_REQTYPE,
0187                   (*seqnum), 0, &pkt_out, sizeof(pkt_out));
0188     if (ret < 0) {
0189         dev_warn(&dev->dev, "could not send state packet (%d)\n", ret);
0190         return ret;
0191     }
0192 
0193     ret = snd_usb_ctl_msg(dev, usb_rcvctrlpipe(dev, 0),
0194                   SC1810C_GET_STATE_REQ,
0195                   SC1810C_GET_STATE_REQTYPE,
0196                   (*seqnum), 0, &pkt_in, sizeof(pkt_in));
0197     if (ret < 0) {
0198         dev_warn(&dev->dev, "could not get state field %u (%d)\n",
0199              field_idx, ret);
0200         return ret;
0201     }
0202 
0203     (*field) = pkt_in.fields[field_idx];
0204     (*seqnum)++;
0205     return 0;
0206 }
0207 
0208 /*
0209  * This is what I got when bypassing the mixer with
0210  * all channels split. I'm not 100% sure of what's going
0211  * on, I could probably clean this up based on my observations
0212  * but I prefer to keep the same behavior as the windows driver.
0213  */
0214 static int snd_s1810c_init_mixer_maps(struct snd_usb_audio *chip)
0215 {
0216     u32 a, b, c, e, n, off;
0217     struct usb_device *dev = chip->dev;
0218 
0219     /* Set initial volume levels ? */
0220     a = 0x64;
0221     e = 0xbc;
0222     for (n = 0; n < 2; n++) {
0223         off = n * 18;
0224         for (b = off; b < 18 + off; b++) {
0225             /* This channel to all outputs ? */
0226             for (c = 0; c <= 8; c++) {
0227                 snd_s1810c_send_ctl_packet(dev, a, b, c, 0, e);
0228                 snd_s1810c_send_ctl_packet(dev, a, b, c, 1, e);
0229             }
0230             /* This channel to main output (again) */
0231             snd_s1810c_send_ctl_packet(dev, a, b, 0, 0, e);
0232             snd_s1810c_send_ctl_packet(dev, a, b, 0, 1, e);
0233         }
0234         /*
0235          * I noticed on UC that DAW channels have different
0236          * initial volumes, so this makes sense.
0237          */
0238         e = 0xb53bf0;
0239     }
0240 
0241     /* Connect analog outputs ? */
0242     a = 0x65;
0243     e = 0x01000000;
0244     for (b = 1; b < 3; b++) {
0245         snd_s1810c_send_ctl_packet(dev, a, b, 0, 0, e);
0246         snd_s1810c_send_ctl_packet(dev, a, b, 0, 1, e);
0247     }
0248     snd_s1810c_send_ctl_packet(dev, a, 0, 0, 0, e);
0249     snd_s1810c_send_ctl_packet(dev, a, 0, 0, 1, e);
0250 
0251     /* Set initial volume levels for S/PDIF mappings ? */
0252     a = 0x64;
0253     e = 0xbc;
0254     c = 3;
0255     for (n = 0; n < 2; n++) {
0256         off = n * 18;
0257         for (b = off; b < 18 + off; b++) {
0258             snd_s1810c_send_ctl_packet(dev, a, b, c, 0, e);
0259             snd_s1810c_send_ctl_packet(dev, a, b, c, 1, e);
0260         }
0261         e = 0xb53bf0;
0262     }
0263 
0264     /* Connect S/PDIF output ? */
0265     a = 0x65;
0266     e = 0x01000000;
0267     snd_s1810c_send_ctl_packet(dev, a, 3, 0, 0, e);
0268     snd_s1810c_send_ctl_packet(dev, a, 3, 0, 1, e);
0269 
0270     /* Connect all outputs (again) ? */
0271     a = 0x65;
0272     e = 0x01000000;
0273     for (b = 0; b < 4; b++) {
0274         snd_s1810c_send_ctl_packet(dev, a, b, 0, 0, e);
0275         snd_s1810c_send_ctl_packet(dev, a, b, 0, 1, e);
0276     }
0277 
0278     /* Basic routing to get sound out of the device */
0279     a = 0x64;
0280     e = 0x01000000;
0281     for (c = 0; c < 4; c++) {
0282         for (b = 0; b < 36; b++) {
0283             if ((c == 0 && b == 18) ||  /* DAW1/2 -> Main */
0284                 (c == 1 && b == 20) ||  /* DAW3/4 -> Line3/4 */
0285                 (c == 2 && b == 22) ||  /* DAW4/5 -> Line5/6 */
0286                 (c == 3 && b == 24)) {  /* DAW5/6 -> S/PDIF */
0287                 /* Left */
0288                 snd_s1810c_send_ctl_packet(dev, a, b, c, 0, e);
0289                 snd_s1810c_send_ctl_packet(dev, a, b, c, 1, 0);
0290                 b++;
0291                 /* Right */
0292                 snd_s1810c_send_ctl_packet(dev, a, b, c, 0, 0);
0293                 snd_s1810c_send_ctl_packet(dev, a, b, c, 1, e);
0294             } else {
0295                 /* Leave the rest disconnected */
0296                 snd_s1810c_send_ctl_packet(dev, a, b, c, 0, 0);
0297                 snd_s1810c_send_ctl_packet(dev, a, b, c, 1, 0);
0298             }
0299         }
0300     }
0301 
0302     /* Set initial volume levels for S/PDIF (again) ? */
0303     a = 0x64;
0304     e = 0xbc;
0305     c = 3;
0306     for (n = 0; n < 2; n++) {
0307         off = n * 18;
0308         for (b = off; b < 18 + off; b++) {
0309             snd_s1810c_send_ctl_packet(dev, a, b, c, 0, e);
0310             snd_s1810c_send_ctl_packet(dev, a, b, c, 1, e);
0311         }
0312         e = 0xb53bf0;
0313     }
0314 
0315     /* Connect S/PDIF outputs (again) ? */
0316     a = 0x65;
0317     e = 0x01000000;
0318     snd_s1810c_send_ctl_packet(dev, a, 3, 0, 0, e);
0319     snd_s1810c_send_ctl_packet(dev, a, 3, 0, 1, e);
0320 
0321     /* Again ? */
0322     snd_s1810c_send_ctl_packet(dev, a, 3, 0, 0, e);
0323     snd_s1810c_send_ctl_packet(dev, a, 3, 0, 1, e);
0324 
0325     return 0;
0326 }
0327 
0328 /*
0329  * Sync state with the device and retrieve the requested field,
0330  * whose index is specified in (kctl->private_value & 0xFF),
0331  * from the received fields array.
0332  */
0333 static int
0334 snd_s1810c_get_switch_state(struct usb_mixer_interface *mixer,
0335                 struct snd_kcontrol *kctl, u32 *state)
0336 {
0337     struct snd_usb_audio *chip = mixer->chip;
0338     struct s1810_mixer_state *private = mixer->private_data;
0339     u32 field = 0;
0340     u32 ctl_idx = (u32) (kctl->private_value & 0xFF);
0341     int ret = 0;
0342 
0343     mutex_lock(&private->usb_mutex);
0344     ret = snd_sc1810c_get_status_field(chip->dev, &field,
0345                        ctl_idx, &private->seqnum);
0346     if (ret < 0)
0347         goto unlock;
0348 
0349     *state = field;
0350  unlock:
0351     mutex_unlock(&private->usb_mutex);
0352     return ret ? ret : 0;
0353 }
0354 
0355 /*
0356  * Send a control packet to the device for the control id
0357  * specified in (kctl->private_value >> 8) with value
0358  * specified in (kctl->private_value >> 16).
0359  */
0360 static int
0361 snd_s1810c_set_switch_state(struct usb_mixer_interface *mixer,
0362                 struct snd_kcontrol *kctl)
0363 {
0364     struct snd_usb_audio *chip = mixer->chip;
0365     struct s1810_mixer_state *private = mixer->private_data;
0366     u32 pval = (u32) kctl->private_value;
0367     u32 ctl_id = (pval >> 8) & 0xFF;
0368     u32 ctl_val = (pval >> 16) & 0x1;
0369     int ret = 0;
0370 
0371     mutex_lock(&private->usb_mutex);
0372     ret = snd_s1810c_send_ctl_packet(chip->dev, 0, 0, 0, ctl_id, ctl_val);
0373     mutex_unlock(&private->usb_mutex);
0374     return ret;
0375 }
0376 
0377 /* Generic get/set/init functions for switch controls */
0378 
0379 static int
0380 snd_s1810c_switch_get(struct snd_kcontrol *kctl,
0381               struct snd_ctl_elem_value *ctl_elem)
0382 {
0383     struct usb_mixer_elem_list *list = snd_kcontrol_chip(kctl);
0384     struct usb_mixer_interface *mixer = list->mixer;
0385     struct s1810_mixer_state *private = mixer->private_data;
0386     u32 pval = (u32) kctl->private_value;
0387     u32 ctl_idx = pval & 0xFF;
0388     u32 state = 0;
0389     int ret = 0;
0390 
0391     mutex_lock(&private->data_mutex);
0392     ret = snd_s1810c_get_switch_state(mixer, kctl, &state);
0393     if (ret < 0)
0394         goto unlock;
0395 
0396     switch (ctl_idx) {
0397     case SC1810C_STATE_LINE_SW:
0398     case SC1810C_STATE_AB_SW:
0399         ctl_elem->value.enumerated.item[0] = (int)state;
0400         break;
0401     default:
0402         ctl_elem->value.integer.value[0] = (long)state;
0403     }
0404 
0405  unlock:
0406     mutex_unlock(&private->data_mutex);
0407     return (ret < 0) ? ret : 0;
0408 }
0409 
0410 static int
0411 snd_s1810c_switch_set(struct snd_kcontrol *kctl,
0412               struct snd_ctl_elem_value *ctl_elem)
0413 {
0414     struct usb_mixer_elem_list *list = snd_kcontrol_chip(kctl);
0415     struct usb_mixer_interface *mixer = list->mixer;
0416     struct s1810_mixer_state *private = mixer->private_data;
0417     u32 pval = (u32) kctl->private_value;
0418     u32 ctl_idx = pval & 0xFF;
0419     u32 curval = 0;
0420     u32 newval = 0;
0421     int ret = 0;
0422 
0423     mutex_lock(&private->data_mutex);
0424     ret = snd_s1810c_get_switch_state(mixer, kctl, &curval);
0425     if (ret < 0)
0426         goto unlock;
0427 
0428     switch (ctl_idx) {
0429     case SC1810C_STATE_LINE_SW:
0430     case SC1810C_STATE_AB_SW:
0431         newval = (u32) ctl_elem->value.enumerated.item[0];
0432         break;
0433     default:
0434         newval = (u32) ctl_elem->value.integer.value[0];
0435     }
0436 
0437     if (curval == newval)
0438         goto unlock;
0439 
0440     kctl->private_value &= ~(0x1 << 16);
0441     kctl->private_value |= (unsigned int)(newval & 0x1) << 16;
0442     ret = snd_s1810c_set_switch_state(mixer, kctl);
0443 
0444  unlock:
0445     mutex_unlock(&private->data_mutex);
0446     return (ret < 0) ? 0 : 1;
0447 }
0448 
0449 static int
0450 snd_s1810c_switch_init(struct usb_mixer_interface *mixer,
0451                const struct snd_kcontrol_new *new_kctl)
0452 {
0453     struct snd_kcontrol *kctl;
0454     struct usb_mixer_elem_info *elem;
0455 
0456     elem = kzalloc(sizeof(struct usb_mixer_elem_info), GFP_KERNEL);
0457     if (!elem)
0458         return -ENOMEM;
0459 
0460     elem->head.mixer = mixer;
0461     elem->control = 0;
0462     elem->head.id = 0;
0463     elem->channels = 1;
0464 
0465     kctl = snd_ctl_new1(new_kctl, elem);
0466     if (!kctl) {
0467         kfree(elem);
0468         return -ENOMEM;
0469     }
0470     kctl->private_free = snd_usb_mixer_elem_free;
0471 
0472     return snd_usb_mixer_add_control(&elem->head, kctl);
0473 }
0474 
0475 static int
0476 snd_s1810c_line_sw_info(struct snd_kcontrol *kctl,
0477             struct snd_ctl_elem_info *uinfo)
0478 {
0479     static const char *const texts[2] = {
0480         "Preamp On (Mic/Inst)",
0481         "Preamp Off (Line in)"
0482     };
0483 
0484     return snd_ctl_enum_info(uinfo, 1, ARRAY_SIZE(texts), texts);
0485 }
0486 
0487 static const struct snd_kcontrol_new snd_s1810c_line_sw = {
0488     .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
0489     .name = "Line 1/2 Source Type",
0490     .info = snd_s1810c_line_sw_info,
0491     .get = snd_s1810c_switch_get,
0492     .put = snd_s1810c_switch_set,
0493     .private_value = (SC1810C_STATE_LINE_SW | SC1810C_CTL_LINE_SW << 8)
0494 };
0495 
0496 static const struct snd_kcontrol_new snd_s1810c_mute_sw = {
0497     .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
0498     .name = "Mute Main Out Switch",
0499     .info = snd_ctl_boolean_mono_info,
0500     .get = snd_s1810c_switch_get,
0501     .put = snd_s1810c_switch_set,
0502     .private_value = (SC1810C_STATE_MUTE_SW | SC1810C_CTL_MUTE_SW << 8)
0503 };
0504 
0505 static const struct snd_kcontrol_new snd_s1810c_48v_sw = {
0506     .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
0507     .name = "48V Phantom Power On Mic Inputs Switch",
0508     .info = snd_ctl_boolean_mono_info,
0509     .get = snd_s1810c_switch_get,
0510     .put = snd_s1810c_switch_set,
0511     .private_value = (SC1810C_STATE_48V_SW | SC1810C_CTL_48V_SW << 8)
0512 };
0513 
0514 static int
0515 snd_s1810c_ab_sw_info(struct snd_kcontrol *kctl,
0516               struct snd_ctl_elem_info *uinfo)
0517 {
0518     static const char *const texts[2] = {
0519         "1/2",
0520         "3/4"
0521     };
0522 
0523     return snd_ctl_enum_info(uinfo, 1, ARRAY_SIZE(texts), texts);
0524 }
0525 
0526 static const struct snd_kcontrol_new snd_s1810c_ab_sw = {
0527     .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
0528     .name = "Headphone 1 Source Route",
0529     .info = snd_s1810c_ab_sw_info,
0530     .get = snd_s1810c_switch_get,
0531     .put = snd_s1810c_switch_set,
0532     .private_value = (SC1810C_STATE_AB_SW | SC1810C_CTL_AB_SW << 8)
0533 };
0534 
0535 static void snd_sc1810_mixer_state_free(struct usb_mixer_interface *mixer)
0536 {
0537     struct s1810_mixer_state *private = mixer->private_data;
0538     kfree(private);
0539     mixer->private_data = NULL;
0540 }
0541 
0542 /* Entry point, called from mixer_quirks.c */
0543 int snd_sc1810_init_mixer(struct usb_mixer_interface *mixer)
0544 {
0545     struct s1810_mixer_state *private = NULL;
0546     struct snd_usb_audio *chip = mixer->chip;
0547     struct usb_device *dev = chip->dev;
0548     int ret = 0;
0549 
0550     /* Run this only once */
0551     if (!list_empty(&chip->mixer_list))
0552         return 0;
0553 
0554     dev_info(&dev->dev,
0555          "Presonus Studio 1810c, device_setup: %u\n", chip->setup);
0556     if (chip->setup == 1)
0557         dev_info(&dev->dev, "(8out/18in @ 48kHz)\n");
0558     else if (chip->setup == 2)
0559         dev_info(&dev->dev, "(6out/8in @ 192kHz)\n");
0560     else
0561         dev_info(&dev->dev, "(8out/14in @ 96kHz)\n");
0562 
0563     ret = snd_s1810c_init_mixer_maps(chip);
0564     if (ret < 0)
0565         return ret;
0566 
0567     private = kzalloc(sizeof(struct s1810_mixer_state), GFP_KERNEL);
0568     if (!private)
0569         return -ENOMEM;
0570 
0571     mutex_init(&private->usb_mutex);
0572     mutex_init(&private->data_mutex);
0573 
0574     mixer->private_data = private;
0575     mixer->private_free = snd_sc1810_mixer_state_free;
0576 
0577     private->seqnum = 1;
0578 
0579     ret = snd_s1810c_switch_init(mixer, &snd_s1810c_line_sw);
0580     if (ret < 0)
0581         return ret;
0582 
0583     ret = snd_s1810c_switch_init(mixer, &snd_s1810c_mute_sw);
0584     if (ret < 0)
0585         return ret;
0586 
0587     ret = snd_s1810c_switch_init(mixer, &snd_s1810c_48v_sw);
0588     if (ret < 0)
0589         return ret;
0590 
0591     ret = snd_s1810c_switch_init(mixer, &snd_s1810c_ab_sw);
0592     if (ret < 0)
0593         return ret;
0594     return ret;
0595 }