Browse Source

USB control creation and free of cgpu

nfactor-troky
Kano 12 years ago
parent
commit
9287ee90f6
  1. 38
      driver-avalon.c
  2. 10
      driver-bflsc.c
  3. 8
      driver-bitforce.c
  4. 9
      driver-icarus.c
  5. 35
      driver-modminer.c
  6. 77
      usbutils.c
  7. 5
      usbutils.h

38
driver-avalon.c

@ -578,17 +578,13 @@ static bool avalon_detect_one(libusb_device *dev, struct usb_find_devices *found
bool configured; bool configured;
int ret; int ret;
avalon = calloc(1, sizeof(struct cgpu_info)); avalon = usb_alloc_cgpu(&avalon_drv, AVALON_MINER_THREADS);
if (unlikely(!avalon))
quit(1, "Failed to calloc avalon in avalon_detect_one");;
avalon->drv = &avalon_drv;
avalon->threads = AVALON_MINER_THREADS;
configured = get_options(this_option_offset, &baud, &miner_count, configured = get_options(this_option_offset, &baud, &miner_count,
&asic_count, &timeout, &frequency); &asic_count, &timeout, &frequency);
if (!usb_init(avalon, dev, found)) if (!usb_init(avalon, dev, found))
return false; goto shin;
/* Even though this is an FTDI type chip, we want to do the parsing /* Even though this is an FTDI type chip, we want to do the parsing
* all ourselves so set it to std usb type */ * 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_initialise(avalon);
avalon->device_path = strdup(devpath); avalon->device_path = strdup(devpath);
add_cgpu(avalon);
avalon->device_data = calloc(sizeof(struct avalon_info), 1); avalon->device_data = calloc(sizeof(struct avalon_info), 1);
if (unlikely(!(avalon->device_data))) 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; info = avalon->device_data;
if (configured) { if (configured) {
@ -635,10 +630,13 @@ static bool avalon_detect_one(libusb_device *dev, struct usb_find_devices *found
info->temp_old = 0; info->temp_old = 0;
ret = avalon_reset(avalon, true); ret = avalon_reset(avalon, true);
if (ret && !configured) { if (ret && !configured)
usb_uninit(avalon); goto unshin;
return false;
} if (!add_cgpu(avalon))
goto unshin;
update_usb_stats(avalon);
avalon_idle(avalon, info); avalon_idle(avalon, info);
@ -648,6 +646,22 @@ static bool avalon_detect_one(libusb_device *dev, struct usb_find_devices *found
info->frequency); info->frequency);
return true; 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) static void avalon_detect(void)

10
driver-bflsc.c

@ -810,13 +810,7 @@ static bool bflsc_detect_one(struct libusb_device *dev, struct usb_find_devices
char *newname; char *newname;
uint16_t latency; uint16_t latency;
struct cgpu_info *bflsc = calloc(1, sizeof(*bflsc)); struct cgpu_info *bflsc = usb_alloc_cgpu(&bflsc_drv, 1);
if (unlikely(!bflsc))
quit(1, "Failed to calloc bflsc in bflsc_detect_one");
bflsc->drv = &bflsc_drv;
bflsc->deven = DEV_ENABLED;
bflsc->threads = 1;
sc_info = calloc(1, sizeof(*sc_info)); sc_info = calloc(1, sizeof(*sc_info));
if (unlikely(!sc_info)) if (unlikely(!sc_info))
@ -980,7 +974,7 @@ shin:
if (bflsc->drv->copy) if (bflsc->drv->copy)
free(bflsc->drv); free(bflsc->drv);
free(bflsc); bflsc = usb_free_cgpu(bflsc);
return false; return false;
} }

8
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; int init_sleep, init_count;
bool ident_first; bool ident_first;
struct cgpu_info *bitforce = NULL; struct cgpu_info *bitforce = usb_alloc_cgpu(&bitforce_drv, 1);
bitforce = calloc(1, sizeof(*bitforce));
bitforce->drv = &bitforce_drv;
bitforce->deven = DEV_ENABLED;
bitforce->threads = 1;
if (!usb_init(bitforce, dev, found)) if (!usb_init(bitforce, dev, found))
goto shin; goto shin;
@ -298,7 +294,7 @@ shin:
if (bitforce->drv->copy) if (bitforce->drv->copy)
free(bitforce->drv); free(bitforce->drv);
free(bitforce); bitforce = usb_free_cgpu(bitforce);
return false; return false;
} }

9
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; int ret, err, amount, tries;
bool ok; bool ok;
icarus = calloc(1, sizeof(struct cgpu_info)); icarus = usb_alloc_cgpu(&icarus_drv, 1);
if (unlikely(!icarus))
quit(1, "Failed to calloc icarus in icarus_detect_one");
icarus->drv = &icarus_drv;
icarus->deven = DEV_ENABLED;
icarus->threads = 1;
if (!usb_init(icarus, dev, found)) if (!usb_init(icarus, dev, found))
goto shin; goto shin;
@ -851,7 +846,7 @@ unshin:
shin: shin:
free(icarus); icarus = usb_free_cgpu(icarus);
return false; return false;
} }

35
driver-modminer.c

