]> sigrok.org Git - libsigrok.git/blobdiff - src/hardware/sysclk-lwla/api.c
Consistently use the "Sysclk" spelling everywhere.
[libsigrok.git] / src / hardware / sysclk-lwla / api.c
index 4e4fc3a5af460b5606168f72e76f2b45742dd8c4..cac7cfc1462b51269d3a019711736c3f297e4e38 100644 (file)
@@ -41,12 +41,12 @@ static const int32_t trigger_matches[] = {
        SR_TRIGGER_FALLING,
 };
 
-static const char *const trigger_source_names[] = {
+static const char *trigger_sources[] = {
        [TRIGGER_CHANNELS] = "CH",
        [TRIGGER_EXT_TRG] = "TRG",
 };
 
-static const char *const signal_edge_names[] = {
+static const char *signal_edges[] = {
        [EDGE_POSITIVE] = "r",
        [EDGE_NEGATIVE] = "f",
 };
@@ -67,7 +67,7 @@ static struct sr_dev_inst *dev_inst_new(const struct model_info *model)
 
        sdi = g_malloc0(sizeof(struct sr_dev_inst));
        sdi->status = SR_ST_INACTIVE;
-       sdi->vendor = g_strdup(VENDOR_NAME);
+       sdi->vendor = g_strdup("Sysclk");
        sdi->model = g_strdup(model->name);
        sdi->priv = devc;
 
@@ -79,7 +79,7 @@ static struct sr_dev_inst *dev_inst_new(const struct model_info *model)
        return sdi;
 }
 
-/* Create a new device instance for a libusb device if it is a SysClk LWLA
+/* Create a new device instance for a libusb device if it is a Sysclk LWLA
  * device and also matches the connection specification.
  */
 static struct sr_dev_inst *dev_inst_new_matching(GSList *conn_matches,
@@ -121,7 +121,7 @@ static struct sr_dev_inst *dev_inst_new_matching(GSList *conn_matches,
        } else {
                if (conn_matches)
                        sr_warn("USB device %d.%d (%04x:%04x) is not a"
-                               " SysClk LWLA.", bus, address, vid, pid);
+                               " Sysclk LWLA.", bus, address, vid, pid);
                return NULL;
        }
        sdi = dev_inst_new(model);
@@ -259,6 +259,8 @@ static int dev_open(struct sr_dev_inst *sdi)
                /* This delay appears to be necessary for reliable operation. */
                g_usleep(30 * 1000);
 
+               sdi->status = SR_ST_ACTIVE;
+
                devc->active_fpga_config = FPGA_NOCONF;
                devc->short_transfer_quirk = FALSE;
                devc->state = STATE_IDLE;
@@ -271,6 +273,7 @@ static int dev_open(struct sr_dev_inst *sdi)
                        break;
 
                /* Rinse and repeat. */
+               sdi->status = SR_ST_INACTIVE;
                sr_usb_close(usb);
        }
 
@@ -301,11 +304,12 @@ static int dev_close(struct sr_dev_inst *sdi)
        if (ret != SR_OK)
                sr_warn("Unable to shut down device.");
 
-       libusb_release_interface(usb->devhdl, USB_INTERFACE);
+       if (usb->devhdl)
+               libusb_release_interface(usb->devhdl, USB_INTERFACE);
 
        sr_usb_close(usb);
 
-       return SR_OK;
+       return ret;
 }
 
 /* Check whether the device options contain a specific key.
@@ -323,8 +327,8 @@ static int has_devopt(const struct model_info *model, uint32_t key)
        return FALSE;
 }
 
-static int config_get(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi,
-                     const struct sr_channel_group *cg)
+static int config_get(uint32_t key, GVariant **data,
+       const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
 {
        struct dev_context *devc;
        unsigned int idx;
@@ -358,21 +362,21 @@ static int config_get(uint32_t key, GVariant **data, const struct sr_dev_inst *s
                break;
        case SR_CONF_CLOCK_EDGE:
                idx = devc->cfg_clock_edge;
-               if (idx >= ARRAY_SIZE(signal_edge_names))
+               if (idx >= ARRAY_SIZE(signal_edges))
                        return SR_ERR_BUG;
-               *data = g_variant_new_string(signal_edge_names[idx]);
+               *data = g_variant_new_string(signal_edges[idx]);
                break;
        case SR_CONF_TRIGGER_SOURCE:
                idx = devc->cfg_trigger_source;
-               if (idx >= ARRAY_SIZE(trigger_source_names))
+               if (idx >= ARRAY_SIZE(trigger_sources))
                        return SR_ERR_BUG;
-               *data = g_variant_new_string(trigger_source_names[idx]);
+               *data = g_variant_new_string(trigger_sources[idx]);
                break;
        case SR_CONF_TRIGGER_SLOPE:
                idx = devc->cfg_trigger_slope;
-               if (idx >= ARRAY_SIZE(signal_edge_names))
+               if (idx >= ARRAY_SIZE(signal_edges))
                        return SR_ERR_BUG;
-               *data = g_variant_new_string(signal_edge_names[idx]);
+               *data = g_variant_new_string(signal_edges[idx]);
                break;
        default:
                /* Must not happen for a key listed in devopts. */
@@ -382,29 +386,8 @@ static int config_get(uint32_t key, GVariant **data, const struct sr_dev_inst *s
        return SR_OK;
 }
 
