From: Bert Vermeulen Date: Sat, 14 Jul 2012 11:00:47 +0000 (+0200) Subject: zeroplus-logic-cube: init and scan fixes X-Git-Tag: dsupstream~811 X-Git-Url: https://sigrok.org/gitaction?a=commitdiff_plain;h=7da6f9d54a6acb699116848141d6ae1650a8dc98;p=libsigrok.git zeroplus-logic-cube: init and scan fixes libusb context needs to be initialized only once, not ever scan. Every scan invalidates the last one, so clean up any previous run's results first. --- diff --git a/hardware/zeroplus-logic-cube/zeroplus.c b/hardware/zeroplus-logic-cube/zeroplus.c index 8e710089..97378d54 100644 --- a/hardware/zeroplus-logic-cube/zeroplus.c +++ b/hardware/zeroplus-logic-cube/zeroplus.c @@ -155,6 +155,7 @@ struct context { uint8_t trigger_value[NUM_TRIGGER_STAGES]; // uint8_t trigger_buffer[NUM_TRIGGER_STAGES]; + /* TODO: this belongs in the device instance */ struct sr_usb_dev_inst *usb; }; @@ -324,6 +325,23 @@ static int configure_probes(struct sr_dev_inst *sdi, const GSList *probes) return SR_OK; } +static void clear_instances(void) +{ + GSList *l; + struct sr_dev_inst *sdi; + + for (l = zdi->instances; l; l = l->next) { + sdi = l->data; + /* Properly close all devices... */ + close_dev(sdi); + /* ...and free all their memory. */ + sr_dev_inst_free(sdi); + } + g_slist_free(zdi->instances); + zdi->instances = NULL; + +} + /* * API callbacks */ @@ -331,7 +349,10 @@ static int configure_probes(struct sr_dev_inst *sdi, const GSList *probes) static int hw_init(void) { - /* Nothing to do. */ + if (libusb_init(&usb_context) != 0) { + sr_err("zp: Failed to initialize USB."); + return 0; + } return SR_OK; } @@ -348,6 +369,8 @@ static GSList *hw_scan(GSList *options) (void)options; devices = NULL; + clear_instances(); + /* Allocate memory for our private driver context. */ if (!(ctx = g_try_malloc(sizeof(struct context)))) { sr_err("zp: %s: ctx malloc failed", __func__); @@ -365,11 +388,6 @@ static GSList *hw_scan(GSList *options) memset(ctx->trigger_value, 0, NUM_TRIGGER_STAGES); // memset(ctx->trigger_buffer, 0, NUM_TRIGGER_STAGES); - if (libusb_init(&usb_context) != 0) { - sr_err("zp: Failed to initialize USB."); - return 0; - } - /* Find all ZEROPLUS analyzers and add them to device list. */ devcnt = 0; libusb_get_device_list(usb_context, &devlist); /* TODO: Errors. */ @@ -489,18 +507,8 @@ static int hw_dev_close(int dev_index) static int hw_cleanup(void) { - GSList *l; - struct sr_dev_inst *sdi; - for (l = zdi->instances; l; l = l->next) { - sdi = l->data; - /* Properly close all devices... */ - close_dev(sdi); - /* ...and free all their memory. */ - sr_dev_inst_free(sdi); - } - g_slist_free(zdi->instances); - zdi->instances = NULL; + clear_instances(); if (usb_context) libusb_exit(usb_context);