Browse Source

ztex: Keep interface claimed as a lock

Luke Dashjr 12 years ago
parent
commit
ba1bcb74be
1 changed files with 13 additions and 0 deletions
  1. 13 0
      libztex.c

+ 13 - 0
libztex.c

@@ -143,6 +143,12 @@ static enum check_result libztex_checkDevice(struct libusb_device *dev)
 		goto done;
 		goto done;
 	}
 	}
 
 
+	if (libusb_claim_interface(hndl, 0) == LIBUSB_ERROR_BUSY)
+	{
+		applog(LOG_DEBUG, "Ztex check device: Interface already busy, skipping");
+		goto done;
+	}
+	
 	cnt = libusb_control_transfer(hndl, 0xc0, 0x22, 0, 0, buf, 40, 500);
 	cnt = libusb_control_transfer(hndl, 0xc0, 0x22, 0, 0, buf, 40, 500);
 	if (unlikely(cnt < 0)) {
 	if (unlikely(cnt < 0)) {
 		applog(LOG_ERR, "Ztex check device: Failed to read ztex descriptor with err %d", cnt);
 		applog(LOG_ERR, "Ztex check device: Failed to read ztex descriptor with err %d", cnt);
@@ -260,7 +266,10 @@ static enum check_result libztex_checkDevice(struct libusb_device *dev)
 done:
 done:
 	bytes_free(&bsdata);
 	bytes_free(&bsdata);
 	if (hndl)
 	if (hndl)
+	{
+		libusb_release_interface(hndl, 0);
 		libusb_close(hndl);
 		libusb_close(hndl);
+	}
 	return ret;
 	return ret;
 }
 }
 
 
@@ -484,6 +493,9 @@ int libztex_configureFpga(struct libztex_device *ztex, const char *repr)
 	rv = libztex_configureFpgaHS(ztex, buf, true, 2, repr);
 	rv = libztex_configureFpgaHS(ztex, buf, true, 2, repr);
 	if (rv != 0)
 	if (rv != 0)
 		rv = libztex_configureFpgaLS(ztex, buf, true, 2, repr);
 		rv = libztex_configureFpgaLS(ztex, buf, true, 2, repr);
+	if (!rv)
+		if (libusb_claim_interface(ztex->hndl, 0) == LIBUSB_ERROR_BUSY)
+			rv = -5;
 	return rv;
 	return rv;
 }
 }
 
 
@@ -704,6 +716,7 @@ int libztex_prepare_device(struct libusb_device *dev, struct libztex_device** zt
 void libztex_destroy_device(struct libztex_device* ztex)
 void libztex_destroy_device(struct libztex_device* ztex)
 {
 {
 	if (ztex->hndl != NULL) {
 	if (ztex->hndl != NULL) {
+		libusb_release_interface(ztex->hndl, 0);
 		libusb_close(ztex->hndl);
 		libusb_close(ztex->hndl);
 		ztex->hndl = NULL;
 		ztex->hndl = NULL;
 	}
 	}