Skip to content

Commit

Permalink
Always use a usb read buffer instead of having to explicitly enable it.
Browse files Browse the repository at this point in the history
  • Loading branch information
ckolivas committed Nov 3, 2013
1 parent 6c2a8d8 commit 8fb7a0d
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 100 deletions.
1 change: 0 additions & 1 deletion driver-avalon.c
Original file line number Diff line number Diff line change
Expand Up @@ -780,7 +780,6 @@ static bool avalon_detect_one(libusb_device *dev, struct usb_find_devices *found
/* Even though this is an FTDI type chip, we want to do the parsing
* all ourselves so set it to std usb type */
avalon->usbdev->usb_type = USB_TYPE_STD;
usb_buffer_enable(avalon);

/* We have a real Avalon! */
avalon_initialise(avalon);
Expand Down
2 changes: 0 additions & 2 deletions driver-bflsc.c
Original file line number Diff line number Diff line change
Expand Up @@ -888,8 +888,6 @@ static bool bflsc_detect_one(struct libusb_device *dev, struct usb_find_devices
mutex_init(&bflsc->device_mutex);
rwlock_init(&sc_info->stat_lock);

usb_buffer_enable(bflsc);

return true;

unshin:
Expand Down
2 changes: 0 additions & 2 deletions driver-bitfury.c
Original file line number Diff line number Diff line change
Expand Up @@ -146,8 +146,6 @@ static bool bitfury_detect_one(struct libusb_device *dev, struct usb_find_device
* hashrate to adapt quickly on starting. */
info->total_nonces = 1;

usb_buffer_enable(bitfury);

if (!bitfury_open(bitfury))
goto out_close;

Expand Down
2 changes: 0 additions & 2 deletions driver-hashfast.c
Original file line number Diff line number Diff line change
Expand Up @@ -347,7 +347,6 @@ static bool hfa_initialise(struct cgpu_info *hashfast)
if (hashfast->usbinfo.nodev)
return false;

usb_buffer_enable(hashfast);
hfa_clear_readbuf(hashfast);

err = usb_transfer(hashfast, 0, 9, 1, 0, C_ATMEL_RESET);
Expand Down Expand Up @@ -916,7 +915,6 @@ static void hfa_shutdown(struct thr_info *thr)
pthread_join(info->read_thr, NULL);
hfa_free_all_work(info);
hfa_clear_readbuf(hashfast);
usb_buffer_disable(hashfast);
free(info->works);
free(info->die_statistics);
free(info->die_status);
Expand Down
2 changes: 0 additions & 2 deletions driver-icarus.c
Original file line number Diff line number Diff line change
Expand Up @@ -822,8 +822,6 @@ static bool icarus_detect_one(struct libusb_device *dev, struct usb_find_devices
if (!usb_init(icarus, dev, found))
goto shin;

usb_buffer_enable(icarus);

get_options(this_option_offset, icarus, &baud, &work_division, &fpga_count);

hex2bin((void *)(&workdata), golden_ob, sizeof(workdata));
Expand Down
121 changes: 35 additions & 86 deletions usbutils.c
Original file line number Diff line number Diff line change
Expand Up @@ -1309,9 +1309,6 @@ static struct cg_usb_device *free_cgusb(struct cg_usb_device *cgusb)

free(cgusb->found);

if (cgusb->buffer)
free(cgusb->buffer);

free(cgusb);

return NULL;
Expand Down Expand Up @@ -2503,6 +2500,7 @@ int _usb_read(struct cgpu_info *cgpu, int intinfo, int epinfo, char *buf, size_t
double max, done;
int bufleft, err, got, tot, pstate;
bool first = true;
bool dobuffer;
char *search;
int endlen;
unsigned char *ptr, *usbbuf = cgpu->usbinfo.bulkbuf;
Expand Down Expand Up @@ -2531,17 +2529,12 @@ int _usb_read(struct cgpu_info *cgpu, int intinfo, int epinfo, char *buf, size_t
timeout = usbdev->found->timeout;

if (end == NULL) {
if (usbdev->buffer && usbdev->bufamt) {
tot = usbdev->bufamt;
bufleft = bufsiz - tot;
tot = usbdev->bufamt;
bufleft = bufsiz - tot;
if (tot)
memcpy(usbbuf, usbdev->buffer, tot);
ptr = usbbuf + tot;
usbdev->bufamt = 0;
} else {
tot = 0;
bufleft = bufsiz;
ptr = usbbuf;
}
ptr = usbbuf + tot;
usbdev->bufamt = 0;

err = LIBUSB_SUCCESS;
initial_timeout = timeout;
Expand Down Expand Up @@ -2592,7 +2585,7 @@ int _usb_read(struct cgpu_info *cgpu, int intinfo, int epinfo, char *buf, size_t
}

// N.B. usbdev->buffer was emptied before the while() loop
if (usbdev->buffer && tot > (int)bufsiz) {
if (tot > (int)bufsiz) {
usbdev->bufamt = tot - bufsiz;
memcpy(usbdev->buffer, usbbuf + bufsiz, usbdev->bufamt);
tot -= usbdev->bufamt;
Expand All @@ -2607,23 +2600,19 @@ int _usb_read(struct cgpu_info *cgpu, int intinfo, int epinfo, char *buf, size_t
goto out_unlock;
}

if (usbdev->buffer && usbdev->bufamt) {
tot = usbdev->bufamt;
bufleft = bufsiz - tot;
tot = usbdev->bufamt;
bufleft = bufsiz - tot;
if (tot)
memcpy(usbbuf, usbdev->buffer, tot);
ptr = usbbuf + tot;
usbdev->bufamt = 0;
} else {
tot = 0;
bufleft = bufsiz;
ptr = usbbuf;
}
ptr = usbbuf + tot;
usbdev->bufamt = 0;

endlen = strlen(end);
err = LIBUSB_SUCCESS;
initial_timeout = timeout;
max = ((double)timeout) / 1000.0;
cgtime(&read_start);

while (bufleft > 0) {
got = 0;
err = usb_bulk_transfer(usbdev->handle, intinfo, epinfo,
Expand Down Expand Up @@ -2670,38 +2659,36 @@ int _usb_read(struct cgpu_info *cgpu, int intinfo, int epinfo, char *buf, size_t
break;
}

if (usbdev->buffer) {
bool dobuffer = false;
dobuffer = false;

if ((search = find_end(usbbuf, usbbuf, tot, tot, (char *)end, endlen, true))) {
// end finishes after bufsiz
if ((search + endlen - (char *)usbbuf) > (int)bufsiz) {
usbdev->bufamt = tot - bufsiz;
dobuffer = true;
} else {
// extra data after end
if (*(search + endlen)) {
usbdev->bufamt = tot - (search + endlen - (char *)usbbuf);
dobuffer = true;
}
}
if ((search = find_end(usbbuf, usbbuf, tot, tot, (char *)end, endlen, true))) {
// end finishes after bufsiz
if ((search + endlen - (char *)usbbuf) > (int)bufsiz) {
usbdev->bufamt = tot - bufsiz;
dobuffer = true;
} else {
// no end, but still bigger than bufsiz
if (tot > (int)bufsiz) {
usbdev->bufamt = tot - bufsiz;
// extra data after end
if (*(search + endlen)) {
usbdev->bufamt = tot - (search + endlen - (char *)usbbuf);
dobuffer = true;
}
}

if (dobuffer) {
tot -= usbdev->bufamt;
memcpy(usbdev->buffer, usbbuf + tot, usbdev->bufamt);
usbbuf[tot] = '\0';
applog(LOG_DEBUG, "USB: %s%i read2 buffering %d extra bytes",
cgpu->drv->name, cgpu->device_id, usbdev->bufamt);
} else {
// no end, but still bigger than bufsiz
if (tot > (int)bufsiz) {
usbdev->bufamt = tot - bufsiz;
dobuffer = true;
}
}

if (dobuffer) {
tot -= usbdev->bufamt;
memcpy(usbdev->buffer, usbbuf + tot, usbdev->bufamt);
usbbuf[tot] = '\0';
applog(LOG_DEBUG, "USB: %s%i read2 buffering %d extra bytes",
cgpu->drv->name, cgpu->device_id, usbdev->bufamt);
}

*processed = tot;
memcpy((char *)buf, (const char *)usbbuf, (tot < (int)bufsiz) ? tot + 1 : (int)bufsiz);

Expand Down Expand Up @@ -3031,44 +3018,6 @@ int _usb_ftdi_set_latency(struct cgpu_info *cgpu, int intinfo)
return err;
}

void usb_buffer_enable(struct cgpu_info *cgpu)
{
struct cg_usb_device *cgusb;
int pstate;

DEVWLOCK(cgpu, pstate);

cgusb = cgpu->usbdev;
if (cgusb && !cgusb->buffer) {
cgusb->bufamt = 0;
cgusb->buffer = malloc(USB_MAX_READ+1);
if (!cgusb->buffer)
quit(1, "Failed to malloc buffer for USB %s%i",
cgpu->drv->name, cgpu->device_id);
cgusb->bufsiz = USB_MAX_READ;
}

DEVWUNLOCK(cgpu, pstate);
}

void usb_buffer_disable(struct cgpu_info *cgpu)
{
struct cg_usb_device *cgusb;
int pstate;

DEVWLOCK(cgpu, pstate);

cgusb = cgpu->usbdev;
if (cgusb && cgusb->buffer) {
cgusb->bufamt = 0;
cgusb->bufsiz = 0;
free(cgusb->buffer);
cgusb->buffer = NULL;
}

DEVWUNLOCK(cgpu, pstate);
}

void usb_buffer_clear(struct cgpu_info *cgpu)
{
int pstate;
Expand Down
8 changes: 3 additions & 5 deletions usbutils.h
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,8 @@ enum usb_types {
USB_TYPE_FTDI
};

#define USB_MAX_READ 8192

struct cg_usb_device {
struct usb_find_devices *found;
libusb_device_handle *handle;
Expand All @@ -196,7 +198,7 @@ struct cg_usb_device {
char *serial_string;
unsigned char fwVersion; // ??
unsigned char interfaceVersion; // ??
char *buffer;
char buffer[USB_MAX_READ];
uint32_t bufsiz;
uint32_t bufamt;
cgtimer_t cgt_last_write;
Expand All @@ -205,8 +207,6 @@ struct cg_usb_device {

#define USB_NOSTAT 0

#define USB_MAX_READ 8192

#define USB_TMO_0 50
#define USB_TMO_1 100
#define USB_TMO_2 500
Expand Down Expand Up @@ -394,8 +394,6 @@ int _usb_transfer_read(struct cgpu_info *cgpu, uint8_t request_type, uint8_t bRe
int usb_ftdi_cts(struct cgpu_info *cgpu);
int _usb_ftdi_set_latency(struct cgpu_info *cgpu, int intinfo);
#define usb_ftdi_set_latency(_cgpu) _usb_ftdi_set_latency(_cgpu, DEFAULT_INTINFO)
void usb_buffer_enable(struct cgpu_info *cgpu);
void usb_buffer_disable(struct cgpu_info *cgpu);
void usb_buffer_clear(struct cgpu_info *cgpu);
uint32_t usb_buffer_size(struct cgpu_info *cgpu);
void usb_set_cps(struct cgpu_info *cgpu, int cps);
Expand Down

0 comments on commit 8fb7a0d

Please sign in to comment.