]> sigrok.org Git - libsigrok.git/blobdiff - hardware/hantek-dso/api.c
hardware: Call libusb_error_name() in all USB-related error messages
[libsigrok.git] / hardware / hantek-dso / api.c
index e221b5f20a871d49c0e20ae6c789ff13ac344f89..01a080017e2eae8339a828e0c295e97adf3467d1 100644 (file)
@@ -262,12 +262,7 @@ static int hw_init(struct sr_context *sr_ctx)
                return SR_ERR_MALLOC;
        }
 
-       if (libusb_init(NULL) != 0) {
-               g_free(drvc);
-               sr_err("Failed to initialize USB.");
-               return SR_ERR;
-       }
-
+       drvc->sr_ctx = sr_ctx;
        hdi->priv = drvc;
 
        return SR_OK;
@@ -294,10 +289,11 @@ static GSList *hw_scan(GSList *options)
        clear_instances();
 
        /* Find all Hantek DSO devices and upload firmware to all of them. */
-       libusb_get_device_list(NULL, &devlist);
+       libusb_get_device_list(drvc->sr_ctx->libusb_ctx, &devlist);
        for (i = 0; devlist[i]; i++) {
                if ((ret = libusb_get_device_descriptor(devlist[i], &des))) {
-                       sr_err("Failed to get device descriptor: %d.", ret);
+                       sr_err("Failed to get device descriptor: %s.",
+                              libusb_error_name(ret));
                        continue;
                }
 
@@ -395,7 +391,8 @@ static int hw_dev_open(struct sr_dev_inst *sdi)
 
        err = libusb_claim_interface(devc->usb->devhdl, USB_INTERFACE);
        if (err != 0) {
-               sr_err("Unable to claim interface: %d.", err);
+               sr_err("Unable to claim interface: %s.",
+                      libusb_error_name(err));
                return SR_ERR;
        }
 
@@ -418,8 +415,6 @@ static int hw_cleanup(void)
 
        clear_instances();
 
-       libusb_exit(NULL);
-
        return SR_OK;
 }
 
@@ -739,6 +734,7 @@ static int handle_event(int fd, int revents, void *cb_data)
        struct sr_datafeed_packet packet;
        struct timeval tv;
        struct dev_context *devc;
+       struct drv_context *drvc = hdi->priv;
        const struct libusb_pollfd **lupfd;
        int num_probes, i;
        uint32_t trigger_offset;
@@ -756,7 +752,7 @@ static int handle_event(int fd, int revents, void *cb_data)
                 * TODO: Doesn't really cancel pending transfers so they might
                 * come in after SR_DF_END is sent.
                 */
-               lupfd = libusb_get_pollfds(NULL);
+               lupfd = libusb_get_pollfds(drvc->sr_ctx->libusb_ctx);
                for (i = 0; lupfd[i]; i++)
                        sr_source_remove(lupfd[i]->fd);
                free(lupfd);
@@ -771,7 +767,7 @@ static int handle_event(int fd, int revents, void *cb_data)
 
        /* Always handle pending libusb events. */
        tv.tv_sec = tv.tv_usec = 0;
-       libusb_handle_events_timeout(NULL, &tv);
+       libusb_handle_events_timeout(drvc->sr_ctx->libusb_ctx, &tv);
 
        /* TODO: ugh */
        if (devc->dev_state == NEW_CAPTURE) {
@@ -855,6 +851,7 @@ static int hw_dev_acquisition_start(const struct sr_dev_inst *sdi,
        struct sr_datafeed_header header;
        struct sr_datafeed_meta_analog meta;
        struct dev_context *devc;
+       struct drv_context *drvc = hdi->priv;
        int i;
 
        if (sdi->status != SR_ST_ACTIVE)
@@ -875,7 +872,7 @@ static int hw_dev_acquisition_start(const struct sr_dev_inst *sdi,
                return SR_ERR;
 
        devc->dev_state = CAPTURE;
-       lupfd = libusb_get_pollfds(NULL);
+       lupfd = libusb_get_pollfds(drvc->sr_ctx->libusb_ctx);
        for (i = 0; lupfd[i]; i++)
                sr_source_add(lupfd[i]->fd, lupfd[i]->events, TICK,
                              handle_event, (void *)sdi);