Commit 38164d637d16bb1e1eb3d7cf2640928be4cd2ed9

Con Kolivas 2013-11-04T08:54:31

Merge branch 'icarushit'

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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
diff --git a/driver-avalon.c b/driver-avalon.c
index 75cd06e..796085a 100644
--- a/driver-avalon.c
+++ b/driver-avalon.c
@@ -780,8 +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_set_pps(avalon, AVALON_USB_PACKETSIZE);
-	usb_buffer_enable(avalon);
 
 	/* We have a real Avalon! */
 	avalon_initialise(avalon);
diff --git a/driver-avalon.h b/driver-avalon.h
index 063fb68..e84ddbb 100644
--- a/driver-avalon.h
+++ b/driver-avalon.h
@@ -64,7 +64,6 @@
 #define AVALON_AUTO_CYCLE 1024
 
 #define AVALON_FTDI_READSIZE 510
-#define AVALON_USB_PACKETSIZE 512
 #define AVALON_READBUF_SIZE 8192
 #define AVALON_RESET_TIMEOUT 100
 #define AVALON_READ_TIMEOUT 18 /* Enough to only half fill the buffer */
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 43a460e..bf53073 100644
--- a/driver-icarus.c
+++ b/driver-icarus.c
@@ -55,10 +55,6 @@
 // The size of a successful nonce read
 #define ICARUS_READ_SIZE 4
 
-#define AMU_PREF_PACKET 256
-#define BLT_PREF_PACKET 512
-#define ICA_PREF_PACKET 256
-
 // Ensure the sizes are correct for the Serial read
 #if (ICARUS_READ_SIZE != 4)
 #error ICARUS_READ_SIZE must be 4
@@ -329,8 +325,6 @@ static void icarus_initialise(struct cgpu_info *icarus, int baud)
 		case IDENT_LLT:
 		case IDENT_CMR1:
 		case IDENT_CMR2:
-			usb_set_pps(icarus, BLT_PREF_PACKET);
-
 			// Reset
 			transfer(icarus, FTDI_TYPE_OUT, FTDI_REQUEST_RESET, FTDI_VALUE_RESET,
 				 interface, C_RESET);
@@ -404,8 +398,6 @@ static void icarus_initialise(struct cgpu_info *icarus, int baud)
 				 interface, C_PURGERX);
 			break;
 		case IDENT_ICA:
-			usb_set_pps(icarus, ICA_PREF_PACKET);
-
 			// Set Data Control
 			transfer(icarus, PL2303_CTRL_OUT, PL2303_REQUEST_CTRL, PL2303_VALUE_CTRL,
 				 interface, C_SETDATA);
@@ -426,8 +418,6 @@ static void icarus_initialise(struct cgpu_info *icarus, int baud)
 				 interface, C_VENDOR);
 			break;
 		case IDENT_AMU:
