Commit 0e60ca301fdda615d8a3c12539fc5c8bcfaa6464

Con Kolivas 2013-11-22T23:21:44

Make usb_detect return the cgpu associated with it to check if it succeeds to decide on whether to increment the device count or not.

diff --git a/driver-avalon.c b/driver-avalon.c
index 4a4b0b8..aaabe55 100644
--- a/driver-avalon.c
+++ b/driver-avalon.c
@@ -741,7 +741,7 @@ static void bitburner_get_version(struct cgpu_info *avalon)
 	}
 }
 
-static bool avalon_detect_one(libusb_device *dev, struct usb_find_devices *found)
+static struct cgpu_info *avalon_detect_one(libusb_device *dev, struct usb_find_devices *found)
 {
 	int baud, miner_count, asic_count, timeout, frequency;
 	int this_option_offset;
@@ -851,7 +851,7 @@ static bool avalon_detect_one(libusb_device *dev, struct usb_find_devices *found
 		bitburner_get_version(avalon);
 	}
 
-	return true;
+	return avalon;
 
 unshin:
 
@@ -864,7 +864,7 @@ shin:
 
 	avalon = usb_free_cgpu(avalon);
 
-	return false;
+	return NULL;
 }
 
 static void avalon_detect(bool __maybe_unused hotplug)
diff --git a/driver-bflsc.c b/driver-bflsc.c
index f3e45c4..5dafa9c 100644
--- a/driver-bflsc.c
+++ b/driver-bflsc.c
@@ -688,7 +688,7 @@ ne:
 	return ok;
 }
 
-static bool bflsc_detect_one(struct libusb_device *dev, struct usb_find_devices *found)
+static struct cgpu_info *bflsc_detect_one(struct libusb_device *dev, struct usb_find_devices *found)
 {
 	struct bflsc_info *sc_info = NULL;
 	char buf[BFLSC_BUFSIZ+1];
@@ -888,7 +888,7 @@ reinit:
 	mutex_init(&bflsc->device_mutex);
 	rwlock_init(&sc_info->stat_lock);
 
-	return true;
+	return bflsc;
 
 unshin:
 
@@ -906,7 +906,7 @@ shin:
 
 	bflsc = usb_free_cgpu(bflsc);
 
-	return false;
+	return NULL;
 }
 
 static void bflsc_detect(bool __maybe_unused hotplug)
diff --git a/driver-bitforce.c b/driver-bitforce.c
index 901ba08..d4f51c1 100644
--- a/driver-bitforce.c
+++ b/driver-bitforce.c
@@ -162,7 +162,7 @@ failed:
 		mutex_unlock(&bitforce->device_mutex);
 }
 
-static bool bitforce_detect_one(struct libusb_device *dev, struct usb_find_devices *found)
+static struct cgpu_info *bitforce_detect_one(struct libusb_device *dev, struct usb_find_devices *found)
 {
 	char buf[BITFORCE_BUFSIZ+1];
 	int err, amount;
@@ -270,7 +270,7 @@ reinit:
 
 	mutex_init(&bitforce->device_mutex);
 
-	return true;
+	return bitforce;
 
 unshin:
 
@@ -285,7 +285,7 @@ shin:
 
 	bitforce = usb_free_cgpu(bitforce);
 
-	return false;
+	return NULL;
 }
 
 static void bitforce_detect(bool __maybe_unused hotplug)
diff --git a/driver-bitfury.c b/driver-bitfury.c
index f171120..a54723a 100644
--- a/driver-bitfury.c
+++ b/driver-bitfury.c
@@ -270,7 +270,7 @@ out_close:
 	return false;
 }
 
-static bool bitfury_detect_one(struct libusb_device *dev, struct usb_find_devices *found)
+static struct cgpu_info *bitfury_detect_one(struct libusb_device *dev, struct usb_find_devices *found)
 {
 	struct cgpu_info *bitfury;
 	struct bitfury_info *info;
@@ -308,7 +308,7 @@ static bool bitfury_detect_one(struct libusb_device *dev, struct usb_find_device
 out:
 		bitfury = usb_free_cgpu(bitfury);
 	}
-	return ret;
+	return bitfury;
 }
 
 static void bitfury_detect(bool __maybe_unused hotplug)
