From 1f1e7adf8fd8339415caf240cf711e3cf3ddb72e Mon Sep 17 00:00:00 2001 From: Tormod Volden Date: Sat, 7 Oct 2023 17:54:17 +0200 Subject: [PATCH 1/3] libusb0 backend: Use macro instead of magic value Signed-off-by: Tormod Volden --- libusbK/src/lusbk_bknd_libusb0.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libusbK/src/lusbk_bknd_libusb0.c b/libusbK/src/lusbk_bknd_libusb0.c index 57333bdf..481d2d9f 100644 --- a/libusbK/src/lusbk_bknd_libusb0.c +++ b/libusbK/src/lusbk_bknd_libusb0.c @@ -226,7 +226,7 @@ int usb_control_msg(HANDLE *dev, int requesttype, int request, req.vendor.value = value; req.vendor.index = index; - if (requesttype & 0x80) + if (requesttype & USB_ENDPOINT_IN) code = LIBUSB_IOCTL_VENDOR_READ; else code = LIBUSB_IOCTL_VENDOR_WRITE; From 4f3c3a6b2d101527d4e840bfd0a025338ab5a589 Mon Sep 17 00:00:00 2001 From: Tormod Volden Date: Sat, 7 Oct 2023 17:48:37 +0200 Subject: [PATCH 2/3] libusb0 backend: Set Win32 last error if usb_control_msg() fails usb_control_msg() returns various error codes, so map these to Win32 errors that can be read out by calling functions. Signed-off-by: Tormod Volden --- libusbK/src/lusbk_bknd_libusb0.c | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/libusbK/src/lusbk_bknd_libusb0.c b/libusbK/src/lusbk_bknd_libusb0.c index 481d2d9f..dcab93c7 100644 --- a/libusbK/src/lusbk_bknd_libusb0.c +++ b/libusbK/src/lusbk_bknd_libusb0.c @@ -68,6 +68,10 @@ KUSB_EXP BOOL KUSB_API LUsb0_ControlTransfer( } else { + if (ret == -EINVAL) + SetLastError(ERROR_INVALID_PARAMETER); + else if (ret == -ENOMEM) + SetLastError(ERROR_NOT_ENOUGH_MEMORY); success = FALSE; } From ae65204f18ad346bc3b9e503776540572ba94517 Mon Sep 17 00:00:00 2001 From: Tormod Volden Date: Sat, 7 Oct 2023 17:56:24 +0200 Subject: [PATCH 3/3] libusb0 backend: Skip GetOverlappedResult() on synchronous completion If DeviceIoControl() succeeds synchronously it isn't advised to call GetOverlappedResult() [1]. In practive it seems like GetOverlappedResult() works in any case, but all Microsoft examples treat the synchronous completion case separately. Also preserve the Win32 last error from the failing call since a failing CloseHandle() might clobber it. [1] https://learn.microsoft.com/en-GB/troubleshoot/windows/win32/asynchronous-disk-io-synchronous Signed-off-by: Tormod Volden --- libusbK/src/lusbk_bknd_libusb0.c | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/libusbK/src/lusbk_bknd_libusb0.c b/libusbK/src/lusbk_bknd_libusb0.c index dcab93c7..b9e4fe32 100644 --- a/libusbK/src/lusbk_bknd_libusb0.c +++ b/libusbK/src/lusbk_bknd_libusb0.c @@ -284,6 +284,7 @@ int _usb_io_sync(HANDLE dev, unsigned int code, void *out, int out_size, { OVERLAPPED ol; DWORD _ret; + DWORD err; memset(&ol, 0, sizeof(ol)); @@ -297,11 +298,20 @@ int _usb_io_sync(HANDLE dev, unsigned int code, void *out, int out_size, if (!DeviceIoControl(dev, code, out, out_size, in, in_size, NULL, &ol)) { - if (GetLastError() != ERROR_IO_PENDING) + err = GetLastError(); + + if (err != ERROR_IO_PENDING) { CloseHandle(ol.hEvent); + SetLastError(err); return FALSE; } + } else { + // successful synchronous completion + if (ret) + *ret = (int) in_size; + CloseHandle(ol.hEvent); + return TRUE; } if (GetOverlappedResult(dev, &ol, &_ret, TRUE)) @@ -312,6 +322,8 @@ int _usb_io_sync(HANDLE dev, unsigned int code, void *out, int out_size, return TRUE; } + err = GetLastError(); CloseHandle(ol.hEvent); + SetLastError(err); return FALSE; } \ No newline at end of file