]> sigrok.org Git - libsigrok.git/blobdiff - hardware/fx2lafw/api.c
build: Portability fixes.
[libsigrok.git] / hardware / fx2lafw / api.c
index 0c591428cbb0963b93056af0da8474302e7d45e2..3242afcead486cb01dc4a7490d37101f6f4a4871 100644 (file)
@@ -28,21 +28,21 @@ static const struct fx2lafw_profile supported_fx2[] = {
         */
        { 0x08a9, 0x0014, "CWAV", "USBee AX", NULL,
                FIRMWARE_DIR "/fx2lafw-cwav-usbeeax.fw",
-               0 },
+               0, NULL, NULL},
        /*
         * CWAV USBee DX
         * XZL-Studio DX
         */
        { 0x08a9, 0x0015, "CWAV", "USBee DX", NULL,
                FIRMWARE_DIR "/fx2lafw-cwav-usbeedx.fw",
-               DEV_CAPS_16BIT },
+               DEV_CAPS_16BIT, NULL, NULL },
 
        /*
         * CWAV USBee SX
         */
        { 0x08a9, 0x0009, "CWAV", "USBee SX", NULL,
                FIRMWARE_DIR "/fx2lafw-cwav-usbeesx.fw",
-               0 },
+               0, NULL, NULL},
 
        /*
         * Saleae Logic
@@ -52,7 +52,7 @@ static const struct fx2lafw_profile supported_fx2[] = {
         */
        { 0x0925, 0x3881, "Saleae", "Logic", NULL,
                FIRMWARE_DIR "/fx2lafw-saleae-logic.fw",
-               0 },
+               0, NULL, NULL},
 
        /*
         * Default Cypress FX2 without EEPROM, e.g.:
@@ -61,16 +61,16 @@ static const struct fx2lafw_profile supported_fx2[] = {
         */
        { 0x04B4, 0x8613, "Cypress", "FX2", NULL,
                FIRMWARE_DIR "/fx2lafw-cypress-fx2.fw",
-               DEV_CAPS_16BIT },
+               DEV_CAPS_16BIT, NULL, NULL },
 
        /*
         * Braintechnology USB-LPS
         */
        { 0x16d0, 0x0498, "Braintechnology", "USB-LPS", NULL,
                FIRMWARE_DIR "/fx2lafw-braintechnology-usb-lps.fw",
-               DEV_CAPS_16BIT },
+               DEV_CAPS_16BIT, NULL, NULL },
 
