Commit d2feacdfb2a03c56f873d7391030db892745533a

Con Kolivas 2013-10-24T22:26:13

Cancel cancellable usb transfers on work restart messages.

diff --git a/cgminer.c b/cgminer.c
index d298241..07bd790 100644
--- a/cgminer.c
+++ b/cgminer.c
@@ -3929,6 +3929,10 @@ static void restart_threads(void)
 	mutex_lock(&restart_lock);
 	pthread_cond_broadcast(&restart_cond);
 	mutex_unlock(&restart_lock);
+
+#ifdef USE_USBUTILS
+	cancel_usb_transfers();
+#endif
 }
 
 static void set_curblock(char *hexstr, unsigned char *hash)
diff --git a/usbutils.c b/usbutils.c
index f5ccbd6..90e890a 100644
--- a/usbutils.c
+++ b/usbutils.c
@@ -2217,6 +2217,22 @@ struct usb_transfer {
 	struct list_head list;
 };
 
+void cancel_usb_transfers(void)
+{
+	struct usb_transfer *ut;
+	int cancellations = 0;
+
+	cg_rlock(&cgusb_fd_lock);
+	list_for_each_entry(ut, &ct_list, list) {
+		libusb_cancel_transfer(ut->transfer);
+		cancellations++;
+	}
+	cg_runlock(&cgusb_fd_lock);
+
+	if (cancellations)
+		applog(LOG_DEBUG, "Cancelled %d USB transfers", cancellations);
+}
+
 static void init_usb_transfer(struct usb_transfer *ut)
 {
 	cgsem_init(&ut->cgsem);
diff --git a/usbutils.h b/usbutils.h
index 2750c09..2253337 100644
--- a/usbutils.h
+++ b/usbutils.h
@@ -356,6 +356,7 @@ enum usb_cmds {
 struct device_drv;
 struct cgpu_info;
 
+void cancel_usb_transfers(void);
 void usb_all(int level);
 const char *usb_cmdname(enum usb_cmds cmd);
 void usb_applog(struct cgpu_info *bflsc, enum usb_cmds cmd, char *msg, int amount, int err);
@@ -409,6 +410,9 @@ void *usb_resource_thread(void *userdata);
 #define usb_read_once_timeout(cgpu, buf, bufsiz, read, timeout, cmd) \
 	_usb_read(cgpu, DEFAULT_INTINFO, DEFAULT_EP_IN, buf, bufsiz, read, timeout, NULL, cmd, true, false)
 
+#define usb_read_once_timeout_cancellable(cgpu, buf, bufsiz, read, timeout, cmd) \
+	_usb_read(cgpu, DEFAULT_INTINFO, DEFAULT_EP_IN, buf, bufsiz, read, timeout, NULL, cmd, true, true)
+
 #define usb_read_ii_once_timeout(cgpu, intinfo, buf, bufsiz, read, timeout, cmd) \
 	_usb_read(cgpu, intinfo, DEFAULT_EP_IN, buf, bufsiz, read, timeout, NULL, cmd, true, false)