]> sigrok.org Git - libsigrok.git/blobdiff - hardware/saleae-logic/saleae-logic.c
sr: s/err/ret/ for consistency.
[libsigrok.git] / hardware / saleae-logic / saleae-logic.c
index ebe02b062d41e115c0953f938acd3e8ecb42f15c..50a6944223111659766aa82d51151f8cdd59e107 100644 (file)
@@ -177,7 +177,7 @@ static int sl_open_dev(int dev_index)
        struct libusb_device_descriptor des;
        struct sr_dev_inst *sdi;
        struct context *ctx;
-       int err, skip, i;
+       int ret, skip, i;
 
        if (!(sdi = sr_dev_inst_get(dev_insts, dev_index)))
                return SR_ERR;
@@ -190,8 +190,8 @@ static int sl_open_dev(int dev_index)
        skip = 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("logic: failed to get device descriptor: %d", err);
+               if ((ret = libusb_get_device_descriptor(devlist[i], &des))) {
+                       sr_err("logic: failed to get device descriptor: %d", ret);
                        continue;
                }
 
@@ -216,7 +216,7 @@ static int sl_open_dev(int dev_index)
                                continue;
                }
 
-               if (!(err = libusb_open(devlist[i], &ctx->usb->devhdl))) {
+               if (!(ret = libusb_open(devlist[i], &ctx->usb->devhdl))) {
                        if (ctx->usb->address == 0xff)
                                /*
                                 * first time we touch this device after firmware upload,
@@ -229,7 +229,7 @@ static int sl_open_dev(int dev_index)
                                sdi->index, ctx->usb->bus,
                                ctx->usb->address, USB_INTERFACE);
                } else {
-                       sr_err("logic: failed to open device: %d", err);
+                       sr_err("logic: failed to open device: %d", ret);
                }
 
                /* if we made it here, we handled the device one way or another */
@@ -332,7 +332,7 @@ static int hw_init(const char *devinfo)
        struct fx2_profile *fx2_prof;
        struct context *ctx;
        libusb_device **devlist;
-       int err, devcnt, i, j;
+       int ret, devcnt, i, j;
 
        /* Avoid compiler warnings. */
        (void)devinfo;
@@ -347,10 +347,10 @@ static int hw_init(const char *devinfo)
        libusb_get_device_list(usb_context, &devlist);
        for (i = 0; devlist[i]; i++) {
                fx2_prof = NULL;
-               err = libusb_get_device_descriptor(devlist[i], &des);
-               if (err != 0) {
+               ret = libusb_get_device_descriptor(devlist[i], &des);
+               if (ret != 0) {
                        sr_err("logic: failed to get device descriptor: %d",
-                              err);
+                              ret);
                        continue;
                }
 
@@ -404,7 +404,7 @@ static int hw_dev_open(int dev_index)
        GTimeVal cur_time;
        struct sr_dev_inst *sdi;
        struct context *ctx;
-       int timediff, err;
+       int timediff, ret;
 
        if (!(sdi = sr_dev_inst_get(dev_insts, dev_index)))
                return SR_ERR;
@@ -414,14 +414,14 @@ static int hw_dev_open(int dev_index)
         * if the firmware was recently uploaded, wait up to MAX_RENUM_DELAY ms
         * for the FX2 to renumerate
         */
-       err = 0;
+       ret = 0;
        if (GTV_TO_MSEC(ctx->fw_updated) > 0) {
                sr_info("logic: 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) {
-                       if ((err = sl_open_dev(dev_index)) == SR_OK)
+                       if ((ret = sl_open_dev(dev_index)) == SR_OK)
                                break;
                        g_usleep(100 * 1000);
                        g_get_current_time(&cur_time);
@@ -429,18 +429,18 @@ static int hw_dev_open(int dev_index)
                }
                sr_info("logic: device came back after %d ms", timediff);
        } else {
-               err = sl_open_dev(dev_index);
+               ret = sl_open_dev(dev_index);
        }
 
-       if (err != SR_OK) {
+       if (ret != SR_OK) {
                sr_err("logic: unable to open device");
                return SR_ERR;
        }
        ctx = sdi->priv;
 
-       err = libusb_claim_interface(ctx->usb->devhdl, USB_INTERFACE);
-       if (err != 0) {
-               sr_err("logic: Unable to claim interface: %d", err);
+       ret = libusb_claim_interface(ctx->usb->devhdl, USB_INTERFACE);
+       if (ret != 0) {
+               sr_err("logic: Unable to claim interface: %d", ret);
                return SR_ERR;
        }
 
@@ -460,7 +460,7 @@ static int hw_dev_close(int dev_index)
 
        if (!(sdi = sr_dev_inst_get(dev_insts, dev_index))) {
                sr_err("logic: %s: sdi was NULL", __func__);
-               return SR_ERR; /* TODO: SR_ERR_ARG? */
+               return SR_ERR_BUG;
        }
 
        /* TODO */
@@ -672,26 +672,33 @@ static int receive_data(int fd, int revents, void *cb_data)
        return TRUE;
 }
 
+static void abort_acquisition(struct context *ctx)
+{
+       struct sr_datafeed_packet packet;
+
+       packet.type = SR_DF_END;
+       sr_session_send(ctx->session_dev_id, &packet);
+
+       ctx->num_samples = -1;
+
+       /* TODO: Need to cancel and free any queued up transfers. */
+}
+
 static void receive_transfer(struct libusb_transfer *transfer)
 {
        /* TODO: These statics have to move to the ctx struct. */
-       static int num_samples = 0;
        static int empty_transfer_count = 0;
        struct sr_datafeed_packet packet;
        struct sr_datafeed_logic logic;
-       struct context *ctx;
+       struct context *ctx = transfer->user_data;
        int cur_buflen, trigger_offset, i;
        unsigned char *cur_buf, *new_buf;
 
-       /* hw_dev_acquisition_stop() is telling us to stop. */
-       if (transfer == NULL)
-               num_samples = -1;
-
        /*
         * If acquisition has already ended, just free any queued up
         * transfer that come in.
         */
-       if (num_samples == -1) {
+       if (ctx->num_samples == -1) {
                if (transfer)
                        libusb_free_transfer(transfer);
                return;
@@ -726,7 +733,7 @@ static void receive_transfer(struct libusb_transfer *transfer)
                         * The FX2 gave up. End the acquisition, the frontend
                         * will work out that the samplecount is short.
                         */
-                       hw_dev_acquisition_stop(-1, ctx->session_dev_id);
+                       abort_acquisition(ctx);
                }
                return;
        } else {
@@ -798,9 +805,9 @@ static void receive_transfer(struct libusb_transfer *transfer)
                sr_session_send(ctx->session_dev_id, &packet);
                g_free(cur_buf);
 
-               num_samples += cur_buflen;
-               if (ctx->limit_samples && (unsigned int) num_samples > ctx->limit_samples) {
-                       hw_dev_acquisition_stop(-1, ctx->session_dev_id);
+               ctx->num_samples += cur_buflen;
+               if (ctx->limit_samples && (unsigned int)ctx->num_samples > ctx->limit_samples) {
+                       abort_acquisition(ctx);
                }
        } else {
                /*
@@ -825,6 +832,7 @@ static int hw_dev_acquisition_start(int dev_index, void *cb_data)
                return SR_ERR;
        ctx = sdi->priv;
        ctx->session_dev_id = cb_data;
+       ctx->num_samples = 0;
 
        if (!(packet = g_try_malloc(sizeof(struct sr_datafeed_packet)))) {
                sr_err("logic: %s: packet malloc failed", __func__);
@@ -878,17 +886,15 @@ 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)
 {
-       struct sr_datafeed_packet packet;
-
-       /* Avoid compiler warnings. */
-       (void)dev_index;
+       struct sr_dev_inst *sdi;
 
-       packet.type = SR_DF_END;
-       sr_session_send(cb_data, &packet);
+       /* unused parameter */
+       (void)cb_data;
 
-       receive_transfer(NULL);
+       if (!(sdi = sr_dev_inst_get(dev_insts, dev_index)))
+               return SR_ERR;
 
-       /* TODO: Need to cancel and free any queued up transfers. */
+       abort_acquisition(sdi->priv);
 
        return SR_OK;
 }