mirror of
https://github.com/utmapp/libusb.git
synced 2026-12-01 14:01:17 -10:00
Windows: Refactoring to consolidate and simplify common code
Both the UsbDk and WinUSB backends perform common steps when handling transfers in order to interact with the poll abstraction, both during submission and when processing transfer completion. With some rearranging of shared structures, this can be yanked from the individual backends and placed in the common area. This allows for several functions to be removed outright from each backend. The cancellation logic can also be simplified by attempting CancelIoEx() at the highest level and delegating to the backend if there are alternatives to try should CancelIoEx() fail. After some analysis of how Windows processes asychronous (OVERLAPPED) requests that the underlying driver completes synchronously, it is now evident that such requests need not be handled in any special fashion. Each function that called a driver function that was expected to complete asynchronously had logic to handle the case of a synchronous completion, so this has all been killed off. This significantly cleans up these call sites as now they must only check for an error condition. Finally, the initialization code for the WinUSB backend has been reworked to load the WinUSB DLL independent of the libusbK DLL. Previously when the libusbK DLL was present, all requests to devices using WinUSB would first be sent through the libusbK DLL where they would then be forwarded to the WinUSB DLL. This is slightly inefficient but is also limiting when using Windows 8.1 or later because support for isochronous transfers through WinUSB will be lost. Signed-off-by: Chris Dickens <[email protected]>
This commit is contained in:
1 parent
67e6816264
commit
9c28ad219b
8 files changed
+827
-1104
No files matched your search
@@ -392,8 +392,7 @@ int usbi_poll(struct pollfd *fds, usbi_nfds_t nfds, int timeout)
|
||||
if (fd == NULL) {
|
||||
fds[n].revents = POLLNVAL;
|
||||
nready++;
|
||||
} else if (HasOverlappedIoCompleted(&fd->overlapped) &&
|
||||
(WaitForSingleObject(fd->overlapped.hEvent, 0) == WAIT_OBJECT_0)) {
|
||||
} else if (HasOverlappedIoCompleted(&fd->overlapped)) {
|
||||
fds[n].revents = fds[n].events;
|
||||
nready++;
|
||||
} else {
|
||||
|
||||
@@ -25,13 +25,6 @@
|
||||
#ifndef LIBUSB_POLL_WINDOWS_H
|
||||
#define LIBUSB_POLL_WINDOWS_H
|
||||
|
||||
// Handle synchronous completion through the overlapped structure
|
||||
#if !defined(STATUS_REPARSE) // reuse the REPARSE status code
|
||||
#define STATUS_REPARSE ((LONG)0x00000104L)
|
||||
#endif
|
||||
#define STATUS_COMPLETED_SYNCHRONOUSLY STATUS_REPARSE
|
||||
#define HasOverlappedIoCompletedSync(lpOverlapped) (((DWORD)(lpOverlapped)->Internal) == STATUS_COMPLETED_SYNCHRONOUSLY)
|
||||
|
||||
#define DUMMY_HANDLE ((HANDLE)(LONG_PTR)-2)
|
||||
|
||||
#define POLLIN 0x0001 /* There is data to read */
|
||||
|
||||
+174
-94
@@ -33,12 +33,14 @@
|
||||
|
||||
#define EPOCH_TIME UINT64_C(116444736000000000) // 1970.01.01 00:00:000 in MS Filetime
|
||||
|
||||
#define STATUS_SUCCESS ((ULONG_PTR)0UL)
|
||||
|
||||
// Public
|
||||
enum windows_version windows_version = WINDOWS_UNDEFINED;
|
||||
|
||||
// Global variables for init/exit
|
||||
static unsigned int init_count = 0;
|
||||
static bool usbdk_available = false;
|
||||
static unsigned int init_count;
|
||||
static bool usbdk_available;
|
||||
|
||||
// Global variables for clock_gettime mechanism
|
||||
static uint64_t hires_ticks_to_ps;
|
||||
@@ -114,7 +116,7 @@ typedef struct htab_entry {
|
||||
char *str;
|
||||
} htab_entry;
|
||||
|
||||
static htab_entry *htab_table = NULL;
|
||||
static htab_entry *htab_table;
|
||||
static usbi_mutex_t htab_mutex;
|
||||
static unsigned long htab_filled;
|
||||
|
||||
@@ -125,7 +127,7 @@ static unsigned long htab_filled;
|
||||
static bool htab_create(struct libusb_context *ctx)
|
||||
{
|
||||
if (htab_table != NULL) {
|
||||
usbi_err(ctx, "hash table already allocated");
|
||||
usbi_err(ctx, "program assertion falied - hash table already allocated");
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -173,7 +175,7 @@ unsigned long htab_hash(const char *str)
|
||||
{
|
||||
unsigned long hval, hval2;
|
||||
unsigned long idx;
|
||||
unsigned long r = 5381;
|
||||
unsigned long r = 5381UL;
|
||||
int c;
|
||||
const char *sz = str;
|
||||
|
||||
@@ -204,7 +206,7 @@ unsigned long htab_hash(const char *str)
|
||||
usbi_dbg("hash collision ('%s' vs '%s')", str, htab_table[idx].str);
|
||||
|
||||
// Second hash function, as suggested in [Knuth]
|
||||
hval2 = 1 + hval % (HTAB_SIZE - 2);
|
||||
hval2 = 1UL + hval % (HTAB_SIZE - 2);
|
||||
|
||||
do {
|
||||
// Because size is prime this guarantees to step through all available indexes
|
||||
@@ -228,14 +230,14 @@ unsigned long htab_hash(const char *str)
|
||||
// If the table is full return an error
|
||||
if (htab_filled >= HTAB_SIZE) {
|
||||
usbi_err(NULL, "hash table is full (%lu entries)", HTAB_SIZE);
|
||||
idx = 0;
|
||||
idx = 0UL;
|
||||
goto out_unlock;
|
||||
}
|
||||
|
||||
htab_table[idx].str = _strdup(str);
|
||||
if (htab_table[idx].str == NULL) {
|
||||
usbi_err(NULL, "could not duplicate string for hash table");
|
||||
idx = 0;
|
||||
idx = 0UL;
|
||||
goto out_unlock;
|
||||
}
|
||||
|
||||
@@ -248,13 +250,33 @@ out_unlock:
|
||||
return idx;
|
||||
}
|
||||
|
||||
enum libusb_transfer_status usbd_status_to_libusb_transfer_status(USBD_STATUS status)
|
||||
{
|
||||
if (USBD_SUCCESS(status))
|
||||
return LIBUSB_TRANSFER_COMPLETED;
|
||||
|
||||
switch (status) {
|
||||
case USBD_STATUS_TIMEOUT:
|
||||
return LIBUSB_TRANSFER_TIMED_OUT;
|
||||
case USBD_STATUS_CANCELED:
|
||||
return LIBUSB_TRANSFER_CANCELLED;
|
||||
case USBD_STATUS_ENDPOINT_HALTED:
|
||||
return LIBUSB_TRANSFER_STALL;
|
||||
case USBD_STATUS_DEVICE_GONE:
|
||||
return LIBUSB_TRANSFER_NO_DEVICE;
|
||||
default:
|
||||
usbi_dbg("USBD_STATUS 0x%08lx translated to LIBUSB_TRANSFER_ERROR", ULONG_CAST(status));
|
||||
return LIBUSB_TRANSFER_ERROR;
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* Make a transfer complete synchronously
|
||||
*/
|
||||
void windows_force_sync_completion(OVERLAPPED *overlapped, ULONG size)
|
||||
{
|
||||
overlapped->Internal = STATUS_COMPLETED_SYNCHRONOUSLY;
|
||||
overlapped->InternalHigh = size;
|
||||
overlapped->Internal = (ULONG_PTR)STATUS_SUCCESS;
|
||||
overlapped->InternalHigh = (ULONG_PTR)size;
|
||||
SetEvent(overlapped->hEvent);
|
||||
}
|
||||
|
||||
@@ -294,7 +316,7 @@ static void get_windows_version(void)
|
||||
const char *arch, *w = NULL;
|
||||
unsigned major, minor, version;
|
||||
ULONGLONG major_equal, minor_equal;
|
||||
BOOL ws;
|
||||
bool ws;
|
||||
|
||||
windows_version = WINDOWS_UNDEFINED;
|
||||
|
||||
@@ -378,16 +400,17 @@ static void get_windows_version(void)
|
||||
}
|
||||
|
||||
static void windows_transfer_callback(const struct windows_backend *backend,
|
||||
struct usbi_transfer *itransfer, DWORD io_result, DWORD io_size)
|
||||
struct usbi_transfer *itransfer, DWORD error, DWORD bytes_transferred)
|
||||
{
|
||||
int status, istatus;
|
||||
struct windows_transfer_priv *transfer_priv = get_transfer_priv(itransfer);
|
||||
enum libusb_transfer_status status, istatus;
|
||||
|
||||
usbi_dbg("handling I/O completion with errcode %lu, size %lu",
|
||||
ULONG_CAST(io_result), ULONG_CAST(io_size));
|
||||
usbi_dbg("handling I/O completion with errcode %lu, length %lu",
|
||||
ULONG_CAST(error), ULONG_CAST(bytes_transferred));
|
||||
|
||||
switch (io_result) {
|
||||
switch (error) {
|
||||
case NO_ERROR:
|
||||
status = backend->copy_transfer_data(itransfer, (uint32_t)io_size);
|
||||
status = backend->copy_transfer_data(itransfer, bytes_transferred);
|
||||
break;
|
||||
case ERROR_GEN_FAILURE:
|
||||
usbi_dbg("detected endpoint stall");
|
||||
@@ -398,9 +421,9 @@ static void windows_transfer_callback(const struct windows_backend *backend,
|
||||
status = LIBUSB_TRANSFER_TIMED_OUT;
|
||||
break;
|
||||
case ERROR_OPERATION_ABORTED:
|
||||
istatus = backend->copy_transfer_data(itransfer, (uint32_t)io_size);
|
||||
istatus = backend->copy_transfer_data(itransfer, bytes_transferred);
|
||||
if (istatus != LIBUSB_TRANSFER_COMPLETED)
|
||||
usbi_dbg("Failed to copy partial data in aborted operation: %d", istatus);
|
||||
usbi_dbg("failed to copy partial data in aborted operation: %d", (int)istatus);
|
||||
|
||||
usbi_dbg("detected operation aborted");
|
||||
status = LIBUSB_TRANSFER_CANCELLED;
|
||||
@@ -412,57 +435,45 @@ static void windows_transfer_callback(const struct windows_backend *backend,
|
||||
break;
|
||||
default:
|
||||
usbi_err(ITRANSFER_CTX(itransfer), "detected I/O error %lu: %s",
|
||||
ULONG_CAST(io_result), windows_error_str(io_result));
|
||||
ULONG_CAST(error), windows_error_str(error));
|
||||
status = LIBUSB_TRANSFER_ERROR;
|
||||
break;
|
||||
}
|
||||
backend->clear_transfer_priv(itransfer); // Cancel polling
|
||||
|
||||
// Cancel polling
|
||||
usbi_close(transfer_priv->pollable_fd.fd);
|
||||
transfer_priv->pollable_fd = INVALID_WINFD;
|
||||
transfer_priv->handle = NULL;
|
||||
|
||||
// Backend-specific cleanup
|
||||
backend->clear_transfer_priv(itransfer);
|
||||
|
||||
if (status == LIBUSB_TRANSFER_CANCELLED)
|
||||
usbi_handle_transfer_cancellation(itransfer);
|
||||
else
|
||||
usbi_handle_transfer_completion(itransfer, (enum libusb_transfer_status)status);
|
||||
}
|
||||
|
||||
static void windows_handle_callback(const struct windows_backend *backend,
|
||||
struct usbi_transfer *itransfer, DWORD io_result, DWORD io_size)
|
||||
{
|
||||
struct libusb_transfer *transfer = USBI_TRANSFER_TO_LIBUSB_TRANSFER(itransfer);
|
||||
|
||||
switch (transfer->type) {
|
||||
case LIBUSB_TRANSFER_TYPE_CONTROL:
|
||||
case LIBUSB_TRANSFER_TYPE_BULK:
|
||||
case LIBUSB_TRANSFER_TYPE_INTERRUPT:
|
||||
case LIBUSB_TRANSFER_TYPE_ISOCHRONOUS:
|
||||
windows_transfer_callback(backend, itransfer, io_result, io_size);
|
||||
break;
|
||||
case LIBUSB_TRANSFER_TYPE_BULK_STREAM:
|
||||
usbi_warn(ITRANSFER_CTX(itransfer), "bulk stream transfers are not yet supported on this platform");
|
||||
break;
|
||||
default:
|
||||
usbi_err(ITRANSFER_CTX(itransfer), "unknown endpoint type %d", transfer->type);
|
||||
}
|
||||
usbi_handle_transfer_completion(itransfer, status);
|
||||
}
|
||||
|
||||
static int windows_init(struct libusb_context *ctx)
|
||||
{
|
||||
struct windows_context_priv *priv = _context_priv(ctx);
|
||||
HANDLE semaphore;
|
||||
char sem_name[11 + 8 + 1]; // strlen("libusb_init") + (32-bit hex PID) + '\0'
|
||||
char mutex_name[11 + 8 + 1]; // strlen("libusb_init") + (32-bit hex PID) + '\0'
|
||||
HANDLE mutex;
|
||||
int r = LIBUSB_ERROR_OTHER;
|
||||
bool winusb_backend_init = false;
|
||||
|
||||
sprintf(sem_name, "libusb_init%08X", (unsigned int)(GetCurrentProcessId() & 0xFFFFFFFF));
|
||||
semaphore = CreateSemaphoreA(NULL, 1, 1, sem_name);
|
||||
if (semaphore == NULL) {
|
||||
usbi_err(ctx, "could not create semaphore: %s", windows_error_str(0));
|
||||
sprintf(mutex_name, "libusb_init%08X", (unsigned int)(GetCurrentProcessId() & 0xFFFFFFFFU));
|
||||
mutex = CreateMutexA(NULL, FALSE, mutex_name);
|
||||
if (mutex == NULL) {
|
||||
usbi_err(ctx, "could not create mutex: %s", windows_error_str(0));
|
||||
return LIBUSB_ERROR_NO_MEM;
|
||||
}
|
||||
|
||||
// A successful wait brings our semaphore count to 0 (unsignaled)
|
||||
// => any concurent wait stalls until the semaphore's release
|
||||
if (WaitForSingleObject(semaphore, INFINITE) != WAIT_OBJECT_0) {
|
||||
usbi_err(ctx, "failure to access semaphore: %s", windows_error_str(0));
|
||||
CloseHandle(semaphore);
|
||||
// A successful wait gives this thread ownership of the mutex
|
||||
// => any concurent wait stalls until the mutex is released
|
||||
if (WaitForSingleObject(mutex, INFINITE) != WAIT_OBJECT_0) {
|
||||
usbi_err(ctx, "failure to access mutex: %s", windows_error_str(0));
|
||||
CloseHandle(mutex);
|
||||
return LIBUSB_ERROR_NO_MEM;
|
||||
}
|
||||
|
||||
@@ -511,26 +522,26 @@ init_exit: // Holds semaphore here
|
||||
--init_count;
|
||||
}
|
||||
|
||||
ReleaseSemaphore(semaphore, 1, NULL); // increase count back to 1
|
||||
CloseHandle(semaphore);
|
||||
ReleaseMutex(mutex);
|
||||
CloseHandle(mutex);
|
||||
return r;
|
||||
}
|
||||
|
||||
static void windows_exit(struct libusb_context *ctx)
|
||||
{
|
||||
HANDLE semaphore;
|
||||
char sem_name[11 + 8 + 1]; // strlen("libusb_init") + (32-bit hex PID) + '\0'
|
||||
UNUSED(ctx);
|
||||
char mutex_name[11 + 8 + 1]; // strlen("libusb_init") + (32-bit hex PID) + '\0'
|
||||
HANDLE mutex;
|
||||
|
||||
sprintf(sem_name, "libusb_init%08lX", (GetCurrentProcessId() & 0xFFFFFFFFUL));
|
||||
semaphore = CreateSemaphoreA(NULL, 1, 1, sem_name);
|
||||
if (semaphore == NULL)
|
||||
sprintf(mutex_name, "libusb_init%08lX", (GetCurrentProcessId() & 0xFFFFFFFFU));
|
||||
mutex = CreateMutexA(NULL, FALSE, mutex_name);
|
||||
if (mutex == NULL)
|
||||
return;
|
||||
|
||||
// A successful wait brings our semaphore count to 0 (unsignaled)
|
||||
// => any concurent wait stalls until the semaphore release
|
||||
if (WaitForSingleObject(semaphore, INFINITE) != WAIT_OBJECT_0) {
|
||||
CloseHandle(semaphore);
|
||||
// A successful wait gives this thread ownership of the mutex
|
||||
// => any concurent wait stalls until the mutex is released
|
||||
if (WaitForSingleObject(mutex, INFINITE) != WAIT_OBJECT_0) {
|
||||
usbi_err(ctx, "failed to access mutex: %s", windows_error_str(0));
|
||||
CloseHandle(mutex);
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -544,8 +555,8 @@ static void windows_exit(struct libusb_context *ctx)
|
||||
htab_destroy();
|
||||
}
|
||||
|
||||
ReleaseSemaphore(semaphore, 1, NULL); // increase count back to 1
|
||||
CloseHandle(semaphore);
|
||||
ReleaseMutex(mutex);
|
||||
CloseHandle(mutex);
|
||||
}
|
||||
|
||||
static int windows_set_option(struct libusb_context *ctx, enum libusb_option option, va_list ap)
|
||||
@@ -567,7 +578,6 @@ static int windows_set_option(struct libusb_context *ctx, enum libusb_option opt
|
||||
default:
|
||||
return LIBUSB_ERROR_NOT_SUPPORTED;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
static int windows_get_device_list(struct libusb_context *ctx, struct discovered_devs **discdevs)
|
||||
@@ -671,29 +681,103 @@ static void windows_destroy_device(struct libusb_device *dev)
|
||||
|
||||
static int windows_submit_transfer(struct usbi_transfer *itransfer)
|
||||
{
|
||||
struct windows_context_priv *priv = _context_priv(ITRANSFER_CTX(itransfer));
|
||||
return priv->backend->submit_transfer(itransfer);
|
||||
struct libusb_transfer *transfer = USBI_TRANSFER_TO_LIBUSB_TRANSFER(itransfer);
|
||||
struct libusb_context *ctx = TRANSFER_CTX(transfer);
|
||||
struct windows_context_priv *priv = _context_priv(ctx);
|
||||
struct windows_transfer_priv *transfer_priv = get_transfer_priv(itransfer);
|
||||
short events;
|
||||
int r;
|
||||
|
||||
switch (transfer->type) {
|
||||
case LIBUSB_TRANSFER_TYPE_CONTROL:
|
||||
events = (transfer->buffer[0] & LIBUSB_ENDPOINT_IN) ? POLLIN : POLLOUT;
|
||||
break;
|
||||
case LIBUSB_TRANSFER_TYPE_BULK:
|
||||
case LIBUSB_TRANSFER_TYPE_INTERRUPT:
|
||||
case LIBUSB_TRANSFER_TYPE_ISOCHRONOUS:
|
||||
events = IS_XFERIN(transfer) ? POLLIN : POLLOUT;
|
||||
break;
|
||||
case LIBUSB_TRANSFER_TYPE_BULK_STREAM:
|
||||
usbi_warn(ctx, "bulk stream transfers are not yet supported on this platform");
|
||||
return LIBUSB_ERROR_NOT_SUPPORTED;
|
||||
default:
|
||||
usbi_err(ctx, "unknown endpoint type %d", transfer->type);
|
||||
return LIBUSB_ERROR_INVALID_PARAM;
|
||||
}
|
||||
|
||||
// Because a Windows OVERLAPPED is used for poll emulation,
|
||||
// a pollable fd is created and stored with each transfer
|
||||
transfer_priv->pollable_fd = usbi_create_fd();
|
||||
if (transfer_priv->pollable_fd.fd < 0) {
|
||||
usbi_err(ctx, "failed to create pollable fd");
|
||||
return LIBUSB_ERROR_NO_MEM;
|
||||
}
|
||||
|
||||
if (transfer_priv->handle != NULL) {
|
||||
usbi_err(ctx, "program assertion failed - transfer HANDLE is not NULL");
|
||||
transfer_priv->handle = NULL;
|
||||
}
|
||||
|
||||
r = priv->backend->submit_transfer(itransfer);
|
||||
if (r != LIBUSB_SUCCESS) {
|
||||
// Always call the backend's clear_transfer_priv() function on failure
|
||||
priv->backend->clear_transfer_priv(itransfer);
|
||||
// Release the pollable fd since it won't be used
|
||||
usbi_close(transfer_priv->pollable_fd.fd);
|
||||
transfer_priv->pollable_fd = INVALID_WINFD;
|
||||
transfer_priv->handle = NULL;
|
||||
return r;
|
||||
}
|
||||
|
||||
// The backend should set the HANDLE used for each submitted transfer
|
||||
// by calling set_transfer_priv_handle()
|
||||
if (transfer_priv->handle == NULL)
|
||||
usbi_err(ctx, "program assertion failed - transfer HANDLE is NULL after transfer was submitted");
|
||||
|
||||
// We don't want to start monitoring the pollable fd before the transfer
|
||||
// has been submitted, so start monitoring it now. Note that if the
|
||||
// usbi_add_pollfd() function fails, the user will never get notified
|
||||
// that the transfer has completed. We don't attempt any cleanup if this
|
||||
// happens because the transfer is already in progress and could even have
|
||||
// completed
|
||||
if (usbi_add_pollfd(ctx, transfer_priv->pollable_fd.fd, events))
|
||||
usbi_err(ctx, "failed to add pollable fd %d for transfer %p",
|
||||
transfer_priv->pollable_fd.fd, transfer);
|
||||
|
||||
return r;
|
||||
}
|
||||
|
||||
static int windows_cancel_transfer(struct usbi_transfer *itransfer)
|
||||
{
|
||||
struct windows_context_priv *priv = _context_priv(ITRANSFER_CTX(itransfer));
|
||||
return priv->backend->cancel_transfer(itransfer);
|
||||
struct windows_transfer_priv *transfer_priv = get_transfer_priv(itransfer);
|
||||
|
||||
// Try CancelIoEx() on the transfer
|
||||
// If that fails, fall back to the backend's cancel_transfer()
|
||||
// function if it is available
|
||||
if (CancelIoEx(transfer_priv->handle, transfer_priv->pollable_fd.overlapped))
|
||||
return LIBUSB_SUCCESS;
|
||||
else if (GetLastError() == ERROR_NOT_FOUND)
|
||||
return LIBUSB_ERROR_NOT_FOUND;
|
||||
|
||||
if (priv->backend->cancel_transfer)
|
||||
return priv->backend->cancel_transfer(itransfer);
|
||||
|
||||
usbi_warn(ITRANSFER_CTX(itransfer), "cancellation not supported for this transfer's driver");
|
||||
return LIBUSB_ERROR_NOT_SUPPORTED;
|
||||
}
|
||||
|
||||
static int windows_handle_events(struct libusb_context *ctx, struct pollfd *fds, usbi_nfds_t nfds, int num_ready)
|
||||
{
|
||||
struct windows_context_priv *priv = _context_priv(ctx);
|
||||
struct usbi_transfer *itransfer;
|
||||
DWORD io_size, io_result;
|
||||
struct windows_transfer_priv *transfer_priv;
|
||||
DWORD result, bytes_transferred;
|
||||
usbi_nfds_t i;
|
||||
bool found;
|
||||
int transfer_fd;
|
||||
int r = LIBUSB_SUCCESS;
|
||||
|
||||
usbi_mutex_lock(&ctx->open_devs_lock);
|
||||
for (i = 0; i < nfds && num_ready > 0; i++) {
|
||||
|
||||
usbi_dbg("checking fd %d with revents = %04x", fds[i].fd, fds[i].revents);
|
||||
|
||||
if (!fds[i].revents)
|
||||
@@ -701,34 +785,30 @@ static int windows_handle_events(struct libusb_context *ctx, struct pollfd *fds,
|
||||
|
||||
num_ready--;
|
||||
|
||||
// Because a Windows OVERLAPPED is used for poll emulation,
|
||||
// a pollable fd is created and stored with each transfer
|
||||
found = false;
|
||||
transfer_fd = -1;
|
||||
transfer_priv = NULL;
|
||||
usbi_mutex_lock(&ctx->flying_transfers_lock);
|
||||
list_for_each_entry(itransfer, &ctx->flying_transfers, list, struct usbi_transfer) {
|
||||
transfer_fd = priv->backend->get_transfer_fd(itransfer);
|
||||
if (transfer_fd == fds[i].fd) {
|
||||
found = true;
|
||||
transfer_priv = get_transfer_priv(itransfer);
|
||||
if (transfer_priv->pollable_fd.fd == fds[i].fd)
|
||||
break;
|
||||
}
|
||||
transfer_priv = NULL;
|
||||
}
|
||||
usbi_mutex_unlock(&ctx->flying_transfers_lock);
|
||||
|
||||
if (found) {
|
||||
priv->backend->get_overlapped_result(itransfer, &io_result, &io_size);
|
||||
|
||||
usbi_remove_pollfd(ctx, transfer_fd);
|
||||
|
||||
// let handle_callback free the event using the transfer wfd
|
||||
// If you don't use the transfer wfd, you run a risk of trying to free a
|
||||
// newly allocated wfd that took the place of the one from the transfer.
|
||||
windows_handle_callback(priv->backend, itransfer, io_result, io_size);
|
||||
} else {
|
||||
if (transfer_priv == NULL) {
|
||||
usbi_err(ctx, "could not find a matching transfer for fd %d", fds[i].fd);
|
||||
r = LIBUSB_ERROR_NOT_FOUND;
|
||||
break;
|
||||
}
|
||||
|
||||
usbi_remove_pollfd(ctx, transfer_priv->pollable_fd.fd);
|
||||
|
||||
if (GetOverlappedResult(transfer_priv->handle, transfer_priv->pollable_fd.overlapped, &bytes_transferred, FALSE))
|
||||
result = NO_ERROR;
|
||||
else
|
||||
result = GetLastError();
|
||||
|
||||
windows_transfer_callback(priv->backend, itransfer, result, bytes_transferred);
|
||||
}
|
||||
usbi_mutex_unlock(&ctx->open_devs_lock);
|
||||
|
||||
@@ -814,5 +894,5 @@ const struct usbi_os_backend usbi_backend = {
|
||||
sizeof(struct windows_context_priv),
|
||||
sizeof(union windows_device_priv),
|
||||
sizeof(union windows_device_handle_priv),
|
||||
sizeof(union windows_transfer_priv),
|
||||
sizeof(struct windows_transfer_priv),
|
||||
};
|
||||
+43
-15
@@ -68,13 +68,13 @@
|
||||
#define DLL_HANDLE_NAME(name) __dll_##name##_handle
|
||||
|
||||
#define DLL_DECLARE_HANDLE(name) \
|
||||
static HMODULE DLL_HANDLE_NAME(name) = NULL
|
||||
static HMODULE DLL_HANDLE_NAME(name)
|
||||
|
||||
#define DLL_GET_HANDLE(name) \
|
||||
do { \
|
||||
DLL_HANDLE_NAME(name) = DLL_LOAD_LIBRARY(name); \
|
||||
if (!DLL_HANDLE_NAME(name)) \
|
||||
return FALSE; \
|
||||
return false; \
|
||||
} while (0)
|
||||
|
||||
#define DLL_FREE_HANDLE(name) \
|
||||
@@ -92,7 +92,7 @@
|
||||
|
||||
#define DLL_DECLARE_FUNC_PREFIXNAME(api, ret, prefixname, name, args) \
|
||||
typedef ret (api * DLL_FUNC_NAME(name))args; \
|
||||
static DLL_FUNC_NAME(name) prefixname = NULL
|
||||
static DLL_FUNC_NAME(name) prefixname
|
||||
|
||||
#define DLL_DECLARE_FUNC(api, ret, name, args) \
|
||||
DLL_DECLARE_FUNC_PREFIXNAME(api, ret, name, name, args)
|
||||
@@ -115,7 +115,7 @@
|
||||
if (prefixname) \
|
||||
break; \
|
||||
if (ret_on_failure) \
|
||||
return FALSE; \
|
||||
return false; \
|
||||
} while (0)
|
||||
|
||||
#define DLL_LOAD_FUNC(dll, name, ret_on_failure) \
|
||||
@@ -271,16 +271,13 @@ struct winusb_device_handle_priv {
|
||||
|
||||
struct usbdk_transfer_priv {
|
||||
USB_DK_TRANSFER_REQUEST request;
|
||||
struct winfd pollable_fd;
|
||||
HANDLE system_handle;
|
||||
PULONG64 IsochronousPacketsArray;
|
||||
PUSB_DK_ISO_TRANSFER_RESULT IsochronousResultsArray;
|
||||
};
|
||||
|
||||
struct winusb_transfer_priv {
|
||||
struct winfd pollable_fd;
|
||||
HANDLE handle;
|
||||
uint8_t interface_number;
|
||||
|
||||
uint8_t *hid_buffer; // 1 byte extended data buffer, required for HID
|
||||
uint8_t *hid_dest; // transfer buffer destination, required for HID
|
||||
size_t hid_expected_size;
|
||||
@@ -322,10 +319,7 @@ struct windows_backend {
|
||||
int (*submit_transfer)(struct usbi_transfer *itransfer);
|
||||
int (*cancel_transfer)(struct usbi_transfer *itransfer);
|
||||
void (*clear_transfer_priv)(struct usbi_transfer *itransfer);
|
||||
int (*copy_transfer_data)(struct usbi_transfer *itransfer, uint32_t io_size);
|
||||
int (*get_transfer_fd)(struct usbi_transfer *itransfer);
|
||||
void (*get_overlapped_result)(struct usbi_transfer *itransfer,
|
||||
DWORD *io_result, DWORD *io_size);
|
||||
enum libusb_transfer_status (*copy_transfer_data)(struct usbi_transfer *itransfer, DWORD length);
|
||||
};
|
||||
|
||||
struct windows_context_priv {
|
||||
@@ -342,15 +336,49 @@ union windows_device_handle_priv {
|
||||
struct winusb_device_handle_priv winusb_priv;
|
||||
};
|
||||
|
||||
union windows_transfer_priv {
|
||||
struct usbdk_transfer_priv usbdk_priv;
|
||||
struct winusb_transfer_priv winusb_priv;
|
||||
struct windows_transfer_priv {
|
||||
struct winfd pollable_fd;
|
||||
HANDLE handle;
|
||||
union {
|
||||
struct usbdk_transfer_priv usbdk_priv;
|
||||
struct winusb_transfer_priv winusb_priv;
|
||||
};
|
||||
};
|
||||
|
||||
static inline struct windows_transfer_priv *get_transfer_priv(struct usbi_transfer *itransfer)
|
||||
{
|
||||
return (struct windows_transfer_priv *)usbi_transfer_get_os_priv(itransfer);
|
||||
}
|
||||
|
||||
static inline OVERLAPPED *get_transfer_priv_overlapped(struct usbi_transfer *itransfer)
|
||||
{
|
||||
struct windows_transfer_priv *transfer_priv = get_transfer_priv(itransfer);
|
||||
return transfer_priv->pollable_fd.overlapped;
|
||||
}
|
||||
|
||||
static inline void set_transfer_priv_handle(struct usbi_transfer *itransfer, HANDLE handle)
|
||||
{
|
||||
struct windows_transfer_priv *transfer_priv = get_transfer_priv(itransfer);
|
||||
transfer_priv->handle = handle;
|
||||
}
|
||||
|
||||
static inline struct usbdk_transfer_priv *get_usbdk_transfer_priv(struct usbi_transfer *itransfer)
|
||||
{
|
||||
struct windows_transfer_priv *transfer_priv = get_transfer_priv(itransfer);
|
||||
return &transfer_priv->usbdk_priv;
|
||||
}
|
||||
|
||||
static inline struct winusb_transfer_priv *get_winusb_transfer_priv(struct usbi_transfer *itransfer)
|
||||
{
|
||||
struct windows_transfer_priv *transfer_priv = get_transfer_priv(itransfer);
|
||||
return &transfer_priv->winusb_priv;
|
||||
}
|
||||
|
||||
extern const struct windows_backend usbdk_backend;
|
||||
extern const struct windows_backend winusb_backend;
|
||||
|
||||
unsigned long htab_hash(const char *str);
|
||||
enum libusb_transfer_status usbd_status_to_libusb_transfer_status(USBD_STATUS status);
|
||||
void windows_force_sync_completion(OVERLAPPED *overlapped, ULONG size);
|
||||
|
||||
#if defined(ENABLE_LOGGING)
|
||||
|
||||
+49
-171
@@ -48,11 +48,6 @@ static inline struct usbdk_device_priv *_usbdk_device_priv(struct libusb_device
|
||||
return (struct usbdk_device_priv *)dev->os_priv;
|
||||
}
|
||||
|
||||
static inline struct usbdk_transfer_priv *_usbdk_transfer_priv(struct usbi_transfer *itransfer)
|
||||
{
|
||||
return (struct usbdk_transfer_priv *)usbi_transfer_get_os_priv(itransfer);
|
||||
}
|
||||
|
||||
static struct {
|
||||
HMODULE module;
|
||||
|
||||
@@ -423,7 +418,7 @@ static int usbdk_open(struct libusb_device_handle *dev_handle)
|
||||
|
||||
priv->redirector_handle = usbdk_helper.StartRedirect(&priv->info.ID);
|
||||
if (priv->redirector_handle == INVALID_HANDLE_VALUE) {
|
||||
usbi_err(DEVICE_CTX(dev_handle->dev), "Redirector startup failed");
|
||||
usbi_err(HANDLE_CTX(dev_handle), "Redirector startup failed");
|
||||
return LIBUSB_ERROR_OTHER;
|
||||
}
|
||||
|
||||
@@ -463,11 +458,10 @@ static int usbdk_claim_interface(struct libusb_device_handle *dev_handle, int if
|
||||
|
||||
static int usbdk_set_interface_altsetting(struct libusb_device_handle *dev_handle, int iface, int altsetting)
|
||||
{
|
||||
struct libusb_context *ctx = HANDLE_CTX(dev_handle);
|
||||
struct usbdk_device_priv *priv = _usbdk_device_priv(dev_handle->dev);
|
||||
|
||||
if (!usbdk_helper.SetAltsetting(priv->redirector_handle, iface, altsetting)) {
|
||||
usbi_err(ctx, "SetAltsetting failed: %s", windows_error_str(0));
|
||||
usbi_err(HANDLE_CTX(dev_handle), "SetAltsetting failed: %s", windows_error_str(0));
|
||||
return LIBUSB_ERROR_NO_DEVICE;
|
||||
}
|
||||
|
||||
@@ -483,11 +477,10 @@ static int usbdk_release_interface(struct libusb_device_handle *dev_handle, int
|
||||
|
||||
static int usbdk_clear_halt(struct libusb_device_handle *dev_handle, unsigned char endpoint)
|
||||
{
|
||||
struct libusb_context *ctx = HANDLE_CTX(dev_handle);
|
||||
struct usbdk_device_priv *priv = _usbdk_device_priv(dev_handle->dev);
|
||||
|
||||
if (!usbdk_helper.ResetPipe(priv->redirector_handle, endpoint)) {
|
||||
usbi_err(ctx, "ResetPipe failed: %s", windows_error_str(0));
|
||||
usbi_err(HANDLE_CTX(dev_handle), "ResetPipe failed: %s", windows_error_str(0));
|
||||
return LIBUSB_ERROR_NO_DEVICE;
|
||||
}
|
||||
|
||||
@@ -496,11 +489,10 @@ static int usbdk_clear_halt(struct libusb_device_handle *dev_handle, unsigned ch
|
||||
|
||||
static int usbdk_reset_device(struct libusb_device_handle *dev_handle)
|
||||
{
|
||||
struct libusb_context *ctx = HANDLE_CTX(dev_handle);
|
||||
struct usbdk_device_priv *priv = _usbdk_device_priv(dev_handle->dev);
|
||||
|
||||
if (!usbdk_helper.ResetDevice(priv->redirector_handle)) {
|
||||
usbi_err(ctx, "ResetDevice failed: %s", windows_error_str(0));
|
||||
usbi_err(HANDLE_CTX(dev_handle), "ResetDevice failed: %s", windows_error_str(0));
|
||||
return LIBUSB_ERROR_NO_DEVICE;
|
||||
}
|
||||
|
||||
@@ -517,13 +509,9 @@ static void usbdk_destroy_device(struct libusb_device *dev)
|
||||
|
||||
static void usbdk_clear_transfer_priv(struct usbi_transfer *itransfer)
|
||||
{
|
||||
struct usbdk_transfer_priv *transfer_priv = _usbdk_transfer_priv(itransfer);
|
||||
struct usbdk_transfer_priv *transfer_priv = get_usbdk_transfer_priv(itransfer);
|
||||
struct libusb_transfer *transfer = USBI_TRANSFER_TO_LIBUSB_TRANSFER(itransfer);
|
||||
|
||||
usbi_close(transfer_priv->pollable_fd.fd);
|
||||
transfer_priv->pollable_fd = INVALID_WINFD;
|
||||
transfer_priv->system_handle = NULL;
|
||||
|
||||
if (transfer->type == LIBUSB_TRANSFER_TYPE_ISOCHRONOUS) {
|
||||
safe_free(transfer_priv->IsochronousPacketsArray);
|
||||
safe_free(transfer_priv->IsochronousResultsArray);
|
||||
@@ -534,9 +522,8 @@ static int usbdk_do_control_transfer(struct usbi_transfer *itransfer)
|
||||
{
|
||||
struct libusb_transfer *transfer = USBI_TRANSFER_TO_LIBUSB_TRANSFER(itransfer);
|
||||
struct usbdk_device_priv *priv = _usbdk_device_priv(transfer->dev_handle->dev);
|
||||
struct usbdk_transfer_priv *transfer_priv = _usbdk_transfer_priv(itransfer);
|
||||
struct libusb_context *ctx = TRANSFER_CTX(transfer);
|
||||
OVERLAPPED *overlapped = transfer_priv->pollable_fd.overlapped;
|
||||
struct usbdk_transfer_priv *transfer_priv = get_usbdk_transfer_priv(itransfer);
|
||||
OVERLAPPED *overlapped = get_transfer_priv_overlapped(itransfer);
|
||||
TransferResult transResult;
|
||||
|
||||
transfer_priv->request.Buffer = (PVOID64)transfer->buffer;
|
||||
@@ -555,10 +542,12 @@ static int usbdk_do_control_transfer(struct usbi_transfer *itransfer)
|
||||
case TransferSuccessAsync:
|
||||
break;
|
||||
case TransferFailure:
|
||||
usbi_err(ctx, "ControlTransfer failed: %s", windows_error_str(0));
|
||||
usbi_err(TRANSFER_CTX(transfer), "ControlTransfer failed: %s", windows_error_str(0));
|
||||
return LIBUSB_ERROR_IO;
|
||||
}
|
||||
|
||||
set_transfer_priv_handle(itransfer, priv->system_handle);
|
||||
|
||||
return LIBUSB_SUCCESS;
|
||||
}
|
||||
|
||||
@@ -566,9 +555,8 @@ static int usbdk_do_bulk_transfer(struct usbi_transfer *itransfer)
|
||||
{
|
||||
struct libusb_transfer *transfer = USBI_TRANSFER_TO_LIBUSB_TRANSFER(itransfer);
|
||||
struct usbdk_device_priv *priv = _usbdk_device_priv(transfer->dev_handle->dev);
|
||||
struct usbdk_transfer_priv *transfer_priv = _usbdk_transfer_priv(itransfer);
|
||||
struct libusb_context *ctx = TRANSFER_CTX(transfer);
|
||||
OVERLAPPED *overlapped = transfer_priv->pollable_fd.overlapped;
|
||||
struct usbdk_transfer_priv *transfer_priv = get_usbdk_transfer_priv(itransfer);
|
||||
OVERLAPPED *overlapped = get_transfer_priv_overlapped(itransfer);
|
||||
TransferResult transferRes;
|
||||
|
||||
transfer_priv->request.Buffer = (PVOID64)transfer->buffer;
|
||||
@@ -582,9 +570,6 @@ static int usbdk_do_bulk_transfer(struct usbi_transfer *itransfer)
|
||||
case LIBUSB_TRANSFER_TYPE_INTERRUPT:
|
||||
transfer_priv->request.TransferType = InterruptTransferType;
|
||||
break;
|
||||
default:
|
||||
usbi_err(ctx, "Wrong transfer type (%d) in usbdk_do_bulk_transfer", transfer->type);
|
||||
return LIBUSB_ERROR_INVALID_PARAM;
|
||||
}
|
||||
|
||||
if (IS_XFERIN(transfer))
|
||||
@@ -599,10 +584,12 @@ static int usbdk_do_bulk_transfer(struct usbi_transfer *itransfer)
|
||||
case TransferSuccessAsync:
|
||||
break;
|
||||
case TransferFailure:
|
||||
usbi_err(ctx, "ReadPipe/WritePipe failed: %s", windows_error_str(0));
|
||||
usbi_err(TRANSFER_CTX(transfer), "ReadPipe/WritePipe failed: %s", windows_error_str(0));
|
||||
return LIBUSB_ERROR_IO;
|
||||
}
|
||||
|
||||
set_transfer_priv_handle(itransfer, priv->system_handle);
|
||||
|
||||
return LIBUSB_SUCCESS;
|
||||
}
|
||||
|
||||
@@ -610,9 +597,8 @@ static int usbdk_do_iso_transfer(struct usbi_transfer *itransfer)
|
||||
{
|
||||
struct libusb_transfer *transfer = USBI_TRANSFER_TO_LIBUSB_TRANSFER(itransfer);
|
||||
struct usbdk_device_priv *priv = _usbdk_device_priv(transfer->dev_handle->dev);
|
||||
struct usbdk_transfer_priv *transfer_priv = _usbdk_transfer_priv(itransfer);
|
||||
struct libusb_context *ctx = TRANSFER_CTX(transfer);
|
||||
OVERLAPPED *overlapped = transfer_priv->pollable_fd.overlapped;
|
||||
struct usbdk_transfer_priv *transfer_priv = get_usbdk_transfer_priv(itransfer);
|
||||
OVERLAPPED *overlapped = get_transfer_priv_overlapped(itransfer);
|
||||
TransferResult transferRes;
|
||||
int i;
|
||||
|
||||
@@ -624,14 +610,14 @@ static int usbdk_do_iso_transfer(struct usbi_transfer *itransfer)
|
||||
transfer_priv->IsochronousPacketsArray = malloc(transfer->num_iso_packets * sizeof(ULONG64));
|
||||
transfer_priv->request.IsochronousPacketsArray = (PVOID64)transfer_priv->IsochronousPacketsArray;
|
||||
if (!transfer_priv->IsochronousPacketsArray) {
|
||||
usbi_err(ctx, "Allocation of IsochronousPacketsArray failed");
|
||||
usbi_err(TRANSFER_CTX(transfer), "Allocation of IsochronousPacketsArray failed");
|
||||
return LIBUSB_ERROR_NO_MEM;
|
||||
}
|
||||
|
||||
transfer_priv->IsochronousResultsArray = malloc(transfer->num_iso_packets * sizeof(USB_DK_ISO_TRANSFER_RESULT));
|
||||
transfer_priv->request.Result.IsochronousResultsArray = (PVOID64)transfer_priv->IsochronousResultsArray;
|
||||
if (!transfer_priv->IsochronousResultsArray) {
|
||||
usbi_err(ctx, "Allocation of isochronousResultsArray failed");
|
||||
usbi_err(TRANSFER_CTX(transfer), "Allocation of isochronousResultsArray failed");
|
||||
return LIBUSB_ERROR_NO_MEM;
|
||||
}
|
||||
|
||||
@@ -653,39 +639,7 @@ static int usbdk_do_iso_transfer(struct usbi_transfer *itransfer)
|
||||
return LIBUSB_ERROR_IO;
|
||||
}
|
||||
|
||||
return LIBUSB_SUCCESS;
|
||||
}
|
||||
|
||||
static int usbdk_do_submit_transfer(struct usbi_transfer *itransfer,
|
||||
short events, int (*transfer_fn)(struct usbi_transfer *))
|
||||
{
|
||||
struct libusb_transfer *transfer = USBI_TRANSFER_TO_LIBUSB_TRANSFER(itransfer);
|
||||
struct libusb_context *ctx = TRANSFER_CTX(transfer);
|
||||
struct usbdk_device_priv *priv = _usbdk_device_priv(transfer->dev_handle->dev);
|
||||
struct usbdk_transfer_priv *transfer_priv = _usbdk_transfer_priv(itransfer);
|
||||
struct winfd wfd;
|
||||
int r;
|
||||
|
||||
wfd = usbi_create_fd();
|
||||
if (wfd.fd < 0)
|
||||
return LIBUSB_ERROR_NO_MEM;
|
||||
|
||||
r = usbi_add_pollfd(ctx, wfd.fd, events);
|
||||
if (r) {
|
||||
usbi_close(wfd.fd);
|
||||
return r;
|
||||
}
|
||||
|
||||
// Use transfer_priv to store data needed for async polling
|
||||
transfer_priv->pollable_fd = wfd;
|
||||
transfer_priv->system_handle = priv->system_handle;
|
||||
|
||||
r = transfer_fn(itransfer);
|
||||
if (r != LIBUSB_SUCCESS) {
|
||||
usbi_remove_pollfd(ctx, wfd.fd);
|
||||
usbdk_clear_transfer_priv(itransfer);
|
||||
return r;
|
||||
}
|
||||
set_transfer_priv_handle(itransfer, priv->system_handle);
|
||||
|
||||
return LIBUSB_SUCCESS;
|
||||
}
|
||||
@@ -693,129 +647,55 @@ static int usbdk_do_submit_transfer(struct usbi_transfer *itransfer,
|
||||
static int usbdk_submit_transfer(struct usbi_transfer *itransfer)
|
||||
{
|
||||
struct libusb_transfer *transfer = USBI_TRANSFER_TO_LIBUSB_TRANSFER(itransfer);
|
||||
int (*transfer_fn)(struct usbi_transfer *);
|
||||
short events;
|
||||
|
||||
switch (transfer->type) {
|
||||
case LIBUSB_TRANSFER_TYPE_CONTROL:
|
||||
events = (transfer->buffer[0] & LIBUSB_ENDPOINT_IN) ? POLLIN : POLLOUT;
|
||||
transfer_fn = usbdk_do_control_transfer;
|
||||
break;
|
||||
return usbdk_do_control_transfer(itransfer);
|
||||
case LIBUSB_TRANSFER_TYPE_BULK:
|
||||
case LIBUSB_TRANSFER_TYPE_INTERRUPT:
|
||||
if (IS_XFEROUT(transfer) && (transfer->flags & LIBUSB_TRANSFER_ADD_ZERO_PACKET))
|
||||
return LIBUSB_ERROR_NOT_SUPPORTED; //TODO: Check whether we can support this in UsbDk
|
||||
events = IS_XFERIN(transfer) ? POLLIN : POLLOUT;
|
||||
transfer_fn = usbdk_do_bulk_transfer;
|
||||
break;
|
||||
return usbdk_do_bulk_transfer(itransfer);
|
||||
case LIBUSB_TRANSFER_TYPE_ISOCHRONOUS:
|
||||
events = IS_XFERIN(transfer) ? POLLIN : POLLOUT;
|
||||
transfer_fn = usbdk_do_iso_transfer;
|
||||
break;
|
||||
return usbdk_do_iso_transfer(itransfer);
|
||||
default:
|
||||
usbi_err(TRANSFER_CTX(transfer), "unknown endpoint type %d", transfer->type);
|
||||
return LIBUSB_ERROR_INVALID_PARAM;
|
||||
// Should not get here since windows_submit_transfer() validates
|
||||
// the transfer->type field
|
||||
usbi_err(TRANSFER_CTX(transfer), "unsupported endpoint type %d", transfer->type);
|
||||
return LIBUSB_ERROR_NOT_SUPPORTED;
|
||||
}
|
||||
|
||||
return usbdk_do_submit_transfer(itransfer, events, transfer_fn);
|
||||
}
|
||||
|
||||
static int usbdk_abort_transfers(struct usbi_transfer *itransfer)
|
||||
static enum libusb_transfer_status usbdk_copy_transfer_data(struct usbi_transfer *itransfer, DWORD length)
|
||||
{
|
||||
struct libusb_transfer *transfer = USBI_TRANSFER_TO_LIBUSB_TRANSFER(itransfer);
|
||||
struct libusb_context *ctx = TRANSFER_CTX(transfer);
|
||||
struct usbdk_device_priv *priv = _usbdk_device_priv(transfer->dev_handle->dev);
|
||||
struct usbdk_transfer_priv *transfer_priv = _usbdk_transfer_priv(itransfer);
|
||||
struct winfd *pollable_fd = &transfer_priv->pollable_fd;
|
||||
struct usbdk_transfer_priv *transfer_priv = get_usbdk_transfer_priv(itransfer);
|
||||
|
||||
// Use CancelIoEx to cancel just a single transfer
|
||||
if (CancelIoEx(priv->system_handle, pollable_fd->overlapped))
|
||||
return LIBUSB_SUCCESS;
|
||||
UNUSED(length);
|
||||
|
||||
usbi_warn(ctx, "CancelIoEx failed: %s", windows_error_str(0));
|
||||
return LIBUSB_ERROR_NOT_FOUND;
|
||||
}
|
||||
if (transfer->type == LIBUSB_TRANSFER_TYPE_ISOCHRONOUS) {
|
||||
ULONG64 i;
|
||||
|
||||
static int usbdk_cancel_transfer(struct usbi_transfer *itransfer)
|
||||
{
|
||||
struct libusb_transfer *transfer = USBI_TRANSFER_TO_LIBUSB_TRANSFER(itransfer);
|
||||
for (i = 0; i < transfer_priv->request.IsochronousPacketsArraySize; i++) {
|
||||
struct libusb_iso_packet_descriptor *lib_desc = &transfer->iso_packet_desc[i];
|
||||
|
||||
switch (transfer->type) {
|
||||
case LIBUSB_TRANSFER_TYPE_CONTROL:
|
||||
// Control transfers cancelled by IoCancelXXX() API
|
||||
// No special treatment needed
|
||||
return LIBUSB_SUCCESS;
|
||||
case LIBUSB_TRANSFER_TYPE_BULK:
|
||||
case LIBUSB_TRANSFER_TYPE_INTERRUPT:
|
||||
case LIBUSB_TRANSFER_TYPE_ISOCHRONOUS:
|
||||
return usbdk_abort_transfers(itransfer);
|
||||
default:
|
||||
usbi_err(ITRANSFER_CTX(itransfer), "unknown endpoint type %d", transfer->type);
|
||||
return LIBUSB_ERROR_INVALID_PARAM;
|
||||
}
|
||||
}
|
||||
|
||||
static int usbdk_copy_transfer_data(struct usbi_transfer *itransfer, uint32_t io_size)
|
||||
{
|
||||
itransfer->transferred += io_size;
|
||||
return LIBUSB_TRANSFER_COMPLETED;
|
||||
}
|
||||
|
||||
static int usbdk_get_transfer_fd(struct usbi_transfer *itransfer)
|
||||
{
|
||||
struct usbdk_transfer_priv *transfer_priv = _usbdk_transfer_priv(itransfer);
|
||||
return transfer_priv->pollable_fd.fd;
|
||||
}
|
||||
|
||||
static DWORD usbdk_translate_usbd_status(USBD_STATUS UsbdStatus)
|
||||
{
|
||||
if (USBD_SUCCESS(UsbdStatus))
|
||||
return NO_ERROR;
|
||||
|
||||
switch (UsbdStatus) {
|
||||
case USBD_STATUS_TIMEOUT:
|
||||
return ERROR_SEM_TIMEOUT;
|
||||
case USBD_STATUS_CANCELED:
|
||||
return ERROR_OPERATION_ABORTED;
|
||||
default:
|
||||
return ERROR_GEN_FAILURE;
|
||||
}
|
||||
}
|
||||
|
||||
static void usbdk_get_overlapped_result(struct usbi_transfer *itransfer, DWORD *io_result, DWORD *io_size)
|
||||
{
|
||||
struct usbdk_transfer_priv *transfer_priv = _usbdk_transfer_priv(itransfer);
|
||||
struct winfd *pollable_fd = &transfer_priv->pollable_fd;
|
||||
|
||||
if (HasOverlappedIoCompletedSync(pollable_fd->overlapped) // Handle async requests that completed synchronously first
|
||||
|| GetOverlappedResult(transfer_priv->system_handle, pollable_fd->overlapped, io_size, FALSE)) { // Regular async overlapped
|
||||
struct libusb_transfer *transfer = USBI_TRANSFER_TO_LIBUSB_TRANSFER(itransfer);
|
||||
|
||||
if (transfer->type == LIBUSB_TRANSFER_TYPE_ISOCHRONOUS) {
|
||||
ULONG64 i;
|
||||
for (i = 0; i < transfer_priv->request.IsochronousPacketsArraySize; i++) {
|
||||
struct libusb_iso_packet_descriptor *lib_desc = &transfer->iso_packet_desc[i];
|
||||
|
||||
switch (transfer_priv->IsochronousResultsArray[i].TransferResult) {
|
||||
case STATUS_SUCCESS:
|
||||
case STATUS_CANCELLED:
|
||||
case STATUS_REQUEST_CANCELED:
|
||||
lib_desc->status = LIBUSB_TRANSFER_COMPLETED; // == ERROR_SUCCESS
|
||||
break;
|
||||
default:
|
||||
lib_desc->status = LIBUSB_TRANSFER_ERROR; // ERROR_UNKNOWN_EXCEPTION;
|
||||
break;
|
||||
}
|
||||
|
||||
lib_desc->actual_length = (unsigned int)transfer_priv->IsochronousResultsArray[i].ActualLength;
|
||||
switch (transfer_priv->IsochronousResultsArray[i].TransferResult) {
|
||||
case STATUS_SUCCESS:
|
||||
case STATUS_CANCELLED:
|
||||
case STATUS_REQUEST_CANCELED:
|
||||
lib_desc->status = LIBUSB_TRANSFER_COMPLETED; // == ERROR_SUCCESS
|
||||
break;
|
||||
default:
|
||||
lib_desc->status = LIBUSB_TRANSFER_ERROR; // ERROR_UNKNOWN_EXCEPTION;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
*io_size = (DWORD)transfer_priv->request.Result.GenResult.BytesTransferred;
|
||||
*io_result = usbdk_translate_usbd_status((USBD_STATUS)transfer_priv->request.Result.GenResult.UsbdStatus);
|
||||
} else {
|
||||
*io_result = GetLastError();
|
||||
lib_desc->actual_length = (unsigned int)transfer_priv->IsochronousResultsArray[i].ActualLength;
|
||||
}
|
||||
}
|
||||
|
||||
itransfer->transferred += (int)transfer_priv->request.Result.GenResult.BytesTransferred;
|
||||
return usbd_status_to_libusb_transfer_status((USBD_STATUS)transfer_priv->request.Result.GenResult.UsbdStatus);
|
||||
}
|
||||
|
||||
const struct windows_backend usbdk_backend = {
|
||||
@@ -837,9 +717,7 @@ const struct windows_backend usbdk_backend = {
|
||||
usbdk_reset_device,
|
||||
usbdk_destroy_device,
|
||||
usbdk_submit_transfer,
|
||||
usbdk_cancel_transfer,
|
||||
NULL, /* cancel_transfer */
|
||||
usbdk_clear_transfer_priv,
|
||||
usbdk_copy_transfer_data,
|
||||
usbdk_get_transfer_fd,
|
||||
usbdk_get_overlapped_result,
|
||||
};
|
||||
+479
-736
File diff suppressed because it is too large.
Load diff
+80
-78
@@ -69,6 +69,8 @@ const GUID GUID_DEVINTERFACE_USB_HUB = {0xF18A0E88, 0xC30C, 0x11D0, {0x88, 0x15,
|
||||
const GUID GUID_DEVINTERFACE_LIBUSB0_FILTER = {0xF9F3FF14, 0xAE21, 0x48A0, {0x8A, 0x25, 0x80, 0x11, 0xA7, 0xA9, 0x31, 0xD9}};
|
||||
#endif
|
||||
|
||||
// The following define MUST be == sizeof(USB_DESCRIPTOR_REQUEST)
|
||||
#define USB_DESCRIPTOR_REQUEST_SIZE 12U
|
||||
|
||||
/*
|
||||
* Multiple USB API backend support
|
||||
@@ -93,7 +95,7 @@ struct windows_usb_api_backend {
|
||||
const char * const designation;
|
||||
const char * const * const driver_name_list; // Driver name, without .sys, e.g. "usbccgp"
|
||||
const uint8_t nb_driver_names;
|
||||
int (*init)(struct libusb_context *ctx);
|
||||
bool (*init)(struct libusb_context *ctx);
|
||||
void (*exit)(void);
|
||||
int (*open)(int sub_api, struct libusb_device_handle *dev_handle);
|
||||
void (*close)(int sub_api, struct libusb_device_handle *dev_handle);
|
||||
@@ -106,9 +108,8 @@ struct windows_usb_api_backend {
|
||||
int (*submit_bulk_transfer)(int sub_api, struct usbi_transfer *itransfer);
|
||||
int (*submit_iso_transfer)(int sub_api, struct usbi_transfer *itransfer);
|
||||
int (*submit_control_transfer)(int sub_api, struct usbi_transfer *itransfer);
|
||||
int (*abort_control)(int sub_api, struct usbi_transfer *itransfer);
|
||||
int (*abort_transfers)(int sub_api, struct usbi_transfer *itransfer);
|
||||
int (*copy_transfer_data)(int sub_api, struct usbi_transfer *itransfer, uint32_t io_size);
|
||||
int (*cancel_transfer)(int sub_api, struct usbi_transfer *itransfer);
|
||||
enum libusb_transfer_status (*copy_transfer_data)(int sub_api, struct usbi_transfer *itransfer, DWORD length);
|
||||
};
|
||||
|
||||
extern const struct windows_usb_api_backend usb_api_backend[USB_API_MAX];
|
||||
@@ -226,8 +227,11 @@ static inline void winusb_device_priv_release(struct libusb_device *dev)
|
||||
free(p->dev_id);
|
||||
free(p->path);
|
||||
if ((dev->num_configurations > 0) && (p->config_descriptor != NULL)) {
|
||||
for (i = 0; i < dev->num_configurations; i++)
|
||||
free(p->config_descriptor[i]);
|
||||
for (i = 0; i < dev->num_configurations; i++) {
|
||||
if (p->config_descriptor[i] == NULL)
|
||||
continue;
|
||||
free((UCHAR *)p->config_descriptor[i] - USB_DESCRIPTOR_REQUEST_SIZE);
|
||||
}
|
||||
}
|
||||
free(p->config_descriptor);
|
||||
free(p->hid);
|
||||
@@ -422,6 +426,12 @@ typedef struct _USB_NODE_CONNECTION_INFORMATION_EX_V2 {
|
||||
/* libusbK */
|
||||
#define ISO_ALWAYS_START_ASAP 0x21
|
||||
|
||||
typedef struct _USBD_ISO_PACKET_DESCRIPTOR {
|
||||
ULONG Offset;
|
||||
ULONG Length;
|
||||
USBD_STATUS Status;
|
||||
} USBD_ISO_PACKET_DESCRIPTOR, *PUSBD_ISO_PACKET_DESCRIPTOR;
|
||||
|
||||
typedef enum _USBD_PIPE_TYPE {
|
||||
UsbdPipeTypeControl,
|
||||
UsbdPipeTypeIsochronous,
|
||||
@@ -429,6 +439,14 @@ typedef enum _USBD_PIPE_TYPE {
|
||||
UsbdPipeTypeInterrupt
|
||||
} USBD_PIPE_TYPE;
|
||||
|
||||
typedef struct {
|
||||
USBD_PIPE_TYPE PipeType;
|
||||
UCHAR PipeId;
|
||||
USHORT MaximumPacketSize;
|
||||
UCHAR Interval;
|
||||
ULONG MaximumBytesPerInterval;
|
||||
} WINUSB_PIPE_INFORMATION_EX, *PWINUSB_PIPE_INFORMATION_EX;
|
||||
|
||||
#include <pshpack1.h>
|
||||
|
||||
typedef struct _WINUSB_SETUP_PACKET {
|
||||
@@ -441,7 +459,8 @@ typedef struct _WINUSB_SETUP_PACKET {
|
||||
|
||||
#include <poppack.h>
|
||||
|
||||
typedef void *WINUSB_INTERFACE_HANDLE, *PWINUSB_INTERFACE_HANDLE;
|
||||
typedef PVOID WINUSB_INTERFACE_HANDLE, *PWINUSB_INTERFACE_HANDLE;
|
||||
typedef PVOID WINUSB_ISOCH_BUFFER_HANDLE, *PWINUSB_ISOCH_BUFFER_HANDLE;
|
||||
|
||||
typedef BOOL (WINAPI *WinUsb_AbortPipe_t)(
|
||||
WINUSB_INTERFACE_HANDLE InterfaceHandle,
|
||||
@@ -471,6 +490,21 @@ typedef BOOL (WINAPI *WinUsb_Initialize_t)(
|
||||
HANDLE DeviceHandle,
|
||||
PWINUSB_INTERFACE_HANDLE InterfaceHandle
|
||||
);
|
||||
typedef BOOL (WINAPI *WinUsb_QueryPipeEx_t)(
|
||||
WINUSB_INTERFACE_HANDLE InterfaceHandle,
|
||||
UCHAR AlternateInterfaceHandle,
|
||||
UCHAR PipeIndex,
|
||||
PWINUSB_PIPE_INFORMATION_EX PipeInformationEx
|
||||
);
|
||||
typedef BOOL (WINAPI *WinUsb_ReadIsochPipeAsap_t)(
|
||||
PWINUSB_ISOCH_BUFFER_HANDLE BufferHandle,
|
||||
ULONG Offset,
|
||||
ULONG Length,
|
||||
BOOL ContinueStream,
|
||||
ULONG NumberOfPackets,
|
||||
PUSBD_ISO_PACKET_DESCRIPTOR IsoPacketDescriptors,
|
||||
LPOVERLAPPED Overlapped
|
||||
);
|
||||
typedef BOOL (WINAPI *WinUsb_ReadPipe_t)(
|
||||
WINUSB_INTERFACE_HANDLE InterfaceHandle,
|
||||
UCHAR PipeID,
|
||||
@@ -479,8 +513,12 @@ typedef BOOL (WINAPI *WinUsb_ReadPipe_t)(
|
||||
PULONG LengthTransferred,
|
||||
LPOVERLAPPED Overlapped
|
||||
);
|
||||
typedef BOOL (WINAPI *WinUsb_ResetDevice_t)(
|
||||
WINUSB_INTERFACE_HANDLE InterfaceHandle
|
||||
typedef BOOL (WINAPI *WinUsb_RegisterIsochBuffer_t)(
|
||||
WINUSB_INTERFACE_HANDLE InterfaceHandle,
|
||||
UCHAR PipeID,
|
||||
PVOID Buffer,
|
||||
ULONG BufferLength,
|
||||
PWINUSB_ISOCH_BUFFER_HANDLE BufferHandle
|
||||
);
|
||||
typedef BOOL (WINAPI *WinUsb_ResetPipe_t)(
|
||||
WINUSB_INTERFACE_HANDLE InterfaceHandle,
|
||||
@@ -497,29 +535,9 @@ typedef BOOL (WINAPI *WinUsb_SetPipePolicy_t)(
|
||||
ULONG ValueLength,
|
||||
PVOID Value
|
||||
);
|
||||
typedef BOOL (WINAPI *WinUsb_WritePipe_t)(
|
||||
WINUSB_INTERFACE_HANDLE InterfaceHandle,
|
||||
UCHAR PipeID,
|
||||
PUCHAR Buffer,
|
||||
ULONG BufferLength,
|
||||
PULONG LengthTransferred,
|
||||
LPOVERLAPPED Overlapped
|
||||
);
|
||||
|
||||
typedef PVOID WINUSB_ISOCH_BUFFER_HANDLE, *PWINUSB_ISOCH_BUFFER_HANDLE;
|
||||
|
||||
typedef BOOL (WINAPI *WinUsb_RegisterIsochBuffer_t)(
|
||||
WINUSB_INTERFACE_HANDLE InterfaceHandle,
|
||||
UCHAR PipeID,
|
||||
PVOID Buffer,
|
||||
ULONG BufferLength,
|
||||
PWINUSB_ISOCH_BUFFER_HANDLE BufferHandle
|
||||
);
|
||||
|
||||
typedef BOOL (WINAPI *WinUsb_UnregisterIsochBuffer_t)(
|
||||
WINUSB_ISOCH_BUFFER_HANDLE BufferHandle
|
||||
);
|
||||
|
||||
typedef BOOL (WINAPI *WinUsb_WriteIsochPipeAsap_t)(
|
||||
WINUSB_ISOCH_BUFFER_HANDLE BufferHandle,
|
||||
ULONG Offset,
|
||||
@@ -527,37 +545,13 @@ typedef BOOL (WINAPI *WinUsb_WriteIsochPipeAsap_t)(
|
||||
BOOL ContinueStream,
|
||||
LPOVERLAPPED Overlapped
|
||||
);
|
||||
|
||||
typedef LONG USBD_STATUS;
|
||||
typedef struct {
|
||||
ULONG Offset;
|
||||
ULONG Length;
|
||||
USBD_STATUS Status;
|
||||
} USBD_ISO_PACKET_DESCRIPTOR, *PUSBD_ISO_PACKET_DESCRIPTOR;
|
||||
|
||||
typedef BOOL (WINAPI *WinUsb_ReadIsochPipeAsap_t)(
|
||||
PWINUSB_ISOCH_BUFFER_HANDLE BufferHandle,
|
||||
ULONG Offset,
|
||||
ULONG Length,
|
||||
BOOL ContinueStream,
|
||||
ULONG NumberOfPackets,
|
||||
PUSBD_ISO_PACKET_DESCRIPTOR IsoPacketDescriptors,
|
||||
LPOVERLAPPED Overlapped
|
||||
);
|
||||
|
||||
typedef struct {
|
||||
USBD_PIPE_TYPE PipeType;
|
||||
UCHAR PipeId;
|
||||
USHORT MaximumPacketSize;
|
||||
UCHAR Interval;
|
||||
ULONG MaximumBytesPerInterval;
|
||||
} WINUSB_PIPE_INFORMATION_EX, *PWINUSB_PIPE_INFORMATION_EX;
|
||||
|
||||
typedef BOOL (WINAPI *WinUsb_QueryPipeEx_t)(
|
||||
typedef BOOL (WINAPI *WinUsb_WritePipe_t)(
|
||||
WINUSB_INTERFACE_HANDLE InterfaceHandle,
|
||||
UCHAR AlternateInterfaceHandle,
|
||||
UCHAR PipeIndex,
|
||||
PWINUSB_PIPE_INFORMATION_EX PipeInformationEx
|
||||
UCHAR PipeID,
|
||||
PUCHAR Buffer,
|
||||
ULONG BufferLength,
|
||||
PULONG LengthTransferred,
|
||||
LPOVERLAPPED Overlapped
|
||||
);
|
||||
|
||||
/* /!\ These must match the ones from the official libusbk.h */
|
||||
@@ -607,15 +601,19 @@ typedef struct _KLIB_VERSION {
|
||||
} KLIB_VERSION, *PKLIB_VERSION;
|
||||
|
||||
typedef BOOL (WINAPI *LibK_GetProcAddress_t)(
|
||||
PVOID *ProcAddress,
|
||||
ULONG DriverID,
|
||||
ULONG FunctionID
|
||||
PVOID ProcAddress,
|
||||
INT DriverID,
|
||||
INT FunctionID
|
||||
);
|
||||
|
||||
typedef VOID (WINAPI *LibK_GetVersion_t)(
|
||||
PKLIB_VERSION Version
|
||||
);
|
||||
|
||||
typedef BOOL (WINAPI *LibK_ResetDevice_t)(
|
||||
WINUSB_INTERFACE_HANDLE InterfaceHandle
|
||||
);
|
||||
|
||||
//KISO_PACKET is equivalent of libusb_iso_packet_descriptor except uses absolute "offset" field instead of sequential Lengths
|
||||
typedef struct _KISO_PACKET {
|
||||
UINT offset;
|
||||
@@ -638,7 +636,7 @@ typedef struct _KISO_CONTEXT {
|
||||
KISO_PACKET IsoPackets[0];
|
||||
} KISO_CONTEXT, *PKISO_CONTEXT;
|
||||
|
||||
typedef BOOL(WINAPI *WinUsb_IsoReadPipe_t)(
|
||||
typedef BOOL(WINAPI *LibK_IsoReadPipe_t)(
|
||||
WINUSB_INTERFACE_HANDLE InterfaceHandle,
|
||||
UCHAR PipeID,
|
||||
PUCHAR Buffer,
|
||||
@@ -647,7 +645,7 @@ typedef BOOL(WINAPI *WinUsb_IsoReadPipe_t)(
|
||||
PKISO_CONTEXT IsoContext
|
||||
);
|
||||
|
||||
typedef BOOL(WINAPI *WinUsb_IsoWritePipe_t)(
|
||||
typedef BOOL(WINAPI *LibK_IsoWritePipe_t)(
|
||||
WINUSB_INTERFACE_HANDLE InterfaceHandle,
|
||||
UCHAR PipeID,
|
||||
PUCHAR Buffer,
|
||||
@@ -657,8 +655,7 @@ typedef BOOL(WINAPI *WinUsb_IsoWritePipe_t)(
|
||||
);
|
||||
|
||||
struct winusb_interface {
|
||||
bool initialized;
|
||||
bool CancelIoEx_supported;
|
||||
HMODULE hDll;
|
||||
WinUsb_AbortPipe_t AbortPipe;
|
||||
WinUsb_ControlTransfer_t ControlTransfer;
|
||||
WinUsb_FlushPipe_t FlushPipe;
|
||||
@@ -666,22 +663,27 @@ struct winusb_interface {
|
||||
WinUsb_GetAssociatedInterface_t GetAssociatedInterface;
|
||||
WinUsb_Initialize_t Initialize;
|
||||
WinUsb_ReadPipe_t ReadPipe;
|
||||
WinUsb_ResetDevice_t ResetDevice;
|
||||
WinUsb_ResetPipe_t ResetPipe;
|
||||
WinUsb_SetCurrentAlternateSetting_t SetCurrentAlternateSetting;
|
||||
WinUsb_SetPipePolicy_t SetPipePolicy;
|
||||
WinUsb_WritePipe_t WritePipe;
|
||||
|
||||
// Isochoronous functions for LibUSBk sub api:
|
||||
WinUsb_IsoReadPipe_t IsoReadPipe;
|
||||
WinUsb_IsoWritePipe_t IsoWritePipe;
|
||||
|
||||
// Isochronous functions for Microsoft WinUSB sub api (native WinUSB):
|
||||
WinUsb_RegisterIsochBuffer_t RegisterIsochBuffer;
|
||||
WinUsb_UnregisterIsochBuffer_t UnregisterIsochBuffer;
|
||||
WinUsb_WriteIsochPipeAsap_t WriteIsochPipeAsap;
|
||||
WinUsb_ReadIsochPipeAsap_t ReadIsochPipeAsap;
|
||||
WinUsb_QueryPipeEx_t QueryPipeEx;
|
||||
union {
|
||||
struct {
|
||||
// Isochoronous functions for libusbK sub api:
|
||||
LibK_IsoReadPipe_t IsoReadPipe;
|
||||
LibK_IsoWritePipe_t IsoWritePipe;
|
||||
// Reset device function for libusbK sub api:
|
||||
LibK_ResetDevice_t ResetDevice;
|
||||
};
|
||||
struct {
|
||||
// Isochronous functions for WinUSB sub api:
|
||||
WinUsb_QueryPipeEx_t QueryPipeEx;
|
||||
WinUsb_ReadIsochPipeAsap_t ReadIsochPipeAsap;
|
||||
WinUsb_RegisterIsochBuffer_t RegisterIsochBuffer;
|
||||
WinUsb_UnregisterIsochBuffer_t UnregisterIsochBuffer;
|
||||
WinUsb_WriteIsochPipeAsap_t WriteIsochPipeAsap;
|
||||
};
|
||||
};
|
||||
};
|
||||
|
||||
/* hid.dll interface */
|
||||
|
||||
@@ -1 +1 @@
|
||||
#define LIBUSB_NANO 11453
|
||||
#define LIBUSB_NANO 11454
|
||||
Reference in new issue
Block a user