Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Special-case sync return from async call and preserve last error #55

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 18 additions & 2 deletions libusbK/src/lusbk_bknd_libusb0.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down Expand Up @@ -226,7 +230,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;
Expand Down Expand Up @@ -280,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));

Expand All @@ -293,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))
Expand All @@ -308,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;
}