+ return SR_OK;
+}
+
+/* Minimise data amount for limit_samples and limit_msec limits. */
+static uint32_t data_amount(const struct sr_dev_inst *sdi)
+{
+ struct dev_context *devc = sdi->priv;
+ uint32_t data_left, data_left_2, i;
+ int32_t time_left;
+
+ if (devc->limit_msec) {
+ time_left = devc->limit_msec - (g_get_monotonic_time() - devc->aq_started) / 1000;
+ data_left = devc->samplerate * MAX(time_left, 0) * NUM_CHANNELS / 1000;
+ } else if (devc->limit_samples) {
+ data_left = (devc->limit_samples - devc->samp_received) * NUM_CHANNELS;
+ } else {
+ data_left = devc->samplerate * NUM_CHANNELS;
+ }
+
+ /* Round up to nearest power of two. */
+ for (i = MIN_PACKET_SIZE; i < data_left; i *= 2)
+ ;
+ data_left_2 = i;
+
+ sr_spew("data_amount: %u (rounded to power of 2: %u)", data_left, data_left_2);
+
+ return data_left_2;
+}
+
+static void send_chunk(struct sr_dev_inst *sdi, unsigned char *buf,
+ int num_samples)
+{
+ struct sr_datafeed_packet packet;
+ struct sr_datafeed_analog analog;
+ struct sr_analog_encoding encoding;
+ struct sr_analog_meaning meaning;
+ struct sr_analog_spec spec;
+ struct dev_context *devc = sdi->priv;
+ GSList *channels = devc->enabled_channels;
+
+ const float ch_bit[] = { RANGE(0) / 255, RANGE(1) / 255 };
+ const float ch_center[] = { RANGE(0) / 2, RANGE(1) / 2 };
+
+ sr_analog_init(&analog, &encoding, &meaning, &spec, 0);
+
+ packet.type = SR_DF_ANALOG;
+ packet.payload = &analog;
+
+ analog.num_samples = num_samples;
+ analog.meaning->mq = SR_MQ_VOLTAGE;
+ analog.meaning->unit = SR_UNIT_VOLT;
+ analog.meaning->mqflags = 0;
+
+ analog.data = g_try_malloc(num_samples * sizeof(float));
+ if (!analog.data) {
+ sr_err("Analog data buffer malloc failed.");
+ devc->dev_state = STOPPING;
+ return;
+ }
+
+ for (int ch = 0; ch < 2; ch++) {
+ if (!devc->ch_enabled[ch])
+ continue;
+
+ float vdivlog = log10f(ch_bit[ch]);
+ int digits = -(int)vdivlog + (vdivlog < 0.0);
+ analog.encoding->digits = digits;
+ analog.spec->spec_digits = digits;
+ analog.meaning->channels = g_slist_append(NULL, channels->data);
+
+ for (int i = 0; i < num_samples; i++) {
+ /*
+ * The device always sends data for both channels. If a channel
+ * is disabled, it contains a copy of the enabled channel's
+ * data. However, we only send the requested channels to
+ * the bus.
+ *
+ * Voltage values are encoded as a value 0-255, where the
+ * value is a point in the range represented by the vdiv
+ * setting. There are 10 vertical divs, so e.g. 500mV/div
+ * represents 5V peak-to-peak where 0 = -2.5V and 255 = +2.5V.
+ */
+ ((float *)analog.data)[i] = ch_bit[ch] * *(buf + i * 2 + ch) - ch_center[ch];
+ }
+
+ sr_session_send(sdi, &packet);
+ g_slist_free(analog.meaning->channels);
+
+ channels = channels->next;
+ }
+ g_free(analog.data);
+}
+
+static void send_data(struct sr_dev_inst *sdi, struct libusb_transfer *buf[], uint64_t samples)
+{
+ int i = 0;
+ uint64_t send = 0;
+ uint32_t chunk;
+
+ while (send < samples) {
+ chunk = MIN(samples - send, (uint64_t)(buf[i]->actual_length / NUM_CHANNELS));
+ send += chunk;
+ send_chunk(sdi, buf[i]->buffer, chunk);
+
+ /*
+ * Everything in this transfer was either copied to the buffer
+ * or sent to the session bus.
+ */
+ g_free(buf[i]->buffer);
+ libusb_free_transfer(buf[i]);
+ i++;
+ }
+}
+
+/*
+ * Called by libusb (as triggered by handle_event()) when a transfer comes in.
+ * Only channel data comes in asynchronously, and all transfers for this are
+ * queued up beforehand, so this just needs to chuck the incoming data onto
+ * the libsigrok session bus.
+ */
+static void LIBUSB_CALL receive_transfer(struct libusb_transfer *transfer)
+{
+ struct sr_dev_inst *sdi;
+ struct dev_context *devc;
+
+ sdi = transfer->user_data;
+ devc = sdi->priv;
+
+ if (devc->dev_state == FLUSH) {
+ g_free(transfer->buffer);
+ libusb_free_transfer(transfer);
+ devc->dev_state = CAPTURE;
+ devc->aq_started = g_get_monotonic_time();
+ read_channel(sdi, data_amount(sdi));
+ return;
+ }
+
+ if (devc->dev_state != CAPTURE)
+ return;
+
+ if (!devc->sample_buf) {
+ devc->sample_buf_size = 10;
+ devc->sample_buf = g_try_malloc(devc->sample_buf_size * sizeof(transfer));
+ devc->sample_buf_write = 0;
+ }
+
+ if (devc->sample_buf_write >= devc->sample_buf_size) {
+ devc->sample_buf_size += 10;
+ devc->sample_buf = g_try_realloc(devc->sample_buf,
+ devc->sample_buf_size * sizeof(transfer));
+ if (!devc->sample_buf) {
+ sr_err("Sample buffer malloc failed.");
+ devc->dev_state = STOPPING;
+ return;
+ }
+ }
+
+ devc->sample_buf[devc->sample_buf_write++] = transfer;
+ devc->samp_received += transfer->actual_length / NUM_CHANNELS;
+
+ sr_spew("receive_transfer(): calculated samplerate == %" PRIu64 "ks/s",
+ (uint64_t)(transfer->actual_length * 1000 /
+ (g_get_monotonic_time() - devc->read_start_ts + 1) /
+ NUM_CHANNELS));
+
+ sr_spew("receive_transfer(): status %s received %d bytes.",
+ libusb_error_name(transfer->status), transfer->actual_length);
+
+ if (transfer->actual_length == 0)
+ /* Nothing to send to the bus. */
+ return;
+
+ if (devc->limit_samples && devc->samp_received >= devc->limit_samples) {
+ sr_info("Requested number of samples reached, stopping. %"
+ PRIu64 " <= %" PRIu64, devc->limit_samples,
+ devc->samp_received);
+ send_data(sdi, devc->sample_buf, devc->limit_samples);
+ sdi->driver->dev_acquisition_stop(sdi);
+ } else if (devc->limit_msec && (g_get_monotonic_time() -
+ devc->aq_started) / 1000 >= devc->limit_msec) {
+ sr_info("Requested time limit reached, stopping. %d <= %d",
+ (uint32_t)devc->limit_msec,
+ (uint32_t)(g_get_monotonic_time() - devc->aq_started) / 1000);
+ send_data(sdi, devc->sample_buf, devc->samp_received);
+ g_free(devc->sample_buf);
+ devc->sample_buf = NULL;
+ sdi->driver->dev_acquisition_stop(sdi);
+ } else {
+ read_channel(sdi, data_amount(sdi));