1
0
mirror of https://github.com/GOSTSec/sgminer synced 2025-01-09 14:28:12 +00:00

usbutils - make FTDI handling automatic

This commit is contained in:
Kano 2013-05-16 21:38:55 +10:00
parent 92e5ee9d66
commit 2631b55ee2
4 changed files with 41 additions and 35 deletions

View File

@ -487,7 +487,7 @@ static bool getok(struct cgpu_info *bflsc, enum usb_cmds cmd, int *err, int *amo
{ {
char buf[BFLSC_BUFSIZ+1]; char buf[BFLSC_BUFSIZ+1];
*err = usb_ftdi_read_nl(bflsc, buf, sizeof(buf)-1, amount, cmd); *err = usb_read_nl(bflsc, buf, sizeof(buf)-1, amount, cmd);
if (*err < 0 || *amount < (int)BFLSC_OK_LEN) if (*err < 0 || *amount < (int)BFLSC_OK_LEN)
return false; return false;
else else
@ -496,7 +496,7 @@ static bool getok(struct cgpu_info *bflsc, enum usb_cmds cmd, int *err, int *amo
static bool getokerr(struct cgpu_info *bflsc, enum usb_cmds cmd, int *err, int *amount, char *buf, size_t bufsiz) static bool getokerr(struct cgpu_info *bflsc, enum usb_cmds cmd, int *err, int *amount, char *buf, size_t bufsiz)
{ {
*err = usb_ftdi_read_nl(bflsc, buf, bufsiz-1, amount, cmd); *err = usb_read_nl(bflsc, buf, bufsiz-1, amount, cmd);
if (*err < 0 || *amount < (int)BFLSC_OK_LEN) if (*err < 0 || *amount < (int)BFLSC_OK_LEN)
return false; return false;
else { else {
@ -529,7 +529,7 @@ static void bflsc_send_flush_work(struct cgpu_info *bflsc, int dev)
} }
} }
/* return True = attempted usb_ftdi_read_ok() /* return True = attempted usb_read_ok()
* set ignore to true means no applog/ignore errors */ * set ignore to true means no applog/ignore errors */
static bool bflsc_qres(struct cgpu_info *bflsc, char *buf, size_t bufsiz, int dev, int *err, int *amount, bool ignore) static bool bflsc_qres(struct cgpu_info *bflsc, char *buf, size_t bufsiz, int dev, int *err, int *amount, bool ignore)
{ {
@ -550,7 +550,7 @@ static bool bflsc_qres(struct cgpu_info *bflsc, char *buf, size_t bufsiz, int de
// of course all other I/O must also be failing ... // of course all other I/O must also be failing ...
} else { } else {
readok = true; readok = true;
*err = usb_ftdi_read_ok(bflsc, buf, bufsiz-1, amount, C_GETRESULTS); *err = usb_read_ok(bflsc, buf, bufsiz-1, amount, C_GETRESULTS);
mutex_unlock(&(bflsc->device_mutex)); mutex_unlock(&(bflsc->device_mutex));
if (*err < 0 || *amount < 1) { if (*err < 0 || *amount < 1) {
@ -695,7 +695,7 @@ static bool getinfo(struct cgpu_info *bflsc, int dev)
return false; return false;
} }
err = usb_ftdi_read_ok(bflsc, buf, sizeof(buf)-1, &amount, C_GETDETAILS); err = usb_read_ok(bflsc, buf, sizeof(buf)-1, &amount, C_GETDETAILS);
if (err < 0 || amount < 1) { if (err < 0 || amount < 1) {
if (err < 0) { if (err < 0) {
applog(LOG_ERR, "%s detect (%s) get details return invalid/timed out (%d:%d)", applog(LOG_ERR, "%s detect (%s) get details return invalid/timed out (%d:%d)",
@ -830,7 +830,7 @@ reinit:
goto unshin; goto unshin;
} }
err = usb_ftdi_read_nl(bflsc, buf, sizeof(buf)-1, &amount, C_GETIDENTIFY); err = usb_read_nl(bflsc, buf, sizeof(buf)-1, &amount, C_GETIDENTIFY);
if (err < 0 || amount < 1) { if (err < 0 || amount < 1) {
init_count++; init_count++;
cgtime(&init_now); cgtime(&init_now);
@ -1095,7 +1095,7 @@ static bool bflsc_get_temp(struct cgpu_info *bflsc, int dev)
return false; return false;
} }
err = usb_ftdi_read_nl(bflsc, temp_buf, sizeof(temp_buf)-1, &amount, C_GETTEMPERATURE); err = usb_read_nl(bflsc, temp_buf, sizeof(temp_buf)-1, &amount, C_GETTEMPERATURE);
if (err < 0 || amount < 1) { if (err < 0 || amount < 1) {
mutex_unlock(&(bflsc->device_mutex)); mutex_unlock(&(bflsc->device_mutex));
if (err < 0) { if (err < 0) {
@ -1117,7 +1117,7 @@ static bool bflsc_get_temp(struct cgpu_info *bflsc, int dev)
return false; return false;
} }
err = usb_ftdi_read_nl(bflsc, volt_buf, sizeof(volt_buf)-1, &amount, C_GETTEMPERATURE); err = usb_read_nl(bflsc, volt_buf, sizeof(volt_buf)-1, &amount, C_GETTEMPERATURE);
if (err < 0 || amount < 1) { if (err < 0 || amount < 1) {
mutex_unlock(&(bflsc->device_mutex)); mutex_unlock(&(bflsc->device_mutex));
if (err < 0) { if (err < 0) {

View File

@ -200,7 +200,7 @@ reinit:
goto unshin; goto unshin;
} }
if ((err = usb_ftdi_read_nl(bitforce, buf, sizeof(buf)-1, &amount, C_GETIDENTIFY)) < 0 || amount < 1) { if ((err = usb_read_nl(bitforce, buf, sizeof(buf)-1, &amount, C_GETIDENTIFY)) < 0 || amount < 1) {
init_count++; init_count++;
cgtime(&init_now); cgtime(&init_now);
if (us_tdiff(&init_now, &init_start) <= REINIT_TIME_MAX) { if (us_tdiff(&init_now, &init_start) <= REINIT_TIME_MAX) {
@ -395,7 +395,7 @@ static bool bitforce_get_temp(struct cgpu_info *bitforce)
return false; return false;
} }
if ((err = usb_ftdi_read_nl(bitforce, buf, sizeof(buf)-1, &amount, C_GETTEMPERATURE)) < 0 || amount < 1) { if ((err = usb_read_nl(bitforce, buf, sizeof(buf)-1, &amount, C_GETTEMPERATURE)) < 0 || amount < 1) {
mutex_unlock(&bitforce->device_mutex); mutex_unlock(&bitforce->device_mutex);
if (err < 0) { if (err < 0) {
applog(LOG_ERR, "%s%i: Error: Get temp return invalid/timed out (%d:%d)", applog(LOG_ERR, "%s%i: Error: Get temp return invalid/timed out (%d:%d)",
@ -470,7 +470,7 @@ re_send:
return false; return false;
} }
if ((err = usb_ftdi_read_nl(bitforce, buf, sizeof(buf)-1, &amount, C_REQUESTSENDWORKSTATUS)) < 0) { if ((err = usb_read_nl(bitforce, buf, sizeof(buf)-1, &amount, C_REQUESTSENDWORKSTATUS)) < 0) {
mutex_unlock(&bitforce->device_mutex); mutex_unlock(&bitforce->device_mutex);
applog(LOG_ERR, "%s%d: read request send work status failed (%d:%d)", applog(LOG_ERR, "%s%d: read request send work status failed (%d:%d)",
bitforce->drv->name, bitforce->device_id, amount, err); bitforce->drv->name, bitforce->device_id, amount, err);
@ -524,7 +524,7 @@ re_send:
return false; return false;
} }
if ((err = usb_ftdi_read_nl(bitforce, buf, sizeof(buf)-1, &amount, C_SENDWORKSTATUS)) < 0) { if ((err = usb_read_nl(bitforce, buf, sizeof(buf)-1, &amount, C_SENDWORKSTATUS)) < 0) {
mutex_unlock(&bitforce->device_mutex); mutex_unlock(&bitforce->device_mutex);
applog(LOG_ERR, "%s%d: read send work status failed (%d:%d)", applog(LOG_ERR, "%s%d: read send work status failed (%d:%d)",
bitforce->drv->name, bitforce->device_id, amount, err); bitforce->drv->name, bitforce->device_id, amount, err);
@ -573,7 +573,7 @@ static int64_t bitforce_get_result(struct thr_info *thr, struct work *work)
mutex_lock(&bitforce->device_mutex); mutex_lock(&bitforce->device_mutex);
usb_write(bitforce, BITFORCE_WORKSTATUS, BITFORCE_WORKSTATUS_LEN, &amount, C_REQUESTWORKSTATUS); usb_write(bitforce, BITFORCE_WORKSTATUS, BITFORCE_WORKSTATUS_LEN, &amount, C_REQUESTWORKSTATUS);
usb_ftdi_read_nl(bitforce, buf, sizeof(buf)-1, &amount, C_GETWORKSTATUS); usb_read_nl(bitforce, buf, sizeof(buf)-1, &amount, C_GETWORKSTATUS);
mutex_unlock(&bitforce->device_mutex); mutex_unlock(&bitforce->device_mutex);
cgtime(&now); cgtime(&now);

View File

@ -115,13 +115,15 @@ static struct usb_endpoints cmr_eps[] = {
}; };
#endif #endif
#define IDVENDOR_FTDI 0x0403
// TODO: Add support for (at least) Isochronous endpoints // TODO: Add support for (at least) Isochronous endpoints
static struct usb_find_devices find_dev[] = { static struct usb_find_devices find_dev[] = {
#ifdef USE_BFLSC #ifdef USE_BFLSC
{ {
.drv = DRV_BFLSC, .drv = DRV_BFLSC,
.name = "BAS", .name = "BAS",
.idVendor = 0x0403, .idVendor = IDVENDOR_FTDI,
.idProduct = 0x6014, .idProduct = 0x6014,
.iManufacturer = "Butterfly Labs", .iManufacturer = "Butterfly Labs",
.iProduct = "BitFORCE SHA256 SC", .iProduct = "BitFORCE SHA256 SC",
@ -136,7 +138,7 @@ static struct usb_find_devices find_dev[] = {
{ {
.drv = DRV_BITFORCE, .drv = DRV_BITFORCE,
.name = "BFL", .name = "BFL",
.idVendor = 0x0403, .idVendor = IDVENDOR_FTDI,
.idProduct = 0x6014, .idProduct = 0x6014,
.iManufacturer = "Butterfly Labs Inc.", .iManufacturer = "Butterfly Labs Inc.",
.iProduct = "BitFORCE SHA256", .iProduct = "BitFORCE SHA256",
@ -164,7 +166,7 @@ static struct usb_find_devices find_dev[] = {
{ {
.drv = DRV_AVALON, .drv = DRV_AVALON,
.name = "AVA", .name = "AVA",
.idVendor = 0x0403, .idVendor = IDVENDOR_FTDI,
.idProduct = 0x6001, .idProduct = 0x6001,
.kernel = 0, .kernel = 0,
.config = 1, .config = 1,
@ -199,7 +201,7 @@ static struct usb_find_devices find_dev[] = {
{ {
.drv = DRV_ICARUS, .drv = DRV_ICARUS,
.name = "BLT", .name = "BLT",
.idVendor = 0x0403, .idVendor = IDVENDOR_FTDI,
.idProduct = 0x6001, .idProduct = 0x6001,
.iProduct = "FT232R USB UART", .iProduct = "FT232R USB UART",
.kernel = 0, .kernel = 0,
@ -212,7 +214,7 @@ static struct usb_find_devices find_dev[] = {
{ {
.drv = DRV_ICARUS, .drv = DRV_ICARUS,
.name = "LLT", .name = "LLT",
.idVendor = 0x0403, .idVendor = IDVENDOR_FTDI,
.idProduct = 0x6001, .idProduct = 0x6001,
.kernel = 0, .kernel = 0,
.config = 1, .config = 1,
@ -1330,6 +1332,9 @@ static int _usb_init(struct cgpu_info *cgpu, struct libusb_device *dev, struct u
cgusb = calloc(1, sizeof(*cgusb)); cgusb = calloc(1, sizeof(*cgusb));
cgusb->found = found; cgusb->found = found;
if (found->idVendor == IDVENDOR_FTDI)
cgusb->usb_type = USB_TYPE_FTDI;
cgusb->descriptor = calloc(1, sizeof(*(cgusb->descriptor))); cgusb->descriptor = calloc(1, sizeof(*(cgusb->descriptor)));
err = libusb_get_device_descriptor(dev, cgusb->descriptor); err = libusb_get_device_descriptor(dev, cgusb->descriptor);
@ -1911,9 +1916,10 @@ static void rejected_inc(struct cgpu_info *cgpu)
#define USB_MAX_READ 8192 #define USB_MAX_READ 8192
int _usb_read(struct cgpu_info *cgpu, int ep, char *buf, size_t bufsiz, int *processed, unsigned int timeout, const char *end, enum usb_cmds cmd, bool ftdi, bool readonce) int _usb_read(struct cgpu_info *cgpu, int ep, char *buf, size_t bufsiz, int *processed, unsigned int timeout, const char *end, enum usb_cmds cmd, bool readonce)
{ {
struct cg_usb_device *usbdev = cgpu->usbdev; struct cg_usb_device *usbdev = cgpu->usbdev;
bool ftdi = (usbdev->usb_type == USB_TYPE_FTDI);
#if DO_USB_STATS #if DO_USB_STATS
struct timeval tv_start; struct timeval tv_start;
#endif #endif

View File

@ -65,12 +65,18 @@ struct usb_find_devices {
struct usb_endpoints *eps; struct usb_endpoints *eps;
}; };
enum usb_types {
USB_TYPE_STD = 0,
USB_TYPE_FTDI
};
struct cg_usb_device { struct cg_usb_device {
struct usb_find_devices *found; struct usb_find_devices *found;
libusb_device_handle *handle; libusb_device_handle *handle;
pthread_mutex_t *mutex; pthread_mutex_t *mutex;
struct libusb_device_descriptor *descriptor; struct libusb_device_descriptor *descriptor;
uint16_t usbver; uint16_t usbver;
enum usb_types usb_type;
int speed; int speed;
char *prod_string; char *prod_string;
char *manuf_string; char *manuf_string;
@ -150,29 +156,32 @@ 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 *)); 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); struct api_data *api_usb_stats(int *count);
void update_usb_stats(struct cgpu_info *cgpu); void update_usb_stats(struct cgpu_info *cgpu);
int _usb_read(struct cgpu_info *cgpu, int ep, char *buf, size_t bufsiz, int *processed, unsigned int timeout, const char *end, enum usb_cmds cmd, bool ftdi, bool readonce); int _usb_read(struct cgpu_info *cgpu, int ep, char *buf, size_t bufsiz, int *processed, unsigned int timeout, const char *end, enum usb_cmds cmd, bool readonce);
int _usb_write(struct cgpu_info *cgpu, int ep, char *buf, size_t bufsiz, int *processed, unsigned int timeout, enum usb_cmds); int _usb_write(struct cgpu_info *cgpu, int ep, char *buf, size_t bufsiz, int *processed, unsigned 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, unsigned int timeout, enum usb_cmds cmd); int _usb_transfer(struct cgpu_info *cgpu, uint8_t request_type, uint8_t bRequest, uint16_t wValue, uint16_t wIndex, unsigned int timeout, enum usb_cmds cmd);
void usb_cleanup(); void usb_cleanup();
void usb_initialise(); void usb_initialise();
#define usb_read(cgpu, buf, bufsiz, read, cmd) \ #define usb_read(cgpu, buf, bufsiz, read, cmd) \
_usb_read(cgpu, DEFAULT_EP_IN, buf, bufsiz, read, DEVTIMEOUT, NULL, cmd, false, false) _usb_read(cgpu, DEFAULT_EP_IN, buf, bufsiz, read, DEVTIMEOUT, NULL, cmd, false)
#define usb_read_once(cgpu, buf, bufsiz, read, cmd) \ #define usb_read_once(cgpu, buf, bufsiz, read, cmd) \
_usb_read(cgpu, DEFAULT_EP_IN, buf, bufsiz, read, DEVTIMEOUT, NULL, cmd, false, true) _usb_read(cgpu, DEFAULT_EP_IN, buf, bufsiz, read, DEVTIMEOUT, NULL, cmd, true)
#define usb_read_nl(cgpu, buf, bufsiz, read, cmd) \ #define usb_read_nl(cgpu, buf, bufsiz, read, cmd) \
_usb_read(cgpu, DEFAULT_EP_IN, buf, bufsiz, read, DEVTIMEOUT, "\n", cmd, false, false) _usb_read(cgpu, DEFAULT_EP_IN, buf, bufsiz, read, DEVTIMEOUT, "\n", cmd, false)
#define usb_read_ok(cgpu, buf, bufsiz, read, cmd) \
_usb_read(cgpu, DEFAULT_EP_IN, buf, bufsiz, read, DEVTIMEOUT, "OK\n", cmd, false)
#define usb_read_ep(cgpu, ep, buf, bufsiz, read, cmd) \ #define usb_read_ep(cgpu, ep, buf, bufsiz, read, cmd) \
_usb_read(cgpu, ep, buf, bufsiz, read, DEVTIMEOUT, NULL, cmd, false, false) _usb_read(cgpu, ep, buf, bufsiz, read, DEVTIMEOUT, NULL, cmd, false)
#define usb_read_timeout(cgpu, buf, bufsiz, read, timeout, cmd) \ #define usb_read_timeout(cgpu, buf, bufsiz, read, timeout, cmd) \
_usb_read(cgpu, DEFAULT_EP_IN, buf, bufsiz, read, timeout, NULL, cmd, false, false) _usb_read(cgpu, DEFAULT_EP_IN, buf, bufsiz, read, timeout, NULL, cmd, false)
#define usb_read_ep_timeout(cgpu, ep, buf, bufsiz, read, timeout, cmd) \ #define usb_read_ep_timeout(cgpu, ep, buf, bufsiz, read, timeout, cmd) \
_usb_read(cgpu, ep, buf, bufsiz, read, timeout, NULL, cmd, false, false) _usb_read(cgpu, ep, buf, bufsiz, read, timeout, NULL, cmd, false)
#define usb_write(cgpu, buf, bufsiz, wrote, cmd) \ #define usb_write(cgpu, buf, bufsiz, wrote, cmd) \
_usb_write(cgpu, DEFAULT_EP_OUT, buf, bufsiz, wrote, DEVTIMEOUT, cmd) _usb_write(cgpu, DEFAULT_EP_OUT, buf, bufsiz, wrote, DEVTIMEOUT, cmd)
@ -186,15 +195,6 @@ void usb_initialise();
#define usb_write_ep_timeout(cgpu, ep, buf, bufsiz, wrote, timeout, cmd) \ #define usb_write_ep_timeout(cgpu, ep, buf, bufsiz, wrote, timeout, cmd) \
_usb_write(cgpu, ep, buf, bufsiz, wrote, timeout, cmd) _usb_write(cgpu, ep, buf, bufsiz, wrote, timeout, cmd)
#define usb_ftdi_read(cgpu, buf, bufsiz, read, cmd) \
_usb_read(cgpu, DEFAULT_EP_IN, buf, bufsiz, read, DEVTIMEOUT, NULL, cmd, true, false)
#define usb_ftdi_read_nl(cgpu, buf, bufsiz, read, cmd) \
_usb_read(cgpu, DEFAULT_EP_IN, buf, bufsiz, read, DEVTIMEOUT, "\n", cmd, true, false)
#define usb_ftdi_read_ok(cgpu, buf, bufsiz, read, cmd) \
_usb_read(cgpu, DEFAULT_EP_IN, buf, bufsiz, read, DEVTIMEOUT, "OK\n", cmd, true, false)
#define usb_transfer(cgpu, typ, req, val, idx, cmd) \ #define usb_transfer(cgpu, typ, req, val, idx, cmd) \
_usb_transfer(cgpu, typ, req, val, idx, DEVTIMEOUT, cmd) _usb_transfer(cgpu, typ, req, val, idx, DEVTIMEOUT, cmd)