Always use a usb read buffer instead of having to explicitly enable it.
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 299 300 301
diff --git a/driver-avalon.c b/driver-avalon.c
index 0e6ab3b..796085a 100644
--- a/driver-avalon.c
+++ b/driver-avalon.c
@@ -780,7 +780,6 @@ static bool avalon_detect_one(libusb_device *dev, struct usb_find_devices *found
/* Even though this is an FTDI type chip, we want to do the parsing
* all ourselves so set it to std usb type */
avalon->usbdev->usb_type = USB_TYPE_STD;
- usb_buffer_enable(avalon);
/* We have a real Avalon! */
avalon_initialise(avalon);
diff --git a/driver-bflsc.c b/driver-bflsc.c
index a64e552..f3e45c4 100644
--- a/driver-bflsc.c
+++ b/driver-bflsc.c
@@ -888,8 +888,6 @@ reinit:
mutex_init(&bflsc->device_mutex);
rwlock_init(&sc_info->stat_lock);
- usb_buffer_enable(bflsc);
-
return true;
unshin:
diff --git a/driver-bitfury.c b/driver-bitfury.c
index 3fdfd88..69f3aca 100644
--- a/driver-bitfury.c
+++ b/driver-bitfury.c
@@ -146,8 +146,6 @@ static bool bitfury_detect_one(struct libusb_device *dev, struct usb_find_device
* hashrate to adapt quickly on starting. */
info->total_nonces = 1;
- usb_buffer_enable(bitfury);
-
if (!bitfury_open(bitfury))
goto out_close;
diff --git a/driver-hashfast.c b/driver-hashfast.c
index c4ce1c1..7549a83 100644
--- a/driver-hashfast.c
+++ b/driver-hashfast.c
@@ -347,7 +347,6 @@ static bool hfa_initialise(struct cgpu_info *hashfast)
if (hashfast->usbinfo.nodev)
return false;
- usb_buffer_enable(hashfast);
hfa_clear_readbuf(hashfast);
err = usb_transfer(hashfast, 0, 9, 1, 0, C_ATMEL_RESET);
@@ -916,7 +915,6 @@ static void hfa_shutdown(struct thr_info *thr)
pthread_join(info->read_thr, NULL);
hfa_free_all_work(info);
hfa_clear_readbuf(hashfast);
- usb_buffer_disable(hashfast);
free(info->works);
free(info->die_statistics);
free(info->die_status);
diff --git a/driver-icarus.c b/driver-icarus.c
index 64c1fd1..bf53073 100644
--- a/driver-icarus.c
+++ b/driver-icarus.c
@@ -822,8 +822,6 @@ static bool icarus_detect_one(struct libusb_device *dev, struct usb_find_devices
if (!usb_init(icarus, dev, found))
goto shin;
- usb_buffer_enable(icarus);
-
get_options(this_option_offset, icarus, &baud, &work_division, &fpga_count);
hex2bin((void *)(&workdata), golden_ob, sizeof(workdata));
diff --git a/usbutils.c b/usbutils.c
index 227dad2..edd89bd 100644
--- a/usbutils.c
+++ b/usbutils.c
@@ -1309,9 +1309,6 @@ 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;
@@ -2503,6 +2500,7 @@ int _usb_read(struct cgpu_info *cgpu, int intinfo, int epinfo, char *buf, size_t
double max, done;
int bufleft, err, got, tot, pstate;
bool first = true;
+ bool dobuffer;
char *search;
int endlen;
unsigned char *ptr, *usbbuf = cgpu->usbinfo.bulkbuf;
@@ -2531,17 +2529,12 @@ int _usb_read(struct cgpu_info *cgpu, int intinfo, int epinfo, char *buf, size_t
timeout = usbdev->found->timeout;
if (end == NULL) {
- if (usbdev->buffer && usbdev->bufamt) {
- tot = usbdev->bufamt;
- bufleft = bufsiz - tot;
+ tot = usbdev->bufamt;
+ bufleft = bufsiz - tot;
+ if (tot)
memcpy(usbbuf, usbdev->buffer, tot);
- ptr = usbbuf + tot;
- usbdev->bufamt = 0;
- } else {
- tot = 0;
- bufleft = bufsiz;
- ptr = usbbuf;
- }
+ ptr = usbbuf + tot;
+ usbdev->bufamt = 0;
err = LIBUSB_SUCCESS;
initial_timeout = timeout;
@@ -2592,7 +2585,7 @@ int _usb_read(struct cgpu_info *cgpu, int intinfo, int epinfo, char *buf, size_t
}
// N.B. usbdev->buffer was emptied before the while() loop
- if (usbdev->buffer && tot > (int)bufsiz) {
+ if (tot > (int)bufsiz) {
usbdev->bufamt = tot - bufsiz;
memcpy(usbdev->buffer, usbbuf + bufsiz, usbdev->bufamt);
tot -= usbdev->bufamt;
@@ -2607,23 +2600,19 @@ int _usb_read(struct cgpu_info *cgpu, int intinfo, int epinfo, char *buf, size_t
goto out_unlock;
}
- if (usbdev->buffer && usbdev->bufamt) {
- tot = usbdev->bufamt;
- bufleft = bufsiz - tot;
+ tot = usbdev->bufamt;
+ bufleft = bufsiz - tot;
+ if (tot)
memcpy(usbbuf, usbdev->buffer, tot);
- ptr = usbbuf + tot;
- usbdev->bufamt = 0;
- } else {
- tot = 0;
- bufleft = bufsiz;
- ptr = usbbuf;
- }
+ ptr = usbbuf + tot;
+ usbdev->bufamt = 0;
endlen = strlen(end);
err = LIBUSB_SUCCESS;
initial_timeout = timeout;
max = ((double)timeout) / 1000.0;
cgtime(&read_start);
+
while (bufleft > 0) {
got = 0;
err = usb_bulk_transfer(usbdev->handle, intinfo, epinfo,
@@ -2670,38 +2659,36 @@ int _usb_read(struct cgpu_info *cgpu, int intinfo, int epinfo, char *buf, size_t
break;
}
- if (usbdev->buffer) {
- bool dobuffer = false;
+ 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;
- }
- }
+ 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 {
- // no end, but still bigger than bufsiz
- if (tot > (int)bufsiz) {
- usbdev->bufamt = tot - bufsiz;
+ // extra data after end
+ if (*(search + endlen)) {
+ usbdev->bufamt = tot - (search + endlen - (char *)usbbuf);
dobuffer = true;
}
}
-
- if (dobuffer) {
- tot -= usbdev->bufamt;
- memcpy(usbdev->buffer, usbbuf + tot, usbdev->bufamt);
- usbbuf[tot] = '\0';
- applog(LOG_DEBUG, "USB: %s%i read2 buffering %d extra bytes",
- cgpu->drv->name, cgpu->device_id, usbdev->bufamt);
+ } 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_DEBUG, "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);
@@ -3031,44 +3018,6 @@ int _usb_ftdi_set_latency(struct cgpu_info *cgpu, int intinfo)
return err;
}
-void usb_buffer_enable(struct cgpu_info *cgpu)
-{
- struct cg_usb_device *cgusb;
- int pstate;
-
- DEVWLOCK(cgpu, pstate);
-
- cgusb = cgpu->usbdev;
- if (cgusb && !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;
- }
-
- DEVWUNLOCK(cgpu, pstate);
-}
-
-void usb_buffer_disable(struct cgpu_info *cgpu)
-{
- struct cg_usb_device *cgusb;
- int pstate;
-
- DEVWLOCK(cgpu, pstate);
-
- cgusb = cgpu->usbdev;
- if (cgusb && cgusb->buffer) {
- cgusb->bufamt = 0;
- cgusb->bufsiz = 0;
- free(cgusb->buffer);
- cgusb->buffer = NULL;
- }
-
- DEVWUNLOCK(cgpu, pstate);
-}
-
void usb_buffer_clear(struct cgpu_info *cgpu)
{
int pstate;
diff --git a/usbutils.h b/usbutils.h
index b3a96e9..f8f9003 100644
--- a/usbutils.h
+++ b/usbutils.h
@@ -181,6 +181,8 @@ enum usb_types {
USB_TYPE_FTDI
};
+#define USB_MAX_READ 8192
+
struct cg_usb_device {
struct usb_find_devices *found;
libusb_device_handle *handle;
@@ -196,7 +198,7 @@ struct cg_usb_device {
char *serial_string;
unsigned char fwVersion; // ??
unsigned char interfaceVersion; // ??
- char *buffer;
+ char buffer[USB_MAX_READ];
uint32_t bufsiz;
uint32_t bufamt;
cgtimer_t cgt_last_write;
@@ -205,8 +207,6 @@ struct cg_usb_device {
#define USB_NOSTAT 0
-#define USB_MAX_READ 8192
-
#define USB_TMO_0 50
#define USB_TMO_1 100
#define USB_TMO_2 500
@@ -394,8 +394,6 @@ int _usb_transfer_read(struct cgpu_info *cgpu, uint8_t request_type, uint8_t bRe
int usb_ftdi_cts(struct cgpu_info *cgpu);
int _usb_ftdi_set_latency(struct cgpu_info *cgpu, int intinfo);
#define usb_ftdi_set_latency(_cgpu) _usb_ftdi_set_latency(_cgpu, DEFAULT_INTINFO)
-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_set_cps(struct cgpu_info *cgpu, int cps);