]> sigrok.org Git - libsigrok.git/blobdiff - hardware/rigol-ds1xx2/api.c
Don't accept over 64 probes.
[libsigrok.git] / hardware / rigol-ds1xx2 / api.c
index 81ea66950c06a560d2868fa0ac2ef18d80913729..d6f055e64549662c01fa4894a3f84d244c1711b0 100644 (file)
@@ -40,10 +40,13 @@ static const int32_t hwcaps[] = {
        SR_CONF_TRIGGER_SOURCE,
        SR_CONF_TRIGGER_SLOPE,
        SR_CONF_HORIZ_TRIGGERPOS,
-       SR_CONF_VDIV,
-       SR_CONF_COUPLING,
        SR_CONF_NUM_TIMEBASE,
+};
+
+static const int32_t analog_hwcaps[] = {
        SR_CONF_NUM_VDIV,
+       SR_CONF_VDIV,
+       SR_CONF_COUPLING,
 };
 
 static const uint64_t timebases[][2] = {
@@ -133,8 +136,10 @@ static const char *coupling[] = {
 static const char *supported_models[] = {
        "DS1052E",
        "DS1102E",
+       "DS1152E",
        "DS1052D",
        "DS1102D",
+       "DS1152D",
 };
 
 SR_PRIV struct sr_dev_driver rigol_ds1xx2_driver_info;
@@ -145,14 +150,16 @@ static void clear_helper(void *priv)
        struct dev_context *devc;
 
        devc = priv;
-
        g_free(devc->coupling[0]);
        g_free(devc->coupling[1]);
        g_free(devc->trigger_source);
        g_free(devc->trigger_slope);
+       g_slist_free(devc->analog_groups[0].probes);
+       g_slist_free(devc->analog_groups[1].probes);
+       g_slist_free(devc->digital_group.probes);
 }
 
-static int clear_instances(void)
+static int dev_clear(void)
 {
        return std_dev_clear(di, clear_helper);
 }
@@ -178,7 +185,7 @@ static int set_cfg(const struct sr_dev_inst *sdi, const char *format, ...)
 
 static int init(struct sr_context *sr_ctx)
 {
-       return std_hw_init(sr_ctx, di, LOG_PREFIX);
+       return std_init(sr_ctx, di, LOG_PREFIX);
 }
 
 static int probe_port(const char *port, GSList **devices)
@@ -258,10 +265,14 @@ static int probe_port(const char *port, GSList **devices)
        devc->has_digital = has_digital;
 
        for (i = 0; i < 2; i++) {
-               if (!(probe = sr_probe_new(i, SR_PROBE_ANALOG, TRUE,
-                               i == 0 ? "CH1" : "CH2")))
+               channel_name = (i == 0 ? "CH1" : "CH2");
+               if (!(probe = sr_probe_new(i, SR_PROBE_ANALOG, TRUE, channel_name)))
                        return SR_ERR_MALLOC;
                sdi->probes = g_slist_append(sdi->probes, probe);
+               devc->analog_groups[i].name = channel_name;
+               devc->analog_groups[i].probes = g_slist_append(NULL, probe);
+               sdi->probe_groups = g_slist_append(sdi->probe_groups,
+                               &devc->analog_groups[i]);
        }
 
        if (devc->has_digital) {
@@ -273,6 +284,11 @@ static int probe_port(const char *port, GSList **devices)
                        if (!probe)
                                return SR_ERR_MALLOC;
                        sdi->probes = g_slist_append(sdi->probes, probe);
+                       devc->digital_group.probes = g_slist_append(
+                                       devc->digital_group.probes, probe);
+                       devc->digital_group.name = "LA";
+                       sdi->probe_groups = g_slist_append(sdi->probe_groups,
+                                       &devc->digital_group);
                }
        }
        sdi->priv = devc;
