Implement avalon_ready and avalon_wait_ready functions for when usb is ready to receive commands.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
diff --git a/driver-avalon.c b/driver-avalon.c
index 34b20c8..defa675 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 c789274..03c89d7 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 50a9d97..6f61e33 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 f1ab43a..22781af 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);