]> sigrok.org Git - libsigrok.git/blobdiff - src/hardware/kingst-la2016/api.c
device: introduce common helpers for channel group allocation
[libsigrok.git] / src / hardware / kingst-la2016 / api.c
index 4337bd0a879a4b63e4b1f059744d156762b28016..b31bbc81d9d26fb6fa9faf97ddb28717f2563e3d 100644 (file)
 #include "libsigrok-internal.h"
 #include "protocol.h"
 
+/*
+ * Default device configuration. Must be applicable to any of the
+ * supported devices (no model specific default values yet). Specific
+ * firmware implementation details unfortunately won't let us detect
+ * and keep using previously configured values.
+ */
+#define LA2016_DFLT_SAMPLERATE SR_MHZ(100)
+#define LA2016_DFLT_SAMPLEDEPTH        (5 * 1000 * 1000)
+#define LA2016_DFLT_CAPT_RATIO 5 /* Capture ratio, in percent. */
+
 static const uint32_t scanopts[] = {
        SR_CONF_CONN,
 };
@@ -45,7 +55,8 @@ static const uint32_t devopts[] = {
        /* TODO: SR_CONF_CONTINUOUS, */
        SR_CONF_CONN | SR_CONF_GET,
        SR_CONF_SAMPLERATE | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
-       SR_CONF_LIMIT_SAMPLES | SR_CONF_SET | SR_CONF_GET | SR_CONF_LIST,
+       SR_CONF_LIMIT_SAMPLES | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
+       SR_CONF_LIMIT_MSEC | SR_CONF_GET | SR_CONF_SET,
        SR_CONF_VOLTAGE_THRESHOLD | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
        SR_CONF_LOGIC_THRESHOLD | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
        SR_CONF_LOGIC_THRESHOLD_CUSTOM | SR_CONF_GET | SR_CONF_SET,
@@ -61,11 +72,21 @@ static const int32_t trigger_matches[] = {
 };
 
 static const char *channel_names[] = {
-       "0", "1", "2", "3", "4", "5", "6", "7",
-       "8", "9", "10", "11", "12", "13", "14", "15",
+       "CH0", "CH1", "CH2", "CH3", "CH4", "CH5", "CH6", "CH7",
+       "CH8", "CH9", "CH10", "CH11", "CH12", "CH13", "CH14", "CH15",
 };
 
+/*
+ * The hardware uses a (model dependent) 100/200/500MHz base clock and
+ * a 16bit divider (common across all models). The range from 10kHz to
+ * 100/200/500MHz should be applicable to all devices. High rates may
+ * suffer from coarse resolution (e.g. in the "500MHz div 2" case) and
+ * may not provide the desired 1/2/5 steps. This is not an issue now,
+ * the 500MHz model is not supported yet by this driver.
+ */
+
 static const uint64_t samplerates_la2016[] = {
+       SR_KHZ(10),
        SR_KHZ(20),
        SR_KHZ(50),
        SR_KHZ(100),
@@ -84,6 +105,7 @@ static const uint64_t samplerates_la2016[] = {
 };
 
 static const uint64_t samplerates_la1016[] = {
+       SR_KHZ(10),
        SR_KHZ(20),
        SR_KHZ(50),
        SR_KHZ(100),
@@ -130,6 +152,7 @@ static const char *logic_threshold[] = {
 static GSList *scan(struct sr_dev_driver *di, GSList *options)
 {
        struct drv_context *drvc;
+       struct sr_context *ctx;
        struct dev_context *devc;
        struct sr_dev_inst *sdi;
        struct sr_usb_dev_inst *usb;
@@ -138,14 +161,16 @@ static GSList *scan(struct sr_dev_driver *di, GSList *options)
        GSList *devices;
        GSList *conn_devices;
        struct libusb_device_descriptor des;
-       libusb_device **devlist;
-       unsigned int i, j;
+       libusb_device **devlist, *dev;
+       size_t dev_count, dev_idx, ch_idx;
+       uint8_t bus, addr;
        const char *conn;
-       char connection_id[64];
+       char conn_id[64];
        uint64_t fw_uploaded;
-       unsigned int dev_addr;
+       int ret;
 
        drvc = di->context;
+       ctx = drvc->sr_ctx;;
 
        conn = NULL;
        for (l = options; l; l = l->next) {
@@ -157,20 +182,27 @@ static GSList *scan(struct sr_dev_driver *di, GSList *options)
                }
        }
        if (conn)
-               conn_devices = sr_usb_find(drvc->sr_ctx->libusb_ctx, conn);
+               conn_devices = sr_usb_find(ctx->libusb_ctx, conn);
        else
                conn_devices = NULL;
 
        /* Find all LA2016 devices, optionally upload firmware to them. */
        devices = NULL;
-       libusb_get_device_list(drvc->sr_ctx->libusb_ctx, &devlist);
-       for (i = 0; devlist[i]; i++) {
+       ret = libusb_get_device_list(ctx->libusb_ctx, &devlist);
+       if (ret < 0) {
+               sr_err("Cannot get device list: %s.", libusb_error_name(ret));
+               return devices;
+       }
+       dev_count = ret;
+       for (dev_idx = 0; dev_idx < dev_count; dev_idx++) {
+               dev = devlist[dev_idx];
+               bus = libusb_get_bus_number(dev);
+               addr = libusb_get_device_address(dev);
                if (conn) {
                        usb = NULL;
                        for (l = conn_devices; l; l = l->next) {
                                usb = l->data;
-                               if (usb->bus == libusb_get_bus_number(devlist[i]) &&
-                                   usb->address == libusb_get_device_address(devlist[i]))
+                               if (usb->bus == bus && usb->address == addr)
                                        break;
                        }
                        if (!l) {
@@ -182,26 +214,26 @@ static GSList *scan(struct sr_dev_driver *di, GSList *options)
                        }
                }
 
-               libusb_get_device_descriptor(devlist[i], &des);
-
-               if (usb_get_port_path(devlist[i], connection_id, sizeof(connection_id)) < 0)
+               libusb_get_device_descriptor(dev, &des);
+               ret = usb_get_port_path(dev, conn_id, sizeof(conn_id));
+               if (ret < 0)
                        continue;
-
                if (des.idVendor != LA2016_VID || des.idProduct != LA2016_PID)
                        continue;
 
                /* USB identification matches, a device was found. */
-               sr_dbg("Found a LA2016 device.");
-               sdi = g_malloc0(sizeof(struct sr_dev_inst));
+               sr_dbg("Found a device (USB identification).");
+               sdi = g_malloc0(sizeof(*sdi));
                sdi->status = SR_ST_INITIALIZING;
-               sdi->connection_id = g_strdup(connection_id);
+               sdi->connection_id = g_strdup(conn_id);
 
                fw_uploaded = 0;
-               dev_addr = libusb_get_device_address(devlist[i]);
                if (des.iProduct != LA2016_IPRODUCT_INDEX) {
-                       sr_info("Device at '%s' has no firmware loaded.", connection_id);
+                       sr_info("Device at '%s' has no firmware loaded.",
+                               conn_id);
 
-                       if (la2016_upload_firmware(drvc->sr_ctx, devlist[i], des.idProduct) != SR_OK) {
+                       ret = la2016_upload_firmware(ctx, dev, des.idProduct);
+                       if (ret != SR_OK) {
                                sr_err("MCU firmware upload failed.");
                                g_free(sdi->connection_id);
                                g_free(sdi);
@@ -209,29 +241,33 @@ static GSList *scan(struct sr_dev_driver *di, GSList *options)
                        }
                        fw_uploaded = g_get_monotonic_time();
                        /* Will re-enumerate. Mark as "unknown address yet". */
-                       dev_addr = 0xff;
+                       addr = 0xff;
                }
 
                sdi->vendor = g_strdup("Kingst");
                sdi->model = g_strdup("LA2016");
 
-               for (j = 0; j < ARRAY_SIZE(channel_names); j++)
-                       sr_channel_new(sdi, j, SR_CHANNEL_LOGIC, TRUE, channel_names[j]);
+               for (ch_idx = 0; ch_idx < ARRAY_SIZE(channel_names); ch_idx++) {
+                       sr_channel_new(sdi, ch_idx, SR_CHANNEL_LOGIC,
+                               TRUE, channel_names[ch_idx]);
+               }
 
                devices = g_slist_append(devices, sdi);
 
-               devc = g_malloc0(sizeof(struct dev_context));
+               devc = g_malloc0(sizeof(*devc));
                sdi->priv = devc;
                devc->fw_uploaded = fw_uploaded;
+               sr_sw_limits_init(&devc->sw_limits);
+               devc->sw_limits.limit_samples = LA2016_DFLT_SAMPLEDEPTH;
+               devc->capture_ratio = LA2016_DFLT_CAPT_RATIO;
+               devc->cur_samplerate = LA2016_DFLT_SAMPLERATE;
                devc->threshold_voltage_idx = 0;
                devc->threshold_voltage = logic_threshold_value[devc->threshold_voltage_idx];
 
                sdi->status = SR_ST_INACTIVE;
                sdi->inst_type = SR_INST_USB;
 
-               sdi->conn = sr_usb_dev_inst_new(
-                       libusb_get_bus_number(devlist[i]),
-                       dev_addr, NULL);
+               sdi->conn = sr_usb_dev_inst_new(bus, addr, NULL);
        }
        libusb_free_device_list(devlist, 1);
        g_slist_free_full(conn_devices, (GDestroyNotify)sr_usb_dev_inst_free);
@@ -242,57 +278,70 @@ static GSList *scan(struct sr_dev_driver *di, GSList *options)
 static int la2016_dev_open(struct sr_dev_inst *sdi)
 {
        struct sr_dev_driver *di;
-       libusb_device **devlist;
+       struct drv_context *drvc;
+       struct sr_context *ctx;
+       libusb_device **devlist, *dev;
        struct sr_usb_dev_inst *usb;
        struct libusb_device_descriptor des;
-       struct drv_context *drvc;
-       int ret, i, device_count;
-       char connection_id[64];
+       int ret;
+       size_t device_count, dev_idx;
+       gboolean check_conn;
+       char conn_id[64];
 
        di = sdi->driver;
        drvc = di->context;
+       ctx = drvc->sr_ctx;;
        usb = sdi->conn;
        ret = SR_ERR;
 
-       device_count = libusb_get_device_list(drvc->sr_ctx->libusb_ctx, &devlist);
-       if (device_count < 0) {
-               sr_err("Failed to get device list: %s.", libusb_error_name(device_count));
+       ret = libusb_get_device_list(ctx->libusb_ctx, &devlist);
+       if (ret < 0) {
+               sr_err("Cannot get device list: %s.", libusb_error_name(ret));
                return SR_ERR;
        }
-
-       for (i = 0; i < device_count; i++) {
-               libusb_get_device_descriptor(devlist[i], &des);
+       device_count = ret;
+       if (!device_count) {
+               sr_warn("Device list is empty. Cannot open.");
+               return SR_ERR;
+       }
+       for (dev_idx = 0; dev_idx < device_count; dev_idx++) {
+               dev = devlist[dev_idx];
+               libusb_get_device_descriptor(dev, &des);
 
                if (des.idVendor != LA2016_VID || des.idProduct != LA2016_PID)
                        continue;
                if (des.iProduct != LA2016_IPRODUCT_INDEX)
                        continue;
 
-               if ((sdi->status == SR_ST_INITIALIZING) || (sdi->status == SR_ST_INACTIVE)) {
+               check_conn = sdi->status == SR_ST_INITIALIZING;
+               check_conn |= sdi->status == SR_ST_INACTIVE;
+               if (check_conn) {
                        /* Check physical USB bus/port address. */
-                       if (usb_get_port_path(devlist[i], connection_id, sizeof(connection_id)) < 0)
+                       ret = usb_get_port_path(dev, conn_id, sizeof(conn_id));
+                       if (ret < 0)
                                continue;
-
-                       if (strcmp(sdi->connection_id, connection_id)) {
+                       if (strcmp(sdi->connection_id, conn_id) != 0) {
                                /* Not the device we looked up before. */
                                continue;
                        }
                }
 
-               if (!(ret = libusb_open(devlist[i], &usb->devhdl))) {
-                       if (usb->address == 0xff) {
-                               /*
-                                * First encounter after firmware upload.
-                                * Grab current address after enumeration.
-                                */
-                               usb->address = libusb_get_device_address(devlist[i]);
-                       }
-               } else {
-                       sr_err("Failed to open device: %s.", libusb_error_name(ret));
-                       ret = SR_ERR;
+               ret = libusb_open(dev, &usb->devhdl);
+               if (ret != 0) {
+                       sr_err("Cannot open device: %s.",
+                               libusb_error_name(ret));
+                       ret = SR_ERR_IO;
                        break;
                }
 
+               if (usb->address == 0xff) {
+                       /*
+                        * First encounter after firmware upload.
+                        * Grab current address after enumeration.
+                        */
+                       usb->address = libusb_get_device_address(dev);
+               }
+
                ret = libusb_claim_interface(usb->devhdl, USB_INTERFACE);
                if (ret == LIBUSB_ERROR_BUSY) {
                        sr_err("Cannot claim USB interface. Another program or driver using it?");
@@ -303,7 +352,8 @@ static int la2016_dev_open(struct sr_dev_inst *sdi)
                        ret = SR_ERR;
                        break;
                } else if (ret != 0) {
-                       sr_err("Cannot claim USB interface: %s.", libusb_error_name(ret));
+                       sr_err("Cannot claim USB interface: %s.",
+                               libusb_error_name(ret));
                        ret = SR_ERR;
                        break;
                }
@@ -315,12 +365,9 @@ static int la2016_dev_open(struct sr_dev_inst *sdi)
 
                sr_info("Opened device on %d.%d (logical) / %s (physical), interface %d.",
                        usb->bus, usb->address, sdi->connection_id, USB_INTERFACE);
-
                ret = SR_OK;
-
                break;
        }
-
        libusb_free_device_list(devlist, 1);
 
        if (ret != SR_OK) {
@@ -329,7 +376,7 @@ static int la2016_dev_open(struct sr_dev_inst *sdi)
                        libusb_close(usb->devhdl);
                        usb->devhdl = NULL;
                }
-               return SR_ERR;
+               return ret;
        }
 
        return SR_OK;
@@ -371,7 +418,7 @@ static int dev_open(struct sr_dev_inst *sdi)
                } while (elapsed_ms < RENUM_CHECK_PERIOD_MS);
                if (ret != SR_OK) {
                        sr_err("Device failed to re-enumerate.");
-                       return SR_ERR;
+                       return ret;
                }
                sr_info("Device came back after %" PRIi64 "ms.", elapsed_ms);
        } else {
@@ -380,7 +427,7 @@ static int dev_open(struct sr_dev_inst *sdi)
 
        if (ret != SR_OK) {
                sr_err("Cannot open device.");
-               return SR_ERR;
+               return ret;
        }
 
        return SR_OK;
@@ -438,8 +485,8 @@ static int config_get(uint32_t key, GVariant **data,
                *data = g_variant_new_uint64(devc->cur_samplerate);
                break;
        case SR_CONF_LIMIT_SAMPLES:
-               *data = g_variant_new_uint64(devc->limit_samples);
-               break;
+       case SR_CONF_LIMIT_MSEC:
+               return sr_sw_limits_config_get(&devc->sw_limits, key, data);
        case SR_CONF_CAPTURE_RATIO:
                *data = g_variant_new_uint64(devc->capture_ratio);
                break;
@@ -478,8 +525,8 @@ static int config_set(uint32_t key, GVariant *data,
                devc->cur_samplerate = g_variant_get_uint64(data);
                break;
        case SR_CONF_LIMIT_SAMPLES:
-               devc->limit_samples = g_variant_get_uint64(data);
-               break;
+       case SR_CONF_LIMIT_MSEC:
+               return sr_sw_limits_config_set(&devc->sw_limits, key, data);
        case SR_CONF_CAPTURE_RATIO:
                devc->capture_ratio = g_variant_get_uint64(data);
                break;
@@ -489,7 +536,8 @@ static int config_set(uint32_t key, GVariant *data,
                devc->threshold_voltage_idx = LOGIC_THRESHOLD_IDX_USER;
                break;
        case SR_CONF_LOGIC_THRESHOLD: {
-               if ((idx = std_str_idx(data, ARRAY_AND_SIZE(logic_threshold))) < 0)
+               idx = std_str_idx(data, ARRAY_AND_SIZE(logic_threshold));
+               if (idx < 0)
                        return SR_ERR_ARG;
                if (idx != LOGIC_THRESHOLD_IDX_USER) {
                        devc->threshold_voltage = logic_threshold_value[idx];
@@ -512,14 +560,16 @@ static int config_list(uint32_t key, GVariant **data,
 {
        struct dev_context *devc;
 
+       devc = sdi ? sdi->priv : NULL;
+
        switch (key) {
        case SR_CONF_SCAN_OPTIONS:
        case SR_CONF_DEVICE_OPTIONS:
-               return STD_CONFIG_LIST(key, data, sdi, cg, scanopts, drvopts, devopts);
+               return STD_CONFIG_LIST(key, data, sdi, cg,
+                       scanopts, drvopts, devopts);
        case SR_CONF_SAMPLERATE:
                if (!sdi)
                        return SR_ERR_ARG;
-               devc = sdi->priv;
                if (devc->max_samplerate == SR_MHZ(200)) {
                        *data = std_gvar_samplerates(ARRAY_AND_SIZE(samplerates_la2016));
                } else {
@@ -527,7 +577,8 @@ static int config_list(uint32_t key, GVariant **data,
                }
                break;
        case SR_CONF_LIMIT_SAMPLES:
-               *data = std_gvar_tuple_u64(LA2016_NUM_SAMPLES_MIN, LA2016_NUM_SAMPLES_MAX);
+               *data = std_gvar_tuple_u64(LA2016_NUM_SAMPLES_MIN,
+                       LA2016_NUM_SAMPLES_MAX);
                break;
        case SR_CONF_VOLTAGE_THRESHOLD:
                *data = std_gvar_min_max_step_thresholds(
@@ -547,62 +598,47 @@ static int config_list(uint32_t key, GVariant **data,
        return SR_OK;
 }
 
-static int configure_channels(const struct sr_dev_inst *sdi)
-{
-       struct dev_context *devc;
-
-       devc = sdi->priv;
-       devc->cur_channels = 0;
-       devc->num_channels = 0;
-
-       for (GSList *l = sdi->channels; l; l = l->next) {
-               struct sr_channel *ch = (struct sr_channel*)l->data;
-               if (ch->enabled == FALSE)
-                       continue;
-               devc->cur_channels |= 1 << ch->index;
-               devc->num_channels++;
-       }
-
-       return SR_OK;
-}
-
 static int dev_acquisition_start(const struct sr_dev_inst *sdi)
 {
        struct sr_dev_driver *di;
        struct drv_context *drvc;
+       struct sr_context *ctx;
        struct dev_context *devc;
        int ret;
 
        di = sdi->driver;
        drvc = di->context;
+       ctx = drvc->sr_ctx;;
        devc = sdi->priv;
 
-       if (configure_channels(sdi) != SR_OK) {
-               sr_err("Cannot configure channels.");
-               return SR_ERR;
+       if (!devc->feed_queue) {
+               devc->feed_queue = feed_queue_logic_alloc(sdi,
+                       LA2016_CONVBUFFER_SIZE, sizeof(uint16_t));
+               if (!devc->feed_queue) {
+                       sr_err("Cannot allocate buffer for session feed.");
+                       return SR_ERR_MALLOC;
+               }
        }
 
-       devc->convbuffer_size = LA2016_CONVBUFFER_SIZE;
-       if (!(devc->convbuffer = g_try_malloc(devc->convbuffer_size))) {
-               sr_err("Cannot allocate conversion buffer.");
-               return SR_ERR_MALLOC;
-       }
+       sr_sw_limits_acquisition_start(&devc->sw_limits);
 
-       if ((ret = la2016_setup_acquisition(sdi)) != SR_OK) {
-               g_free(devc->convbuffer);
-               devc->convbuffer = NULL;
+       ret = la2016_setup_acquisition(sdi);
+       if (ret != SR_OK) {
+               feed_queue_logic_free(devc->feed_queue);
+               devc->feed_queue = NULL;
                return ret;
        }
 
-       devc->ctx = drvc->sr_ctx;
-
-       if ((ret = la2016_start_acquisition(sdi)) != SR_OK) {
+       ret = la2016_start_acquisition(sdi);
+       if (ret != SR_OK) {
                la2016_abort_acquisition(sdi);
+               feed_queue_logic_free(devc->feed_queue);
+               devc->feed_queue = NULL;
                return ret;
        }
 
-       devc->have_trigger = 0;
-       usb_source_add(sdi->session, drvc->sr_ctx, 50,
+       devc->completion_seen = FALSE;
+       usb_source_add(sdi->session, ctx, 50,
                la2016_receive_data, (void *)sdi);
 
        std_session_send_df_header(sdi);