Merge pull request #464 from kanoi/master usbutils changes
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 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196
diff --git a/usbutils.c b/usbutils.c
index f42c7d4..25e8f02 100644
--- a/usbutils.c
+++ b/usbutils.c
@@ -402,12 +402,9 @@ struct resource_reply {
// Replies to lock requests
struct resource_reply *res_reply_head = NULL;
-// Set this to 0 to remove stats processing
-#define DO_USB_STATS 1
-
-static bool stats_initialised = false;
-
-#if DO_USB_STATS
+// Some stats need to always be defined
+#define SEQ0 0
+#define SEQ1 1
// NONE must be 0 - calloced
#define MODE_NONE 0
@@ -416,6 +413,13 @@ static bool stats_initialised = false;
#define MODE_BULK_READ (1 << 2)
#define MODE_BULK_WRITE (1 << 3)
+// Set this to 0 to remove stats processing
+#define DO_USB_STATS 1
+
+static bool stats_initialised = false;
+
+#if DO_USB_STATS
+
#define MODE_SEP_STR "+"
#define MODE_NONE_STR "X"
#define MODE_CTRL_READ_STR "cr"
@@ -451,9 +455,6 @@ struct cg_usb_stats {
struct cg_usb_stats_details *details;
};
-#define SEQ0 0
-#define SEQ1 1
-
static struct cg_usb_stats *usb_stats = NULL;
static int next_stat = USB_NOSTAT;
@@ -2208,10 +2209,14 @@ static int
usb_bulk_transfer(struct libusb_device_handle *dev_handle,
unsigned char endpoint, unsigned char *data, int length,
int *transferred, unsigned int timeout,
- struct cgpu_info *cgpu, enum usb_cmds cmd)
+ struct cgpu_info *cgpu, __maybe_unused int mode,
+ enum usb_cmds cmd, __maybe_unused int seq)
{
uint16_t MaxPacketSize;
int err, errn, tries = 0;
+#if DO_USB_STATS
+ struct timeval tv_start, tv_finish;
+#endif
/* Limit length of transfer to the largest this descriptor supports
* and leave the higher level functions to transfer more if needed. */
@@ -2222,11 +2227,14 @@ usb_bulk_transfer(struct libusb_device_handle *dev_handle,
if (length > MaxPacketSize)
length = MaxPacketSize;
+ STATS_TIMEVAL(&tv_start);
cg_rlock(&cgusb_fd_lock);
err = libusb_bulk_transfer(dev_handle, endpoint, data, length,
transferred, timeout);
errn = errno;
cg_runlock(&cgusb_fd_lock);
+ STATS_TIMEVAL(&tv_finish);
+ USB_STATS(cgpu, &tv_start, &tv_finish, err, mode, cmd, seq);
if (err < 0)
applog(LOG_DEBUG, "%s%i: %s (amt=%d err=%d ern=%d)",
@@ -2246,11 +2254,14 @@ usb_bulk_transfer(struct libusb_device_handle *dev_handle,
break;
}
+ STATS_TIMEVAL(&tv_start);
cg_rlock(&cgusb_fd_lock);
err = libusb_bulk_transfer(dev_handle, endpoint, data,
length, transferred, timeout);
errn = errno;
cg_runlock(&cgusb_fd_lock);
+ STATS_TIMEVAL(&tv_finish);
+ USB_STATS(cgpu, &tv_start, &tv_finish, err, mode, cmd, seq);
if (err < 0)
applog(LOG_DEBUG, "%s%i: %s (amt=%d err=%d ern=%d)",
@@ -2274,9 +2285,6 @@ int _usb_read(struct cgpu_info *cgpu, int ep, char *buf, size_t bufsiz, int *pro
{
struct cg_usb_device *usbdev;
bool ftdi;
-#if DO_USB_STATS
- struct timeval tv_start;
-#endif
struct timeval read_start, tv_finish;
unsigned int initial_timeout;
double max, done;
@@ -2284,9 +2292,7 @@ int _usb_read(struct cgpu_info *cgpu, int ep, char *buf, size_t bufsiz, int *pro
bool first = true;
char *search;
int endlen;
-
- // We add 4: 1 for null, 2 for FTDI status and 1 to round to 4 bytes
- unsigned char usbbuf[USB_MAX_READ+4], *ptr;
+ unsigned char *ptr, *usbbuf = cgpu->usbinfo.bulkbuf;
size_t usbbufread;
DEVLOCK(cgpu, pstate);
@@ -2358,14 +2364,11 @@ int _usb_read(struct cgpu_info *cgpu, int ep, char *buf, size_t bufsiz, int *pro
}
}
}
- STATS_TIMEVAL(&tv_start);
err = usb_bulk_transfer(usbdev->handle,
usbdev->found->eps[ep].ep,
ptr, usbbufread, &got, timeout,
- cgpu, cmd);
+ cgpu, MODE_BULK_READ, cmd, first ? SEQ0 : SEQ1);
cgtime(&tv_finish);
- USB_STATS(cgpu, &tv_start, &tv_finish, err,
- MODE_BULK_READ, cmd, first ? SEQ0 : SEQ1);
ptr[got] = '\0';
USBDEBUG("USB debug: @_usb_read(%s (nodev=%s)) first=%s err=%d%s got=%d ptr='%s' usbbufread=%zu", cgpu->drv->name, bool_str(cgpu->usbinfo.nodev), bool_str(first), err, isnodev(err), got, (char *)str_text((char *)ptr), usbbufread);
@@ -2467,14 +2470,11 @@ int _usb_read(struct cgpu_info *cgpu, int ep, char *buf, size_t bufsiz, int *pro
}
}
}
- STATS_TIMEVAL(&tv_start);
err = usb_bulk_transfer(usbdev->handle,
usbdev->found->eps[ep].ep, ptr,
usbbufread, &got, timeout,
- cgpu, cmd);
+ cgpu, MODE_BULK_READ, cmd, first ? SEQ0 : SEQ1);
cgtime(&tv_finish);
- USB_STATS(cgpu, &tv_start, &tv_finish, err,
- MODE_BULK_READ, cmd, first ? SEQ0 : SEQ1);
ptr[got] = '\0';
USBDEBUG("USB debug: @_usb_read(%s (nodev=%s)) first=%s err=%d%s got=%d ptr='%s' usbbufread=%zu", cgpu->drv->name, bool_str(cgpu->usbinfo.nodev), bool_str(first), err, isnodev(err), got, (char *)str_text((char *)ptr), usbbufread);
@@ -2561,9 +2561,6 @@ out_unlock:
int _usb_write(struct cgpu_info *cgpu, int ep, char *buf, size_t bufsiz, int *processed, unsigned int timeout, enum usb_cmds cmd)
{
struct cg_usb_device *usbdev;
-#if DO_USB_STATS
- struct timeval tv_start;
-#endif
struct timeval read_start, tv_finish;
unsigned int initial_timeout;
double max, done;
@@ -2615,14 +2612,11 @@ int _usb_write(struct cgpu_info *cgpu, int ep, char *buf, size_t bufsiz, int *pr
cgtime(&(usbdev->last_write_tv));
usbdev->last_write_siz = bufsiz;
}
- STATS_TIMEVAL(&tv_start);
err = usb_bulk_transfer(usbdev->handle,
usbdev->found->eps[ep].ep,
(unsigned char *)buf, bufsiz, &sent,
- timeout, cgpu, cmd);
+ timeout, cgpu, MODE_BULK_WRITE, cmd, first ? SEQ0 : SEQ1);
cgtime(&tv_finish);
- USB_STATS(cgpu, &tv_start, &tv_finish, err,
- MODE_BULK_WRITE, cmd, first ? SEQ0 : SEQ1);
USBDEBUG("USB debug: @_usb_write(%s (nodev=%s)) err=%d%s sent=%d", cgpu->drv->name, bool_str(cgpu->usbinfo.nodev), err, isnodev(err), sent);
diff --git a/usbutils.h b/usbutils.h
index 9f3c283..75f1e51 100644
--- a/usbutils.h
+++ b/usbutils.h
@@ -184,6 +184,8 @@ struct cg_usb_device {
#define USB_NOSTAT 0
+#define USB_MAX_READ 8192
+
struct cg_usb_info {
uint8_t bus_number;
uint8_t device_address;
@@ -215,6 +217,13 @@ struct cg_usb_info {
double total_read_delay;
uint64_t write_delay_count;
double total_write_delay;
+
+ /*
+ * We add 4: 1 for null, 2 for FTDI status and 1 to round to 4 bytes
+ * If a single device ever has multiple end points then it will need
+ * multiple of these
+ */
+ unsigned char bulkbuf[USB_MAX_READ+4];
};
enum usb_cmds {