]> sigrok.org Git - libsigrok.git/blobdiff - hardware/fx2lafw/fx2lafw.c
fx2lafw: Added more to hw_cleanup
[libsigrok.git] / hardware / fx2lafw / fx2lafw.c
index 3efcdb434bff292cba0fa390d2727ab6ca95b5fb..636a0e7045d7e2616942f35a4e6baf710c04977c 100644 (file)
@@ -213,6 +213,23 @@ static int fx2lafw_open_dev(int dev_index)
        return SR_OK;
 }
 
+static void close_dev(struct sr_dev_inst *sdi)
+{
+       struct fx2lafw_device *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 fx2lafw_device* fx2lafw_device_new(void)
 {
        struct fx2lafw_device *fx2lafw;
@@ -354,9 +371,18 @@ static int hw_dev_open(int device_index)
        return SR_OK;
 }
 
-static int hw_dev_close(int device_index)
+static int hw_dev_close(int dev_index)
 {
-       (void)device_index;
+       struct sr_dev_inst *sdi;
+
+       if (!(sdi = sr_dev_inst_get(dev_insts, dev_index))) {
+               sr_err("fx2lafw: %s: sdi was NULL", __func__);
+               return SR_ERR; /* TODO: SR_ERR_ARG? */
+       }
+
+       /* TODO */
+       close_dev(sdi);
+
        return SR_OK;
 }
 
@@ -364,20 +390,36 @@ static int hw_cleanup(void)
 {
        GSList *l;
        struct sr_dev_inst *sdi;
+       struct fx2lafw_device *ctx;
+       int ret = SR_OK;
 
-       for(l = device_instances; l; l = l->next) {
+       for(l = dev_insts; l; l = l->next) {
+               if (!(sdi = l->data)) {
+                       /* Log error, but continue cleaning up the rest. */
+                       sr_err("fx2lafw: %s: sdi was NULL, continuing", __func__);
+                       ret = SR_ERR_BUG;
+                       continue;
+               }
+               if (!(ctx = sdi->priv)) {
+                       /* Log error, but continue cleaning up the rest. */
+                       sr_err("fx2lafw: %s: sdi->priv was NULL, continuing",
+                              __func__);
+                       ret = SR_ERR_BUG;
+                       continue;
+               }
+               close_dev(sdi);
                sdi = l->data;
                sr_dev_inst_free(sdi);
        }
 
-       g_slist_free(device_instances);
-       device_instances = NULL;
+       g_slist_free(dev_insts);
+       dev_insts = NULL;
 
        if(usb_context)
                libusb_exit(usb_context);
        usb_context = NULL;
 
-       return SR_OK;
+       return ret;
 }
 
 static void *hw_dev_info_get(int device_index, int device_info_id)