]> sigrok.org Git - libsigrok.git/blobdiff - hardware/fx2lafw/fx2lafw.c
sr/drivers: change driver dev_acquisition_start/_stop calls to use sdi
[libsigrok.git] / hardware / fx2lafw / fx2lafw.c
index d7fdf2f52c36c433ae0d2de086cb062173fdc31e..87edea6f52ba936580381225b1200c13851033ac 100644 (file)
@@ -143,8 +143,11 @@ static libusb_context *usb_context = NULL;
 
 SR_PRIV struct sr_dev_driver fx2lafw_driver_info;
 static struct sr_dev_driver *fdi = &fx2lafw_driver_info;
-static int hw_dev_config_set(int dev_index, int hwcap, const void *value);
-static int hw_dev_acquisition_stop(int dev_index, void *cb_data);
+static int hw_dev_close(struct sr_dev_inst *sdi);
+static int hw_dev_config_set(const struct sr_dev_inst *sdi, int hwcap,
+               const void *value);
+static int hw_dev_acquisition_stop(const struct sr_dev_inst *sdi,
+               void *cb_data);
 
 /**
  * Check the USB configuration to determine if this is an fx2lafw device.
@@ -190,18 +193,15 @@ static gboolean check_conf_profile(libusb_device *dev)
        return ret;
 }
 
-static int fx2lafw_dev_open(int dev_index)
+static int fx2lafw_dev_open(struct sr_dev_inst *sdi)
 {
        libusb_device **devlist;
        struct libusb_device_descriptor des;
-       struct sr_dev_inst *sdi;
        struct context *ctx;
        struct version_info vi;
        int ret, skip, i;
        uint8_t revid;
 
-       if (!(sdi = sr_dev_inst_get(fdi->instances, dev_index)))
-               return SR_ERR;
        ctx = sdi->priv;
 
        if (sdi->status == SR_ST_ACTIVE)
@@ -228,7 +228,7 @@ static int fx2lafw_dev_open(int dev_index)
                        continue;
 
                if (sdi->status == SR_ST_INITIALIZING) {
-                       if (skip != dev_index) {
+                       if (skip != sdi->index) {
                                /* Skip devices of this type that aren't the one we want. */
                                skip += 1;
                                continue;
@@ -297,23 +297,6 @@ static int fx2lafw_dev_open(int dev_index)
        return SR_OK;
 }
 
-static void close_dev(struct sr_dev_inst *sdi)
-{
-       struct context *ctx;
-
-       ctx = sdi->priv;
-
-       if (ctx->usb->devhdl == NULL)
-               return;
-
-       sr_info("fx2lafw: Closing device %d on %d.%d interface %d.",
-               sdi->index, ctx->usb->bus, ctx->usb->address, USB_INTERFACE);
-       libusb_release_interface(ctx->usb->devhdl, USB_INTERFACE);
-       libusb_close(ctx->usb->devhdl);
-       ctx->usb->devhdl = NULL;
-       sdi->status = SR_ST_INACTIVE;
-}
-
 static int configure_probes(struct context *ctx, GSList *probes)
 {
        struct sr_probe *probe;
@@ -399,7 +382,7 @@ static int clear_instances(void)
                        ret = SR_ERR_BUG;
                        continue;
                }
-               close_dev(sdi);
+               hw_dev_close(sdi);
                sdi = l->data;
                sr_dev_inst_free(sdi);
        }
@@ -470,7 +453,7 @@ static GSList *hw_scan(GSList *options)
                sdi = sr_dev_inst_new(devcnt, SR_ST_INITIALIZING,
                        prof->vendor, prof->model, prof->model_version);
                if (!sdi)
-                       return 0;
+                       return NULL;
                sdi->driver = fdi;
 
                /* Fill in probelist according to this device's profile. */
@@ -478,7 +461,7 @@ static GSList *hw_scan(GSList *options)
                for (j = 0; j < num_logic_probes; j++) {
                        if (!(probe = sr_probe_new(j, SR_PROBE_LOGIC, TRUE,
                                        probe_names[j])))
-                               return 0;
+                               return NULL;
                        sdi->probes = g_slist_append(sdi->probes, probe);
                }
 
@@ -512,15 +495,12 @@ static GSList *hw_scan(GSList *options)
        return devices;
 }
 