@@ -307,8 +323,9 @@ static GSList *scan(GSList *options)
                if (probe_port(port, &devices) == SR_ERR_MALLOC)
                        return NULL;
        } else {
-               if (!(dir = g_dir_open("/sys/class/usb/", 0, NULL)))
-                       return NULL;
+               if (!(dir = g_dir_open("/sys/class/usbmisc/", 0, NULL)))
+                       if (!(dir = g_dir_open("/sys/class/usb/", 0, NULL)))
+                               return NULL;
                while ((dev_name = g_dir_read_name(dir))) {
                        if (strncmp(dev_name, "usbtmc", 6))
                                continue;
@@ -364,21 +381,43 @@ static int dev_close(struct sr_dev_inst *sdi)
 
 static int cleanup(void)
 {
-       return clear_instances();
+       return dev_clear();
 }
 
-static int config_get(int id, GVariant **data, const struct sr_dev_inst *sdi)
+static int config_get(int id, GVariant **data, const struct sr_dev_inst *sdi,
+               const struct sr_probe_group *probe_group)
 {
+       struct dev_context *devc;
+       unsigned int i;
 
-       (void)sdi;
+       if (!sdi || !(devc = sdi->priv))
+               return SR_ERR_ARG;
+
+       /* If a probe group is specified, it must be a valid one. */
+       if (probe_group) {
+               if (probe_group != &devc->analog_groups[0]
+                               && probe_group != &devc->analog_groups[1]) {
+                       sr_err("Invalid probe group specified.");
+                       return SR_ERR;
+               }
+       }
 
        switch (id) {
        case SR_CONF_NUM_TIMEBASE:
                *data = g_variant_new_int32(NUM_TIMEBASE);
                break;
        case SR_CONF_NUM_VDIV:
-               *data = g_variant_new_int32(NUM_VDIV);
-               break;
+               if (!probe_group) {
+                       sr_err("No probe group specified.");
+                       return SR_ERR_PROBE_GROUP;
+               }
+               for (i = 0; i < 2; i++) {
+                       if (probe_group == &devc->analog_groups[i]) {
+                               *data = g_variant_new_int32(NUM_VDIV);
+                               return SR_OK;
+                       }
+               }
+               return SR_ERR_NA;
        default:
                return SR_ERR_NA;
        }
@@ -386,20 +425,31 @@ static int config_get(int id, GVariant **data, const struct sr_dev_inst *sdi)
        return SR_OK;
 }
 
-static int config_set(int id, GVariant *data, const struct sr_dev_inst *sdi)
+static int config_set(int id, GVariant *data, const struct sr_dev_inst *sdi,
+               const struct sr_probe_group *probe_group)
 {
        struct dev_context *devc;
        uint64_t tmp_u64, p, q;
        double t_dbl;
-       unsigned int i;
+       unsigned int i, j;
        int ret;
        const char *tmp_str;
 
-       devc = sdi->priv;
+       if (!(devc = sdi->priv))
+               return SR_ERR_ARG;
 
        if (sdi->status != SR_ST_ACTIVE)
                return SR_ERR_DEV_CLOSED;
 
+       /* If a probe group is specified, it must be a valid one. */
+       if (probe_group) {
+               if (probe_group != &devc->analog_groups[0]
+                               && probe_group != &devc->analog_groups[1]) {
+                       sr_err("Invalid probe group specified.");
+                       return SR_ERR;
+               }
+       }
+
        ret = SR_OK;
        switch (id) {
        case SR_CONF_LIMIT_FRAMES:
@@ -457,35 +507,44 @@ static int config_set(int id, GVariant *data, const struct sr_dev_inst *sdi)
                        ret = SR_ERR_ARG;
                break;
        case SR_CONF_VDIV:
+               if (!probe_group) {
+                       sr_err("No probe group specified.");
+                       return SR_ERR_PROBE_GROUP;
+               }
                g_variant_get(data, "(tt)", &p, &q);
-               for (i = 0; i < ARRAY_SIZE(vdivs); i++) {
-                       if (vdivs[i][0] != p || vdivs[i][1] != q)
-                               continue;
-                       devc->vdiv[0] = devc->vdiv[1] = (float)p / q;
-                       set_cfg(sdi, ":CHAN1:SCAL %.3f", devc->vdiv[0]);
-                       ret = set_cfg(sdi, ":CHAN2:SCAL %.3f", devc->vdiv[1]);
-                       break;
+               for (i = 0; i < 2; i++) {
+                       if (probe_group == &devc->analog_groups[i]) {
+                               for (j = 0; j < ARRAY_SIZE(vdivs); j++) {
+                                       if (vdivs[j][0] != p || vdivs[j][1] != q)
+                                               continue;
+                                       devc->vdiv[i] = (float)p / q;
+                                       return set_cfg(sdi, ":CHAN%d:SCAL %.3f", i + 1,
+                                                       devc->vdiv[i]);
+                               }
+                               return SR_ERR_ARG;
+                       }
                }
-               if (i == ARRAY_SIZE(vdivs))
-                       ret = SR_ERR_ARG;
-               break;
+               return SR_ERR_NA;
        case SR_CONF_COUPLING:
-               /* TODO: Not supporting coupling per channel yet. */
+               if (!probe_group) {
+                       sr_err("No probe group specified.");
+                       return SR_ERR_PROBE_GROUP;
+               }
                tmp_str = g_variant_get_string(data, NULL);
-               for (i = 0; i < ARRAY_SIZE(coupling); i++) {
-                       if (!strcmp(tmp_str, coupling[i])) {
-                               g_free(devc->coupling[0]);
-                               g_free(devc->coupling[1]);
-                               devc->coupling[0] = g_strdup(coupling[i]);
-                               devc->coupling[1] = g_strdup(coupling[i]);
-                               set_cfg(sdi, ":CHAN1:COUP %s", devc->coupling[0]);
-                               ret = set_cfg(sdi, ":CHAN2:COUP %s", devc->coupling[1]);
-                               break;
+               for (i = 0; i < 2; i++) {
+                       if (probe_group == &devc->analog_groups[i]) {
+                               for (j = 0; j < ARRAY_SIZE(coupling); j++) {
+                                       if (!strcmp(tmp_str, coupling[j])) {
+                                               g_free(devc->coupling[i]);
+                                               devc->coupling[i] = g_strdup(coupling[j]);
+                                               return set_cfg(sdi, ":CHAN%d:COUP %s", i + 1,
+                                                               devc->coupling[i]);
+                                       }
+                               }
+                               return SR_ERR_ARG;
                        }
                }
-               if (i == ARRAY_SIZE(coupling))
-                       ret = SR_ERR_ARG;
-               break;
+               return SR_ERR_NA;
        default:
                ret = SR_ERR_NA;
                break;
@@ -494,26 +553,71 @@ static int config_set(int id, GVariant *data, const struct sr_dev_inst *sdi)
        return ret;
 }
 
-static int config_list(int key, GVariant **data, const struct sr_dev_inst *sdi)
+static int config_list(int key, GVariant **data, const struct sr_dev_inst *sdi,
+               const struct sr_probe_group *probe_group)
 {
        GVariant *tuple, *rational[2];
        GVariantBuilder gvb;
        unsigned int i;
        struct dev_context *devc;
 
-       switch (key) {
-       case SR_CONF_SCAN_OPTIONS:
+       if (key == SR_CONF_SCAN_OPTIONS) {
                *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32,
                                hwopts, ARRAY_SIZE(hwopts), sizeof(int32_t));
+               return SR_OK;
+       } else if (key == SR_CONF_DEVICE_OPTIONS && probe_group == NULL) {
+               *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32,
+                       hwcaps, ARRAY_SIZE(hwcaps), sizeof(int32_t));
+               return SR_OK;
+       }
+
+       /* Every other option requires a valid device instance. */
+       if (!sdi || !(devc = sdi->priv))
+               return SR_ERR_ARG;
+
+       /* If a probe group is specified, it must be a valid one. */
+       if (probe_group) {
+               if (probe_group != &devc->analog_groups[0]
+                               && probe_group != &devc->analog_groups[1]) {
+                       sr_err("Invalid probe group specified.");
+                       return SR_ERR;
+               }
+       }
+
+       switch (key) {
                break;
        case SR_CONF_DEVICE_OPTIONS:
-               *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32,
-                               hwcaps, ARRAY_SIZE(hwcaps), sizeof(int32_t));
+               if (!probe_group) {
+                       sr_err("No probe group specified.");
+                       return SR_ERR_PROBE_GROUP;
+               }
+               if (probe_group == &devc->digital_group) {
+                       *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32,
+                               NULL, 0, sizeof(int32_t));
+                       return SR_OK;
+               } else {
+                       for (i = 0; i < 2; i++) {
+                               if (probe_group == &devc->analog_groups[i]) {
+                                       *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32,
+                                               analog_hwcaps, ARRAY_SIZE(analog_hwcaps), sizeof(int32_t));
+                                       return SR_OK;
+                               }
+                       }
+                       return SR_ERR_NA;
+               }
                break;
        case SR_CONF_COUPLING:
+               if (!probe_group) {
+                       sr_err("No probe group specified.");
+                       return SR_ERR_PROBE_GROUP;
+               }
                *data = g_variant_new_strv(coupling, ARRAY_SIZE(coupling));
                break;
        case SR_CONF_VDIV:
+               if (!probe_group) {
+                       sr_err("No probe group specified.");
+                       return SR_ERR_PROBE_GROUP;
+               }
                g_variant_builder_init(&gvb, G_VARIANT_TYPE_ARRAY);
                for (i = 0; i < ARRAY_SIZE(vdivs); i++) {
                        rational[0] = g_variant_new_uint64(vdivs[i][0]);
@@ -534,10 +638,6 @@ static int config_list(int key, GVariant **data, const struct sr_dev_inst *sdi)
                *data = g_variant_builder_end(&gvb);
                break;
        case SR_CONF_TRIGGER_SOURCE:
-               if (!sdi || !sdi->priv)
-                       /* Can't know this until we have the exact model. */
-                       return SR_ERR_ARG;
-               devc = sdi->priv;
                *data = g_variant_new_strv(trigger_sources,
                                devc->has_digital ? ARRAY_SIZE(trigger_sources) : 4);
                break;
@@ -646,7 +746,7 @@ SR_PRIV struct sr_dev_driver rigol_ds1xx2_driver_info = {
        .cleanup = cleanup,
        .scan = scan,
        .dev_list = dev_list,
-       .dev_clear = clear_instances,
+       .dev_clear = dev_clear,
        .config_get = config_get,
        .config_set = config_set,
        .config_list = config_list,