-			usb_set_pps(icarus, AMU_PREF_PACKET);
-
 			// Enable the UART
 			transfer(icarus, CP210X_TYPE_OUT, CP210X_REQUEST_IFC_ENABLE,
 				 CP210X_VALUE_UART_ENABLE,
@@ -832,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 cd4918c..edd89bd 100644
--- a/usbutils.c
+++ b/usbutils.c
@@ -103,8 +103,8 @@ static struct list_head ut_list;
 #ifdef USE_BFLSC
 // N.B. transfer size is 512 with USB2.0, but only 64 with USB1.1
 static struct usb_epinfo bas_epinfos[] = {
-	{ LIBUSB_TRANSFER_TYPE_BULK,	64,	EPI(1), 0, 0, 0 },
-	{ LIBUSB_TRANSFER_TYPE_BULK,	64,	EPO(2), 0, 0, 0 }
+	{ LIBUSB_TRANSFER_TYPE_BULK,	64,	EPI(1), 0, 0 },
+	{ LIBUSB_TRANSFER_TYPE_BULK,	64,	EPO(2), 0, 0 }
 };
 
 static struct usb_intinfo bas_ints[] = {
@@ -115,8 +115,8 @@ static struct usb_intinfo bas_ints[] = {
 #ifdef USE_BITFORCE
 // N.B. transfer size is 512 with USB2.0, but only 64 with USB1.1
 static struct usb_epinfo bfl_epinfos[] = {
-	{ LIBUSB_TRANSFER_TYPE_BULK,	64,	EPI(1), 0, 0, 0 },
-	{ LIBUSB_TRANSFER_TYPE_BULK,	64,	EPO(2), 0, 0, 0 }
+	{ LIBUSB_TRANSFER_TYPE_BULK,	64,	EPI(1), 0, 0 },
+	{ LIBUSB_TRANSFER_TYPE_BULK,	64,	EPO(2), 0, 0 }
 };
 
 static struct usb_intinfo bfl_ints[] = {
@@ -126,12 +126,12 @@ static struct usb_intinfo bfl_ints[] = {
 
 #ifdef USE_BITFURY
 static struct usb_epinfo bfu0_epinfos[] = {
-	{ LIBUSB_TRANSFER_TYPE_INTERRUPT,	8,	EPI(2), 0, 0, 0 }
+	{ LIBUSB_TRANSFER_TYPE_INTERRUPT,	8,	EPI(2), 0, 0 }
 };
 
 static struct usb_epinfo bfu1_epinfos[] = {
-	{ LIBUSB_TRANSFER_TYPE_BULK,	16,	EPI(3), 0, 0, 0 },
-	{ LIBUSB_TRANSFER_TYPE_BULK,	16,	EPO(4), 0, 0, 0 }
+	{ LIBUSB_TRANSFER_TYPE_BULK,	16,	EPI(3), 0, 0 },
+	{ LIBUSB_TRANSFER_TYPE_BULK,	16,	EPO(4), 0, 0 }
 };
 
 /* Default to interface 1 */
@@ -145,12 +145,12 @@ static struct usb_intinfo bfu_ints[] = {
 #include "driver-hashfast.h"
 
 static struct usb_epinfo hfa0_epinfos[] = {
-	{ LIBUSB_TRANSFER_TYPE_INTERRUPT,	8,	EPI(3), 0, 0, 0 }
+	{ LIBUSB_TRANSFER_TYPE_INTERRUPT,	8,	EPI(3), 0, 0 }
 };
 
 static struct usb_epinfo hfa1_epinfos[] = {
-	{ LIBUSB_TRANSFER_TYPE_BULK,	64,	EPI(1), 0, 0, 0 },
-	{ LIBUSB_TRANSFER_TYPE_BULK,	64,	EPO(2), 0, 0, 0 }
+	{ LIBUSB_TRANSFER_TYPE_BULK,	64,	EPI(1), 0, 0 },
+	{ LIBUSB_TRANSFER_TYPE_BULK,	64,	EPO(2), 0, 0 }
 };
 
 /* Default to interface 1 */
@@ -162,8 +162,8 @@ static struct usb_intinfo hfa_ints[] = {
 
 #ifdef USE_MODMINER
 static struct usb_epinfo mmq_epinfos[] = {
-	{ LIBUSB_TRANSFER_TYPE_BULK,	64,	EPI(3), 0, 0, 0 },
-	{ LIBUSB_TRANSFER_TYPE_BULK,	64,	EPO(3), 0, 0, 0 }
+	{ LIBUSB_TRANSFER_TYPE_BULK,	64,	EPI(3), 0, 0 },
+	{ LIBUSB_TRANSFER_TYPE_BULK,	64,	EPO(3), 0, 0 }
 };
 
 static struct usb_intinfo mmq_ints[] = {
@@ -173,8 +173,8 @@ static struct usb_intinfo mmq_ints[] = {
 
 #ifdef USE_AVALON
 static struct usb_epinfo ava_epinfos[] = {
-	{ LIBUSB_TRANSFER_TYPE_BULK,	64,	EPI(1), 0, 0, 0 },
-	{ LIBUSB_TRANSFER_TYPE_BULK,	64,	EPO(2), 0, 0, 0 }
+	{ LIBUSB_TRANSFER_TYPE_BULK,	64,	EPI(1), 0, 0 },
+	{ LIBUSB_TRANSFER_TYPE_BULK,	64,	EPO(2), 0, 0 }
 };
 
 static struct usb_intinfo ava_ints[] = {
@@ -184,8 +184,8 @@ static struct usb_intinfo ava_ints[] = {
 
 #ifdef USE_KLONDIKE
 static struct usb_epinfo kln_epinfos[] = {
-	{ LIBUSB_TRANSFER_TYPE_BULK,	64,	EPI(1), 0, 0, 0 },
-	{ LIBUSB_TRANSFER_TYPE_BULK,	64,	EPO(1), 0, 0, 0 }
+	{ LIBUSB_TRANSFER_TYPE_BULK,	64,	EPI(1), 0, 0 },
+	{ LIBUSB_TRANSFER_TYPE_BULK,	64,	EPO(1), 0, 0 }
 };
 
 static struct usb_intinfo kln_ints[] = {
@@ -195,8 +195,8 @@ static struct usb_intinfo kln_ints[] = {
 
 #ifdef USE_ICARUS
 static struct usb_epinfo ica_epinfos[] = {
-	{ LIBUSB_TRANSFER_TYPE_BULK,	64,	EPI(3), 0, 0, 0 },
-	{ LIBUSB_TRANSFER_TYPE_BULK,	64,	EPO(2), 0, 0, 0 }
+	{ LIBUSB_TRANSFER_TYPE_BULK,	64,	EPI(3), 0, 0 },
+	{ LIBUSB_TRANSFER_TYPE_BULK,	64,	EPO(2), 0, 0 }
 };
 
 static struct usb_intinfo ica_ints[] = {
@@ -204,8 +204,8 @@ static struct usb_intinfo ica_ints[] = {
 };
 
 static struct usb_epinfo amu_epinfos[] = {
-	{ LIBUSB_TRANSFER_TYPE_BULK,	64,	EPI(1), 0, 0, 0 },
-	{ LIBUSB_TRANSFER_TYPE_BULK,	64,	EPO(1), 0, 0, 0 }
+	{ LIBUSB_TRANSFER_TYPE_BULK,	64,	EPI(1), 0, 0 },
+	{ LIBUSB_TRANSFER_TYPE_BULK,	64,	EPO(1), 0, 0 }
 };
 
 static struct usb_intinfo amu_ints[] = {
@@ -213,8 +213,8 @@ static struct usb_intinfo amu_ints[] = {
 };
 
 static struct usb_epinfo llt_epinfos[] = {
-	{ LIBUSB_TRANSFER_TYPE_BULK,	64,	EPI(1), 0, 0, 0 },
-	{ LIBUSB_TRANSFER_TYPE_BULK,	64,	EPO(2), 0, 0, 0 }
+	{ LIBUSB_TRANSFER_TYPE_BULK,	64,	EPI(1), 0, 0 },
+	{ LIBUSB_TRANSFER_TYPE_BULK,	64,	EPO(2), 0, 0 }
 };
 
 static struct usb_intinfo llt_ints[] = {
@@ -222,8 +222,8 @@ static struct usb_intinfo llt_ints[] = {
 };
 
 static struct usb_epinfo cmr1_epinfos[] = {
-	{ LIBUSB_TRANSFER_TYPE_BULK,	64,	EPI(1), 0, 0, 0 },
-	{ LIBUSB_TRANSFER_TYPE_BULK,	64,	EPO(2), 0, 0, 0 }
+	{ LIBUSB_TRANSFER_TYPE_BULK,	64,	EPI(1), 0, 0 },
+	{ LIBUSB_TRANSFER_TYPE_BULK,	64,	EPO(2), 0, 0 }
 };
 
 static struct usb_intinfo cmr1_ints[] = {
@@ -231,20 +231,20 @@ static struct usb_intinfo cmr1_ints[] = {
 };
 
 static struct usb_epinfo cmr2_epinfos0[] = {
-	{ LIBUSB_TRANSFER_TYPE_BULK,	64,	EPI(1), 0, 0, 0 },
-	{ LIBUSB_TRANSFER_TYPE_BULK,	64,	EPO(2), 0, 0, 0 }
+	{ LIBUSB_TRANSFER_TYPE_BULK,	64,	EPI(1), 0, 0 },
+	{ LIBUSB_TRANSFER_TYPE_BULK,	64,	EPO(2), 0, 0 }
 };
 static struct usb_epinfo cmr2_epinfos1[] = {
-	{ LIBUSB_TRANSFER_TYPE_BULK,	64,	EPI(3), 0, 0, 0 },
-	{ LIBUSB_TRANSFER_TYPE_BULK,	64,	EPO(4), 0, 0, 0 },
+	{ LIBUSB_TRANSFER_TYPE_BULK,	64,	EPI(3), 0, 0 },
+	{ LIBUSB_TRANSFER_TYPE_BULK,	64,	EPO(4), 0, 0 },
 };
 static struct usb_epinfo cmr2_epinfos2[] = {
-	{ LIBUSB_TRANSFER_TYPE_BULK,	64,	EPI(5), 0, 0, 0 },
-	{ LIBUSB_TRANSFER_TYPE_BULK,	64,	EPO(6), 0, 0, 0 },
+	{ LIBUSB_TRANSFER_TYPE_BULK,	64,	EPI(5), 0, 0 },
+	{ LIBUSB_TRANSFER_TYPE_BULK,	64,	EPO(6), 0, 0 },
 };
 static struct usb_epinfo cmr2_epinfos3[] = {
-	{ LIBUSB_TRANSFER_TYPE_BULK,	64,	EPI(7), 0, 0, 0 },
-	{ LIBUSB_TRANSFER_TYPE_BULK,	64,	EPO(8), 0, 0, 0 }
+	{ LIBUSB_TRANSFER_TYPE_BULK,	64,	EPI(7), 0, 0 },
+	{ LIBUSB_TRANSFER_TYPE_BULK,	64,	EPO(8), 0, 0 }
 };
 
 static struct usb_intinfo cmr2_ints[] = {
@@ -466,6 +466,15 @@ static const char *nodatareturned = "no data returned ";
 			cgpu->usbinfo.continuous_ioerr_count = 0; \
 		}
 
+/* Timeout errors on writes are unusual and should be treated as IO errors. */
+#define WRITEIOERR_CHECK(cgpu, err) \
+		if (err == LIBUSB_ERROR_IO || err == LIBUSB_ERROR_TIMEOUT) { \
+			cgpu->usbinfo.ioerr_count++; \
+			cgpu->usbinfo.continuous_ioerr_count++; \
+		} else { \
+			cgpu->usbinfo.continuous_ioerr_count = 0; \
+		}
+
 #if 0 // enable USBDEBUG - only during development testing
  static const char *debug_true_str = "true";
  static const char *debug_false_str = "false";
@@ -1300,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;
@@ -2332,6 +2338,9 @@ static void LIBUSB_CALL transfer_callback(struct libusb_transfer *transfer)
 
 static int usb_transfer_toerr(int ret)
 {
+	if (ret <= 0)
+		return ret;
+
 	switch (ret) {
 		default:
 		case LIBUSB_TRANSFER_COMPLETED:
@@ -2405,15 +2414,24 @@ usb_bulk_transfer(struct libusb_device_handle *dev_handle, int intinfo,
 		  struct cgpu_info *cgpu, __maybe_unused int mode,
 		  enum usb_cmds cmd, __maybe_unused int seq, bool cancellable)
 {
+	int bulk_timeout, callback_timeout = timeout;
 	struct usb_epinfo *usb_epinfo;
 	struct usb_transfer ut;
 	unsigned char endpoint;
-	uint16_t MaxPacketSize;
 	int err, errn;
 #if DO_USB_STATS
 	struct timeval tv_start, tv_finish;
 #endif
 	unsigned char buf[512];
+#ifdef WIN32
+	/* On windows the callback_timeout is a safety mechanism only. */
+	bulk_timeout = timeout;
+	callback_timeout += timeout + cgpu->usbdev->found->timeout;
+#else
+	/* We give the transfer no timeout since we manage timeouts ourself on
+	 * non windows. */
+	bulk_timeout = 0;
+#endif
 
 	usb_epinfo = &(cgpu->usbdev->found->intinfos[intinfo].epinfos[epinfo]);
 	endpoint = usb_epinfo->ep;
@@ -2423,37 +2441,33 @@ usb_bulk_transfer(struct libusb_device_handle *dev_handle, int intinfo,
 	if (unlikely(cgpu->shutdown))
 		return libusb_bulk_transfer(dev_handle, endpoint, data, length, transferred, timeout);
 
-	/* Limit length of transfer to the largest this descriptor supports
-	 * and leave the higher level functions to transfer more if needed. */
-	if (usb_epinfo->PrefPacketSize)
-		MaxPacketSize = usb_epinfo->PrefPacketSize;
-	else
-		MaxPacketSize = usb_epinfo->wMaxPacketSize;
-	if (length > MaxPacketSize)
-		length = MaxPacketSize;
+	if (length > usb_epinfo->wMaxPacketSize)
+		length = usb_epinfo->wMaxPacketSize;
 	if ((endpoint & LIBUSB_ENDPOINT_DIR_MASK) == LIBUSB_ENDPOINT_OUT)
 		memcpy(buf, data, length);
 
 	USBDEBUG("USB debug: @usb_bulk_transfer(%s (nodev=%s),intinfo=%d,epinfo=%d,data=%p,length=%d,timeout=%u,mode=%d,cmd=%s,seq=%d) endpoint=%d", cgpu->drv->name, bool_str(cgpu->usbinfo.nodev), intinfo, epinfo, data, length, timeout, mode, usb_cmdname(cmd), seq, (int)endpoint);
 
 	init_usb_transfer(&ut);
-	/* We give the transfer no timeout since we manage timeouts ourself */
 	libusb_fill_bulk_transfer(ut.transfer, dev_handle, endpoint, buf, length,
-				  transfer_callback, &ut, 0);
+				  transfer_callback, &ut, bulk_timeout);
 	STATS_TIMEVAL(&tv_start);
 	err = usb_submit_transfer(&ut, ut.transfer, cancellable);
 	errn = errno;
 	if (!err)
-		err = callback_wait(&ut, transferred, timeout);
+		err = callback_wait(&ut, transferred, callback_timeout);
+	else
+		err = usb_transfer_toerr(err);
 	complete_usb_transfer(&ut);
 
 	STATS_TIMEVAL(&tv_finish);
 	USB_STATS(cgpu, &tv_start, &tv_finish, err, mode, cmd, seq, timeout);
 
-	if (err < 0)
+	if (err < 0) {
 		applog(LOG_DEBUG, "%s%i: %s (amt=%d err=%d ern=%d)",
 				cgpu->drv->name, cgpu->device_id,
 				usb_cmdname(cmd), *transferred, err, errn);
+	}
 
 	if (err == LIBUSB_ERROR_PIPE) {
 		int retries = 0;
@@ -2472,12 +2486,12 @@ usb_bulk_transfer(struct libusb_device_handle *dev_handle, int intinfo,
 		} while (err && ++retries < USB_RETRY_MAX);
 	}
 	if ((endpoint & LIBUSB_ENDPOINT_DIR_MASK) == LIBUSB_ENDPOINT_IN)
-		memcpy(data, buf, length);
+		memcpy(data, buf, *transferred);
 
 	return err;
 }
 
-int _usb_read(struct cgpu_info *cgpu, int intinfo, int epinfo, char *buf, size_t bufsiz, int *processed, unsigned int timeout, const char *end, enum usb_cmds cmd, bool readonce, bool cancellable)
+int _usb_read(struct cgpu_info *cgpu, int intinfo, int epinfo, char *buf, size_t bufsiz, int *processed, int timeout, const char *end, enum usb_cmds cmd, bool readonce, bool cancellable)
 {
 	struct cg_usb_device *usbdev;
 	bool ftdi;
@@ -2486,10 +2500,11 @@ 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;
-	size_t usbbufread;
+	const size_t usbbufread = 512; /* Always read full size */
 
 	DEVRLOCK(cgpu, pstate);
 
@@ -2514,50 +2529,20 @@ 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;
 		max = ((double)timeout) / 1000.0;
 		cgtime(&read_start);
 		while (bufleft > 0) {
-			// 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;
 
-			if (first && usbdev->usecps && usbdev->last_write_siz) {
-				cgtimer_t now, already_done;
-				double sleep_estimate;
-				double write_time = (double)(usbdev->last_write_siz) /
-						    (double)(usbdev->cps) * 1000;
-
-				cgtimer_time(&now);
-				cgtimer_sub(&now, &usbdev->cgt_last_write, &already_done);
-				sleep_estimate = write_time - cgtimer_to_ms(&already_done);
-
-				if (sleep_estimate > 0.0) {
-					cgsleep_ms_r(&usbdev->cgt_last_write, write_time);
-					cgpu->usbinfo.read_delay_count++;
-					cgpu->usbinfo.total_read_delay += sleep_estimate;
-				}
-			}
 			err = usb_bulk_transfer(usbdev->handle, intinfo, epinfo,
 						ptr, usbbufread, &got, timeout,
 						cgpu, MODE_BULK_READ, cmd, first ? SEQ0 : SEQ1,
@@ -2595,12 +2580,12 @@ int _usb_read(struct cgpu_info *cgpu, int intinfo, int epinfo, char *buf, size_t
 			if (unlikely(done >= max))
 				break;
 			timeout = initial_timeout - (done * 1000);
-			if (!timeout)
+			if (timeout <= 0)
 				break;
 		}
 
 		// 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;
@@ -2615,50 +2600,21 @@ 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) {
-		// 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;
-		if (first && usbdev->usecps && usbdev->last_write_siz) {
-			cgtimer_t now, already_done;
-			double sleep_estimate;
-			double write_time = (double)(usbdev->last_write_siz) /
-					    (double)(usbdev->cps) * 1000;
-
-			cgtimer_time(&now);
-			cgtimer_sub(&now, &usbdev->cgt_last_write, &already_done);
-			sleep_estimate = write_time - cgtimer_to_ms(&already_done);
-
-			if (sleep_estimate > 0.0) {
-				cgsleep_ms_r(&usbdev->cgt_last_write, write_time);
-				cgpu->usbinfo.read_delay_count++;
-				cgpu->usbinfo.total_read_delay += sleep_estimate;
-			}
-		}
 		err = usb_bulk_transfer(usbdev->handle, intinfo, epinfo,
 					ptr, usbbufread, &got, timeout,
 					cgpu, MODE_BULK_READ, cmd, first ? SEQ0 : SEQ1,
@@ -2699,42 +2655,40 @@ int _usb_read(struct cgpu_info *cgpu, int intinfo, int epinfo, char *buf, size_t
 		if (unlikely(done >= max))
 			break;
 		timeout = initial_timeout - (done * 1000);
-		if (!timeout)
+		if (timeout <= 0)
 			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);
 
@@ -2756,7 +2710,7 @@ out_noerrmsg:
 	return err;
 }
 
-int _usb_write(struct cgpu_info *cgpu, int intinfo, int epinfo, char *buf, size_t bufsiz, int *processed, unsigned int timeout, enum usb_cmds cmd)
+int _usb_write(struct cgpu_info *cgpu, int intinfo, int epinfo, char *buf, size_t bufsiz, int *processed, int timeout, enum usb_cmds cmd)
 {
 	struct cg_usb_device *usbdev;
 	struct timeval read_start, tv_finish;
@@ -2788,29 +2742,15 @@ int _usb_write(struct cgpu_info *cgpu, int intinfo, int epinfo, char *buf, size_
 	max = ((double)timeout) / 1000.0;
 	cgtime(&read_start);
 	while (bufsiz > 0) {
-		sent = 0;
+		int tosend = bufsiz;
+
 		if (usbdev->usecps) {
-			if (usbdev->last_write_siz) {
-				cgtimer_t now, already_done;
-				double sleep_estimate;
-				double write_time = (double)(usbdev->last_write_siz) /
-						    (double)(usbdev->cps) * 1000;
-
-				cgtimer_time(&now);
-				cgtimer_sub(&now, &usbdev->cgt_last_write, &already_done);
-				sleep_estimate = write_time - cgtimer_to_ms(&already_done);
-
-				if (sleep_estimate > 0.0) {
-					cgsleep_ms_r(&usbdev->cgt_last_write, write_time);
-					cgpu->usbinfo.write_delay_count++;
-					cgpu->usbinfo.total_write_delay += sleep_estimate;
-				}
-			}
-			cgsleep_prepare_r(&usbdev->cgt_last_write);
-			usbdev->last_write_siz = bufsiz;
+			int cpms = usbdev->cps / 1000 ? : 1;
+			if (tosend > cpms)
+				tosend = cpms;
 		}
 		err = usb_bulk_transfer(usbdev->handle, intinfo, epinfo,
-					(unsigned char *)buf, bufsiz, &sent, timeout,
+					(unsigned char *)buf, tosend, &sent, timeout,
 					cgpu, MODE_BULK_WRITE, cmd, first ? SEQ0 : SEQ1,
 					false);
 		cgtime(&tv_finish);
@@ -2834,7 +2774,7 @@ int _usb_write(struct cgpu_info *cgpu, int intinfo, int epinfo, char *buf, size_
 		if (unlikely(done >= max))
 			break;
 		timeout = initial_timeout - (done * 1000);
-		if (!timeout)
+		if (timeout <= 0)
 			break;
 	}
 
@@ -2928,26 +2868,6 @@ int __usb_transfer(struct cgpu_info *cgpu, uint8_t request_type, uint8_t bReques
 
 	USBDEBUG("USB debug: @_usb_transfer() buf=%s", bin2hex(buf, (size_t)siz));
 
-	if (usbdev->usecps) {
-		if (usbdev->last_write_siz) {
-			cgtimer_t now, already_done;
-			double sleep_estimate;
-			double write_time = (double)(usbdev->last_write_siz) /
-					    (double)(usbdev->cps) * 1000;
-
-			cgtimer_time(&now);
-			cgtimer_sub(&now, &usbdev->cgt_last_write, &already_done);
-			sleep_estimate = write_time - cgtimer_to_ms(&already_done);
-
-			if (sleep_estimate > 0.0) {
-				cgsleep_ms_r(&usbdev->cgt_last_write, write_time);
-				cgpu->usbinfo.write_delay_count++;
-				cgpu->usbinfo.total_write_delay += sleep_estimate;
-			}
-		}
-		cgsleep_prepare_r(&usbdev->cgt_last_write);
-		usbdev->last_write_siz = siz;
-	}
 	STATS_TIMEVAL(&tv_start);
 	err = usb_control_transfer(cgpu, usbdev->handle, request_type, bRequest,
 				   wValue, wIndex, buf, (uint16_t)siz, timeout);
@@ -3011,22 +2931,6 @@ int _usb_transfer_read(struct cgpu_info *cgpu, uint8_t request_type, uint8_t bRe
 
 	*amount = 0;
 
-	if (usbdev->usecps && usbdev->last_write_siz) {
-		cgtimer_t now, already_done;
-		double sleep_estimate;
-		double write_time = (double)(usbdev->last_write_siz) /
-				    (double)(usbdev->cps) * 1000;
-
-		cgtimer_time(&now);
-		cgtimer_sub(&now, &usbdev->cgt_last_write, &already_done);
-		sleep_estimate = write_time - cgtimer_to_ms(&already_done);
-
-		if (sleep_estimate > 0.0) {
-			cgsleep_ms_r(&usbdev->cgt_last_write, write_time);
-			cgpu->usbinfo.read_delay_count++;
-			cgpu->usbinfo.total_read_delay += sleep_estimate;
-		}
-	}
 	memset(tbuf, 0, 64);
 	STATS_TIMEVAL(&tv_start);
 	err = usb_control_transfer(cgpu, usbdev->handle, request_type, bRequest,
@@ -3114,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;
@@ -3252,40 +3118,6 @@ enum sub_ident usb_ident(struct cgpu_info *cgpu)
 	return ident;
 }
 
-/*
- * If you pass both intinfo and epinfo as <0 then it will set all
- * endpoints to PrefPacketSize
- * If intinfo >=0 but epinfo <0 then it will set all endpoints
- * for the given one intinfo to PrefPacketSize
- * If both are >=0 then it will set only the specified single
- * endpoint (intinfo,epinfo) to PrefPacketSize
- */
-void _usb_set_pps(struct cgpu_info *cgpu, int intinfo, int epinfo, uint16_t PrefPacketSize)
-{
-	struct usb_find_devices *found;
-	int pstate;
-
-	DEVWLOCK(cgpu, pstate);
-
-	if (cgpu->usbdev) {
-		found = cgpu->usbdev->found;
-		if (intinfo >= 0 && epinfo >= 0)
-			found->intinfos[intinfo].epinfos[epinfo].PrefPacketSize = PrefPacketSize;
-		else {
-			if (intinfo >= 0) {
-				for (epinfo = 0; epinfo < found->intinfos[intinfo].epinfo_count; epinfo++)
-					found->intinfos[intinfo].epinfos[epinfo].PrefPacketSize = PrefPacketSize;
-			} else {
-				for (intinfo = 0; intinfo < found->intinfo_count ; intinfo++)
-					for (epinfo = 0; epinfo < found->intinfos[intinfo].epinfo_count; epinfo++)
-						found->intinfos[intinfo].epinfos[epinfo].PrefPacketSize = PrefPacketSize;
-			}
-		}
-	}
-
-	DEVWUNLOCK(cgpu, pstate);
-}
-
 // Need to set all devices with matching usbdev
 void usb_set_dev_start(struct cgpu_info *cgpu)
 {
diff --git a/usbutils.h b/usbutils.h
index 3ef1fa3..f8f9003 100644
--- a/usbutils.h
+++ b/usbutils.h
@@ -122,7 +122,6 @@ struct usb_epinfo {
 	uint16_t size;
 	unsigned char ep;
 	uint16_t wMaxPacketSize;
-	uint16_t PrefPacketSize;
 	bool found;
 };
 
@@ -182,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;
@@ -197,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;
@@ -206,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
@@ -388,15 +387,13 @@ bool usb_init(struct cgpu_info *cgpu, struct libusb_device *dev, struct usb_find
 void usb_detect(struct device_drv *drv, bool (*device_detect)(struct libusb_device *, struct usb_find_devices *));
 struct api_data *api_usb_stats(int *count);
 void update_usb_stats(struct cgpu_info *cgpu);
-int _usb_read(struct cgpu_info *cgpu, int intinfo, int epinfo, char *buf, size_t bufsiz, int *processed, unsigned int timeout, const char *end, enum usb_cmds cmd, bool readonce, bool cancellable);
-int _usb_write(struct cgpu_info *cgpu, int intinfo, int epinfo, char *buf, size_t bufsiz, int *processed, unsigned int timeout, enum usb_cmds);
+int _usb_read(struct cgpu_info *cgpu, int intinfo, int epinfo, char *buf, size_t bufsiz, int *processed, int timeout, const char *end, enum usb_cmds cmd, bool readonce, bool cancellable);
+int _usb_write(struct cgpu_info *cgpu, int intinfo, int epinfo, char *buf, size_t bufsiz, int *processed, 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);
 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);
@@ -405,8 +402,6 @@ void usb_disable_cps(struct cgpu_info *cgpu);
 int _usb_interface(struct cgpu_info *cgpu, int intinfo);
 #define usb_interface(_cgpu) _usb_interface(_cgpu, DEFAULT_INTINFO)
 enum sub_ident usb_ident(struct cgpu_info *cgpu);
-void _usb_set_pps(struct cgpu_info *cgpu, int intinfo, int epinfo, uint16_t PrefPacketSize);
-#define usb_set_pps(_cgpu, _pps) _usb_set_pps(_cgpu, -1, -1, _pps)
 void usb_set_dev_start(struct cgpu_info *cgpu);
 void usb_cleanup();
 void usb_initialise();