@ -119,9 +119,8 @@ static bool modminer_detect_one(struct libusb_device *dev, struct usb_find_devic
int err, i, amount; int err, i, amount;
bool added = false; bool added = false;
struct cgpu_info *modminer = NULL; struct cgpu_info *modminer = usb_alloc_cgpu(&modminer_drv, 1);
modminer = calloc(1, sizeof(*modminer));
modminer->drv = &modminer_drv;
modminer->modminer_mutex = calloc(1, sizeof(*(modminer->modminer_mutex))); modminer->modminer_mutex = calloc(1, sizeof(*(modminer->modminer_mutex)));
mutex_init(modminer->modminer_mutex); mutex_init(modminer->modminer_mutex);
modminer->fpgaid = (char)0; 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 // TODO: test with 1 board missing in the middle and each end
// to see how that affects the sequence numbers // to see how that affects the sequence numbers
for (i = 0; i < buf[0]; i++) { for (i = 0; i < buf[0]; i++) {
struct cgpu_info *tmp = calloc(1, sizeof(*tmp)); struct cgpu_info *tmp = usb_copy_cgpu(modminer);
tmp->drv = copy_drv(modminer->drv);
tmp->name = devname;
sprintf(devpath, "%d:%d:%d", sprintf(devpath, "%d:%d:%d",
(int)(modminer->usbinfo.bus_number), (int)(modminer->usbinfo.bus_number),
@ -204,22 +200,17 @@ static bool modminer_detect_one(struct libusb_device *dev, struct usb_find_devic
i); i);
tmp->device_path = strdup(devpath); 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 // Only the first copy gets the already used stats
if (!added) if (added)
tmp->usbinfo.usbstat = modminer->usbinfo.usbstat; tmp->usbinfo.usbstat = USB_NOSTAT;
tmp->fpgaid = (char)i; tmp->fpgaid = (char)i;
tmp->modminer_mutex = modminer->modminer_mutex; tmp->modminer_mutex = modminer->modminer_mutex;
tmp->deven = DEV_ENABLED;
tmp->threads = 1;
if (!add_cgpu(tmp)) { if (!add_cgpu(tmp)) {
free(tmp->device_path); free(tmp->device_path);
if (tmp->drv->copy) tmp = usb_free_cgpu(tmp);
free(tmp->drv);
free(tmp);
goto unshin; goto unshin;
} }
@ -228,10 +219,7 @@ static bool modminer_detect_one(struct libusb_device *dev, struct usb_find_devic
added = true; added = true;
} }
if (modminer->drv->copy) modminer = usb_free_cgpu(modminer);
free(modminer->drv);
free(modminer);
return true; return true;
@ -243,10 +231,7 @@ shin:
if (!added) if (!added)
free(modminer->modminer_mutex); free(modminer->modminer_mutex);
if (modminer->drv->copy) modminer = usb_free_cgpu(modminer);
free(modminer->drv);
free(modminer);
if (added) if (added)
return true; return true;

77
usbutils.c

@ -451,7 +451,7 @@ struct cg_usb_stats {
#define SEQ1 1 #define SEQ1 1
static struct cg_usb_stats *usb_stats = NULL; 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) \ #define USB_STATS(sgpu, sta, fin, err, mode, cmd, seq) \
stats(cgpu, 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); 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_FAIL 0
#define USB_INIT_OK 1 #define USB_INIT_OK 1
#define USB_INIT_IGNORE 2 #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); cgusb->manuf_string, cgusb->serial_string);
cgpu->usbdev = cgusb; cgpu->usbdev = cgusb;
cgpu->usbinfo.nodev = false;
libusb_free_config_descriptor(config); libusb_free_config_descriptor(config);
@ -1876,7 +1925,7 @@ struct api_data *api_usb_stats(__maybe_unused int *count)
int cmdseq; int cmdseq;
char modes_s[32]; char modes_s[32];
if (next_stat == 0) if (next_stat == USB_NOSTAT)
return NULL; return NULL;
while (*count < next_stat * C_MAX * 2) { while (*count < next_stat * C_MAX * 2) {
@ -1949,21 +1998,25 @@ static void newstats(struct cgpu_info *cgpu)
int i; int i;
mutex_lock(&cgusb_lock); 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)) 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].name = cgpu->drv->name;
usb_stats[next_stat-1].device_id = -1; usb_stats[next_stat].device_id = -1;
usb_stats[next_stat-1].details = calloc(1, sizeof(struct cg_usb_stats_details) * C_MAX * 2); usb_stats[next_stat].details = calloc(1, sizeof(struct cg_usb_stats_details) * C_MAX * 2);
if (unlikely(!usb_stats[next_stat-1].details)) if (unlikely(!usb_stats[next_stat].details))
quit(1, "USB failed to calloc details for %d", next_stat); quit(1, "USB failed to calloc details for %d", next_stat+1);
for (i = 1; i < C_MAX * 2; i += 2) 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 #endif

5
usbutils.h

@ -177,6 +177,8 @@ struct cg_usb_device {
uint32_t bufamt; uint32_t bufamt;
}; };
#define USB_NOSTAT 0
struct cg_usb_info { struct cg_usb_info {
uint8_t bus_number; uint8_t bus_number;
uint8_t device_address; uint8_t device_address;
@ -258,6 +260,9 @@ struct cgpu_info;
void usb_all(int level); void usb_all(int level);
const char *usb_cmdname(enum usb_cmds cmd); 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); 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); void usb_uninit(struct cgpu_info *cgpu);
bool usb_init(struct cgpu_info *cgpu, struct libusb_device *dev, struct usb_find_devices *found); 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, bool (*device_detect)(struct libusb_device *, struct usb_find_devices *));

Loading…
Cancel
Save