]> sigrok.org Git - libsigrok.git/blobdiff - hardware/zeroplus-logic-cube/zeroplus.c
hardware: Call libusb_error_name() in all USB-related error messages
[libsigrok.git] / hardware / zeroplus-logic-cube / zeroplus.c
index f292ba568eeade006da86d62c920f501251a3f62..56b020228bbf2251308a797b17b663df86f7679f 100644 (file)
@@ -108,8 +108,6 @@ static const char *probe_names[NUM_PROBES + 1] = {
 SR_PRIV struct sr_dev_driver zeroplus_logic_cube_driver_info;
 static struct sr_dev_driver *zdi = &zeroplus_logic_cube_driver_info;
 
-static libusb_context *usb_context = NULL;
-
 /*
  * The hardware supports more samplerates than these, but these are the
  * options hardcoded into the vendor's Windows GUI.
@@ -303,7 +301,7 @@ static int clear_instances(void)
  * API callbacks
  */
 
-static int hw_init(void)
+static int hw_init(struct sr_context *sr_ctx)
 {
        struct drv_context *drvc;
 
@@ -311,13 +309,9 @@ static int hw_init(void)
                sr_err("zeroplus: driver context malloc failed.");
                return SR_ERR_MALLOC;
        }
+       drvc->sr_ctx = sr_ctx;
        zdi->priv = drvc;
 
-       if (libusb_init(&usb_context) != 0) {
-               sr_err("zp: Failed to initialize USB.");
-               return 0;
-       }
-
        return SR_OK;
 }
 
@@ -334,6 +328,7 @@ static GSList *hw_scan(GSList *options)
        int ret, devcnt, i, j;
 
        (void)options;
+
        drvc = zdi->priv;
        devices = NULL;
 
@@ -341,12 +336,13 @@ static GSList *hw_scan(GSList *options)
 
        /* Find all ZEROPLUS analyzers and add them to device list. */
        devcnt = 0;
-       libusb_get_device_list(usb_context, &devlist); /* TODO: Errors. */
+       libusb_get_device_list(drvc->sr_ctx->libusb_ctx, &devlist); /* TODO: Errors. */
 
        for (i = 0; devlist[i]; i++) {
                ret = libusb_get_device_descriptor(devlist[i], &des);
                if (ret != 0) {
-                       sr_err("zp: failed to get device descriptor: %d", ret);
+                       sr_err("zp: failed to get device descriptor: %s",
+                              libusb_error_name(ret));
                        continue;
                }
 
@@ -421,6 +417,7 @@ static GSList *hw_dev_list(void)
 static int hw_dev_open(struct sr_dev_inst *sdi)
 {
        struct dev_context *devc;
+       struct drv_context *drvc = zdi->priv;
        libusb_device **devlist, *dev;
        struct libusb_device_descriptor des;
        int device_count, ret, i;
@@ -430,7 +427,8 @@ static int hw_dev_open(struct sr_dev_inst *sdi)
                return SR_ERR_ARG;
        }
 
-       device_count = libusb_get_device_list(usb_context, &devlist);
+       device_count = libusb_get_device_list(drvc->sr_ctx->libusb_ctx,
+                                             &devlist);
        if (device_count < 0) {
                sr_err("zp: Failed to retrieve device list");
                return SR_ERR;
@@ -439,8 +437,8 @@ static int hw_dev_open(struct sr_dev_inst *sdi)
        dev = NULL;
        for (i = 0; i < device_count; i++) {
                if ((ret = libusb_get_device_descriptor(devlist[i], &des))) {
-                       sr_err("fx2lafw: Failed to get device descriptor: %d.",
-                              ret);
+                       sr_err("zp: Failed to get device descriptor: %s.",
+                              libusb_error_name(ret));
                        continue;
                }
                if (libusb_get_bus_number(devlist[i]) == devc->usb->bus
@@ -461,20 +459,21 @@ static int hw_dev_open(struct sr_dev_inst *sdi)
                        sdi->index, devc->usb->bus,
                        devc->usb->address, USB_INTERFACE);
        } else {
-               sr_err("zp: failed to open device: %d", ret);
+               sr_err("zp: failed to open device: %s", libusb_error_name(ret));
                return SR_ERR;
        }
 
        ret = libusb_set_configuration(devc->usb->devhdl, USB_CONFIGURATION);
        if (ret < 0) {
-               sr_err("zp: Unable to set USB configuration %d: %d",
-                      USB_CONFIGURATION, ret);
+               sr_err("zp: Unable to set USB configuration %d: %s",
+                      USB_CONFIGURATION, libusb_error_name(ret));
                return SR_ERR;
        }
 
        ret = libusb_claim_interface(devc->usb->devhdl, USB_INTERFACE);
        if (ret != 0) {
-               sr_err("zp: Unable to claim interface: %d", ret);
+               sr_err("zp: Unable to claim interface: %s",
+                      libusb_error_name(ret));
                return SR_ERR;
        }
 
@@ -541,10 +540,6 @@ static int hw_cleanup(void)
 
        clear_instances();
 
-       if (usb_context)
-               libusb_exit(usb_context);
-       usb_context = NULL;
-
        return SR_OK;
 }
 
@@ -799,8 +794,7 @@ static int hw_dev_acquisition_start(const struct sr_dev_inst *sdi,
 }
 
 /* TODO: This stops acquisition on ALL devices, ignoring dev_index. */
-static int hw_dev_acquisition_stop(const struct sr_dev_inst *sdi,
-               void *cb_data)
+static int hw_dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data)
 {
        struct sr_datafeed_packet packet;
        struct dev_context *devc;