Browse Source

Merge branch 'icarushit'

nfactor-troky
Con Kolivas 11 years ago
parent
commit
38164d637d
  1. 2
      driver-avalon.c
  2. 1
      driver-avalon.h
  3. 2
      driver-bflsc.c
  4. 2
      driver-bitfury.c
  5. 2
      driver-hashfast.c
  6. 12
      driver-icarus.c
  7. 330
      usbutils.c
  8. 15
      usbutils.h

2
driver-avalon.c

@ -780,8 +780,6 @@ static bool avalon_detect_one(libusb_device *dev, struct usb_find_devices *found @@ -780,8 +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_set_pps(avalon, AVALON_USB_PACKETSIZE);
usb_buffer_enable(avalon);
/* We have a real Avalon! */
avalon_initialise(avalon);

1
driver-avalon.h

@ -64,7 +64,6 @@ @@ -64,7 +64,6 @@
#define AVALON_AUTO_CYCLE 1024
#define AVALON_FTDI_READSIZE 510
#define AVALON_USB_PACKETSIZE 512
#define AVALON_READBUF_SIZE 8192
#define AVALON_RESET_TIMEOUT 100
#define AVALON_READ_TIMEOUT 18 /* Enough to only half fill the buffer */

2
driver-bflsc.c

@ -888,8 +888,6 @@ reinit: @@ -888,8 +888,6 @@ reinit:
mutex_init(&bflsc->device_mutex);
rwlock_init(&sc_info->stat_lock);
usb_buffer_enable(bflsc);
return true;
unshin:

2
driver-bitfury.c

@ -146,8 +146,6 @@ static bool bitfury_detect_one(struct libusb_device *dev, struct usb_find_device @@ -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;

2
driver-hashfast.c

@ -347,7 +347,6 @@ static bool hfa_initialise(struct cgpu_info *hashfast) @@ -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);
@ -916,7 +915,6 @@ static void hfa_shutdown(struct thr_info *thr) @@ -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);

12
driver-icarus.c

@ -55,10 +55,6 @@ @@ -55,10 +55,6 @@
// The size of a successful nonce read
#define ICARUS_READ_SIZE 4
#define AMU_PREF_PACKET 256
#define BLT_PREF_PACKET 512
#define ICA_PREF_PACKET 256
// Ensure the sizes are correct for the Serial read
#if (ICARUS_READ_SIZE != 4)
#error ICARUS_READ_SIZE must be 4
@ -329,8 +325,6 @@ static void icarus_initialise(struct cgpu_info *icarus, int baud) @@ -329,8 +325,6 @@ static void icarus_initialise(struct cgpu_info *icarus, int baud)
case IDENT_LLT:
case IDENT_CMR1:
case IDENT_CMR2:
usb_set_pps(icarus, BLT_PREF_PACKET);
// Reset
transfer(icarus, FTDI_TYPE_OUT, FTDI_REQUEST_RESET, FTDI_VALUE_RESET,
interface, C_RESET);
@ -404,8 +398,6 @@ static void icarus_initialise(struct cgpu_info *icarus, int baud) @@ -404,8 +398,6 @@ static void icarus_initialise(struct cgpu_info *icarus, int baud)
interface, C_PURGERX);
break;
case IDENT_ICA:
usb_set_pps(icarus, ICA_PREF_PACKET);
// Set Data Control
transfer(icarus, PL2303_CTRL_OUT, PL2303_REQUEST_CTRL, PL2303_VALUE_CTRL,
interface, C_SETDATA);
@ -426,8 +418,6 @@ static void icarus_initialise(struct cgpu_info *icarus, int baud) @@ -426,8 +418,6 @@ static void icarus_initialise(struct cgpu_info *icarus, int baud)
interface, C_VENDOR);
break;
case IDENT_AMU:
usb_set_pps(icarus, AMU_PREF_PACKET);
// Enable the UART
transfer(icarus, CP210X_TYPE_OUT, CP210X_REQUEST_IFC_ENABLE,
CP210X_VALUE_UART_ENABLE,
@ -832,8 +822,6 @@ static bool icarus_detect_one(struct libusb_device *dev, struct usb_find_devices @@ -832,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));

330
usbutils.c

