+ 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;
+ 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;
+ }
+
+ data_left += MIN_PACKET_SIZE; /* Driver does not handle small buffers. */
+
+ sr_spew("data_amount %u", data_left);
+
+ return data_left;
+}
+
+static void send_chunk(struct sr_dev_inst *sdi, unsigned char *buf,
+ int num_samples)
+{
+ struct sr_datafeed_packet packet;
+ struct sr_datafeed_analog_old analog;
+ struct dev_context *devc = sdi->priv;
+ int num_channels, data_offset, i;
+
+ const float ch1_bit = RANGE(0) / 255;
+ const float ch2_bit = RANGE(1) / 255;
+ const float ch1_center = RANGE(0) / 2;
+ const float ch2_center = RANGE(1) / 2;
+
+ const gboolean ch1_ena = !!devc->ch_enabled[0];
+ const gboolean ch2_ena = !!devc->ch_enabled[1];
+
+ num_channels = (ch1_ena && ch2_ena) ? 2 : 1;
+ packet.type = SR_DF_ANALOG_OLD;
+ packet.payload = &analog;
+
+ analog.channels = devc->enabled_channels;
+ analog.num_samples = num_samples;
+ analog.mq = SR_MQ_VOLTAGE;
+ analog.unit = SR_UNIT_VOLT;
+ analog.mqflags = 0;
+
+ analog.data = g_try_malloc(analog.num_samples * sizeof(float) * num_channels);
+ if (!analog.data) {
+ sr_err("Analog data buffer malloc failed.");
+ devc->dev_state = STOPPING;
+ return;
+ }
+
+ data_offset = 0;
+ for (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.
+ */
+ if (ch1_ena)
+ analog.data[data_offset++] = (ch1_bit * *(buf + i * 2) - ch1_center);
+ if (ch2_ena)
+ analog.data[data_offset++] = (ch2_bit * *(buf + i * 2 + 1) - ch2_center);
+ }
+
+ sr_session_send(devc->cb_data, &packet);
+ 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) {
+ 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;
+ }