-static int hw_dev_open(int dev_index)
+static int hw_dev_open(struct sr_dev_inst *sdi)
 {
-       struct sr_dev_inst *sdi;
        struct context *ctx;
        int ret;
        int64_t timediff_us, timediff_ms;
 
-       if (!(sdi = sr_dev_inst_get(fdi->instances, dev_index)))
-               return SR_ERR;
        ctx = sdi->priv;
 
        /*
@@ -534,7 +514,7 @@ static int hw_dev_open(int dev_index)
                g_usleep(300 * 1000);
                timediff_ms = 0;
                while (timediff_ms < MAX_RENUM_DELAY_MS) {
-                       if ((ret = fx2lafw_dev_open(dev_index)) == SR_OK)
+                       if ((ret = fx2lafw_dev_open(sdi)) == SR_OK)
                                break;
                        g_usleep(100 * 1000);
 
@@ -544,7 +524,7 @@ static int hw_dev_open(int dev_index)
                }
                sr_info("fx2lafw: Device came back after %d ms.", timediff_ms);
        } else {
-               ret = fx2lafw_dev_open(dev_index);
+               ret = fx2lafw_dev_open(sdi);
        }
 
        if (ret != SR_OK) {
@@ -575,7 +555,7 @@ static int hw_dev_open(int dev_index)
 
        if (ctx->cur_samplerate == 0) {
                /* Samplerate hasn't been set; default to the slowest one. */
-               if (hw_dev_config_set(dev_index, SR_HWCAP_SAMPLERATE,
+               if (hw_dev_config_set(sdi, SR_HWCAP_SAMPLERATE,
                    &supported_samplerates[0]) == SR_ERR)
                        return SR_ERR;
        }
@@ -583,17 +563,20 @@ static int hw_dev_open(int dev_index)
        return SR_OK;
 }
 
-static int hw_dev_close(int dev_index)
+static int hw_dev_close(struct sr_dev_inst *sdi)
 {
-       struct sr_dev_inst *sdi;
+       struct context *ctx;
 
-       if (!(sdi = sr_dev_inst_get(fdi->instances, dev_index))) {
-               sr_err("fx2lafw: %s: sdi was NULL.", __func__);
-               return SR_ERR_BUG;
-       }
+       ctx = sdi->priv;
+       if (ctx->usb->devhdl == NULL)
+               return SR_ERR;
 
-       /* TODO */
-       close_dev(sdi);
+       sr_info("fx2lafw: Closing device %d on %d.%d interface %d.",
+               sdi->index, ctx->usb->bus, ctx->usb->address, USB_INTERFACE);
+       libusb_release_interface(ctx->usb->devhdl, USB_INTERFACE);
+       libusb_close(ctx->usb->devhdl);
+       ctx->usb->devhdl = NULL;
+       sdi->status = SR_ST_INACTIVE;
 
        return SR_OK;
 }
@@ -666,14 +649,12 @@ static int hw_dev_status_get(int dev_index)
        return sdi->status;
 }
 
-static int hw_dev_config_set(int dev_index, int hwcap, const void *value)
+static int hw_dev_config_set(const struct sr_dev_inst *sdi, int hwcap,
+               const void *value)
 {
-       struct sr_dev_inst *sdi;
        struct context *ctx;
        int ret;
 
-       if (!(sdi = sr_dev_inst_get(fdi->instances, dev_index)))
-               return SR_ERR;
        ctx = sdi->priv;
 
        if (hwcap == SR_HWCAP_SAMPLERATE) {
@@ -948,9 +929,9 @@ static unsigned int get_timeout(struct context *ctx)
        return timeout + timeout / 4; /* Leave a headroom of 25% percent */
 }
 
-static int hw_dev_acquisition_start(int dev_index, void *cb_data)
+static int hw_dev_acquisition_start(const struct sr_dev_inst *sdi,
+               void *cb_data)
 {
-       struct sr_dev_inst *sdi;
        struct sr_datafeed_packet packet;
        struct sr_datafeed_header header;
        struct sr_datafeed_meta_logic meta;
@@ -961,10 +942,7 @@ static int hw_dev_acquisition_start(int dev_index, void *cb_data)
        int ret;
        unsigned char *buf;
 
-       if (!(sdi = sr_dev_inst_get(fdi->instances, dev_index)))
-               return SR_ERR;
        ctx = sdi->priv;
-
        if (ctx->submitted_transfers != 0)
                return SR_ERR;
 
@@ -1030,16 +1008,13 @@ static int hw_dev_acquisition_start(int dev_index, void *cb_data)
 }
 
 /* TODO: This stops acquisition on ALL devices, ignoring dev_index. */
-static int hw_dev_acquisition_stop(int dev_index, void *cb_data)
+static int hw_dev_acquisition_stop(const struct sr_dev_inst *sdi,
+               void *cb_data)
 {
-       struct sr_dev_inst *sdi;
 
        /* Avoid compiler warnings. */
        (void)cb_data;
 
-       if (!(sdi = sr_dev_inst_get(fdi->instances, dev_index)))
-               return SR_ERR;
        abort_acquisition(sdi->priv);
 
        return SR_OK;