Commit e35e8ae135c4e9452874bc0818f87e09b7479df0

Kano 2013-03-04T09:45:07

Implement and use usb_cleanup() on shutdown or restart

diff --git a/cgminer.c b/cgminer.c
index 3c70257..910bce5 100644
--- a/cgminer.c
+++ b/cgminer.c
@@ -2842,6 +2842,16 @@ static void __kill_work(void)
 	applog(LOG_DEBUG, "Killing off API thread");
 	thr = &control_thr[api_thr_id];
 	thr_info_cancel(thr);
+
+#if defined(USE_MODMINER) || defined(USE_BITFORCE)
+	/* Release USB resources in case it's a restart
+	 * and not a QUIT */
+	if (!opt_scrypt) {
+		applog(LOG_DEBUG, "Releasing all USB devices");
+		usb_cleanup();
+	}
+#endif
+
 }
 
 /* This should be the common exit path */
diff --git a/usbutils.c b/usbutils.c
index f0dd7c7..27c67ce 100644
--- a/usbutils.c
+++ b/usbutils.c
@@ -184,6 +184,7 @@ static const char *C_SETFLOW_S = "SetFlowCtrl";
 static const char *C_SETMODEM_S = "SetModemCtrl";
 static const char *C_PURGERX_S = "PurgeRx";
 static const char *C_PURGETX_S = "PurgeTx";
+static const char *C_FLASHREPLY_S = "FlashReply";
 
 #ifdef EOL
 #undef EOL
@@ -572,6 +573,7 @@ static void cgusb_check_init()
 		usb_commands[C_SETMODEM] = C_SETMODEM_S;
 		usb_commands[C_PURGERX] = C_PURGERX_S;
 		usb_commands[C_PURGETX] = C_PURGETX_S;
+		usb_commands[C_FLASHREPLY] = C_FLASHREPLY_S;
 
 		stats_initialised = true;
 	}
@@ -1522,5 +1524,17 @@ int _usb_transfer(struct cgpu_info *cgpu, uint8_t request_type, uint8_t bRequest
 
 void usb_cleanup()
 {
-	// TODO:
+	int i;
+
+	mutex_lock(&devices_lock);
+	for (i = 0; i < total_devices; i++) {
+		switch (devices[i]->drv->drv_id) {
+			case DRIVER_BITFORCE:
+			case DRIVER_MODMINER:
+				release_cgpu(devices[i]);
+			default:
+				break;
+		}
+	}
+	mutex_unlock(&devices_lock);
 }
diff --git a/usbutils.h b/usbutils.h
index 1ec1936..fc6340f 100644
--- a/usbutils.h
+++ b/usbutils.h
@@ -122,6 +122,7 @@ enum usb_cmds {
 	C_SETMODEM,
 	C_PURGERX,
 	C_PURGETX,
+	C_FLASHREPLY,
 	C_MAX
 };