- } else if (sdi->status == ST_INACTIVE) {
- /*
- * This device is fully enumerated, so we need to find this
- * device by vendor, product, bus and address.
- */
- libusb_get_device_list(usb_context, &devlist);
- for (i = 0; devlist[i]; i++) {
- /* TODO: Error handling. */
- err = opendev3(&sdi, devlist[i], &des, USB_VENDOR,
- USB_PRODUCT, USB_INTERFACE);
+
+ if (des.idVendor != fx2->fw_vid || des.idProduct != fx2->fw_pid)
+ continue;
+
+ if (sdi->status == SR_ST_INITIALIZING) {
+ if (skip != device_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]) != sdi->usb->bus
+ || libusb_get_device_address(devlist[i]) != sdi->usb->address)
+ /* this is not the one */
+ continue;
+ }
+
+ if (!(err = libusb_open(devlist[i], &sdi->usb->devhdl))) {
+ if (sdi->usb->address == 0xff)
+ /*
+ * first time we touch this device after firmware upload,
+ * so we don't know the address yet.
+ */
+ sdi->usb->address = libusb_get_device_address(devlist[i]);
+
+ sdi->status = SR_ST_ACTIVE;
+ g_message("saleae: opened device %d on %d.%d interface %d",
+ sdi->index, sdi->usb->bus,
+ sdi->usb->address, USB_INTERFACE);
+ } else {
+ g_warning("failed to open device: %d", err);
+ sdi = NULL;