@ -103,8 +103,8 @@ static struct list_head ut_list; @@ -103,8 +103,8 @@ static struct list_head ut_list;
#ifdef USE_BFLSC
// N.B. transfer size is 512 with USB2.0, but only 64 with USB1.1
static struct usb_epinfo bas_epinfos[] = {
{ LIBUSB_TRANSFER_TYPE_BULK, 64, EPI(1), 0, 0, 0 },
{ LIBUSB_TRANSFER_TYPE_BULK, 64, EPO(2), 0, 0, 0 }
{ LIBUSB_TRANSFER_TYPE_BULK, 64, EPI(1), 0, 0 },
{ LIBUSB_TRANSFER_TYPE_BULK, 64, EPO(2), 0, 0 }
};
static struct usb_intinfo bas_ints[] = {
@ -115,8 +115,8 @@ static struct usb_intinfo bas_ints[] = { @@ -115,8 +115,8 @@ static struct usb_intinfo bas_ints[] = {
#ifdef USE_BITFORCE
// N.B. transfer size is 512 with USB2.0, but only 64 with USB1.1
static struct usb_epinfo bfl_epinfos[] = {
{ LIBUSB_TRANSFER_TYPE_BULK, 64, EPI(1), 0, 0, 0 },
{ LIBUSB_TRANSFER_TYPE_BULK, 64, EPO(2), 0, 0, 0 }
{ LIBUSB_TRANSFER_TYPE_BULK, 64, EPI(1), 0, 0 },
{ LIBUSB_TRANSFER_TYPE_BULK, 64, EPO(2), 0, 0 }
};
static struct usb_intinfo bfl_ints[] = {
@ -126,12 +126,12 @@ static struct usb_intinfo bfl_ints[] = { @@ -126,12 +126,12 @@ static struct usb_intinfo bfl_ints[] = {
#ifdef USE_BITFURY
static struct usb_epinfo bfu0_epinfos[] = {
{ LIBUSB_TRANSFER_TYPE_INTERRUPT, 8, EPI(2), 0, 0, 0 }
{ LIBUSB_TRANSFER_TYPE_INTERRUPT, 8, EPI(2), 0, 0 }
};
static struct usb_epinfo bfu1_epinfos[] = {
{ LIBUSB_TRANSFER_TYPE_BULK, 16, EPI(3), 0, 0, 0 },
{ LIBUSB_TRANSFER_TYPE_BULK, 16, EPO(4), 0, 0, 0 }
{ LIBUSB_TRANSFER_TYPE_BULK, 16, EPI(3), 0, 0 },
{ LIBUSB_TRANSFER_TYPE_BULK, 16, EPO(4), 0, 0 }
};
/* Default to interface 1 */
@ -145,12 +145,12 @@ static struct usb_intinfo bfu_ints[] = { @@ -145,12 +145,12 @@ static struct usb_intinfo bfu_ints[] = {
#include "driver-hashfast.h"
static struct usb_epinfo hfa0_epinfos[] = {
{ LIBUSB_TRANSFER_TYPE_INTERRUPT, 8, EPI(3), 0, 0, 0 }
{ LIBUSB_TRANSFER_TYPE_INTERRUPT, 8, EPI(3), 0, 0 }
};
static struct usb_epinfo hfa1_epinfos[] = {
{ LIBUSB_TRANSFER_TYPE_BULK, 64, EPI(1), 0, 0, 0 },
{ LIBUSB_TRANSFER_TYPE_BULK, 64, EPO(2), 0, 0, 0 }
{ LIBUSB_TRANSFER_TYPE_BULK, 64, EPI(1), 0, 0 },
{ LIBUSB_TRANSFER_TYPE_BULK, 64, EPO(2), 0, 0 }
};
/* Default to interface 1 */
@ -162,8 +162,8 @@ static struct usb_intinfo hfa_ints[] = { @@ -162,8 +162,8 @@ static struct usb_intinfo hfa_ints[] = {
#ifdef USE_MODMINER
static struct usb_epinfo mmq_epinfos[] = {
{ LIBUSB_TRANSFER_TYPE_BULK, 64, EPI(3), 0, 0, 0 },
{ LIBUSB_TRANSFER_TYPE_BULK, 64, EPO(3), 0, 0, 0 }
{ LIBUSB_TRANSFER_TYPE_BULK, 64, EPI(3), 0, 0 },
{ LIBUSB_TRANSFER_TYPE_BULK, 64, EPO(3), 0, 0 }
};
static struct usb_intinfo mmq_ints[] = {
@ -173,8 +173,8 @@ static struct usb_intinfo mmq_ints[] = { @@ -173,8 +173,8 @@ static struct usb_intinfo mmq_ints[] = {
#ifdef USE_AVALON
static struct usb_epinfo ava_epinfos[] = {
{ LIBUSB_TRANSFER_TYPE_BULK, 64, EPI(1), 0, 0, 0 },
{ LIBUSB_TRANSFER_TYPE_BULK, 64, EPO(2), 0, 0, 0 }
{ LIBUSB_TRANSFER_TYPE_BULK, 64, EPI(1), 0, 0 },
{ LIBUSB_TRANSFER_TYPE_BULK, 64, EPO(2), 0, 0 }
};
static struct usb_intinfo ava_ints[] = {
@ -184,8 +184,8 @@ static struct usb_intinfo ava_ints[] = { @@ -184,8 +184,8 @@ static struct usb_intinfo ava_ints[] = {
#ifdef USE_KLONDIKE
static struct usb_epinfo kln_epinfos[] = {
{ LIBUSB_TRANSFER_TYPE_BULK, 64, EPI(1), 0, 0, 0 },
{ LIBUSB_TRANSFER_TYPE_BULK, 64, EPO(1), 0, 0, 0 }
{ LIBUSB_TRANSFER_TYPE_BULK, 64, EPI(1), 0, 0 },
{ LIBUSB_TRANSFER_TYPE_BULK, 64, EPO(1), 0, 0 }
};
static struct usb_intinfo kln_ints[] = {
@ -195,8 +195,8 @@ static struct usb_intinfo kln_ints[] = { @@ -195,8 +195,8 @@ static struct usb_intinfo kln_ints[] = {
#ifdef USE_ICARUS
static struct usb_epinfo ica_epinfos[] = {
{ LIBUSB_TRANSFER_TYPE_BULK, 64, EPI(3), 0, 0, 0 },
{ LIBUSB_TRANSFER_TYPE_BULK, 64, EPO(2), 0, 0, 0 }
{ LIBUSB_TRANSFER_TYPE_BULK, 64, EPI(3), 0, 0 },
{ LIBUSB_TRANSFER_TYPE_BULK, 64, EPO(2), 0, 0 }
};
static struct usb_intinfo ica_ints[] = {
@ -204,8 +204,8 @@ static struct usb_intinfo ica_ints[] = { @@ -204,8 +204,8 @@ static struct usb_intinfo ica_ints[] = {
};
static struct usb_epinfo amu_epinfos[] = {
{ LIBUSB_TRANSFER_TYPE_BULK, 64, EPI(1), 0, 0, 0 },
{ LIBUSB_TRANSFER_TYPE_BULK, 64, EPO(1), 0, 0, 0 }
{ LIBUSB_TRANSFER_TYPE_BULK, 64, EPI(1), 0, 0 },
{ LIBUSB_TRANSFER_TYPE_BULK, 64, EPO(1), 0, 0 }
};
static struct usb_intinfo amu_ints[] = {
@ -213,8 +213,8 @@ static struct usb_intinfo amu_ints[] = { @@ -213,8 +213,8 @@ static struct usb_intinfo amu_ints[] = {
};
static struct usb_epinfo llt_epinfos[] = {
{ LIBUSB_TRANSFER_TYPE_BULK, 64, EPI(1), 0, 0, 0 },
{ LIBUSB_TRANSFER_TYPE_BULK, 64, EPO(2), 0, 0, 0 }
{ LIBUSB_TRANSFER_TYPE_BULK, 64, EPI(1), 0, 0 },
{ LIBUSB_TRANSFER_TYPE_BULK, 64, EPO(2), 0, 0 }
};
static struct usb_intinfo llt_ints[] = {
@ -222,8 +222,8 @@ static struct usb_intinfo llt_ints[] = { @@ -222,8 +222,8 @@ static struct usb_intinfo llt_ints[] = {
};
static struct usb_epinfo cmr1_epinfos[] = {
{ LIBUSB_TRANSFER_TYPE_BULK, 64, EPI(1), 0, 0, 0 },
{ LIBUSB_TRANSFER_TYPE_BULK, 64, EPO(2), 0, 0, 0 }
{ LIBUSB_TRANSFER_TYPE_BULK, 64, EPI(1), 0, 0 },
{ LIBUSB_TRANSFER_TYPE_BULK, 64, EPO(2), 0, 0 }
};
static struct usb_intinfo cmr1_ints[] = {
@ -231,20 +231,20 @@ static struct usb_intinfo cmr1_ints[] = { @@ -231,20 +231,20 @@ static struct usb_intinfo cmr1_ints[] = {
};
static struct usb_epinfo cmr2_epinfos0[] = {
{ LIBUSB_TRANSFER_TYPE_BULK, 64, EPI(1), 0, 0, 0 },
{ LIBUSB_TRANSFER_TYPE_BULK, 64, EPO(2), 0, 0, 0 }
{ LIBUSB_TRANSFER_TYPE_BULK, 64, EPI(1), 0, 0 },
{ LIBUSB_TRANSFER_TYPE_BULK, 64, EPO(2), 0, 0 }
};
static struct usb_epinfo cmr2_epinfos1[] = {
{ LIBUSB_TRANSFER_TYPE_BULK, 64, EPI(3), 0, 0, 0 },
{ LIBUSB_TRANSFER_TYPE_BULK, 64, EPO(4), 0, 0, 0 },
{ LIBUSB_TRANSFER_TYPE_BULK, 64, EPI(3), 0, 0 },
{ LIBUSB_TRANSFER_TYPE_BULK, 64, EPO(4), 0, 0 },
};
static struct usb_epinfo cmr2_epinfos2[] = {
{ LIBUSB_TRANSFER_TYPE_BULK, 64, EPI(5), 0, 0, 0 },
{ LIBUSB_TRANSFER_TYPE_BULK, 64, EPO(6), 0, 0, 0 },
{ LIBUSB_TRANSFER_TYPE_BULK, 64, EPI(5), 0, 0 },
{ LIBUSB_TRANSFER_TYPE_BULK, 64, EPO(6), 0, 0 },
};
static struct usb_epinfo cmr2_epinfos3[] = {
{ LIBUSB_TRANSFER_TYPE_BULK, 64, EPI(7), 0, 0, 0 },
{ LIBUSB_TRANSFER_TYPE_BULK, 64, EPO(8), 0, 0, 0 }
{ LIBUSB_TRANSFER_TYPE_BULK, 64, EPI(7), 0, 0 },
{ LIBUSB_TRANSFER_TYPE_BULK, 64, EPO(8), 0, 0 }
};
static struct usb_intinfo cmr2_ints[] = {
@ -466,6 +466,15 @@ static const char *nodatareturned = "no data returned "; @@ -466,6 +466,15 @@ static const char *nodatareturned = "no data returned ";
cgpu->usbinfo.continuous_ioerr_count = 0; \
}
/* Timeout errors on writes are unusual and should be treated as IO errors. */
#define WRITEIOERR_CHECK(cgpu, err) \
if (err == LIBUSB_ERROR_IO || err == LIBUSB_ERROR_TIMEOUT) { \
cgpu->usbinfo.ioerr_count++; \
cgpu->usbinfo.continuous_ioerr_count++; \
} else { \
cgpu->usbinfo.continuous_ioerr_count = 0; \
}
#if 0 // enable USBDEBUG - only during development testing
static const char *debug_true_str = "true";
static const char *debug_false_str = "false";
@ -1300,9 +1309,6 @@ static struct cg_usb_device *free_cgusb(struct cg_usb_device *cgusb) @@ -1300,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;
@ -2332,6 +2338,9 @@ static void LIBUSB_CALL transfer_callback(struct libusb_transfer *transfer) @@ -2332,6 +2338,9 @@ static void LIBUSB_CALL transfer_callback(struct libusb_transfer *transfer)
static int usb_transfer_toerr(int ret)
{
if (ret <= 0)
return ret;
switch (ret) {
default:
case LIBUSB_TRANSFER_COMPLETED:
@ -2405,15 +2414,24 @@ usb_bulk_transfer(struct libusb_device_handle *dev_handle, int intinfo, @@ -2405,15 +2414,24 @@ usb_bulk_transfer(struct libusb_device_handle *dev_handle, int intinfo,
struct cgpu_info *cgpu, __maybe_unused int mode,
enum usb_cmds cmd, __maybe_unused int seq, bool cancellable)
{
int bulk_timeout, callback_timeout = timeout;
struct usb_epinfo *usb_epinfo;
struct usb_transfer ut;
unsigned char endpoint;
uint16_t MaxPacketSize;
int err, errn;
#if DO_USB_STATS
struct timeval tv_start, tv_finish;
#endif
unsigned char buf[512];
#ifdef WIN32
/* On windows the callback_timeout is a safety mechanism only. */
bulk_timeout = timeout;
callback_timeout += timeout + cgpu->usbdev->found->timeout;
#else
/* We give the transfer no timeout since we manage timeouts ourself on
* non windows. */
bulk_timeout = 0;
#endif
usb_epinfo = &(cgpu->usbdev->found->intinfos[intinfo].epinfos[epinfo]);
endpoint = usb_epinfo->ep;
@ -2423,37 +2441,33 @@ usb_bulk_transfer(struct libusb_device_handle *dev_handle, int intinfo, @@ -2423,37 +2441,33 @@ usb_bulk_transfer(struct libusb_device_handle *dev_handle, int intinfo,
if (unlikely(cgpu->shutdown))
return libusb_bulk_transfer(dev_handle, endpoint, data, length, transferred, timeout);
/* Limit length of transfer to the largest this descriptor supports
* and leave the higher level functions to transfer more if needed. */
if (usb_epinfo->PrefPacketSize)
MaxPacketSize = usb_epinfo->PrefPacketSize;
else
MaxPacketSize = usb_epinfo->wMaxPacketSize;
if (length > MaxPacketSize)
length = MaxPacketSize;
if (length > usb_epinfo->wMaxPacketSize)
length = usb_epinfo->wMaxPacketSize;
if ((endpoint & LIBUSB_ENDPOINT_DIR_MASK) == LIBUSB_ENDPOINT_OUT)
memcpy(buf, data, length);
USBDEBUG("USB debug: @usb_bulk_transfer(%s (nodev=%s),intinfo=%d,epinfo=%d,data=%p,length=%d,timeout=%u,mode=%d,cmd=%s,seq=%d) endpoint=%d", cgpu->drv->name, bool_str(cgpu->usbinfo.nodev), intinfo, epinfo, data, length, timeout, mode, usb_cmdname(cmd), seq, (int)endpoint);
init_usb_transfer(&ut);
/* We give the transfer no timeout since we manage timeouts ourself */
libusb_fill_bulk_transfer(ut.transfer, dev_handle, endpoint, buf, length,
transfer_callback, &ut, 0);
transfer_callback, &ut, bulk_timeout);
STATS_TIMEVAL(&tv_start);
err = usb_submit_transfer(&ut, ut.transfer, cancellable);
errn = errno;
if (!err)
err = callback_wait(&ut, transferred, timeout);
err = callback_wait(&ut, transferred, callback_timeout);
else
err = usb_transfer_toerr(err);
complete_usb_transfer(&ut);
STATS_TIMEVAL(&tv_finish);
USB_STATS(cgpu, &tv_start, &tv_finish, err, mode, cmd, seq, timeout);
if (err < 0)
if (err < 0) {
applog(LOG_DEBUG, "%s%i: %s (amt=%d err=%d ern=%d)",
cgpu->drv->name, cgpu->device_id,
usb_cmdname(cmd), *transferred, err, errn);
}
if (err == LIBUSB_ERROR_PIPE) {
int retries = 0;
@ -2472,12 +2486,12 @@ usb_bulk_transfer(struct libusb_device_handle *dev_handle, int intinfo, @@ -2472,12 +2486,12 @@ usb_bulk_transfer(struct libusb_device_handle *dev_handle, int intinfo,
} while (err && ++retries < USB_RETRY_MAX);
}
if ((endpoint & LIBUSB_ENDPOINT_DIR_MASK) == LIBUSB_ENDPOINT_IN)
memcpy(data, buf, length);
memcpy(data, buf, *transferred);
return err;
}
int _usb_read(struct cgpu_info *cgpu, int intinfo, int epinfo, char *buf, size_t bufsiz, int *processed, unsigned int timeout, const char *end, enum usb_cmds cmd, bool readonce, bool cancellable)
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)
{
struct cg_usb_device *usbdev;
bool ftdi;
@ -2486,10 +2500,11 @@ int _usb_read(struct cgpu_info *cgpu, int intinfo, int epinfo, char *buf, size_t @@ -2486,10 +2500,11 @@ 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;
size_t usbbufread;
const size_t usbbufread = 512; /* Always read full size */
DEVRLOCK(cgpu, pstate);
@ -2514,50 +2529,20 @@ int _usb_read(struct cgpu_info *cgpu, int intinfo, int epinfo, char *buf, size_t @@ -2514,50 +2529,20 @@ 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;
if (tot)
memcpy(usbbuf, usbdev->buffer, tot);
ptr = usbbuf + tot;
usbdev->bufamt = 0;
} else {
tot = 0;
bufleft = bufsiz;
ptr = usbbuf;
}
err = LIBUSB_SUCCESS;
initial_timeout = timeout;
max = ((double)timeout) / 1000.0;
cgtime(&read_start);
while (bufleft > 0) {
// TODO: use (USB_MAX_READ - tot) always?
if (usbdev->buffer)
usbbufread = USB_MAX_READ - tot;
else {
if (ftdi)
usbbufread = bufleft + 2;
else
usbbufread = bufleft;
}
got = 0;
if (first && usbdev->usecps && usbdev->last_write_siz) {
cgtimer_t now, already_done;
double sleep_estimate;
double write_time = (double)(usbdev->last_write_siz) /
(double)(usbdev->cps) * 1000;
cgtimer_time(&now);
cgtimer_sub(&now, &usbdev->cgt_last_write, &already_done);
sleep_estimate = write_time - cgtimer_to_ms(&already_done);
if (sleep_estimate > 0.0) {
cgsleep_ms_r(&usbdev->cgt_last_write, write_time);
cgpu->usbinfo.read_delay_count++;
cgpu->usbinfo.total_read_delay += sleep_estimate;
}
}
err = usb_bulk_transfer(usbdev->handle, intinfo, epinfo,
ptr, usbbufread, &got, timeout,
cgpu, MODE_BULK_READ, cmd, first ? SEQ0 : SEQ1,
@ -2595,12 +2580,12 @@ int _usb_read(struct cgpu_info *cgpu, int intinfo, int epinfo, char *buf, size_t @@ -2595,12 +2580,12 @@ int _usb_read(struct cgpu_info *cgpu, int intinfo, int epinfo, char *buf, size_t
if (unlikely(done >= max))
break;
timeout = initial_timeout - (done * 1000);
if (!timeout)
if (timeout <= 0)
break;
}
// 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;
@ -2615,50 +2600,21 @@ int _usb_read(struct cgpu_info *cgpu, int intinfo, int epinfo, char *buf, size_t @@ -2615,50 +2600,21 @@ 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;
if (tot)
memcpy(usbbuf, usbdev->buffer, tot);
ptr = usbbuf + tot;
usbdev->bufamt = 0;
} else {
tot = 0;
bufleft = bufsiz;
ptr = usbbuf;
}
endlen = strlen(end);
err = LIBUSB_SUCCESS;
initial_timeout = timeout;
max = ((double)timeout) / 1000.0;
cgtime(&read_start);
while (bufleft > 0) {
// TODO: use (USB_MAX_READ - tot) always?
if (usbdev->buffer)
usbbufread = USB_MAX_READ - tot;
else {
if (ftdi)
usbbufread = bufleft + 2;
else
usbbufread = bufleft;
}
got = 0;
if (first && usbdev->usecps && usbdev->last_write_siz) {
cgtimer_t now, already_done;
double sleep_estimate;
double write_time = (double)(usbdev->last_write_siz) /
(double)(usbdev->cps) * 1000;
cgtimer_time(&now);
cgtimer_sub(&now, &usbdev->cgt_last_write, &already_done);
sleep_estimate = write_time - cgtimer_to_ms(&already_done);
if (sleep_estimate > 0.0) {
cgsleep_ms_r(&usbdev->cgt_last_write, write_time);
cgpu->usbinfo.read_delay_count++;
cgpu->usbinfo.total_read_delay += sleep_estimate;
}
}
err = usb_bulk_transfer(usbdev->handle, intinfo, epinfo,
ptr, usbbufread, &got, timeout,
cgpu, MODE_BULK_READ, cmd, first ? SEQ0 : SEQ1,
@ -2699,12 +2655,11 @@ int _usb_read(struct cgpu_info *cgpu, int intinfo, int epinfo, char *buf, size_t @@ -2699,12 +2655,11 @@ int _usb_read(struct cgpu_info *cgpu, int intinfo, int epinfo, char *buf, size_t
if (unlikely(done >= max))
break;
timeout = initial_timeout - (done * 1000);
if (!timeout)
if (timeout <= 0)
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
@ -2733,7 +2688,6 @@ int _usb_read(struct cgpu_info *cgpu, int intinfo, int epinfo, char *buf, size_t @@ -2733,7 +2688,6 @@ int _usb_read(struct cgpu_info *cgpu, int intinfo, int epinfo, char *buf, size_t
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);
@ -2756,7 +2710,7 @@ out_noerrmsg: @@ -2756,7 +2710,7 @@ out_noerrmsg:
return err;
}
int _usb_write(struct cgpu_info *cgpu, int intinfo, int epinfo, char *buf, size_t bufsiz, int *processed, unsigned int timeout, enum usb_cmds cmd)
int _usb_write(struct cgpu_info *cgpu, int intinfo, int epinfo, char *buf, size_t bufsiz, int *processed, int timeout, enum usb_cmds cmd)
{
struct cg_usb_device *usbdev;
struct timeval read_start, tv_finish;
@ -2788,29 +2742,15 @@ int _usb_write(struct cgpu_info *cgpu, int intinfo, int epinfo, char *buf, size_ @@ -2788,29 +2742,15 @@ int _usb_write(struct cgpu_info *cgpu, int intinfo, int epinfo, char *buf, size_
max = ((double)timeout) / 1000.0;
cgtime(&read_start);
while (bufsiz > 0) {
sent = 0;
if (usbdev->usecps) {
if (usbdev->last_write_siz) {
cgtimer_t now, already_done;
double sleep_estimate;
double write_time = (double)(usbdev->last_write_siz) /
(double)(usbdev->cps) * 1000;
cgtimer_time(&now);
cgtimer_sub(&now, &usbdev->cgt_last_write, &already_done);
sleep_estimate = write_time - cgtimer_to_ms(&already_done);
int tosend = bufsiz;
if (sleep_estimate > 0.0) {
cgsleep_ms_r(&usbdev->cgt_last_write, write_time);
cgpu->usbinfo.write_delay_count++;
cgpu->usbinfo.total_write_delay += sleep_estimate;
}
}
cgsleep_prepare_r(&usbdev->cgt_last_write);
usbdev->last_write_siz = bufsiz;
if (usbdev->usecps) {
int cpms = usbdev->cps / 1000 ? : 1;
if (tosend > cpms)
tosend = cpms;
}
err = usb_bulk_transfer(usbdev->handle, intinfo, epinfo,
(unsigned char *)buf, bufsiz, &sent, timeout,
(unsigned char *)buf, tosend, &sent, timeout,
cgpu, MODE_BULK_WRITE, cmd, first ? SEQ0 : SEQ1,
false);
cgtime(&tv_finish);
@ -2834,7 +2774,7 @@ int _usb_write(struct cgpu_info *cgpu, int intinfo, int epinfo, char *buf, size_ @@ -2834,7 +2774,7 @@ int _usb_write(struct cgpu_info *cgpu, int intinfo, int epinfo, char *buf, size_
if (unlikely(done >= max))
break;
timeout = initial_timeout - (done * 1000);
if (!timeout)
if (timeout <= 0)
break;
}
@ -2928,26 +2868,6 @@ int __usb_transfer(struct cgpu_info *cgpu, uint8_t request_type, uint8_t bReques @@ -2928,26 +2868,6 @@ int __usb_transfer(struct cgpu_info *cgpu, uint8_t request_type, uint8_t bReques
USBDEBUG("USB debug: @_usb_transfer() buf=%s", bin2hex(buf, (size_t)siz));
if (usbdev->usecps) {
if (usbdev->last_write_siz) {
cgtimer_t now, already_done;
double sleep_estimate;
double write_time = (double)(usbdev->last_write_siz) /
(double)(usbdev->cps) * 1000;
cgtimer_time(&now);
cgtimer_sub(&now, &usbdev->cgt_last_write, &already_done);
sleep_estimate = write_time - cgtimer_to_ms(&already_done);
if (sleep_estimate > 0.0) {
cgsleep_ms_r(&usbdev->cgt_last_write, write_time);
cgpu->usbinfo.write_delay_count++;
cgpu->usbinfo.total_write_delay += sleep_estimate;
}
}
cgsleep_prepare_r(&usbdev->cgt_last_write);
usbdev->last_write_siz = siz;
}
STATS_TIMEVAL(&tv_start);
err = usb_control_transfer(cgpu, usbdev->handle, request_type, bRequest,
wValue, wIndex, buf, (uint16_t)siz, timeout);
@ -3011,22 +2931,6 @@ int _usb_transfer_read(struct cgpu_info *cgpu, uint8_t request_type, uint8_t bRe @@ -3011,22 +2931,6 @@ int _usb_transfer_read(struct cgpu_info *cgpu, uint8_t request_type, uint8_t bRe
*amount = 0;
if (usbdev->usecps && usbdev->last_write_siz) {
cgtimer_t now, already_done;
double sleep_estimate;
double write_time = (double)(usbdev->last_write_siz) /
(double)(usbdev->cps) * 1000;
cgtimer_time(&now);
cgtimer_sub(&now, &usbdev->cgt_last_write, &already_done);
sleep_estimate = write_time - cgtimer_to_ms(&already_done);
if (sleep_estimate > 0.0) {
cgsleep_ms_r(&usbdev->cgt_last_write, write_time);
cgpu->usbinfo.read_delay_count++;
cgpu->usbinfo.total_read_delay += sleep_estimate;
}
}
memset(tbuf, 0, 64);
STATS_TIMEVAL(&tv_start);
err = usb_control_transfer(cgpu, usbdev->handle, request_type, bRequest,
@ -3114,44 +3018,6 @@ int _usb_ftdi_set_latency(struct cgpu_info *cgpu, int intinfo) @@ -3114,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;
@ -3252,40 +3118,6 @@ enum sub_ident usb_ident(struct cgpu_info *cgpu) @@ -3252,40 +3118,6 @@ enum sub_ident usb_ident(struct cgpu_info *cgpu)
return ident;
}
/*
* If you pass both intinfo and epinfo as <0 then it will set all
* endpoints to PrefPacketSize
* If intinfo >=0 but epinfo <0 then it will set all endpoints
* for the given one intinfo to PrefPacketSize
* If both are >=0 then it will set only the specified single
* endpoint (intinfo,epinfo) to PrefPacketSize
*/
void _usb_set_pps(struct cgpu_info *cgpu, int intinfo, int epinfo, uint16_t PrefPacketSize)
{
struct usb_find_devices *found;
int pstate;
DEVWLOCK(cgpu, pstate);
if (cgpu->usbdev) {
found = cgpu->usbdev->found;
if (intinfo >= 0 && epinfo >= 0)
found->intinfos[intinfo].epinfos[epinfo].PrefPacketSize = PrefPacketSize;
else {
if (intinfo >= 0) {
for (epinfo = 0; epinfo < found->intinfos[intinfo].epinfo_count; epinfo++)
found->intinfos[intinfo].epinfos[epinfo].PrefPacketSize = PrefPacketSize;
} else {
for (intinfo = 0; intinfo < found->intinfo_count ; intinfo++)
for (epinfo = 0; epinfo < found->intinfos[intinfo].epinfo_count; epinfo++)
found->intinfos[intinfo].epinfos[epinfo].PrefPacketSize = PrefPacketSize;
}
}
}
DEVWUNLOCK(cgpu, pstate);
}
// Need to set all devices with matching usbdev
void usb_set_dev_start(struct cgpu_info *cgpu)
{

15
usbutils.h

@ -122,7 +122,6 @@ struct usb_epinfo { @@ -122,7 +122,6 @@ struct usb_epinfo {
uint16_t size;
unsigned char ep;
uint16_t wMaxPacketSize;
uint16_t PrefPacketSize;
bool found;
};
@ -182,6 +181,8 @@ enum usb_types { @@ -182,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;
@ -197,7 +198,7 @@ struct cg_usb_device { @@ -197,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;
@ -206,8 +207,6 @@ struct cg_usb_device { @@ -206,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
@ -388,15 +387,13 @@ bool usb_init(struct cgpu_info *cgpu, struct libusb_device *dev, struct usb_find @@ -388,15 +387,13 @@ bool usb_init(struct cgpu_info *cgpu, struct libusb_device *dev, struct usb_find
void usb_detect(struct device_drv *drv, bool (*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, unsigned int timeout, const char *end, enum usb_cmds cmd, bool readonce, bool cancellable);
int _usb_write(struct cgpu_info *cgpu, int intinfo, int epinfo, char *buf, size_t bufsiz, int *processed, unsigned int timeout, enum usb_cmds);
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);
int _usb_write(struct cgpu_info *cgpu, int intinfo, int epinfo, char *buf, size_t bufsiz, int *processed, int timeout, enum usb_cmds);
int _usb_transfer(struct cgpu_info *cgpu, uint8_t request_type, uint8_t bRequest, uint16_t wValue, uint16_t wIndex, uint32_t *data, int siz, unsigned int timeout, enum usb_cmds cmd);
int _usb_transfer_read(struct cgpu_info *cgpu, uint8_t request_type, uint8_t bRequest, uint16_t wValue, uint16_t wIndex, char *buf, int bufsiz, int *amount, unsigned int timeout, enum usb_cmds cmd);
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);
@ -405,8 +402,6 @@ void usb_disable_cps(struct cgpu_info *cgpu); @@ -405,8 +402,6 @@ void usb_disable_cps(struct cgpu_info *cgpu);
int _usb_interface(struct cgpu_info *cgpu, int intinfo);
#define usb_interface(_cgpu) _usb_interface(_cgpu, DEFAULT_INTINFO)
enum sub_ident usb_ident(struct cgpu_info *cgpu);
void _usb_set_pps(struct cgpu_info *cgpu, int intinfo, int epinfo, uint16_t PrefPacketSize);
#define usb_set_pps(_cgpu, _pps) _usb_set_pps(_cgpu, -1, -1, _pps)
void usb_set_dev_start(struct cgpu_info *cgpu);
void usb_cleanup();
void usb_initialise();

Loading…
Cancel
Save