-       { 0, 0, 0, 0, 0, 0, 0 }
+       { 0, 0, 0, 0, 0, 0, 0, 0, 0 }
 };
 
 static const int32_t hwopts[] = {
@@ -79,7 +79,7 @@ static const int32_t hwopts[] = {
 
 static const int32_t hwcaps[] = {
        SR_CONF_LOGIC_ANALYZER,
-       SR_CONF_TRIGGER_TYPE,
+       SR_CONF_TRIGGER_MATCH,
        SR_CONF_SAMPLERATE,
 
        /* These are really implemented in the driver, not the hardware. */
@@ -87,12 +87,20 @@ static const int32_t hwcaps[] = {
        SR_CONF_CONTINUOUS,
 };
 
-static const char *probe_names[] = {
+static const char *channel_names[] = {
        "0",  "1",  "2",  "3",  "4",  "5",  "6",  "7",
        "8",  "9", "10", "11", "12", "13", "14", "15",
        NULL,
 };
 
+static const int32_t soft_trigger_matches[] = {
+       SR_TRIGGER_ZERO,
+       SR_TRIGGER_ONE,
+       SR_TRIGGER_RISING,
+       SR_TRIGGER_FALLING,
+       SR_TRIGGER_EDGE,
+};
+
 static const uint64_t samplerates[] = {
        SR_KHZ(20),
        SR_KHZ(25),
@@ -115,11 +123,6 @@ static const uint64_t samplerates[] = {
 SR_PRIV struct sr_dev_driver fx2lafw_driver_info;
 static struct sr_dev_driver *di = &fx2lafw_driver_info;
 
-static int dev_clear(void)
-{
-       return std_dev_clear(di, NULL);
-}
-
 static int init(struct sr_context *sr_ctx)
 {
        return std_init(sr_ctx, di, LOG_PREFIX);
@@ -131,14 +134,16 @@ static GSList *scan(GSList *options)
        struct dev_context *devc;
        struct sr_dev_inst *sdi;
        struct sr_usb_dev_inst *usb;
-       struct sr_probe *probe;
+       struct sr_channel *ch;
        struct sr_config *src;
        const struct fx2lafw_profile *prof;
        GSList *l, *devices, *conn_devices;
        struct libusb_device_descriptor des;
        libusb_device **devlist;
-       int devcnt, num_logic_probes, ret, i, j;
+       struct libusb_device_handle *hdl;
+       int devcnt, num_logic_channels, ret, i, j;
        const char *conn;
+       char manufacturer[64], product[64];
 
        drvc = di->priv;
 
@@ -180,11 +185,41 @@ static GSList *scan(GSList *options)
                        continue;
                }
 
+               if ((ret = libusb_open(devlist[i], &hdl)) < 0)
+                       continue;
+
+               if (des.iManufacturer == 0) {
+                       manufacturer[0] = '\0';
+               } else if ((ret = libusb_get_string_descriptor_ascii(hdl,
+                               des.iManufacturer, (unsigned char *) manufacturer,
+                               sizeof(manufacturer))) < 0) {
+                       sr_warn("Failed to get manufacturer string descriptor: %s.",
+                               libusb_error_name(ret));
+                       continue;
+               }
+
+               if (des.iProduct == 0) {
+                       product[0] = '\0';
+               } else if ((ret = libusb_get_string_descriptor_ascii(hdl,
+                               des.iProduct, (unsigned char *) product,
+                               sizeof(product))) < 0) {
+                       sr_warn("Failed to get product string descriptor: %s.",
+                               libusb_error_name(ret));
+                       continue;
+               }
+
+               libusb_close(hdl);
+
                prof = NULL;
                for (j = 0; supported_fx2[j].vid; j++) {
                        if (des.idVendor == supported_fx2[j].vid &&
-                               des.idProduct == supported_fx2[j].pid) {
+                                       des.idProduct == supported_fx2[j].pid &&
+                                       (!supported_fx2[j].usb_manufacturer ||
+                                        !strcmp(manufacturer, supported_fx2[j].usb_manufacturer)) &&
+                                       (!supported_fx2[j].usb_manufacturer ||
+                                        !strcmp(product, supported_fx2[j].usb_product))) {
                                prof = &supported_fx2[j];
+                               break;
                        }
                }
 
@@ -199,13 +234,13 @@ static GSList *scan(GSList *options)
                        return NULL;
                sdi->driver = di;
 
-               /* Fill in probelist according to this device's profile. */
-               num_logic_probes = prof->dev_caps & DEV_CAPS_16BIT ? 16 : 8;
-               for (j = 0; j < num_logic_probes; j++) {
-                       if (!(probe = sr_probe_new(j, SR_PROBE_LOGIC, TRUE,
-                                       probe_names[j])))
+               /* Fill in channellist according to this device's profile. */
+               num_logic_channels = prof->dev_caps & DEV_CAPS_16BIT ? 16 : 8;
+               for (j = 0; j < num_logic_channels; j++) {
+                       if (!(ch = sr_channel_new(j, SR_CHANNEL_LOGIC, TRUE,
+                                       channel_names[j])))
                                return NULL;
-                       sdi->probes = g_slist_append(sdi->probes, probe);
+                       sdi->channels = g_slist_append(sdi->channels, ch);
                }
 
                devc = fx2lafw_dev_new();
@@ -230,7 +265,7 @@ static GSList *scan(GSList *options)
                                sr_err("Firmware upload failed for "
                                       "device %d.", devcnt);
                        sdi->inst_type = SR_INST_USB;
-                       sdi->conn = sr_usb_dev_inst_new (libusb_get_bus_number(devlist[i]),
+                       sdi->conn = sr_usb_dev_inst_new(libusb_get_bus_number(devlist[i]),
                                        0xff, NULL);
                }
        }
@@ -291,7 +326,7 @@ static int dev_open(struct sr_dev_inst *sdi)
 
        ret = libusb_claim_interface(usb->devhdl, USB_INTERFACE);
        if (ret != 0) {
-               switch(ret) {
+               switch (ret) {
                case LIBUSB_ERROR_BUSY:
                        sr_err("Unable to claim USB interface. Another "
                               "program or driver has already claimed it.");
@@ -342,7 +377,7 @@ static int cleanup(void)
        if (!(drvc = di->priv))
                return SR_OK;
 
-       ret = dev_clear();
+       ret = std_dev_clear(di, NULL);
 
        g_free(drvc);
        di->priv = NULL;
@@ -351,17 +386,22 @@ static int cleanup(void)
 }
 
 static int config_get(int id, GVariant **data, const struct sr_dev_inst *sdi,
-               const struct sr_probe_group *probe_group)
+               const struct sr_channel_group *cg)
 {
        struct dev_context *devc;
        struct sr_usb_dev_inst *usb;
        char str[128];
 
-       (void)probe_group;
+       (void)cg;
+
+       if (!sdi)
+               return SR_ERR_ARG;
+
+       devc = sdi->priv;
 
        switch (id) {
        case SR_CONF_CONN:
-               if (!sdi || !sdi->conn)
+               if (!sdi->conn)
                        return SR_ERR_ARG;
                usb = sdi->conn;
                if (usb->address == 255)
@@ -371,10 +411,10 @@ static int config_get(int id, GVariant **data, const struct sr_dev_inst *sdi,
                snprintf(str, 128, "%d.%d", usb->bus, usb->address);
                *data = g_variant_new_string(str);
                break;
+       case SR_CONF_LIMIT_SAMPLES:
+               *data = g_variant_new_uint64(devc->limit_samples);
+               break;
        case SR_CONF_SAMPLERATE:
-               if (!sdi)
-                       return SR_ERR;
-               devc = sdi->priv;
                *data = g_variant_new_uint64(devc->cur_samplerate);
                break;
        default:
@@ -385,39 +425,46 @@ static int config_get(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)
+               const struct sr_channel_group *cg)
 {
        struct dev_context *devc;
        int ret;
 
-       (void)probe_group;
+       (void)cg;
+
+       if (!sdi)
+               return SR_ERR_ARG;
 
        if (sdi->status != SR_ST_ACTIVE)
                return SR_ERR;
 
        devc = sdi->priv;
 
-       if (id == SR_CONF_SAMPLERATE) {
-               devc->cur_samplerate = g_variant_get_uint64(data);
-               ret = SR_OK;
-       } else if (id == SR_CONF_LIMIT_SAMPLES) {
-               devc->limit_samples = g_variant_get_uint64(data);
-               ret = SR_OK;
-       } else {
-               ret = SR_ERR_NA;
+       ret = SR_OK;
+
+       switch (id)
+       {
+               case SR_CONF_SAMPLERATE:
+                       devc->cur_samplerate = g_variant_get_uint64(data);
+                       break;
+               case SR_CONF_LIMIT_SAMPLES:
+                       devc->limit_samples = g_variant_get_uint64(data);
+                       break;
+               default:
+                       ret = SR_ERR_NA;
        }
 
        return ret;
 }
 
 static int config_list(int key, GVariant **data, const struct sr_dev_inst *sdi,
-               const struct sr_probe_group *probe_group)
+               const struct sr_channel_group *cg)
 {
        GVariant *gvar;
        GVariantBuilder gvb;
 
        (void)sdi;
-       (void)probe_group;
+       (void)cg;
 
        switch (key) {
        case SR_CONF_SCAN_OPTIONS:
@@ -435,8 +482,10 @@ static int config_list(int key, GVariant **data, const struct sr_dev_inst *sdi,
                g_variant_builder_add(&gvb, "{sv}", "samplerates", gvar);
                *data = g_variant_builder_end(&gvb);
                break;
-       case SR_CONF_TRIGGER_TYPE:
-               *data = g_variant_new_string(TRIGGER_TYPE);
+       case SR_CONF_TRIGGER_MATCH:
+               *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32,
+                               soft_trigger_matches, ARRAY_SIZE(soft_trigger_matches),
+                               sizeof(int32_t));
                break;
        default:
                return SR_ERR_NA;
@@ -467,6 +516,7 @@ static int dev_acquisition_start(const struct sr_dev_inst *sdi, void *cb_data)
        struct dev_context *devc;
        struct drv_context *drvc;
        struct sr_usb_dev_inst *usb;
+       struct sr_trigger *trigger;
        struct libusb_transfer *transfer;
        unsigned int i, timeout, num_transfers;
        int ret;
@@ -480,16 +530,17 @@ static int dev_acquisition_start(const struct sr_dev_inst *sdi, void *cb_data)
        devc = sdi->priv;
        usb = sdi->conn;
 
-       /* Configures devc->trigger_* and devc->sample_wide */
-       if (fx2lafw_configure_probes(sdi) != SR_OK) {
-               sr_err("Failed to configure probes.");
-               return SR_ERR;
-       }
-
        devc->cb_data = cb_data;
-       devc->num_samples = 0;
+       devc->sent_samples = 0;
+       devc->acq_aborted = FALSE;
        devc->empty_transfer_count = 0;
 
+       if ((trigger = sr_session_trigger_get(sdi->session))) {
+               devc->stl = soft_trigger_logic_new(sdi, trigger);
+               devc->trigger_fired = FALSE;
+       } else
+               devc->trigger_fired = TRUE;
+
        timeout = fx2lafw_get_timeout(devc);
        num_transfers = fx2lafw_get_number_of_transfers(devc);
        size = fx2lafw_get_buffer_size(devc);
@@ -510,7 +561,7 @@ static int dev_acquisition_start(const struct sr_dev_inst *sdi, void *cb_data)
                transfer = libusb_alloc_transfer(0);
                libusb_fill_bulk_transfer(transfer, usb->devhdl,
                                2 | LIBUSB_ENDPOINT_IN, buf, size,
-                               fx2lafw_receive_transfer, devc, timeout);
+                               fx2lafw_receive_transfer, (void *)sdi, timeout);
                if ((ret = libusb_submit_transfer(transfer)) != 0) {
                        sr_err("Failed to submit transfer: %s.",
                               libusb_error_name(ret));
@@ -525,13 +576,12 @@ static int dev_acquisition_start(const struct sr_dev_inst *sdi, void *cb_data)
 
        devc->ctx = drvc->sr_ctx;
 
-       usb_source_add(devc->ctx, timeout, receive_data, NULL);
+       usb_source_add(sdi->session, devc->ctx, timeout, receive_data, NULL);
 
        /* Send header packet to the session bus. */
        std_session_send_df_header(cb_data, LOG_PREFIX);
 
-       if ((ret = fx2lafw_command_start_acquisition(usb->devhdl,
-               devc->cur_samplerate, devc->sample_wide)) != SR_OK) {
+       if ((ret = fx2lafw_command_start_acquisition(sdi)) != SR_OK) {
                fx2lafw_abort_acquisition(devc);
                return ret;
        }
@@ -556,7 +606,7 @@ SR_PRIV struct sr_dev_driver fx2lafw_driver_info = {
        .cleanup = cleanup,
        .scan = scan,
        .dev_list = dev_list,
-       .dev_clear = dev_clear,
+       .dev_clear = NULL,
        .config_get = config_get,
        .config_set = config_set,
        .config_list = config_list,