+static int logic16_dev_open(struct sr_dev_inst *sdi)
+{
+ libusb_device **devlist;
+ struct sr_usb_dev_inst *usb;
+ struct libusb_device_descriptor des;
+ struct dev_context *devc;
+ struct drv_context *drvc;
+ int ret, skip, i, device_count;
+
+ drvc = di->priv;
+ devc = sdi->priv;
+ usb = sdi->conn;
+
+ if (sdi->status == SR_ST_ACTIVE)
+ /* Device is already in use. */
+ return SR_ERR;
+
+ skip = 0;
+ device_count = libusb_get_device_list(drvc->sr_ctx->libusb_ctx, &devlist);
+ if (device_count < 0) {
+ sr_err("Failed to get device list: %s.",
+ libusb_error_name(device_count));
+ return SR_ERR;
+ }
+
+ for (i = 0; i < device_count; i++) {
+ if ((ret = libusb_get_device_descriptor(devlist[i], &des))) {
+ sr_err("Failed to get device descriptor: %s.",
+ libusb_error_name(ret));
+ continue;
+ }
+
+ if (des.idVendor != LOGIC16_VID
+ || des.idProduct != LOGIC16_PID)
+ continue;
+
+ if (sdi->status == SR_ST_INITIALIZING) {
+ if (skip != sdi->index) {
+ /* Skip devices of this type that aren't the one we want. */
+ skip += 1;
+ continue;
+ }
+ } else if (sdi->status == SR_ST_INACTIVE) {
+ /*
+ * This device is fully enumerated, so we need to find
+ * this device by vendor, product, bus and address.
+ */
+ if (libusb_get_bus_number(devlist[i]) != usb->bus
+ || libusb_get_device_address(devlist[i]) != usb->address)
+ /* This is not the one. */
+ continue;
+ }
+
+ if (!(ret = libusb_open(devlist[i], &usb->devhdl))) {
+ if (usb->address == 0xff)
+ /*
+ * First time we touch this device after FW
+ * upload, so we don't know the address yet.
+ */
+ usb->address = libusb_get_device_address(devlist[i]);
+ } else {
+ sr_err("Failed to open device: %s.",
+ libusb_error_name(ret));
+ break;
+ }
+
+ sdi->status = SR_ST_ACTIVE;
+ sr_info("Opened device %d on %d.%d, "
+ "interface %d.",
+ sdi->index, usb->bus, usb->address,
+ USB_INTERFACE);
+
+ break;
+ }
+ libusb_free_device_list(devlist, 1);
+
+ if (sdi->status != SR_ST_ACTIVE)
+ return SR_ERR;
+
+ return SR_OK;
+}
+