diff --git a/driver-icarus.c b/driver-icarus.c
index 7da4a99..af35124 100644
--- a/driver-icarus.c
+++ b/driver-icarus.c
@@ -786,7 +786,7 @@ static void get_options(int this_option_offset, struct cgpu_info *icarus, int *b
 	}
 }
 
-static bool icarus_detect_one(struct libusb_device *dev, struct usb_find_devices *found)
+static struct cgpu_info *icarus_detect_one(struct libusb_device *dev, struct usb_find_devices *found)
 {
 	int this_option_offset = ++option_offset;
 	struct ICARUS_INFO *info;
@@ -1010,7 +1010,7 @@ cmr2_retry:
 		}
 	}
 
-	return true;
+	return icarus;
 
 unshin:
 
@@ -1022,7 +1022,7 @@ shin:
 
 	icarus = usb_free_cgpu(icarus);
 
-	return false;
+	return NULL;
 }
 
 static void icarus_detect(bool __maybe_unused hotplug)
diff --git a/driver-klondike.c b/driver-klondike.c
index 3a740ec..5da915c 100644
--- a/driver-klondike.c
+++ b/driver-klondike.c
@@ -765,7 +765,7 @@ static void control_init(struct cgpu_info *klncgpu)
 			  klncgpu->drv->name, klncgpu->device_id, err);
 }
 
-static bool klondike_detect_one(struct libusb_device *dev, struct usb_find_devices *found)
+static struct cgpu_info *klondike_detect_one(struct libusb_device *dev, struct usb_find_devices *found)
 {
 	struct cgpu_info *klncgpu = usb_alloc_cgpu(&klondike_drv, 1);
 	struct klondike_info *klninfo = NULL;
@@ -823,7 +823,7 @@ static bool klondike_detect_one(struct libusb_device *dev, struct usb_find_devic
 				applog(LOG_DEBUG, "Klondike cgpu added");
 				rwlock_init(&klninfo->stat_lock);
 				cglock_init(&klninfo->klist_lock);
-				return true;
+				return klncgpu;
 			}
 		}
 		usb_uninit(klncgpu);
@@ -831,7 +831,7 @@ static bool klondike_detect_one(struct libusb_device *dev, struct usb_find_devic
 	free(klninfo->free);
 	free(klninfo);
 	free(klncgpu);
-	return false;
+	return NULL;
 }
 
 static void klondike_detect(bool __maybe_unused hotplug)
diff --git a/driver-modminer.c b/driver-modminer.c
index e10cbba..642d13d 100644
--- a/driver-modminer.c
+++ b/driver-modminer.c
@@ -109,7 +109,7 @@ static void do_ping(struct cgpu_info *modminer)
 		modminer->drv->name, modminer->fpgaid, amount, err);
 }
 
