+ if ((sdi->status == SR_ST_INITIALIZING) ||
+ (sdi->status == SR_ST_INACTIVE)) {
+ /*
+ * Check device by its physical USB bus/port address.
+ */
+ if (usb_get_port_path(devlist[i], connection_id, sizeof(connection_id)) < 0)
+ continue;
+
+ if (strcmp(sdi->connection_id, connection_id))
+ /* This is not the one. */
+ continue;
+ }
+
+ if (!(err = libusb_open(devlist[i], &usb->devhdl))) {
+ if (usb->address == 0xff) {
+ /*
+ * First time we touch this device after firmware upload,
+ * so we don't know the address yet.
+ */
+ usb->address = libusb_get_device_address(devlist[i]);
+ }
+
+ sr_info("Opened device on %d.%d (logical) / "
+ "%s (physical) interface %d.",
+ usb->bus, usb->address,
+ sdi->connection_id, USB_INTERFACE);
+
+ err = SR_OK;
+ } else {
+ sr_err("Failed to open device: %s.",
+ libusb_error_name(err));
+ err = SR_ERR;
+ }
+
+ /* If we made it here, we handled the device (somehow). */
+ break;
+ }
+
+ libusb_free_device_list(devlist, 1);
+
+ return err;
+}
+
+SR_PRIV void hantek_6xxx_close(struct sr_dev_inst *sdi)
+{
+ struct sr_usb_dev_inst *usb;
+
+ usb = sdi->conn;
+
+ if (!usb->devhdl)
+ return;
+
+ sr_info("Closing device on %d.%d (logical) / %s (physical) interface %d.",
+ usb->bus, usb->address, sdi->connection_id, USB_INTERFACE);
+ libusb_release_interface(usb->devhdl, USB_INTERFACE);
+ libusb_close(usb->devhdl);
+ usb->devhdl = NULL;
+ sdi->status = SR_ST_INACTIVE;
+}
+
+SR_PRIV int hantek_6xxx_get_channeldata(const struct sr_dev_inst *sdi,
+ libusb_transfer_cb_fn cb, uint32_t data_amount)
+{
+ struct sr_usb_dev_inst *usb;
+ struct libusb_transfer *transfer;
+ int ret;
+ unsigned char *buf;
+
+ sr_dbg("Request channel data.");
+
+ usb = sdi->conn;
+
+ if (!(buf = g_try_malloc(data_amount))) {
+ sr_err("Failed to malloc USB endpoint buffer.");
+ return SR_ERR_MALLOC;
+ }
+ transfer = libusb_alloc_transfer(0);
+ libusb_fill_bulk_transfer(transfer, usb->devhdl, HANTEK_EP_IN, buf,
+ data_amount, cb, (void *)sdi, 4000);
+ if ((ret = libusb_submit_transfer(transfer)) < 0) {
+ sr_err("Failed to submit transfer: %s.",
+ libusb_error_name(ret));
+ /* TODO: Free them all. */
+ libusb_free_transfer(transfer);
+ g_free(buf);
+ return SR_ERR;
+ }
+
+ return SR_OK;
+}
+
+static uint8_t samplerate_to_reg(uint64_t samplerate)
+{
+ const uint64_t samplerate_values[] = {SAMPLERATE_VALUES};
+ const uint8_t samplerate_regs[] = {SAMPLERATE_REGS};
+ uint32_t i;
+
+ for (i = 0; i < ARRAY_SIZE(samplerate_values); i++) {
+ if (samplerate_values[i] == samplerate)
+ return samplerate_regs[i];
+ }
+
+ sr_err("Failed to convert samplerate: %" PRIu64 ".", samplerate);
+
+ return samplerate_regs[ARRAY_SIZE(samplerate_values) - 1];
+}
+
+static uint8_t voltage_to_reg(uint8_t state)
+{
+ const uint8_t vdiv_reg[] = {VDIV_REG};
+
+ if (state < ARRAY_SIZE(vdiv_reg)) {
+ return vdiv_reg[state];
+ } else {
+ sr_err("Failed to convert vdiv: %d.", state);
+ return vdiv_reg[ARRAY_SIZE(vdiv_reg) - 1];
+ }
+}
+
+static int write_control(const struct sr_dev_inst *sdi,
+ enum control_requests reg, uint8_t value)
+{
+ struct sr_usb_dev_inst *usb = sdi->conn;
+ int ret;
+
+ sr_spew("hantek_6xxx_write_control: 0x%x 0x%x", reg, value);
+
+ if ((ret = libusb_control_transfer(usb->devhdl,
+ LIBUSB_REQUEST_TYPE_VENDOR, (uint8_t)reg,
+ 0, 0, &value, 1, 100)) <= 0) {
+ sr_err("Failed to control transfer: 0x%x: %s.", reg,
+ libusb_error_name(ret));
+ return ret;