usbutils optional read buffering
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 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298
diff --git a/usbutils.c b/usbutils.c
index 9a7cca6..99d1d4c 100644
--- a/usbutils.c
+++ b/usbutils.c
@@ -1311,6 +1311,9 @@ 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;
@@ -2024,6 +2027,36 @@ static void rejected_inc(struct cgpu_info *cgpu, uint32_t mode)
}
#endif
+static char *find_end(unsigned char *buf, unsigned char *ptr, int ptrlen, int tot, char *end, int endlen, bool first)
+{
+ unsigned char *search;
+
+ if (endlen > tot)
+ return NULL;
+
+ // If end is only 1 char - do a faster search
+ if (endlen == 1) {
+ if (first)
+ search = buf;
+ else
+ search = ptr;
+
+ return strchr((char *)search, *end);
+ } else {
+ if (first)
+ search = buf;
+ else {
+ // must allow end to have been chopped in 2
+ if ((tot - ptrlen) >= (endlen - 1))
+ search = ptr - (endlen - 1);
+ else
+ search = ptr - (tot - ptrlen);
+ }
+
+ return strstr((char *)search, end);
+ }
+}
+
#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, __maybe_unused enum usb_cmds cmd, bool readonce)
@@ -2037,8 +2070,8 @@ int _usb_read(struct cgpu_info *cgpu, int ep, char *buf, size_t bufsiz, int *pro
unsigned int initial_timeout;
double max, done;
int bufleft, err, got, tot;
- __maybe_unused bool first = true;
- unsigned char *search;
+ bool first = true;
+ char *search;
int endlen;
// We add 4: 1 for null, 2 for FTDI status and 1 to round to 4 bytes
@@ -2064,19 +2097,34 @@ int _usb_read(struct cgpu_info *cgpu, int ep, char *buf, size_t bufsiz, int *pro
timeout = usbdev->found->timeout;
if (end == NULL) {
- tot = 0;
- ptr = usbbuf;
- bufleft = bufsiz;
+ if (usbdev->buffer && usbdev->bufamt) {
+ tot = usbdev->bufamt;
+ bufleft = bufsiz - 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) {
- if (ftdi)
- usbbufread = bufleft + 2;
- else
- usbbufread = bufleft;
+ // 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;
+
STATS_TIMEVAL(&tv_start);
err = libusb_bulk_transfer(usbdev->handle,
usbdev->found->eps[ep].ep,
@@ -2120,6 +2168,16 @@ int _usb_read(struct cgpu_info *cgpu, int ep, char *buf, size_t bufsiz, int *pro
break;
}
+ // N.B. usbdev->buffer was emptied before the while() loop
+ if (usbdev->buffer && tot > (int)bufsiz) {
+ usbdev->bufamt = tot - bufsiz;
+ memcpy(usbdev->buffer, ptr + bufsiz, usbdev->bufamt);
+ tot -= usbdev->bufamt;
+ usbbuf[tot] = '\0';
+ applog(LOG_ERR, "USB: %s%i read1 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);
@@ -2129,19 +2187,33 @@ int _usb_read(struct cgpu_info *cgpu, int ep, char *buf, size_t bufsiz, int *pro
return err;
}
- tot = 0;
- ptr = usbbuf;
- bufleft = bufsiz;
+ if (usbdev->buffer && usbdev->bufamt) {
+ tot = usbdev->bufamt;
+ bufleft = bufsiz - 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) {
- if (ftdi)
- usbbufread = bufleft + 2;
- else
- usbbufread = bufleft;
+ // 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;
STATS_TIMEVAL(&tv_start);
err = libusb_bulk_transfer(usbdev->handle,
@@ -2172,23 +2244,8 @@ int _usb_read(struct cgpu_info *cgpu, int ep, char *buf, size_t bufsiz, int *pro
if (err || readonce)
break;
- // WARNING - this will return data past END ('if' there is extra data)
- if (endlen <= tot) {
- // If END is only 1 char - do a faster search
- if (endlen == 1) {
- if (strchr((char *)ptr, *end))
- break;
- } else {
- // must allow END to have been chopped in 2 transfers
- if ((tot - got) >= (endlen - 1))
- search = ptr - (endlen - 1);
- else
- search = ptr - (tot - got);
-
- if (strstr((char *)search, end))
- break;
- }
- }
+ if (find_end(usbbuf, ptr, got, tot, (char *)end, endlen, first))
+ break;
ptr += got;
bufleft -= got;
@@ -2204,6 +2261,38 @@ int _usb_read(struct cgpu_info *cgpu, int ep, char *buf, size_t bufsiz, int *pro
break;
}
+ if (usbdev->buffer) {
+ bool dobuffer = false;
+
+ if ((search = find_end(usbbuf, usbbuf, tot, tot, (char *)end, endlen, true))) {
+ // end finishes after bufsiz
+ if ((search + endlen - (char *)usbbuf) > (int)bufsiz) {
+ usbdev->bufamt = tot - bufsiz;
+ dobuffer = true;
+ } else {
+ // extra data after end
+ if (*(search + endlen)) {
+ usbdev->bufamt = tot - (search + endlen - (char *)usbbuf);
+ dobuffer = true;
+ }
+ }
+ } else {
+ // no end, but still bigger than bufsiz
+ if (tot > (int)bufsiz) {
+ usbdev->bufamt = tot - bufsiz;
+ dobuffer = true;
+ }
+ }
+
+ if (dobuffer) {
+ tot -= usbdev->bufamt;
+ memcpy(usbdev->buffer, usbbuf + tot, usbdev->bufamt);
+ usbbuf[tot] = '\0';
+ applog(LOG_ERR, "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);
@@ -2400,6 +2489,45 @@ int usb_ftdi_cts(struct cgpu_info *cgpu)
return (ret & FTDI_RS0_CTS);
}
+void usb_buffer_enable(struct cgpu_info *cgpu)
+{
+ struct cg_usb_device *cgusb = cgpu->usbdev;
+
+ if (!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;
+ }
+}
+
+void usb_buffer_disable(struct cgpu_info *cgpu)
+{
+ struct cg_usb_device *cgusb = cgpu->usbdev;
+
+ if (cgusb->buffer) {
+ cgusb->bufamt = 0;
+ cgusb->bufsiz = 0;
+ free(cgusb->buffer);
+ }
+}
+
+void usb_buffer_clear(struct cgpu_info *cgpu)
+{
+ struct cg_usb_device *cgusb = cgpu->usbdev;
+
+ cgusb->bufamt = 0;
+}
+
+uint32_t usb_buffer_size(struct cgpu_info *cgpu)
+{
+ struct cg_usb_device *cgusb = cgpu->usbdev;
+
+ return cgusb->bufamt;
+}
+
void usb_cleanup()
{
struct cgpu_info *cgpu;
diff --git a/usbutils.h b/usbutils.h
index 5ff90c2..71aa8fb 100644
--- a/usbutils.h
+++ b/usbutils.h
@@ -167,6 +167,9 @@ struct cg_usb_device {
char *serial_string;
unsigned char fwVersion; // ??
unsigned char interfaceVersion; // ??
+ char *buffer;
+ uint32_t bufsiz;
+ uint32_t bufamt;
};
struct cg_usb_info {
@@ -258,6 +261,10 @@ 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);
+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_cleanup();
void usb_initialise();
void *usb_resource_thread(void *userdata);