+ uint64_t i, j;
+ uint8_t num_samples, low, high;
+
+ for (i = 0; i < num_bytes; i += 2) {
+ num_samples = devc->compressed_buf[i + 0] >> 1;
+
+ low = devc->compressed_buf[i + 0] & (1 << 0);
+ high = devc->compressed_buf[i + 1];
+
+ for (j = 0; j < num_samples; j++) {
+ devc->sample_buf[devc->bytes_received++] = high;
+ devc->sample_buf[devc->bytes_received++] = low;
+ }
+ }
+}
+
+static void send_samples(struct dev_context *devc, uint64_t samples_to_send)
+{
+ struct sr_datafeed_packet packet;
+ struct sr_datafeed_logic logic;
+
+ sr_spew("Sending %" PRIu64 " samples.", samples_to_send);
+
+ packet.type = SR_DF_LOGIC;
+ packet.payload = &logic;
+ logic.length = samples_to_send * 2;
+ logic.unitsize = 2; /* We need 2 bytes for 9 probes. */
+ logic.data = devc->sample_buf;
+ sr_session_send(devc->cb_data, &packet);
+
+ devc->samples_sent += samples_to_send;
+ devc->bytes_received -= samples_to_send * 2;
+}
+
+SR_PRIV int scanaplus_get_device_id(struct dev_context *devc)
+{
+ int ret;
+ uint16_t val1, val2;
+
+ /* FTDI EEPROM indices 16+17 contain the 3 device ID bytes. */
+ if ((ret = ftdi_read_eeprom_location(devc->ftdic, 16, &val1)) < 0) {
+ sr_err("Failed to read EEPROM index 16 (%d): %s.",
+ ret, ftdi_get_error_string(devc->ftdic));
+ return SR_ERR;
+ }
+ if ((ret = ftdi_read_eeprom_location(devc->ftdic, 17, &val2)) < 0) {
+ sr_err("Failed to read EEPROM index 17 (%d): %s.",
+ ret, ftdi_get_error_string(devc->ftdic));
+ return SR_ERR;
+ }
+
+ /*
+ * Note: Bit 7 of the three bytes must not be used, apparently.
+ *
+ * Even though the three bits can be either 0 or 1 (we've seen both
+ * in actual ScanaPLUS devices), the device ID as sent to the FPGA
+ * has bit 7 of each byte zero'd out.
+ *
+ * It is unknown whether bit 7 of these bytes has any meaning,
+ * whether it's used somewhere, or whether it can be simply ignored.
+ */
+ devc->devid[0] = ((val1 >> 0) & 0xff) & ~(1 << 7);
+ devc->devid[1] = ((val1 >> 8) & 0xff) & ~(1 << 7);
+ devc->devid[2] = ((val2 >> 0) & 0xff) & ~(1 << 7);
+
+ return SR_OK;
+}
+
+static int scanaplus_clear_device_id(struct dev_context *devc)
+{
+ uint8_t buf[2];
+
+ buf[0] = 0x8c;
+ buf[1] = 0x00;
+ if (scanaplus_write(devc, (uint8_t *)&buf, 2) < 0)
+ return SR_ERR;
+
+ buf[0] = 0x8e;
+ buf[1] = 0x00;
+ if (scanaplus_write(devc, (uint8_t *)&buf, 2) < 0)
+ return SR_ERR;
+
+ buf[0] = 0x8f;
+ buf[1] = 0x00;
+ if (scanaplus_write(devc, (uint8_t *)&buf, 2) < 0)
+ return SR_ERR;
+
+ return SR_OK;
+}
+
+static int scanaplus_send_device_id(struct dev_context *devc)
+{
+ uint8_t buf[2];
+
+ buf[0] = 0x8c;
+ buf[1] = devc->devid[0];
+ if (scanaplus_write(devc, (uint8_t *)&buf, 2) < 0)
+ return SR_ERR;
+
+ buf[0] = 0x8e;
+ buf[1] = devc->devid[1];
+ if (scanaplus_write(devc, (uint8_t *)&buf, 2) < 0)
+ return SR_ERR;
+
+ buf[0] = 0x8f;
+ buf[1] = devc->devid[2];
+ if (scanaplus_write(devc, (uint8_t *)&buf, 2) < 0)
+ return SR_ERR;
+
+ return SR_OK;
+}
+
+SR_PRIV int scanaplus_init(struct dev_context *devc)
+{
+ int i;
+ uint8_t buf[8];
+
+ buf[0] = 0x88;
+ buf[1] = 0x41;
+ if (scanaplus_write(devc, (uint8_t *)&buf, 2) < 0)
+ return SR_ERR;
+
+ buf[0] = 0x89;
+ buf[1] = 0x64;
+ buf[2] = 0x8a;
+ buf[3] = 0x64;
+ if (scanaplus_write(devc, (uint8_t *)&buf, 4) < 0)
+ return SR_ERR;
+
+ buf[0] = 0x88;
+ buf[1] = 0x41;
+ if (scanaplus_write(devc, (uint8_t *)&buf, 2) < 0)
+ return SR_ERR;
+
+ buf[0] = 0x88;
+ buf[1] = 0x40;
+ if (scanaplus_write(devc, (uint8_t *)&buf, 2) < 0)
+ return SR_ERR;
+
+ buf[0] = 0x8d;
+ buf[1] = 0x01;
+ buf[2] = 0x8d;
+ buf[3] = 0x05;
+ buf[4] = 0x8d;
+ buf[5] = 0x01;
+ buf[6] = 0x8d;
+ buf[7] = 0x02;
+ if (scanaplus_write(devc, (uint8_t *)&buf, 8) < 0)
+ return SR_ERR;
+
+ for (i = 0; i < 57; i++) {
+ buf[0] = 0x8d;
+ buf[1] = 0x06;
+ if (scanaplus_write(devc, (uint8_t *)&buf, 2) < 0)
+ return SR_ERR;
+
+ buf[0] = 0x8d;
+ buf[1] = 0x02;
+ if (scanaplus_write(devc, (uint8_t *)&buf, 2) < 0)
+ return SR_ERR;
+ }
+
+ if (scanaplus_send_device_id(devc) < 0)
+ return SR_ERR;
+
+ buf[0] = 0x88;
+ buf[1] = 0x40;
+ if (scanaplus_write(devc, (uint8_t *)&buf, 2) < 0)
+ return SR_ERR;
+
+ return SR_OK;
+}
+
+SR_PRIV int scanaplus_start_acquisition(struct dev_context *devc)
+{
+ uint8_t buf[4];
+
+ /* Threshold and differential probe settings not yet implemented. */
+
+ buf[0] = 0x89;
+ buf[1] = 0x7f; /* Logic level threshold for probes 1-4. */
+ buf[2] = 0x8a;
+ buf[3] = 0x7f; /* Logic level threshold for probes 5-9. */
+ if (scanaplus_write(devc, (uint8_t *)&buf, 4) < 0)
+ return SR_ERR;
+
+ buf[0] = 0x88;
+ buf[1] = 0x40; /* Special config of probes 5/6 and 7/8. */
+ /* 0x40: normal, 0x50: ch56 diff, 0x48: ch78 diff, 0x58: ch5678 diff */
+ if (scanaplus_write(devc, (uint8_t *)&buf, 2) < 0)
+ return SR_ERR;
+
+ if (scanaplus_clear_device_id(devc) < 0)
+ return SR_ERR;
+
+ if (scanaplus_send_device_id(devc) < 0)
+ return SR_ERR;
+
+ return SR_OK;
+}
+
+SR_PRIV int scanaplus_receive_data(int fd, int revents, void *cb_data)
+{
+ int bytes_read;
+ struct sr_dev_inst *sdi;