Skip to content

Commit cddf18c

Browse files
Antonio Ospitezarvox
authored andcommitted
libusbemu: implement libusb_bulk_transfer()
Signed-off-by: Drew Fisher <[email protected]> Signed-off-by: Antonio Ospite <[email protected]>
1 parent e04b4c7 commit cddf18c

File tree

2 files changed

+32
-0
lines changed

2 files changed

+32
-0
lines changed

platform/windows/libusb10emu/libusb-1.0/libusb.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,8 @@ libusb_device_handle *libusb_open_device_with_vid_pid(libusb_context *ctx,
7979
uint16_t vendor_id, uint16_t product_id);
8080

8181
int libusb_control_transfer(libusb_device_handle* dev_handle, uint8_t bmRequestType, uint8_t bRequest, uint16_t wValue, uint16_t wIndex, unsigned char* data, uint16_t wLength, unsigned int timeout);
82+
int libusb_bulk_transfer(libusb_device_handle *dev_handle, unsigned char endpoint, unsigned char *data, int length, int *actual_length, unsigned int timeout);
83+
8284

8385
struct libusb_transfer* libusb_alloc_transfer(int iso_packets);
8486
void libusb_free_transfer(struct libusb_transfer* transfer);
@@ -114,6 +116,11 @@ struct libusb_device_descriptor
114116
uint8_t bNumConfigurations;
115117
};
116118

119+
enum libusb_endpoint_direction {
120+
LIBUSB_ENDPOINT_IN = 0x80,
121+
LIBUSB_ENDPOINT_OUT = 0x00
122+
};
123+
117124
enum libusb_transfer_status
118125
{
119126
LIBUSB_TRANSFER_COMPLETED,

platform/windows/libusb10emu/libusb-1.0/libusbemu.cpp

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -363,6 +363,31 @@ int libusb_control_transfer(libusb_device_handle* dev_handle, uint8_t bmRequestT
363363
return(bytes_transferred);
364364
}
365365

366+
int libusb_bulk_transfer(libusb_device_handle* dev_handle, uint8_t endpoint, uint8_t *data, int length, int *transferred, unsigned int timeout)
367+
{
368+
// in libusb-1.0 a timeout of zero it means 'wait indefinitely'; in libusb-0.1, a timeout of zero means 'return immediately'!
369+
timeout = (0 == timeout) ? 60000 : timeout; // wait 60000ms (60s = 1min) if the transfer is supposed to wait indefinitely...
370+
int bytes_transferred;
371+
if (endpoint & LIBUSB_ENDPOINT_IN) { // Device to Host
372+
bytes_transferred = usb_bulk_read(dev_handle->handle, endpoint, (char*)data, length, timeout);
373+
} else { // Host to Device
374+
bytes_transferred = usb_bulk_write(dev_handle->handle, endpoint, (char*)data, length, timeout);
375+
}
376+
if (bytes_transferred < 0) {
377+
// 0 on success (and populates transferred)
378+
// LIBUSB_ERROR_TIMEOUT if the transfer timed out (and populates transferred)
379+
// LIBUSB_ERROR_PIPE if the endpoint halted
380+
// LIBUSB_ERROR_OVERFLOW if the device offered more data, see Packets and overflows
381+
// LIBUSB_ERROR_NO_DEVICE if the device has been disconnected
382+
// another LIBUSB_ERROR code on other failures
383+
*transferred = 0;
384+
LIBUSBEMU_ERROR_LIBUSBWIN32();
385+
return(LIBUSB_ERROR_OTHER);
386+
}
387+
*transferred = bytes_transferred;
388+
return 0;
389+
}
390+
366391
// FROM HERE ON CODE BECOMES QUITE MESSY: ASYNCHRONOUS TRANSFERS MANAGEMENT
367392

368393
struct libusb_transfer* libusb_alloc_transfer(int iso_packets)

0 commit comments

Comments
 (0)