]> sigrok.org Git - libsigrok.git/blobdiff - src/hardware/kingst-la2016/protocol.c
kingst-la2016: use a pool of USB bulk transfers, speedup download
[libsigrok.git] / src / hardware / kingst-la2016 / protocol.c
index def77ba40128ce11030bf6cda2714e8dbaaaee85..36625258e4819038c45f0a5a632b6ccdf6c74938 100644 (file)
@@ -943,6 +943,154 @@ SR_PRIV int la2016_upload_firmware(const struct sr_dev_inst *sdi,
        return SR_OK;
 }
 
+static void LIBUSB_CALL receive_transfer(struct libusb_transfer *xfer);
+
+static void la2016_usbxfer_release_cb(gpointer p)
+{
+       struct libusb_transfer *xfer;
+
+       xfer = p;
+       g_free(xfer->buffer);
+       libusb_free_transfer(xfer);
+}
+
+static int la2016_usbxfer_release(const struct sr_dev_inst *sdi)
+{
+       struct dev_context *devc;
+
+       devc = sdi ? sdi->priv : NULL;
+       if (!devc)
+               return SR_ERR_ARG;
+
+       /* Release all USB transfers. */
+       g_slist_free_full(devc->transfers, la2016_usbxfer_release_cb);
+       devc->transfers = NULL;
+
+       return SR_OK;
+}
+
+static int la2016_usbxfer_allocate(const struct sr_dev_inst *sdi)
+{
+       struct dev_context *devc;
+       size_t bufsize, xfercount;
+       uint8_t *buffer;
+       struct libusb_transfer *xfer;
+
+       devc = sdi ? sdi->priv : NULL;
+       if (!devc)
+               return SR_ERR_ARG;
+
+       /* Transfers were already allocated before? */
+       if (devc->transfers)
+               return SR_OK;
+
+       /*
+        * Allocate all USB transfers and their buffers. Arrange for a
+        * buffer size which is within the device's capabilities, and
+        * is a multiple of the USB endpoint's size, to make use of the
+        * RAW_IO performance feature.
+        *
+        * Implementation detail: The LA2016_USB_BUFSZ value happens
+        * to match all those constraints. No additional arithmetics is
+        * required in this location.
+        */
+       bufsize = LA2016_USB_BUFSZ;
+       xfercount = LA2016_USB_XFER_COUNT;
+       while (xfercount--) {
+               buffer = g_try_malloc(bufsize);
+               if (!buffer) {
+                       sr_err("Cannot allocate USB transfer buffer.");
+                       return SR_ERR_MALLOC;
+               }
+               xfer = libusb_alloc_transfer(0);
+               if (!xfer) {
+                       sr_err("Cannot allocate USB transfer.");
+                       g_free(buffer);
+                       return SR_ERR_MALLOC;
+               }
+               xfer->buffer = buffer;
+               devc->transfers = g_slist_append(devc->transfers, xfer);
+       }
+       devc->transfer_bufsize = bufsize;
+
+       return SR_OK;
+}
+
+static int la2016_usbxfer_cancel_all(const struct sr_dev_inst *sdi)
+{
+       struct dev_context *devc;
+       GSList *l;
+       struct libusb_transfer *xfer;
+
+       devc = sdi ? sdi->priv : NULL;
+       if (!devc)
+               return SR_ERR_ARG;
+
+       /* Unconditionally cancel the transfer. Ignore errors. */
+       for (l = devc->transfers; l; l = l->next) {
+               xfer = l->data;
+               if (!xfer)
+                       continue;
+               libusb_cancel_transfer(xfer);
+       }
+
+       return SR_OK;
+}
+
+static int la2016_usbxfer_resubmit(const struct sr_dev_inst *sdi,
+       struct libusb_transfer *xfer)
+{
+       struct dev_context *devc;
+       struct sr_usb_dev_inst *usb;
+       libusb_transfer_cb_fn cb;
+       int ret;
+
+       devc = sdi ? sdi->priv : NULL;
+       usb = sdi ? sdi->conn : NULL;
+       if (!devc || !usb)
+               return SR_ERR_ARG;
+
+       if (!xfer)
+               return SR_ERR_ARG;
+
+       cb = receive_transfer;
+       libusb_fill_bulk_transfer(xfer, usb->devhdl,
+               USB_EP_CAPTURE_DATA | LIBUSB_ENDPOINT_IN,
+               xfer->buffer, devc->transfer_bufsize,
+               cb, (void *)sdi, CAPTURE_TIMEOUT_MS);
+       ret = libusb_submit_transfer(xfer);
+       if (ret != 0) {
+               sr_err("Cannot submit USB transfer: %s.",
+                       libusb_error_name(ret));
+               return SR_ERR_IO;
+       }
+
+       return SR_OK;
+}
+
+static int la2016_usbxfer_submit_all(const struct sr_dev_inst *sdi)
+{
+       struct dev_context *devc;
+       GSList *l;
+       struct libusb_transfer *xfer;
+       int ret;
+
+       devc = sdi ? sdi->priv : NULL;
+       if (!devc)
+               return SR_ERR_ARG;
+
+       for (l = devc->transfers; l; l = l->next) {
+               xfer = l->data;
+               if (!xfer)
+                       return SR_ERR_ARG;
+               ret = la2016_usbxfer_resubmit(sdi, xfer);
+               if (ret != SR_OK)
+                       return ret;
+       }
+
+       return SR_OK;
+}
+
 SR_PRIV int la2016_setup_acquisition(const struct sr_dev_inst *sdi,
        double voltage)
 {
@@ -975,6 +1123,10 @@ SR_PRIV int la2016_start_acquisition(const struct sr_dev_inst *sdi)
 {
        int ret;
 
+       ret = la2016_usbxfer_allocate(sdi);
+       if (ret != SR_OK)
+               return ret;
+
        ret = set_run_mode(sdi, RUNMODE_RUN);
        if (ret != SR_OK)
                return ret;
@@ -996,32 +1148,24 @@ static int la2016_stop_acquisition(const struct sr_dev_inst *sdi)
 SR_PRIV int la2016_abort_acquisition(const struct sr_dev_inst *sdi)
 {
        int ret;
-       struct dev_context *devc;
 
        ret = la2016_stop_acquisition(sdi);
        if (ret != SR_OK)
                return ret;
 
-       devc = sdi ? sdi->priv : NULL;
-       if (devc && devc->transfer)
-               libusb_cancel_transfer(devc->transfer);
+       (void)la2016_usbxfer_cancel_all(sdi);
 
        return SR_OK;
 }
 
-static int la2016_start_download(const struct sr_dev_inst *sdi,
-       libusb_transfer_cb_fn cb)
+static int la2016_start_download(const struct sr_dev_inst *sdi)
 {
        struct dev_context *devc;
-       struct sr_usb_dev_inst *usb;
        int ret;
        uint8_t wrbuf[REG_SAMPLING - REG_BULK]; /* Width of REG_BULK. */
        uint8_t *wrptr;
-       uint32_t to_read;
-       uint8_t *buffer;
 
        devc = sdi->priv;
-       usb = sdi->conn;
 
        ret = get_capture_info(sdi);
        if (ret != SR_OK)
@@ -1052,42 +1196,17 @@ static int la2016_start_download(const struct sr_dev_inst *sdi,
                sr_err("Cannot send USB bulk config.");
                return ret;
        }
-       ret = ctrl_out(sdi, CMD_BULK_START, 0x00, 0, NULL, 0);
+
+       ret = la2016_usbxfer_submit_all(sdi);
        if (ret != SR_OK) {
-               sr_err("Cannot unblock USB bulk transfers.");
+               sr_err("Cannot submit USB bulk transfers.");
                return ret;
        }
 
-       /*
-        * Pick a buffer size for all USB transfers. The buffer size
-        * must be a multiple of the endpoint packet size. And cannot
-        * exceed a maximum value.
-        */
-       to_read = devc->n_bytes_to_read;
-       if (to_read >= LA2016_USB_BUFSZ) /* Multiple transfers. */
-               to_read = LA2016_USB_BUFSZ;
-       to_read += LA2016_EP6_PKTSZ - 1;
-       to_read /= LA2016_EP6_PKTSZ;
-       to_read *= LA2016_EP6_PKTSZ;
-       buffer = g_try_malloc(to_read);
-       if (!buffer) {
-               sr_dbg("USB bulk transfer size %d bytes.", (int)to_read);
-               sr_err("Cannot allocate buffer for USB bulk transfer.");
-               return SR_ERR_MALLOC;
-       }
-
-       devc->transfer = libusb_alloc_transfer(0);
-       libusb_fill_bulk_transfer(devc->transfer,
-               usb->devhdl, USB_EP_CAPTURE_DATA | LIBUSB_ENDPOINT_IN,
-               buffer, to_read, cb, (void *)sdi, DEFAULT_TIMEOUT_MS);
-
-       ret = libusb_submit_transfer(devc->transfer);
-       if (ret != 0) {
-               sr_err("Cannot submit USB transfer: %s.", libusb_error_name(ret));
-               libusb_free_transfer(devc->transfer);
-               devc->transfer = NULL;
-               g_free(buffer);
-               return SR_ERR_IO;
+       ret = ctrl_out(sdi, CMD_BULK_START, 0x00, 0, NULL, 0);
+       if (ret != SR_OK) {
+               sr_err("Cannot start USB bulk transfers.");
+               return ret;
        }
 
        return SR_OK;
@@ -1099,10 +1218,10 @@ static int la2016_start_download(const struct sr_dev_inst *sdi,
  * contain a number of samples (8bit repeat count per 16bit sample data).
  */
 static void send_chunk(struct sr_dev_inst *sdi,
-       const uint8_t *packets, size_t num_xfers)
+       const uint8_t *data_buffer, size_t data_length)
 {
        struct dev_context *devc;
-       size_t num_pkts;
+       size_t num_xfers, num_pkts;
        const uint8_t *rp;
        uint32_t sample_value;
        size_t repetitions;
@@ -1119,8 +1238,20 @@ static void send_chunk(struct sr_dev_inst *sdi,
                devc->trigger_marked = TRUE;
        }
 
+       /*
+        * Adjust the number of remaining bytes to read from the device
+        * before the processing of the currently received chunk affects
+        * the variable which holds the number of received bytes.
+        */
+       if (data_length > devc->n_bytes_to_read)
+               devc->n_bytes_to_read = 0;
+       else
+               devc->n_bytes_to_read -= data_length;
+
+       /* Process the received chunk of capture data. */
        sample_value = 0;
-       rp = packets;
+       rp = data_buffer;
+       num_xfers = data_length / TRANSFER_PACKET_LENGTH;
        while (num_xfers--) {
                num_pkts = devc->packets_per_chunk;
                while (num_pkts--) {
@@ -1153,6 +1284,18 @@ static void send_chunk(struct sr_dev_inst *sdi,
                (void)read_u8_inc(&rp); /* Skip sequence number. */
        }
 
+       /*
+        * Check for several conditions which shall terminate the
+        * capture data download: When the amount of capture data in
+        * the device is exhausted. When the user specified samples
+        * count limit is reached.
+        */
+       if (!devc->n_bytes_to_read) {
+               devc->download_finished = TRUE;
+       } else {
+               sr_dbg("%" PRIu32 " more bytes to download from the device.",
+                       devc->n_bytes_to_read);
+       }
        if (!devc->download_finished && sr_sw_limits_check(&devc->sw_limits)) {
                sr_dbg("Acquisition limit reached.");
                devc->download_finished = TRUE;
@@ -1168,14 +1311,13 @@ static void LIBUSB_CALL receive_transfer(struct libusb_transfer *transfer)
 {
        struct sr_dev_inst *sdi;
        struct dev_context *devc;
-       struct sr_usb_dev_inst *usb;
-       size_t num_xfers;
+       gboolean was_cancelled;
        int ret;
 
        sdi = transfer->user_data;
        devc = sdi->priv;
-       usb = sdi->conn;
 
+       was_cancelled = transfer->status == LIBUSB_TRANSFER_CANCELLED;
        sr_dbg("receive_transfer(): status %s received %d bytes.",
                libusb_error_name(transfer->status), transfer->actual_length);
        /*
@@ -1185,37 +1327,19 @@ static void LIBUSB_CALL receive_transfer(struct libusb_transfer *transfer)
         * or exhausting the device's captured data will complete the
         * sample data download.
         */
-       num_xfers = transfer->actual_length / TRANSFER_PACKET_LENGTH;
-       send_chunk(sdi, transfer->buffer, num_xfers);
-
-       devc->n_bytes_to_read -= transfer->actual_length;
-       if (devc->n_bytes_to_read) {
-               uint32_t to_read = devc->n_bytes_to_read;
-               /*
-                * Determine read size for the next USB transfer. Make
-                * the buffer size a multiple of the endpoint packet
-                * size. Don't exceed a maximum value.
-                */
-               if (to_read >= LA2016_USB_BUFSZ)
-                       to_read = LA2016_USB_BUFSZ;
-               to_read += LA2016_EP6_PKTSZ - 1;
-               to_read /= LA2016_EP6_PKTSZ;
-               to_read *= LA2016_EP6_PKTSZ;
-               libusb_fill_bulk_transfer(transfer,
-                       usb->devhdl, USB_EP_CAPTURE_DATA | LIBUSB_ENDPOINT_IN,
-                       transfer->buffer, to_read,
-                       receive_transfer, (void *)sdi, DEFAULT_TIMEOUT_MS);
-
-               ret = libusb_submit_transfer(transfer);
-               if (ret == 0)
+       send_chunk(sdi, transfer->buffer, transfer->actual_length);
+
+       /*
+        * Re-submit completed transfers (regardless of timeout or
+        * data reception), unless the transfer was cancelled when
+        * the acquisition was terminated or has completed.
+        */
+       if (!was_cancelled && !devc->download_finished) {
+               ret = la2016_usbxfer_resubmit(sdi, transfer);
+               if (ret == SR_OK)
                        return;
-               sr_err("Cannot submit another USB transfer: %s.",
-                       libusb_error_name(ret));
+               devc->download_finished = TRUE;
        }
-
-       g_free(transfer->buffer);
-       libusb_free_transfer(transfer);
-       devc->download_finished = TRUE;
 }
 
 SR_PRIV int la2016_receive_data(int fd, int revents, void *cb_data)
@@ -1259,7 +1383,7 @@ SR_PRIV int la2016_receive_data(int fd, int revents, void *cb_data)
                /* Initiate the download of acquired sample data. */
                std_session_send_df_frame_begin(sdi);
                devc->frame_begin_sent = TRUE;
-               ret = la2016_start_download(sdi, receive_transfer);
+               ret = la2016_start_download(sdi);
                if (ret != SR_OK) {
                        sr_err("Cannot start acquisition data download.");
                        return FALSE;
@@ -1270,7 +1394,7 @@ SR_PRIV int la2016_receive_data(int fd, int revents, void *cb_data)
        }
 
        /* Handle USB reception. Drives sample data download. */
-       tv.tv_sec = tv.tv_usec = 0;
+       memset(&tv, 0, sizeof(tv));
        libusb_handle_events_timeout(drvc->sr_ctx->libusb_ctx, &tv);
 
        /* Postprocess completion of sample data download. */
@@ -1279,7 +1403,10 @@ SR_PRIV int la2016_receive_data(int fd, int revents, void *cb_data)
 
                la2016_stop_acquisition(sdi);
                usb_source_remove(sdi->session, drvc->sr_ctx);
-               devc->transfer = NULL;
+
+               la2016_usbxfer_cancel_all(sdi);
+               memset(&tv, 0, sizeof(tv));
+               libusb_handle_events_timeout(drvc->sr_ctx->libusb_ctx, &tv);
 
                feed_queue_logic_flush(devc->feed_queue);
                feed_queue_logic_free(devc->feed_queue);
@@ -1487,6 +1614,11 @@ SR_PRIV int la2016_deinit_hardware(const struct sr_dev_inst *sdi)
        return SR_OK;
 }
 
+SR_PRIV void la2016_release_resources(const struct sr_dev_inst *sdi)
+{
+       (void)la2016_usbxfer_release(sdi);
+}
+
 SR_PRIV int la2016_write_pwm_config(const struct sr_dev_inst *sdi, size_t idx)
 {
        return set_pwm_config(sdi, idx);