-/* Helper for mapping a string-typed configuration value to an index
- * within a table of possible values.
- */
-static int lookup_index(GVariant *value, const char *const *table, int len)
-{
-       const char *entry;
-       int i;
-
-       entry = g_variant_get_string(value, NULL);
-       if (!entry)
-               return -1;
-
-       /* Linear search is fine for very small tables. */
-       for (i = 0; i < len; i++) {
-               if (strcmp(entry, table[i]) == 0)
-                       return i;
-       }
-
-       return -1;
-}
-
-static int config_set(uint32_t key, GVariant *data, const struct sr_dev_inst *sdi,
-                     const struct sr_channel_group *cg)
+static int config_set(uint32_t key, GVariant *data,
+       const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
 {
        uint64_t value;
        struct dev_context *devc;
@@ -448,23 +431,17 @@ static int config_set(uint32_t key, GVariant *data, const struct sr_dev_inst *sd
                        ? CLOCK_EXT_CLK : CLOCK_INTERNAL;
                break;
        case SR_CONF_CLOCK_EDGE:
-               idx = lookup_index(data, signal_edge_names,
-                                  ARRAY_SIZE(signal_edge_names));
-               if (idx < 0)
+               if ((idx = std_str_idx(data, ARRAY_AND_SIZE(signal_edges))) < 0)
                        return SR_ERR_ARG;
                devc->cfg_clock_edge = idx;
                break;
        case SR_CONF_TRIGGER_SOURCE:
-               idx = lookup_index(data, trigger_source_names,
-                                  ARRAY_SIZE(trigger_source_names));
-               if (idx < 0)
+               if ((idx = std_str_idx(data, ARRAY_AND_SIZE(trigger_sources))) < 0)
                        return SR_ERR_ARG;
                devc->cfg_trigger_source = idx;
                break;
        case SR_CONF_TRIGGER_SLOPE:
-               idx = lookup_index(data, signal_edge_names,
-                                  ARRAY_SIZE(signal_edge_names));
-               if (idx < 0)
+               if ((idx = std_str_idx(data, ARRAY_AND_SIZE(signal_edges))) < 0)
                        return SR_ERR_ARG;
                devc->cfg_trigger_slope = idx;
                break;
@@ -477,7 +454,7 @@ static int config_set(uint32_t key, GVariant *data, const struct sr_dev_inst *sd
 }
 
 static int config_channel_set(const struct sr_dev_inst *sdi,
-                             struct sr_channel *ch, unsigned int changes)
+       struct sr_channel *ch, unsigned int changes)
 {
        uint64_t channel_bit;
        struct dev_context *devc;
@@ -596,8 +573,7 @@ static int config_commit(const struct sr_dev_inst *sdi)
 }
 
 static int config_list(uint32_t key, GVariant **data,
-                      const struct sr_dev_inst *sdi,
-                      const struct sr_channel_group *cg)
+       const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
 {
        struct dev_context *devc;
 
@@ -607,12 +583,13 @@ static int config_list(uint32_t key, GVariant **data,
        case SR_CONF_SCAN_OPTIONS:
        case SR_CONF_DEVICE_OPTIONS:
                return std_opts_config_list(key, data, sdi, cg,
-                       scanopts, ARRAY_SIZE(scanopts),
-                       drvopts, ARRAY_SIZE(drvopts),
+                       ARRAY_AND_SIZE(scanopts), ARRAY_AND_SIZE(drvopts),
                        (devc) ? devc->model->devopts : NULL,
                        (devc) ? devc->model->num_devopts : 0);
        }
 
+       if (!devc)
+               return SR_ERR_ARG;
        if (!has_devopt(devc->model, key | SR_CONF_LIST))
                return SR_ERR_NA;
 
@@ -621,16 +598,14 @@ static int config_list(uint32_t key, GVariant **data,
                *data = std_gvar_samplerates(devc->model->samplerates, devc->model->num_samplerates);
                break;
        case SR_CONF_TRIGGER_MATCH:
-               *data = std_gvar_array_i32(trigger_matches, ARRAY_SIZE(trigger_matches));
+               *data = std_gvar_array_i32(ARRAY_AND_SIZE(trigger_matches));
                break;
        case SR_CONF_TRIGGER_SOURCE:
-               *data = g_variant_new_strv(trigger_source_names,
-                       ARRAY_SIZE(trigger_source_names));
+               *data = g_variant_new_strv(ARRAY_AND_SIZE(trigger_sources));
                break;
        case SR_CONF_TRIGGER_SLOPE:
        case SR_CONF_CLOCK_EDGE:
-               *data = g_variant_new_strv(signal_edge_names,
-                       ARRAY_SIZE(signal_edge_names));
+               *data = g_variant_new_strv(ARRAY_AND_SIZE(signal_edges));
                break;
        default:
                /* Must not happen for a key listed in devopts. */
@@ -665,7 +640,7 @@ static int dev_acquisition_stop(struct sr_dev_inst *sdi)
 
 static struct sr_dev_driver sysclk_lwla_driver_info = {
        .name = "sysclk-lwla",
-       .longname = "SysClk LWLA series",
+       .longname = "Sysclk LWLA series",
        .api_version = 1,
        .init = std_init,
        .cleanup = std_cleanup,