+static int hw_dev_config_set(int dev_index, int hwcap, void *value);
+static int hw_dev_acquisition_stop(int dev_index, void *session_dev_id);
+
+/**
+ * Check the USB configuration to determine if this is an fx2lafw device.
+ *
+ * @return TRUE if the device's configuration profile match fx2lafw
+ * configuration, FALSE otherwise.
+ */
+static gboolean check_conf_profile(libusb_device *dev)
+{
+ struct libusb_device_descriptor des;
+ struct libusb_config_descriptor *conf_dsc = NULL;
+ const struct libusb_interface_descriptor *intf_dsc;
+ gboolean ret = FALSE;
+
+ while (!ret) {
+ /* Assume the firmware has not been loaded, unless proven wrong. */
+ ret = 0;
+
+ if (libusb_get_device_descriptor(dev, &des) != 0)
+ break;
+
+ if (des.bNumConfigurations != 1)
+ /* Need exactly 1 configuration. */
+ break;
+
+ if (libusb_get_config_descriptor(dev, 0, &conf_dsc) != 0)
+ break;
+
+ if (conf_dsc->bNumInterfaces != 1)
+ /* Need exactly 1 interface. */
+ break;
+
+ if (conf_dsc->interface[0].num_altsetting != 1)
+ /* Need just one alternate setting. */
+ break;
+
+ intf_dsc = &(conf_dsc->interface[0].altsetting[0]);
+ if (intf_dsc->bNumEndpoints != 2)
+ /* Need exactly 2 end points. */
+ break;
+
+ if ((intf_dsc->endpoint[0].bEndpointAddress & 0x8f) !=
+ (2 | LIBUSB_ENDPOINT_IN)) // 0x82
+ /* The first endpoint should be 2 (inbound). */
+ break;
+
+ /* TODO: Check the debug channel... */
+
+ /* If we made it here, it must be an fx2lafw. */
+ ret = TRUE;
+ }
+
+ if (conf_dsc)
+ libusb_free_config_descriptor(conf_dsc);
+
+ return ret;
+}
+
+static int fx2lafw_open_dev(int dev_index)
+{
+ libusb_device **devlist;
+ struct libusb_device_descriptor des;
+ struct sr_dev_inst *sdi;
+ struct context *ctx;
+ int ret, skip, i;
+
+ if (!(sdi = sr_dev_inst_get(dev_insts, dev_index)))
+ return SR_ERR;
+ ctx = sdi->priv;
+
+ if (sdi->status == SR_ST_ACTIVE)
+ /* already in use */
+ return SR_ERR;
+
+ skip = 0;
+ libusb_get_device_list(usb_context, &devlist);
+ for (i = 0; devlist[i]; i++) {
+ if ((ret = libusb_get_device_descriptor(devlist[i], &des))) {
+ sr_err("fx2lafw: failed to get device descriptor: %d", ret);
+ continue;
+ }
+
+ if (des.idVendor != ctx->profile->vid
+ || des.idProduct != ctx->profile->pid)
+ continue;
+
+ if (sdi->status == SR_ST_INITIALIZING) {
+ if (skip != dev_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]) != ctx->usb->bus
+ || libusb_get_device_address(devlist[i]) != ctx->usb->address)
+ /* this is not the one */
+ continue;
+ }
+
+ if (!(ret = libusb_open(devlist[i], &ctx->usb->devhdl))) {
+ if (ctx->usb->address == 0xff)
+ /*
+ * first time we touch this device after firmware upload,
+ * so we don't know the address yet.
+ */
+ ctx->usb->address = libusb_get_device_address(devlist[i]);
+
+ sdi->status = SR_ST_ACTIVE;
+ sr_info("fx2lafw: opened device %d on %d.%d interface %d",
+ sdi->index, ctx->usb->bus,
+ ctx->usb->address, USB_INTERFACE);
+ } else {
+ sr_err("fx2lafw: failed to open device: %d", ret);
+ }
+
+ /* if we made it here, we handled the device one way or another */
+ break;
+ }
+ libusb_free_device_list(devlist, 1);
+
+ if (sdi->status != SR_ST_ACTIVE)
+ return SR_ERR;
+
+ return SR_OK;
+}
+
+static void close_dev(struct sr_dev_inst *sdi)
+{
+ struct context *ctx;
+
+ ctx = sdi->priv;
+
+ if (ctx->usb->devhdl == NULL)
+ return;
+
+ sr_info("fx2lafw: closing device %d on %d.%d interface %d", sdi->index,
+ ctx->usb->bus, ctx->usb->address, USB_INTERFACE);
+ libusb_release_interface(ctx->usb->devhdl, USB_INTERFACE);
+ libusb_close(ctx->usb->devhdl);
+ ctx->usb->devhdl = NULL;
+ sdi->status = SR_ST_INACTIVE;
+}
+
+static struct context *fx2lafw_device_new(void)