-static bool modminer_detect_one(struct libusb_device *dev, struct usb_find_devices *found)
+static struct cgpu_info *modminer_detect_one(struct libusb_device *dev, struct usb_find_devices *found)
 {
 	char buf[0x100+1];
 	char *devname = NULL;
@@ -217,7 +217,7 @@ static bool modminer_detect_one(struct libusb_device *dev, struct usb_find_devic
 
 	modminer = usb_free_cgpu(modminer);
 
-	return true;
+	return modminer;
 
 unshin:
 	if (!added)
@@ -232,9 +232,9 @@ shin:
 	modminer = usb_free_cgpu(modminer);
 
 	if (added)
-		return true;
+		return modminer;
 	else
-		return false;
+		return NULL;
 }
 
 static void modminer_detect(bool __maybe_unused hotplug)
diff --git a/usbutils.c b/usbutils.c
index 482471f..b3459d2 100644
--- a/usbutils.c
+++ b/usbutils.c
@@ -1410,6 +1410,7 @@ void usb_uninit(struct cgpu_info *cgpu)
 static void release_cgpu(struct cgpu_info *cgpu)
 {
 	struct cg_usb_device *cgusb = cgpu->usbdev;
+	bool initted = cgpu->usbinfo.initialised;
 	struct cgpu_info *lookcgpu;
 	int i;
 
@@ -1420,9 +1421,11 @@ static void release_cgpu(struct cgpu_info *cgpu)
 	applog(LOG_DEBUG, "USB release %s%i",
 			cgpu->drv->name, cgpu->device_id);
 
-	zombie_devs++;
-	total_count--;
-	drv_count[cgpu->drv->drv_id].count--;
+	if (initted) {
+		zombie_devs++;
+		total_count--;
+		drv_count[cgpu->drv->drv_id].count--;
+	}
 
 	cgpu->usbinfo.nodev = true;
 	cgpu->usbinfo.nodev_count++;
@@ -1432,8 +1435,10 @@ static void release_cgpu(struct cgpu_info *cgpu)
 	for (i = 0; i < total_devices; i++) {
 		lookcgpu = get_devices(i);
 		if (lookcgpu != cgpu && lookcgpu->usbdev == cgusb) {
-			total_count--;
-			drv_count[lookcgpu->drv->drv_id].count--;
+			if (initted) {
+				total_count--;
+				drv_count[lookcgpu->drv->drv_id].count--;
+			}
 
 			lookcgpu->usbinfo.nodev = true;
 			lookcgpu->usbinfo.nodev_count++;
@@ -1978,11 +1983,12 @@ static struct usb_find_devices *usb_check(__maybe_unused struct device_drv *drv,
 	return NULL;
 }
 
-void usb_detect(struct device_drv *drv, bool (*device_detect)(struct libusb_device *, struct usb_find_devices *))
+void usb_detect(struct device_drv *drv, struct cgpu_info *(*device_detect)(struct libusb_device *, struct usb_find_devices *))
 {
 	libusb_device **list;
 	ssize_t count, i;
 	struct usb_find_devices *found;
+	struct cgpu_info *cgpu;
 
 	applog(LOG_DEBUG, "USB scan devices: checking for %s devices", drv->name);
 
@@ -2027,9 +2033,11 @@ void usb_detect(struct device_drv *drv, bool (*device_detect)(struct libusb_devi
 			if (is_in_use(list[i]) || cgminer_usb_lock(drv, list[i]) == false)
 				free(found);
 			else {
-				if (!device_detect(list[i], found))
+				cgpu = device_detect(list[i], found);
+				if (!cgpu)
 					cgminer_usb_unlock(drv, list[i]);
 				else {
+					cgpu->usbinfo.initialised = true;
 					total_count++;
 					drv_count[drv->drv_id].count++;
 				}
diff --git a/usbutils.h b/usbutils.h
index a663816..035f83e 100644
--- a/usbutils.h
+++ b/usbutils.h
@@ -230,6 +230,7 @@ struct cg_usb_info {
 	uint8_t device_address;
 	int usbstat;
 	bool nodev;
+	bool initialised;
 	int nodev_count;
 	struct timeval last_nodev;
 	uint32_t ioerr_count;
@@ -388,7 +389,7 @@ struct cgpu_info *usb_alloc_cgpu(struct device_drv *drv, int threads);
 struct cgpu_info *usb_free_cgpu(struct cgpu_info *cgpu);
 void usb_uninit(struct cgpu_info *cgpu);
 bool usb_init(struct cgpu_info *cgpu, struct libusb_device *dev, struct usb_find_devices *found);
-void usb_detect(struct device_drv *drv, bool (*device_detect)(struct libusb_device *, struct usb_find_devices *));
+void usb_detect(struct device_drv *drv, struct cgpu_info *(*device_detect)(struct libusb_device *, struct usb_find_devices *));
 struct api_data *api_usb_stats(int *count);
 void update_usb_stats(struct cgpu_info *cgpu);
 int _usb_read(struct cgpu_info *cgpu, int intinfo, int epinfo, char *buf, size_t bufsiz, int *processed, int timeout, const char *end, enum usb_cmds cmd, bool readonce, bool cancellable);