Skip to content

(untested) Use overlapped from caller in LUsb0_ControlTransfer #34

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
47 changes: 31 additions & 16 deletions libusbK/src/lusbk_bknd_libusb0.c
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,10 @@ KUSB_EXP BOOL KUSB_API LUsb0_ControlTransfer(
int ret;
PKUSB_HANDLE_INTERNAL handle;

UNUSED(Overlapped);

Pub_To_Priv_UsbK(InterfaceHandle, handle, return FALSE);
ErrorSetAction(!PoolHandle_Inc_UsbK(handle), ERROR_RESOURCE_NOT_AVAILABLE, return FALSE, "->PoolHandle_Inc_UsbK");

ret = usb_control_msg(Dev_Handle(), SetupPacket.RequestType, SetupPacket.Request, SetupPacket.Value, SetupPacket.Index, Buffer, BufferLength, LIBUSB_DEFAULT_TIMEOUT);
ret = usb_control_msg(Dev_Handle(), SetupPacket.RequestType, SetupPacket.Request, SetupPacket.Value, SetupPacket.Index, Buffer, BufferLength, LIBUSB_DEFAULT_TIMEOUT, Overlapped);

if (ret >= 0)
{
Expand Down Expand Up @@ -119,7 +117,7 @@ int usb_set_configuration(HANDLE *dev, int configuration)
req.timeout = LIBUSB_DEFAULT_TIMEOUT;

if (!_usb_io_sync(dev, LIBUSB_IOCTL_SET_CONFIGURATION,
&req, sizeof(libusb_request), NULL, 0, NULL))
&req, sizeof(libusb_request), NULL, 0, NULL, NULL))
{
USBERR("could not set config %d: ", configuration);
return FALSE;
Expand All @@ -130,7 +128,8 @@ int usb_set_configuration(HANDLE *dev, int configuration)

// See libusb-win32 windows.c:671-815
int usb_control_msg(HANDLE *dev, int requesttype, int request,
int value, int index, PUCHAR bytes, int size, int timeout)
int value, int index, PUCHAR bytes, int size, int timeout,
LPOVERLAPPED Overlapped)
{
int read = 0;
libusb_request req;
Expand Down Expand Up @@ -255,7 +254,8 @@ int usb_control_msg(HANDLE *dev, int requesttype, int request,
in_size = 0;
}

if (!_usb_io_sync(dev, code, out, out_size, in, in_size, &read))
if (!_usb_io_sync(dev, code, out, out_size, in, in_size, &read,
Overlapped))
{
USBERR("sending control message failed");
if (!(requesttype & USB_ENDPOINT_IN))
Expand All @@ -276,38 +276,53 @@ int usb_control_msg(HANDLE *dev, int requesttype, int request,
}

int _usb_io_sync(HANDLE dev, unsigned int code, void *out, int out_size,
void *in, int in_size, int *ret)
void *in, int in_size, int *ret, LPOVERLAPPED Overlapped)
{
OVERLAPPED ol;
OVERLAPPED *olp;
DWORD _ret;

memset(&ol, 0, sizeof(ol));

if (ret)
*ret = 0;

ol.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
if (!Overlapped)
{
ol.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);

if (!ol.hEvent)
return FALSE;
if (!ol.hEvent)
return FALSE;

olp = &ol;
}
else
{
olp = Overlapped;
}

if (!DeviceIoControl(dev, code, out, out_size, in, in_size, NULL, &ol))
if (!DeviceIoControl(dev, code, out, out_size, in, in_size, NULL, olp))
{
if (GetLastError() != ERROR_IO_PENDING)
{
CloseHandle(ol.hEvent);
USBERR("DeviceIoControl failed");
if (!Overlapped)
CloseHandle(ol.hEvent);
return FALSE;
}
}

if (GetOverlappedResult(dev, &ol, &_ret, TRUE))
if (GetOverlappedResult(dev, olp, &_ret, TRUE))
{
if (ret)
*ret = (int)_ret;
CloseHandle(ol.hEvent);
if (!Overlapped)
CloseHandle(ol.hEvent);
return TRUE;
}

CloseHandle(ol.hEvent);
USBERR("GetOverlappedResult failed");
if (!Overlapped)
CloseHandle(ol.hEvent);
return FALSE;
}
}
7 changes: 4 additions & 3 deletions libusbK/src/lusbk_bknd_libusb0.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,10 @@ binary distributions.
#endif

int _usb_io_sync(HANDLE dev, unsigned int code, void *out, int out_size,
void *in, int in_size, int *ret);
void *in, int in_size, int *ret, LPOVERLAPPED Overlapped);

int usb_control_msg(HANDLE *dev, int requesttype, int request,
int value, int index, PUCHAR bytes, int size, int timeout);
int value, int index, PUCHAR bytes, int size, int timeout,
LPOVERLAPPED Overlapped);

int usb_set_configuration(HANDLE *dev, int configuration);
int usb_set_configuration(HANDLE *dev, int configuration);