From a32e521d0a98b8b8f62db37d30886373cd5232cd Mon Sep 17 00:00:00 2001 From: Con Kolivas Date: Mon, 27 May 2013 17:17:42 +1000 Subject: [PATCH] Implement avalon_ready and avalon_wait_ready functions for when usb is ready to receive commands. --- driver-avalon.c | 6 ++++++ driver-avalon.h | 1 + usbutils.c | 20 ++++++++++++++++++++ usbutils.h | 1 + 4 files changed, 28 insertions(+) diff --git a/driver-avalon.c b/driver-avalon.c index 34b20c82..defa675a 100644 --- a/driver-avalon.c +++ b/driver-avalon.c @@ -121,6 +121,12 @@ static inline void avalon_create_task(struct avalon_task *at, memcpy(at->data, work->data + 64, 12); } +static void avalon_wait_ready(struct cgpu_info *avalon) +{ + while (!avalon_ready(avalon)) + nmsleep(40); +} + static int avalon_write(struct cgpu_info *avalon, char *buf, ssize_t len) { ssize_t wrote = 0; diff --git a/driver-avalon.h b/driver-avalon.h index c7892747..03c89d7d 100644 --- a/driver-avalon.h +++ b/driver-avalon.h @@ -123,6 +123,7 @@ struct avalon_info { #define AVA_SEND_OK 0 #define avalon_buffer_full(avalon) !usb_ftdi_cts(avalon) +#define avalon_ready(avalon) usb_ftdi_ctw(avalon) #define AVALON_READ_TIME(baud) ((double)AVALON_READ_SIZE * (double)8.0 / (double)(baud)) #define ASSERT1(condition) __maybe_unused static char sizeof_uint32_t_must_be_4[(condition)?1:-1] diff --git a/usbutils.c b/usbutils.c index 50a9d97f..6f61e333 100644 --- a/usbutils.c +++ b/usbutils.c @@ -2240,6 +2240,26 @@ int usb_ftdi_cts(struct cgpu_info *cgpu) return (ret & FTDI_RS0_CTS); } +#define FTDI_RS_DR 1 +#define FTDI_RS_OE (1<<1) +#define FTDI_RS_PE (1<<2) +#define FTDI_RS_FE (1<<3) +#define FTDI_RS_BI (1<<4) +#define FTDI_RS_THRE (1<<5) +#define FTDI_RS_TEMT (1<<6) +#define FTDI_RS_FIFO (1<<7) + +int usb_ftdi_ctw(struct cgpu_info *cgpu) +{ + struct cg_usb_device *usbdev = cgpu->usbdev; + unsigned char buf[2]; + + libusb_control_transfer(usbdev->handle, (uint8_t)FTDI_TYPE_IN, + (uint8_t)5, (uint16_t)0, (uint16_t)0, + buf, 2, DEVTIMEOUT); + return ((buf[1] & FTDI_RS_THRE) && (buf[1] & FTDI_RS_TEMT)); +} + int _usb_write(struct cgpu_info *cgpu, int ep, char *buf, size_t bufsiz, int *processed, unsigned int timeout, __maybe_unused enum usb_cmds cmd) { struct cg_usb_device *usbdev = cgpu->usbdev; diff --git a/usbutils.h b/usbutils.h index f1ab43a0..22781afa 100644 --- a/usbutils.h +++ b/usbutils.h @@ -238,6 +238,7 @@ struct api_data *api_usb_stats(int *count); 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 readonce); int usb_ftdi_cts(struct cgpu_info *cgpu); +int usb_ftdi_ctw(struct cgpu_info *cgpu); 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, 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);