13
13
14
14
#define USBHIDASYNC_MAX_DEVICES 256
15
15
16
+ #define USBHIDASYNC_OUT_TIMEOUT 20 // ms
17
+
16
18
typedef struct {
17
19
int configuration ;
18
20
struct {
@@ -65,8 +67,6 @@ static void print_error_libusb(const char * file, int line, const char * func, c
65
67
66
68
static libusb_context * ctx = NULL ;
67
69
68
- #define REPORTS_MAX 2
69
-
70
70
static struct libusb_transfer * * transfers = NULL ;
71
71
static unsigned int transfers_nb = 0 ;
72
72
@@ -240,7 +240,10 @@ static void usb_callback(struct libusb_transfer* transfer) {
240
240
int device = (unsigned long ) transfer -> user_data ;
241
241
242
242
//make sure the device still exists, in case something went wrong
243
- USBHIDASYNC_CHECK_DEVICE (device ,/* void */ )
243
+ if (usbhidasync_check_device (device , __FILE__ , __LINE__ , __func__ ) < 0 ) {
244
+ remove_transfer (transfer );
245
+ return ;
246
+ }
244
247
245
248
if (transfer -> type == LIBUSB_TRANSFER_TYPE_INTERRUPT ) {
246
249
if (transfer -> status == LIBUSB_TRANSFER_COMPLETED ) {
@@ -251,7 +254,11 @@ static void usb_callback(struct libusb_transfer* transfer) {
251
254
usbdevices [device ].callback .fp_write (usbdevices [device ].callback .user );
252
255
}
253
256
} else {
254
- if (transfer -> status != LIBUSB_TRANSFER_TIMED_OUT && transfer -> status != LIBUSB_TRANSFER_CANCELLED ) {
257
+ if (transfer -> endpoint == usbdevices [device ].config .endpoints .out .address ) {
258
+ usbdevices [device ].callback .fp_write (usbdevices [device ].callback .user );
259
+ }
260
+ if (transfer -> endpoint == usbdevices [device ].config .endpoints .out .address
261
+ || (transfer -> status != LIBUSB_TRANSFER_TIMED_OUT && transfer -> status != LIBUSB_TRANSFER_CANCELLED )) {
255
262
fprintf (stderr , "libusb_transfer failed with status %s (endpoint=0x%02x)\n" ,
256
263
libusb_error_name (transfer -> status ), transfer -> endpoint );
257
264
}
@@ -909,7 +916,7 @@ int usbhidasync_write(int device, const void * buf, unsigned int count) {
909
916
}
910
917
911
918
libusb_fill_interrupt_transfer (transfer , usbdevices [device ].devh , usbdevices [device ].config .endpoints .out .address ,
912
- buffer , count , (libusb_transfer_cb_fn ) usb_callback , (void * ) (unsigned long ) device , 1000 );
919
+ buffer , count , (libusb_transfer_cb_fn ) usb_callback , (void * ) (unsigned long ) device , USBHIDASYNC_OUT_TIMEOUT );
913
920
914
921
return submit_transfer (transfer );
915
922
}
0 commit comments