]> sigrok.org Git - libsigrok.git/blobdiff - hardware/hantek-dso/api.c
hantek-dso: use driver struct-based device instance list
[libsigrok.git] / hardware / hantek-dso / api.c
index 5701f4d554fea6299ade25c246c1fbe6b3f0b7a6..eccc6e7856a0fd8f30db88551ce2fcf5cd980c81 100644 (file)
 #include <inttypes.h>
 #include <glib.h>
 #include <libusb.h>
-#include "sigrok.h"
-#include "sigrok-internal.h"
+#include "libsigrok.h"
+#include "libsigrok-internal.h"
 #include "config.h"
 #include "dso.h"
 
-/* FIXME: Temporary build fix, this will be removed later. */
-#define GTV_TO_MSEC(gtv)       (gtv.tv_sec * 1000 + gtv.tv_usec / 1000)
 
-/* Max time in ms before we want to check on events */
+/* Max time in ms before we want to check on USB events */
+/* TODO tune this properly */
 #define TICK    1
 
 static const int hwcaps[] = {
@@ -62,12 +61,22 @@ static const char *probe_names[] = {
 };
 
 static const struct dso_profile dev_profiles[] = {
-       {       0x04b4, 0x2090,
-               0x04b5, 0x2090,
+       {       0x04b4, 0x2090, 0x04b5, 0x2090,
                "Hantek", "DSO-2090",
-               NULL, 2,
-               FIRMWARE_DIR "/hantek-dso-2090.fw" },
-       { 0, 0, 0, 0, 0, 0, 0, 0, 0 },
+               FIRMWARE_DIR "/hantek-dso-2xxx.fw" },
+       {       0x04b4, 0x2150, 0x04b5, 0x2150,
+               "Hantek", "DSO-2150",
+               FIRMWARE_DIR "/hantek-dso-2xxx.fw" },
+       {       0x04b4, 0x2250, 0x04b5, 0x2250,
+               "Hantek", "DSO-2250",
+               FIRMWARE_DIR "/hantek-dso-2xxx.fw" },
+       {       0x04b4, 0x5200, 0x04b5, 0x5200,
+               "Hantek", "DSO-5200",
+               FIRMWARE_DIR "/hantek-dso-5xxx.fw" },
+       {       0x04b4, 0x520a, 0x04b5, 0x520a,
+               "Hantek", "DSO-5200A",
+               FIRMWARE_DIR "/hantek-dso-5xxx.fw" },
+       { 0, 0, 0, 0, 0, 0, 0 },
 };
 
 static const uint64_t buffersizes[] = {
@@ -117,6 +126,7 @@ static const char *trigger_sources[] = {
        "CH1",
        "CH2",
        "EXT",
+       /* TODO: forced */
        NULL,
 };
 
@@ -135,7 +145,8 @@ static const char *coupling[] = {
 };
 
 SR_PRIV libusb_context *usb_context = NULL;
-SR_PRIV GSList *dev_insts = NULL;
+SR_PRIV struct sr_dev_driver hantek_dso_driver_info;
+static struct sr_dev_driver *hdi = &hantek_dso_driver_info;
 
 static struct sr_dev_inst *dso_dev_new(int index, const struct dso_profile *prof)
 {
@@ -143,7 +154,7 @@ static struct sr_dev_inst *dso_dev_new(int index, const struct dso_profile *prof
        struct context *ctx;
 
        sdi = sr_dev_inst_new(index, SR_ST_INITIALIZING,
-               prof->vendor, prof->model, prof->model_version);
+               prof->vendor, prof->model, NULL);
        if (!sdi)
                return NULL;
 
@@ -168,7 +179,7 @@ static struct sr_dev_inst *dso_dev_new(int index, const struct dso_profile *prof
        ctx->triggersource = g_strdup(DEFAULT_TRIGGER_SOURCE);
        ctx->triggerposition = DEFAULT_HORIZ_TRIGGERPOS;
        sdi->priv = ctx;
-       dev_insts = g_slist_append(dev_insts, sdi);
+       hdi->instances = g_slist_append(hdi->instances, sdi);
 
        return sdi;
 }
@@ -190,29 +201,32 @@ static int configure_probes(struct context *ctx, const GSList *probes)
        return SR_OK;
 }
 
-static int hw_init(const char *devinfo)
+static int hw_init(void)
+{
+
+       if (libusb_init(&usb_context) != 0) {
+               sr_err("hantek-dso: Failed to initialize USB.");
+               return SR_ERR;
+       }
+
+       return SR_OK;
+}
+
+static int hw_scan(void)
 {
        struct sr_dev_inst *sdi;
        struct libusb_device_descriptor des;
        const struct dso_profile *prof;
        struct context *ctx;
        libusb_device **devlist;
-       int err, devcnt, i, j;
-
-       /* Avoid compiler warnings. */
-       (void)devinfo;
-
-       if (libusb_init(&usb_context) != 0) {
-               sr_err("hantek-dso: Failed to initialize USB.");
-               return 0;
-       }
+       int devcnt, ret, i, j;
 
        /* Find all Hantek DSO devices and upload firmware to all of them. */
        devcnt = 0;
        libusb_get_device_list(usb_context, &devlist);
        for (i = 0; devlist[i]; i++) {
-               if ((err = libusb_get_device_descriptor(devlist[i], &des))) {
-                       sr_err("hantek-dso: failed to get device descriptor: %d", err);
+               if ((ret = libusb_get_device_descriptor(devlist[i], &des))) {
+                       sr_err("hantek-dso: failed to get device descriptor: %d", ret);
                        continue;
                }
 
@@ -228,7 +242,7 @@ static int hw_init(const char *devinfo)
                                if (ezusb_upload_firmware(devlist[i], USB_CONFIGURATION,
                                                prof->firmware) == SR_OK)
                                        /* Remember when the firmware on this device was updated */
-                                       g_get_current_time(&ctx->fw_updated);
+                                       ctx->fw_updated = g_get_monotonic_time();
                                else
                                        sr_err("hantek-dso: firmware upload failed for "
                                               "device %d", devcnt);
@@ -263,33 +277,34 @@ static int hw_init(const char *devinfo)
 
 static int hw_dev_open(int dev_index)
 {
-       GTimeVal cur_time;
        struct sr_dev_inst *sdi;
        struct context *ctx;
-       int timediff, err;
+       int64_t timediff_us, timediff_ms;
+       int err;
 
-       if (!(sdi = sr_dev_inst_get(dev_insts, dev_index)))
+       if (!(sdi = sr_dev_inst_get(hdi->instances, dev_index)))
                return SR_ERR_ARG;
        ctx = sdi->priv;
 
        /*
-        * if the firmware was recently uploaded, wait up to MAX_RENUM_DELAY ms
+        * if the firmware was recently uploaded, wait up to MAX_RENUM_DELAY_MS
         * for the FX2 to renumerate
         */
-       err = 0;
-       if (GTV_TO_MSEC(ctx->fw_updated) > 0) {
+       err = SR_ERR;
+       if (ctx->fw_updated > 0) {
                sr_info("hantek-dso: waiting for device to reset");
                /* takes at least 300ms for the FX2 to be gone from the USB bus */
                g_usleep(300 * 1000);
-               timediff = 0;
-               while (timediff < MAX_RENUM_DELAY) {
+               timediff_ms = 0;
+               while (timediff_ms < MAX_RENUM_DELAY_MS) {
                        if ((err = dso_open(dev_index)) == SR_OK)
                                break;
                        g_usleep(100 * 1000);
-                       g_get_current_time(&cur_time);
-                       timediff = GTV_TO_MSEC(cur_time) - GTV_TO_MSEC(ctx->fw_updated);
+                       timediff_us = g_get_monotonic_time() - ctx->fw_updated;
+                       timediff_ms = timediff_us / 1000;
+                       sr_spew("hantek-dso: waited %" PRIi64 " ms", timediff_ms);
                }
-               sr_info("hantek-dso: device came back after %d ms", timediff);
+               sr_info("hantek-dso: device came back after %d ms", timediff_ms);
        } else {
                err = dso_open(dev_index);
        }
@@ -312,7 +327,7 @@ static int hw_dev_close(int dev_index)
 {
        struct sr_dev_inst *sdi;
 
-       if (!(sdi = sr_dev_inst_get(dev_insts, dev_index)))
+       if (!(sdi = sr_dev_inst_get(hdi->instances, dev_index)))
                return SR_ERR_ARG;
 
        dso_close(sdi);
@@ -327,7 +342,7 @@ static int hw_cleanup(void)
        struct context *ctx;
 
        /* Properly close and free all devices. */
-       for (l = dev_insts; l; l = l->next) {
+       for (l = hdi->instances; l; l = l->next) {
                if (!(sdi = l->data)) {
                        /* Log error, but continue cleaning up the rest. */
                        sr_err("hantek-dso: %s: sdi was NULL, continuing", __func__);
@@ -345,8 +360,8 @@ static int hw_cleanup(void)
                sr_dev_inst_free(sdi);
        }
 
-       g_slist_free(dev_insts);
-       dev_insts = NULL;
+       g_slist_free(hdi->instances);
+       hdi->instances = NULL;
 
        if (usb_context)
                libusb_exit(usb_context);
@@ -358,13 +373,11 @@ static int hw_cleanup(void)
 static const void *hw_dev_info_get(int dev_index, int dev_info_id)
 {
        struct sr_dev_inst *sdi;
-       struct context *ctx;
        const void *info;
        uint64_t tmp;
 
-       if (!(sdi = sr_dev_inst_get(dev_insts, dev_index)))
+       if (!(sdi = sr_dev_inst_get(hdi->instances, dev_index)))
                return NULL;
-       ctx = sdi->priv;
 
        info = NULL;
        switch (dev_info_id) {
@@ -372,7 +385,7 @@ static const void *hw_dev_info_get(int dev_index, int dev_info_id)
                info = sdi;
                break;
        case SR_DI_NUM_PROBES:
-               info = GINT_TO_POINTER(ctx->profile->num_probes);
+               info = GINT_TO_POINTER(NUM_PROBES);
                break;
        case SR_DI_PROBE_NAMES:
                info = probe_names;
@@ -408,7 +421,7 @@ static int hw_dev_status_get(int dev_index)
 {
        struct sr_dev_inst *sdi;
 
-       if (!(sdi = sr_dev_inst_get(dev_insts, dev_index)))
+       if (!(sdi = sr_dev_inst_get(hdi->instances, dev_index)))
                return SR_ST_NOT_FOUND;
 
        return sdi->status;
@@ -429,7 +442,7 @@ static int hw_dev_config_set(int dev_index, int hwcap, const void *value)
        int ret, i;
        char **targets;
 
-       if (!(sdi = sr_dev_inst_get(dev_insts, dev_index)))
+       if (!(sdi = sr_dev_inst_get(hdi->instances, dev_index)))
                return SR_ERR;
 
        if (sdi->status != SR_ST_ACTIVE)
@@ -782,7 +795,7 @@ static int hw_dev_acquisition_start(int dev_index, void *cb_data)
        struct context *ctx;
        int i;
 
-       if (!(sdi = sr_dev_inst_get(dev_insts, dev_index)))
+       if (!(sdi = sr_dev_inst_get(hdi->instances, dev_index)))
                return SR_ERR;
 
        if (sdi->status != SR_ST_ACTIVE)
@@ -814,7 +827,7 @@ static int hw_dev_acquisition_start(int dev_index, void *cb_data)
        /* Send metadata about the SR_DF_ANALOG packets to come. */
        packet.type = SR_DF_META_ANALOG;
        packet.payload = &meta;
-       meta.num_probes = ctx->profile->num_probes;
+       meta.num_probes = NUM_PROBES;
        sr_session_send(cb_data, &packet);
 
        return SR_OK;
@@ -829,7 +842,7 @@ static int hw_dev_acquisition_stop(int dev_index, void *cb_data)
        struct sr_dev_inst *sdi;
        struct context *ctx;
 
-       if (!(sdi = sr_dev_inst_get(dev_insts, dev_index)))
+       if (!(sdi = sr_dev_inst_get(hdi->instances, dev_index)))
                return SR_ERR;
 
        if (sdi->status != SR_ST_ACTIVE)
@@ -850,6 +863,7 @@ SR_PRIV struct sr_dev_driver hantek_dso_driver_info = {
        .api_version = 1,
        .init = hw_init,
        .cleanup = hw_cleanup,
+       .scan = hw_scan,
        .dev_open = hw_dev_open,
        .dev_close = hw_dev_close,
        .dev_info_get = hw_dev_info_get,
@@ -858,4 +872,5 @@ SR_PRIV struct sr_dev_driver hantek_dso_driver_info = {
        .dev_config_set = hw_dev_config_set,
        .dev_acquisition_start = hw_dev_acquisition_start,
        .dev_acquisition_stop = hw_dev_acquisition_stop,
+       .instances = NULL,
 };