]> sigrok.org Git - libsigrok.git/blobdiff - src/hardware/ftdi-la/api.c
drivers: Factor out std_gvar_samplerates{,_steps}().
[libsigrok.git] / src / hardware / ftdi-la / api.c
index 6b545afb08be17cdb82d351198269077ad7d0758..d435c64a8bab49d5cbea94fc3e001c84123a753a 100644 (file)
@@ -28,8 +28,11 @@ static const uint32_t scanopts[] = {
        SR_CONF_CONN,
 };
 
-static const uint32_t devopts[] = {
+static const uint32_t drvopts[] = {
        SR_CONF_LOGIC_ANALYZER,
+};
+
+static const uint32_t devopts[] = {
        SR_CONF_CONTINUOUS,
        SR_CONF_LIMIT_SAMPLES | SR_CONF_SET,
        SR_CONF_SAMPLERATE | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
@@ -108,7 +111,6 @@ static void scan_device(struct ftdi_context *ftdic,
                return;
        }
 
-       /* Allocate memory for our private device context. */
        devc = g_malloc0(sizeof(struct dev_context));
 
        /* Allocate memory for the incoming data. */
@@ -135,7 +137,6 @@ static void scan_device(struct ftdi_context *ftdic,
        }
        sr_dbg("Found an FTDI device: %s.", model);
 
-       /* Register the device with libsigrok. */
        sdi = g_malloc0(sizeof(struct sr_dev_inst));
        sdi->status = SR_ST_INACTIVE;
        sdi->vendor = vendor;
@@ -211,7 +212,6 @@ static GSList *scan(struct sr_dev_driver *di, GSList *options)
                }
        }
 
-       /* Allocate memory for the FTDI context (ftdic) and initialize it. */
        ftdic = ftdi_new();
        if (!ftdic) {
                sr_err("Failed to initialize libftdi.");
@@ -240,17 +240,14 @@ static GSList *scan(struct sr_dev_driver *di, GSList *options)
        return std_scan_complete(di, devices);
 }
 
-static void clear_helper(void *priv)
+static void clear_helper(struct dev_context *devc)
 {
-       struct dev_context *devc;
-
-       devc = priv;
        g_free(devc->data_buf);
 }
 
 static int dev_clear(const struct sr_dev_driver *di)
 {
-       return std_dev_clear_with_callback(di, clear_helper);
+       return std_dev_clear_with_callback(di, (std_dev_clear_callback)clear_helper);
 }
 
 static int dev_open(struct sr_dev_inst *sdi)
@@ -273,7 +270,6 @@ static int dev_open(struct sr_dev_inst *sdi)
                goto err_ftdi_free;
        }
 
-       /* Purge RX/TX buffers in the FTDI chip. */
        ret = ftdi_usb_purge_buffers(devc->ftdic);
        if (ret < 0) {
                sr_err("Failed to purge FTDI RX/TX buffers (%d): %s.",
@@ -281,7 +277,6 @@ static int dev_open(struct sr_dev_inst *sdi)
                goto err_dev_open_close_ftdic;
        }
 
-       /* Reset the FTDI bitmode. */
        ret = ftdi_set_bitmode(devc->ftdic, 0x00, BITMODE_RESET);
        if (ret < 0) {
                sr_err("Failed to reset the FTDI chip bitmode (%d): %s.",
@@ -326,7 +321,6 @@ static int dev_close(struct sr_dev_inst *sdi)
 static int config_get(uint32_t key, GVariant **data,
        const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
 {
-       int ret;
        struct dev_context *devc;
        struct sr_usb_dev_inst *usb;
        char str[128];
@@ -335,7 +329,6 @@ static int config_get(uint32_t key, GVariant **data,
 
        devc = sdi->priv;
 
-       ret = SR_OK;
        switch (key) {
        case SR_CONF_SAMPLERATE:
                *data = g_variant_new_uint64(devc->cur_samplerate);
@@ -351,13 +344,12 @@ static int config_get(uint32_t key, GVariant **data,
                return SR_ERR_NA;
        }
 
-       return ret;
+       return SR_OK;
 }
 
 static int config_set(uint32_t key, GVariant *data,
        const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
 {
-       int ret;
        struct dev_context *devc;
        uint64_t value;
 
@@ -365,13 +357,11 @@ static int config_set(uint32_t key, GVariant *data,
 
        devc = sdi->priv;
 
-       ret = SR_OK;
        switch (key) {
        case SR_CONF_LIMIT_MSEC:
                value = g_variant_get_uint64(data);
                /* TODO: Implement. */
-               ret = SR_ERR_NA;
-               break;
+               return SR_ERR_NA;
        case SR_CONF_LIMIT_SAMPLES:
                if (g_variant_get_uint64(data) == 0)
                        return SR_ERR_ARG;
@@ -384,44 +374,27 @@ static int config_set(uint32_t key, GVariant *data,
                devc->cur_samplerate = value;
                return ftdi_la_set_samplerate(devc);
        default:
-               ret = SR_ERR_NA;
+               return SR_ERR_NA;
        }
 
-       return ret;
+       return SR_OK;
 }
 
 static int config_list(uint32_t key, GVariant **data,
        const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
 {
-       int ret;
-       GVariant *gvar;
-       GVariantBuilder gvb;
-
-       (void)sdi;
-       (void)cg;
-
-       ret = SR_OK;
        switch (key) {
        case SR_CONF_SCAN_OPTIONS:
-               *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
-                       scanopts, ARRAY_SIZE(scanopts), sizeof(uint32_t));
-               break;
        case SR_CONF_DEVICE_OPTIONS:
-               *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
-                       devopts, ARRAY_SIZE(devopts), sizeof(uint32_t));
-               break;
+               return STD_CONFIG_LIST(key, data, sdi, cg, scanopts, drvopts, devopts);
        case SR_CONF_SAMPLERATE:
-               g_variant_builder_init(&gvb, G_VARIANT_TYPE("a{sv}"));
-               gvar = g_variant_new_fixed_array(G_VARIANT_TYPE("t"),
-                       samplerates, ARRAY_SIZE(samplerates), sizeof(uint64_t));
-               g_variant_builder_add(&gvb, "{sv}", "samplerate-steps", gvar);
-               *data = g_variant_builder_end(&gvb);
+               *data = std_gvar_samplerates_steps(samplerates, ARRAY_SIZE(samplerates));
                break;
        default:
                return SR_ERR_NA;
        }
 
-       return ret;
+       return SR_OK;
 }
 
 static int dev_acquisition_start(const struct sr_dev_inst *sdi)