+ struct stream_state_t *stream;
+ size_t bit_count;
+ const uint8_t *rp;
+ uint32_t sample_value;
+ uint8_t sample_buff[sizeof(sample_value)];
+ size_t bit_idx;
+ uint32_t ch_mask;
+
+ devc = sdi->priv;
+ stream = &devc->stream;
+
+ /* Ignore incoming USB data after complete sample data download. */
+ if (devc->download_finished)
+ return;
+ sr_dbg("Stream mode, got another chunk: %p, length %zu.",
+ data_buffer, data_length);
+
+ /* TODO Add soft trigger support when in stream mode? */
+
+ /* All channels' chunks carry 16 samples for one channel. */
+ bit_count = 16;
+ data_length /= sizeof(uint16_t);
+
+ rp = data_buffer;
+ sample_value = 0;
+ while (data_length--) {
+ /* Get another entity. */
+ sample_value = read_u16le_inc(&rp);
+
+ /* Map the entity's bits to a channel's samples. */
+ ch_mask = stream->channel_masks[stream->channel_index];
+ for (bit_idx = 0; bit_idx < bit_count; bit_idx++) {
+ if (sample_value & (1UL << bit_idx))
+ stream->sample_data[bit_idx] |= ch_mask;
+ }
+
+ /*
+ * Advance to the next channel. Submit a block of
+ * samples when all channels' data was seen.
+ */
+ stream->channel_index++;
+ if (stream->channel_index != stream->enabled_count)
+ continue;
+ for (bit_idx = 0; bit_idx < bit_count; bit_idx++) {
+ sample_value = stream->sample_data[bit_idx];
+ write_u32le(sample_buff, sample_value);
+ feed_queue_logic_submit(devc->feed_queue, sample_buff, 1);
+ }
+ sr_sw_limits_update_samples_read(&devc->sw_limits, bit_count);
+ devc->total_samples += bit_count;
+ memset(stream->sample_data, 0, sizeof(stream->sample_data));
+ stream->channel_index = 0;
+ }
+
+ /*
+ * Need we count empty or failed USB transfers? This version
+ * doesn't, assumes that timeouts are perfectly legal because
+ * transfers are started early, and slow samplerates or trigger
+ * support in hardware are plausible causes for empty transfers.
+ *
+ * TODO Maybe a good condition would be (rather large) a timeout
+ * after a previous capture data chunk was seen? So that stalled
+ * streaming gets detected which _is_ an exceptional condition.
+ * We have observed these when "runmode" is set early but bulk
+ * transfers start late with a pause after setting the runmode.
+ */
+ if (sr_sw_limits_check(&devc->sw_limits)) {
+ sr_dbg("Acquisition end reached (sw limits).");
+ devc->download_finished = TRUE;
+ }
+ if (devc->download_finished) {
+ sr_dbg("Stream receive done, flushing session feed queue.");
+ feed_queue_logic_flush(devc->feed_queue);
+ }
+ sr_dbg("Total samples after chunk: %" PRIu64 ".", devc->total_samples);
+}
+
+static void LIBUSB_CALL receive_transfer(struct libusb_transfer *transfer)
+{
+ struct sr_dev_inst *sdi;
+ struct dev_context *devc;
+ gboolean was_cancelled, device_gone;
+ int ret;
+
+ sdi = transfer->user_data;
+ devc = sdi->priv;
+
+ was_cancelled = transfer->status == LIBUSB_TRANSFER_CANCELLED;
+ device_gone = transfer->status == LIBUSB_TRANSFER_NO_DEVICE;
+ sr_dbg("receive_transfer(): status %s received %d bytes.",
+ libusb_error_name(transfer->status), transfer->actual_length);
+ if (device_gone) {
+ sr_warn("Lost communication to USB device.");
+ devc->download_finished = TRUE;
+ return;
+ }
+
+ /*
+ * Implementation detail: A USB transfer timeout is not fatal
+ * here. We just process whatever was received, empty input is
+ * perfectly acceptable. Reaching (or exceeding) the sw limits
+ * or exhausting the device's captured data will complete the
+ * sample data download.
+ */
+ if (devc->continuous)
+ stream_data(sdi, transfer->buffer, transfer->actual_length);
+ else
+ 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;
+ devc->download_finished = TRUE;
+ }
+}
+
+SR_PRIV int la2016_receive_data(int fd, int revents, void *cb_data)
+{
+ const struct sr_dev_inst *sdi;
+ struct dev_context *devc;
+ struct drv_context *drvc;
+ struct timeval tv;