From 9287ee90f6fd54927042eb5ea4a061e66eebbc05 Mon Sep 17 00:00:00 2001 From: Kano Date: Mon, 10 Jun 2013 15:22:37 +1000 Subject: [PATCH 1/7] USB control creation and free of cgpu --- driver-avalon.c | 38 +++++++++++++++-------- driver-bflsc.c | 10 ++---- driver-bitforce.c | 8 ++--- driver-icarus.c | 9 ++---- driver-modminer.c | 35 ++++++--------------- usbutils.c | 77 +++++++++++++++++++++++++++++++++++++++-------- usbutils.h | 5 +++ 7 files changed, 112 insertions(+), 70 deletions(-) diff --git a/driver-avalon.c b/driver-avalon.c index ddb7be7d..610483b1 100644 --- a/driver-avalon.c +++ b/driver-avalon.c @@ -578,17 +578,13 @@ static bool avalon_detect_one(libusb_device *dev, struct usb_find_devices *found bool configured; int ret; - avalon = calloc(1, sizeof(struct cgpu_info)); - if (unlikely(!avalon)) - quit(1, "Failed to calloc avalon in avalon_detect_one");; - avalon->drv = &avalon_drv; - avalon->threads = AVALON_MINER_THREADS; + avalon = usb_alloc_cgpu(&avalon_drv, AVALON_MINER_THREADS); configured = get_options(this_option_offset, &baud, &miner_count, &asic_count, &timeout, &frequency); if (!usb_init(avalon, dev, found)) - return false; + goto shin; /* Even though this is an FTDI type chip, we want to do the parsing * all ourselves so set it to std usb type */ @@ -602,11 +598,10 @@ static bool avalon_detect_one(libusb_device *dev, struct usb_find_devices *found avalon_initialise(avalon); avalon->device_path = strdup(devpath); - add_cgpu(avalon); avalon->device_data = calloc(sizeof(struct avalon_info), 1); if (unlikely(!(avalon->device_data))) - quit(1, "Failed to malloc avalon_info data"); + quit(1, "Failed to calloc avalon_info data"); info = avalon->device_data; if (configured) { @@ -635,10 +630,13 @@ static bool avalon_detect_one(libusb_device *dev, struct usb_find_devices *found info->temp_old = 0; ret = avalon_reset(avalon, true); - if (ret && !configured) { - usb_uninit(avalon); - return false; - } + if (ret && !configured) + goto unshin; + + if (!add_cgpu(avalon)) + goto unshin; + + update_usb_stats(avalon); avalon_idle(avalon, info); @@ -648,6 +646,22 @@ static bool avalon_detect_one(libusb_device *dev, struct usb_find_devices *found info->frequency); return true; + +unshin: + + usb_uninit(avalon); + +shin: + + if (avalon->device_data) + free(avalon->device_data); + + if (avalon->device_path) + free(avalon->device_path); + + avalon = usb_free_cgpu(avalon); + + return false; } static void avalon_detect(void) diff --git a/driver-bflsc.c b/driver-bflsc.c index 2b13ede6..501fa56d 100644 --- a/driver-bflsc.c +++ b/driver-bflsc.c @@ -810,13 +810,7 @@ static bool bflsc_detect_one(struct libusb_device *dev, struct usb_find_devices char *newname; uint16_t latency; - struct cgpu_info *bflsc = calloc(1, sizeof(*bflsc)); - - if (unlikely(!bflsc)) - quit(1, "Failed to calloc bflsc in bflsc_detect_one"); - bflsc->drv = &bflsc_drv; - bflsc->deven = DEV_ENABLED; - bflsc->threads = 1; + struct cgpu_info *bflsc = usb_alloc_cgpu(&bflsc_drv, 1); sc_info = calloc(1, sizeof(*sc_info)); if (unlikely(!sc_info)) @@ -980,7 +974,7 @@ shin: if (bflsc->drv->copy) free(bflsc->drv); - free(bflsc); + bflsc = usb_free_cgpu(bflsc); return false; } diff --git a/driver-bitforce.c b/driver-bitforce.c index c845285e..7e2f1d1e 100644 --- a/driver-bitforce.c +++ b/driver-bitforce.c @@ -173,11 +173,7 @@ static bool bitforce_detect_one(struct libusb_device *dev, struct usb_find_devic int init_sleep, init_count; bool ident_first; - struct cgpu_info *bitforce = NULL; - bitforce = calloc(1, sizeof(*bitforce)); - bitforce->drv = &bitforce_drv; - bitforce->deven = DEV_ENABLED; - bitforce->threads = 1; + struct cgpu_info *bitforce = usb_alloc_cgpu(&bitforce_drv, 1); if (!usb_init(bitforce, dev, found)) goto shin; @@ -298,7 +294,7 @@ shin: if (bitforce->drv->copy) free(bitforce->drv); - free(bitforce); + bitforce = usb_free_cgpu(bitforce); return false; } diff --git a/driver-icarus.c b/driver-icarus.c index a6c6fb0e..d854101d 100644 --- a/driver-icarus.c +++ b/driver-icarus.c @@ -751,12 +751,7 @@ static bool icarus_detect_one(struct libusb_device *dev, struct usb_find_devices int ret, err, amount, tries; bool ok; - icarus = calloc(1, sizeof(struct cgpu_info)); - if (unlikely(!icarus)) - quit(1, "Failed to calloc icarus in icarus_detect_one"); - icarus->drv = &icarus_drv; - icarus->deven = DEV_ENABLED; - icarus->threads = 1; + icarus = usb_alloc_cgpu(&icarus_drv, 1); if (!usb_init(icarus, dev, found)) goto shin; @@ -851,7 +846,7 @@ unshin: shin: - free(icarus); + icarus = usb_free_cgpu(icarus); return false; } diff --git a/driver-modminer.c b/driver-modminer.c index 8d609dfb..5df05603 100644 --- a/driver-modminer.c +++ b/driver-modminer.c @@ -119,9 +119,8 @@ static bool modminer_detect_one(struct libusb_device *dev, struct usb_find_devic int err, i, amount; bool added = false; - struct cgpu_info *modminer = NULL; - modminer = calloc(1, sizeof(*modminer)); - modminer->drv = &modminer_drv; + struct cgpu_info *modminer = usb_alloc_cgpu(&modminer_drv, 1); + modminer->modminer_mutex = calloc(1, sizeof(*(modminer->modminer_mutex))); mutex_init(modminer->modminer_mutex); modminer->fpgaid = (char)0; @@ -193,10 +192,7 @@ static bool modminer_detect_one(struct libusb_device *dev, struct usb_find_devic // TODO: test with 1 board missing in the middle and each end // to see how that affects the sequence numbers for (i = 0; i < buf[0]; i++) { - struct cgpu_info *tmp = calloc(1, sizeof(*tmp)); - - tmp->drv = copy_drv(modminer->drv); - tmp->name = devname; + struct cgpu_info *tmp = usb_copy_cgpu(modminer); sprintf(devpath, "%d:%d:%d", (int)(modminer->usbinfo.bus_number), @@ -204,22 +200,17 @@ static bool modminer_detect_one(struct libusb_device *dev, struct usb_find_devic i); tmp->device_path = strdup(devpath); - tmp->usbdev = modminer->usbdev; - tmp->usbinfo.bus_number = modminer->usbinfo.bus_number; - tmp->usbinfo.device_address = modminer->usbinfo.device_address; + // Only the first copy gets the already used stats - if (!added) - tmp->usbinfo.usbstat = modminer->usbinfo.usbstat; + if (added) + tmp->usbinfo.usbstat = USB_NOSTAT; + tmp->fpgaid = (char)i; tmp->modminer_mutex = modminer->modminer_mutex; - tmp->deven = DEV_ENABLED; - tmp->threads = 1; if (!add_cgpu(tmp)) { free(tmp->device_path); - if (tmp->drv->copy) - free(tmp->drv); - free(tmp); + tmp = usb_free_cgpu(tmp); goto unshin; } @@ -228,10 +219,7 @@ static bool modminer_detect_one(struct libusb_device *dev, struct usb_find_devic added = true; } - if (modminer->drv->copy) - free(modminer->drv); - - free(modminer); + modminer = usb_free_cgpu(modminer); return true; @@ -243,10 +231,7 @@ shin: if (!added) free(modminer->modminer_mutex); - if (modminer->drv->copy) - free(modminer->drv); - - free(modminer); + modminer = usb_free_cgpu(modminer); if (added) return true; diff --git a/usbutils.c b/usbutils.c index 53430fa7..1a0e1182 100644 --- a/usbutils.c +++ b/usbutils.c @@ -451,7 +451,7 @@ struct cg_usb_stats { #define SEQ1 1 static struct cg_usb_stats *usb_stats = NULL; -static int next_stat = 0; +static int next_stat = USB_NOSTAT; #define USB_STATS(sgpu, sta, fin, err, mode, cmd, seq) \ stats(cgpu, sta, fin, err, mode, cmd, seq) @@ -1380,6 +1380,54 @@ static void release_cgpu(struct cgpu_info *cgpu) cgminer_usb_unlock_bd(cgpu->drv, cgpu->usbinfo.bus_number, cgpu->usbinfo.device_address); } +// Currently only used by MMQ +struct cgpu_info *usb_copy_cgpu(struct cgpu_info *orig) +{ + struct cgpu_info *copy = calloc(1, sizeof(*copy)); + + if (unlikely(!copy)) + quit(1, "Failed to calloc cgpu for %s in usb_copy_cgpu", orig->drv->dname); + + copy->name = orig->name; + copy->drv = copy_drv(orig->drv); + copy->deven = orig->deven; + copy->threads = orig->threads; + + copy->usbdev = orig->usbdev; + + memcpy(&(copy->usbinfo), &(orig->usbinfo), sizeof(copy->usbinfo)); + + copy->usbinfo.nodev = (copy->usbdev != NULL); + + return copy; +} + +struct cgpu_info *usb_alloc_cgpu(struct device_drv *drv, int threads) +{ + struct cgpu_info *cgpu = calloc(1, sizeof(*cgpu)); + + if (unlikely(!cgpu)) + quit(1, "Failed to calloc cgpu for %s in usb_alloc_cgpu", drv->dname); + + cgpu->drv = drv; + cgpu->deven = DEV_ENABLED; + cgpu->threads = threads; + + cgpu->usbinfo.nodev = true; + + return cgpu; +} + +struct cgpu_info *usb_free_cgpu(struct cgpu_info *cgpu) +{ + if (cgpu->drv->copy) + free(cgpu->drv); + + free(cgpu); + + return NULL; +} + #define USB_INIT_FAIL 0 #define USB_INIT_OK 1 #define USB_INIT_IGNORE 2 @@ -1616,6 +1664,7 @@ static int _usb_init(struct cgpu_info *cgpu, struct libusb_device *dev, struct u cgusb->manuf_string, cgusb->serial_string); cgpu->usbdev = cgusb; + cgpu->usbinfo.nodev = false; libusb_free_config_descriptor(config); @@ -1876,7 +1925,7 @@ struct api_data *api_usb_stats(__maybe_unused int *count) int cmdseq; char modes_s[32]; - if (next_stat == 0) + if (next_stat == USB_NOSTAT) return NULL; while (*count < next_stat * C_MAX * 2) { @@ -1949,21 +1998,25 @@ static void newstats(struct cgpu_info *cgpu) int i; mutex_lock(&cgusb_lock); - cgpu->usbinfo.usbstat = ++next_stat; - mutex_unlock(&cgusb_lock); - usb_stats = realloc(usb_stats, sizeof(*usb_stats) * next_stat); + cgpu->usbinfo.usbstat = next_stat + 1; + + usb_stats = realloc(usb_stats, sizeof(*usb_stats) * (next_stat+1)); if (unlikely(!usb_stats)) - quit(1, "USB failed to realloc usb_stats %d", next_stat); + quit(1, "USB failed to realloc usb_stats %d", next_stat+1); - usb_stats[next_stat-1].name = cgpu->drv->name; - usb_stats[next_stat-1].device_id = -1; - usb_stats[next_stat-1].details = calloc(1, sizeof(struct cg_usb_stats_details) * C_MAX * 2); - if (unlikely(!usb_stats[next_stat-1].details)) - quit(1, "USB failed to calloc details for %d", next_stat); + usb_stats[next_stat].name = cgpu->drv->name; + usb_stats[next_stat].device_id = -1; + usb_stats[next_stat].details = calloc(1, sizeof(struct cg_usb_stats_details) * C_MAX * 2); + if (unlikely(!usb_stats[next_stat].details)) + quit(1, "USB failed to calloc details for %d", next_stat+1); for (i = 1; i < C_MAX * 2; i += 2) - usb_stats[next_stat-1].details[i].seq = 1; + usb_stats[next_stat].details[i].seq = 1; + + next_stat++; + + mutex_unlock(&cgusb_lock); } #endif diff --git a/usbutils.h b/usbutils.h index 199cbb14..96f2b84b 100644 --- a/usbutils.h +++ b/usbutils.h @@ -177,6 +177,8 @@ struct cg_usb_device { uint32_t bufamt; }; +#define USB_NOSTAT 0 + struct cg_usb_info { uint8_t bus_number; uint8_t device_address; @@ -258,6 +260,9 @@ struct cgpu_info; 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); +struct cgpu_info *usb_copy_cgpu(struct cgpu_info *orig); +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 *)); From 749fa78d8a882cdfaec8813f0cf64ed63f5642d2 Mon Sep 17 00:00:00 2001 From: Kano Date: Mon, 10 Jun 2013 15:39:44 +1000 Subject: [PATCH 2/7] tidy up free in device detect functions --- driver-avalon.c | 8 ++++---- driver-bflsc.c | 10 ++++++---- driver-bitforce.c | 8 ++++---- driver-icarus.c | 1 + driver-modminer.c | 6 +++++- 5 files changed, 20 insertions(+), 13 deletions(-) diff --git a/driver-avalon.c b/driver-avalon.c index 610483b1..35307c4d 100644 --- a/driver-avalon.c +++ b/driver-avalon.c @@ -653,11 +653,11 @@ unshin: shin: - if (avalon->device_data) - free(avalon->device_data); + free(avalon->device_data); + avalon->device_data = NULL; - if (avalon->device_path) - free(avalon->device_path); + free(avalon->device_path); + avalon->device_path = NULL; avalon = usb_free_cgpu(avalon); diff --git a/driver-bflsc.c b/driver-bflsc.c index 501fa56d..52ba312d 100644 --- a/driver-bflsc.c +++ b/driver-bflsc.c @@ -966,13 +966,15 @@ unshin: shin: free(bflsc->device_path); + bflsc->device_path = NULL; + free(bflsc->device_data); + bflsc->device_data = NULL; - if (bflsc->name != blank) + if (bflsc->name != blank) { free(bflsc->name); - - if (bflsc->drv->copy) - free(bflsc->drv); + bflsc->name = NULL; + } bflsc = usb_free_cgpu(bflsc); diff --git a/driver-bitforce.c b/driver-bitforce.c index 7e2f1d1e..de575cff 100644 --- a/driver-bitforce.c +++ b/driver-bitforce.c @@ -287,12 +287,12 @@ unshin: shin: free(bitforce->device_path); + bitforce->device_path = NULL; - if (bitforce->name != blank) + if (bitforce->name != blank) { free(bitforce->name); - - if (bitforce->drv->copy) - free(bitforce->drv); + bitforce->name = NULL; + } bitforce = usb_free_cgpu(bitforce); diff --git a/driver-icarus.c b/driver-icarus.c index d854101d..4b06c53d 100644 --- a/driver-icarus.c +++ b/driver-icarus.c @@ -843,6 +843,7 @@ unshin: usb_uninit(icarus); free(icarus->device_path); + icarus->device_path = NULL; shin: diff --git a/driver-modminer.c b/driver-modminer.c index 5df05603..f537e7d8 100644 --- a/driver-modminer.c +++ b/driver-modminer.c @@ -210,6 +210,7 @@ static bool modminer_detect_one(struct libusb_device *dev, struct usb_find_devic if (!add_cgpu(tmp)) { free(tmp->device_path); + tmp->device_path = NULL; tmp = usb_free_cgpu(tmp); goto unshin; } @@ -228,8 +229,10 @@ unshin: usb_uninit(modminer); shin: - if (!added) + if (!added) { free(modminer->modminer_mutex); + modminer->modminer_mutex = NULL; + } modminer = usb_free_cgpu(modminer); @@ -1096,6 +1099,7 @@ static void modminer_hw_error(struct thr_info *thr) static void modminer_fpga_shutdown(struct thr_info *thr) { free(thr->cgpu_data); + thr->cgpu_data = NULL; } static char *modminer_set_device(struct cgpu_info *modminer, char *option, char *setting, char *replybuf) From b8b137aeef6cd313e7879aba56cd4830c2480bf4 Mon Sep 17 00:00:00 2001 From: Kano Date: Mon, 10 Jun 2013 15:59:02 +1000 Subject: [PATCH 3/7] USB make device_path handled by usbutils --- driver-avalon.c | 12 +----------- driver-bflsc.c | 29 +++++++++-------------------- driver-bitforce.c | 26 ++++++++------------------ driver-icarus.c | 16 +++------------- driver-modminer.c | 28 +++++++++++----------------- usbutils.c | 12 ++++++++++-- 6 files changed, 42 insertions(+), 81 deletions(-) diff --git a/driver-avalon.c b/driver-avalon.c index 35307c4d..df17d00b 100644 --- a/driver-avalon.c +++ b/driver-avalon.c @@ -574,7 +574,6 @@ static bool avalon_detect_one(libusb_device *dev, struct usb_find_devices *found int this_option_offset = ++option_offset; struct avalon_info *info; struct cgpu_info *avalon; - char devpath[20]; bool configured; int ret; @@ -591,14 +590,8 @@ static bool avalon_detect_one(libusb_device *dev, struct usb_find_devices *found avalon->usbdev->usb_type = USB_TYPE_STD; /* We have a real Avalon! */ - sprintf(devpath, "%d:%d", - (int)(avalon->usbinfo.bus_number), - (int)(avalon->usbinfo.device_address)); - avalon_initialise(avalon); - avalon->device_path = strdup(devpath); - avalon->device_data = calloc(sizeof(struct avalon_info), 1); if (unlikely(!(avalon->device_data))) quit(1, "Failed to calloc avalon_info data"); @@ -642,7 +635,7 @@ static bool avalon_detect_one(libusb_device *dev, struct usb_find_devices *found applog(LOG_DEBUG, "Avalon Detected: %s " "(miner_count=%d asic_count=%d timeout=%d frequency=%d)", - devpath, info->miner_count, info->asic_count, info->timeout, + avalon->device_path, info->miner_count, info->asic_count, info->timeout, info->frequency); return true; @@ -656,9 +649,6 @@ shin: free(avalon->device_data); avalon->device_data = NULL; - free(avalon->device_path); - avalon->device_path = NULL; - avalon = usb_free_cgpu(avalon); return false; diff --git a/driver-bflsc.c b/driver-bflsc.c index 52ba312d..9d1b71c7 100644 --- a/driver-bflsc.c +++ b/driver-bflsc.c @@ -802,7 +802,6 @@ static bool bflsc_detect_one(struct libusb_device *dev, struct usb_find_devices { struct bflsc_info *sc_info = NULL; char buf[BFLSC_BUFSIZ+1]; - char devpath[20]; int i, err, amount; struct timeval init_start, init_now; int init_sleep, init_count; @@ -821,11 +820,6 @@ static bool bflsc_detect_one(struct libusb_device *dev, struct usb_find_devices if (!usb_init(bflsc, dev, found)) goto shin; - sprintf(devpath, "%d:%d", - (int)(bflsc->usbinfo.bus_number), - (int)(bflsc->usbinfo.device_address)); - - // Allow 2 complete attempts if the 1st time returns an unrecognised reply ident_first = true; retry: @@ -837,7 +831,7 @@ reinit: err = write_to_dev(bflsc, 0, BFLSC_IDENTIFY, BFLSC_IDENTIFY_LEN, &amount, C_REQUESTIDENTIFY); if (err < 0 || amount != BFLSC_IDENTIFY_LEN) { applog(LOG_ERR, "%s detect (%s) send identify request failed (%d:%d)", - bflsc->drv->dname, devpath, amount, err); + bflsc->drv->dname, bflsc->device_path, amount, err); goto unshin; } @@ -848,7 +842,7 @@ reinit: if (us_tdiff(&init_now, &init_start) <= REINIT_TIME_MAX) { if (init_count == 2) { applog(LOG_WARNING, "%s detect (%s) 2nd init failed (%d:%d) - retrying", - bflsc->drv->dname, devpath, amount, err); + bflsc->drv->dname, bflsc->device_path, amount, err); } nmsleep(init_sleep); if ((init_sleep * 2) <= REINIT_TIME_MAX_MS) @@ -858,14 +852,14 @@ reinit: if (init_count > 0) applog(LOG_WARNING, "%s detect (%s) init failed %d times %.2fs", - bflsc->drv->dname, devpath, init_count, tdiff(&init_now, &init_start)); + bflsc->drv->dname, bflsc->device_path, init_count, tdiff(&init_now, &init_start)); if (err < 0) { applog(LOG_ERR, "%s detect (%s) error identify reply (%d:%d)", - bflsc->drv->dname, devpath, amount, err); + bflsc->drv->dname, bflsc->device_path, amount, err); } else { applog(LOG_ERR, "%s detect (%s) empty identify reply (%d)", - bflsc->drv->dname, devpath, amount); + bflsc->drv->dname, bflsc->device_path, amount); } goto unshin; @@ -874,24 +868,22 @@ reinit: if (unlikely(!strstr(buf, BFLSC_BFLSC))) { applog(LOG_DEBUG, "%s detect (%s) found an FPGA '%s' ignoring", - bflsc->drv->dname, devpath, buf); + bflsc->drv->dname, bflsc->device_path, buf); goto unshin; } if (unlikely(strstr(buf, BFLSC_IDENTITY))) { if (ident_first) { applog(LOG_DEBUG, "%s detect (%s) didn't recognise '%s' trying again ...", - bflsc->drv->dname, devpath, buf); + bflsc->drv->dname, bflsc->device_path, buf); ident_first = false; goto retry; } applog(LOG_DEBUG, "%s detect (%s) didn't recognise '%s' on 2nd attempt", - bflsc->drv->dname, devpath, buf); + bflsc->drv->dname, bflsc->device_path, buf); goto unshin; } - bflsc->device_path = strdup(devpath); - if (!getinfo(bflsc, 0)) goto unshin; @@ -945,7 +937,7 @@ reinit: // We have a real BFLSC! applog(LOG_DEBUG, "%s (%s) identified as: '%s'", - bflsc->drv->dname, devpath, bflsc->drv->name); + bflsc->drv->dname, bflsc->device_path, bflsc->drv->name); if (!add_cgpu(bflsc)) goto unshin; @@ -965,9 +957,6 @@ unshin: shin: - free(bflsc->device_path); - bflsc->device_path = NULL; - free(bflsc->device_data); bflsc->device_data = NULL; diff --git a/driver-bitforce.c b/driver-bitforce.c index de575cff..33061701 100644 --- a/driver-bitforce.c +++ b/driver-bitforce.c @@ -166,7 +166,6 @@ failed: static bool bitforce_detect_one(struct libusb_device *dev, struct usb_find_devices *found) { char buf[BITFORCE_BUFSIZ+1]; - char devpath[20]; int err, amount; char *s; struct timeval init_start, init_now; @@ -178,10 +177,6 @@ static bool bitforce_detect_one(struct libusb_device *dev, struct usb_find_devic if (!usb_init(bitforce, dev, found)) goto shin; - sprintf(devpath, "%d:%d", - (int)(bitforce->usbinfo.bus_number), - (int)(bitforce->usbinfo.device_address)); - // Allow 2 complete attempts if the 1st time returns an unrecognised reply ident_first = true; retry: @@ -192,7 +187,7 @@ reinit: bitforce_initialise(bitforce, false); if ((err = usb_write(bitforce, BITFORCE_IDENTIFY, BITFORCE_IDENTIFY_LEN, &amount, C_REQUESTIDENTIFY)) < 0 || amount != BITFORCE_IDENTIFY_LEN) { applog(LOG_ERR, "%s detect (%s) send identify request failed (%d:%d)", - bitforce->drv->dname, devpath, amount, err); + bitforce->drv->dname, bitforce->device_path, amount, err); goto unshin; } @@ -202,7 +197,7 @@ reinit: if (us_tdiff(&init_now, &init_start) <= REINIT_TIME_MAX) { if (init_count == 2) { applog(LOG_WARNING, "%s detect (%s) 2nd init failed (%d:%d) - retrying", - bitforce->drv->dname, devpath, amount, err); + bitforce->drv->dname, bitforce->device_path, amount, err); } nmsleep(init_sleep); if ((init_sleep * 2) <= REINIT_TIME_MAX_MS) @@ -212,14 +207,14 @@ reinit: if (init_count > 0) applog(LOG_WARNING, "%s detect (%s) init failed %d times %.2fs", - bitforce->drv->dname, devpath, init_count, tdiff(&init_now, &init_start)); + bitforce->drv->dname, bitforce->device_path, init_count, tdiff(&init_now, &init_start)); if (err < 0) { applog(LOG_ERR, "%s detect (%s) error identify reply (%d:%d)", - bitforce->drv->dname, devpath, amount, err); + bitforce->drv->dname, bitforce->device_path, amount, err); } else { applog(LOG_ERR, "%s detect (%s) empty identify reply (%d)", - bitforce->drv->dname, devpath, amount); + bitforce->drv->dname, bitforce->device_path, amount); } goto unshin; @@ -229,12 +224,12 @@ reinit: if (unlikely(!strstr(buf, "SHA256"))) { if (ident_first) { applog(LOG_WARNING, "%s detect (%s) didn't recognise '%s' trying again ...", - bitforce->drv->dname, devpath, buf); + bitforce->drv->dname, bitforce->device_path, buf); ident_first = false; goto retry; } applog(LOG_ERR, "%s detect (%s) didn't recognise '%s' on 2nd attempt", - bitforce->drv->dname, devpath, buf); + bitforce->drv->dname, bitforce->device_path, buf); goto unshin; } @@ -256,7 +251,7 @@ reinit: // We have a real BitForce! applog(LOG_DEBUG, "%s (%s) identified as: '%s'", - bitforce->drv->dname, devpath, bitforce->name); + bitforce->drv->dname, bitforce->device_path, bitforce->name); /* Initially enable support for nonce range and disable it later if it * fails */ @@ -269,8 +264,6 @@ reinit: bitforce->kname = KNAME_WORK; } - bitforce->device_path = strdup(devpath); - if (!add_cgpu(bitforce)) goto unshin; @@ -286,9 +279,6 @@ unshin: shin: - free(bitforce->device_path); - bitforce->device_path = NULL; - if (bitforce->name != blank) { free(bitforce->name); bitforce->name = NULL; diff --git a/driver-icarus.c b/driver-icarus.c index 4b06c53d..86a781ac 100644 --- a/driver-icarus.c +++ b/driver-icarus.c @@ -728,7 +728,6 @@ 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) { int this_option_offset = ++option_offset; - char devpath[20]; struct ICARUS_INFO *info; struct timeval tv_start, tv_finish; @@ -758,12 +757,6 @@ static bool icarus_detect_one(struct libusb_device *dev, struct usb_find_devices get_options(this_option_offset, icarus, &baud, &work_division, &fpga_count); - sprintf(devpath, "%d:%d", - (int)(icarus->usbinfo.bus_number), - (int)(icarus->usbinfo.device_address)); - - icarus->device_path = strdup(devpath); - hex2bin(ob_bin, golden_ob, sizeof(ob_bin)); tries = 2; @@ -789,7 +782,7 @@ static bool icarus_detect_one(struct libusb_device *dev, struct usb_find_devices applog(LOG_ERR, "Icarus Detect: " "Test failed at %s: get %s, should: %s", - devpath, nonce_hex, golden_nonce); + icarus->device_path, nonce_hex, golden_nonce); } } free(nonce_hex); @@ -801,7 +794,7 @@ static bool icarus_detect_one(struct libusb_device *dev, struct usb_find_devices applog(LOG_DEBUG, "Icarus Detect: " "Test succeeded at %s: got %s", - devpath, golden_nonce); + icarus->device_path, golden_nonce); /* We have a real Icarus! */ if (!add_cgpu(icarus)) @@ -810,7 +803,7 @@ static bool icarus_detect_one(struct libusb_device *dev, struct usb_find_devices update_usb_stats(icarus); applog(LOG_INFO, "%s%d: Found at %s", - icarus->drv->name, icarus->device_id, devpath); + icarus->drv->name, icarus->device_id, icarus->device_path); applog(LOG_DEBUG, "%s%d: Init baud=%d work_division=%d fpga_count=%d", icarus->drv->name, icarus->device_id, baud, work_division, fpga_count); @@ -842,9 +835,6 @@ unshin: usb_uninit(icarus); - free(icarus->device_path); - icarus->device_path = NULL; - shin: icarus = usb_free_cgpu(icarus); diff --git a/driver-modminer.c b/driver-modminer.c index f537e7d8..1519bc9b 100644 --- a/driver-modminer.c +++ b/driver-modminer.c @@ -115,7 +115,7 @@ static bool modminer_detect_one(struct libusb_device *dev, struct usb_find_devic { char buf[0x100+1]; char *devname = NULL; - char devpath[20]; + char devpath[32]; int err, i, amount; bool added = false; @@ -128,44 +128,40 @@ static bool modminer_detect_one(struct libusb_device *dev, struct usb_find_devic if (!usb_init(modminer, dev, found)) goto shin; - sprintf(devpath, "%d:%d", - (int)(modminer->usbinfo.bus_number), - (int)(modminer->usbinfo.device_address)); - do_ping(modminer); if ((err = usb_write(modminer, MODMINER_GET_VERSION, 1, &amount, C_REQUESTVERSION)) < 0 || amount != 1) { applog(LOG_ERR, "%s detect (%s) send version request failed (%d:%d)", - modminer->drv->dname, devpath, amount, err); + modminer->drv->dname, modminer->device_path, amount, err); goto unshin; } if ((err = usb_read_once(modminer, buf, sizeof(buf)-1, &amount, C_GETVERSION)) < 0 || amount < 1) { if (err < 0) applog(LOG_ERR, "%s detect (%s) no version reply (%d)", - modminer->drv->dname, devpath, err); + modminer->drv->dname, modminer->device_path, err); else applog(LOG_ERR, "%s detect (%s) empty version reply (%d)", - modminer->drv->dname, devpath, amount); + modminer->drv->dname, modminer->device_path, amount); applog(LOG_DEBUG, "%s detect (%s) check the firmware", - modminer->drv->dname, devpath); + modminer->drv->dname, modminer->device_path); goto unshin; } buf[amount] = '\0'; devname = strdup(buf); - applog(LOG_DEBUG, "%s (%s) identified as: %s", modminer->drv->dname, devpath, devname); + applog(LOG_DEBUG, "%s (%s) identified as: %s", modminer->drv->dname, modminer->device_path, devname); if ((err = usb_write(modminer, MODMINER_FPGA_COUNT, 1, &amount, C_REQUESTFPGACOUNT) < 0 || amount != 1)) { applog(LOG_ERR, "%s detect (%s) FPGA count request failed (%d:%d)", - modminer->drv->dname, devpath, amount, err); + modminer->drv->dname, modminer->device_path, amount, err); goto unshin; } if ((err = usb_read(modminer, buf, 1, &amount, C_GETFPGACOUNT)) < 0 || amount != 1) { applog(LOG_ERR, "%s detect (%s) no FPGA count reply (%d:%d)", - modminer->drv->dname, devpath, amount, err); + modminer->drv->dname, modminer->device_path, amount, err); goto unshin; } @@ -174,18 +170,18 @@ static bool modminer_detect_one(struct libusb_device *dev, struct usb_find_devic if (buf[0] == 0) { applog(LOG_ERR, "%s detect (%s) zero FPGA count from %s", - modminer->drv->dname, devpath, devname); + modminer->drv->dname, modminer->device_path, devname); goto unshin; } if (buf[0] < 1 || buf[0] > 4) { applog(LOG_ERR, "%s detect (%s) invalid FPGA count (%u) from %s", - modminer->drv->dname, devpath, buf[0], devname); + modminer->drv->dname, modminer->device_path, buf[0], devname); goto unshin; } applog(LOG_DEBUG, "%s (%s) %s has %u FPGAs", - modminer->drv->dname, devpath, devname, buf[0]); + modminer->drv->dname, modminer->device_path, devname, buf[0]); modminer->name = devname; @@ -209,8 +205,6 @@ static bool modminer_detect_one(struct libusb_device *dev, struct usb_find_devic tmp->modminer_mutex = modminer->modminer_mutex; if (!add_cgpu(tmp)) { - free(tmp->device_path); - tmp->device_path = NULL; tmp = usb_free_cgpu(tmp); goto unshin; } diff --git a/usbutils.c b/usbutils.c index 1a0e1182..fd3859d6 100644 --- a/usbutils.c +++ b/usbutils.c @@ -1423,6 +1423,8 @@ struct cgpu_info *usb_free_cgpu(struct cgpu_info *cgpu) if (cgpu->drv->copy) free(cgpu->drv); + free(cgpu->device_path); + free(cgpu); return NULL; @@ -1439,6 +1441,7 @@ static int _usb_init(struct cgpu_info *cgpu, struct libusb_device *dev, struct u const struct libusb_interface_descriptor *idesc; const struct libusb_endpoint_descriptor *epdesc; unsigned char strbuf[STRBUFLEN+1]; + char devpath[32]; char devstr[STRBUFLEN+1]; int err, i, j, k; int bad = USB_INIT_FAIL; @@ -1446,8 +1449,13 @@ static int _usb_init(struct cgpu_info *cgpu, struct libusb_device *dev, struct u cgpu->usbinfo.bus_number = libusb_get_bus_number(dev); cgpu->usbinfo.device_address = libusb_get_device_address(dev); - sprintf(devstr, "- %s device %d:%d", found->name, - cgpu->usbinfo.bus_number, cgpu->usbinfo.device_address); + sprintf(devpath, "%d:%d", + (int)(cgpu->usbinfo.bus_number), + (int)(cgpu->usbinfo.device_address)); + + cgpu->device_path = strdup(devpath); + + sprintf(devstr, "- %s device %s", found->name, devpath); cgusb = calloc(1, sizeof(*cgusb)); if (unlikely(!cgusb)) From 7d35c6921ea1456f0e8e6f560aac41d0ac24ccd1 Mon Sep 17 00:00:00 2001 From: Kano Date: Mon, 10 Jun 2013 18:52:18 +1000 Subject: [PATCH 4/7] ubsutils lock all access to nodev and cgusb --- driver-modminer.c | 4 ++-- usbutils.c | 51 ++++++++++++++++++++++++++++++++++++++++++++++- usbutils.h | 14 ++++++++++++- 3 files changed, 65 insertions(+), 4 deletions(-) diff --git a/driver-modminer.c b/driver-modminer.c index 1519bc9b..581016e9 100644 --- a/driver-modminer.c +++ b/driver-modminer.c @@ -205,7 +205,7 @@ static bool modminer_detect_one(struct libusb_device *dev, struct usb_find_devic tmp->modminer_mutex = modminer->modminer_mutex; if (!add_cgpu(tmp)) { - tmp = usb_free_cgpu(tmp); + tmp = usb_free_cgpu_devlock(tmp, !added); goto unshin; } @@ -228,7 +228,7 @@ shin: modminer->modminer_mutex = NULL; } - modminer = usb_free_cgpu(modminer); + modminer = usb_free_cgpu_devlock(modminer, !added); if (added) return true; diff --git a/usbutils.c b/usbutils.c index fd3859d6..d43e34c3 100644 --- a/usbutils.c +++ b/usbutils.c @@ -1339,6 +1339,10 @@ void usb_uninit(struct cgpu_info *cgpu) cgpu->usbdev = free_cgusb(cgpu->usbdev); } +/* + * N.B. this is always called inside + * wr_lock(cgpu->usbinfo.devlock); + */ static void release_cgpu(struct cgpu_info *cgpu) { struct cg_usb_device *cgusb = cgpu->usbdev; @@ -1399,6 +1403,8 @@ struct cgpu_info *usb_copy_cgpu(struct cgpu_info *orig) copy->usbinfo.nodev = (copy->usbdev != NULL); + copy->usbinfo.devlock = orig->usbinfo.devlock; + return copy; } @@ -1415,16 +1421,25 @@ struct cgpu_info *usb_alloc_cgpu(struct device_drv *drv, int threads) cgpu->usbinfo.nodev = true; + cgpu->usbinfo.devlock = calloc(1, sizeof(*(cgpu->usbinfo.devlock))); + if (unlikely(!cgpu->usbinfo.devlock)) + quit(1, "Failed to calloc devlock for %s in usb_alloc_cgpu", drv->dname); + + rwlock_init(cgpu->usbinfo.devlock); + return cgpu; } -struct cgpu_info *usb_free_cgpu(struct cgpu_info *cgpu) +struct cgpu_info *usb_free_cgpu_devlock(struct cgpu_info *cgpu, bool free_devlock) { if (cgpu->drv->copy) free(cgpu->drv); free(cgpu->device_path); + if (free_devlock) + free(cgpu->usbinfo.devlock); + free(cgpu); return NULL; @@ -1446,6 +1461,8 @@ static int _usb_init(struct cgpu_info *cgpu, struct libusb_device *dev, struct u int err, i, j, k; int bad = USB_INIT_FAIL; + wr_lock(cgpu->usbinfo.devlock); + cgpu->usbinfo.bus_number = libusb_get_bus_number(dev); cgpu->usbinfo.device_address = libusb_get_device_address(dev); @@ -1684,6 +1701,8 @@ static int _usb_init(struct cgpu_info *cgpu, struct libusb_device *dev, struct u cgpu->drv->name = (char *)(found->name); } + wr_unlock(cgpu->usbinfo.devlock); + return USB_INIT_OK; cldame: @@ -1697,6 +1716,8 @@ dame: cgusb = free_cgusb(cgusb); + wr_unlock(cgpu->usbinfo.devlock); + return bad; } @@ -2145,11 +2166,15 @@ int _usb_read(struct cgpu_info *cgpu, int ep, char *buf, size_t bufsiz, int *pro unsigned char usbbuf[USB_MAX_READ+4], *ptr; size_t usbbufread; + wr_lock(cgpu->usbinfo.devlock); + if (cgpu->usbinfo.nodev) { *buf = '\0'; *processed = 0; USB_REJECT(cgpu, MODE_BULK_READ); + wr_unlock(cgpu->usbinfo.devlock); + return LIBUSB_ERROR_NO_DEVICE; } @@ -2252,6 +2277,8 @@ int _usb_read(struct cgpu_info *cgpu, int ep, char *buf, size_t bufsiz, int *pro if (NODEV(err)) release_cgpu(cgpu); + wr_unlock(cgpu->usbinfo.devlock); + return err; } @@ -2367,6 +2394,8 @@ int _usb_read(struct cgpu_info *cgpu, int ep, char *buf, size_t bufsiz, int *pro if (NODEV(err)) release_cgpu(cgpu); + wr_unlock(cgpu->usbinfo.devlock); + return err; } @@ -2382,6 +2411,8 @@ int _usb_write(struct cgpu_info *cgpu, int ep, char *buf, size_t bufsiz, int *pr __maybe_unused bool first = true; int err, sent, tot; + wr_lock(cgpu->usbinfo.devlock); + USBDEBUG("USB debug: _usb_write(%s (nodev=%s),ep=%d,buf='%s',bufsiz=%zu,proc=%p,timeout=%u,cmd=%s)", cgpu->drv->name, bool_str(cgpu->usbinfo.nodev), ep, (char *)str_text(buf), bufsiz, processed, timeout, usb_cmdname(cmd)); *processed = 0; @@ -2389,6 +2420,8 @@ int _usb_write(struct cgpu_info *cgpu, int ep, char *buf, size_t bufsiz, int *pr if (cgpu->usbinfo.nodev) { USB_REJECT(cgpu, MODE_BULK_WRITE); + wr_unlock(cgpu->usbinfo.devlock); + return LIBUSB_ERROR_NO_DEVICE; } @@ -2441,6 +2474,8 @@ int _usb_write(struct cgpu_info *cgpu, int ep, char *buf, size_t bufsiz, int *pr if (NODEV(err)) release_cgpu(cgpu); + wr_unlock(cgpu->usbinfo.devlock); + return err; } @@ -2453,11 +2488,15 @@ int _usb_transfer(struct cgpu_info *cgpu, uint8_t request_type, uint8_t bRequest uint32_t *buf = NULL; int err, i, bufsiz; + wr_lock(cgpu->usbinfo.devlock); + USBDEBUG("USB debug: _usb_transfer(%s (nodev=%s),type=%"PRIu8",req=%"PRIu8",value=%"PRIu16",index=%"PRIu16",siz=%d,timeout=%u,cmd=%s)", cgpu->drv->name, bool_str(cgpu->usbinfo.nodev), request_type, bRequest, wValue, wIndex, siz, timeout, usb_cmdname(cmd)); if (cgpu->usbinfo.nodev) { USB_REJECT(cgpu, MODE_CTRL_WRITE); + wr_unlock(cgpu->usbinfo.devlock); + return LIBUSB_ERROR_NO_DEVICE; } usbdev = cgpu->usbdev; @@ -2494,6 +2533,8 @@ int _usb_transfer(struct cgpu_info *cgpu, uint8_t request_type, uint8_t bRequest if (NODEV(err)) release_cgpu(cgpu); + wr_unlock(cgpu->usbinfo.devlock); + return err; } @@ -2505,11 +2546,15 @@ int _usb_transfer_read(struct cgpu_info *cgpu, uint8_t request_type, uint8_t bRe #endif int err; + wr_lock(cgpu->usbinfo.devlock); + USBDEBUG("USB debug: _usb_transfer_read(%s (nodev=%s),type=%"PRIu8",req=%"PRIu8",value=%"PRIu16",index=%"PRIu16",bufsiz=%d,timeout=%u,cmd=%s)", cgpu->drv->name, bool_str(cgpu->usbinfo.nodev), request_type, bRequest, wValue, wIndex, bufsiz, timeout, usb_cmdname(cmd)); if (cgpu->usbinfo.nodev) { USB_REJECT(cgpu, MODE_CTRL_READ); + wr_unlock(cgpu->usbinfo.devlock); + return LIBUSB_ERROR_NO_DEVICE; } usbdev = cgpu->usbdev; @@ -2534,6 +2579,8 @@ int _usb_transfer_read(struct cgpu_info *cgpu, uint8_t request_type, uint8_t bRe } else if (NODEV(err)) release_cgpu(cgpu); + wr_unlock(cgpu->usbinfo.devlock); + return err; } @@ -2647,7 +2694,9 @@ void usb_cleanup() case DRIVER_MODMINER: case DRIVER_ICARUS: case DRIVER_AVALON: + wr_lock(cgpu->usbinfo.devlock); release_cgpu(cgpu); + wr_unlock(cgpu->usbinfo.devlock); count++; break; default: diff --git a/usbutils.h b/usbutils.h index 96f2b84b..e9c794a8 100644 --- a/usbutils.h +++ b/usbutils.h @@ -188,6 +188,17 @@ struct cg_usb_info { struct timeval last_nodev; uint32_t ioerr_count; uint32_t continuous_ioerr_count; + + /* + * for nodev and cgusb access (read and write) + * it's a pointer so MMQ can have it in multiple devices + * + * N.B. general mining code doesn't need to use the read + * lock for 'nodev' if it calls a usb_read/write/etc function + * that uses the lock - however, all usbutils code MUST use it + * to avoid devices disappearing while in use by multiple threads + */ + pthread_rwlock_t *devlock; }; enum usb_cmds { @@ -262,7 +273,8 @@ 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); struct cgpu_info *usb_copy_cgpu(struct cgpu_info *orig); struct cgpu_info *usb_alloc_cgpu(struct device_drv *drv, int threads); -struct cgpu_info *usb_free_cgpu(struct cgpu_info *cgpu); +struct cgpu_info *usb_free_cgpu_devlock(struct cgpu_info *cgpu, bool free_devlock); +#define usb_free_cgpu(cgpu) usb_free_cgpu_devlock(cgpu, true) 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 *)); From 92fee70205a6d6007f132b10c72eeda15324e8ad Mon Sep 17 00:00:00 2001 From: Kano Date: Mon, 10 Jun 2013 21:40:21 +1000 Subject: [PATCH 5/7] MMQ fix nodev failure caused by changes --- driver-modminer.c | 2 +- usbutils.c | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/driver-modminer.c b/driver-modminer.c index 581016e9..67a5b957 100644 --- a/driver-modminer.c +++ b/driver-modminer.c @@ -214,7 +214,7 @@ static bool modminer_detect_one(struct libusb_device *dev, struct usb_find_devic added = true; } - modminer = usb_free_cgpu(modminer); + modminer = usb_free_cgpu_devlock(modminer, !added); return true; diff --git a/usbutils.c b/usbutils.c index d43e34c3..f036ea48 100644 --- a/usbutils.c +++ b/usbutils.c @@ -1401,7 +1401,7 @@ struct cgpu_info *usb_copy_cgpu(struct cgpu_info *orig) memcpy(&(copy->usbinfo), &(orig->usbinfo), sizeof(copy->usbinfo)); - copy->usbinfo.nodev = (copy->usbdev != NULL); + copy->usbinfo.nodev = (copy->usbdev == NULL); copy->usbinfo.devlock = orig->usbinfo.devlock; From 91034145aeaf8277593770f2dc72ec4a4b49e4b2 Mon Sep 17 00:00:00 2001 From: Kano Date: Wed, 12 Jun 2013 17:03:48 +1000 Subject: [PATCH 6/7] usbutils avoid leaving devlock locked when thread cancelled --- usbutils.c | 47 ++++++++++++++++++++++++++++++----------------- 1 file changed, 30 insertions(+), 17 deletions(-) diff --git a/usbutils.c b/usbutils.c index f036ea48..c15260ae 100644 --- a/usbutils.c +++ b/usbutils.c @@ -1341,7 +1341,7 @@ void usb_uninit(struct cgpu_info *cgpu) /* * N.B. this is always called inside - * wr_lock(cgpu->usbinfo.devlock); + * DEVLOCK(cgpu); */ static void release_cgpu(struct cgpu_info *cgpu) { @@ -1449,6 +1449,19 @@ struct cgpu_info *usb_free_cgpu_devlock(struct cgpu_info *cgpu, bool free_devloc #define USB_INIT_OK 1 #define USB_INIT_IGNORE 2 +/* + * WARNING - these assume DEVLOCK(cgpu) is called first and + * DEVUNLOCK(cgpu) in called in the same function inside the same brace level + * You must call DEVUNLOCK(cgpu) before exiting the function or it will leave + * the thread Cancelability unrestored + */ +#define DEVLOCK(cgpu) int _pth_state; \ + pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED, &_pth_state); \ + wr_lock(cgpu->usbinfo.devlock); + +#define DEVUNLOCK(cgpu) wr_unlock(cgpu->usbinfo.devlock); \ + pthread_setcanceltype(_pth_state, NULL); + static int _usb_init(struct cgpu_info *cgpu, struct libusb_device *dev, struct usb_find_devices *found) { struct cg_usb_device *cgusb = NULL; @@ -1461,7 +1474,7 @@ static int _usb_init(struct cgpu_info *cgpu, struct libusb_device *dev, struct u int err, i, j, k; int bad = USB_INIT_FAIL; - wr_lock(cgpu->usbinfo.devlock); + DEVLOCK(cgpu); cgpu->usbinfo.bus_number = libusb_get_bus_number(dev); cgpu->usbinfo.device_address = libusb_get_device_address(dev); @@ -1701,7 +1714,7 @@ static int _usb_init(struct cgpu_info *cgpu, struct libusb_device *dev, struct u cgpu->drv->name = (char *)(found->name); } - wr_unlock(cgpu->usbinfo.devlock); + DEVUNLOCK(cgpu); return USB_INIT_OK; @@ -1716,7 +1729,7 @@ dame: cgusb = free_cgusb(cgusb); - wr_unlock(cgpu->usbinfo.devlock); + DEVUNLOCK(cgpu); return bad; } @@ -2166,14 +2179,14 @@ int _usb_read(struct cgpu_info *cgpu, int ep, char *buf, size_t bufsiz, int *pro unsigned char usbbuf[USB_MAX_READ+4], *ptr; size_t usbbufread; - wr_lock(cgpu->usbinfo.devlock); + DEVLOCK(cgpu); if (cgpu->usbinfo.nodev) { *buf = '\0'; *processed = 0; USB_REJECT(cgpu, MODE_BULK_READ); - wr_unlock(cgpu->usbinfo.devlock); + DEVUNLOCK(cgpu); return LIBUSB_ERROR_NO_DEVICE; } @@ -2277,7 +2290,7 @@ int _usb_read(struct cgpu_info *cgpu, int ep, char *buf, size_t bufsiz, int *pro if (NODEV(err)) release_cgpu(cgpu); - wr_unlock(cgpu->usbinfo.devlock); + DEVUNLOCK(cgpu); return err; } @@ -2394,7 +2407,7 @@ int _usb_read(struct cgpu_info *cgpu, int ep, char *buf, size_t bufsiz, int *pro if (NODEV(err)) release_cgpu(cgpu); - wr_unlock(cgpu->usbinfo.devlock); + DEVUNLOCK(cgpu); return err; } @@ -2411,7 +2424,7 @@ int _usb_write(struct cgpu_info *cgpu, int ep, char *buf, size_t bufsiz, int *pr __maybe_unused bool first = true; int err, sent, tot; - wr_lock(cgpu->usbinfo.devlock); + DEVLOCK(cgpu); USBDEBUG("USB debug: _usb_write(%s (nodev=%s),ep=%d,buf='%s',bufsiz=%zu,proc=%p,timeout=%u,cmd=%s)", cgpu->drv->name, bool_str(cgpu->usbinfo.nodev), ep, (char *)str_text(buf), bufsiz, processed, timeout, usb_cmdname(cmd)); @@ -2420,7 +2433,7 @@ int _usb_write(struct cgpu_info *cgpu, int ep, char *buf, size_t bufsiz, int *pr if (cgpu->usbinfo.nodev) { USB_REJECT(cgpu, MODE_BULK_WRITE); - wr_unlock(cgpu->usbinfo.devlock); + DEVUNLOCK(cgpu); return LIBUSB_ERROR_NO_DEVICE; } @@ -2474,7 +2487,7 @@ int _usb_write(struct cgpu_info *cgpu, int ep, char *buf, size_t bufsiz, int *pr if (NODEV(err)) release_cgpu(cgpu); - wr_unlock(cgpu->usbinfo.devlock); + DEVUNLOCK(cgpu); return err; } @@ -2488,14 +2501,14 @@ int _usb_transfer(struct cgpu_info *cgpu, uint8_t request_type, uint8_t bRequest uint32_t *buf = NULL; int err, i, bufsiz; - wr_lock(cgpu->usbinfo.devlock); + DEVLOCK(cgpu); USBDEBUG("USB debug: _usb_transfer(%s (nodev=%s),type=%"PRIu8",req=%"PRIu8",value=%"PRIu16",index=%"PRIu16",siz=%d,timeout=%u,cmd=%s)", cgpu->drv->name, bool_str(cgpu->usbinfo.nodev), request_type, bRequest, wValue, wIndex, siz, timeout, usb_cmdname(cmd)); if (cgpu->usbinfo.nodev) { USB_REJECT(cgpu, MODE_CTRL_WRITE); - wr_unlock(cgpu->usbinfo.devlock); + DEVUNLOCK(cgpu); return LIBUSB_ERROR_NO_DEVICE; } @@ -2533,7 +2546,7 @@ int _usb_transfer(struct cgpu_info *cgpu, uint8_t request_type, uint8_t bRequest if (NODEV(err)) release_cgpu(cgpu); - wr_unlock(cgpu->usbinfo.devlock); + DEVUNLOCK(cgpu); return err; } @@ -2546,14 +2559,14 @@ int _usb_transfer_read(struct cgpu_info *cgpu, uint8_t request_type, uint8_t bRe #endif int err; - wr_lock(cgpu->usbinfo.devlock); + DEVLOCK(cgpu); USBDEBUG("USB debug: _usb_transfer_read(%s (nodev=%s),type=%"PRIu8",req=%"PRIu8",value=%"PRIu16",index=%"PRIu16",bufsiz=%d,timeout=%u,cmd=%s)", cgpu->drv->name, bool_str(cgpu->usbinfo.nodev), request_type, bRequest, wValue, wIndex, bufsiz, timeout, usb_cmdname(cmd)); if (cgpu->usbinfo.nodev) { USB_REJECT(cgpu, MODE_CTRL_READ); - wr_unlock(cgpu->usbinfo.devlock); + DEVUNLOCK(cgpu); return LIBUSB_ERROR_NO_DEVICE; } @@ -2579,7 +2592,7 @@ int _usb_transfer_read(struct cgpu_info *cgpu, uint8_t request_type, uint8_t bRe } else if (NODEV(err)) release_cgpu(cgpu); - wr_unlock(cgpu->usbinfo.devlock); + DEVUNLOCK(cgpu); return err; } From 65e21f18cb25542afe9b976aee1d500eca416be2 Mon Sep 17 00:00:00 2001 From: Kano Date: Wed, 12 Jun 2013 22:05:35 +1000 Subject: [PATCH 7/7] Icarus driver elaspsed timeout shouldn't be just USB I/O --- driver-icarus.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/driver-icarus.c b/driver-icarus.c index 86a781ac..8983e289 100644 --- a/driver-icarus.c +++ b/driver-icarus.c @@ -445,7 +445,7 @@ static int icarus_get_nonce(struct cgpu_info *icarus, unsigned char *buf, struct if (amt >= read_amount) return ICA_NONCE_OK; - rc += SECTOMS(tdiff(&read_finish, &read_start)); + rc = SECTOMS(tdiff(&read_finish, tv_start)); if (rc >= read_time) { if (amt > 0) applog(LOG_DEBUG, "Icarus Read: Timeout reading for %d ms", rc);