summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorChris Dickens <christopher.a.dickens@gmail.com>2016-01-26 23:40:33 -0800
committerChris Dickens <christopher.a.dickens@gmail.com>2016-01-26 23:52:24 -0800
commita3ec36d3ecc268d2f8b9f1f9800cd438c57cab56 (patch)
treef47df79cebf3b99d6a54bc9035620531c05416db
parent48bfef6c1d1d2275dc1999a8b7b55954983d809c (diff)
downloadlibusb-a3ec36d3ecc268d2f8b9f1f9800cd438c57cab56.tar.gz
Windows: Source file cleanup
This commit addresses a few different issues: 1) Whitespace made consistent with the rest of the library source 2) Formatting of function and variable declarations made consistent with the rest of the library source 3) Functions and variables made static where possible 4) Definitions in header files moved if not used publicly Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
-rw-r--r--libusb/os/windows_common.h4
-rw-r--r--libusb/os/windows_nt_common.c739
-rw-r--r--libusb/os/windows_nt_common.h4
-rw-r--r--libusb/os/windows_usbdk.c1299
-rw-r--r--libusb/os/windows_usbdk.h174
-rw-r--r--libusb/os/windows_winusb.c1272
-rw-r--r--libusb/os/windows_winusb.h505
-rw-r--r--libusb/version_nano.h2
8 files changed, 2009 insertions, 1990 deletions
diff --git a/libusb/os/windows_common.h b/libusb/os/windows_common.h
index 466f31d..45c59af 100644
--- a/libusb/os/windows_common.h
+++ b/libusb/os/windows_common.h
@@ -58,9 +58,7 @@
#define ARRAYSIZE(A) (sizeof(A)/sizeof((A)[0]))
#endif
-#define ERR_BUFFER_SIZE 256
-#define TIMER_REQUEST_RETRY_MS 100
-#define MAX_TIMER_SEMAPHORES 128
+#define ERR_BUFFER_SIZE 256
/*
diff --git a/libusb/os/windows_nt_common.c b/libusb/os/windows_nt_common.c
index d99e3b8..8972865 100644
--- a/libusb/os/windows_nt_common.c
+++ b/libusb/os/windows_nt_common.c
@@ -23,10 +23,10 @@
*/
#include <config.h>
-#include <stdio.h>
+
#include <inttypes.h>
#include <process.h>
-
+#include <stdio.h>
#include "libusbi.h"
#include "windows_common.h"
@@ -36,91 +36,96 @@
const uint64_t epoch_time = UINT64_C(116444736000000000); // 1970.01.01 00:00:000 in MS Filetime
// Global variables for clock_gettime mechanism
-uint64_t hires_ticks_to_ps;
-uint64_t hires_frequency;
+static uint64_t hires_ticks_to_ps;
+static uint64_t hires_frequency;
-#define WM_TIMER_REQUEST (WM_USER + 1)
-#define WM_TIMER_EXIT (WM_USER + 2)
+#define TIMER_REQUEST_RETRY_MS 100
+#define WM_TIMER_REQUEST (WM_USER + 1)
+#define WM_TIMER_EXIT (WM_USER + 2)
// used for monotonic clock_gettime()
struct timer_request {
- struct timespec *tp;
- HANDLE event;
+ struct timespec *tp;
+ HANDLE event;
};
// Timer thread
-HANDLE timer_thread = NULL;
-DWORD timer_thread_id = 0;
+static HANDLE timer_thread = NULL;
+static DWORD timer_thread_id = 0;
/* User32 dependencies */
DLL_DECLARE_PREFIXED(WINAPI, BOOL, p, GetMessageA, (LPMSG, HWND, UINT, UINT));
DLL_DECLARE_PREFIXED(WINAPI, BOOL, p, PeekMessageA, (LPMSG, HWND, UINT, UINT, UINT));
DLL_DECLARE_PREFIXED(WINAPI, BOOL, p, PostThreadMessageA, (DWORD, UINT, WPARAM, LPARAM));
-static unsigned __stdcall windows_clock_gettime_threaded(void* param);
+static unsigned __stdcall windows_clock_gettime_threaded(void *param);
/*
* Converts a windows error to human readable string
* uses retval as errorcode, or, if 0, use GetLastError()
*/
#if defined(ENABLE_LOGGING)
-char *windows_error_str(uint32_t retval)
+const char *windows_error_str(DWORD retval)
{
- static char err_string[ERR_BUFFER_SIZE];
-
- DWORD size;
- ssize_t i;
- uint32_t error_code, format_error;
-
- error_code = retval ? retval : GetLastError();
-
- safe_sprintf(err_string, ERR_BUFFER_SIZE, "[%u] ", error_code);
-
- // Translate codes returned by SetupAPI. The ones we are dealing with are either
- // in 0x0000xxxx or 0xE000xxxx and can be distinguished from standard error codes.
- // See http://msdn.microsoft.com/en-us/library/windows/hardware/ff545011.aspx
- switch (error_code & 0xE0000000) {
- case 0:
- error_code = HRESULT_FROM_WIN32(error_code); // Still leaves ERROR_SUCCESS unmodified
- break;
- case 0xE0000000:
- error_code = 0x80000000 | (FACILITY_SETUPAPI << 16) | (error_code & 0x0000FFFF);
- break;
- default:
- break;
- }
-
- size = FormatMessageA(FORMAT_MESSAGE_FROM_SYSTEM, NULL, error_code,
- MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), &err_string[safe_strlen(err_string)],
- ERR_BUFFER_SIZE - (DWORD)safe_strlen(err_string), NULL);
- if (size == 0) {
- format_error = GetLastError();
- if (format_error)
- safe_sprintf(err_string, ERR_BUFFER_SIZE,
- "Windows error code %u (FormatMessage error code %u)", error_code, format_error);
- else
- safe_sprintf(err_string, ERR_BUFFER_SIZE, "Unknown error code %u", error_code);
- }
- else {
- // Remove CR/LF terminators
- for (i = safe_strlen(err_string) - 1; (i >= 0) && ((err_string[i] == 0x0A) || (err_string[i] == 0x0D)); i--) {
- err_string[i] = 0;
- }
- }
- return err_string;
+ static char err_string[ERR_BUFFER_SIZE];
+
+ DWORD error_code, format_error;
+ DWORD size;
+ ssize_t i;
+
+ error_code = retval ? retval : GetLastError();
+
+ safe_sprintf(err_string, ERR_BUFFER_SIZE, "[%u] ", error_code);
+
+ // Translate codes returned by SetupAPI. The ones we are dealing with are either
+ // in 0x0000xxxx or 0xE000xxxx and can be distinguished from standard error codes.
+ // See http://msdn.microsoft.com/en-us/library/windows/hardware/ff545011.aspx
+ switch (error_code & 0xE0000000) {
+ case 0:
+ error_code = HRESULT_FROM_WIN32(error_code); // Still leaves ERROR_SUCCESS unmodified
+ break;
+ case 0xE0000000:
+ error_code = 0x80000000 | (FACILITY_SETUPAPI << 16) | (error_code & 0x0000FFFF);
+ break;
+ default:
+ break;
+ }
+
+ size = FormatMessageA(FORMAT_MESSAGE_FROM_SYSTEM, NULL, error_code,
+ MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), &err_string[safe_strlen(err_string)],
+ ERR_BUFFER_SIZE - (DWORD)safe_strlen(err_string), NULL);
+ if (size == 0) {
+ format_error = GetLastError();
+ if (format_error)
+ safe_sprintf(err_string, ERR_BUFFER_SIZE,
+ "Windows error code %u (FormatMessage error code %u)", error_code, format_error);
+ else
+ safe_sprintf(err_string, ERR_BUFFER_SIZE, "Unknown error code %u", error_code);
+ }
+ else {
+ // Remove CR/LF terminators
+ for (i = safe_strlen(err_string) - 1; (i >= 0) && ((err_string[i] == 0x0A) || (err_string[i] == 0x0D)); i--)
+ err_string[i] = 0;
+ }
+
+ return err_string;
}
#endif
/* Hash table functions - modified From glibc 2.3.2:
[Aho,Sethi,Ullman] Compilers: Principles, Techniques and Tools, 1986
[Knuth] The Art of Computer Programming, part 3 (6.4) */
+
+#define HTAB_SIZE 1021
+
typedef struct htab_entry {
unsigned long used;
char* str;
} htab_entry;
-htab_entry* htab_table = NULL;
-usbi_mutex_t htab_write_mutex = NULL;
-unsigned long htab_size, htab_filled;
+
+static htab_entry *htab_table = NULL;
+static usbi_mutex_t htab_write_mutex = NULL;
+static unsigned long htab_size, htab_filled;
/* For the used double hash method the table size has to be a prime. To
correct the user given table size we need a prime test. This trivial
@@ -141,7 +146,7 @@ static int isprime(unsigned long number)
We allocate one element more as the found prime number says.
This is done for more effective indexing as explained in the
comment for the hash function. */
-int htab_create(struct libusb_context *ctx, unsigned long nel)
+static bool htab_create(struct libusb_context *ctx, unsigned long nel)
{
if (htab_table != NULL) {
usbi_err(ctx, "hash table already allocated");
@@ -152,7 +157,7 @@ int htab_create(struct libusb_context *ctx, unsigned long nel)
// Change nel to the first prime number not smaller as nel.
nel |= 1;
- while(!isprime(nel))
+ while (!isprime(nel))
nel += 2;
htab_size = nel;
@@ -160,47 +165,47 @@ int htab_create(struct libusb_context *ctx, unsigned long nel)
htab_filled = 0;
// allocate memory and zero out.
- htab_table = (htab_entry*) calloc(htab_size + 1, sizeof(htab_entry));
+ htab_table = calloc(htab_size + 1, sizeof(htab_entry));
if (htab_table == NULL) {
usbi_err(ctx, "could not allocate space for hash table");
- return 0;
+ return false;
}
- return 1;
+ return true;
}
/* After using the hash table it has to be destroyed. */
-void htab_destroy(void)
+static void htab_destroy(void)
{
- size_t i;
- if (htab_table == NULL) {
+ unsigned long i;
+
+ if (htab_table == NULL)
return;
- }
- for (i=0; i<htab_size; i++) {
- if (htab_table[i].used) {
+ for (i = 0; i < htab_size; i++) {
+ if (htab_table[i].used)
safe_free(htab_table[i].str);
- }
}
+
usbi_mutex_destroy(&htab_write_mutex);
safe_free(htab_table);
}
/* This is the search function. It uses double hashing with open addressing.
- We use an trick to speed up the lookup. The table is created with one
+ We use a trick to speed up the lookup. The table is created with one
more element available. This enables us to use the index zero special.
This index will never be used because we store the first hash index in
the field used where zero means not used. Every other value means used.
The used field can be used as a first fast comparison for equality of
the stored and the parameter value. This helps to prevent unnecessary
expensive calls of strcmp. */
-unsigned long htab_hash(char* str)
+unsigned long htab_hash(const char *str)
{
unsigned long hval, hval2;
unsigned long idx;
unsigned long r = 5381;
int c;
- char* sz = str;
+ const char *sz = str;
if (str == NULL)
return 0;
@@ -220,11 +225,9 @@ unsigned long htab_hash(char* str)
idx = hval;
if (htab_table[idx].used) {
- if ( (htab_table[idx].used == hval)
- && (safe_strcmp(str, htab_table[idx].str) == 0) ) {
- // existing hash
- return idx;
- }
+ if ((htab_table[idx].used == hval) && (safe_strcmp(str, htab_table[idx].str) == 0))
+ return idx; // existing hash
+
usbi_dbg("hash collision ('%s' vs '%s')", str, htab_table[idx].str);
// Second hash function, as suggested in [Knuth]
@@ -232,22 +235,18 @@ unsigned long htab_hash(char* str)
do {
// Because size is prime this guarantees to step through all available indexes
- if (idx <= hval2) {
+ if (idx <= hval2)
idx = htab_size + idx - hval2;
- } else {
+ else
idx -= hval2;
- }
// If we visited all entries leave the loop unsuccessfully
- if (idx == hval) {
+ if (idx == hval)
break;
- }
// If entry is found use it.
- if ( (htab_table[idx].used == hval)
- && (safe_strcmp(str, htab_table[idx].str) == 0) ) {
+ if ((htab_table[idx].used == hval) && (safe_strcmp(str, htab_table[idx].str) == 0))
return idx;
- }
}
while (htab_table[idx].used);
}
@@ -268,13 +267,13 @@ unsigned long htab_hash(char* str)
// string (same hash, different string) at the same time is extremely low
safe_free(htab_table[idx].str);
htab_table[idx].used = hval;
- htab_table[idx].str = (char*) malloc(safe_strlen(str)+1);
+ htab_table[idx].str = malloc(safe_strlen(str) + 1);
if (htab_table[idx].str == NULL) {
usbi_err(NULL, "could not duplicate string for hash table");
usbi_mutex_unlock(&htab_write_mutex);
return 0;
}
- memcpy(htab_table[idx].str, str, safe_strlen(str)+1);
+ memcpy(htab_table[idx].str, str, safe_strlen(str) + 1);
++htab_filled;
usbi_mutex_unlock(&htab_write_mutex);
@@ -283,334 +282,322 @@ unsigned long htab_hash(char* str)
static int windows_init_dlls(void)
{
- DLL_LOAD_PREFIXED(User32.dll, p, GetMessageA, TRUE);
- DLL_LOAD_PREFIXED(User32.dll, p, PeekMessageA, TRUE);
- DLL_LOAD_PREFIXED(User32.dll, p, PostThreadMessageA, TRUE);
- return LIBUSB_SUCCESS;
+ DLL_LOAD_PREFIXED(User32.dll, p, GetMessageA, TRUE);
+ DLL_LOAD_PREFIXED(User32.dll, p, PeekMessageA, TRUE);
+ DLL_LOAD_PREFIXED(User32.dll, p, PostThreadMessageA, TRUE);
+
+ return LIBUSB_SUCCESS;
}
-bool windows_init_clock(struct libusb_context *ctx)
+static bool windows_init_clock(struct libusb_context *ctx)
{
- DWORD_PTR affinity, dummy;
- HANDLE event = NULL;
- LARGE_INTEGER li_frequency;
- int i;
-
- if (QueryPerformanceFrequency(&li_frequency)) {
- // Load DLL imports
- if (windows_init_dlls() != LIBUSB_SUCCESS) {
- usbi_err(ctx, "could not resolve DLL functions");
- return false;
- }
-
- // The hires frequency can go as high as 4 GHz, so we'll use a conversion
- // to picoseconds to compute the tv_nsecs part in clock_gettime
- hires_frequency = li_frequency.QuadPart;
- hires_ticks_to_ps = UINT64_C(1000000000000) / hires_frequency;
- usbi_dbg("hires timer available (Frequency: %"PRIu64" Hz)", hires_frequency);
-
- // Because QueryPerformanceCounter might report different values when
- // running on different cores, we create a separate thread for the timer
- // calls, which we glue to the first available core always to prevent timing discrepancies.
- if (!GetProcessAffinityMask(GetCurrentProcess(), &affinity, &dummy) || (affinity == 0)) {
- usbi_err(ctx, "could not get process affinity: %s", windows_error_str(0));
- return false;
- }
- // The process affinity mask is a bitmask where each set bit represents a core on
- // which this process is allowed to run, so we find the first set bit
- for (i = 0; !(affinity & (DWORD_PTR)(1 << i)); i++);
- affinity = (DWORD_PTR)(1 << i);
-
- usbi_dbg("timer thread will run on core #%d", i);
-
- event = CreateEvent(NULL, FALSE, FALSE, NULL);
- if (event == NULL) {
- usbi_err(ctx, "could not create event: %s", windows_error_str(0));
- return false;
- }
- timer_thread = (HANDLE)_beginthreadex(NULL, 0, windows_clock_gettime_threaded, (void *)event,
- 0, (unsigned int *)&timer_thread_id);
- if (timer_thread == NULL) {
- usbi_err(ctx, "unable to create timer thread - aborting");
- CloseHandle(event);
- return false;
- }
- if (!SetThreadAffinityMask(timer_thread, affinity)) {
- usbi_warn(ctx, "unable to set timer thread affinity, timer discrepancies may arise");
- }
-
- // Wait for timer thread to init before continuing.
- if (WaitForSingleObject(event, INFINITE) != WAIT_OBJECT_0) {
- usbi_err(ctx, "failed to wait for timer thread to become ready - aborting");
- CloseHandle(event);
- return false;
- }
-
- CloseHandle(event);
- } else {
- usbi_dbg("no hires timer available on this platform");
- hires_frequency = 0;
- hires_ticks_to_ps = UINT64_C(0);
- }
-
- return true;
+ DWORD_PTR affinity, dummy;
+ HANDLE event = NULL;
+ LARGE_INTEGER li_frequency;
+ int i;
+
+ if (QueryPerformanceFrequency(&li_frequency)) {
+ // Load DLL imports
+ if (windows_init_dlls() != LIBUSB_SUCCESS) {
+ usbi_err(ctx, "could not resolve DLL functions");
+ return false;
+ }
+
+ // The hires frequency can go as high as 4 GHz, so we'll use a conversion
+ // to picoseconds to compute the tv_nsecs part in clock_gettime
+ hires_frequency = li_frequency.QuadPart;
+ hires_ticks_to_ps = UINT64_C(1000000000000) / hires_frequency;
+ usbi_dbg("hires timer available (Frequency: %"PRIu64" Hz)", hires_frequency);
+
+ // Because QueryPerformanceCounter might report different values when
+ // running on different cores, we create a separate thread for the timer
+ // calls, which we glue to the first available core always to prevent timing discrepancies.
+ if (!GetProcessAffinityMask(GetCurrentProcess(), &affinity, &dummy) || (affinity == 0)) {
+ usbi_err(ctx, "could not get process affinity: %s", windows_error_str(0));
+ return false;
+ }
+
+ // The process affinity mask is a bitmask where each set bit represents a core on
+ // which this process is allowed to run, so we find the first set bit
+ for (i = 0; !(affinity & (DWORD_PTR)(1 << i)); i++);
+ affinity = (DWORD_PTR)(1 << i);
+
+ usbi_dbg("timer thread will run on core #%d", i);
+
+ event = CreateEvent(NULL, FALSE, FALSE, NULL);
+ if (event == NULL) {
+ usbi_err(ctx, "could not create event: %s", windows_error_str(0));
+ return false;
+ }
+
+ timer_thread = (HANDLE)_beginthreadex(NULL, 0, windows_clock_gettime_threaded, (void *)event,
+ 0, (unsigned int *)&timer_thread_id);
+ if (timer_thread == NULL) {
+ usbi_err(ctx, "unable to create timer thread - aborting");
+ CloseHandle(event);
+ return false;
+ }
+
+ if (!SetThreadAffinityMask(timer_thread, affinity))
+ usbi_warn(ctx, "unable to set timer thread affinity, timer discrepancies may arise");
+
+ // Wait for timer thread to init before continuing.
+ if (WaitForSingleObject(event, INFINITE) != WAIT_OBJECT_0) {
+ usbi_err(ctx, "failed to wait for timer thread to become ready - aborting");
+ CloseHandle(event);
+ return false;
+ }
+
+ CloseHandle(event);
+ } else {
+ usbi_dbg("no hires timer available on this platform");
+ hires_frequency = 0;
+ hires_ticks_to_ps = UINT64_C(0);
+ }
+
+ return true;
}
void windows_destroy_clock(void)
{
- if (timer_thread) {
- // actually the signal to quit the thread.
- if (!pPostThreadMessageA(timer_thread_id, WM_TIMER_EXIT, 0, 0) ||
- (WaitForSingleObject(timer_thread, INFINITE) != WAIT_OBJECT_0)) {
- usbi_dbg("could not wait for timer thread to quit");
- TerminateThread(timer_thread, 1);
- // shouldn't happen, but we're destroying
- // all objects it might have held anyway.
- }
- CloseHandle(timer_thread);
- timer_thread = NULL;
- timer_thread_id = 0;
- }
+ if (timer_thread) {
+ // actually the signal to quit the thread.
+ if (!pPostThreadMessageA(timer_thread_id, WM_TIMER_EXIT, 0, 0)
+ || (WaitForSingleObject(timer_thread, INFINITE) != WAIT_OBJECT_0)) {
+ usbi_dbg("could not wait for timer thread to quit");
+ TerminateThread(timer_thread, 1);
+ // shouldn't happen, but we're destroying
+ // all objects it might have held anyway.
+ }
+ CloseHandle(timer_thread);
+ timer_thread = NULL;
+ timer_thread_id = 0;
+ }
}
/*
* Monotonic and real time functions
*/
-static unsigned __stdcall windows_clock_gettime_threaded(void* param)
+static unsigned __stdcall windows_clock_gettime_threaded(void *param)
{
- struct timer_request *request;
- LARGE_INTEGER hires_counter;
- MSG msg;
-
- // The following call will create this thread's message queue
- // See https://msdn.microsoft.com/en-us/library/windows/desktop/ms644946.aspx
- pPeekMessageA(&msg, NULL, WM_USER, WM_USER, PM_NOREMOVE);
-
- // Signal windows_init() that we're ready to service requests
- if (!SetEvent((HANDLE)param)) {
- usbi_dbg("SetEvent failed for timer init event: %s", windows_error_str(0));
- }
- param = NULL;
-
- // Main loop - wait for requests
- while (1) {
-
- if (pGetMessageA(&msg, NULL, WM_TIMER_REQUEST, WM_TIMER_EXIT) == -1) {
- usbi_err(NULL, "GetMessage failed for timer thread: %s", windows_error_str(0));
- return 1;
- }
-
- switch (msg.message) {
- case WM_TIMER_REQUEST:
- // Requests to this thread are for hires always
- // Microsoft says that this function always succeeds on XP and later
- // See https://msdn.microsoft.com/en-us/library/windows/desktop/ms644904.aspx
- request = (struct timer_request *)msg.lParam;
- QueryPerformanceCounter(&hires_counter);
- request->tp->tv_sec = (long)(hires_counter.QuadPart / hires_frequency);
- request->tp->tv_nsec = (long)(((hires_counter.QuadPart % hires_frequency) / 1000) * hires_ticks_to_ps);
- if (!SetEvent(request->event)) {
- usbi_err(NULL, "SetEvent failed for timer request: %s", windows_error_str(0));
- }
- break;
-
- case WM_TIMER_EXIT:
- usbi_dbg("timer thread quitting");
- return 0;
- }
-
- }
+ struct timer_request *request;
+ LARGE_INTEGER hires_counter;
+ MSG msg;
+
+ // The following call will create this thread's message queue
+ // See https://msdn.microsoft.com/en-us/library/windows/desktop/ms644946.aspx
+ pPeekMessageA(&msg, NULL, WM_USER, WM_USER, PM_NOREMOVE);
+
+ // Signal windows_init_clock() that we're ready to service requests
+ if (!SetEvent((HANDLE)param))
+ usbi_dbg("SetEvent failed for timer init event: %s", windows_error_str(0));
+ param = NULL;
+
+ // Main loop - wait for requests
+ while (1) {
+ if (pGetMessageA(&msg, NULL, WM_TIMER_REQUEST, WM_TIMER_EXIT) == -1) {
+ usbi_err(NULL, "GetMessage failed for timer thread: %s", windows_error_str(0));
+ return 1;
+ }
+
+ switch (msg.message) {
+ case WM_TIMER_REQUEST:
+ // Requests to this thread are for hires always
+ // Microsoft says that this function always succeeds on XP and later
+ // See https://msdn.microsoft.com/en-us/library/windows/desktop/ms644904.aspx
+ request = (struct timer_request *)msg.lParam;
+ QueryPerformanceCounter(&hires_counter);
+ request->tp->tv_sec = (long)(hires_counter.QuadPart / hires_frequency);
+ request->tp->tv_nsec = (long)(((hires_counter.QuadPart % hires_frequency) / 1000) * hires_ticks_to_ps);
+ if (!SetEvent(request->event))
+ usbi_err(NULL, "SetEvent failed for timer request: %s", windows_error_str(0));
+ break;
+ case WM_TIMER_EXIT:
+ usbi_dbg("timer thread quitting");
+ return 0;
+ }
+ }
}
int windows_clock_gettime(int clk_id, struct timespec *tp)
{
- struct timer_request request;
- FILETIME filetime;
- ULARGE_INTEGER rtime;
- DWORD r;
- switch (clk_id) {
- case USBI_CLOCK_MONOTONIC:
- if (timer_thread) {
- request.tp = tp;
- request.event = CreateEvent(NULL, FALSE, FALSE, NULL);
- if (request.event == NULL) {
- return LIBUSB_ERROR_NO_MEM;
- }
-
- if (!pPostThreadMessageA(timer_thread_id, WM_TIMER_REQUEST, 0, (LPARAM)&request)) {
- usbi_err(NULL, "PostThreadMessage failed for timer thread: %s", windows_error_str(0));
- CloseHandle(request.event);
- return LIBUSB_ERROR_OTHER;
- }
-
- do {
- r = WaitForSingleObject(request.event, TIMER_REQUEST_RETRY_MS);
- if (r == WAIT_TIMEOUT) {
- usbi_dbg("could not obtain a timer value within reasonable timeframe - too much load?");
- }
- else if (r == WAIT_FAILED) {
- usbi_err(NULL, "WaitForSingleObject failed: %s", windows_error_str(0));
- }
- } while (r == WAIT_TIMEOUT);
- CloseHandle(request.event);
-
- if (r == WAIT_OBJECT_0) {
- return LIBUSB_SUCCESS;
- } else {
- return LIBUSB_ERROR_OTHER;
- }
- }
- // Fall through and return real-time if monotonic was not detected @ timer init
- case USBI_CLOCK_REALTIME:
- // We follow http://msdn.microsoft.com/en-us/library/ms724928%28VS.85%29.aspx
- // with a predef epoch_time to have an epoch that starts at 1970.01.01 00:00
- // Note however that our resolution is bounded by the Windows system time
- // functions and is at best of the order of 1 ms (or, usually, worse)
- GetSystemTimeAsFileTime(&filetime);
- rtime.LowPart = filetime.dwLowDateTime;
- rtime.HighPart = filetime.dwHighDateTime;
- rtime.QuadPart -= epoch_time;
- tp->tv_sec = (long)(rtime.QuadPart / 10000000);
- tp->tv_nsec = (long)((rtime.QuadPart % 10000000) * 100);
- return LIBUSB_SUCCESS;
- default:
- return LIBUSB_ERROR_INVALID_PARAM;
- }
+ struct timer_request request;
+ FILETIME filetime;
+ ULARGE_INTEGER rtime;
+ DWORD r;
+
+ switch (clk_id) {
+ case USBI_CLOCK_MONOTONIC:
+ if (timer_thread) {
+ request.tp = tp;
+ request.event = CreateEvent(NULL, FALSE, FALSE, NULL);
+ if (request.event == NULL)
+ return LIBUSB_ERROR_NO_MEM;
+
+ if (!pPostThreadMessageA(timer_thread_id, WM_TIMER_REQUEST, 0, (LPARAM)&request)) {
+ usbi_err(NULL, "PostThreadMessage failed for timer thread: %s", windows_error_str(0));
+ CloseHandle(request.event);
+ return LIBUSB_ERROR_OTHER;
+ }
+
+ do {
+ r = WaitForSingleObject(request.event, TIMER_REQUEST_RETRY_MS);
+ if (r == WAIT_TIMEOUT)
+ usbi_dbg("could not obtain a timer value within reasonable timeframe - too much load?");
+ else if (r == WAIT_FAILED)
+ usbi_err(NULL, "WaitForSingleObject failed: %s", windows_error_str(0));
+ } while (r == WAIT_TIMEOUT);
+ CloseHandle(request.event);
+
+ if (r == WAIT_OBJECT_0)
+ return LIBUSB_SUCCESS;
+ else
+ return LIBUSB_ERROR_OTHER;
+ }
+ // Fall through and return real-time if monotonic was not detected @ timer init
+ case USBI_CLOCK_REALTIME:
+ // We follow http://msdn.microsoft.com/en-us/library/ms724928%28VS.85%29.aspx
+ // with a predef epoch_time to have an epoch that starts at 1970.01.01 00:00
+ // Note however that our resolution is bounded by the Windows system time
+ // functions and is at best of the order of 1 ms (or, usually, worse)
+ GetSystemTimeAsFileTime(&filetime);
+ rtime.LowPart = filetime.dwLowDateTime;
+ rtime.HighPart = filetime.dwHighDateTime;
+ rtime.QuadPart -= epoch_time;
+ tp->tv_sec = (long)(rtime.QuadPart / 10000000);
+ tp->tv_nsec = (long)((rtime.QuadPart % 10000000) * 100);
+ return LIBUSB_SUCCESS;
+ default:
+ return LIBUSB_ERROR_INVALID_PARAM;
+ }
}
static void windows_transfer_callback(struct usbi_transfer *itransfer, uint32_t io_result, uint32_t io_size)
{
- int status, istatus;
-
- usbi_dbg("handling I/O completion with errcode %u, size %u", io_result, io_size);
-
- switch (io_result) {
- case NO_ERROR:
- status = windows_copy_transfer_data(itransfer, io_size);
- break;
- case ERROR_GEN_FAILURE:
- usbi_dbg("detected endpoint stall");
- status = LIBUSB_TRANSFER_STALL;
- break;
- case ERROR_SEM_TIMEOUT:
- usbi_dbg("detected semaphore timeout");
- status = LIBUSB_TRANSFER_TIMED_OUT;
- break;
- case ERROR_OPERATION_ABORTED:
- istatus = windows_copy_transfer_data(itransfer, io_size);
- if (istatus != LIBUSB_TRANSFER_COMPLETED) {
- usbi_dbg("Failed to copy partial data in aborted operation: %d", istatus);
- }
- if (itransfer->flags & USBI_TRANSFER_TIMED_OUT) {
- usbi_dbg("detected timeout");
- status = LIBUSB_TRANSFER_TIMED_OUT;
- }
- else {
- usbi_dbg("detected operation aborted");
- status = LIBUSB_TRANSFER_CANCELLED;
- }
- break;
- default:
- usbi_err(ITRANSFER_CTX(itransfer), "detected I/O error %u: %s", io_result, windows_error_str(io_result));
- status = LIBUSB_TRANSFER_ERROR;
- break;
- }
- windows_clear_transfer_priv(itransfer); // Cancel polling
- usbi_handle_transfer_completion(itransfer, (enum libusb_transfer_status)status);
+ int status, istatus;
+
+ usbi_dbg("handling I/O completion with errcode %u, size %u", io_result, io_size);
+
+ switch (io_result) {
+ case NO_ERROR:
+ status = windows_copy_transfer_data(itransfer, io_size);
+ break;
+ case ERROR_GEN_FAILURE:
+ usbi_dbg("detected endpoint stall");
+ status = LIBUSB_TRANSFER_STALL;
+ break;
+ case ERROR_SEM_TIMEOUT:
+ usbi_dbg("detected semaphore timeout");
+ status = LIBUSB_TRANSFER_TIMED_OUT;
+ break;
+ case ERROR_OPERATION_ABORTED:
+ istatus = windows_copy_transfer_data(itransfer, io_size);
+ if (istatus != LIBUSB_TRANSFER_COMPLETED)
+ usbi_dbg("Failed to copy partial data in aborted operation: %d", istatus);
+ if (itransfer->flags & USBI_TRANSFER_TIMED_OUT) {
+ usbi_dbg("detected timeout");
+ status = LIBUSB_TRANSFER_TIMED_OUT;
+ }
+ else {
+ usbi_dbg("detected operation aborted");
+ status = LIBUSB_TRANSFER_CANCELLED;
+ }
+ break;
+ default:
+ usbi_err(ITRANSFER_CTX(itransfer), "detected I/O error %u: %s", io_result, windows_error_str(io_result));
+ status = LIBUSB_TRANSFER_ERROR;
+ break;
+ }
+ windows_clear_transfer_priv(itransfer); // Cancel polling
+ usbi_handle_transfer_completion(itransfer, (enum libusb_transfer_status)status);
}
void windows_handle_callback(struct usbi_transfer *itransfer, uint32_t io_result, uint32_t 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(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);
- }
+ 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(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);
+ }
}
int windows_handle_events(struct libusb_context *ctx, struct pollfd *fds, POLL_NFDS_TYPE nfds, int num_ready)
{
- POLL_NFDS_TYPE i = 0;
- bool found = false;
- struct usbi_transfer *transfer;
- struct winfd *pollable_fd = NULL;
- DWORD io_size, io_result;
-
- 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) {
- continue;
- }
-
- num_ready--;
-
- // Because a Windows OVERLAPPED is used for poll emulation,
- // a pollable fd is created and stored with each transfer
- usbi_mutex_lock(&ctx->flying_transfers_lock);
- found = false;
- list_for_each_entry(transfer, &ctx->flying_transfers, list, struct usbi_transfer) {
- pollable_fd = windows_get_fd(transfer);
- if (pollable_fd->fd == fds[i].fd) {
- found = true;
- break;
- }
- }
- usbi_mutex_unlock(&ctx->flying_transfers_lock);
-
- if (found) {
- windows_get_overlapped_result(transfer, pollable_fd, &io_result, &io_size);
-
- usbi_remove_pollfd(ctx, pollable_fd->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(transfer, io_result, io_size);
- } else {
- usbi_mutex_unlock(&ctx->open_devs_lock);
- usbi_err(ctx, "could not find a matching transfer for fd %d", fds[i]);
- return LIBUSB_ERROR_NOT_FOUND;
- }
- }
-
- usbi_mutex_unlock(&ctx->open_devs_lock);
- return LIBUSB_SUCCESS;
+ POLL_NFDS_TYPE i = 0;
+ bool found = false;
+ struct usbi_transfer *transfer;
+ struct winfd *pollable_fd = NULL;
+ DWORD io_size, io_result;
+
+ 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)
+ continue;
+
+ num_ready--;
+
+ // Because a Windows OVERLAPPED is used for poll emulation,
+ // a pollable fd is created and stored with each transfer
+ usbi_mutex_lock(&ctx->flying_transfers_lock);
+ found = false;
+ list_for_each_entry(transfer, &ctx->flying_transfers, list, struct usbi_transfer) {
+ pollable_fd = windows_get_fd(transfer);
+ if (pollable_fd->fd == fds[i].fd) {
+ found = true;
+ break;
+ }
+ }
+ usbi_mutex_unlock(&ctx->flying_transfers_lock);
+
+ if (found) {
+ windows_get_overlapped_result(transfer, pollable_fd, &io_result, &io_size);
+
+ usbi_remove_pollfd(ctx, pollable_fd->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(transfer, io_result, io_size);
+ } else {
+ usbi_mutex_unlock(&ctx->open_devs_lock);
+ usbi_err(ctx, "could not find a matching transfer for fd %d", fds[i]);
+ return LIBUSB_ERROR_NOT_FOUND;
+ }
+ }
+ usbi_mutex_unlock(&ctx->open_devs_lock);
+
+ return LIBUSB_SUCCESS;
}
int windows_common_init(struct libusb_context *ctx)
{
- static const unsigned long HTAB_SIZE = 1021;
-
- if (!windows_init_clock(ctx)){
- goto error_roll_back;
- }
+ if (!windows_init_clock(ctx))
+ goto error_roll_back;
- if (!htab_create(ctx, HTAB_SIZE)) {
- goto error_roll_back;
- }
+ if (!htab_create(ctx, HTAB_SIZE))
+ goto error_roll_back;
- return LIBUSB_SUCCESS;
+ return LIBUSB_SUCCESS;
error_roll_back:
- windows_common_exit();
-
- return LIBUSB_ERROR_NO_MEM;
+ windows_common_exit();
+ return LIBUSB_ERROR_NO_MEM;
}
void windows_common_exit(void)
{
- htab_destroy();
- windows_destroy_clock();
+ htab_destroy();
+ windows_destroy_clock();
}
diff --git a/libusb/os/windows_nt_common.h b/libusb/os/windows_nt_common.h
index ac907ce..ceb547b 100644
--- a/libusb/os/windows_nt_common.h
+++ b/libusb/os/windows_nt_common.h
@@ -42,7 +42,7 @@ typedef struct libusb_device_descriptor USB_DEVICE_DESCRIPTOR, *PUSB_DEVICE_DESC
int windows_common_init(struct libusb_context *ctx);
void windows_common_exit(void);
-unsigned long htab_hash(char* str);
+unsigned long htab_hash(const char *str);
int windows_clock_gettime(int clk_id, struct timespec *tp);
void windows_clear_transfer_priv(struct usbi_transfer *itransfer);
@@ -54,5 +54,5 @@ void windows_handle_callback(struct usbi_transfer *itransfer, uint32_t io_result
int windows_handle_events(struct libusb_context *ctx, struct pollfd *fds, POLL_NFDS_TYPE nfds, int num_ready);
#if defined(ENABLE_LOGGING)
-char *windows_error_str(uint32_t retval);
+const char *windows_error_str(DWORD retval);
#endif
diff --git a/libusb/os/windows_usbdk.c b/libusb/os/windows_usbdk.c
index 3336d9a..f2bea93 100644
--- a/libusb/os/windows_usbdk.c
+++ b/libusb/os/windows_usbdk.c
@@ -43,910 +43,861 @@ typedef CONST WCHAR *PCWCHAR;
#if !defined(STATUS_SUCCESS)
typedef LONG NTSTATUS;
-#define STATUS_SUCCESS ((NTSTATUS)0x00000000L)
+#define STATUS_SUCCESS ((NTSTATUS)0x00000000L)
#endif
#if !defined(STATUS_CANCELLED)
-#define STATUS_CANCELLED ((NTSTATUS)0xC0000120L)
+#define STATUS_CANCELLED ((NTSTATUS)0xC0000120L)
#endif
#if !defined(STATUS_REQUEST_CANCELED)
-#define STATUS_REQUEST_CANCELED ((NTSTATUS)0xC0000703L)
+#define STATUS_REQUEST_CANCELED ((NTSTATUS)0xC0000703L)
#endif
#if !defined(USBD_SUCCESS)
typedef int32_t USBD_STATUS;
-#define USBD_SUCCESS(Status) ((USBD_STATUS) (Status) >= 0)
-#define USBD_PENDING(Status) ((ULONG) (Status) >> 30 == 1)
-#define USBD_ERROR(Status) ((USBD_STATUS) (Status) < 0)
-#define USBD_STATUS_STALL_PID ((USBD_STATUS) 0xc0000004)
-#define USBD_STATUS_ENDPOINT_HALTED ((USBD_STATUS) 0xc0000030)
-#define USBD_STATUS_BAD_START_FRAME ((USBD_STATUS) 0xc0000a00)
-#define USBD_STATUS_TIMEOUT ((USBD_STATUS) 0xc0006000)
-#define USBD_STATUS_CANCELED ((USBD_STATUS) 0xc0010000)
+#define USBD_SUCCESS(Status) ((USBD_STATUS) (Status) >= 0)
+#define USBD_PENDING(Status) ((ULONG) (Status) >> 30 == 1)
+#define USBD_ERROR(Status) ((USBD_STATUS) (Status) < 0)
+#define USBD_STATUS_STALL_PID ((USBD_STATUS) 0xc0000004)
+#define USBD_STATUS_ENDPOINT_HALTED ((USBD_STATUS) 0xc0000030)
+#define USBD_STATUS_BAD_START_FRAME ((USBD_STATUS) 0xc0000a00)
+#define USBD_STATUS_TIMEOUT ((USBD_STATUS) 0xc0006000)
+#define USBD_STATUS_CANCELED ((USBD_STATUS) 0xc0010000)
#endif
static int concurrent_usage = -1;
static int init_succeeded = 0;
struct usbdk_device_priv {
- USB_DK_DEVICE_INFO info;
- PUSB_CONFIGURATION_DESCRIPTOR *config_descriptors;
- HANDLE redirector_handle;
- uint8_t active_configuration;
+ USB_DK_DEVICE_INFO info;
+ PUSB_CONFIGURATION_DESCRIPTOR *config_descriptors;
+ HANDLE redirector_handle;
+ uint8_t active_configuration;
};
struct usbdk_device_handle_priv {
- //This field is to solve compilation error on VS2013:
- //error C2016: C requires that a struct or union has at least one member
- int dummy;
+ //This field is to solve compilation error on VS2013:
+ //error C2016: C requires that a struct or union has at least one member
+ int dummy;
};
struct usbdk_transfer_priv {
- USB_DK_TRANSFER_REQUEST request;
- struct winfd pollable_fd;
- PULONG64 IsochronousPacketsArray;
- PUSB_DK_ISO_TRANSFER_RESULT IsochronousResultsArray;
+ USB_DK_TRANSFER_REQUEST request;
+ struct winfd pollable_fd;
+ PULONG64 IsochronousPacketsArray;
+ PUSB_DK_ISO_TRANSFER_RESULT IsochronousResultsArray;
};
static inline struct usbdk_device_priv *_usbdk_device_priv(struct libusb_device *dev)
{
- return (struct usbdk_device_priv*) dev->os_priv;
+ 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);
+ return (struct usbdk_transfer_priv *)usbi_transfer_get_os_priv(itransfer);
}
static struct {
- HMODULE module;
-
- USBDK_GET_DEVICES_LIST GetDevicesList;
- USBDK_RELEASE_DEVICES_LIST ReleaseDevicesList;
- USBDK_START_REDIRECT StartRedirect;
- USBDK_STOP_REDIRECT StopRedirect;
- USBDK_GET_CONFIGURATION_DESCRIPTOR GetConfigurationDescriptor;
- USBDK_RELEASE_CONFIGURATION_DESCRIPTOR ReleaseConfigurationDescriptor;
- USBDK_READ_PIPE ReadPipe;
- USBDK_WRITE_PIPE WritePipe;
- USBDK_ABORT_PIPE AbortPipe;
- USBDK_RESET_PIPE ResetPipe;
- USBDK_SET_ALTSETTING SetAltsetting;
- USBDK_RESET_DEVICE ResetDevice;
- USBDK_GET_REDIRECTOR_SYSTEM_HANDLE GetRedirectorSystemHandle;
+ HMODULE module;
+
+ USBDK_GET_DEVICES_LIST GetDevicesList;
+ USBDK_RELEASE_DEVICES_LIST ReleaseDevicesList;
+ USBDK_START_REDIRECT StartRedirect;
+ USBDK_STOP_REDIRECT StopRedirect;
+ USBDK_GET_CONFIGURATION_DESCRIPTOR GetConfigurationDescriptor;
+ USBDK_RELEASE_CONFIGURATION_DESCRIPTOR ReleaseConfigurationDescriptor;
+ USBDK_READ_PIPE ReadPipe;
+ USBDK_WRITE_PIPE WritePipe;
+ USBDK_ABORT_PIPE AbortPipe;
+ USBDK_RESET_PIPE ResetPipe;
+ USBDK_SET_ALTSETTING SetAltsetting;
+ USBDK_RESET_DEVICE ResetDevice;
+ USBDK_GET_REDIRECTOR_SYSTEM_HANDLE GetRedirectorSystemHandle;
} usbdk_helper;
static FARPROC get_usbdk_proc_addr(struct libusb_context *ctx, LPCSTR api_name)
{
- FARPROC api_ptr = GetProcAddress(usbdk_helper.module, api_name);
- if (api_ptr == NULL) {
- DWORD err = GetLastError();
- usbi_err(ctx, "UsbDkHelper API %s not found, error %d", api_name, err);
- }
+ FARPROC api_ptr = GetProcAddress(usbdk_helper.module, api_name);
- return api_ptr;
+ if (api_ptr == NULL) {
+ DWORD err = GetLastError();
+ usbi_err(ctx, "UsbDkHelper API %s not found, error %d", api_name, err);
+ }
+
+ return api_ptr;
}
static void unload_usbdk_helper_dll(void)
{
- FreeLibrary(usbdk_helper.module);
+ FreeLibrary(usbdk_helper.module);
}
static int load_usbdk_helper_dll(struct libusb_context *ctx)
{
- usbdk_helper.module = LoadLibraryA("UsbDkHelper");
- if (usbdk_helper.module == NULL) {
- DWORD err = GetLastError();
- usbi_err(ctx, "Failed to load UsbDkHelper.dll, error %d", err);
- return LIBUSB_ERROR_NOT_FOUND;
- }
-
- usbdk_helper.GetDevicesList = (USBDK_GET_DEVICES_LIST) get_usbdk_proc_addr(ctx, "UsbDk_GetDevicesList");
- if (usbdk_helper.GetDevicesList == NULL) {
- goto error_unload;
- }
-
- usbdk_helper.ReleaseDevicesList = (USBDK_RELEASE_DEVICES_LIST) get_usbdk_proc_addr(ctx, "UsbDk_ReleaseDevicesList");
- if (usbdk_helper.ReleaseDevicesList == NULL) {
- goto error_unload;
- }
-
- usbdk_helper.StartRedirect = (USBDK_START_REDIRECT) get_usbdk_proc_addr(ctx, "UsbDk_StartRedirect");
- if (usbdk_helper.StartRedirect == NULL) {
- goto error_unload;
- }
-
- usbdk_helper.StopRedirect = (USBDK_STOP_REDIRECT) get_usbdk_proc_addr(ctx, "UsbDk_StopRedirect");
- if (usbdk_helper.StopRedirect == NULL) {
- goto error_unload;
- }
-
- usbdk_helper.GetConfigurationDescriptor = (USBDK_GET_CONFIGURATION_DESCRIPTOR) get_usbdk_proc_addr(ctx, "UsbDk_GetConfigurationDescriptor");
- if (usbdk_helper.GetConfigurationDescriptor == NULL) {
- goto error_unload;
- }
-
- usbdk_helper.ReleaseConfigurationDescriptor = (USBDK_RELEASE_CONFIGURATION_DESCRIPTOR) get_usbdk_proc_addr(ctx, "UsbDk_ReleaseConfigurationDescriptor");
- if (usbdk_helper.ReleaseConfigurationDescriptor == NULL) {
- goto error_unload;
- }
-
- usbdk_helper.ReadPipe = (USBDK_READ_PIPE) get_usbdk_proc_addr(ctx, "UsbDk_ReadPipe");
- if (usbdk_helper.ReadPipe == NULL) {
- goto error_unload;
- }
-
- usbdk_helper.WritePipe = (USBDK_WRITE_PIPE) get_usbdk_proc_addr(ctx, "UsbDk_WritePipe");
- if (usbdk_helper.WritePipe == NULL) {
- goto error_unload;
- }
-
- usbdk_helper.AbortPipe = (USBDK_ABORT_PIPE)get_usbdk_proc_addr(ctx, "UsbDk_AbortPipe");
- if (usbdk_helper.AbortPipe == NULL) {
- goto error_unload;
- }
-
- usbdk_helper.ResetPipe = (USBDK_RESET_PIPE)get_usbdk_proc_addr(ctx, "UsbDk_ResetPipe");
- if (usbdk_helper.ResetPipe == NULL) {
- goto error_unload;
- }
-
- usbdk_helper.SetAltsetting = (USBDK_SET_ALTSETTING)get_usbdk_proc_addr(ctx, "UsbDk_SetAltsetting");
- if (usbdk_helper.SetAltsetting == NULL) {
- goto error_unload;
- }
-
- usbdk_helper.ResetDevice = (USBDK_RESET_DEVICE)get_usbdk_proc_addr(ctx, "UsbDk_ResetDevice");
- if (usbdk_helper.ResetDevice == NULL) {
- goto error_unload;
- }
-
- usbdk_helper.GetRedirectorSystemHandle = (USBDK_GET_REDIRECTOR_SYSTEM_HANDLE)get_usbdk_proc_addr(ctx, "UsbDk_GetRedirectorSystemHandle");
- if (usbdk_helper.GetRedirectorSystemHandle == NULL) {
- goto error_unload;
- }
-
- return LIBUSB_SUCCESS;
+ usbdk_helper.module = LoadLibraryA("UsbDkHelper");
+ if (usbdk_helper.module == NULL) {
+ DWORD err = GetLastError();
+ usbi_err(ctx, "Failed to load UsbDkHelper.dll, error %d", err);
+ return LIBUSB_ERROR_NOT_FOUND;
+ }
+
+ usbdk_helper.GetDevicesList = (USBDK_GET_DEVICES_LIST)get_usbdk_proc_addr(ctx, "UsbDk_GetDevicesList");
+ if (usbdk_helper.GetDevicesList == NULL)
+ goto error_unload;
+
+ usbdk_helper.ReleaseDevicesList = (USBDK_RELEASE_DEVICES_LIST)get_usbdk_proc_addr(ctx, "UsbDk_ReleaseDevicesList");
+ if (usbdk_helper.ReleaseDevicesList == NULL)
+ goto error_unload;
+
+ usbdk_helper.StartRedirect = (USBDK_START_REDIRECT)get_usbdk_proc_addr(ctx, "UsbDk_StartRedirect");
+ if (usbdk_helper.StartRedirect == NULL)
+ goto error_unload;
+
+ usbdk_helper.StopRedirect = (USBDK_STOP_REDIRECT)get_usbdk_proc_addr(ctx, "UsbDk_StopRedirect");
+ if (usbdk_helper.StopRedirect == NULL)
+ goto error_unload;
+
+ usbdk_helper.GetConfigurationDescriptor = (USBDK_GET_CONFIGURATION_DESCRIPTOR)get_usbdk_proc_addr(ctx, "UsbDk_GetConfigurationDescriptor");
+ if (usbdk_helper.GetConfigurationDescriptor == NULL)
+ goto error_unload;
+
+ usbdk_helper.ReleaseConfigurationDescriptor = (USBDK_RELEASE_CONFIGURATION_DESCRIPTOR)get_usbdk_proc_addr(ctx, "UsbDk_ReleaseConfigurationDescriptor");
+ if (usbdk_helper.ReleaseConfigurationDescriptor == NULL)
+ goto error_unload;
+
+ usbdk_helper.ReadPipe = (USBDK_READ_PIPE)get_usbdk_proc_addr(ctx, "UsbDk_ReadPipe");
+ if (usbdk_helper.ReadPipe == NULL)
+ goto error_unload;
+
+ usbdk_helper.WritePipe = (USBDK_WRITE_PIPE)get_usbdk_proc_addr(ctx, "UsbDk_WritePipe");
+ if (usbdk_helper.WritePipe == NULL)
+ goto error_unload;
+
+ usbdk_helper.AbortPipe = (USBDK_ABORT_PIPE)get_usbdk_proc_addr(ctx, "UsbDk_AbortPipe");
+ if (usbdk_helper.AbortPipe == NULL)
+ goto error_unload;
+
+ usbdk_helper.ResetPipe = (USBDK_RESET_PIPE)get_usbdk_proc_addr(ctx, "UsbDk_ResetPipe");
+ if (usbdk_helper.ResetPipe == NULL)
+ goto error_unload;
+
+ usbdk_helper.SetAltsetting = (USBDK_SET_ALTSETTING)get_usbdk_proc_addr(ctx, "UsbDk_SetAltsetting");
+ if (usbdk_helper.SetAltsetting == NULL)
+ goto error_unload;
+
+ usbdk_helper.ResetDevice = (USBDK_RESET_DEVICE)get_usbdk_proc_addr(ctx, "UsbDk_ResetDevice");
+ if (usbdk_helper.ResetDevice == NULL)
+ goto error_unload;
+
+ usbdk_helper.GetRedirectorSystemHandle = (USBDK_GET_REDIRECTOR_SYSTEM_HANDLE)get_usbdk_proc_addr(ctx, "UsbDk_GetRedirectorSystemHandle");
+ if (usbdk_helper.GetRedirectorSystemHandle == NULL)
+ goto error_unload;
+
+ return LIBUSB_SUCCESS;
error_unload:
- FreeLibrary(usbdk_helper.module);
- return LIBUSB_ERROR_NOT_FOUND;
+ FreeLibrary(usbdk_helper.module);
+ return LIBUSB_ERROR_NOT_FOUND;
}
static int usbdk_init(struct libusb_context *ctx)
{
- int r;
+ int r;
- if ((++concurrent_usage == 0) || !init_succeeded) {
- r = load_usbdk_helper_dll(ctx);
- if (r) {
- return r;
- }
+ if ((++concurrent_usage == 0) || !init_succeeded) {
+ r = load_usbdk_helper_dll(ctx);
+ if (r)
+ return r;
- init_polling();
+ init_polling();
- r = windows_common_init(ctx);
- if (r) {
- goto error_roll_back;
- }
+ r = windows_common_init(ctx);
+ if (r)
+ goto error_roll_back;
- init_succeeded = 1;
- }
+ init_succeeded = 1;
+ }
- return LIBUSB_SUCCESS;
+ return LIBUSB_SUCCESS;
error_roll_back:
- windows_common_exit();
- unload_usbdk_helper_dll();
- return r;
+ windows_common_exit();
+ unload_usbdk_helper_dll();
+ return r;
}
static int usbdk_get_session_id_for_device(struct libusb_context *ctx,
- PUSB_DK_DEVICE_ID id,
- unsigned long* session_id)
+ PUSB_DK_DEVICE_ID id, unsigned long *session_id)
{
- char dev_identity[ARRAYSIZE(id->DeviceID) + ARRAYSIZE(id->InstanceID)];
+ char dev_identity[ARRAYSIZE(id->DeviceID) + ARRAYSIZE(id->InstanceID)];
- if (sprintf(dev_identity, "%S%S", id->DeviceID, id->InstanceID) == -1) {
- usbi_warn(ctx, "cannot form device identity", id->DeviceID);
- return LIBUSB_ERROR_NOT_SUPPORTED;
- }
+ if (sprintf(dev_identity, "%S%S", id->DeviceID, id->InstanceID) == -1) {
+ usbi_warn(ctx, "cannot form device identity", id->DeviceID);
+ return LIBUSB_ERROR_NOT_SUPPORTED;
+ }
- *session_id = htab_hash(dev_identity);
+ *session_id = htab_hash(dev_identity);
- return LIBUSB_SUCCESS;
+ return LIBUSB_SUCCESS;
}
-static void usbdk_release_config_descriptors(struct usbdk_device_priv* p, uint8_t count)
+static void usbdk_release_config_descriptors(struct usbdk_device_priv *p, uint8_t count)
{
- uint8_t i;
- for (i = 0; i < count; i++) {
- usbdk_helper.ReleaseConfigurationDescriptor(p->config_descriptors[i]);
- }
- free(p->config_descriptors);
- p->config_descriptors = NULL;
+ uint8_t i;
+
+ for (i = 0; i < count; i++)
+ usbdk_helper.ReleaseConfigurationDescriptor(p->config_descriptors[i]);
+
+ free(p->config_descriptors);
+ p->config_descriptors = NULL;
}
static int usbdk_cache_config_descriptors(struct libusb_context *ctx,
- struct usbdk_device_priv* p,
- PUSB_DK_DEVICE_INFO info)
-{
- uint8_t i;
- USB_DK_CONFIG_DESCRIPTOR_REQUEST Request;
- Request.ID = info->ID;
-
- p->config_descriptors = calloc(info->DeviceDescriptor.bNumConfigurations,
- sizeof(PUSB_CONFIGURATION_DESCRIPTOR));
- if (p->config_descriptors == NULL) {
- usbi_err(ctx, "failed to allocate configuration descriptors holder");
- return LIBUSB_ERROR_NO_MEM;
- }
-
- for (i = 0; i < info->DeviceDescriptor.bNumConfigurations; i++) {
- ULONG Length;
-
- Request.Index = i;
- if (!usbdk_helper.GetConfigurationDescriptor(&Request,
- &p->config_descriptors[i],
- &Length)) {
- usbi_err(ctx, "failed to retrieve configuration descriptors");
- usbdk_release_config_descriptors(p, i);
- return LIBUSB_ERROR_OTHER;
- }
- }
-
- return LIBUSB_SUCCESS;
-}
-
-static inline int usbdk_device_priv_init(struct libusb_context *ctx, libusb_device* dev, PUSB_DK_DEVICE_INFO info)
-{
- struct usbdk_device_priv* p = _usbdk_device_priv(dev);
- p->info = *info;
- p->active_configuration = 0;
- return usbdk_cache_config_descriptors(ctx, p, info);
-}
-
-static void usbdk_device_init(libusb_device* dev, PUSB_DK_DEVICE_INFO info)
-{
- dev->bus_number = (uint8_t) info->FilterID;
- dev->port_number = (uint8_t) info->Port;
- dev->parent_dev = NULL;
-
- //Addresses in libusb are 1-based
- dev->device_address = (uint8_t) info->Port + 1;
-
- dev->num_configurations = info->DeviceDescriptor.bNumConfigurations;
- dev->device_descriptor = info->DeviceDescriptor;
-
- switch (info->Speed)
- {
- case LowSpeed:
- dev->speed = LIBUSB_SPEED_LOW;
- break;
- case FullSpeed:
- dev->speed = LIBUSB_SPEED_FULL;
- break;
- case HighSpeed:
- dev->speed = LIBUSB_SPEED_HIGH;
- break;
- case SuperSpeed:
- dev->speed = LIBUSB_SPEED_SUPER;
- break;
- case NoSpeed:
- default:
- dev->speed = LIBUSB_SPEED_UNKNOWN;
- break;
- }
+ struct usbdk_device_priv *p, PUSB_DK_DEVICE_INFO info)
+{
+ uint8_t i;
+ USB_DK_CONFIG_DESCRIPTOR_REQUEST Request;
+ Request.ID = info->ID;
+
+ p->config_descriptors = calloc(info->DeviceDescriptor.bNumConfigurations, sizeof(PUSB_CONFIGURATION_DESCRIPTOR));
+ if (p->config_descriptors == NULL) {
+ usbi_err(ctx, "failed to allocate configuration descriptors holder");
+ return LIBUSB_ERROR_NO_MEM;
+ }
+
+ for (i = 0; i < info->DeviceDescriptor.bNumConfigurations; i++) {
+ ULONG Length;
+
+ Request.Index = i;
+ if (!usbdk_helper.GetConfigurationDescriptor(&Request, &p->config_descriptors[i], &Length)) {
+ usbi_err(ctx, "failed to retrieve configuration descriptors");
+ usbdk_release_config_descriptors(p, i);
+ return LIBUSB_ERROR_OTHER;
+ }
+ }
+
+ return LIBUSB_SUCCESS;
+}
+
+static inline int usbdk_device_priv_init(struct libusb_context *ctx, struct libusb_device *dev, PUSB_DK_DEVICE_INFO info)
+{
+ struct usbdk_device_priv *p = _usbdk_device_priv(dev);
+
+ p->info = *info;
+ p->active_configuration = 0;
+
+ return usbdk_cache_config_descriptors(ctx, p, info);
+}
+
+static void usbdk_device_init(libusb_device *dev, PUSB_DK_DEVICE_INFO info)
+{
+ dev->bus_number = (uint8_t)info->FilterID;
+ dev->port_number = (uint8_t)info->Port;
+ dev->parent_dev = NULL;
+
+ //Addresses in libusb are 1-based
+ dev->device_address = (uint8_t)(info->Port + 1);
+
+ dev->num_configurations = info->DeviceDescriptor.bNumConfigurations;
+ dev->device_descriptor = info->DeviceDescriptor;
+
+ switch (info->Speed) {
+ case LowSpeed:
+ dev->speed = LIBUSB_SPEED_LOW;
+ break;
+ case FullSpeed:
+ dev->speed = LIBUSB_SPEED_FULL;
+ break;
+ case HighSpeed:
+ dev->speed = LIBUSB_SPEED_HIGH;
+ break;
+ case SuperSpeed:
+ dev->speed = LIBUSB_SPEED_SUPER;
+ break;
+ case NoSpeed:
+ default:
+ dev->speed = LIBUSB_SPEED_UNKNOWN;
+ break;
+ }
}
static int usbdk_get_device_list(struct libusb_context *ctx, struct discovered_devs **_discdevs)
{
- int r = LIBUSB_SUCCESS;
- ULONG i;
-
- struct discovered_devs *discdevs = NULL;
- ULONG dev_number;
- PUSB_DK_DEVICE_INFO devices;
-
- if(!usbdk_helper.GetDevicesList(&devices, &dev_number)) {
- return LIBUSB_ERROR_OTHER;
- }
-
- for (i = 0; i < dev_number; ++i) {
- unsigned long session_id;
- struct libusb_device *dev = NULL;
-
- if (usbdk_get_session_id_for_device(ctx, &devices[i].ID, &session_id)) {
- continue;
- }
-
- dev = usbi_get_device_by_session_id(ctx, session_id);
- if (dev == NULL) {
- dev = usbi_alloc_device(ctx, session_id);
- if (dev == NULL) {
- usbi_err(ctx, "failed to allocate a new device structure");
- continue;
- }
-
- usbdk_device_init(dev, &devices[i]);
- if (usbdk_device_priv_init(ctx, dev, &devices[i]) != LIBUSB_SUCCESS) {
- libusb_unref_device(dev);
- continue;
- }
- }
-
- discdevs = discovered_devs_append(*_discdevs, dev);
-
- libusb_unref_device(dev);
-
- if (!discdevs)
- {
- usbi_err(ctx, "cannot append new device to list");
- r = LIBUSB_ERROR_NO_MEM;
- goto func_exit;
- }
- *_discdevs = discdevs;
- }
+ int r = LIBUSB_SUCCESS;
+ ULONG i;
+ struct discovered_devs *discdevs = NULL;
+ ULONG dev_number;
+ PUSB_DK_DEVICE_INFO devices;
+
+ if(!usbdk_helper.GetDevicesList(&devices, &dev_number))
+ return LIBUSB_ERROR_OTHER;
+
+ for (i = 0; i < dev_number; i++) {
+ unsigned long session_id;
+ struct libusb_device *dev = NULL;
+
+ if (usbdk_get_session_id_for_device(ctx, &devices[i].ID, &session_id))
+ continue;
+
+ dev = usbi_get_device_by_session_id(ctx, session_id);
+ if (dev == NULL) {
+ dev = usbi_alloc_device(ctx, session_id);
+ if (dev == NULL) {
+ usbi_err(ctx, "failed to allocate a new device structure");
+ continue;
+ }
+
+ usbdk_device_init(dev, &devices[i]);
+ if (usbdk_device_priv_init(ctx, dev, &devices[i]) != LIBUSB_SUCCESS) {
+ libusb_unref_device(dev);
+ continue;
+ }
+ }
+
+ discdevs = discovered_devs_append(*_discdevs, dev);
+ libusb_unref_device(dev);
+ if (!discdevs) {
+ usbi_err(ctx, "cannot append new device to list");
+ r = LIBUSB_ERROR_NO_MEM;
+ goto func_exit;
+ }
+
+ *_discdevs = discdevs;
+ }
func_exit:
- usbdk_helper.ReleaseDevicesList(devices);
- return r;
+ usbdk_helper.ReleaseDevicesList(devices);
+ return r;
}
static void usbdk_exit(void)
{
- if ((--concurrent_usage < 0) && init_succeeded) {
- windows_common_exit();
- exit_polling();
- unload_usbdk_helper_dll();
- init_succeeded = 0;
- }
+ if ((--concurrent_usage < 0) && init_succeeded) {
+ windows_common_exit();
+ exit_polling();
+ unload_usbdk_helper_dll();
+ init_succeeded = 0;
+ }
}
static int usbdk_get_device_descriptor(struct libusb_device *dev, unsigned char *buffer, int *host_endian)
{
- struct usbdk_device_priv *priv = _usbdk_device_priv(dev);
+ struct usbdk_device_priv *priv = _usbdk_device_priv(dev);
- memcpy(buffer, &priv->info.DeviceDescriptor, DEVICE_DESC_LENGTH);
- *host_endian = 0;
+ memcpy(buffer, &priv->info.DeviceDescriptor, DEVICE_DESC_LENGTH);
+ *host_endian = 0;
- return LIBUSB_SUCCESS;
+ return LIBUSB_SUCCESS;
}
static int usbdk_get_config_descriptor(struct libusb_device *dev, uint8_t config_index, unsigned char *buffer, size_t len, int *host_endian)
{
- struct usbdk_device_priv *priv = _usbdk_device_priv(dev);
- PUSB_CONFIGURATION_DESCRIPTOR config_header;
- size_t size;
+ struct usbdk_device_priv *priv = _usbdk_device_priv(dev);
+ PUSB_CONFIGURATION_DESCRIPTOR config_header;
+ size_t size;
- if (config_index >= dev->num_configurations)
- return LIBUSB_ERROR_INVALID_PARAM;
+ if (config_index >= dev->num_configurations)
+ return LIBUSB_ERROR_INVALID_PARAM;
- config_header = (PUSB_CONFIGURATION_DESCRIPTOR) priv->config_descriptors[config_index];
+ config_header = (PUSB_CONFIGURATION_DESCRIPTOR)priv->config_descriptors[config_index];
- size = min(config_header->wTotalLength, len);
- memcpy(buffer, config_header, size);
- *host_endian = 0;
+ size = min(config_header->wTotalLength, len);
+ memcpy(buffer, config_header, size);
+ *host_endian = 0;
- return (int)size;
+ return (int)size;
}
static inline int usbdk_get_active_config_descriptor(struct libusb_device *dev, unsigned char *buffer, size_t len, int *host_endian)
{
- return usbdk_get_config_descriptor(dev, _usbdk_device_priv(dev)->active_configuration,
- buffer, len, host_endian);
+ return usbdk_get_config_descriptor(dev, _usbdk_device_priv(dev)->active_configuration,
+ buffer, len, host_endian);
}
static int usbdk_open(struct libusb_device_handle *dev_handle)
{
- struct usbdk_device_priv *priv = _usbdk_device_priv(dev_handle->dev);
+ struct usbdk_device_priv *priv = _usbdk_device_priv(dev_handle->dev);
- 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");
- return LIBUSB_ERROR_OTHER;
- }
+ 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");
+ return LIBUSB_ERROR_OTHER;
+ }
- return LIBUSB_SUCCESS;
+ return LIBUSB_SUCCESS;
}
static void usbdk_close(struct libusb_device_handle *dev_handle)
{
- struct usbdk_device_priv *priv = _usbdk_device_priv(dev_handle->dev);
+ struct usbdk_device_priv *priv = _usbdk_device_priv(dev_handle->dev);
- if (!usbdk_helper.StopRedirect(priv->redirector_handle))
- {
- struct libusb_context *ctx = DEVICE_CTX(dev_handle->dev);
- usbi_err(ctx, "Redirector shutdown failed");
- }
+ if (!usbdk_helper.StopRedirect(priv->redirector_handle)) {
+ struct libusb_context *ctx = DEVICE_CTX(dev_handle->dev);
+ usbi_err(ctx, "Redirector shutdown failed");
+ }
}
static int usbdk_get_configuration(struct libusb_device_handle *dev_handle, int *config)
{
- *config = _usbdk_device_priv(dev_handle->dev)->active_configuration;
- return LIBUSB_SUCCESS;
+ *config = _usbdk_device_priv(dev_handle->dev)->active_configuration;
+
+ return LIBUSB_SUCCESS;
}
static int usbdk_set_configuration(struct libusb_device_handle *dev_handle, int config)
{
- UNREFERENCED_PARAMETER(dev_handle);
- UNREFERENCED_PARAMETER(config);
- return LIBUSB_SUCCESS;
+ UNUSED(dev_handle);
+ UNUSED(config);
+ return LIBUSB_SUCCESS;
}
static int usbdk_claim_interface(struct libusb_device_handle *dev_handle, int iface)
{
- UNREFERENCED_PARAMETER(dev_handle);
- UNREFERENCED_PARAMETER(iface);
- return LIBUSB_SUCCESS;
+ UNUSED(dev_handle);
+ UNUSED(iface);
+ return LIBUSB_SUCCESS;
}
static int usbdk_set_interface_altsetting(struct libusb_device_handle *dev_handle, int iface, int altsetting)
{
- struct libusb_context *ctx = DEVICE_CTX(dev_handle->dev);
- struct usbdk_device_priv *priv = _usbdk_device_priv(dev_handle->dev);
+ struct libusb_context *ctx = DEVICE_CTX(dev_handle->dev);
+ 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));
- return LIBUSB_ERROR_NO_DEVICE;
- }
+ if (!usbdk_helper.SetAltsetting(priv->redirector_handle, iface, altsetting)) {
+ usbi_err(ctx, "SetAltsetting failed: %s", windows_error_str(0));
+ return LIBUSB_ERROR_NO_DEVICE;
+ }
- return LIBUSB_SUCCESS;
+ return LIBUSB_SUCCESS;
}
static int usbdk_release_interface(struct libusb_device_handle *dev_handle, int iface)
{
- UNREFERENCED_PARAMETER(dev_handle);
- UNREFERENCED_PARAMETER(iface);
- return LIBUSB_SUCCESS;
+ UNUSED(dev_handle);
+ UNUSED(iface);
+ return LIBUSB_SUCCESS;
}
static int usbdk_clear_halt(struct libusb_device_handle *dev_handle, unsigned char endpoint)
{
- struct libusb_context *ctx = DEVICE_CTX(dev_handle->dev);
- struct usbdk_device_priv *priv = _usbdk_device_priv(dev_handle->dev);
+ struct libusb_context *ctx = DEVICE_CTX(dev_handle->dev);
+ 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));
- return LIBUSB_ERROR_NO_DEVICE;
- }
+ if (!usbdk_helper.ResetPipe(priv->redirector_handle, endpoint)) {
+ usbi_err(ctx, "ResetPipe failed: %s", windows_error_str(0));
+ return LIBUSB_ERROR_NO_DEVICE;
+ }
- return LIBUSB_SUCCESS;
+ return LIBUSB_SUCCESS;
}
static int usbdk_reset_device(struct libusb_device_handle *dev_handle)
{
- struct libusb_context *ctx = DEVICE_CTX(dev_handle->dev);
- struct usbdk_device_priv *priv = _usbdk_device_priv(dev_handle->dev);
+ struct libusb_context *ctx = DEVICE_CTX(dev_handle->dev);
+ 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));
- return LIBUSB_ERROR_NO_DEVICE;
- }
+ if (!usbdk_helper.ResetDevice(priv->redirector_handle)) {
+ usbi_err(ctx, "ResetDevice failed: %s", windows_error_str(0));
+ return LIBUSB_ERROR_NO_DEVICE;
+ }
- return LIBUSB_SUCCESS;
+ return LIBUSB_SUCCESS;
}
static int usbdk_kernel_driver_active(struct libusb_device_handle *dev_handle, int iface)
{
- UNREFERENCED_PARAMETER(dev_handle);
- UNREFERENCED_PARAMETER(iface);
- return LIBUSB_ERROR_NOT_SUPPORTED;
+ UNUSED(dev_handle);
+ UNUSED(iface);
+ return LIBUSB_ERROR_NOT_SUPPORTED;
}
static int usbdk_attach_kernel_driver(struct libusb_device_handle *dev_handle, int iface)
{
- UNREFERENCED_PARAMETER(dev_handle);
- UNREFERENCED_PARAMETER(iface);
- return LIBUSB_ERROR_NOT_SUPPORTED;
+ UNUSED(dev_handle);
+ UNUSED(iface);
+ return LIBUSB_ERROR_NOT_SUPPORTED;
}
static int usbdk_detach_kernel_driver(struct libusb_device_handle *dev_handle, int iface)
{
- UNREFERENCED_PARAMETER(dev_handle);
- UNREFERENCED_PARAMETER(iface);
- return LIBUSB_ERROR_NOT_SUPPORTED;
+ UNUSED(dev_handle);
+ UNUSED(iface);
+ return LIBUSB_ERROR_NOT_SUPPORTED;
}
static void usbdk_destroy_device(struct libusb_device *dev)
{
- struct usbdk_device_priv* p = _usbdk_device_priv(dev);
+ struct usbdk_device_priv* p = _usbdk_device_priv(dev);
- if (p->config_descriptors != NULL)
- {
- usbdk_release_config_descriptors(p, p->info.DeviceDescriptor.bNumConfigurations);
- }
+ if (p->config_descriptors != NULL)
+ usbdk_release_config_descriptors(p, p->info.DeviceDescriptor.bNumConfigurations);
}
void windows_clear_transfer_priv(struct usbi_transfer *itransfer)
{
- struct usbdk_transfer_priv *transfer_priv = _usbdk_transfer_priv(itransfer);
- struct libusb_transfer *transfer = USBI_TRANSFER_TO_LIBUSB_TRANSFER(itransfer);
+ struct usbdk_transfer_priv *transfer_priv = _usbdk_transfer_priv(itransfer);
+ struct libusb_transfer *transfer = USBI_TRANSFER_TO_LIBUSB_TRANSFER(itransfer);
- usbi_free_fd(&transfer_priv->pollable_fd);
+ usbi_free_fd(&transfer_priv->pollable_fd);
- if (transfer->type == LIBUSB_TRANSFER_TYPE_ISOCHRONOUS){
- safe_free(transfer_priv->IsochronousPacketsArray);
- safe_free(transfer_priv->IsochronousResultsArray);
- }
+ if (transfer->type == LIBUSB_TRANSFER_TYPE_ISOCHRONOUS) {
+ safe_free(transfer_priv->IsochronousPacketsArray);
+ safe_free(transfer_priv->IsochronousResultsArray);
+ }
}
static int usbdk_do_control_transfer(struct usbi_transfer *itransfer)
{
- ULONG Length;
- TransferResult transResult;
-
- 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 = DEVICE_CTX(transfer->dev_handle->dev);
-
- HANDLE sysHandle = usbdk_helper.GetRedirectorSystemHandle(priv->redirector_handle);
-
- struct winfd wfd = usbi_create_fd(sysHandle, RW_READ, NULL, NULL);
- // Always use the handle returned from usbi_create_fd (wfd.handle)
- if (wfd.fd < 0) {
- return LIBUSB_ERROR_NO_MEM;
- }
-
- transfer_priv->request.Buffer = (PVOID64)(uintptr_t)transfer->buffer;
- transfer_priv->request.BufferLength = transfer->length;
- transfer_priv->request.TransferType = ControlTransferType;
- transfer_priv->pollable_fd = INVALID_WINFD;
- Length = (ULONG)transfer->length;
-
- if (IS_XFERIN(transfer)) {
- transResult = usbdk_helper.ReadPipe(priv->redirector_handle, &transfer_priv->request, wfd.overlapped);
- }
- else {
- transResult = usbdk_helper.WritePipe(priv->redirector_handle, &transfer_priv->request, wfd.overlapped);
- }
-
-
- switch (transResult)
- {
- case TransferSuccess:
- wfd.overlapped->Internal = STATUS_COMPLETED_SYNCHRONOUSLY;
- wfd.overlapped->InternalHigh = (DWORD)Length;
- break;
- case TransferSuccessAsync:
- break;
- case TransferFailure:
- {
- usbi_err(ctx, "ControlTransfer failed: %s", windows_error_str(0));
- usbi_free_fd(&wfd);
- return LIBUSB_ERROR_IO;
- }
- }
-
- // Use priv_transfer to store data needed for async polling
- transfer_priv->pollable_fd = wfd;
- usbi_add_pollfd(ctx, transfer_priv->pollable_fd.fd, POLLIN);
- return LIBUSB_SUCCESS;
+ 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 = DEVICE_CTX(transfer->dev_handle->dev);
+ ULONG Length;
+ TransferResult transResult;
+ HANDLE sysHandle;
+
+ sysHandle = usbdk_helper.GetRedirectorSystemHandle(priv->redirector_handle);
+
+ struct winfd wfd = usbi_create_fd(sysHandle, RW_READ, NULL, NULL);
+ // Always use the handle returned from usbi_create_fd (wfd.handle)
+ if (wfd.fd < 0)
+ return LIBUSB_ERROR_NO_MEM;
+
+ transfer_priv->request.Buffer = (PVOID64)(uintptr_t)transfer->buffer;
+ transfer_priv->request.BufferLength = transfer->length;
+ transfer_priv->request.TransferType = ControlTransferType;
+ transfer_priv->pollable_fd = INVALID_WINFD;
+ Length = (ULONG)transfer->length;
+
+ if (IS_XFERIN(transfer))
+ transResult = usbdk_helper.ReadPipe(priv->redirector_handle, &transfer_priv->request, wfd.overlapped);
+ else
+ transResult = usbdk_helper.WritePipe(priv->redirector_handle, &transfer_priv->request, wfd.overlapped);
+
+ switch (transResult) {
+ case TransferSuccess:
+ wfd.overlapped->Internal = STATUS_COMPLETED_SYNCHRONOUSLY;
+ wfd.overlapped->InternalHigh = (DWORD)Length;
+ break;
+ case TransferSuccessAsync:
+ break;
+ case TransferFailure:
+ usbi_err(ctx, "ControlTransfer failed: %s", windows_error_str(0));
+ usbi_free_fd(&wfd);
+ return LIBUSB_ERROR_IO;
+ }
+
+ // Use priv_transfer to store data needed for async polling
+ transfer_priv->pollable_fd = wfd;
+ usbi_add_pollfd(ctx, transfer_priv->pollable_fd.fd, POLLIN);
+
+ return LIBUSB_SUCCESS;
}
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 = DEVICE_CTX(transfer->dev_handle->dev);
- struct winfd wfd;
- TransferResult transferRes;
- HANDLE sysHandle;
-
- transfer_priv->request.Buffer = (PVOID64) (uintptr_t) transfer->buffer;
- transfer_priv->request.BufferLength = transfer->length;
- transfer_priv->request.EndpointAddress = transfer->endpoint;
-
- switch (transfer->type)
- {
- case LIBUSB_TRANSFER_TYPE_BULK:
- transfer_priv->request.TransferType = BulkTransferType;
- break;
- case LIBUSB_TRANSFER_TYPE_INTERRUPT:
- transfer_priv->request.TransferType = IntertuptTransferType;
- break;
- default:
- usbi_err(ctx, "Wrong transfer type (%d) in usbdk_do_bulk_transfer. %s", transfer->type, windows_error_str(0));
- return LIBUSB_ERROR_INVALID_PARAM;
- }
-
- transfer_priv->pollable_fd = INVALID_WINFD;
-
- sysHandle = usbdk_helper.GetRedirectorSystemHandle(priv->redirector_handle);
-
- wfd = usbi_create_fd(sysHandle, IS_XFERIN(transfer) ? RW_READ : RW_WRITE, NULL, NULL);
- // Always use the handle returned from usbi_create_fd (wfd.handle)
- if (wfd.fd < 0) {
- return LIBUSB_ERROR_NO_MEM;
- }
-
- if (IS_XFERIN(transfer)) {
- transferRes = usbdk_helper.ReadPipe(priv->redirector_handle, &transfer_priv->request, wfd.overlapped);
- }
- else {
- transferRes = usbdk_helper.WritePipe(priv->redirector_handle, &transfer_priv->request, wfd.overlapped);
- }
-
- switch (transferRes)
- {
- case TransferSuccess:
- wfd.overlapped->Internal = STATUS_COMPLETED_SYNCHRONOUSLY;
- break;
- case TransferSuccessAsync:
- break;
- case TransferFailure:
- {
- usbi_err(ctx, "ReadPipe/WritePipe failed: %s", windows_error_str(0));
- usbi_free_fd(&wfd);
- return LIBUSB_ERROR_IO;
- }
- }
-
- transfer_priv->pollable_fd = wfd;
- usbi_add_pollfd(ctx, transfer_priv->pollable_fd.fd, (short)(IS_XFERIN(transfer) ? POLLIN : POLLOUT));
- return LIBUSB_SUCCESS;
+ 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 = DEVICE_CTX(transfer->dev_handle->dev);
+ struct winfd wfd;
+ TransferResult transferRes;
+ HANDLE sysHandle;
+
+ transfer_priv->request.Buffer = (PVOID64)(uintptr_t)transfer->buffer;
+ transfer_priv->request.BufferLength = transfer->length;
+ transfer_priv->request.EndpointAddress = transfer->endpoint;
+
+ switch (transfer->type) {
+ case LIBUSB_TRANSFER_TYPE_BULK:
+ transfer_priv->request.TransferType = BulkTransferType;
+ break;
+ case LIBUSB_TRANSFER_TYPE_INTERRUPT:
+ transfer_priv->request.TransferType = IntertuptTransferType;
+ break;
+ default:
+ usbi_err(ctx, "Wrong transfer type (%d) in usbdk_do_bulk_transfer. %s", transfer->type, windows_error_str(0));
+ return LIBUSB_ERROR_INVALID_PARAM;
+ }
+
+ transfer_priv->pollable_fd = INVALID_WINFD;
+
+ sysHandle = usbdk_helper.GetRedirectorSystemHandle(priv->redirector_handle);
+
+ wfd = usbi_create_fd(sysHandle, IS_XFERIN(transfer) ? RW_READ : RW_WRITE, NULL, NULL);
+ // Always use the handle returned from usbi_create_fd (wfd.handle)
+ if (wfd.fd < 0)
+ return LIBUSB_ERROR_NO_MEM;
+
+ if (IS_XFERIN(transfer))
+ transferRes = usbdk_helper.ReadPipe(priv->redirector_handle, &transfer_priv->request, wfd.overlapped);
+ else
+ transferRes = usbdk_helper.WritePipe(priv->redirector_handle, &transfer_priv->request, wfd.overlapped);
+
+ switch (transferRes) {
+ case TransferSuccess:
+ wfd.overlapped->Internal = STATUS_COMPLETED_SYNCHRONOUSLY;
+ break;
+ case TransferSuccessAsync:
+ break;
+ case TransferFailure:
+ usbi_err(ctx, "ReadPipe/WritePipe failed: %s", windows_error_str(0));
+ usbi_free_fd(&wfd);
+ return LIBUSB_ERROR_IO;
+ }
+
+ transfer_priv->pollable_fd = wfd;
+ usbi_add_pollfd(ctx, transfer_priv->pollable_fd.fd, IS_XFERIN(transfer) ? POLLIN : POLLOUT);
+
+ return LIBUSB_SUCCESS;
}
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 = DEVICE_CTX(transfer->dev_handle->dev);
- struct winfd wfd;
- TransferResult transferRes;
- int i;
- HANDLE sysHandle;
-
- transfer_priv->request.Buffer = (PVOID64)(uintptr_t)transfer->buffer;
- transfer_priv->request.BufferLength = transfer->length;
- transfer_priv->request.EndpointAddress = transfer->endpoint;
- transfer_priv->request.TransferType = IsochronousTransferType;
- transfer_priv->request.IsochronousPacketsArraySize = transfer->num_iso_packets;
- transfer_priv->IsochronousPacketsArray = malloc(transfer->num_iso_packets * sizeof(ULONG64));
- transfer_priv->request.IsochronousPacketsArray = (PVOID64)(uintptr_t)transfer_priv->IsochronousPacketsArray;
- if (!transfer_priv->IsochronousPacketsArray){
- usbi_err(ctx, "Allocation of IsochronousPacketsArray is failed, %s", windows_error_str(0));
- return LIBUSB_ERROR_IO;
- }
-
- transfer_priv->IsochronousResultsArray = malloc(transfer->num_iso_packets * sizeof(USB_DK_ISO_TRANSFER_RESULT));
- transfer_priv->request.Result.IsochronousResultsArray = (PVOID64)(uintptr_t)transfer_priv->IsochronousResultsArray;
- if (!transfer_priv->IsochronousResultsArray){
- usbi_err(ctx, "Allocation of isochronousResultsArray is failed, %s", windows_error_str(0));
- free(transfer_priv->IsochronousPacketsArray);
- return LIBUSB_ERROR_IO;
- }
-
- for (i = 0; i < transfer->num_iso_packets; i++){
- transfer_priv->IsochronousPacketsArray[i] = transfer->iso_packet_desc[i].length;
- }
-
- transfer_priv->pollable_fd = INVALID_WINFD;
-
- sysHandle = usbdk_helper.GetRedirectorSystemHandle(priv->redirector_handle);
-
- wfd = usbi_create_fd(sysHandle, IS_XFERIN(transfer) ? RW_READ : RW_WRITE, NULL, NULL);
- // Always use the handle returned from usbi_create_fd (wfd.handle)
- if (wfd.fd < 0) {
- free(transfer_priv->IsochronousPacketsArray);
- free(transfer_priv->IsochronousResultsArray);
- return LIBUSB_ERROR_NO_MEM;
- }
-
- if (IS_XFERIN(transfer)) {
- transferRes = usbdk_helper.ReadPipe(priv->redirector_handle, &transfer_priv->request, wfd.overlapped);
- }
- else {
- transferRes = usbdk_helper.WritePipe(priv->redirector_handle, &transfer_priv->request, wfd.overlapped);
- }
-
- switch (transferRes){
- case TransferSuccess:
- wfd.overlapped->Internal = STATUS_COMPLETED_SYNCHRONOUSLY;
- break;
- case TransferSuccessAsync:
- break;
- case TransferFailure:
- {
- usbi_err(ctx, "ReadPipe/WritePipe failed: %s", windows_error_str(0));
- usbi_free_fd(&wfd);
- free(transfer_priv->IsochronousPacketsArray);
- free(transfer_priv->IsochronousResultsArray);
- return LIBUSB_ERROR_IO;
- }
- }
-
- transfer_priv->pollable_fd = wfd;
- usbi_add_pollfd(ctx, transfer_priv->pollable_fd.fd, (short)(IS_XFERIN(transfer) ? POLLIN : POLLOUT));
-
- return LIBUSB_SUCCESS;
+ 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 = DEVICE_CTX(transfer->dev_handle->dev);
+ struct winfd wfd;
+ TransferResult transferRes;
+ int i;
+ HANDLE sysHandle;
+
+ transfer_priv->request.Buffer = (PVOID64)(uintptr_t)transfer->buffer;
+ transfer_priv->request.BufferLength = transfer->length;
+ transfer_priv->request.EndpointAddress = transfer->endpoint;
+ transfer_priv->request.TransferType = IsochronousTransferType;
+ transfer_priv->request.IsochronousPacketsArraySize = transfer->num_iso_packets;
+ transfer_priv->IsochronousPacketsArray = malloc(transfer->num_iso_packets * sizeof(ULONG64));
+ transfer_priv->request.IsochronousPacketsArray = (PVOID64)(uintptr_t)transfer_priv->IsochronousPacketsArray;
+ if (!transfer_priv->IsochronousPacketsArray) {
+ usbi_err(ctx, "Allocation of IsochronousPacketsArray is failed, %s", windows_error_str(0));
+ return LIBUSB_ERROR_IO;
+ }
+
+ transfer_priv->IsochronousResultsArray = malloc(transfer->num_iso_packets * sizeof(USB_DK_ISO_TRANSFER_RESULT));
+ transfer_priv->request.Result.IsochronousResultsArray = (PVOID64)(uintptr_t)transfer_priv->IsochronousResultsArray;
+ if (!transfer_priv->IsochronousResultsArray) {
+ usbi_err(ctx, "Allocation of isochronousResultsArray is failed, %s", windows_error_str(0));
+ free(transfer_priv->IsochronousPacketsArray);
+ return LIBUSB_ERROR_IO;
+ }
+
+ for (i = 0; i < transfer->num_iso_packets; i++)
+ transfer_priv->IsochronousPacketsArray[i] = transfer->iso_packet_desc[i].length;
+
+ transfer_priv->pollable_fd = INVALID_WINFD;
+
+ sysHandle = usbdk_helper.GetRedirectorSystemHandle(priv->redirector_handle);
+
+ wfd = usbi_create_fd(sysHandle, IS_XFERIN(transfer) ? RW_READ : RW_WRITE, NULL, NULL);
+ // Always use the handle returned from usbi_create_fd (wfd.handle)
+ if (wfd.fd < 0) {
+ free(transfer_priv->IsochronousPacketsArray);
+ free(transfer_priv->IsochronousResultsArray);
+ return LIBUSB_ERROR_NO_MEM;
+ }
+
+ if (IS_XFERIN(transfer))
+ transferRes = usbdk_helper.ReadPipe(priv->redirector_handle, &transfer_priv->request, wfd.overlapped);
+ else
+ transferRes = usbdk_helper.WritePipe(priv->redirector_handle, &transfer_priv->request, wfd.overlapped);
+
+ switch (transferRes) {
+ case TransferSuccess:
+ wfd.overlapped->Internal = STATUS_COMPLETED_SYNCHRONOUSLY;
+ break;
+ case TransferSuccessAsync:
+ break;
+ case TransferFailure:
+ usbi_err(ctx, "ReadPipe/WritePipe failed: %s", windows_error_str(0));
+ usbi_free_fd(&wfd);
+ free(transfer_priv->IsochronousPacketsArray);
+ free(transfer_priv->IsochronousResultsArray);
+ return LIBUSB_ERROR_IO;
+ }
+
+ transfer_priv->pollable_fd = wfd;
+ usbi_add_pollfd(ctx, transfer_priv->pollable_fd.fd, IS_XFERIN(transfer) ? POLLIN : POLLOUT);
+
+ return LIBUSB_SUCCESS;
}
static int usbdk_submit_transfer(struct usbi_transfer *itransfer)
{
- struct libusb_transfer *transfer = USBI_TRANSFER_TO_LIBUSB_TRANSFER(itransfer);
-
- switch (transfer->type) {
- case LIBUSB_TRANSFER_TYPE_CONTROL:
- 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) {
- //TODO: Check whether we can support this in UsbDk
- return LIBUSB_ERROR_NOT_SUPPORTED;
- } else {
- return usbdk_do_bulk_transfer(itransfer);
- }
-
- case LIBUSB_TRANSFER_TYPE_ISOCHRONOUS:
- return usbdk_do_iso_transfer(itransfer);
- default:
- usbi_err(TRANSFER_CTX(transfer), "unknown endpoint type %d", transfer->type);
- return LIBUSB_ERROR_INVALID_PARAM;
- }
+ struct libusb_transfer *transfer = USBI_TRANSFER_TO_LIBUSB_TRANSFER(itransfer);
+
+ switch (transfer->type) {
+ case LIBUSB_TRANSFER_TYPE_CONTROL:
+ 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
+ else
+ return usbdk_do_bulk_transfer(itransfer);
+ case LIBUSB_TRANSFER_TYPE_ISOCHRONOUS:
+ return usbdk_do_iso_transfer(itransfer);
+ default:
+ usbi_err(TRANSFER_CTX(transfer), "unknown endpoint type %d", transfer->type);
+ return LIBUSB_ERROR_INVALID_PARAM;
+ }
}
static int usbdk_abort_transfers(struct usbi_transfer *itransfer)
{
- struct libusb_transfer *transfer = USBI_TRANSFER_TO_LIBUSB_TRANSFER(itransfer);
- struct libusb_context *ctx = DEVICE_CTX(transfer->dev_handle->dev);
- struct usbdk_device_priv *priv = _usbdk_device_priv(transfer->dev_handle->dev);
+ struct libusb_transfer *transfer = USBI_TRANSFER_TO_LIBUSB_TRANSFER(itransfer);
+ struct libusb_context *ctx = DEVICE_CTX(transfer->dev_handle->dev);
+ struct usbdk_device_priv *priv = _usbdk_device_priv(transfer->dev_handle->dev);
- if (!usbdk_helper.AbortPipe(priv->redirector_handle, transfer->endpoint)) {
- usbi_err(ctx, "AbortPipe failed: %s", windows_error_str(0));
- return LIBUSB_ERROR_NO_DEVICE;
- }
+ if (!usbdk_helper.AbortPipe(priv->redirector_handle, transfer->endpoint)) {
+ usbi_err(ctx, "AbortPipe failed: %s", windows_error_str(0));
+ return LIBUSB_ERROR_NO_DEVICE;
+ }
- return LIBUSB_SUCCESS;
+ return LIBUSB_SUCCESS;
}
static int usbdk_cancel_transfer(struct usbi_transfer *itransfer)
{
- struct libusb_transfer *transfer = USBI_TRANSFER_TO_LIBUSB_TRANSFER(itransfer);
+ struct libusb_transfer *transfer = USBI_TRANSFER_TO_LIBUSB_TRANSFER(itransfer);
- 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;
- }
+ 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;
+ }
}
int windows_copy_transfer_data(struct usbi_transfer *itransfer, uint32_t io_size)
{
- itransfer->transferred += io_size;
- return LIBUSB_TRANSFER_COMPLETED;
+ itransfer->transferred += io_size;
+ return LIBUSB_TRANSFER_COMPLETED;
}
struct winfd *windows_get_fd(struct usbi_transfer *transfer)
{
- struct usbdk_transfer_priv *transfer_priv = _usbdk_transfer_priv(transfer);
- return &transfer_priv->pollable_fd;
+ struct usbdk_transfer_priv *transfer_priv = _usbdk_transfer_priv(transfer);
+ return &transfer_priv->pollable_fd;
}
static DWORD usbdk_translate_usbd_status(USBD_STATUS UsbdStatus)
{
- if (USBD_SUCCESS(UsbdStatus))
- {
- return NO_ERROR;
- }
-
- switch (UsbdStatus)
- {
- case USBD_STATUS_STALL_PID:
- case USBD_STATUS_ENDPOINT_HALTED:
- case USBD_STATUS_BAD_START_FRAME:
- return ERROR_GEN_FAILURE;
- case USBD_STATUS_TIMEOUT:
- return ERROR_SEM_TIMEOUT;
- case USBD_STATUS_CANCELED:
- return ERROR_OPERATION_ABORTED;
- default:
- return ERROR_FUNCTION_FAILED;
- }
+ if (USBD_SUCCESS(UsbdStatus))
+ return NO_ERROR;
+
+ switch (UsbdStatus) {
+ case USBD_STATUS_STALL_PID:
+ case USBD_STATUS_ENDPOINT_HALTED:
+ case USBD_STATUS_BAD_START_FRAME:
+ return ERROR_GEN_FAILURE;
+ case USBD_STATUS_TIMEOUT:
+ return ERROR_SEM_TIMEOUT;
+ case USBD_STATUS_CANCELED:
+ return ERROR_OPERATION_ABORTED;
+ default:
+ return ERROR_FUNCTION_FAILED;
+ }
}
void windows_get_overlapped_result(struct usbi_transfer *transfer, struct winfd *pollable_fd, DWORD *io_result, DWORD *io_size)
{
- if (HasOverlappedIoCompletedSync(pollable_fd->overlapped) || // Handle async requests that completed synchronously first
- GetOverlappedResult(pollable_fd->handle, pollable_fd->overlapped, io_size, false)) { // Regular async overlapped
- struct libusb_transfer *ltransfer = USBI_TRANSFER_TO_LIBUSB_TRANSFER(transfer);
- struct usbdk_transfer_priv *transfer_priv = _usbdk_transfer_priv(transfer);
-
- if (ltransfer->type == LIBUSB_TRANSFER_TYPE_ISOCHRONOUS){
- int i;
- for (i = 0; i < transfer_priv->request.IsochronousPacketsArraySize; i++) {
- struct libusb_iso_packet_descriptor *lib_desc = &ltransfer->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;
- }
- }
-
- *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();
- }
+ if (HasOverlappedIoCompletedSync(pollable_fd->overlapped) // Handle async requests that completed synchronously first
+ || GetOverlappedResult(pollable_fd->handle, pollable_fd->overlapped, io_size, false)) { // Regular async overlapped
+ struct libusb_transfer *ltransfer = USBI_TRANSFER_TO_LIBUSB_TRANSFER(transfer);
+ struct usbdk_transfer_priv *transfer_priv = _usbdk_transfer_priv(transfer);
+
+ if (ltransfer->type == LIBUSB_TRANSFER_TYPE_ISOCHRONOUS) {
+ int i;
+ for (i = 0; i < transfer_priv->request.IsochronousPacketsArraySize; i++) {
+ struct libusb_iso_packet_descriptor *lib_desc = &ltransfer->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;
+ }
+ }
+
+ *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();
+ }
}
static int usbdk_clock_gettime(int clk_id, struct timespec *tp)
{
- return windows_clock_gettime(clk_id, tp);
+ return windows_clock_gettime(clk_id, tp);
}
const struct usbi_os_backend usbdk_backend = {
- "Windows",
- USBI_CAP_HAS_HID_ACCESS,
- usbdk_init,
- usbdk_exit,
+ "Windows",
+ USBI_CAP_HAS_HID_ACCESS,
+ usbdk_init,
+ usbdk_exit,
- usbdk_get_device_list,
- NULL,
- usbdk_open,
- usbdk_close,
+ usbdk_get_device_list,
+ NULL,
+ usbdk_open,
+ usbdk_close,
- usbdk_get_device_descriptor,
- usbdk_get_active_config_descriptor,
- usbdk_get_config_descriptor,
- NULL,
+ usbdk_get_device_descriptor,
+ usbdk_get_active_config_descriptor,
+ usbdk_get_config_descriptor,
+ NULL,
- usbdk_get_configuration,
- usbdk_set_configuration,
- usbdk_claim_interface,
- usbdk_release_interface,
+ usbdk_get_configuration,
+ usbdk_set_configuration,
+ usbdk_claim_interface,
+ usbdk_release_interface,
- usbdk_set_interface_altsetting,
- usbdk_clear_halt,
- usbdk_reset_device,
+ usbdk_set_interface_altsetting,
+ usbdk_clear_halt,
+ usbdk_reset_device,
- NULL,
- NULL,
+ NULL,
+ NULL,
- usbdk_kernel_driver_active,
- usbdk_detach_kernel_driver,
- usbdk_attach_kernel_driver,
+ usbdk_kernel_driver_active,
+ usbdk_detach_kernel_driver,
+ usbdk_attach_kernel_driver,
- usbdk_destroy_device,
+ usbdk_destroy_device,
- usbdk_submit_transfer,
- usbdk_cancel_transfer,
- windows_clear_transfer_priv,
+ usbdk_submit_transfer,
+ usbdk_cancel_transfer,
+ windows_clear_transfer_priv,
- windows_handle_events,
- NULL,
+ windows_handle_events,
+ NULL,
- usbdk_clock_gettime,
+ usbdk_clock_gettime,
#if defined(USBI_TIMERFD_AVAILABLE)
- NULL,
+ NULL,
#endif
- sizeof(struct usbdk_device_priv),
- sizeof(struct usbdk_device_handle_priv),
- sizeof(struct usbdk_transfer_priv),
+ sizeof(struct usbdk_device_priv),
+ sizeof(struct usbdk_device_handle_priv),
+ sizeof(struct usbdk_transfer_priv),
};
#endif /* USE_USBDK */
diff --git a/libusb/os/windows_usbdk.h b/libusb/os/windows_usbdk.h
index 6b71735..04a9787 100644
--- a/libusb/os/windows_usbdk.h
+++ b/libusb/os/windows_usbdk.h
@@ -23,104 +23,124 @@
#pragma once
-typedef struct tag_USB_DK_DEVICE_ID
-{
- WCHAR DeviceID[MAX_DEVICE_ID_LEN];
- WCHAR InstanceID[MAX_DEVICE_ID_LEN];
+typedef struct tag_USB_DK_DEVICE_ID {
+ WCHAR DeviceID[MAX_DEVICE_ID_LEN];
+ WCHAR InstanceID[MAX_DEVICE_ID_LEN];
} USB_DK_DEVICE_ID, *PUSB_DK_DEVICE_ID;
-static inline
-void UsbDkFillIDStruct(USB_DK_DEVICE_ID *ID, PCWCHAR DeviceID, PCWCHAR InstanceID)
+static inline void UsbDkFillIDStruct(USB_DK_DEVICE_ID *ID, PCWCHAR DeviceID, PCWCHAR InstanceID)
{
- wcsncpy_s(ID->DeviceID, DeviceID, MAX_DEVICE_ID_LEN);
- wcsncpy_s(ID->InstanceID, InstanceID, MAX_DEVICE_ID_LEN);
+ wcsncpy_s(ID->DeviceID, DeviceID, MAX_DEVICE_ID_LEN);
+ wcsncpy_s(ID->InstanceID, InstanceID, MAX_DEVICE_ID_LEN);
}
-typedef struct tag_USB_DK_DEVICE_INFO
-{
- USB_DK_DEVICE_ID ID;
- ULONG64 FilterID;
- ULONG64 Port;
- ULONG64 Speed;
- USB_DEVICE_DESCRIPTOR DeviceDescriptor;
+typedef struct tag_USB_DK_DEVICE_INFO {
+ USB_DK_DEVICE_ID ID;
+ ULONG64 FilterID;
+ ULONG64 Port;
+ ULONG64 Speed;
+ USB_DEVICE_DESCRIPTOR DeviceDescriptor;
} USB_DK_DEVICE_INFO, *PUSB_DK_DEVICE_INFO;
-typedef struct tag_USB_DK_CONFIG_DESCRIPTOR_REQUEST
-{
- USB_DK_DEVICE_ID ID;
- ULONG64 Index;
+typedef struct tag_USB_DK_CONFIG_DESCRIPTOR_REQUEST {
+ USB_DK_DEVICE_ID ID;
+ ULONG64 Index;
} USB_DK_CONFIG_DESCRIPTOR_REQUEST, *PUSB_DK_CONFIG_DESCRIPTOR_REQUEST;
-typedef struct tag_USB_DK_ISO_TRANSFER_RESULT
-{
- ULONG64 ActualLength;
- ULONG64 TransferResult;
+typedef struct tag_USB_DK_ISO_TRANSFER_RESULT {
+ ULONG64 ActualLength;
+ ULONG64 TransferResult;
} USB_DK_ISO_TRANSFER_RESULT, *PUSB_DK_ISO_TRANSFER_RESULT;
-typedef struct tag_USB_DK_GEN_TRANSFER_RESULT
-{
- ULONG64 BytesTransferred;
- ULONG64 UsbdStatus; // USBD_STATUS code
+typedef struct tag_USB_DK_GEN_TRANSFER_RESULT {
+ ULONG64 BytesTransferred;
+ ULONG64 UsbdStatus; // USBD_STATUS code
} USB_DK_GEN_TRANSFER_RESULT, *PUSB_DK_GEN_TRANSFER_RESULT;
-typedef struct tag_USB_DK_TRANSFER_RESULT
-{
- USB_DK_GEN_TRANSFER_RESULT GenResult;
- PVOID64 IsochronousResultsArray; // array of USB_DK_ISO_TRANSFER_RESULT
+typedef struct tag_USB_DK_TRANSFER_RESULT {
+ USB_DK_GEN_TRANSFER_RESULT GenResult;
+ PVOID64 IsochronousResultsArray; // array of USB_DK_ISO_TRANSFER_RESULT
} USB_DK_TRANSFER_RESULT, *PUSB_DK_TRANSFER_RESULT;
-typedef struct tag_USB_DK_TRANSFER_REQUEST
-{
- ULONG64 EndpointAddress;
- PVOID64 Buffer;
- ULONG64 BufferLength;
- ULONG64 TransferType;
- ULONG64 IsochronousPacketsArraySize;
- PVOID64 IsochronousPacketsArray;
+typedef struct tag_USB_DK_TRANSFER_REQUEST {
+ ULONG64 EndpointAddress;
+ PVOID64 Buffer;
+ ULONG64 BufferLength;
+ ULONG64 TransferType;
+ ULONG64 IsochronousPacketsArraySize;
+ PVOID64 IsochronousPacketsArray;
- USB_DK_TRANSFER_RESULT Result;
+ USB_DK_TRANSFER_RESULT Result;
} USB_DK_TRANSFER_REQUEST, *PUSB_DK_TRANSFER_REQUEST;
-typedef enum
-{
- TransferFailure = 0,
- TransferSuccess,
- TransferSuccessAsync
+typedef enum {
+ TransferFailure = 0,
+ TransferSuccess,
+ TransferSuccessAsync
} TransferResult;
-typedef enum
-{
- NoSpeed = 0,
- LowSpeed,
- FullSpeed,
- HighSpeed,
- SuperSpeed
+typedef enum {
+ NoSpeed = 0,
+ LowSpeed,
+ FullSpeed,
+ HighSpeed,
+ SuperSpeed
} USB_DK_DEVICE_SPEED;
-typedef enum
-{
- ControlTransferType,
- BulkTransferType,
- IntertuptTransferType,
- IsochronousTransferType
+typedef enum {
+ ControlTransferType,
+ BulkTransferType,
+ IntertuptTransferType,
+ IsochronousTransferType
} USB_DK_TRANSFER_TYPE;
-typedef BOOL (__cdecl *USBDK_GET_DEVICES_LIST) (PUSB_DK_DEVICE_INFO *, PULONG);
-typedef void (__cdecl *USBDK_RELEASE_DEVICES_LIST) (PUSB_DK_DEVICE_INFO);
-
-typedef HANDLE (__cdecl *USBDK_START_REDIRECT) (PUSB_DK_DEVICE_ID);
-typedef BOOL (__cdecl *USBDK_STOP_REDIRECT) (HANDLE);
-
-typedef BOOL (__cdecl *USBDK_GET_CONFIGURATION_DESCRIPTOR) (PUSB_DK_CONFIG_DESCRIPTOR_REQUEST Request,
- PUSB_CONFIGURATION_DESCRIPTOR *Descriptor,
- PULONG Length);
-typedef void (__cdecl *USBDK_RELEASE_CONFIGURATION_DESCRIPTOR) (PUSB_CONFIGURATION_DESCRIPTOR Descriptor);
-
-typedef TransferResult (__cdecl *USBDK_WRITE_PIPE) (HANDLE DeviceHandle, PUSB_DK_TRANSFER_REQUEST Request, LPOVERLAPPED lpOverlapped);
-typedef TransferResult (__cdecl *USBDK_READ_PIPE) (HANDLE DeviceHandle, PUSB_DK_TRANSFER_REQUEST Request, LPOVERLAPPED lpOverlapped);
-typedef BOOL (__cdecl *USBDK_ABORT_PIPE) (HANDLE DeviceHandle, ULONG64 PipeAddress);
-typedef BOOL (__cdecl *USBDK_RESET_PIPE) (HANDLE DeviceHandle, ULONG64 PipeAddress);
-typedef BOOL (__cdecl *USBDK_SET_ALTSETTING) (HANDLE DeviceHandle, ULONG64 InterfaceIdx, ULONG64 AltSettingIdx);
-typedef BOOL (__cdecl *USBDK_RESET_DEVICE) (HANDLE DeviceHandle);
-
-typedef HANDLE (__cdecl *USBDK_GET_REDIRECTOR_SYSTEM_HANDLE) (HANDLE DeviceHandle);
+typedef BOOL (__cdecl *USBDK_GET_DEVICES_LIST)(
+ PUSB_DK_DEVICE_INFO *DeviceInfo,
+ PULONG DeviceNumber
+);
+typedef void (__cdecl *USBDK_RELEASE_DEVICES_LIST)(
+ PUSB_DK_DEVICE_INFO DeviceInfo
+);
+typedef HANDLE (__cdecl *USBDK_START_REDIRECT)(
+ PUSB_DK_DEVICE_ID DeviceId
+);
+typedef BOOL (__cdecl *USBDK_STOP_REDIRECT)(
+ HANDLE DeviceHandle
+);
+typedef BOOL (__cdecl *USBDK_GET_CONFIGURATION_DESCRIPTOR)(
+ PUSB_DK_CONFIG_DESCRIPTOR_REQUEST Request,
+ PUSB_CONFIGURATION_DESCRIPTOR *Descriptor,
+ PULONG Length
+);
+typedef void (__cdecl *USBDK_RELEASE_CONFIGURATION_DESCRIPTOR)(
+ PUSB_CONFIGURATION_DESCRIPTOR Descriptor
+);
+typedef TransferResult (__cdecl *USBDK_WRITE_PIPE)(
+ HANDLE DeviceHandle,
+ PUSB_DK_TRANSFER_REQUEST Request,
+ LPOVERLAPPED lpOverlapped
+);
+typedef TransferResult (__cdecl *USBDK_READ_PIPE)(
+ HANDLE DeviceHandle,
+ PUSB_DK_TRANSFER_REQUEST Request,
+ LPOVERLAPPED lpOverlapped
+);
+typedef BOOL (__cdecl *USBDK_ABORT_PIPE)(
+ HANDLE DeviceHandle,
+ ULONG64 PipeAddress
+);
+typedef BOOL (__cdecl *USBDK_RESET_PIPE)(
+ HANDLE DeviceHandle,
+ ULONG64 PipeAddress
+);
+typedef BOOL (__cdecl *USBDK_SET_ALTSETTING)(
+ HANDLE DeviceHandle,
+ ULONG64 InterfaceIdx,
+ ULONG64 AltSettingIdx
+);
+typedef BOOL (__cdecl *USBDK_RESET_DEVICE)(
+ HANDLE DeviceHandle
+);
+typedef HANDLE (__cdecl *USBDK_GET_REDIRECTOR_SYSTEM_HANDLE)(
+ HANDLE DeviceHandle
+);
diff --git a/libusb/os/windows_winusb.c b/libusb/os/windows_winusb.c
index ef40096..0488e8e 100644
--- a/libusb/os/windows_winusb.c
+++ b/libusb/os/windows_winusb.c
@@ -41,9 +41,20 @@
#include "poll_windows.h"
#include "windows_winusb.h"
+#define HANDLE_VALID(h) (((h) != 0) && ((h) != INVALID_HANDLE_VALUE))
+
// The 2 macros below are used in conjunction with safe loops.
-#define LOOP_CHECK(fcall) { r=fcall; if (r != LIBUSB_SUCCESS) continue; }
-#define LOOP_BREAK(err) { r=err; continue; }
+#define LOOP_CHECK(fcall) \
+ { \
+ r = fcall; \
+ if (r != LIBUSB_SUCCESS) \
+ continue; \
+ }
+#define LOOP_BREAK(err) \
+ { \
+ r = err; \
+ continue; \
+ }
// Helper prototypes
static int windows_get_active_config_descriptor(struct libusb_device *dev, unsigned char *buffer, size_t len, int *host_endian);
@@ -99,37 +110,50 @@ static int composite_copy_transfer_data(int sub_api, struct usbi_transfer *itran
// Global variables
-uint64_t hires_frequency, hires_ticks_to_ps;
int windows_version = WINDOWS_UNDEFINED;
static char windows_version_str[128] = "Windows Undefined";
// Concurrency
static int concurrent_usage = -1;
-usbi_mutex_t autoclaim_lock;
+static usbi_mutex_t autoclaim_lock;
// API globals
-#define CHECK_WINUSBX_AVAILABLE(sub_api) do { if (sub_api == SUB_API_NOTSET) sub_api = priv->sub_api; \
- if (!WinUSBX[sub_api].initialized) return LIBUSB_ERROR_ACCESS; } while(0)
-static struct winusb_interface WinUSBX[SUB_API_MAX];
-const char* sub_api_name[SUB_API_MAX] = WINUSBX_DRV_NAMES;
-bool api_hid_available = false;
-#define CHECK_HID_AVAILABLE do { if (!api_hid_available) return LIBUSB_ERROR_ACCESS; } while (0)
+#define CHECK_WINUSBX_AVAILABLE(sub_api) \
+ do { \
+ if (sub_api == SUB_API_NOTSET) \
+ sub_api = priv->sub_api; \
+ if (!WinUSBX[sub_api].initialized) \
+ return LIBUSB_ERROR_ACCESS; \
+ } while(0)
-static inline BOOLEAN guid_eq(const GUID *guid1, const GUID *guid2) {
- if ((guid1 != NULL) && (guid2 != NULL)) {
+static struct winusb_interface WinUSBX[SUB_API_MAX];
+static const char *sub_api_name[SUB_API_MAX] = WINUSBX_DRV_NAMES;
+static bool api_hid_available = false;
+#define CHECK_HID_AVAILABLE \
+ do { \
+ if (!api_hid_available) \
+ return LIBUSB_ERROR_ACCESS; \
+ } while (0)
+
+static inline BOOLEAN guid_eq(const GUID *guid1, const GUID *guid2)
+{
+ if ((guid1 != NULL) && (guid2 != NULL))
return (memcmp(guid1, guid2, sizeof(GUID)) == 0);
- }
+
return false;
}
#if defined(ENABLE_LOGGING)
-static char* guid_to_string(const GUID* guid)
+static char *guid_to_string(const GUID *guid)
{
static char guid_string[MAX_GUID_STRING_LENGTH];
- if (guid == NULL) return NULL;
+ if (guid == NULL)
+ return NULL;
+
sprintf(guid_string, "{%08X-%04X-%04X-%02X%02X-%02X%02X%02X%02X%02X%02X}",
(unsigned int)guid->Data1, guid->Data2, guid->Data3,
guid->Data4[0], guid->Data4[1], guid->Data4[2], guid->Data4[3],
guid->Data4[4], guid->Data4[5], guid->Data4[6], guid->Data4[7]);
+
return guid_string;
}
#endif
@@ -138,38 +162,39 @@ static char* guid_to_string(const GUID* guid)
* Sanitize Microsoft's paths: convert to uppercase, add prefix and fix backslashes.
* Return an allocated sanitized string or NULL on error.
*/
-static char* sanitize_path(const char* path)
+static char *sanitize_path(const char *path)
{
const char root_prefix[] = "\\\\.\\";
size_t j, size, root_size;
- char* ret_path = NULL;
+ char *ret_path = NULL;
size_t add_root = 0;
if (path == NULL)
return NULL;
- size = safe_strlen(path)+1;
- root_size = sizeof(root_prefix)-1;
+ size = safe_strlen(path) + 1;
+ root_size = sizeof(root_prefix) - 1;
// Microsoft indiscriminately uses '\\?\', '\\.\', '##?#" or "##.#" for root prefixes.
- if (!((size > 3) && (((path[0] == '\\') && (path[1] == '\\') && (path[3] == '\\')) ||
- ((path[0] == '#') && (path[1] == '#') && (path[3] == '#'))))) {
+ if (!((size > 3) && (((path[0] == '\\') && (path[1] == '\\') && (path[3] == '\\'))
+ || ((path[0] == '#') && (path[1] == '#') && (path[3] == '#'))))) {
add_root = root_size;
size += add_root;
}
- if ((ret_path = (char*) calloc(size, 1)) == NULL)
+ ret_path = calloc(1, size);
+ if (ret_path == NULL)
return NULL;
safe_strcpy(&ret_path[add_root], size-add_root, path);
// Ensure consistency with root prefix
- for (j=0; j<root_size; j++)
+ for (j = 0; j < root_size; j++)
ret_path[j] = root_prefix[j];
// Same goes for '\' and '#' after the root prefix. Ensure '#' is used
- for(j=root_size; j<size; j++) {
- ret_path[j] = (char)toupper((int)ret_path[j]); // Fix case too
+ for (j = root_size; j < size; j++) {
+ ret_path[j] = (char)toupper((int)ret_path[j]); // Fix case too
if (ret_path[j] == '\\')
ret_path[j] = '#';
}
@@ -215,21 +240,20 @@ static int init_dlls(void)
* incremented index starting at zero) until all interfaces have been returned.
*/
static bool get_devinfo_data(struct libusb_context *ctx,
- HDEVINFO *dev_info, SP_DEVINFO_DATA *dev_info_data, const char* usb_class, unsigned _index)
+ HDEVINFO *dev_info, SP_DEVINFO_DATA *dev_info_data, const char *usb_class, unsigned _index)
{
if (_index <= 0) {
*dev_info = pSetupDiGetClassDevsA(NULL, usb_class, NULL, DIGCF_PRESENT|DIGCF_ALLCLASSES);
- if (*dev_info == INVALID_HANDLE_VALUE) {
+ if (*dev_info == INVALID_HANDLE_VALUE)
return false;
- }
}
dev_info_data->cbSize = sizeof(SP_DEVINFO_DATA);
if (!pSetupDiEnumDeviceInfo(*dev_info, _index, dev_info_data)) {
- if (GetLastError() != ERROR_NO_MORE_ITEMS) {
+ if (GetLastError() != ERROR_NO_MORE_ITEMS)
usbi_err(ctx, "Could not obtain device info data for index %u: %s",
_index, windows_error_str(0));
- }
+
pSetupDiDestroyDeviceInfoList(*dev_info);
*dev_info = INVALID_HANDLE_VALUE;
return false;
@@ -251,23 +275,22 @@ static bool get_devinfo_data(struct libusb_context *ctx,
* incremented index starting at zero) until all interfaces have been returned.
*/
static SP_DEVICE_INTERFACE_DETAIL_DATA_A *get_interface_details(struct libusb_context *ctx,
- HDEVINFO *dev_info, SP_DEVINFO_DATA *dev_info_data, const GUID* guid, unsigned _index)
+ HDEVINFO *dev_info, SP_DEVINFO_DATA *dev_info_data, const GUID *guid, unsigned _index)
{
SP_DEVICE_INTERFACE_DATA dev_interface_data;
SP_DEVICE_INTERFACE_DETAIL_DATA_A *dev_interface_details = NULL;
DWORD size;
- if (_index <= 0) {
+ if (_index <= 0)
*dev_info = pSetupDiGetClassDevsA(guid, NULL, NULL, DIGCF_PRESENT|DIGCF_DEVICEINTERFACE);
- }
if (dev_info_data != NULL) {
dev_info_data->cbSize = sizeof(SP_DEVINFO_DATA);
if (!pSetupDiEnumDeviceInfo(*dev_info, _index, dev_info_data)) {
- if (GetLastError() != ERROR_NO_MORE_ITEMS) {
+ if (GetLastError() != ERROR_NO_MORE_ITEMS)
usbi_err(ctx, "Could not obtain device info data for index %u: %s",
_index, windows_error_str(0));
- }
+
pSetupDiDestroyDeviceInfoList(*dev_info);
*dev_info = INVALID_HANDLE_VALUE;
return NULL;
@@ -276,10 +299,10 @@ static SP_DEVICE_INTERFACE_DETAIL_DATA_A *get_interface_details(struct libusb_co
dev_interface_data.cbSize = sizeof(SP_DEVICE_INTERFACE_DATA);
if (!pSetupDiEnumDeviceInterfaces(*dev_info, NULL, guid, _index, &dev_interface_data)) {
- if (GetLastError() != ERROR_NO_MORE_ITEMS) {
+ if (GetLastError() != ERROR_NO_MORE_ITEMS)
usbi_err(ctx, "Could not obtain interface data for index %u: %s",
_index, windows_error_str(0));
- }
+
pSetupDiDestroyDeviceInfoList(*dev_info);
*dev_info = INVALID_HANDLE_VALUE;
return NULL;
@@ -298,7 +321,8 @@ static SP_DEVICE_INTERFACE_DETAIL_DATA_A *get_interface_details(struct libusb_co
goto err_exit;
}
- if ((dev_interface_details = (SP_DEVICE_INTERFACE_DETAIL_DATA_A*) calloc(size, 1)) == NULL) {
+ dev_interface_details = calloc(1, size);
+ if (dev_interface_details == NULL) {
usbi_err(ctx, "could not allocate interface data for index %u.", _index);
goto err_exit;
}
@@ -320,35 +344,39 @@ err_exit:
/* For libusb0 filter */
static SP_DEVICE_INTERFACE_DETAIL_DATA_A *get_interface_details_filter(struct libusb_context *ctx,
- HDEVINFO *dev_info, SP_DEVINFO_DATA *dev_info_data, const GUID* guid, unsigned _index, char* filter_path){
+ HDEVINFO *dev_info, SP_DEVINFO_DATA *dev_info_data, const GUID *guid, unsigned _index, char *filter_path)
+{
SP_DEVICE_INTERFACE_DATA dev_interface_data;
SP_DEVICE_INTERFACE_DETAIL_DATA_A *dev_interface_details = NULL;
DWORD size;
- if (_index <= 0) {
+
+ if (_index <= 0)
*dev_info = pSetupDiGetClassDevsA(guid, NULL, NULL, DIGCF_PRESENT|DIGCF_DEVICEINTERFACE);
- }
+
if (dev_info_data != NULL) {
dev_info_data->cbSize = sizeof(SP_DEVINFO_DATA);
if (!pSetupDiEnumDeviceInfo(*dev_info, _index, dev_info_data)) {
- if (GetLastError() != ERROR_NO_MORE_ITEMS) {
+ if (GetLastError() != ERROR_NO_MORE_ITEMS)
usbi_err(ctx, "Could not obtain device info data for index %u: %s",
_index, windows_error_str(0));
- }
+
pSetupDiDestroyDeviceInfoList(*dev_info);
*dev_info = INVALID_HANDLE_VALUE;
return NULL;
}
}
+
dev_interface_data.cbSize = sizeof(SP_DEVICE_INTERFACE_DATA);
if (!pSetupDiEnumDeviceInterfaces(*dev_info, NULL, guid, _index, &dev_interface_data)) {
- if (GetLastError() != ERROR_NO_MORE_ITEMS) {
+ if (GetLastError() != ERROR_NO_MORE_ITEMS)
usbi_err(ctx, "Could not obtain interface data for index %u: %s",
_index, windows_error_str(0));
- }
+
pSetupDiDestroyDeviceInfoList(*dev_info);
*dev_info = INVALID_HANDLE_VALUE;
return NULL;
}
+
// Read interface data (dummy + actual) to access the device path
if (!pSetupDiGetDeviceInterfaceDetailA(*dev_info, &dev_interface_data, NULL, 0, &size, NULL)) {
// The dummy call should fail with ERROR_INSUFFICIENT_BUFFER
@@ -361,26 +389,29 @@ static SP_DEVICE_INTERFACE_DETAIL_DATA_A *get_interface_details_filter(struct li
usbi_err(ctx, "program assertion failed - http://msdn.microsoft.com/en-us/library/ms792901.aspx is wrong.");
goto err_exit;
}
- if ((dev_interface_details = malloc(size)) == NULL) {
+
+ dev_interface_details = calloc(1, size);
+ if (dev_interface_details == NULL) {
usbi_err(ctx, "could not allocate interface data for index %u.", _index);
goto err_exit;
}
+
dev_interface_details->cbSize = sizeof(SP_DEVICE_INTERFACE_DETAIL_DATA_A);
- if (!pSetupDiGetDeviceInterfaceDetailA(*dev_info, &dev_interface_data,
- dev_interface_details, size, &size, NULL)) {
+ if (!pSetupDiGetDeviceInterfaceDetailA(*dev_info, &dev_interface_data, dev_interface_details, size, &size, NULL))
usbi_err(ctx, "could not access interface data (actual) for index %u: %s",
_index, windows_error_str(0));
- }
+
// [trobinso] lookup the libusb0 symbolic index.
if (dev_interface_details) {
- HKEY hkey_device_interface=pSetupDiOpenDeviceInterfaceRegKey(*dev_info,&dev_interface_data,0,KEY_READ);
+ HKEY hkey_device_interface = pSetupDiOpenDeviceInterfaceRegKey(*dev_info, &dev_interface_data, 0, KEY_READ);
if (hkey_device_interface != INVALID_HANDLE_VALUE) {
- DWORD libusb0_symboliclink_index=0;
- DWORD value_length=sizeof(DWORD);
- DWORD value_type=0;
+ DWORD libusb0_symboliclink_index = 0;
+ DWORD value_length = sizeof(DWORD);
+ DWORD value_type = 0;
LONG status;
+
status = pRegQueryValueExW(hkey_device_interface, L"LUsb0", NULL, &value_type,
- (LPBYTE) &libusb0_symboliclink_index, &value_length);
+ (LPBYTE)&libusb0_symboliclink_index, &value_length);
if (status == ERROR_SUCCESS) {
if (libusb0_symboliclink_index < 256) {
// libusb0.sys is connected to this device instance.
@@ -394,11 +425,14 @@ static SP_DEVICE_INTERFACE_DETAIL_DATA_A *get_interface_details_filter(struct li
pRegCloseKey(hkey_device_interface);
}
}
+
return dev_interface_details;
+
err_exit:
pSetupDiDestroyDeviceInfoList(*dev_info);
*dev_info = INVALID_HANDLE_VALUE;
- return NULL;}
+ return NULL;
+}
/*
* Returns the session ID of a device's nth level ancestor
@@ -408,25 +442,27 @@ static unsigned long get_ancestor_session_id(DWORD devinst, unsigned level)
{
DWORD parent_devinst;
unsigned long session_id = 0;
- char* sanitized_path = NULL;
+ char *sanitized_path = NULL;
char path[MAX_PATH_LENGTH];
unsigned i;
- if (level < 1) return 0;
- for (i = 0; i<level; i++) {
- if (CM_Get_Parent(&parent_devinst, devinst, 0) != CR_SUCCESS) {
+ if (level < 1)
+ return 0;
+
+ for (i = 0; i < level; i++) {
+ if (CM_Get_Parent(&parent_devinst, devinst, 0) != CR_SUCCESS)
return 0;
- }
devinst = parent_devinst;
}
- if (CM_Get_Device_IDA(devinst, path, MAX_PATH_LENGTH, 0) != CR_SUCCESS) {
+
+ if (CM_Get_Device_IDA(devinst, path, MAX_PATH_LENGTH, 0) != CR_SUCCESS)
return 0;
- }
+
// TODO: (post hotplug): try without sanitizing
sanitized_path = sanitize_path(path);
- if (sanitized_path == NULL) {
+ if (sanitized_path == NULL)
return 0;
- }
+
session_id = htab_hash(sanitized_path);
safe_free(sanitized_path);
return session_id;
@@ -484,14 +520,14 @@ static int windows_assign_endpoints(struct libusb_device_handle *dev_handle, int
return LIBUSB_SUCCESS;
}
- priv->usb_interface[iface].endpoint = (uint8_t*) malloc(if_desc->bNumEndpoints);
+ priv->usb_interface[iface].endpoint = malloc(if_desc->bNumEndpoints);
if (priv->usb_interface[iface].endpoint == NULL) {
libusb_free_config_descriptor(conf_desc);
return LIBUSB_ERROR_NO_MEM;
}
priv->usb_interface[iface].nb_endpoints = if_desc->bNumEndpoints;
- for (i=0; i<if_desc->bNumEndpoints; i++) {
+ for (i = 0; i < if_desc->bNumEndpoints; i++) {
priv->usb_interface[iface].endpoint[i] = if_desc->endpoint[i].bEndpointAddress;
usbi_dbg("(re)assigned endpoint %02X to interface %d", priv->usb_interface[iface].endpoint[i], iface);
}
@@ -503,19 +539,24 @@ static int windows_assign_endpoints(struct libusb_device_handle *dev_handle, int
// Lookup for a match in the list of API driver names
// return -1 if not found, driver match number otherwise
-static int get_sub_api(char* driver, int api){
+static int get_sub_api(char *driver, int api)
+{
int i;
const char sep_str[2] = {LIST_SEPARATOR, 0};
char *tok, *tmp_str;
size_t len = safe_strlen(driver);
- if (len == 0) return SUB_API_NOTSET;
- tmp_str = (char*) calloc(len+1, 1);
- if (tmp_str == NULL) return SUB_API_NOTSET;
- memcpy(tmp_str, driver, len+1);
+ if (len == 0)
+ return SUB_API_NOTSET;
+
+ tmp_str = calloc(1, len + 1);
+ if (tmp_str == NULL)
+ return SUB_API_NOTSET;
+
+ memcpy(tmp_str, driver, len + 1);
tok = strtok(tmp_str, sep_str);
while (tok != NULL) {
- for (i=0; i<usb_api_backend[api].nb_driver_names; i++) {
+ for (i = 0; i < usb_api_backend[api].nb_driver_names; i++) {
if (safe_stricmp(tok, usb_api_backend[api].driver_name_list[i]) == 0) {
free(tmp_str);
return i;
@@ -523,7 +564,8 @@ static int get_sub_api(char* driver, int api){
}
tok = strtok(NULL, sep_str);
}
- free (tmp_str);
+
+ free(tmp_str);
return SUB_API_NOTSET;
}
@@ -548,16 +590,14 @@ static int auto_claim(struct libusb_transfer *transfer, int *interface_number, i
}
usbi_mutex_lock(&autoclaim_lock);
- if (current_interface < 0) // No serviceable interface was found
- {
- for (current_interface=0; current_interface<USB_MAXINTERFACES; current_interface++) {
+ if (current_interface < 0) { // No serviceable interface was found
+ for (current_interface = 0; current_interface < USB_MAXINTERFACES; current_interface++) {
// Must claim an interface of the same API type
- if ( (priv->usb_interface[current_interface].apib->id == api_type)
- && (libusb_claim_interface(transfer->dev_handle, current_interface) == LIBUSB_SUCCESS) ) {
+ if ((priv->usb_interface[current_interface].apib->id == api_type)
+ && (libusb_claim_interface(transfer->dev_handle, current_interface) == LIBUSB_SUCCESS)) {
usbi_dbg("auto-claimed interface %d for control request", current_interface);
- if (handle_priv->autoclaim_count[current_interface] != 0) {
+ if (handle_priv->autoclaim_count[current_interface] != 0)
usbi_warn(ctx, "program assertion failed - autoclaim_count was nonzero");
- }
handle_priv->autoclaim_count[current_interface]++;
break;
}
@@ -569,23 +609,21 @@ static int auto_claim(struct libusb_transfer *transfer, int *interface_number, i
} else {
// If we have a valid interface that was autoclaimed, we must increment
// its autoclaim count so that we can prevent an early release.
- if (handle_priv->autoclaim_count[current_interface] != 0) {
+ if (handle_priv->autoclaim_count[current_interface] != 0)
handle_priv->autoclaim_count[current_interface]++;
- }
}
usbi_mutex_unlock(&autoclaim_lock);
*interface_number = current_interface;
return r;
-
}
static void auto_release(struct usbi_transfer *itransfer)
{
- struct windows_transfer_priv *transfer_priv = (struct windows_transfer_priv*)usbi_transfer_get_os_priv(itransfer);
+ struct windows_transfer_priv *transfer_priv = usbi_transfer_get_os_priv(itransfer);
struct libusb_transfer *transfer = USBI_TRANSFER_TO_LIBUSB_TRANSFER(itransfer);
libusb_device_handle *dev_handle = transfer->dev_handle;
- struct windows_device_handle_priv* handle_priv = _device_handle_priv(dev_handle);
+ struct windows_device_handle_priv *handle_priv = _device_handle_priv(dev_handle);
int r;
usbi_mutex_lock(&autoclaim_lock);
@@ -593,12 +631,11 @@ static void auto_release(struct usbi_transfer *itransfer)
handle_priv->autoclaim_count[transfer_priv->interface_number]--;
if (handle_priv->autoclaim_count[transfer_priv->interface_number] == 0) {
r = libusb_release_interface(dev_handle, transfer_priv->interface_number);
- if (r == LIBUSB_SUCCESS) {
+ if (r == LIBUSB_SUCCESS)
usbi_dbg("auto-released interface %d", transfer_priv->interface_number);
- } else {
+ else
usbi_dbg("failed to auto-release interface %d (%s)",
transfer_priv->interface_number, libusb_error_name((enum libusb_error)r));
- }
}
}
usbi_mutex_unlock(&autoclaim_lock);
@@ -608,24 +645,25 @@ static void auto_release(struct usbi_transfer *itransfer)
static BOOL is_x64(void)
{
BOOL ret = FALSE;
+
// Detect if we're running a 32 or 64 bit system
if (sizeof(uintptr_t) < 8) {
DLL_LOAD_PREFIXED(Kernel32.dll, p, IsWow64Process, FALSE);
- if (pIsWow64Process != NULL) {
+ if (pIsWow64Process != NULL)
(*pIsWow64Process)(GetCurrentProcess(), &ret);
- }
} else {
ret = TRUE;
}
+
return ret;
}
static void get_windows_version(void)
{
OSVERSIONINFOEXA vi, vi2;
- const char* w = 0;
- const char* w64 = "32 bit";
- char* vptr;
+ const char *w = 0;
+ const char *w64 = "32 bit";
+ char *vptr;
size_t vlen;
unsigned major, minor;
ULONGLONG major_equal, minor_equal;
@@ -641,7 +679,6 @@ static void get_windows_version(void)
}
if (vi.dwPlatformId == VER_PLATFORM_WIN32_NT) {
-
if (vi.dwMajorVersion > 6 || (vi.dwMajorVersion == 6 && vi.dwMinorVersion >= 2)) {
// Starting with Windows 8.1 Preview, GetVersionEx() does no longer report the actual OS version
// See: http://msdn.microsoft.com/en-us/library/windows/desktop/dn302074.aspx
@@ -649,19 +686,24 @@ static void get_windows_version(void)
major_equal = VerSetConditionMask(0, VER_MAJORVERSION, VER_EQUAL);
for (major = vi.dwMajorVersion; major <= 9; major++) {
memset(&vi2, 0, sizeof(vi2));
- vi2.dwOSVersionInfoSize = sizeof(vi2); vi2.dwMajorVersion = major;
+ vi2.dwOSVersionInfoSize = sizeof(vi2);
+ vi2.dwMajorVersion = major;
if (!VerifyVersionInfoA(&vi2, VER_MAJORVERSION, major_equal))
continue;
+
if (vi.dwMajorVersion < major) {
- vi.dwMajorVersion = major; vi.dwMinorVersion = 0;
+ vi.dwMajorVersion = major;
+ vi.dwMinorVersion = 0;
}
minor_equal = VerSetConditionMask(0, VER_MINORVERSION, VER_EQUAL);
for (minor = vi.dwMinorVersion; minor <= 9; minor++) {
- memset(&vi2, 0, sizeof(vi2)); vi2.dwOSVersionInfoSize = sizeof(vi2);
+ memset(&vi2, 0, sizeof(vi2));
+ vi2.dwOSVersionInfoSize = sizeof(vi2);
vi2.dwMinorVersion = minor;
if (!VerifyVersionInfoA(&vi2, VER_MINORVERSION, minor_equal))
continue;
+
vi.dwMinorVersion = minor;
break;
}
@@ -674,22 +716,14 @@ static void get_windows_version(void)
ws = (vi.wProductType <= VER_NT_WORKSTATION);
windows_version = vi.dwMajorVersion << 4 | vi.dwMinorVersion;
switch (windows_version) {
- case 0x50: w = "2000";
- break;
- case 0x51: w = "XP";
- break;
- case 0x52: w = ("2003");
- break;
- case 0x60: w = (ws?"Vista":"2008");
- break;
- case 0x61: w = (ws?"7":"2008_R2");
- break;
- case 0x62: w = (ws?"8":"2012");
- break;
- case 0x63: w = (ws?"8.1":"2012_R2");
- break;
- case 0x64: w = (ws?"10":"2015");
- break;
+ case 0x50: w = "2000"; break;
+ case 0x51: w = "XP"; break;
+ case 0x52: w = ("2003"); break;
+ case 0x60: w = (ws ? "Vista" : "2008"); break;
+ case 0x61: w = (ws ? "7" : "2008_R2"); break;
+ case 0x62: w = (ws ? "8" : "2012"); break;
+ case 0x63: w = (ws ? "8.1": "2012_R2"); break;
+ case 0x64: w = (ws ? "10": "2015"); break;
default:
if (windows_version < 0x50)
windows_version = WINDOWS_UNSUPPORTED;
@@ -706,7 +740,7 @@ static void get_windows_version(void)
vptr = &windows_version_str[sizeof("Windows ") - 1];
vlen = sizeof(windows_version_str) - sizeof("Windows ") - 1;
if (!w)
- safe_sprintf(vptr, vlen, "%s %u.%u %s", (vi.dwPlatformId==VER_PLATFORM_WIN32_NT?"NT":"??"),
+ safe_sprintf(vptr, vlen, "%s %u.%u %s", (vi.dwPlatformId == VER_PLATFORM_WIN32_NT ? "NT" : "??"),
(unsigned int)vi.dwMajorVersion, (unsigned int)vi.dwMinorVersion, w64);
else if (vi.wServicePackMinor)
safe_sprintf(vptr, vlen, "%s SP%u.%u %s", w, vi.wServicePackMajor, vi.wServicePackMinor, w64);
@@ -727,7 +761,7 @@ static int windows_init(struct libusb_context *ctx)
{
int i, r = LIBUSB_ERROR_OTHER;
HANDLE semaphore;
- char sem_name[11+1+8]; // strlen(libusb_init)+'\0'+(32-bit hex PID)
+ char sem_name[11 + 1 + 8]; // strlen(libusb_init)+'\0'+(32-bit hex PID)
sprintf(sem_name, "libusb_init%08X", (unsigned int)GetCurrentProcessId());
semaphore = CreateSemaphoreA(NULL, 1, 1, sem_name);
@@ -746,7 +780,7 @@ static int windows_init(struct libusb_context *ctx)
// NB: concurrent usage supposes that init calls are equally balanced with
// exit calls. If init is called more than exit, we will not exit properly
- if ( ++concurrent_usage == 0 ) { // First init?
+ if (++concurrent_usage == 0) { // First init?
get_windows_version();
usbi_dbg(windows_version_str);
if (windows_version == WINDOWS_UNSUPPORTED) {
@@ -768,14 +802,12 @@ static int windows_init(struct libusb_context *ctx)
}
// Initialize the low level APIs (we don't care about errors at this stage)
- for (i=0; i<USB_API_MAX; i++) {
+ for (i = 0; i < USB_API_MAX; i++)
usb_api_backend[i].init(SUB_API_NOTSET, ctx);
- }
r = windows_common_init(ctx);
- if (r) {
+ if (r)
goto init_exit;
- }
}
// At this stage, either we went through full init successfully, or didn't need to
r = LIBUSB_SUCCESS;
@@ -789,7 +821,7 @@ init_exit: // Holds semaphore here.
if (r != LIBUSB_SUCCESS)
--concurrent_usage; // Not expected to call libusb_exit if we failed.
- ReleaseSemaphore(semaphore, 1, NULL); // increase count back to 1
+ ReleaseSemaphore(semaphore, 1, NULL); // increase count back to 1
CloseHandle(semaphore);
return r;
}
@@ -816,22 +848,24 @@ static int force_hcd_device_descriptor(struct libusb_device *dev)
usbi_err(ctx, "program assertion failed - HCD hub has no parent");
return LIBUSB_ERROR_NO_DEVICE;
}
+
parent_priv = _device_priv(priv->parent_dev);
if (sscanf(parent_priv->path, "\\\\.\\PCI#VEN_%04x&DEV_%04x%*s", &vid, &pid) == 2) {
priv->dev_descriptor.idVendor = (uint16_t)vid;
priv->dev_descriptor.idProduct = (uint16_t)pid;
} else {
usbi_warn(ctx, "could not infer VID/PID of HCD hub from '%s'", parent_priv->path);
- priv->dev_descriptor.idVendor = 0x1d6b; // Linux Foundation root hub
+ priv->dev_descriptor.idVendor = 0x1d6b; // Linux Foundation root hub
priv->dev_descriptor.idProduct = 1;
}
+
return LIBUSB_SUCCESS;
}
/*
* fetch and cache all the config descriptors through I/O
*/
-static int cache_config_descriptors(struct libusb_device *dev, HANDLE hub_handle, char* device_id)
+static int cache_config_descriptors(struct libusb_device *dev, HANDLE hub_handle, char *device_id)
{
DWORD size, ret_size;
struct libusb_context *ctx = DEVICE_CTX(dev);
@@ -839,21 +873,21 @@ static int cache_config_descriptors(struct libusb_device *dev, HANDLE hub_handle
int r;
uint8_t i;
- USB_CONFIGURATION_DESCRIPTOR_SHORT cd_buf_short; // dummy request
- PUSB_DESCRIPTOR_REQUEST cd_buf_actual = NULL; // actual request
+ USB_CONFIGURATION_DESCRIPTOR_SHORT cd_buf_short; // dummy request
+ PUSB_DESCRIPTOR_REQUEST cd_buf_actual = NULL; // actual request
PUSB_CONFIGURATION_DESCRIPTOR cd_data = NULL;
if (dev->num_configurations == 0)
return LIBUSB_ERROR_INVALID_PARAM;
- priv->config_descriptor = (unsigned char**) calloc(dev->num_configurations, sizeof(unsigned char*));
+ priv->config_descriptor = calloc(dev->num_configurations, sizeof(unsigned char *));
if (priv->config_descriptor == NULL)
return LIBUSB_ERROR_NO_MEM;
- for (i=0; i<dev->num_configurations; i++)
+
+ for (i = 0; i < dev->num_configurations; i++)
priv->config_descriptor[i] = NULL;
- for (i=0, r=LIBUSB_SUCCESS; ; i++)
- {
+ for (i = 0, r = LIBUSB_SUCCESS; ; i++) {
// safe loop: release all dynamic resources
safe_free(cd_buf_actual);
@@ -886,11 +920,11 @@ static int cache_config_descriptors(struct libusb_device *dev, HANDLE hub_handle
}
size = sizeof(USB_DESCRIPTOR_REQUEST) + cd_buf_short.data.wTotalLength;
- if ((cd_buf_actual = (PUSB_DESCRIPTOR_REQUEST) calloc(1, size)) == NULL) {
+ cd_buf_actual = calloc(1, size);
+ if (cd_buf_actual == NULL) {
usbi_err(ctx, "could not allocate configuration descriptor buffer for '%s'.", device_id);
LOOP_BREAK(LIBUSB_ERROR_NO_MEM);
}
- memset(cd_buf_actual, 0, size);
// Actual call
cd_buf_actual->ConnectionIndex = (ULONG)priv->port;
@@ -906,7 +940,7 @@ static int cache_config_descriptors(struct libusb_device *dev, HANDLE hub_handle
LOOP_BREAK(LIBUSB_ERROR_IO);
}
- cd_data = (PUSB_CONFIGURATION_DESCRIPTOR)((UCHAR*)cd_buf_actual+sizeof(USB_DESCRIPTOR_REQUEST));
+ cd_data = (PUSB_CONFIGURATION_DESCRIPTOR)((UCHAR *)cd_buf_actual + sizeof(USB_DESCRIPTOR_REQUEST));
if ((size != ret_size) || (cd_data->wTotalLength != cd_buf_short.data.wTotalLength)) {
usbi_err(ctx, "unexpected configuration descriptor size (actual) for '%s'.", device_id);
@@ -922,7 +956,7 @@ static int cache_config_descriptors(struct libusb_device *dev, HANDLE hub_handle
i, cd_data->bConfigurationValue, cd_data->wTotalLength);
// Cache the descriptor
- priv->config_descriptor[i] = (unsigned char*) malloc(cd_data->wTotalLength);
+ priv->config_descriptor[i] = malloc(cd_data->wTotalLength);
if (priv->config_descriptor[i] == NULL)
LOOP_BREAK(LIBUSB_ERROR_NO_MEM);
memcpy(priv->config_descriptor[i], cd_data, cd_data->wTotalLength);
@@ -933,8 +967,8 @@ static int cache_config_descriptors(struct libusb_device *dev, HANDLE hub_handle
/*
* Populate a libusb device structure
*/
-static int init_device(struct libusb_device* dev, struct libusb_device* parent_dev,
- uint8_t port_number, char* device_id, DWORD devinst)
+static int init_device(struct libusb_device *dev, struct libusb_device *parent_dev,
+ uint8_t port_number, char *device_id, DWORD devinst)
{
HANDLE handle;
DWORD size;
@@ -942,13 +976,13 @@ static int init_device(struct libusb_device* dev, struct libusb_device* parent_d
USB_NODE_CONNECTION_INFORMATION_EX_V2 conn_info_v2;
struct windows_device_priv *priv, *parent_priv;
struct libusb_context *ctx;
- struct libusb_device* tmp_dev;
+ struct libusb_device *tmp_dev;
unsigned long tmp_id;
unsigned i;
- if ((dev == NULL) || (parent_dev == NULL)) {
+ if ((dev == NULL) || (parent_dev == NULL))
return LIBUSB_ERROR_NOT_FOUND;
- }
+
ctx = DEVICE_CTX(dev);
priv = _device_priv(dev);
parent_priv = _device_priv(parent_dev);
@@ -960,24 +994,31 @@ static int init_device(struct libusb_device* dev, struct libusb_device* parent_d
// It is possible for the parent hub not to have been initialized yet
// If that's the case, lookup the ancestors to set the bus number
if (parent_dev->bus_number == 0) {
- for (i=2; ; i++) {
+ for (i = 2; ; i++) {
tmp_id = get_ancestor_session_id(devinst, i);
- if (tmp_id == 0) break;
+ if (tmp_id == 0)
+ break;
+
tmp_dev = usbi_get_device_by_session_id(ctx, tmp_id);
- if (tmp_dev == NULL) continue;
+ if (tmp_dev == NULL)
+ continue;
+
if (tmp_dev->bus_number != 0) {
usbi_dbg("got bus number from ancestor #%u", i);
parent_dev->bus_number = tmp_dev->bus_number;
libusb_unref_device(tmp_dev);
break;
}
+
libusb_unref_device(tmp_dev);
}
}
+
if (parent_dev->bus_number == 0) {
usbi_err(ctx, "program assertion failed: unable to find ancestor bus number for '%s'", device_id);
return LIBUSB_ERROR_NOT_FOUND;
}
+
dev->bus_number = parent_dev->bus_number;
priv->port = port_number;
dev->port_number = port_number;
@@ -986,17 +1027,18 @@ static int init_device(struct libusb_device* dev, struct libusb_device* parent_d
dev->parent_dev = parent_dev;
// If the device address is already set, we can stop here
- if (dev->device_address != 0) {
+ if (dev->device_address != 0)
return LIBUSB_SUCCESS;
- }
+
memset(&conn_info, 0, sizeof(conn_info));
- if (priv->depth != 0) { // Not a HCD hub
+ if (priv->depth != 0) { // Not a HCD hub
handle = CreateFileA(parent_priv->path, GENERIC_WRITE, FILE_SHARE_WRITE, NULL, OPEN_EXISTING,
FILE_FLAG_OVERLAPPED, NULL);
if (handle == INVALID_HANDLE_VALUE) {
usbi_warn(ctx, "could not open hub %s: %s", parent_priv->path, windows_error_str(0));
return LIBUSB_ERROR_ACCESS;
}
+
size = sizeof(conn_info);
conn_info.ConnectionIndex = (ULONG)port_number;
// coverity[tainted_data_argument]
@@ -1007,15 +1049,18 @@ static int init_device(struct libusb_device* dev, struct libusb_device* parent_d
safe_closehandle(handle);
return LIBUSB_ERROR_NO_DEVICE;
}
+
if (conn_info.ConnectionStatus == NoDeviceConnected) {
usbi_err(ctx, "device '%s' is no longer connected!", device_id);
safe_closehandle(handle);
return LIBUSB_ERROR_NO_DEVICE;
}
+
memcpy(&priv->dev_descriptor, &(conn_info.DeviceDescriptor), sizeof(USB_DEVICE_DESCRIPTOR));
dev->num_configurations = priv->dev_descriptor.bNumConfigurations;
priv->active_config = conn_info.CurrentConfigurationValue;
usbi_dbg("found %u configurations (active conf: %u)", dev->num_configurations, priv->active_config);
+
// If we can't read the config descriptors, just set the number of confs to zero
if (cache_config_descriptors(dev, handle, device_id) != LIBUSB_SUCCESS) {
dev->num_configurations = 0;
@@ -1040,13 +1085,13 @@ static int init_device(struct libusb_device* dev, struct libusb_device* parent_d
safe_closehandle(handle);
- if (conn_info.DeviceAddress > UINT8_MAX) {
+ if (conn_info.DeviceAddress > UINT8_MAX)
usbi_err(ctx, "program assertion failed: device address overflow");
- }
+
dev->device_address = (uint8_t)conn_info.DeviceAddress + 1;
- if (dev->device_address == 1) {
+ if (dev->device_address == 1)
usbi_err(ctx, "program assertion failed: device address collision with root hub");
- }
+
switch (conn_info.Speed) {
case 0: dev->speed = LIBUSB_SPEED_LOW; break;
case 1: dev->speed = LIBUSB_SPEED_FULL; break;
@@ -1057,7 +1102,7 @@ static int init_device(struct libusb_device* dev, struct libusb_device* parent_d
break;
}
} else {
- dev->device_address = 1; // root hubs are set to use device number 1
+ dev->device_address = 1; // root hubs are set to use device number 1
force_hcd_device_descriptor(dev);
}
@@ -1085,38 +1130,37 @@ static void get_api_type(struct libusb_context *ctx, HDEVINFO *dev_info,
*api = USB_API_UNSUPPORTED;
*sub_api = SUB_API_NOTSET;
+
// Check the service & filter names to know the API we should use
- for (k=0; k<3; k++) {
+ for (k = 0; k < 3; k++) {
if (pSetupDiGetDeviceRegistryPropertyA(*dev_info, dev_info_data, lookup[k].reg_prop,
- &reg_type, (BYTE*)lookup[k].list, MAX_KEY_LENGTH, &size)) {
+ &reg_type, (BYTE *)lookup[k].list, MAX_KEY_LENGTH, &size)) {
// Turn the REG_SZ SPDRP_SERVICE into REG_MULTI_SZ
- if (lookup[k].reg_prop == SPDRP_SERVICE) {
- // our buffers are MAX_KEY_LENGTH+1 so we can overflow if needed
- lookup[k].list[safe_strlen(lookup[k].list)+1] = 0;
- }
+ if (lookup[k].reg_prop == SPDRP_SERVICE)
+ // our buffers are MAX_KEY_LENGTH + 1 so we can overflow if needed
+ lookup[k].list[safe_strlen(lookup[k].list) + 1] = 0;
+
// MULTI_SZ is a pain to work with. Turn it into something much more manageable
// NB: none of the driver names we check against contain LIST_SEPARATOR,
// (currently ';'), so even if an unsuported one does, it's not an issue
- for (l=0; (lookup[k].list[l] != 0) || (lookup[k].list[l+1] != 0); l++) {
- if (lookup[k].list[l] == 0) {
+ for (l = 0; (lookup[k].list[l] != 0) || (lookup[k].list[l + 1] != 0); l++) {
+ if (lookup[k].list[l] == 0)
lookup[k].list[l] = LIST_SEPARATOR;
- }
}
usbi_dbg("%s(s): %s", lookup[k].designation, lookup[k].list);
} else {
- if (GetLastError() != ERROR_INVALID_DATA) {
+ if (GetLastError() != ERROR_INVALID_DATA)
usbi_dbg("could not access %s: %s", lookup[k].designation, windows_error_str(0));
- }
lookup[k].list[0] = 0;
}
}
- for (i=1; i<USB_API_MAX; i++) {
- for (k=0; k<3; k++) {
+ for (i = 1; i < USB_API_MAX; i++) {
+ for (k = 0; k < 3; k++) {
j = get_sub_api(lookup[k].list, i);
if (j >= 0) {
- usbi_dbg("matched %s name against %s",
- lookup[k].designation, (i!=USB_API_WINUSBX)?usb_api_backend[i].designation:sub_api_name[j]);
+ usbi_dbg("matched %s name against %s", lookup[k].designation,
+ (i != USB_API_WINUSBX) ? usb_api_backend[i].designation : sub_api_name[j]);
*api = i;
*sub_api = j;
return;
@@ -1125,8 +1169,8 @@ static void get_api_type(struct libusb_context *ctx, HDEVINFO *dev_info,
}
}
-static int set_composite_interface(struct libusb_context* ctx, struct libusb_device* dev,
- char* dev_interface_path, char* device_id, int api, int sub_api)
+static int set_composite_interface(struct libusb_context *ctx, struct libusb_device *dev,
+ char *dev_interface_path, char *device_id, int api, int sub_api)
{
unsigned i;
struct windows_device_priv *priv = _device_priv(dev);
@@ -1141,19 +1185,18 @@ static int set_composite_interface(struct libusb_context* ctx, struct libusb_dev
// devices will have only MI_00 & MI_03 for instance), we retrieve the actual
// interface number from the path's MI value
interface_number = 0;
- for (i=0; device_id[i] != 0; ) {
- if ( (device_id[i++] == 'M') && (device_id[i++] == 'I')
- && (device_id[i++] == '_') ) {
- interface_number = (device_id[i++] - '0')*10;
+ for (i = 0; device_id[i] != 0; ) {
+ if ((device_id[i++] == 'M') && (device_id[i++] == 'I')
+ && (device_id[i++] == '_')) {
+ interface_number = (device_id[i++] - '0') * 10;
interface_number += device_id[i] - '0';
break;
}
}
- if (device_id[i] == 0) {
+ if (device_id[i] == 0)
usbi_warn(ctx, "failure to read interface number for %s. Using default value %d",
device_id, interface_number);
- }
if (priv->usb_interface[interface_number].path != NULL) {
if (api == USB_API_HID) {
@@ -1171,7 +1214,7 @@ static int set_composite_interface(struct libusb_context* ctx, struct libusb_dev
priv->usb_interface[interface_number].apib = &usb_api_backend[api];
priv->usb_interface[interface_number].sub_api = sub_api;
if ((api == USB_API_HID) && (priv->hid == NULL)) {
- priv->hid = (struct hid_device_priv*) calloc(1, sizeof(struct hid_device_priv));
+ priv->hid = calloc(1, sizeof(struct hid_device_priv));
if (priv->hid == NULL)
return LIBUSB_ERROR_NO_MEM;
}
@@ -1179,8 +1222,8 @@ static int set_composite_interface(struct libusb_context* ctx, struct libusb_dev
return LIBUSB_SUCCESS;
}
-static int set_hid_interface(struct libusb_context* ctx, struct libusb_device* dev,
- char* dev_interface_path)
+static int set_hid_interface(struct libusb_context *ctx, struct libusb_device *dev,
+ char *dev_interface_path)
{
int i;
struct windows_device_priv *priv = _device_priv(dev);
@@ -1188,12 +1231,12 @@ static int set_hid_interface(struct libusb_context* ctx, struct libusb_device* d
if (priv->hid == NULL) {
usbi_err(ctx, "program assertion failed: parent is not HID");
return LIBUSB_ERROR_NO_DEVICE;
- }
- if (priv->hid->nb_interfaces == USB_MAXINTERFACES) {
+ } else if (priv->hid->nb_interfaces == USB_MAXINTERFACES) {
usbi_err(ctx, "program assertion failed: max USB interfaces reached for HID device");
return LIBUSB_ERROR_NO_DEVICE;
}
- for (i=0; i<priv->hid->nb_interfaces; i++) {
+
+ for (i = 0; i < priv->hid->nb_interfaces; i++) {
if (safe_strcmp(priv->usb_interface[i].path, dev_interface_path) == 0) {
usbi_dbg("interface[%d] already set to %s", i, dev_interface_path);
return LIBUSB_SUCCESS;
@@ -1214,12 +1257,12 @@ static int windows_get_device_list(struct libusb_context *ctx, struct discovered
{
struct discovered_devs *discdevs;
HDEVINFO dev_info = { 0 };
- const char* usb_class[] = {"USB", "NUSB3", "IUSB3"};
+ const char *usb_class[] = {"USB", "NUSB3", "IUSB3"};
SP_DEVINFO_DATA dev_info_data = { 0 };
SP_DEVICE_INTERFACE_DETAIL_DATA_A *dev_interface_details = NULL;
GUID hid_guid;
#define MAX_ENUM_GUIDS 64
- const GUID* guid[MAX_ENUM_GUIDS];
+ const GUID *guid[MAX_ENUM_GUIDS];
#define HCD_PASS 0
#define HUB_PASS 1
#define GEN_PASS 2
@@ -1233,16 +1276,16 @@ static int windows_get_device_list(struct libusb_context *ctx, struct discovered
char strbuf[MAX_PATH_LENGTH];
struct libusb_device *dev, *parent_dev;
struct windows_device_priv *priv, *parent_priv;
- char* dev_interface_path = NULL;
- char* dev_id_path = NULL;
+ char *dev_interface_path = NULL;
+ char *dev_id_path = NULL;
unsigned long session_id;
DWORD size, reg_type, port_nr, install_state;
HKEY key;
WCHAR guid_string_w[MAX_GUID_STRING_LENGTH];
- GUID* if_guid;
+ GUID *if_guid;
LONG s;
// Keep a list of newly allocated devs to unref
- libusb_device** unref_list;
+ libusb_device **unref_list;
unsigned int unref_size = 64;
unsigned int unref_cur = 0;
@@ -1261,19 +1304,18 @@ static int windows_get_device_list(struct libusb_context *ctx, struct discovered
guid[DEV_PASS] = &GUID_DEVINTERFACE_USB_DEVICE;
HidD_GetHidGuid(&hid_guid);
guid[HID_PASS] = &hid_guid;
- nb_guids = HID_PASS+1;
+ nb_guids = HID_PASS + 1;
- unref_list = (libusb_device**) calloc(unref_size, sizeof(libusb_device*));
- if (unref_list == NULL) {
+ unref_list = calloc(unref_size, sizeof(libusb_device *));
+ if (unref_list == NULL)
return LIBUSB_ERROR_NO_MEM;
- }
for (pass = 0; ((pass < nb_guids) && (r == LIBUSB_SUCCESS)); pass++) {
//#define ENUM_DEBUG
#ifdef ENUM_DEBUG
const char *passname[] = { "HCD", "HUB", "GEN", "DEV", "HID", "EXT" };
- usbi_dbg("#### PROCESSING %ss %s", passname[(pass<=HID_PASS)?pass:HID_PASS+1],
- (pass!=GEN_PASS)?guid_to_string(guid[pass]):"");
+ usbi_dbg("#### PROCESSING %ss %s", passname[(pass <= HID_PASS) ? pass : (HID_PASS + 1)],
+ (pass != GEN_PASS) ? guid_to_string(guid[pass]) : "");
#endif
for (i = 0; ; i++) {
// safe loop: free up any (unprotected) dynamic resource
@@ -1285,24 +1327,24 @@ static int windows_get_device_list(struct libusb_context *ctx, struct discovered
dev = parent_dev = NULL;
// Safe loop: end of loop conditions
- if (r != LIBUSB_SUCCESS) {
+ if (r != LIBUSB_SUCCESS)
break;
- }
+
if ((pass == HCD_PASS) && (i == UINT8_MAX)) {
usbi_warn(ctx, "program assertion failed - found more than %d buses, skipping the rest.", UINT8_MAX);
break;
}
+
if (pass != GEN_PASS) {
// Except for GEN, all passes deal with device interfaces
dev_interface_details = get_interface_details(ctx, &dev_info, &dev_info_data, guid[pass], i);
- if (dev_interface_details == NULL) {
+ if (dev_interface_details == NULL)
break;
- } else {
- dev_interface_path = sanitize_path(dev_interface_details->DevicePath);
- if (dev_interface_path == NULL) {
- usbi_warn(ctx, "could not sanitize device interface path for '%s'", dev_interface_details->DevicePath);
- continue;
- }
+
+ dev_interface_path = sanitize_path(dev_interface_details->DevicePath);
+ if (dev_interface_path == NULL) {
+ usbi_warn(ctx, "could not sanitize device interface path for '%s'", dev_interface_details->DevicePath);
+ continue;
}
} else {
// Workaround for a Nec/Renesas USB 3.0 driver bug where root hubs are
@@ -1324,6 +1366,7 @@ static int windows_get_device_list(struct libusb_context *ctx, struct discovered
(unsigned int)dev_info_data.DevInst);
continue;
}
+
dev_id_path = sanitize_path(path);
if (dev_id_path == NULL) {
usbi_warn(ctx, "could not sanitize device id path for devinst %X, skipping",
@@ -1337,9 +1380,8 @@ static int windows_get_device_list(struct libusb_context *ctx, struct discovered
// The SPDRP_ADDRESS for USB devices is the device port number on the hub
port_nr = 0;
if ((pass >= HUB_PASS) && (pass <= GEN_PASS)) {
- if ( (!pSetupDiGetDeviceRegistryPropertyA(dev_info, &dev_info_data, SPDRP_ADDRESS,
- &reg_type, (BYTE*)&port_nr, 4, &size))
- || (size != 4) ) {
+ if ((!pSetupDiGetDeviceRegistryPropertyA(dev_info, &dev_info_data, SPDRP_ADDRESS,
+ &reg_type, (BYTE *)&port_nr, 4, &size)) || (size != 4)) {
usbi_warn(ctx, "could not retrieve port number for device '%s', skipping: %s",
dev_id_path, windows_error_str(0));
continue;
@@ -1356,7 +1398,7 @@ static int windows_get_device_list(struct libusb_context *ctx, struct discovered
// We use the GEN pass to detect driverless devices...
size = sizeof(strbuf);
if (!pSetupDiGetDeviceRegistryPropertyA(dev_info, &dev_info_data, SPDRP_DRIVER,
- &reg_type, (BYTE*)strbuf, size, &size)) {
+ &reg_type, (BYTE *)strbuf, size, &size)) {
usbi_info(ctx, "The following device has no driver: '%s'", dev_id_path);
usbi_info(ctx, "libusb will not be able to access it.");
}
@@ -1365,7 +1407,7 @@ static int windows_get_device_list(struct libusb_context *ctx, struct discovered
if (key != INVALID_HANDLE_VALUE) {
size = sizeof(guid_string_w);
s = pRegQueryValueExW(key, L"DeviceInterfaceGUIDs", NULL, &reg_type,
- (BYTE*)guid_string_w, &size);
+ (BYTE *)guid_string_w, &size);
pRegCloseKey(key);
if (s == ERROR_SUCCESS) {
if (nb_guids >= MAX_ENUM_GUIDS) {
@@ -1373,7 +1415,7 @@ static int windows_get_device_list(struct libusb_context *ctx, struct discovered
usbi_err(ctx, "program assertion failed: too many GUIDs");
LOOP_BREAK(LIBUSB_ERROR_OVERFLOW);
}
- if_guid = (GUID*) calloc(1, sizeof(GUID));
+ if_guid = calloc(1, sizeof(GUID));
if (if_guid == NULL) {
usbi_err(ctx, "could not calloc for if_guid: not enough memory");
LOOP_BREAK(LIBUSB_ERROR_NO_MEM);
@@ -1389,9 +1431,8 @@ static int windows_get_device_list(struct libusb_context *ctx, struct discovered
break;
default:
// Get the API type (after checking that the driver installation is OK)
- if ( (!pSetupDiGetDeviceRegistryPropertyA(dev_info, &dev_info_data, SPDRP_INSTALL_STATE,
- &reg_type, (BYTE*)&install_state, 4, &size))
- || (size != 4) ){
+ if ((!pSetupDiGetDeviceRegistryPropertyA(dev_info, &dev_info_data, SPDRP_INSTALL_STATE,
+ &reg_type, (BYTE *)&install_state, 4, &size)) || (size != 4)) {
usbi_warn(ctx, "could not detect installation state of driver for '%s': %s",
dev_id_path, windows_error_str(0));
} else if (install_state != 0) {
@@ -1414,21 +1455,24 @@ static int windows_get_device_list(struct libusb_context *ctx, struct discovered
parent_dev = NULL;
for (ancestor = 1; parent_dev == NULL; ancestor++) {
session_id = get_ancestor_session_id(dev_info_data.DevInst, ancestor);
- if (session_id == 0) {
+ if (session_id == 0)
break;
- }
+
parent_dev = usbi_get_device_by_session_id(ctx, session_id);
}
+
if (parent_dev == NULL) {
usbi_dbg("unlisted ancestor for '%s' (non USB HID, newly connected, etc.) - ignoring", dev_id_path);
continue;
}
+
parent_priv = _device_priv(parent_dev);
// virtual USB devices are also listed during GEN - don't process these yet
- if ( (pass == GEN_PASS) && (parent_priv->apib->id != USB_API_HUB) ) {
+ if ((pass == GEN_PASS) && (parent_priv->apib->id != USB_API_HUB)) {
libusb_unref_device(parent_dev);
continue;
}
+
break;
}
@@ -1444,20 +1488,23 @@ static int windows_get_device_list(struct libusb_context *ctx, struct discovered
" - ignoring", dev_id_path);
continue;
}
+
usbi_dbg("allocating new device for session [%lX]", session_id);
- if ((dev = usbi_alloc_device(ctx, session_id)) == NULL) {
+ dev = usbi_alloc_device(ctx, session_id);
+ if (dev == NULL)
LOOP_BREAK(LIBUSB_ERROR_NO_MEM);
- }
+
windows_device_priv_init(dev);
} else {
usbi_dbg("found existing device for session [%lX] (%u.%u)",
session_id, dev->bus_number, dev->device_address);
}
+
// Keep track of devices that need unref
unref_list[unref_cur++] = dev;
if (unref_cur >= unref_size) {
unref_size += 64;
- unref_list = usbi_reallocf(unref_list, unref_size*sizeof(libusb_device*));
+ unref_list = usbi_reallocf(unref_list, unref_size * sizeof(libusb_device *));
if (unref_list == NULL) {
usbi_err(ctx, "could not realloc list for unref - aborting.");
LOOP_BREAK(LIBUSB_ERROR_NO_MEM);
@@ -1469,13 +1516,14 @@ static int windows_get_device_list(struct libusb_context *ctx, struct discovered
// Setup device
switch (pass) {
case HCD_PASS:
- dev->bus_number = (uint8_t)(i + 1); // bus 0 is reserved for disconnected
+ dev->bus_number = (uint8_t)(i + 1); // bus 0 is reserved for disconnected
dev->device_address = 0;
dev->num_configurations = 0;
priv->apib = &usb_api_backend[USB_API_HUB];
priv->sub_api = SUB_API_NOTSET;
- priv->depth = UINT8_MAX; // Overflow to 0 for HCD Hubs
- priv->path = dev_interface_path; dev_interface_path = NULL;
+ priv->depth = UINT8_MAX; // Overflow to 0 for HCD Hubs
+ priv->path = dev_interface_path;
+ dev_interface_path = NULL;
break;
case HUB_PASS:
case DEV_PASS:
@@ -1483,7 +1531,8 @@ static int windows_get_device_list(struct libusb_context *ctx, struct discovered
if (priv->path != NULL)
break;
// Take care of API initialization
- priv->path = dev_interface_path; dev_interface_path = NULL;
+ priv->path = dev_interface_path;
+ dev_interface_path = NULL;
priv->apib = &usb_api_backend[api];
priv->sub_api = sub_api;
switch(api) {
@@ -1492,24 +1541,23 @@ static int windows_get_device_list(struct libusb_context *ctx, struct discovered
break;
case USB_API_HID:
priv->hid = calloc(1, sizeof(struct hid_device_priv));
- if (priv->hid == NULL) {
+ if (priv->hid == NULL)
LOOP_BREAK(LIBUSB_ERROR_NO_MEM);
- }
+
priv->hid->nb_interfaces = 0;
break;
default:
// For other devices, the first interface is the same as the device
- priv->usb_interface[0].path = (char*) calloc(safe_strlen(priv->path)+1, 1);
- if (priv->usb_interface[0].path != NULL) {
- safe_strcpy(priv->usb_interface[0].path, safe_strlen(priv->path)+1, priv->path);
- } else {
+ priv->usb_interface[0].path = calloc(1, safe_strlen(priv->path));
+ if (priv->usb_interface[0].path != NULL)
+ safe_strcpy(priv->usb_interface[0].path, safe_strlen(priv->path) + 1, priv->path);
+ else
usbi_warn(ctx, "could not duplicate interface path '%s'", priv->path);
- }
// The following is needed if we want API calls to work for both simple
// and composite devices.
- for(j=0; j<USB_MAXINTERFACES; j++) {
+ for(j = 0; j < USB_MAXINTERFACES; j++)
priv->usb_interface[j].apib = &usb_api_backend[api];
- }
+
break;
}
break;
@@ -1518,9 +1566,9 @@ static int windows_get_device_list(struct libusb_context *ctx, struct discovered
if (r == LIBUSB_SUCCESS) {
// Append device to the list of discovered devices
discdevs = discovered_devs_append(*_discdevs, dev);
- if (!discdevs) {
+ if (!discdevs)
LOOP_BREAK(LIBUSB_ERROR_NO_MEM);
- }
+
*_discdevs = discdevs;
} else if (r == LIBUSB_ERROR_NO_DEVICE) {
// This can occur if the device was disconnected but Windows hasn't
@@ -1528,11 +1576,13 @@ static int windows_get_device_list(struct libusb_context *ctx, struct discovered
r = LIBUSB_SUCCESS;
}
break;
- default: // HID_PASS and later
+ default: // HID_PASS and later
if (parent_priv->apib->id == USB_API_HID) {
usbi_dbg("setting HID interface for [%lX]:", parent_dev->session_data);
r = set_hid_interface(ctx, parent_dev, dev_interface_path);
- if (r != LIBUSB_SUCCESS) LOOP_BREAK(r);
+ if (r != LIBUSB_SUCCESS)
+ LOOP_BREAK(r);
+
dev_interface_path = NULL;
} else if (parent_priv->apib->id == USB_API_COMPOSITE) {
usbi_dbg("setting composite interface for [%lX]:", parent_dev->session_data);
@@ -1555,15 +1605,13 @@ static int windows_get_device_list(struct libusb_context *ctx, struct discovered
}
// Free any additional GUIDs
- for (pass = HID_PASS+1; pass < nb_guids; pass++) {
+ for (pass = HID_PASS + 1; pass < nb_guids; pass++)
safe_free(guid[pass]);
- }
// Unref newly allocated devs
if (unref_list != NULL) {
- for (i=0; i<unref_cur; i++) {
+ for (i = 0; i < unref_cur; i++)
safe_unref_device(unref_list[i]);
- }
free(unref_list);
}
@@ -1577,13 +1625,12 @@ static void windows_exit(void)
{
int i;
HANDLE semaphore;
- char sem_name[11+1+8]; // strlen(libusb_init)+'\0'+(32-bit hex PID)
+ char sem_name[11 + 1 + 8]; // strlen(libusb_init)+'\0'+(32-bit hex PID)
sprintf(sem_name, "libusb_init%08X", (unsigned int)GetCurrentProcessId());
semaphore = CreateSemaphoreA(NULL, 1, 1, sem_name);
- if (semaphore == NULL) {
+ if (semaphore == NULL)
return;
- }
// A successful wait brings our semaphore count to 0 (unsignaled)
// => any concurent wait stalls until the semaphore release
@@ -1593,16 +1640,15 @@ static void windows_exit(void)
}
// Only works if exits and inits are balanced exactly
- if (--concurrent_usage < 0) { // Last exit
- for (i=0; i<USB_API_MAX; i++) {
+ if (--concurrent_usage < 0) { // Last exit
+ for (i = 0; i < USB_API_MAX; i++)
usb_api_backend[i].exit(SUB_API_NOTSET);
- }
exit_polling();
windows_common_exit();
usbi_mutex_destroy(&autoclaim_lock);
}
- ReleaseSemaphore(semaphore, 1, NULL); // increase count back to 1
+ ReleaseSemaphore(semaphore, 1, NULL); // increase count back to 1
CloseHandle(semaphore);
}
@@ -1610,7 +1656,7 @@ static int windows_get_device_descriptor(struct libusb_device *dev, unsigned cha
{
struct windows_device_priv *priv = _device_priv(dev);
- memcpy(buffer, &(priv->dev_descriptor), DEVICE_DESC_LENGTH);
+ memcpy(buffer, &priv->dev_descriptor, DEVICE_DESC_LENGTH);
*host_endian = 0;
return LIBUSB_SUCCESS;
@@ -1703,9 +1749,9 @@ static int windows_set_configuration(struct libusb_device_handle *dev_handle, in
LIBUSB_REQUEST_SET_CONFIGURATION, (uint16_t)config,
0, NULL, 0, 1000);
- if (r == LIBUSB_SUCCESS) {
+ if (r == LIBUSB_SUCCESS)
priv->active_config = (uint8_t)config;
- }
+
return r;
}
@@ -1715,13 +1761,12 @@ static int windows_claim_interface(struct libusb_device_handle *dev_handle, int
struct windows_device_priv *priv = _device_priv(dev_handle->dev);
safe_free(priv->usb_interface[iface].endpoint);
- priv->usb_interface[iface].nb_endpoints= 0;
+ priv->usb_interface[iface].nb_endpoints = 0;
r = priv->apib->claim_interface(SUB_API_NOTSET, dev_handle, iface);
- if (r == LIBUSB_SUCCESS) {
+ if (r == LIBUSB_SUCCESS)
r = windows_assign_endpoints(dev_handle, iface, 0);
- }
return r;
}
@@ -1732,13 +1777,12 @@ static int windows_set_interface_altsetting(struct libusb_device_handle *dev_han
struct windows_device_priv *priv = _device_priv(dev_handle->dev);
safe_free(priv->usb_interface[iface].endpoint);
- priv->usb_interface[iface].nb_endpoints= 0;
+ priv->usb_interface[iface].nb_endpoints = 0;
r = priv->apib->set_interface_altsetting(SUB_API_NOTSET, dev_handle, iface, altsetting);
- if (r == LIBUSB_SUCCESS) {
+ if (r == LIBUSB_SUCCESS)
r = windows_assign_endpoints(dev_handle, iface, altsetting);
- }
return r;
}
@@ -1785,7 +1829,7 @@ static void windows_destroy_device(struct libusb_device *dev)
void windows_clear_transfer_priv(struct usbi_transfer *itransfer)
{
- struct windows_transfer_priv *transfer_priv = (struct windows_transfer_priv*)usbi_transfer_get_os_priv(itransfer);
+ struct windows_transfer_priv *transfer_priv = usbi_transfer_get_os_priv(itransfer);
usbi_free_fd(&transfer_priv->pollable_fd);
safe_free(transfer_priv->hid_buffer);
@@ -1797,14 +1841,13 @@ static int submit_bulk_transfer(struct usbi_transfer *itransfer)
{
struct libusb_transfer *transfer = USBI_TRANSFER_TO_LIBUSB_TRANSFER(itransfer);
struct libusb_context *ctx = DEVICE_CTX(transfer->dev_handle->dev);
- struct windows_transfer_priv *transfer_priv = (struct windows_transfer_priv*)usbi_transfer_get_os_priv(itransfer);
+ struct windows_transfer_priv *transfer_priv = usbi_transfer_get_os_priv(itransfer);
struct windows_device_priv *priv = _device_priv(transfer->dev_handle->dev);
int r;
r = priv->apib->submit_bulk_transfer(SUB_API_NOTSET, itransfer);
- if (r != LIBUSB_SUCCESS) {
+ if (r != LIBUSB_SUCCESS)
return r;
- }
usbi_add_pollfd(ctx, transfer_priv->pollable_fd.fd,
(short)(IS_XFERIN(transfer) ? POLLIN : POLLOUT));
@@ -1816,14 +1859,13 @@ static int submit_iso_transfer(struct usbi_transfer *itransfer)
{
struct libusb_transfer *transfer = USBI_TRANSFER_TO_LIBUSB_TRANSFER(itransfer);
struct libusb_context *ctx = DEVICE_CTX(transfer->dev_handle->dev);
- struct windows_transfer_priv *transfer_priv = (struct windows_transfer_priv*)usbi_transfer_get_os_priv(itransfer);
+ struct windows_transfer_priv *transfer_priv = usbi_transfer_get_os_priv(itransfer);
struct windows_device_priv *priv = _device_priv(transfer->dev_handle->dev);
int r;
r = priv->apib->submit_iso_transfer(SUB_API_NOTSET, itransfer);
- if (r != LIBUSB_SUCCESS) {
+ if (r != LIBUSB_SUCCESS)
return r;
- }
usbi_add_pollfd(ctx, transfer_priv->pollable_fd.fd,
(short)(IS_XFERIN(transfer) ? POLLIN : POLLOUT));
@@ -1835,19 +1877,17 @@ static int submit_control_transfer(struct usbi_transfer *itransfer)
{
struct libusb_transfer *transfer = USBI_TRANSFER_TO_LIBUSB_TRANSFER(itransfer);
struct libusb_context *ctx = DEVICE_CTX(transfer->dev_handle->dev);
- struct windows_transfer_priv *transfer_priv = (struct windows_transfer_priv*)usbi_transfer_get_os_priv(itransfer);
+ struct windows_transfer_priv *transfer_priv = usbi_transfer_get_os_priv(itransfer);
struct windows_device_priv *priv = _device_priv(transfer->dev_handle->dev);
int r;
r = priv->apib->submit_control_transfer(SUB_API_NOTSET, itransfer);
- if (r != LIBUSB_SUCCESS) {
+ if (r != LIBUSB_SUCCESS)
return r;
- }
usbi_add_pollfd(ctx, transfer_priv->pollable_fd.fd, POLLIN);
return LIBUSB_SUCCESS;
-
}
static int windows_submit_transfer(struct usbi_transfer *itransfer)
@@ -1859,8 +1899,7 @@ static int windows_submit_transfer(struct usbi_transfer *itransfer)
return submit_control_transfer(itransfer);
case LIBUSB_TRANSFER_TYPE_BULK:
case LIBUSB_TRANSFER_TYPE_INTERRUPT:
- if (IS_XFEROUT(transfer) &&
- transfer->flags & LIBUSB_TRANSFER_ADD_ZERO_PACKET)
+ if (IS_XFEROUT(transfer) && (transfer->flags & LIBUSB_TRANSFER_ADD_ZERO_PACKET))
return LIBUSB_ERROR_NOT_SUPPORTED;
return submit_bulk_transfer(itransfer);
case LIBUSB_TRANSFER_TYPE_ISOCHRONOUS:
@@ -1926,9 +1965,9 @@ void windows_get_overlapped_result(struct usbi_transfer *transfer, struct winfd
if (HasOverlappedIoCompletedSync(pollable_fd->overlapped)) {
*io_result = NO_ERROR;
*io_size = (DWORD)pollable_fd->overlapped->InternalHigh;
- // Regular async overlapped
}
else if (GetOverlappedResult(pollable_fd->handle, pollable_fd->overlapped, io_size, false)) {
+ // Regular async overlapped
*io_result = NO_ERROR;
}
else {
@@ -1991,62 +2030,96 @@ const struct usbi_os_backend windows_backend = {
/*
* USB API backends
*/
-static int unsupported_init(int sub_api, struct libusb_context *ctx) {
+static int unsupported_init(int sub_api, struct libusb_context *ctx)
+{
return LIBUSB_SUCCESS;
}
-static int unsupported_exit(int sub_api) {
+
+static int unsupported_exit(int sub_api)
+{
return LIBUSB_SUCCESS;
}
-static int unsupported_open(int sub_api, struct libusb_device_handle *dev_handle) {
+
+static int unsupported_open(int sub_api, struct libusb_device_handle *dev_handle)
+{
PRINT_UNSUPPORTED_API(open);
}
-static void unsupported_close(int sub_api, struct libusb_device_handle *dev_handle) {
+
+static void unsupported_close(int sub_api, struct libusb_device_handle *dev_handle)
+{
usbi_dbg("unsupported API call for 'close'");
}
-static int unsupported_configure_endpoints(int sub_api, struct libusb_device_handle *dev_handle, int iface) {
+
+static int unsupported_configure_endpoints(int sub_api, struct libusb_device_handle *dev_handle, int iface)
+{
PRINT_UNSUPPORTED_API(configure_endpoints);
}
-static int unsupported_claim_interface(int sub_api, struct libusb_device_handle *dev_handle, int iface) {
+
+static int unsupported_claim_interface(int sub_api, struct libusb_device_handle *dev_handle, int iface)
+{
PRINT_UNSUPPORTED_API(claim_interface);
}
-static int unsupported_set_interface_altsetting(int sub_api, struct libusb_device_handle *dev_handle, int iface, int altsetting) {
+
+static int unsupported_set_interface_altsetting(int sub_api, struct libusb_device_handle *dev_handle, int iface, int altsetting)
+{
PRINT_UNSUPPORTED_API(set_interface_altsetting);
}
-static int unsupported_release_interface(int sub_api, struct libusb_device_handle *dev_handle, int iface) {
+
+static int unsupported_release_interface(int sub_api, struct libusb_device_handle *dev_handle, int iface)
+{
PRINT_UNSUPPORTED_API(release_interface);
}
-static int unsupported_clear_halt(int sub_api, struct libusb_device_handle *dev_handle, unsigned char endpoint) {
+
+static int unsupported_clear_halt(int sub_api, struct libusb_device_handle *dev_handle, unsigned char endpoint)
+{
PRINT_UNSUPPORTED_API(clear_halt);
}
-static int unsupported_reset_device(int sub_api, struct libusb_device_handle *dev_handle) {
+
+static int unsupported_reset_device(int sub_api, struct libusb_device_handle *dev_handle)
+{
PRINT_UNSUPPORTED_API(reset_device);
}
-static int unsupported_submit_bulk_transfer(int sub_api, struct usbi_transfer *itransfer) {
+
+static int unsupported_submit_bulk_transfer(int sub_api, struct usbi_transfer *itransfer)
+{
PRINT_UNSUPPORTED_API(submit_bulk_transfer);
}
-static int unsupported_submit_iso_transfer(int sub_api, struct usbi_transfer *itransfer) {
+
+static int unsupported_submit_iso_transfer(int sub_api, struct usbi_transfer *itransfer)
+{
PRINT_UNSUPPORTED_API(submit_iso_transfer);
}
-static int unsupported_submit_control_transfer(int sub_api, struct usbi_transfer *itransfer) {
+
+static int unsupported_submit_control_transfer(int sub_api, struct usbi_transfer *itransfer)
+{
PRINT_UNSUPPORTED_API(submit_control_transfer);
}
-static int unsupported_abort_control(int sub_api, struct usbi_transfer *itransfer) {
+
+static int unsupported_abort_control(int sub_api, struct usbi_transfer *itransfer)
+{
PRINT_UNSUPPORTED_API(abort_control);
}
-static int unsupported_abort_transfers(int sub_api, struct usbi_transfer *itransfer) {
+
+static int unsupported_abort_transfers(int sub_api, struct usbi_transfer *itransfer)
+{
PRINT_UNSUPPORTED_API(abort_transfers);
}
-static int unsupported_copy_transfer_data(int sub_api, struct usbi_transfer *itransfer, uint32_t io_size) {
+
+static int unsupported_copy_transfer_data(int sub_api, struct usbi_transfer *itransfer, uint32_t io_size)
+{
PRINT_UNSUPPORTED_API(copy_transfer_data);
}
-static int common_configure_endpoints(int sub_api, struct libusb_device_handle *dev_handle, int iface) {
+
+static int common_configure_endpoints(int sub_api, struct libusb_device_handle *dev_handle, int iface)
+{
return LIBUSB_SUCCESS;
}
+
// These names must be uppercase
-const char* hub_driver_names[] = {"USBHUB", "USBHUB3", "USB3HUB", "NUSB3HUB", "RUSB3HUB", "FLXHCIH", "TIHUB3", "ETRONHUB3", "VIAHUB3", "ASMTHUB3", "IUSB3HUB", "VUSB3HUB", "AMDHUB30", "VHHUB"};
-const char* composite_driver_names[] = {"USBCCGP"};
-const char* winusbx_driver_names[] = WINUSBX_DRV_NAMES;
-const char* hid_driver_names[] = {"HIDUSB", "MOUHID", "KBDHID"};
+static const char *hub_driver_names[] = {"USBHUB", "USBHUB3", "USB3HUB", "NUSB3HUB", "RUSB3HUB", "FLXHCIH", "TIHUB3", "ETRONHUB3", "VIAHUB3", "ASMTHUB3", "IUSB3HUB", "VUSB3HUB", "AMDHUB30", "VHHUB"};
+static const char *composite_driver_names[] = {"USBCCGP"};
+static const char *winusbx_driver_names[] = WINUSBX_DRV_NAMES;
+static const char *hid_driver_names[] = {"HIDUSB", "MOUHID", "KBDHID"};
const struct windows_usb_api_backend usb_api_backend[USB_API_MAX] = {
{
USB_API_UNSUPPORTED,
@@ -2069,7 +2142,8 @@ const struct windows_usb_api_backend usb_api_backend[USB_API_MAX] = {
unsupported_abort_control,
unsupported_abort_transfers,
unsupported_copy_transfer_data,
- }, {
+ },
+ {
USB_API_HUB,
"HUB API",
hub_driver_names,
@@ -2090,7 +2164,8 @@ const struct windows_usb_api_backend usb_api_backend[USB_API_MAX] = {
unsupported_abort_control,
unsupported_abort_transfers,
unsupported_copy_transfer_data,
- }, {
+ },
+ {
USB_API_COMPOSITE,
"Composite API",
composite_driver_names,
@@ -2111,7 +2186,8 @@ const struct windows_usb_api_backend usb_api_backend[USB_API_MAX] = {
composite_abort_control,
composite_abort_transfers,
composite_copy_transfer_data,
- }, {
+ },
+ {
USB_API_WINUSBX,
"WinUSB-like APIs",
winusbx_driver_names,
@@ -2132,7 +2208,8 @@ const struct windows_usb_api_backend usb_api_backend[USB_API_MAX] = {
winusbx_abort_control,
winusbx_abort_transfers,
winusbx_copy_transfer_data,
- }, {
+ },
+ {
USB_API_HID,
"HID API",
hid_driver_names,
@@ -2160,48 +2237,55 @@ const struct windows_usb_api_backend usb_api_backend[USB_API_MAX] = {
/*
* WinUSB-like (WinUSB, libusb0/libusbK through libusbk DLL) API functions
*/
-#define WinUSBX_Set(fn) do { if (native_winusb) WinUSBX[i].fn = (WinUsb_##fn##_t) GetProcAddress(h, "WinUsb_" #fn); \
- else pLibK_GetProcAddress((PVOID*)&WinUSBX[i].fn, i, KUSB_FNID_##fn); } while (0)
+#define WinUSBX_Set(fn) \
+ do { \
+ if (native_winusb) \
+ WinUSBX[i].fn = (WinUsb_##fn##_t) GetProcAddress(h, "WinUsb_" #fn); \
+ else \
+ pLibK_GetProcAddress((PVOID *)&WinUSBX[i].fn, i, KUSB_FNID_##fn); \
+ } while (0)
static int winusbx_init(int sub_api, struct libusb_context *ctx)
{
- HMODULE h = NULL;
- bool native_winusb = false;
+ HMODULE h;
+ bool native_winusb;
int i;
KLIB_VERSION LibK_Version;
LibK_GetProcAddress_t pLibK_GetProcAddress = NULL;
- LibK_GetVersion_t pLibK_GetVersion = NULL;
+ LibK_GetVersion_t pLibK_GetVersion;
h = GetModuleHandleA("libusbK");
- if (h == NULL) {
+ if (h == NULL)
h = LoadLibraryA("libusbK");
- }
+
if (h == NULL) {
usbi_info(ctx, "libusbK DLL is not available, will use native WinUSB");
h = GetModuleHandleA("WinUSB");
- if (h == NULL) {
+ if (h == NULL)
h = LoadLibraryA("WinUSB");
- } if (h == NULL) {
+
+ if (h == NULL) {
usbi_warn(ctx, "WinUSB DLL is not available either, "
"you will not be able to access devices outside of enumeration");
return LIBUSB_ERROR_NOT_FOUND;
}
} else {
usbi_dbg("using libusbK DLL for universal access");
- pLibK_GetVersion = (LibK_GetVersion_t) GetProcAddress(h, "LibK_GetVersion");
+ pLibK_GetVersion = (LibK_GetVersion_t)GetProcAddress(h, "LibK_GetVersion");
if (pLibK_GetVersion != NULL) {
pLibK_GetVersion(&LibK_Version);
usbi_dbg("libusbK version: %d.%d.%d.%d", LibK_Version.Major, LibK_Version.Minor,
LibK_Version.Micro, LibK_Version.Nano);
}
- pLibK_GetProcAddress = (LibK_GetProcAddress_t) GetProcAddress(h, "LibK_GetProcAddress");
+ pLibK_GetProcAddress = (LibK_GetProcAddress_t)GetProcAddress(h, "LibK_GetProcAddress");
if (pLibK_GetProcAddress == NULL) {
usbi_err(ctx, "LibK_GetProcAddress() not found in libusbK DLL");
return LIBUSB_ERROR_NOT_FOUND;
}
}
+
native_winusb = (pLibK_GetProcAddress == NULL);
- for (i=SUB_API_LIBUSBK; i<SUB_API_MAX; i++) {
+ for (i = SUB_API_LIBUSBK; i < SUB_API_MAX; i++) {
WinUSBX_Set(AbortPipe);
WinUSBX_Set(ControlTransfer);
WinUSBX_Set(FlushPipe);
@@ -2222,9 +2306,9 @@ static int winusbx_init(int sub_api, struct libusb_context *ctx)
WinUSBX_Set(SetPipePolicy);
WinUSBX_Set(SetPowerPolicy);
WinUSBX_Set(WritePipe);
- if (!native_winusb) {
+ if (!native_winusb)
WinUSBX_Set(ResetDevice);
- }
+
if (WinUSBX[i].Initialize != NULL) {
WinUSBX[i].initialized = true;
usbi_dbg("initalized sub API %s", sub_api_name[i]);
@@ -2233,6 +2317,7 @@ static int winusbx_init(int sub_api, struct libusb_context *ctx)
WinUSBX[i].initialized = false;
}
}
+
return LIBUSB_SUCCESS;
}
@@ -2257,14 +2342,14 @@ static int winusbx_open(int sub_api, struct libusb_device_handle *dev_handle)
// WinUSB requires a separate handle for each interface
for (i = 0; i < USB_MAXINTERFACES; i++) {
- if ( (priv->usb_interface[i].path != NULL)
- && (priv->usb_interface[i].apib->id == USB_API_WINUSBX) ) {
+ if ((priv->usb_interface[i].path != NULL)
+ && (priv->usb_interface[i].apib->id == USB_API_WINUSBX)) {
file_handle = CreateFileA(priv->usb_interface[i].path, GENERIC_WRITE | GENERIC_READ, FILE_SHARE_WRITE | FILE_SHARE_READ,
NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL | FILE_FLAG_OVERLAPPED, NULL);
if (file_handle == INVALID_HANDLE_VALUE) {
usbi_err(ctx, "could not open device %s (interface %d): %s", priv->usb_interface[i].path, i, windows_error_str(0));
switch(GetLastError()) {
- case ERROR_FILE_NOT_FOUND: // The device was disconnected
+ case ERROR_FILE_NOT_FOUND: // The device was disconnected
return LIBUSB_ERROR_NO_DEVICE;
case ERROR_ACCESS_DENIED:
return LIBUSB_ERROR_ACCESS;
@@ -2288,6 +2373,7 @@ static void winusbx_close(int sub_api, struct libusb_device_handle *dev_handle)
if (sub_api == SUB_API_NOTSET)
sub_api = priv->sub_api;
+
if (!WinUSBX[sub_api].initialized)
return;
@@ -2297,13 +2383,12 @@ static void winusbx_close(int sub_api, struct libusb_device_handle *dev_handle)
for (i = 0; i < USB_MAXINTERFACES; i++) {
if (priv->usb_interface[i].apib->id == USB_API_WINUSBX) {
handle = handle_priv->interface_handle[i].api_handle;
- if ((handle != 0) && (handle != INVALID_HANDLE_VALUE)) {
+ if (HANDLE_VALID(handle))
WinUSBX[sub_api].Free(handle);
- }
+
handle = handle_priv->interface_handle[i].dev_handle;
- if ((handle != 0) && (handle != INVALID_HANDLE_VALUE)) {
+ if (HANDLE_VALID(handle))
CloseHandle(handle);
- }
}
}
}
@@ -2312,18 +2397,16 @@ static void winusbx_close(int sub_api, struct libusb_device_handle *dev_handle)
// then free and close interface 0 last
for (i = 1; i < USB_MAXINTERFACES; i++) {
handle = handle_priv->interface_handle[i].api_handle;
- if ((handle != 0) && (handle != INVALID_HANDLE_VALUE)) {
+ if (HANDLE_VALID(handle))
WinUSBX[sub_api].Free(handle);
- }
}
handle = handle_priv->interface_handle[0].api_handle;
- if ((handle != 0) && (handle != INVALID_HANDLE_VALUE)) {
+ if (HANDLE_VALID(handle))
WinUSBX[sub_api].Free(handle);
- }
+
handle = handle_priv->interface_handle[0].dev_handle;
- if ((handle != 0) && (handle != INVALID_HANDLE_VALUE)) {
+ if (HANDLE_VALID(handle))
CloseHandle(handle);
- }
}
}
@@ -2341,35 +2424,34 @@ static int winusbx_configure_endpoints(int sub_api, struct libusb_device_handle
// With handle and enpoints set (in parent), we can setup the default pipe properties
// see http://download.microsoft.com/download/D/1/D/D1DD7745-426B-4CC3-A269-ABBBE427C0EF/DVC-T705_DDC08.pptx
- for (i=-1; i<priv->usb_interface[iface].nb_endpoints; i++) {
- endpoint_address =(i==-1)?0:priv->usb_interface[iface].endpoint[i];
+ for (i = -1; i < priv->usb_interface[iface].nb_endpoints; i++) {
+ endpoint_address = (i == -1) ? 0 : priv->usb_interface[iface].endpoint[i];
if (!WinUSBX[sub_api].SetPipePolicy(winusb_handle, endpoint_address,
- PIPE_TRANSFER_TIMEOUT, sizeof(ULONG), &timeout)) {
+ PIPE_TRANSFER_TIMEOUT, sizeof(ULONG), &timeout))
usbi_dbg("failed to set PIPE_TRANSFER_TIMEOUT for control endpoint %02X", endpoint_address);
- }
- if ((i == -1) || (sub_api == SUB_API_LIBUSB0)) {
- continue; // Other policies don't apply to control endpoint or libusb0
- }
+
+ if ((i == -1) || (sub_api == SUB_API_LIBUSB0))
+ continue; // Other policies don't apply to control endpoint or libusb0
+
policy = false;
if (!WinUSBX[sub_api].SetPipePolicy(winusb_handle, endpoint_address,
- SHORT_PACKET_TERMINATE, sizeof(UCHAR), &policy)) {
+ SHORT_PACKET_TERMINATE, sizeof(UCHAR), &policy))
usbi_dbg("failed to disable SHORT_PACKET_TERMINATE for endpoint %02X", endpoint_address);
- }
+
if (!WinUSBX[sub_api].SetPipePolicy(winusb_handle, endpoint_address,
- IGNORE_SHORT_PACKETS, sizeof(UCHAR), &policy)) {
+ IGNORE_SHORT_PACKETS, sizeof(UCHAR), &policy))
usbi_dbg("failed to disable IGNORE_SHORT_PACKETS for endpoint %02X", endpoint_address);
- }
+
policy = true;
/* ALLOW_PARTIAL_READS must be enabled due to likely libusbK bug. See:
https://sourceforge.net/mailarchive/message.php?msg_id=29736015 */
if (!WinUSBX[sub_api].SetPipePolicy(winusb_handle, endpoint_address,
- ALLOW_PARTIAL_READS, sizeof(UCHAR), &policy)) {
+ ALLOW_PARTIAL_READS, sizeof(UCHAR), &policy))
usbi_dbg("failed to enable ALLOW_PARTIAL_READS for endpoint %02X", endpoint_address);
- }
+
if (!WinUSBX[sub_api].SetPipePolicy(winusb_handle, endpoint_address,
- AUTO_CLEAR_STALL, sizeof(UCHAR), &policy)) {
+ AUTO_CLEAR_STALL, sizeof(UCHAR), &policy))
usbi_dbg("failed to enable AUTO_CLEAR_STALL for endpoint %02X", endpoint_address);
- }
}
return LIBUSB_SUCCESS;
@@ -2381,15 +2463,15 @@ static int winusbx_claim_interface(int sub_api, struct libusb_device_handle *dev
struct windows_device_handle_priv *handle_priv = _device_handle_priv(dev_handle);
struct windows_device_priv *priv = _device_priv(dev_handle->dev);
bool is_using_usbccgp = (priv->apib->id == USB_API_COMPOSITE);
- HANDLE file_handle, winusb_handle;
- DWORD err;
- int i;
SP_DEVICE_INTERFACE_DETAIL_DATA_A *dev_interface_details = NULL;
HDEVINFO dev_info = INVALID_HANDLE_VALUE;
SP_DEVINFO_DATA dev_info_data;
- char* dev_path_no_guid = NULL;
+ char *dev_path_no_guid = NULL;
char filter_path[] = "\\\\.\\libusb0-0000";
bool found_filter = false;
+ HANDLE file_handle, winusb_handle;
+ DWORD err;
+ int i;
CHECK_WINUSBX_AVAILABLE(sub_api);
@@ -2398,9 +2480,8 @@ static int winusbx_claim_interface(int sub_api, struct libusb_device_handle *dev
if ((is_using_usbccgp) || (iface == 0)) {
// composite device (independent interfaces) or interface 0
file_handle = handle_priv->interface_handle[iface].dev_handle;
- if ((file_handle == 0) || (file_handle == INVALID_HANDLE_VALUE)) {
+ if (!HANDLE_VALID(file_handle))
return LIBUSB_ERROR_NOT_FOUND;
- }
if (!WinUSBX[sub_api].Initialize(file_handle, &winusb_handle)) {
handle_priv->interface_handle[iface].api_handle = INVALID_HANDLE_VALUE;
@@ -2417,9 +2498,9 @@ static int winusbx_claim_interface(int sub_api, struct libusb_device_handle *dev
safe_free(dev_interface_details);
safe_free(dev_path_no_guid);
dev_interface_details = get_interface_details_filter(ctx, &dev_info, &dev_info_data, &GUID_DEVINTERFACE_LIBUSB0_FILTER, i, filter_path);
- if ((found_filter) || (dev_interface_details == NULL)) {
+ if ((found_filter) || (dev_interface_details == NULL))
break;
- }
+
// ignore GUID part
dev_path_no_guid = sanitize_path(strtok(dev_interface_details->DevicePath, "{"));
if (safe_strncmp(dev_path_no_guid, priv->usb_interface[iface].path, safe_strlen(dev_path_no_guid)) == 0) {
@@ -2448,7 +2529,7 @@ static int winusbx_claim_interface(int sub_api, struct libusb_device_handle *dev
winusb_handle = handle_priv->interface_handle[0].api_handle;
// It is a requirement for multiple interface devices on Windows that, to you
// must first claim the first interface before you claim the others
- if ((winusb_handle == 0) || (winusb_handle == INVALID_HANDLE_VALUE)) {
+ if (!HANDLE_VALID(winusb_handle)) {
file_handle = handle_priv->interface_handle[0].dev_handle;
if (WinUSBX[sub_api].Initialize(file_handle, &winusb_handle)) {
handle_priv->interface_handle[0].api_handle = winusb_handle;
@@ -2458,7 +2539,7 @@ static int winusbx_claim_interface(int sub_api, struct libusb_device_handle *dev
return LIBUSB_ERROR_ACCESS;
}
}
- if (!WinUSBX[sub_api].GetAssociatedInterface(winusb_handle, (UCHAR)(iface-1),
+ if (!WinUSBX[sub_api].GetAssociatedInterface(winusb_handle, (UCHAR)(iface - 1),
&handle_priv->interface_handle[iface].api_handle)) {
handle_priv->interface_handle[iface].api_handle = INVALID_HANDLE_VALUE;
switch(GetLastError()) {
@@ -2489,9 +2570,8 @@ static int winusbx_release_interface(int sub_api, struct libusb_device_handle *d
CHECK_WINUSBX_AVAILABLE(sub_api);
winusb_handle = handle_priv->interface_handle[iface].api_handle;
- if ((winusb_handle == 0) || (winusb_handle == INVALID_HANDLE_VALUE)) {
+ if (!HANDLE_VALID(winusb_handle))
return LIBUSB_ERROR_NOT_FOUND;
- }
WinUSBX[sub_api].Free(winusb_handle);
handle_priv->interface_handle[iface].api_handle = INVALID_HANDLE_VALUE;
@@ -2513,15 +2593,13 @@ static int get_valid_interface(struct libusb_device_handle *dev_handle, int api_
return -1;
}
- for (i=0; i<USB_MAXINTERFACES; i++) {
- if ( (handle_priv->interface_handle[i].dev_handle != 0)
- && (handle_priv->interface_handle[i].dev_handle != INVALID_HANDLE_VALUE)
- && (handle_priv->interface_handle[i].api_handle != 0)
- && (handle_priv->interface_handle[i].api_handle != INVALID_HANDLE_VALUE)
- && (priv->usb_interface[i].apib->id == api_id) ) {
+ for (i = 0; i < USB_MAXINTERFACES; i++) {
+ if (HANDLE_VALID(handle_priv->interface_handle[i].dev_handle)
+ && HANDLE_VALID(handle_priv->interface_handle[i].api_handle)
+ && (priv->usb_interface[i].apib->id == api_id))
return i;
- }
}
+
return -1;
}
@@ -2532,19 +2610,18 @@ static int interface_by_endpoint(struct windows_device_priv *priv,
struct windows_device_handle_priv *handle_priv, uint8_t endpoint_address)
{
int i, j;
- for (i=0; i<USB_MAXINTERFACES; i++) {
- if (handle_priv->interface_handle[i].api_handle == INVALID_HANDLE_VALUE)
- continue;
- if (handle_priv->interface_handle[i].api_handle == 0)
+
+ for (i = 0; i < USB_MAXINTERFACES; i++) {
+ if (!HANDLE_VALID(handle_priv->interface_handle[i].api_handle))
continue;
if (priv->usb_interface[i].endpoint == NULL)
continue;
- for (j=0; j<priv->usb_interface[i].nb_endpoints; j++) {
- if (priv->usb_interface[i].endpoint[j] == endpoint_address) {
+ for (j = 0; j < priv->usb_interface[i].nb_endpoints; j++) {
+ if (priv->usb_interface[i].endpoint[j] == endpoint_address)
return i;
- }
}
}
+
return -1;
}
@@ -2553,10 +2630,9 @@ static int winusbx_submit_control_transfer(int sub_api, struct usbi_transfer *it
struct libusb_transfer *transfer = USBI_TRANSFER_TO_LIBUSB_TRANSFER(itransfer);
struct libusb_context *ctx = DEVICE_CTX(transfer->dev_handle->dev);
struct windows_device_priv *priv = _device_priv(transfer->dev_handle->dev);
- struct windows_transfer_priv *transfer_priv = (struct windows_transfer_priv*)usbi_transfer_get_os_priv(itransfer);
- struct windows_device_handle_priv *handle_priv = _device_handle_priv(
- transfer->dev_handle);
- WINUSB_SETUP_PACKET *setup = (WINUSB_SETUP_PACKET *) transfer->buffer;
+ struct windows_transfer_priv *transfer_priv = usbi_transfer_get_os_priv(itransfer);
+ struct windows_device_handle_priv *handle_priv = _device_handle_priv(transfer->dev_handle);
+ WINUSB_SETUP_PACKET *setup = (WINUSB_SETUP_PACKET *)transfer->buffer;
ULONG size;
HANDLE winusb_handle;
int current_interface;
@@ -2572,9 +2648,8 @@ static int winusbx_submit_control_transfer(int sub_api, struct usbi_transfer *it
current_interface = get_valid_interface(transfer->dev_handle, USB_API_WINUSBX);
if (current_interface < 0) {
- if (auto_claim(transfer, &current_interface, USB_API_WINUSBX) != LIBUSB_SUCCESS) {
+ if (auto_claim(transfer, &current_interface, USB_API_WINUSBX) != LIBUSB_SUCCESS)
return LIBUSB_ERROR_NOT_FOUND;
- }
}
usbi_dbg("will use interface %d", current_interface);
@@ -2582,13 +2657,12 @@ static int winusbx_submit_control_transfer(int sub_api, struct usbi_transfer *it
wfd = usbi_create_fd(winusb_handle, RW_READ, NULL, NULL);
// Always use the handle returned from usbi_create_fd (wfd.handle)
- if (wfd.fd < 0) {
+ if (wfd.fd < 0)
return LIBUSB_ERROR_NO_MEM;
- }
// Sending of set configuration control requests from WinUSB creates issues
- if ( ((setup->request_type & (0x03 << 5)) == LIBUSB_REQUEST_TYPE_STANDARD)
- && (setup->request == LIBUSB_REQUEST_SET_CONFIGURATION) ) {
+ if (((setup->request_type & (0x03 << 5)) == LIBUSB_REQUEST_TYPE_STANDARD)
+ && (setup->request == LIBUSB_REQUEST_SET_CONFIGURATION)) {
if (setup->value != priv->active_config) {
usbi_warn(ctx, "cannot set configuration other than the default one");
usbi_free_fd(&wfd);
@@ -2598,7 +2672,7 @@ static int winusbx_submit_control_transfer(int sub_api, struct usbi_transfer *it
wfd.overlapped->InternalHigh = 0;
} else {
if (!WinUSBX[sub_api].ControlTransfer(wfd.handle, *setup, transfer->buffer + LIBUSB_CONTROL_SETUP_SIZE, size, NULL, wfd.overlapped)) {
- if(GetLastError() != ERROR_IO_PENDING) {
+ if (GetLastError() != ERROR_IO_PENDING) {
usbi_warn(ctx, "ControlTransfer failed: %s", windows_error_str(0));
usbi_free_fd(&wfd);
return LIBUSB_ERROR_IO;
@@ -2625,12 +2699,11 @@ static int winusbx_set_interface_altsetting(int sub_api, struct libusb_device_ha
CHECK_WINUSBX_AVAILABLE(sub_api);
- if (altsetting > 255) {
+ if (altsetting > 255)
return LIBUSB_ERROR_INVALID_PARAM;
- }
winusb_handle = handle_priv->interface_handle[iface].api_handle;
- if ((winusb_handle == 0) || (winusb_handle == INVALID_HANDLE_VALUE)) {
+ if (!HANDLE_VALID(winusb_handle)) {
usbi_err(ctx, "interface must be claimed first");
return LIBUSB_ERROR_NOT_FOUND;
}
@@ -2647,7 +2720,7 @@ static int winusbx_submit_bulk_transfer(int sub_api, struct usbi_transfer *itran
{
struct libusb_transfer *transfer = USBI_TRANSFER_TO_LIBUSB_TRANSFER(itransfer);
struct libusb_context *ctx = DEVICE_CTX(transfer->dev_handle->dev);
- struct windows_transfer_priv *transfer_priv = (struct windows_transfer_priv*)usbi_transfer_get_os_priv(itransfer);
+ struct windows_transfer_priv *transfer_priv = usbi_transfer_get_os_priv(itransfer);
struct windows_device_handle_priv *handle_priv = _device_handle_priv(transfer->dev_handle);
struct windows_device_priv *priv = _device_priv(transfer->dev_handle->dev);
HANDLE winusb_handle;
@@ -2671,9 +2744,8 @@ static int winusbx_submit_bulk_transfer(int sub_api, struct usbi_transfer *itran
wfd = usbi_create_fd(winusb_handle, IS_XFERIN(transfer) ? RW_READ : RW_WRITE, NULL, NULL);
// Always use the handle returned from usbi_create_fd (wfd.handle)
- if (wfd.fd < 0) {
+ if (wfd.fd < 0)
return LIBUSB_ERROR_NO_MEM;
- }
if (IS_XFERIN(transfer)) {
usbi_dbg("reading %d bytes", transfer->length);
@@ -2682,8 +2754,9 @@ static int winusbx_submit_bulk_transfer(int sub_api, struct usbi_transfer *itran
usbi_dbg("writing %d bytes", transfer->length);
ret = WinUSBX[sub_api].WritePipe(wfd.handle, transfer->endpoint, transfer->buffer, transfer->length, NULL, wfd.overlapped);
}
+
if (!ret) {
- if(GetLastError() != ERROR_IO_PENDING) {
+ if (GetLastError() != ERROR_IO_PENDING) {
usbi_err(ctx, "ReadPipe/WritePipe failed: %s", windows_error_str(0));
usbi_free_fd(&wfd);
return LIBUSB_ERROR_IO;
@@ -2743,7 +2816,7 @@ static int winusbx_abort_transfers(int sub_api, struct usbi_transfer *itransfer)
struct libusb_transfer *transfer = USBI_TRANSFER_TO_LIBUSB_TRANSFER(itransfer);
struct libusb_context *ctx = DEVICE_CTX(transfer->dev_handle->dev);
struct windows_device_handle_priv *handle_priv = _device_handle_priv(transfer->dev_handle);
- struct windows_transfer_priv *transfer_priv = (struct windows_transfer_priv*)usbi_transfer_get_os_priv(itransfer);
+ struct windows_transfer_priv *transfer_priv = usbi_transfer_get_os_priv(itransfer);
struct windows_device_priv *priv = _device_priv(transfer->dev_handle->dev);
HANDLE winusb_handle;
int current_interface;
@@ -2788,33 +2861,31 @@ static int winusbx_reset_device(int sub_api, struct libusb_device_handle *dev_ha
CHECK_WINUSBX_AVAILABLE(sub_api);
// Reset any available pipe (except control)
- for (i=0; i<USB_MAXINTERFACES; i++) {
+ for (i = 0; i < USB_MAXINTERFACES; i++) {
winusb_handle = handle_priv->interface_handle[i].api_handle;
- for (wfd = handle_to_winfd(winusb_handle); wfd.fd > 0;)
- {
+ for (wfd = handle_to_winfd(winusb_handle); wfd.fd > 0; ) {
// Cancel any pollable I/O
usbi_remove_pollfd(ctx, wfd.fd);
usbi_free_fd(&wfd);
wfd = handle_to_winfd(winusb_handle);
}
- if ( (winusb_handle != 0) && (winusb_handle != INVALID_HANDLE_VALUE)) {
- for (j=0; j<priv->usb_interface[i].nb_endpoints; j++) {
+ if (HANDLE_VALID(winusb_handle)) {
+ for (j = 0; j < priv->usb_interface[i].nb_endpoints; j++) {
usbi_dbg("resetting ep %02X", priv->usb_interface[i].endpoint[j]);
- if (!WinUSBX[sub_api].AbortPipe(winusb_handle, priv->usb_interface[i].endpoint[j])) {
+ if (!WinUSBX[sub_api].AbortPipe(winusb_handle, priv->usb_interface[i].endpoint[j]))
usbi_err(ctx, "AbortPipe (pipe address %02X) failed: %s",
priv->usb_interface[i].endpoint[j], windows_error_str(0));
- }
+
// FlushPipe seems to fail on OUT pipes
if (IS_EPIN(priv->usb_interface[i].endpoint[j])
- && (!WinUSBX[sub_api].FlushPipe(winusb_handle, priv->usb_interface[i].endpoint[j])) ) {
+ && (!WinUSBX[sub_api].FlushPipe(winusb_handle, priv->usb_interface[i].endpoint[j])))
usbi_err(ctx, "FlushPipe (pipe address %02X) failed: %s",
priv->usb_interface[i].endpoint[j], windows_error_str(0));
- }
- if (!WinUSBX[sub_api].ResetPipe(winusb_handle, priv->usb_interface[i].endpoint[j])) {
+
+ if (!WinUSBX[sub_api].ResetPipe(winusb_handle, priv->usb_interface[i].endpoint[j]))
usbi_err(ctx, "ResetPipe (pipe address %02X) failed: %s",
priv->usb_interface[i].endpoint[j], windows_error_str(0));
- }
}
}
}
@@ -2822,10 +2893,10 @@ static int winusbx_reset_device(int sub_api, struct libusb_device_handle *dev_ha
// libusbK & libusb0 have the ability to issue an actual device reset
if (WinUSBX[sub_api].ResetDevice != NULL) {
winusb_handle = handle_priv->interface_handle[0].api_handle;
- if ( (winusb_handle != 0) && (winusb_handle != INVALID_HANDLE_VALUE)) {
+ if (HANDLE_VALID(winusb_handle))
WinUSBX[sub_api].ResetDevice(winusb_handle);
- }
}
+
return LIBUSB_SUCCESS;
}
@@ -2840,19 +2911,20 @@ static int winusbx_copy_transfer_data(int sub_api, struct usbi_transfer *itransf
* Note that functions that complete data transfer synchronously must return
* LIBUSB_COMPLETED instead of LIBUSB_SUCCESS
*/
-static int _hid_get_hid_descriptor(struct hid_device_priv* dev, void *data, size_t *size);
-static int _hid_get_report_descriptor(struct hid_device_priv* dev, void *data, size_t *size);
+static int _hid_get_hid_descriptor(struct hid_device_priv *dev, void *data, size_t *size);
+static int _hid_get_report_descriptor(struct hid_device_priv *dev, void *data, size_t *size);
static int _hid_wcslen(WCHAR *str)
{
int i = 0;
- while (str[i] && (str[i] != 0x409)) {
+
+ while (str[i] && (str[i] != 0x409))
i++;
- }
+
return i;
}
-static int _hid_get_device_descriptor(struct hid_device_priv* dev, void *data, size_t *size)
+static int _hid_get_device_descriptor(struct hid_device_priv *dev, void *data, size_t *size)
{
struct libusb_device_descriptor d;
@@ -2874,10 +2946,11 @@ static int _hid_get_device_descriptor(struct hid_device_priv* dev, void *data, s
if (*size > LIBUSB_DT_DEVICE_SIZE)
*size = LIBUSB_DT_DEVICE_SIZE;
memcpy(data, &d, *size);
+
return LIBUSB_COMPLETED;
}
-static int _hid_get_config_descriptor(struct hid_device_priv* dev, void *data, size_t *size)
+static int _hid_get_config_descriptor(struct hid_device_priv *dev, void *data, size_t *size)
{
char num_endpoints = 0;
size_t config_total_len = 0;
@@ -2896,7 +2969,6 @@ static int _hid_get_config_descriptor(struct hid_device_priv* dev, void *data, s
config_total_len = LIBUSB_DT_CONFIG_SIZE + LIBUSB_DT_INTERFACE_SIZE
+ LIBUSB_DT_HID_SIZE + num_endpoints * LIBUSB_DT_ENDPOINT_SIZE;
-
cd = (struct libusb_config_descriptor *)tmp;
id = (struct libusb_interface_descriptor *)(tmp + LIBUSB_DT_CONFIG_SIZE);
hd = (struct libusb_hid_descriptor *)(tmp + LIBUSB_DT_CONFIG_SIZE
@@ -2907,7 +2979,7 @@ static int _hid_get_config_descriptor(struct hid_device_priv* dev, void *data, s
cd->bLength = LIBUSB_DT_CONFIG_SIZE;
cd->bDescriptorType = LIBUSB_DT_CONFIG;
- cd->wTotalLength = (uint16_t) config_total_len;
+ cd->wTotalLength = (uint16_t)config_total_len;
cd->bNumInterfaces = 1;
cd->bConfigurationValue = 1;
cd->iConfiguration = 0;
@@ -2934,7 +3006,7 @@ static int _hid_get_config_descriptor(struct hid_device_priv* dev, void *data, s
ed->bmAttributes = 3;
ed->wMaxPacketSize = dev->input_report_size - 1;
ed->bInterval = 10;
- ed = (struct libusb_endpoint_descriptor *)((char*)ed + LIBUSB_DT_ENDPOINT_SIZE);
+ ed = (struct libusb_endpoint_descriptor *)((char *)ed + LIBUSB_DT_ENDPOINT_SIZE);
}
if (dev->output_report_size) {
@@ -2949,57 +3021,54 @@ static int _hid_get_config_descriptor(struct hid_device_priv* dev, void *data, s
if (*size > config_total_len)
*size = config_total_len;
memcpy(data, tmp, *size);
+
return LIBUSB_COMPLETED;
}
-static int _hid_get_string_descriptor(struct hid_device_priv* dev, int _index,
- void *data, size_t *size)
+static int _hid_get_string_descriptor(struct hid_device_priv *dev, int _index,
+ void *data, size_t *size)
{
void *tmp = NULL;
size_t tmp_size = 0;
int i;
/* language ID, EN-US */
- char string_langid[] = {
- 0x09,
- 0x04
- };
+ char string_langid[] = {0x09, 0x04};
- if ((*size < 2) || (*size > 255)) {
+ if ((*size < 2) || (*size > 255))
return LIBUSB_ERROR_OVERFLOW;
- }
if (_index == 0) {
tmp = string_langid;
- tmp_size = sizeof(string_langid)+2;
+ tmp_size = sizeof(string_langid) + 2;
} else {
- for (i=0; i<3; i++) {
+ for (i = 0; i < 3; i++) {
if (_index == (dev->string_index[i])) {
tmp = dev->string[i];
- tmp_size = (_hid_wcslen(dev->string[i])+1) * sizeof(WCHAR);
+ tmp_size = (_hid_wcslen(dev->string[i]) + 1) * sizeof(WCHAR);
break;
}
}
- if (i == 3) { // not found
+
+ if (i == 3) // not found
return LIBUSB_ERROR_INVALID_PARAM;
- }
}
- if(!tmp_size) {
+ if (!tmp_size)
return LIBUSB_ERROR_INVALID_PARAM;
- }
- if (tmp_size < *size) {
+ if (tmp_size < *size)
*size = tmp_size;
- }
+
// 2 byte header
- ((uint8_t*)data)[0] = (uint8_t)*size;
- ((uint8_t*)data)[1] = LIBUSB_DT_STRING;
- memcpy((uint8_t*)data+2, tmp, *size-2);
+ ((uint8_t *)data)[0] = (uint8_t)*size;
+ ((uint8_t *)data)[1] = LIBUSB_DT_STRING;
+ memcpy((uint8_t *)data + 2, tmp, *size - 2);
+
return LIBUSB_COMPLETED;
}
-static int _hid_get_hid_descriptor(struct hid_device_priv* dev, void *data, size_t *size)
+static int _hid_get_hid_descriptor(struct hid_device_priv *dev, void *data, size_t *size)
{
struct libusb_hid_descriptor d;
uint8_t tmp[MAX_HID_DESCRIPTOR_SIZE];
@@ -3018,10 +3087,11 @@ static int _hid_get_hid_descriptor(struct hid_device_priv* dev, void *data, size
if (*size > LIBUSB_DT_HID_SIZE)
*size = LIBUSB_DT_HID_SIZE;
memcpy(data, &d, *size);
+
return LIBUSB_COMPLETED;
}
-static int _hid_get_report_descriptor(struct hid_device_priv* dev, void *data, size_t *size)
+static int _hid_get_report_descriptor(struct hid_device_priv *dev, void *data, size_t *size)
{
uint8_t d[MAX_HID_DESCRIPTOR_SIZE];
size_t i = 0;
@@ -3084,11 +3154,12 @@ static int _hid_get_report_descriptor(struct hid_device_priv* dev, void *data, s
if (*size > i)
*size = i;
memcpy(data, d, *size);
+
return LIBUSB_COMPLETED;
}
-static int _hid_get_descriptor(struct hid_device_priv* dev, HANDLE hid_handle, int recipient,
- int type, int _index, void *data, size_t *size)
+static int _hid_get_descriptor(struct hid_device_priv *dev, HANDLE hid_handle, int recipient,
+ int type, int _index, void *data, size_t *size)
{
switch(type) {
case LIBUSB_DT_DEVICE:
@@ -3118,21 +3189,20 @@ static int _hid_get_descriptor(struct hid_device_priv* dev, HANDLE hid_handle, i
return LIBUSB_COMPLETED;
return LIBUSB_ERROR_OTHER;
}
+
usbi_dbg("unsupported");
return LIBUSB_ERROR_NOT_SUPPORTED;
}
-static int _hid_get_report(struct hid_device_priv* dev, HANDLE hid_handle, int id, void *data,
- struct windows_transfer_priv *tp, size_t *size, OVERLAPPED* overlapped,
- int report_type)
+static int _hid_get_report(struct hid_device_priv *dev, HANDLE hid_handle, int id, void *data,
+ struct windows_transfer_priv *tp, size_t *size, OVERLAPPED *overlapped, int report_type)
{
uint8_t *buf;
DWORD ioctl_code, read_size, expected_size = (DWORD)*size;
int r = LIBUSB_SUCCESS;
- if (tp->hid_buffer != NULL) {
+ if (tp->hid_buffer != NULL)
usbi_dbg("program assertion failed: hid_buffer is not NULL");
- }
if ((*size == 0) || (*size > MAX_HID_REPORT_SIZE)) {
usbi_dbg("invalid size (%u)", *size);
@@ -3140,31 +3210,31 @@ static int _hid_get_report(struct hid_device_priv* dev, HANDLE hid_handle, int i
}
switch (report_type) {
- case HID_REPORT_TYPE_INPUT:
- ioctl_code = IOCTL_HID_GET_INPUT_REPORT;
- break;
- case HID_REPORT_TYPE_FEATURE:
- ioctl_code = IOCTL_HID_GET_FEATURE;
- break;
- default:
- usbi_dbg("unknown HID report type %d", report_type);
- return LIBUSB_ERROR_INVALID_PARAM;
+ case HID_REPORT_TYPE_INPUT:
+ ioctl_code = IOCTL_HID_GET_INPUT_REPORT;
+ break;
+ case HID_REPORT_TYPE_FEATURE:
+ ioctl_code = IOCTL_HID_GET_FEATURE;
+ break;
+ default:
+ usbi_dbg("unknown HID report type %d", report_type);
+ return LIBUSB_ERROR_INVALID_PARAM;
}
// Add a trailing byte to detect overflows
- buf = (uint8_t*)calloc(expected_size+1, 1);
- if (buf == NULL) {
+ buf = calloc(1, expected_size + 1);
+ if (buf == NULL)
return LIBUSB_ERROR_NO_MEM;
- }
- buf[0] = (uint8_t)id; // Must be set always
+
+ buf[0] = (uint8_t)id; // Must be set always
usbi_dbg("report ID: 0x%02X", buf[0]);
tp->hid_expected_size = expected_size;
read_size = expected_size;
// NB: The size returned by DeviceIoControl doesn't include report IDs when not in use (0)
- if (!DeviceIoControl(hid_handle, ioctl_code, buf, expected_size+1,
- buf, expected_size+1, &read_size, overlapped)) {
+ if (!DeviceIoControl(hid_handle, ioctl_code, buf, expected_size + 1,
+ buf, expected_size + 1, &read_size, overlapped)) {
if (GetLastError() != ERROR_IO_PENDING) {
usbi_dbg("Failed to Read HID Report: %s", windows_error_str(0));
safe_free(buf);
@@ -3172,7 +3242,7 @@ static int _hid_get_report(struct hid_device_priv* dev, HANDLE hid_handle, int i
}
// Asynchronous wait
tp->hid_buffer = buf;
- tp->hid_dest = (uint8_t*)data; // copy dest, as not necessarily the start of the transfer buffer
+ tp->hid_dest = data; // copy dest, as not necessarily the start of the transfer buffer
return LIBUSB_SUCCESS;
}
@@ -3181,9 +3251,9 @@ static int _hid_get_report(struct hid_device_priv* dev, HANDLE hid_handle, int i
usbi_warn(NULL, "program assertion failed - read completed synchronously, but no data was read");
*size = 0;
} else {
- if (buf[0] != id) {
+ if (buf[0] != id)
usbi_warn(NULL, "mismatched report ID (data is %02X, parameter is %02X)", buf[0], id);
- }
+
if ((size_t)read_size > expected_size) {
r = LIBUSB_ERROR_OVERFLOW;
usbi_dbg("OVERFLOW!");
@@ -3192,27 +3262,24 @@ static int _hid_get_report(struct hid_device_priv* dev, HANDLE hid_handle, int i
}
*size = MIN((size_t)read_size, *size);
- if (id == 0) {
- // Discard report ID
- memcpy(data, buf+1, *size);
- } else {
+ if (id == 0)
+ memcpy(data, buf + 1, *size); // Discard report ID
+ else
memcpy(data, buf, *size);
- }
}
+
safe_free(buf);
return r;
}
-static int _hid_set_report(struct hid_device_priv* dev, HANDLE hid_handle, int id, void *data,
- struct windows_transfer_priv *tp, size_t *size, OVERLAPPED* overlapped,
- int report_type)
+static int _hid_set_report(struct hid_device_priv *dev, HANDLE hid_handle, int id, void *data,
+ struct windows_transfer_priv *tp, size_t *size, OVERLAPPED *overlapped, int report_type)
{
uint8_t *buf = NULL;
- DWORD ioctl_code, write_size= (DWORD)*size;
+ DWORD ioctl_code, write_size = (DWORD)*size;
- if (tp->hid_buffer != NULL) {
+ if (tp->hid_buffer != NULL)
usbi_dbg("program assertion failed: hid_buffer is not NULL");
- }
if ((*size == 0) || (*size > MAX_HID_REPORT_SIZE)) {
usbi_dbg("invalid size (%u)", *size);
@@ -3220,27 +3287,27 @@ static int _hid_set_report(struct hid_device_priv* dev, HANDLE hid_handle, int i
}
switch (report_type) {
- case HID_REPORT_TYPE_OUTPUT:
- ioctl_code = IOCTL_HID_SET_OUTPUT_REPORT;
- break;
- case HID_REPORT_TYPE_FEATURE:
- ioctl_code = IOCTL_HID_SET_FEATURE;
- break;
- default:
- usbi_dbg("unknown HID report type %d", report_type);
- return LIBUSB_ERROR_INVALID_PARAM;
+ case HID_REPORT_TYPE_OUTPUT:
+ ioctl_code = IOCTL_HID_SET_OUTPUT_REPORT;
+ break;
+ case HID_REPORT_TYPE_FEATURE:
+ ioctl_code = IOCTL_HID_SET_FEATURE;
+ break;
+ default:
+ usbi_dbg("unknown HID report type %d", report_type);
+ return LIBUSB_ERROR_INVALID_PARAM;
}
usbi_dbg("report ID: 0x%02X", id);
// When report IDs are not used (i.e. when id == 0), we must add
// a null report ID. Otherwise, we just use original data buffer
- if (id == 0) {
+ if (id == 0)
write_size++;
- }
- buf = (uint8_t*) malloc(write_size);
- if (buf == NULL) {
+
+ buf = malloc(write_size);
+ if (buf == NULL)
return LIBUSB_ERROR_NO_MEM;
- }
+
if (id == 0) {
buf[0] = 0;
memcpy(buf + 1, data, *size);
@@ -3248,9 +3315,8 @@ static int _hid_set_report(struct hid_device_priv* dev, HANDLE hid_handle, int i
// This seems like a waste, but if we don't duplicate the
// data, we'll get issues when freeing hid_buffer
memcpy(buf, data, *size);
- if (buf[0] != id) {
+ if (buf[0] != id)
usbi_warn(NULL, "mismatched report ID (data is %02X, parameter is %02X)", buf[0], id);
- }
}
// NB: The size returned by DeviceIoControl doesn't include report IDs when not in use (0)
@@ -3268,22 +3334,22 @@ static int _hid_set_report(struct hid_device_priv* dev, HANDLE hid_handle, int i
// Transfer completed synchronously
*size = write_size;
- if (write_size == 0) {
+ if (write_size == 0)
usbi_dbg("program assertion failed - write completed synchronously, but no data was written");
- }
+
safe_free(buf);
return LIBUSB_COMPLETED;
}
-static int _hid_class_request(struct hid_device_priv* dev, HANDLE hid_handle, int request_type,
- int request, int value, int _index, void *data, struct windows_transfer_priv *tp,
- size_t *size, OVERLAPPED* overlapped)
+static int _hid_class_request(struct hid_device_priv *dev, HANDLE hid_handle, int request_type,
+ int request, int value, int _index, void *data, struct windows_transfer_priv *tp,
+ size_t *size, OVERLAPPED *overlapped)
{
int report_type = (value >> 8) & 0xFF;
int report_id = value & 0xFF;
- if ( (LIBUSB_REQ_RECIPIENT(request_type) != LIBUSB_RECIPIENT_INTERFACE)
- && (LIBUSB_REQ_RECIPIENT(request_type) != LIBUSB_RECIPIENT_DEVICE) )
+ if ((LIBUSB_REQ_RECIPIENT(request_type) != LIBUSB_RECIPIENT_INTERFACE)
+ && (LIBUSB_REQ_RECIPIENT(request_type) != LIBUSB_RECIPIENT_DEVICE))
return LIBUSB_ERROR_INVALID_PARAM;
if (LIBUSB_REQ_OUT(request_type) && request == HID_REQ_SET_REPORT)
@@ -3335,28 +3401,27 @@ static int hid_open(int sub_api, struct libusb_device_handle *dev_handle)
struct libusb_context *ctx = DEVICE_CTX(dev_handle->dev);
struct windows_device_priv *priv = _device_priv(dev_handle->dev);
struct windows_device_handle_priv *handle_priv = _device_handle_priv(dev_handle);
-
HIDD_ATTRIBUTES hid_attributes;
PHIDP_PREPARSED_DATA preparsed_data = NULL;
HIDP_CAPS capabilities;
HIDP_VALUE_CAPS *value_caps;
-
HANDLE hid_handle = INVALID_HANDLE_VALUE;
int i, j;
// report IDs handling
ULONG size[3];
- const char* type[3] = {"input", "output", "feature"};
- int nb_ids[2]; // zero and nonzero report IDs
+ const char *type[3] = {"input", "output", "feature"};
+ int nb_ids[2]; // zero and nonzero report IDs
CHECK_HID_AVAILABLE;
+
if (priv->hid == NULL) {
usbi_err(ctx, "program assertion failed - private HID structure is unitialized");
return LIBUSB_ERROR_NOT_FOUND;
}
for (i = 0; i < USB_MAXINTERFACES; i++) {
- if ( (priv->usb_interface[i].path != NULL)
- && (priv->usb_interface[i].apib->id == USB_API_HID) ) {
+ if ((priv->usb_interface[i].path != NULL)
+ && (priv->usb_interface[i].apib->id == USB_API_HID)) {
hid_handle = CreateFileA(priv->usb_interface[i].path, GENERIC_WRITE | GENERIC_READ, FILE_SHARE_WRITE | FILE_SHARE_READ,
NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL | FILE_FLAG_OVERLAPPED, NULL);
/*
@@ -3373,7 +3438,7 @@ static int hid_open(int sub_api, struct libusb_device_handle *dev_handle)
if (hid_handle == INVALID_HANDLE_VALUE) {
usbi_err(ctx, "could not open device %s (interface %d): %s", priv->path, i, windows_error_str(0));
switch(GetLastError()) {
- case ERROR_FILE_NOT_FOUND: // The device was disconnected
+ case ERROR_FILE_NOT_FOUND: // The device was disconnected
return LIBUSB_ERROR_NO_DEVICE;
case ERROR_ACCESS_DENIED:
return LIBUSB_ERROR_ACCESS;
@@ -3398,8 +3463,8 @@ static int hid_open(int sub_api, struct libusb_device_handle *dev_handle)
priv->hid->pid = hid_attributes.ProductID;
// Set the maximum available input buffer size
- for (i=32; HidD_SetNumInputBuffers(hid_handle, i); i*=2);
- usbi_dbg("set maximum input buffer size to %d", i/2);
+ for (i = 32; HidD_SetNumInputBuffers(hid_handle, i); i *= 2);
+ usbi_dbg("set maximum input buffer size to %d", i / 2);
// Get the maximum input and output report size
if (!HidD_GetPreparsedData(hid_handle, &preparsed_data) || !preparsed_data) {
@@ -3415,29 +3480,27 @@ static int hid_open(int sub_api, struct libusb_device_handle *dev_handle)
size[0] = capabilities.NumberInputValueCaps;
size[1] = capabilities.NumberOutputValueCaps;
size[2] = capabilities.NumberFeatureValueCaps;
- for (j=HidP_Input; j<=HidP_Feature; j++) {
+ for (j = HidP_Input; j <= HidP_Feature; j++) {
usbi_dbg("%u HID %s report value(s) found", (unsigned int)size[j], type[j]);
priv->hid->uses_report_ids[j] = false;
if (size[j] > 0) {
- value_caps = (HIDP_VALUE_CAPS*) calloc(size[j], sizeof(HIDP_VALUE_CAPS));
- if ( (value_caps != NULL)
- && (HidP_GetValueCaps((HIDP_REPORT_TYPE)j, value_caps, &size[j], preparsed_data) == HIDP_STATUS_SUCCESS)
- && (size[j] >= 1) ) {
+ value_caps = calloc(size[j], sizeof(HIDP_VALUE_CAPS));
+ if ((value_caps != NULL)
+ && (HidP_GetValueCaps((HIDP_REPORT_TYPE)j, value_caps, &size[j], preparsed_data) == HIDP_STATUS_SUCCESS)
+ && (size[j] >= 1)) {
nb_ids[0] = 0;
nb_ids[1] = 0;
- for (i=0; i<(int)size[j]; i++) {
+ for (i = 0; i < (int)size[j]; i++) {
usbi_dbg(" Report ID: 0x%02X", value_caps[i].ReportID);
- if (value_caps[i].ReportID != 0) {
+ if (value_caps[i].ReportID != 0)
nb_ids[1]++;
- } else {
+ else
nb_ids[0]++;
- }
}
if (nb_ids[1] != 0) {
- if (nb_ids[0] != 0) {
+ if (nb_ids[0] != 0)
usbi_warn(ctx, "program assertion failed: zero and nonzero report IDs used for %s",
type[j]);
- }
priv->hid->uses_report_ids[j] = true;
}
} else {
@@ -3454,31 +3517,26 @@ static int hid_open(int sub_api, struct libusb_device_handle *dev_handle)
// Fetch string descriptors
priv->hid->string_index[0] = priv->dev_descriptor.iManufacturer;
- if (priv->hid->string_index[0] != 0) {
- HidD_GetManufacturerString(hid_handle, priv->hid->string[0],
- sizeof(priv->hid->string[0]));
- } else {
+ if (priv->hid->string_index[0] != 0)
+ HidD_GetManufacturerString(hid_handle, priv->hid->string[0], sizeof(priv->hid->string[0]));
+ else
priv->hid->string[0][0] = 0;
- }
+
priv->hid->string_index[1] = priv->dev_descriptor.iProduct;
- if (priv->hid->string_index[1] != 0) {
- HidD_GetProductString(hid_handle, priv->hid->string[1],
- sizeof(priv->hid->string[1]));
- } else {
+ if (priv->hid->string_index[1] != 0)
+ HidD_GetProductString(hid_handle, priv->hid->string[1], sizeof(priv->hid->string[1]));
+ else
priv->hid->string[1][0] = 0;
- }
+
priv->hid->string_index[2] = priv->dev_descriptor.iSerialNumber;
- if (priv->hid->string_index[2] != 0) {
- HidD_GetSerialNumberString(hid_handle, priv->hid->string[2],
- sizeof(priv->hid->string[2]));
- } else {
+ if (priv->hid->string_index[2] != 0)
+ HidD_GetSerialNumberString(hid_handle, priv->hid->string[2], sizeof(priv->hid->string[2]));
+ else
priv->hid->string[2][0] = 0;
- }
} while(0);
- if (preparsed_data) {
+ if (preparsed_data)
HidD_FreePreparsedData(preparsed_data);
- }
return LIBUSB_SUCCESS;
}
@@ -3496,9 +3554,8 @@ static void hid_close(int sub_api, struct libusb_device_handle *dev_handle)
for (i = 0; i < USB_MAXINTERFACES; i++) {
if (priv->usb_interface[i].apib->id == USB_API_HID) {
file_handle = handle_priv->interface_handle[i].api_handle;
- if ( (file_handle != 0) && (file_handle != INVALID_HANDLE_VALUE)) {
+ if (HANDLE_VALID(file_handle))
CloseHandle(file_handle);
- }
}
}
}
@@ -3511,14 +3568,13 @@ static int hid_claim_interface(int sub_api, struct libusb_device_handle *dev_han
CHECK_HID_AVAILABLE;
// NB: Disconnection detection is not possible in this function
- if (priv->usb_interface[iface].path == NULL) {
- return LIBUSB_ERROR_NOT_FOUND; // invalid iface
- }
+ if (priv->usb_interface[iface].path == NULL)
+ return LIBUSB_ERROR_NOT_FOUND; // invalid iface
// We use dev_handle as a flag for interface claimed
- if (handle_priv->interface_handle[iface].dev_handle == INTERFACE_CLAIMED) {
- return LIBUSB_ERROR_BUSY; // already claimed
- }
+ if (handle_priv->interface_handle[iface].dev_handle == INTERFACE_CLAIMED)
+ return LIBUSB_ERROR_BUSY; // already claimed
+
handle_priv->interface_handle[iface].dev_handle = INTERFACE_CLAIMED;
@@ -3535,13 +3591,11 @@ static int hid_release_interface(int sub_api, struct libusb_device_handle *dev_h
CHECK_HID_AVAILABLE;
- if (priv->usb_interface[iface].path == NULL) {
- return LIBUSB_ERROR_NOT_FOUND; // invalid iface
- }
+ if (priv->usb_interface[iface].path == NULL)
+ return LIBUSB_ERROR_NOT_FOUND; // invalid iface
- if (handle_priv->interface_handle[iface].dev_handle != INTERFACE_CLAIMED) {
- return LIBUSB_ERROR_NOT_FOUND; // invalid iface
- }
+ if (handle_priv->interface_handle[iface].dev_handle != INTERFACE_CLAIMED)
+ return LIBUSB_ERROR_NOT_FOUND; // invalid iface
handle_priv->interface_handle[iface].dev_handle = INVALID_HANDLE_VALUE;
@@ -3554,9 +3608,8 @@ static int hid_set_interface_altsetting(int sub_api, struct libusb_device_handle
CHECK_HID_AVAILABLE;
- if (altsetting > 255) {
+ if (altsetting > 255)
return LIBUSB_ERROR_INVALID_PARAM;
- }
if (altsetting != 0) {
usbi_err(ctx, "set interface altsetting not supported for altsetting >0");
@@ -3569,11 +3622,11 @@ static int hid_set_interface_altsetting(int sub_api, struct libusb_device_handle
static int hid_submit_control_transfer(int sub_api, struct usbi_transfer *itransfer)
{
struct libusb_transfer *transfer = USBI_TRANSFER_TO_LIBUSB_TRANSFER(itransfer);
- struct windows_transfer_priv *transfer_priv = (struct windows_transfer_priv*)usbi_transfer_get_os_priv(itransfer);
+ struct windows_transfer_priv *transfer_priv = usbi_transfer_get_os_priv(itransfer);
struct windows_device_handle_priv *handle_priv = _device_handle_priv(transfer->dev_handle);
struct windows_device_priv *priv = _device_priv(transfer->dev_handle->dev);
struct libusb_context *ctx = DEVICE_CTX(transfer->dev_handle->dev);
- WINUSB_SETUP_PACKET *setup = (WINUSB_SETUP_PACKET *) transfer->buffer;
+ WINUSB_SETUP_PACKET *setup = (WINUSB_SETUP_PACKET *)transfer->buffer;
HANDLE hid_handle;
struct winfd wfd;
int current_interface, config;
@@ -3587,24 +3640,21 @@ static int hid_submit_control_transfer(int sub_api, struct usbi_transfer *itrans
transfer_priv->hid_dest = NULL;
size = transfer->length - LIBUSB_CONTROL_SETUP_SIZE;
- if (size > MAX_CTRL_BUFFER_LENGTH) {
+ if (size > MAX_CTRL_BUFFER_LENGTH)
return LIBUSB_ERROR_INVALID_PARAM;
- }
current_interface = get_valid_interface(transfer->dev_handle, USB_API_HID);
if (current_interface < 0) {
- if (auto_claim(transfer, &current_interface, USB_API_HID) != LIBUSB_SUCCESS) {
+ if (auto_claim(transfer, &current_interface, USB_API_HID) != LIBUSB_SUCCESS)
return LIBUSB_ERROR_NOT_FOUND;
- }
}
usbi_dbg("will use interface %d", current_interface);
hid_handle = handle_priv->interface_handle[current_interface].api_handle;
// Always use the handle returned from usbi_create_fd (wfd.handle)
wfd = usbi_create_fd(hid_handle, RW_READ, NULL, NULL);
- if (wfd.fd < 0) {
+ if (wfd.fd < 0)
return LIBUSB_ERROR_NOT_FOUND;
- }
switch(LIBUSB_REQ_TYPE(setup->request_type)) {
case LIBUSB_REQUEST_TYPE_STANDARD:
@@ -3617,7 +3667,7 @@ static int hid_submit_control_transfer(int sub_api, struct usbi_transfer *itrans
r = windows_get_configuration(transfer->dev_handle, &config);
if (r == LIBUSB_SUCCESS) {
size = 1;
- ((uint8_t*)transfer->buffer)[LIBUSB_CONTROL_SETUP_SIZE] = (uint8_t)config;
+ ((uint8_t *)transfer->buffer)[LIBUSB_CONTROL_SETUP_SIZE] = (uint8_t)config;
r = LIBUSB_COMPLETED;
}
break;
@@ -3631,14 +3681,13 @@ static int hid_submit_control_transfer(int sub_api, struct usbi_transfer *itrans
break;
case LIBUSB_REQUEST_GET_INTERFACE:
size = 1;
- ((uint8_t*)transfer->buffer)[LIBUSB_CONTROL_SETUP_SIZE] = 0;
+ ((uint8_t *)transfer->buffer)[LIBUSB_CONTROL_SETUP_SIZE] = 0;
r = LIBUSB_COMPLETED;
break;
case LIBUSB_REQUEST_SET_INTERFACE:
r = hid_set_interface_altsetting(0, transfer->dev_handle, setup->index, setup->value);
- if (r == LIBUSB_SUCCESS) {
+ if (r == LIBUSB_SUCCESS)
r = LIBUSB_COMPLETED;
- }
break;
default:
usbi_warn(ctx, "unsupported HID control request");
@@ -3647,7 +3696,7 @@ static int hid_submit_control_transfer(int sub_api, struct usbi_transfer *itrans
}
break;
case LIBUSB_REQUEST_TYPE_CLASS:
- r =_hid_class_request(priv->hid, wfd.handle, setup->request_type, setup->request, setup->value,
+ r = _hid_class_request(priv->hid, wfd.handle, setup->request_type, setup->request, setup->value,
setup->index, transfer->buffer + LIBUSB_CONTROL_SETUP_SIZE, transfer_priv,
&size, wfd.overlapped);
break;
@@ -3677,9 +3726,10 @@ static int hid_submit_control_transfer(int sub_api, struct usbi_transfer *itrans
return r;
}
-static int hid_submit_bulk_transfer(int sub_api, struct usbi_transfer *itransfer) {
+static int hid_submit_bulk_transfer(int sub_api, struct usbi_transfer *itransfer)
+{
struct libusb_transfer *transfer = USBI_TRANSFER_TO_LIBUSB_TRANSFER(itransfer);
- struct windows_transfer_priv *transfer_priv = (struct windows_transfer_priv*)usbi_transfer_get_os_priv(itransfer);
+ struct windows_transfer_priv *transfer_priv = usbi_transfer_get_os_priv(itransfer);
struct libusb_context *ctx = DEVICE_CTX(transfer->dev_handle->dev);
struct windows_device_handle_priv *handle_priv = _device_handle_priv(transfer->dev_handle);
struct windows_device_priv *priv = _device_priv(transfer->dev_handle->dev);
@@ -3709,38 +3759,38 @@ static int hid_submit_bulk_transfer(int sub_api, struct usbi_transfer *itransfer
wfd = usbi_create_fd(hid_handle, direction_in?RW_READ:RW_WRITE, NULL, NULL);
// Always use the handle returned from usbi_create_fd (wfd.handle)
- if (wfd.fd < 0) {
+ if (wfd.fd < 0)
return LIBUSB_ERROR_NO_MEM;
- }
// If report IDs are not in use, an extra prefix byte must be added
- if ( ((direction_in) && (!priv->hid->uses_report_ids[0]))
- || ((!direction_in) && (!priv->hid->uses_report_ids[1])) ) {
- length = transfer->length+1;
- } else {
+ if (((direction_in) && (!priv->hid->uses_report_ids[0]))
+ || ((!direction_in) && (!priv->hid->uses_report_ids[1])))
+ length = transfer->length + 1;
+ else
length = transfer->length;
- }
+
// Add a trailing byte to detect overflows on input
- transfer_priv->hid_buffer = (uint8_t*)calloc(length+1, 1);
- if (transfer_priv->hid_buffer == NULL) {
+ transfer_priv->hid_buffer = calloc(1, length + 1);
+ if (transfer_priv->hid_buffer == NULL)
return LIBUSB_ERROR_NO_MEM;
- }
+
transfer_priv->hid_expected_size = length;
if (direction_in) {
transfer_priv->hid_dest = transfer->buffer;
usbi_dbg("reading %d bytes (report ID: 0x00)", length);
- ret = ReadFile(wfd.handle, transfer_priv->hid_buffer, length+1, &size, wfd.overlapped);
+ ret = ReadFile(wfd.handle, transfer_priv->hid_buffer, length + 1, &size, wfd.overlapped);
} else {
- if (!priv->hid->uses_report_ids[1]) {
- memcpy(transfer_priv->hid_buffer+1, transfer->buffer, transfer->length);
- } else {
+ if (!priv->hid->uses_report_ids[1])
+ memcpy(transfer_priv->hid_buffer + 1, transfer->buffer, transfer->length);
+ else
// We could actually do without the calloc and memcpy in this case
memcpy(transfer_priv->hid_buffer, transfer->buffer, transfer->length);
- }
+
usbi_dbg("writing %d bytes (report ID: 0x%02X)", length, transfer_priv->hid_buffer[0]);
ret = WriteFile(wfd.handle, transfer_priv->hid_buffer, length, &size, wfd.overlapped);
}
+
if (!ret) {
if (GetLastError() != ERROR_IO_PENDING) {
usbi_err(ctx, "HID transfer failed: %s", windows_error_str(0));
@@ -3751,9 +3801,9 @@ static int hid_submit_bulk_transfer(int sub_api, struct usbi_transfer *itransfer
} else {
// Only write operations that completed synchronously need to free up
// hid_buffer. For reads, copy_transfer_data() handles that process.
- if (!direction_in) {
+ if (!direction_in)
safe_free(transfer_priv->hid_buffer);
- }
+
if (size == 0) {
usbi_err(ctx, "program assertion failed - no data was transferred");
size = 1;
@@ -3775,7 +3825,7 @@ static int hid_submit_bulk_transfer(int sub_api, struct usbi_transfer *itransfer
static int hid_abort_transfers(int sub_api, struct usbi_transfer *itransfer)
{
struct libusb_transfer *transfer = USBI_TRANSFER_TO_LIBUSB_TRANSFER(itransfer);
- struct windows_transfer_priv *transfer_priv = (struct windows_transfer_priv*)usbi_transfer_get_os_priv(itransfer);
+ struct windows_transfer_priv *transfer_priv = usbi_transfer_get_os_priv(itransfer);
struct windows_device_handle_priv *handle_priv = _device_handle_priv(transfer->dev_handle);
HANDLE hid_handle;
int current_interface;
@@ -3800,10 +3850,10 @@ static int hid_reset_device(int sub_api, struct libusb_device_handle *dev_handle
// Flushing the queues on all interfaces is the best we can achieve
for (current_interface = 0; current_interface < USB_MAXINTERFACES; current_interface++) {
hid_handle = handle_priv->interface_handle[current_interface].api_handle;
- if ((hid_handle != 0) && (hid_handle != INVALID_HANDLE_VALUE)) {
+ if (HANDLE_VALID(hid_handle))
HidD_FlushQueue(hid_handle);
- }
}
+
return LIBUSB_SUCCESS;
}
@@ -3838,7 +3888,8 @@ static int hid_clear_halt(int sub_api, struct libusb_device_handle *dev_handle,
}
// This extra function is only needed for HID
-static int hid_copy_transfer_data(int sub_api, struct usbi_transfer *itransfer, uint32_t io_size) {
+static int hid_copy_transfer_data(int sub_api, struct usbi_transfer *itransfer, uint32_t io_size)
+{
struct libusb_transfer *transfer = USBI_TRANSFER_TO_LIBUSB_TRANSFER(itransfer);
struct libusb_context *ctx = DEVICE_CTX(transfer->dev_handle->dev);
struct windows_transfer_priv *transfer_priv = usbi_transfer_get_os_priv(itransfer);
@@ -3847,7 +3898,7 @@ static int hid_copy_transfer_data(int sub_api, struct usbi_transfer *itransfer,
if (transfer_priv->hid_buffer != NULL) {
// If we have a valid hid_buffer, it means the transfer was async
- if (transfer_priv->hid_dest != NULL) { // Data readout
+ if (transfer_priv->hid_dest != NULL) { // Data readout
if (corrected_size > 0) {
// First, check for overflow
if (corrected_size > transfer_priv->hid_expected_size) {
@@ -3859,7 +3910,7 @@ static int hid_copy_transfer_data(int sub_api, struct usbi_transfer *itransfer,
if (transfer_priv->hid_buffer[0] == 0) {
// Discard the 1 byte report ID prefix
corrected_size--;
- memcpy(transfer_priv->hid_dest, transfer_priv->hid_buffer+1, corrected_size);
+ memcpy(transfer_priv->hid_dest, transfer_priv->hid_buffer + 1, corrected_size);
} else {
memcpy(transfer_priv->hid_dest, transfer_priv->hid_buffer, corrected_size);
}
@@ -3869,6 +3920,7 @@ static int hid_copy_transfer_data(int sub_api, struct usbi_transfer *itransfer,
// For write, we just need to free the hid buffer
safe_free(transfer_priv->hid_buffer);
}
+
itransfer->transferred += corrected_size;
return r;
}
@@ -3892,10 +3944,10 @@ static int composite_open(int sub_api, struct libusb_device_handle *dev_handle)
struct windows_device_priv *priv = _device_priv(dev_handle->dev);
int r = LIBUSB_ERROR_NOT_FOUND;
uint8_t i;
- // SUB_API_MAX+1 as the SUB_API_MAX pos is used to indicate availability of HID
- bool available[SUB_API_MAX+1] = {0};
+ // SUB_API_MAX + 1 as the SUB_API_MAX pos is used to indicate availability of HID
+ bool available[SUB_API_MAX + 1] = { 0 };
- for (i=0; i<USB_MAXINTERFACES; i++) {
+ for (i = 0; i < USB_MAXINTERFACES; i++) {
switch (priv->usb_interface[i].apib->id) {
case USB_API_WINUSBX:
if (priv->usb_interface[i].sub_api != SUB_API_NOTSET)
@@ -3909,17 +3961,17 @@ static int composite_open(int sub_api, struct libusb_device_handle *dev_handle)
}
}
- for (i=0; i<SUB_API_MAX; i++) { // WinUSB-like drivers
+ for (i = 0; i < SUB_API_MAX; i++) { // WinUSB-like drivers
if (available[i]) {
r = usb_api_backend[USB_API_WINUSBX].open(i, dev_handle);
- if (r != LIBUSB_SUCCESS) {
+ if (r != LIBUSB_SUCCESS)
return r;
- }
}
}
- if (available[SUB_API_MAX]) { // HID driver
+
+ if (available[SUB_API_MAX]) // HID driver
r = hid_open(SUB_API_NOTSET, dev_handle);
- }
+
return r;
}
@@ -3927,10 +3979,10 @@ static void composite_close(int sub_api, struct libusb_device_handle *dev_handle
{
struct windows_device_priv *priv = _device_priv(dev_handle->dev);
uint8_t i;
- // SUB_API_MAX+1 as the SUB_API_MAX pos is used to indicate availability of HID
- bool available[SUB_API_MAX+1] = {0};
+ // SUB_API_MAX + 1 as the SUB_API_MAX pos is used to indicate availability of HID
+ bool available[SUB_API_MAX + 1] = { 0 };
- for (i=0; i<USB_MAXINTERFACES; i++) {
+ for (i = 0; i < USB_MAXINTERFACES; i++) {
switch (priv->usb_interface[i].apib->id) {
case USB_API_WINUSBX:
if (priv->usb_interface[i].sub_api != SUB_API_NOTSET)
@@ -3944,19 +3996,19 @@ static void composite_close(int sub_api, struct libusb_device_handle *dev_handle
}
}
- for (i=0; i<SUB_API_MAX; i++) { // WinUSB-like drivers
- if (available[i]) {
+ for (i = 0; i < SUB_API_MAX; i++) { // WinUSB-like drivers
+ if (available[i])
usb_api_backend[USB_API_WINUSBX].close(i, dev_handle);
- }
}
- if (available[SUB_API_MAX]) { // HID driver
+
+ if (available[SUB_API_MAX]) // HID driver
hid_close(SUB_API_NOTSET, dev_handle);
- }
}
static int composite_claim_interface(int sub_api, struct libusb_device_handle *dev_handle, int iface)
{
struct windows_device_priv *priv = _device_priv(dev_handle->dev);
+
return priv->usb_interface[iface].apib->
claim_interface(priv->usb_interface[iface].sub_api, dev_handle, iface);
}
@@ -3964,6 +4016,7 @@ static int composite_claim_interface(int sub_api, struct libusb_device_handle *d
static int composite_set_interface_altsetting(int sub_api, struct libusb_device_handle *dev_handle, int iface, int altsetting)
{
struct windows_device_priv *priv = _device_priv(dev_handle->dev);
+
return priv->usb_interface[iface].apib->
set_interface_altsetting(priv->usb_interface[iface].sub_api, dev_handle, iface, altsetting);
}
@@ -3971,6 +4024,7 @@ static int composite_set_interface_altsetting(int sub_api, struct libusb_device_
static int composite_release_interface(int sub_api, struct libusb_device_handle *dev_handle, int iface)
{
struct windows_device_priv *priv = _device_priv(dev_handle->dev);
+
return priv->usb_interface[iface].apib->
release_interface(priv->usb_interface[iface].sub_api, dev_handle, iface);
}
@@ -3981,7 +4035,7 @@ static int composite_submit_control_transfer(int sub_api, struct usbi_transfer *
struct libusb_context *ctx = DEVICE_CTX(transfer->dev_handle->dev);
struct windows_device_priv *priv = _device_priv(transfer->dev_handle->dev);
struct libusb_config_descriptor *conf_desc;
- WINUSB_SETUP_PACKET *setup = (WINUSB_SETUP_PACKET *) transfer->buffer;
+ WINUSB_SETUP_PACKET *setup = (WINUSB_SETUP_PACKET *)transfer->buffer;
int iface, pass, r;
// Interface shouldn't matter for control, but it does in practice, with Windows'
@@ -3992,7 +4046,7 @@ static int composite_submit_control_transfer(int sub_api, struct usbi_transfer *
iface = setup->index & 0xFF;
break;
case LIBUSB_RECIPIENT_ENDPOINT:
- r = libusb_get_config_descriptor(transfer->dev_handle->dev, (uint8_t)(priv->active_config-1), &conf_desc);
+ r = libusb_get_config_descriptor(transfer->dev_handle->dev, (uint8_t)(priv->active_config - 1), &conf_desc);
if (r == LIBUSB_SUCCESS) {
iface = get_interface_by_endpoint(conf_desc, (setup->index & 0xFF));
libusb_free_config_descriptor(conf_desc);
@@ -4009,9 +4063,8 @@ static int composite_submit_control_transfer(int sub_api, struct usbi_transfer *
usbi_dbg("attempting control transfer targeted to interface %d", iface);
if (priv->usb_interface[iface].path != NULL) {
r = priv->usb_interface[iface].apib->submit_control_transfer(priv->usb_interface[iface].sub_api, itransfer);
- if (r == LIBUSB_SUCCESS) {
+ if (r == LIBUSB_SUCCESS)
return r;
- }
}
}
@@ -4027,9 +4080,8 @@ static int composite_submit_control_transfer(int sub_api, struct usbi_transfer *
usbi_dbg("using interface %d", iface);
r = priv->usb_interface[iface].apib->submit_control_transfer(priv->usb_interface[iface].sub_api, itransfer);
// If not supported on this API, it may be supported on another, so don't give up yet!!
- if (r == LIBUSB_ERROR_NOT_SUPPORTED) {
+ if (r == LIBUSB_ERROR_NOT_SUPPORTED)
continue;
- }
return r;
}
}
@@ -4053,7 +4105,8 @@ static int composite_submit_bulk_transfer(int sub_api, struct usbi_transfer *itr
}
return priv->usb_interface[current_interface].apib->
- submit_bulk_transfer(priv->usb_interface[current_interface].sub_api, itransfer);}
+ submit_bulk_transfer(priv->usb_interface[current_interface].sub_api, itransfer);
+}
static int composite_submit_iso_transfer(int sub_api, struct usbi_transfer *itransfer) {
struct libusb_transfer *transfer = USBI_TRANSFER_TO_LIBUSB_TRANSFER(itransfer);
@@ -4069,7 +4122,8 @@ static int composite_submit_iso_transfer(int sub_api, struct usbi_transfer *itra
}
return priv->usb_interface[current_interface].apib->
- submit_iso_transfer(priv->usb_interface[current_interface].sub_api, itransfer);}
+ submit_iso_transfer(priv->usb_interface[current_interface].sub_api, itransfer);
+}
static int composite_clear_halt(int sub_api, struct libusb_device_handle *dev_handle, unsigned char endpoint)
{
@@ -4085,7 +4139,8 @@ static int composite_clear_halt(int sub_api, struct libusb_device_handle *dev_ha
}
return priv->usb_interface[current_interface].apib->
- clear_halt(priv->usb_interface[current_interface].sub_api, dev_handle, endpoint);}
+ clear_halt(priv->usb_interface[current_interface].sub_api, dev_handle, endpoint);
+}
static int composite_abort_control(int sub_api, struct usbi_transfer *itransfer)
{
@@ -4094,7 +4149,8 @@ static int composite_abort_control(int sub_api, struct usbi_transfer *itransfer)
struct windows_device_priv *priv = _device_priv(transfer->dev_handle->dev);
return priv->usb_interface[transfer_priv->interface_number].apib->
- abort_control(priv->usb_interface[transfer_priv->interface_number].sub_api, itransfer);}
+ abort_control(priv->usb_interface[transfer_priv->interface_number].sub_api, itransfer);
+}
static int composite_abort_transfers(int sub_api, struct usbi_transfer *itransfer)
{
@@ -4103,7 +4159,8 @@ static int composite_abort_transfers(int sub_api, struct usbi_transfer *itransfe
struct windows_device_priv *priv = _device_priv(transfer->dev_handle->dev);
return priv->usb_interface[transfer_priv->interface_number].apib->
- abort_transfers(priv->usb_interface[transfer_priv->interface_number].sub_api, itransfer);}
+ abort_transfers(priv->usb_interface[transfer_priv->interface_number].sub_api, itransfer);
+}
static int composite_reset_device(int sub_api, struct libusb_device_handle *dev_handle)
{
@@ -4111,23 +4168,24 @@ static int composite_reset_device(int sub_api, struct libusb_device_handle *dev_
int r;
uint8_t i;
bool available[SUB_API_MAX];
- for (i = 0; i<SUB_API_MAX; i++) {
+
+ for (i = 0; i < SUB_API_MAX; i++)
available[i] = false;
- }
- for (i=0; i<USB_MAXINTERFACES; i++) {
- if ( (priv->usb_interface[i].apib->id == USB_API_WINUSBX)
- && (priv->usb_interface[i].sub_api != SUB_API_NOTSET) ) {
+
+ for (i = 0; i < USB_MAXINTERFACES; i++) {
+ if ((priv->usb_interface[i].apib->id == USB_API_WINUSBX)
+ && (priv->usb_interface[i].sub_api != SUB_API_NOTSET))
available[priv->usb_interface[i].sub_api] = true;
- }
}
- for (i=0; i<SUB_API_MAX; i++) {
+
+ for (i = 0; i < SUB_API_MAX; i++) {
if (available[i]) {
r = usb_api_backend[USB_API_WINUSBX].reset_device(i, dev_handle);
- if (r != LIBUSB_SUCCESS) {
+ if (r != LIBUSB_SUCCESS)
return r;
- }
}
}
+
return LIBUSB_SUCCESS;
}
diff --git a/libusb/os/windows_winusb.h b/libusb/os/windows_winusb.h
index 85baf11..8f53c4a 100644
--- a/libusb/os/windows_winusb.h
+++ b/libusb/os/windows_winusb.h
@@ -37,7 +37,7 @@
// Missing from MSVC6 setupapi.h
#if !defined(SPDRP_ADDRESS)
-#define SPDRP_ADDRESS 28
+#define SPDRP_ADDRESS 28
#endif
#if !defined(SPDRP_INSTALL_STATE)
#define SPDRP_INSTALL_STATE 34
@@ -56,20 +56,20 @@
#define _beginthreadex(a, b, c, d, e, f) CreateThread(a, b, (LPTHREAD_START_ROUTINE)c, d, e, (LPDWORD)f)
#endif
-#define MAX_CTRL_BUFFER_LENGTH 4096
-#define MAX_USB_DEVICES 256
-#define MAX_USB_STRING_LENGTH 128
-#define MAX_HID_REPORT_SIZE 1024
-#define MAX_HID_DESCRIPTOR_SIZE 256
-#define MAX_GUID_STRING_LENGTH 40
-#define MAX_PATH_LENGTH 128
-#define MAX_KEY_LENGTH 256
-#define LIST_SEPARATOR ';'
+#define MAX_CTRL_BUFFER_LENGTH 4096
+#define MAX_USB_DEVICES 256
+#define MAX_USB_STRING_LENGTH 128
+#define MAX_HID_REPORT_SIZE 1024
+#define MAX_HID_DESCRIPTOR_SIZE 256
+#define MAX_GUID_STRING_LENGTH 40
+#define MAX_PATH_LENGTH 128
+#define MAX_KEY_LENGTH 256
+#define LIST_SEPARATOR ';'
// Handle code for HID interface that have been claimed ("dibs")
-#define INTERFACE_CLAIMED ((HANDLE)(intptr_t)0xD1B5)
+#define INTERFACE_CLAIMED ((HANDLE)(intptr_t)0xD1B5)
// Additional return code for HID operations that completed synchronously
-#define LIBUSB_COMPLETED (LIBUSB_SUCCESS + 1)
+#define LIBUSB_COMPLETED (LIBUSB_SUCCESS + 1)
// http://msdn.microsoft.com/en-us/library/ff545978.aspx
// http://msdn.microsoft.com/en-us/library/ff545972.aspx
@@ -91,28 +91,28 @@ const GUID GUID_DEVINTERFACE_LIBUSB0_FILTER = { 0xF9F3FF14, 0xAE21, 0x48A0, {0x8
/*
* Multiple USB API backend support
*/
-#define USB_API_UNSUPPORTED 0
-#define USB_API_HUB 1
-#define USB_API_COMPOSITE 2
-#define USB_API_WINUSBX 3
-#define USB_API_HID 4
-#define USB_API_MAX 5
+#define USB_API_UNSUPPORTED 0
+#define USB_API_HUB 1
+#define USB_API_COMPOSITE 2
+#define USB_API_WINUSBX 3
+#define USB_API_HID 4
+#define USB_API_MAX 5
// The following is used to indicate if the HID or composite extra props have already been set.
-#define USB_API_SET (1<<USB_API_MAX)
+#define USB_API_SET (1 << USB_API_MAX)
// Sub-APIs for WinUSB-like driver APIs (WinUSB, libusbK, libusb-win32 through the libusbK DLL)
// Must have the same values as the KUSB_DRVID enum from libusbk.h
-#define SUB_API_NOTSET -1
-#define SUB_API_LIBUSBK 0
-#define SUB_API_LIBUSB0 1
-#define SUB_API_WINUSB 2
-#define SUB_API_MAX 3
+#define SUB_API_NOTSET -1
+#define SUB_API_LIBUSBK 0
+#define SUB_API_LIBUSB0 1
+#define SUB_API_WINUSB 2
+#define SUB_API_MAX 3
-#define WINUSBX_DRV_NAMES { "libusbK", "libusb0", "WinUSB"}
+#define WINUSBX_DRV_NAMES {"libusbK", "libusb0", "WinUSB"}
struct windows_usb_api_backend {
const uint8_t id;
- const char* designation;
+ const char *designation;
const char **driver_name_list; // Driver name, without .sys, e.g. "usbccgp"
const uint8_t nb_driver_names;
int (*init)(int sub_api, struct libusb_context *ctx);
@@ -135,9 +135,9 @@ struct windows_usb_api_backend {
extern const struct windows_usb_api_backend usb_api_backend[USB_API_MAX];
-#define PRINT_UNSUPPORTED_API(fname) \
- usbi_dbg("unsupported API call for '" \
- #fname "' (unrecognized device driver)"); \
+#define PRINT_UNSUPPORTED_API(fname) \
+ usbi_dbg("unsupported API call for '" \
+ #fname "' (unrecognized device driver)"); \
return LIBUSB_ERROR_NOT_SUPPORTED;
/*
@@ -147,39 +147,40 @@ extern const struct windows_usb_api_backend usb_api_backend[USB_API_MAX];
// TODO (v2+): move hid desc to libusb.h?
struct libusb_hid_descriptor {
- uint8_t bLength;
- uint8_t bDescriptorType;
+ uint8_t bLength;
+ uint8_t bDescriptorType;
uint16_t bcdHID;
- uint8_t bCountryCode;
- uint8_t bNumDescriptors;
- uint8_t bClassDescriptorType;
+ uint8_t bCountryCode;
+ uint8_t bNumDescriptors;
+ uint8_t bClassDescriptorType;
uint16_t wClassDescriptorLength;
};
-#define LIBUSB_DT_HID_SIZE 9
+
+#define LIBUSB_DT_HID_SIZE 9
#define HID_MAX_CONFIG_DESC_SIZE (LIBUSB_DT_CONFIG_SIZE + LIBUSB_DT_INTERFACE_SIZE \
+ LIBUSB_DT_HID_SIZE + 2 * LIBUSB_DT_ENDPOINT_SIZE)
-#define HID_MAX_REPORT_SIZE 1024
-#define HID_IN_EP 0x81
-#define HID_OUT_EP 0x02
-#define LIBUSB_REQ_RECIPIENT(request_type) ((request_type) & 0x1F)
-#define LIBUSB_REQ_TYPE(request_type) ((request_type) & (0x03 << 5))
-#define LIBUSB_REQ_IN(request_type) ((request_type) & LIBUSB_ENDPOINT_IN)
-#define LIBUSB_REQ_OUT(request_type) (!LIBUSB_REQ_IN(request_type))
+#define HID_MAX_REPORT_SIZE 1024
+#define HID_IN_EP 0x81
+#define HID_OUT_EP 0x02
+#define LIBUSB_REQ_RECIPIENT(request_type) ((request_type) & 0x1F)
+#define LIBUSB_REQ_TYPE(request_type) ((request_type) & (0x03 << 5))
+#define LIBUSB_REQ_IN(request_type) ((request_type) & LIBUSB_ENDPOINT_IN)
+#define LIBUSB_REQ_OUT(request_type) (!LIBUSB_REQ_IN(request_type))
// The following are used for HID reports IOCTLs
#define HID_CTL_CODE(id) \
- CTL_CODE (FILE_DEVICE_KEYBOARD, (id), METHOD_NEITHER, FILE_ANY_ACCESS)
+ CTL_CODE (FILE_DEVICE_KEYBOARD, (id), METHOD_NEITHER, FILE_ANY_ACCESS)
#define HID_BUFFER_CTL_CODE(id) \
- CTL_CODE (FILE_DEVICE_KEYBOARD, (id), METHOD_BUFFERED, FILE_ANY_ACCESS)
+ CTL_CODE (FILE_DEVICE_KEYBOARD, (id), METHOD_BUFFERED, FILE_ANY_ACCESS)
#define HID_IN_CTL_CODE(id) \
- CTL_CODE (FILE_DEVICE_KEYBOARD, (id), METHOD_IN_DIRECT, FILE_ANY_ACCESS)
+ CTL_CODE (FILE_DEVICE_KEYBOARD, (id), METHOD_IN_DIRECT, FILE_ANY_ACCESS)
#define HID_OUT_CTL_CODE(id) \
- CTL_CODE (FILE_DEVICE_KEYBOARD, (id), METHOD_OUT_DIRECT, FILE_ANY_ACCESS)
+ CTL_CODE (FILE_DEVICE_KEYBOARD, (id), METHOD_OUT_DIRECT, FILE_ANY_ACCESS)
-#define IOCTL_HID_GET_FEATURE HID_OUT_CTL_CODE(100)
-#define IOCTL_HID_GET_INPUT_REPORT HID_OUT_CTL_CODE(104)
-#define IOCTL_HID_SET_FEATURE HID_IN_CTL_CODE(100)
-#define IOCTL_HID_SET_OUTPUT_REPORT HID_IN_CTL_CODE(101)
+#define IOCTL_HID_GET_FEATURE HID_OUT_CTL_CODE(100)
+#define IOCTL_HID_GET_INPUT_REPORT HID_OUT_CTL_CODE(104)
+#define IOCTL_HID_SET_FEATURE HID_IN_CTL_CODE(100)
+#define IOCTL_HID_SET_OUTPUT_REPORT HID_IN_CTL_CODE(101)
enum libusb_hid_request_type {
HID_REQ_GET_REPORT = 0x01,
@@ -201,43 +202,46 @@ struct hid_device_priv {
uint16_t pid;
uint8_t config;
uint8_t nb_interfaces;
- bool uses_report_ids[3]; // input, ouptput, feature
+ bool uses_report_ids[3]; // input, ouptput, feature
uint16_t input_report_size;
uint16_t output_report_size;
uint16_t feature_report_size;
WCHAR string[3][MAX_USB_STRING_LENGTH];
- uint8_t string_index[3]; // man, prod, ser
+ uint8_t string_index[3]; // man, prod, ser
};
struct windows_device_priv {
- uint8_t depth; // distance to HCD
- uint8_t port; // port number on the hub
+ uint8_t depth; // distance to HCD
+ uint8_t port; // port number on the hub
uint8_t active_config;
- struct libusb_device *parent_dev; // access to parent is required for usermode ops
+ struct libusb_device *parent_dev; // access to parent is required for usermode ops
struct windows_usb_api_backend const *apib;
- char *path; // device interface path
- int sub_api; // for WinUSB-like APIs
+ char *path; // device interface path
+ int sub_api; // for WinUSB-like APIs
struct {
- char *path; // each interface needs a device interface path,
+ char *path; // each interface needs a device interface path,
struct windows_usb_api_backend const *apib; // an API backend (multiple drivers support),
int sub_api;
- int8_t nb_endpoints; // and a set of endpoint addresses (USB_MAXENDPOINTS)
+ int8_t nb_endpoints; // and a set of endpoint addresses (USB_MAXENDPOINTS)
uint8_t *endpoint;
- bool restricted_functionality; // indicates if the interface functionality is restricted
- // by Windows (eg. HID keyboards or mice cannot do R/W)
+ bool restricted_functionality; // indicates if the interface functionality is restricted
+ // by Windows (eg. HID keyboards or mice cannot do R/W)
} usb_interface[USB_MAXINTERFACES];
struct hid_device_priv *hid;
USB_DEVICE_DESCRIPTOR dev_descriptor;
- unsigned char **config_descriptor; // list of pointers to the cached config descriptors
+ unsigned char **config_descriptor; // list of pointers to the cached config descriptors
};
-static inline struct windows_device_priv *_device_priv(struct libusb_device *dev) {
+static inline struct windows_device_priv *_device_priv(struct libusb_device *dev)
+{
return (struct windows_device_priv *)dev->os_priv;
}
-static inline void windows_device_priv_init(libusb_device* dev) {
- struct windows_device_priv* p = _device_priv(dev);
+static inline void windows_device_priv_init(struct libusb_device *dev)
+{
+ struct windows_device_priv *p = _device_priv(dev);
int i;
+
p->depth = 0;
p->port = 0;
p->parent_dev = NULL;
@@ -247,8 +251,8 @@ static inline void windows_device_priv_init(libusb_device* dev) {
p->hid = NULL;
p->active_config = 0;
p->config_descriptor = NULL;
- memset(&(p->dev_descriptor), 0, sizeof(USB_DEVICE_DESCRIPTOR));
- for (i=0; i<USB_MAXINTERFACES; i++) {
+ memset(&p->dev_descriptor, 0, sizeof(USB_DEVICE_DESCRIPTOR));
+ for (i = 0; i < USB_MAXINTERFACES; i++) {
p->usb_interface[i].path = NULL;
p->usb_interface[i].apib = &usb_api_backend[USB_API_UNSUPPORTED];
p->usb_interface[i].sub_api = SUB_API_NOTSET;
@@ -258,17 +262,19 @@ static inline void windows_device_priv_init(libusb_device* dev) {
}
}
-static inline void windows_device_priv_release(libusb_device* dev) {
- struct windows_device_priv* p = _device_priv(dev);
+static inline void windows_device_priv_release(struct libusb_device *dev)
+{
+ struct windows_device_priv *p = _device_priv(dev);
int i;
+
safe_free(p->path);
if ((dev->num_configurations > 0) && (p->config_descriptor != NULL)) {
- for (i=0; i < dev->num_configurations; i++)
+ for (i = 0; i < dev->num_configurations; i++)
safe_free(p->config_descriptor[i]);
}
safe_free(p->config_descriptor);
safe_free(p->hid);
- for (i=0; i<USB_MAXINTERFACES; i++) {
+ for (i = 0; i < USB_MAXINTERFACES; i++) {
safe_free(p->usb_interface[i].path);
safe_free(p->usb_interface[i].endpoint);
}
@@ -288,7 +294,7 @@ struct windows_device_handle_priv {
static inline struct windows_device_handle_priv *_device_handle_priv(
struct libusb_device_handle *handle)
{
- return (struct windows_device_handle_priv *) handle->os_priv;
+ return (struct windows_device_handle_priv *)handle->os_priv;
}
// used for async polling functions
@@ -302,9 +308,9 @@ struct windows_transfer_priv {
// used to match a device driver (including filter drivers) against a supported API
struct driver_lookup {
- char list[MAX_KEY_LENGTH+1];// REG_MULTI_SZ list of services (driver) names
- const DWORD reg_prop; // SPDRP registry key to use to retrieve list
- const char* designation; // internal designation (for debug output)
+ char list[MAX_KEY_LENGTH + 1]; // REG_MULTI_SZ list of services (driver) names
+ const DWORD reg_prop; // SPDRP registry key to use to retrieve list
+ const char* designation; // internal designation (for debug output)
};
/* OLE32 dependency */
@@ -336,57 +342,57 @@ typedef DEVNODE *PDEVNODE, *PDEVINST;
typedef DWORD RETURN_TYPE;
typedef RETURN_TYPE CONFIGRET;
-#define CR_SUCCESS 0x00000000
-#define CR_NO_SUCH_DEVNODE 0x0000000D
-
-#define USB_DEVICE_DESCRIPTOR_TYPE LIBUSB_DT_DEVICE
-#define USB_CONFIGURATION_DESCRIPTOR_TYPE LIBUSB_DT_CONFIG
-#define USB_STRING_DESCRIPTOR_TYPE LIBUSB_DT_STRING
-#define USB_INTERFACE_DESCRIPTOR_TYPE LIBUSB_DT_INTERFACE
-#define USB_ENDPOINT_DESCRIPTOR_TYPE LIBUSB_DT_ENDPOINT
-
-#define USB_REQUEST_GET_STATUS LIBUSB_REQUEST_GET_STATUS
-#define USB_REQUEST_CLEAR_FEATURE LIBUSB_REQUEST_CLEAR_FEATURE
-#define USB_REQUEST_SET_FEATURE LIBUSB_REQUEST_SET_FEATURE
-#define USB_REQUEST_SET_ADDRESS LIBUSB_REQUEST_SET_ADDRESS
-#define USB_REQUEST_GET_DESCRIPTOR LIBUSB_REQUEST_GET_DESCRIPTOR
-#define USB_REQUEST_SET_DESCRIPTOR LIBUSB_REQUEST_SET_DESCRIPTOR
-#define USB_REQUEST_GET_CONFIGURATION LIBUSB_REQUEST_GET_CONFIGURATION
-#define USB_REQUEST_SET_CONFIGURATION LIBUSB_REQUEST_SET_CONFIGURATION
-#define USB_REQUEST_GET_INTERFACE LIBUSB_REQUEST_GET_INTERFACE
-#define USB_REQUEST_SET_INTERFACE LIBUSB_REQUEST_SET_INTERFACE
-#define USB_REQUEST_SYNC_FRAME LIBUSB_REQUEST_SYNCH_FRAME
-
-#define USB_GET_NODE_INFORMATION 258
-#define USB_GET_DESCRIPTOR_FROM_NODE_CONNECTION 260
-#define USB_GET_NODE_CONNECTION_NAME 261
-#define USB_GET_HUB_CAPABILITIES 271
+#define CR_SUCCESS 0x00000000
+#define CR_NO_SUCH_DEVNODE 0x0000000D
+
+#define USB_DEVICE_DESCRIPTOR_TYPE LIBUSB_DT_DEVICE
+#define USB_CONFIGURATION_DESCRIPTOR_TYPE LIBUSB_DT_CONFIG
+#define USB_STRING_DESCRIPTOR_TYPE LIBUSB_DT_STRING
+#define USB_INTERFACE_DESCRIPTOR_TYPE LIBUSB_DT_INTERFACE
+#define USB_ENDPOINT_DESCRIPTOR_TYPE LIBUSB_DT_ENDPOINT
+
+#define USB_REQUEST_GET_STATUS LIBUSB_REQUEST_GET_STATUS
+#define USB_REQUEST_CLEAR_FEATURE LIBUSB_REQUEST_CLEAR_FEATURE
+#define USB_REQUEST_SET_FEATURE LIBUSB_REQUEST_SET_FEATURE
+#define USB_REQUEST_SET_ADDRESS LIBUSB_REQUEST_SET_ADDRESS
+#define USB_REQUEST_GET_DESCRIPTOR LIBUSB_REQUEST_GET_DESCRIPTOR
+#define USB_REQUEST_SET_DESCRIPTOR LIBUSB_REQUEST_SET_DESCRIPTOR
+#define USB_REQUEST_GET_CONFIGURATION LIBUSB_REQUEST_GET_CONFIGURATION
+#define USB_REQUEST_SET_CONFIGURATION LIBUSB_REQUEST_SET_CONFIGURATION
+#define USB_REQUEST_GET_INTERFACE LIBUSB_REQUEST_GET_INTERFACE
+#define USB_REQUEST_SET_INTERFACE LIBUSB_REQUEST_SET_INTERFACE
+#define USB_REQUEST_SYNC_FRAME LIBUSB_REQUEST_SYNCH_FRAME
+
+#define USB_GET_NODE_INFORMATION 258
+#define USB_GET_DESCRIPTOR_FROM_NODE_CONNECTION 260
+#define USB_GET_NODE_CONNECTION_NAME 261
+#define USB_GET_HUB_CAPABILITIES 271
#if !defined(USB_GET_NODE_CONNECTION_INFORMATION_EX)
-#define USB_GET_NODE_CONNECTION_INFORMATION_EX 274
+#define USB_GET_NODE_CONNECTION_INFORMATION_EX 274
#endif
#if !defined(USB_GET_HUB_CAPABILITIES_EX)
-#define USB_GET_HUB_CAPABILITIES_EX 276
+#define USB_GET_HUB_CAPABILITIES_EX 276
#endif
#if !defined(USB_GET_NODE_CONNECTION_INFORMATION_EX_V2)
-#define USB_GET_NODE_CONNECTION_INFORMATION_EX_V2 279
+#define USB_GET_NODE_CONNECTION_INFORMATION_EX_V2 279
#endif
#ifndef METHOD_BUFFERED
-#define METHOD_BUFFERED 0
+#define METHOD_BUFFERED 0
#endif
#ifndef FILE_ANY_ACCESS
-#define FILE_ANY_ACCESS 0x00000000
+#define FILE_ANY_ACCESS 0x00000000
#endif
#ifndef FILE_DEVICE_UNKNOWN
-#define FILE_DEVICE_UNKNOWN 0x00000022
+#define FILE_DEVICE_UNKNOWN 0x00000022
#endif
#ifndef FILE_DEVICE_USB
-#define FILE_DEVICE_USB FILE_DEVICE_UNKNOWN
+#define FILE_DEVICE_USB FILE_DEVICE_UNKNOWN
#endif
#ifndef CTL_CODE
-#define CTL_CODE(DeviceType, Function, Method, Access)( \
- ((DeviceType) << 16) | ((Access) << 14) | ((Function) << 2) | (Method))
+#define CTL_CODE(DeviceType, Function, Method, Access) \
+ (((DeviceType) << 16) | ((Access) << 14) | ((Function) << 2) | (Method))
#endif
typedef enum USB_CONNECTION_STATUS {
@@ -413,45 +419,45 @@ DLL_DECLARE(WINAPI, CONFIGRET, CM_Get_Sibling, (PDEVINST, DEVINST, ULONG));
DLL_DECLARE(WINAPI, CONFIGRET, CM_Get_Device_IDA, (DEVINST, PCHAR, ULONG, ULONG));
#define IOCTL_USB_GET_HUB_CAPABILITIES_EX \
- CTL_CODE( FILE_DEVICE_USB, USB_GET_HUB_CAPABILITIES_EX, METHOD_BUFFERED, FILE_ANY_ACCESS)
+ CTL_CODE( FILE_DEVICE_USB, USB_GET_HUB_CAPABILITIES_EX, METHOD_BUFFERED, FILE_ANY_ACCESS)
#define IOCTL_USB_GET_HUB_CAPABILITIES \
- CTL_CODE(FILE_DEVICE_USB, USB_GET_HUB_CAPABILITIES, METHOD_BUFFERED, FILE_ANY_ACCESS)
+ CTL_CODE(FILE_DEVICE_USB, USB_GET_HUB_CAPABILITIES, METHOD_BUFFERED, FILE_ANY_ACCESS)
#define IOCTL_USB_GET_DESCRIPTOR_FROM_NODE_CONNECTION \
- CTL_CODE(FILE_DEVICE_USB, USB_GET_DESCRIPTOR_FROM_NODE_CONNECTION, METHOD_BUFFERED, FILE_ANY_ACCESS)
+ CTL_CODE(FILE_DEVICE_USB, USB_GET_DESCRIPTOR_FROM_NODE_CONNECTION, METHOD_BUFFERED, FILE_ANY_ACCESS)
#define IOCTL_USB_GET_ROOT_HUB_NAME \
- CTL_CODE(FILE_DEVICE_USB, HCD_GET_ROOT_HUB_NAME, METHOD_BUFFERED, FILE_ANY_ACCESS)
+ CTL_CODE(FILE_DEVICE_USB, HCD_GET_ROOT_HUB_NAME, METHOD_BUFFERED, FILE_ANY_ACCESS)
#define IOCTL_USB_GET_NODE_INFORMATION \
- CTL_CODE(FILE_DEVICE_USB, USB_GET_NODE_INFORMATION, METHOD_BUFFERED, FILE_ANY_ACCESS)
+ CTL_CODE(FILE_DEVICE_USB, USB_GET_NODE_INFORMATION, METHOD_BUFFERED, FILE_ANY_ACCESS)
#define IOCTL_USB_GET_NODE_CONNECTION_INFORMATION_EX \
- CTL_CODE(FILE_DEVICE_USB, USB_GET_NODE_CONNECTION_INFORMATION_EX, METHOD_BUFFERED, FILE_ANY_ACCESS)
+ CTL_CODE(FILE_DEVICE_USB, USB_GET_NODE_CONNECTION_INFORMATION_EX, METHOD_BUFFERED, FILE_ANY_ACCESS)
#define IOCTL_USB_GET_NODE_CONNECTION_INFORMATION_EX_V2 \
- CTL_CODE(FILE_DEVICE_USB, USB_GET_NODE_CONNECTION_INFORMATION_EX_V2, METHOD_BUFFERED, FILE_ANY_ACCESS)
+ CTL_CODE(FILE_DEVICE_USB, USB_GET_NODE_CONNECTION_INFORMATION_EX_V2, METHOD_BUFFERED, FILE_ANY_ACCESS)
#define IOCTL_USB_GET_NODE_CONNECTION_ATTRIBUTES \
- CTL_CODE(FILE_DEVICE_USB, USB_GET_NODE_CONNECTION_ATTRIBUTES, METHOD_BUFFERED, FILE_ANY_ACCESS)
+ CTL_CODE(FILE_DEVICE_USB, USB_GET_NODE_CONNECTION_ATTRIBUTES, METHOD_BUFFERED, FILE_ANY_ACCESS)
#define IOCTL_USB_GET_NODE_CONNECTION_NAME \
- CTL_CODE(FILE_DEVICE_USB, USB_GET_NODE_CONNECTION_NAME, METHOD_BUFFERED, FILE_ANY_ACCESS)
+ CTL_CODE(FILE_DEVICE_USB, USB_GET_NODE_CONNECTION_NAME, METHOD_BUFFERED, FILE_ANY_ACCESS)
// Most of the structures below need to be packed
#pragma pack(push, 1)
typedef struct USB_INTERFACE_DESCRIPTOR {
- UCHAR bLength;
- UCHAR bDescriptorType;
- UCHAR bInterfaceNumber;
- UCHAR bAlternateSetting;
- UCHAR bNumEndpoints;
- UCHAR bInterfaceClass;
- UCHAR bInterfaceSubClass;
- UCHAR bInterfaceProtocol;
- UCHAR iInterface;
+ UCHAR bLength;
+ UCHAR bDescriptorType;
+ UCHAR bInterfaceNumber;
+ UCHAR bAlternateSetting;
+ UCHAR bNumEndpoints;
+ UCHAR bInterfaceClass;
+ UCHAR bInterfaceSubClass;
+ UCHAR bInterfaceProtocol;
+ UCHAR iInterface;
} USB_INTERFACE_DESCRIPTOR, *PUSB_INTERFACE_DESCRIPTOR;
typedef struct USB_CONFIGURATION_DESCRIPTOR_SHORT {
@@ -469,39 +475,39 @@ typedef struct USB_CONFIGURATION_DESCRIPTOR_SHORT {
} USB_CONFIGURATION_DESCRIPTOR_SHORT;
typedef struct USB_ENDPOINT_DESCRIPTOR {
- UCHAR bLength;
- UCHAR bDescriptorType;
- UCHAR bEndpointAddress;
- UCHAR bmAttributes;
- USHORT wMaxPacketSize;
- UCHAR bInterval;
+ UCHAR bLength;
+ UCHAR bDescriptorType;
+ UCHAR bEndpointAddress;
+ UCHAR bmAttributes;
+ USHORT wMaxPacketSize;
+ UCHAR bInterval;
} USB_ENDPOINT_DESCRIPTOR, *PUSB_ENDPOINT_DESCRIPTOR;
typedef struct USB_DESCRIPTOR_REQUEST {
- ULONG ConnectionIndex;
+ ULONG ConnectionIndex;
struct {
- UCHAR bmRequest;
- UCHAR bRequest;
- USHORT wValue;
- USHORT wIndex;
- USHORT wLength;
+ UCHAR bmRequest;
+ UCHAR bRequest;
+ USHORT wValue;
+ USHORT wIndex;
+ USHORT wLength;
} SetupPacket;
-// UCHAR Data[0];
+// UCHAR Data[0];
} USB_DESCRIPTOR_REQUEST, *PUSB_DESCRIPTOR_REQUEST;
typedef struct USB_HUB_DESCRIPTOR {
- UCHAR bDescriptorLength;
- UCHAR bDescriptorType;
- UCHAR bNumberOfPorts;
- USHORT wHubCharacteristics;
- UCHAR bPowerOnToPowerGood;
- UCHAR bHubControlCurrent;
- UCHAR bRemoveAndPowerMask[64];
+ UCHAR bDescriptorLength;
+ UCHAR bDescriptorType;
+ UCHAR bNumberOfPorts;
+ USHORT wHubCharacteristics;
+ UCHAR bPowerOnToPowerGood;
+ UCHAR bHubControlCurrent;
+ UCHAR bRemoveAndPowerMask[64];
} USB_HUB_DESCRIPTOR, *PUSB_HUB_DESCRIPTOR;
typedef struct USB_ROOT_HUB_NAME {
- ULONG ActualLength;
- WCHAR RootHubName[1];
+ ULONG ActualLength;
+ WCHAR RootHubName[1];
} USB_ROOT_HUB_NAME, *PUSB_ROOT_HUB_NAME;
typedef struct USB_ROOT_HUB_NAME_FIXED {
@@ -510,9 +516,9 @@ typedef struct USB_ROOT_HUB_NAME_FIXED {
} USB_ROOT_HUB_NAME_FIXED;
typedef struct USB_NODE_CONNECTION_NAME {
- ULONG ConnectionIndex;
- ULONG ActualLength;
- WCHAR NodeName[1];
+ ULONG ConnectionIndex;
+ ULONG ActualLength;
+ WCHAR NodeName[1];
} USB_NODE_CONNECTION_NAME, *PUSB_NODE_CONNECTION_NAME;
typedef struct USB_NODE_CONNECTION_NAME_FIXED {
@@ -529,41 +535,41 @@ typedef struct USB_HUB_NAME_FIXED {
} USB_HUB_NAME_FIXED;
typedef struct USB_HUB_INFORMATION {
- USB_HUB_DESCRIPTOR HubDescriptor;
- BOOLEAN HubIsBusPowered;
+ USB_HUB_DESCRIPTOR HubDescriptor;
+ BOOLEAN HubIsBusPowered;
} USB_HUB_INFORMATION, *PUSB_HUB_INFORMATION;
typedef struct USB_MI_PARENT_INFORMATION {
- ULONG NumberOfInterfaces;
+ ULONG NumberOfInterfaces;
} USB_MI_PARENT_INFORMATION, *PUSB_MI_PARENT_INFORMATION;
typedef struct USB_NODE_INFORMATION {
- USB_HUB_NODE NodeType;
+ USB_HUB_NODE NodeType;
union {
- USB_HUB_INFORMATION HubInformation;
- USB_MI_PARENT_INFORMATION MiParentInformation;
+ USB_HUB_INFORMATION HubInformation;
+ USB_MI_PARENT_INFORMATION MiParentInformation;
} u;
} USB_NODE_INFORMATION, *PUSB_NODE_INFORMATION;
typedef struct USB_PIPE_INFO {
- USB_ENDPOINT_DESCRIPTOR EndpointDescriptor;
- ULONG ScheduleOffset;
+ USB_ENDPOINT_DESCRIPTOR EndpointDescriptor;
+ ULONG ScheduleOffset;
} USB_PIPE_INFO, *PUSB_PIPE_INFO;
typedef struct USB_NODE_CONNECTION_INFORMATION_EX {
- ULONG ConnectionIndex;
- USB_DEVICE_DESCRIPTOR DeviceDescriptor;
- UCHAR CurrentConfigurationValue;
- UCHAR Speed;
- BOOLEAN DeviceIsHub;
- USHORT DeviceAddress;
- ULONG NumberOfOpenPipes;
- USB_CONNECTION_STATUS ConnectionStatus;
-// USB_PIPE_INFO PipeList[0];
+ ULONG ConnectionIndex;
+ USB_DEVICE_DESCRIPTOR DeviceDescriptor;
+ UCHAR CurrentConfigurationValue;
+ UCHAR Speed;
+ BOOLEAN DeviceIsHub;
+ USHORT DeviceAddress;
+ ULONG NumberOfOpenPipes;
+ USB_CONNECTION_STATUS ConnectionStatus;
+// USB_PIPE_INFO PipeList[0];
} USB_NODE_CONNECTION_INFORMATION_EX, *PUSB_NODE_CONNECTION_INFORMATION_EX;
typedef union _USB_PROTOCOLS {
- ULONG ul;
+ ULONG ul;
struct {
ULONG Usb110:1;
ULONG Usb200:1;
@@ -599,7 +605,7 @@ typedef struct USB_HUB_CAP_FLAGS {
} USB_HUB_CAP_FLAGS, *PUSB_HUB_CAP_FLAGS;
typedef struct USB_HUB_CAPABILITIES {
- ULONG HubIs2xCapable : 1;
+ ULONG HubIs2xCapable:1;
} USB_HUB_CAPABILITIES, *PUSB_HUB_CAPABILITIES;
typedef struct USB_HUB_CAPABILITIES_EX {
@@ -610,20 +616,20 @@ typedef struct USB_HUB_CAPABILITIES_EX {
/* winusb.dll interface */
-#define SHORT_PACKET_TERMINATE 0x01
-#define AUTO_CLEAR_STALL 0x02
-#define PIPE_TRANSFER_TIMEOUT 0x03
-#define IGNORE_SHORT_PACKETS 0x04
-#define ALLOW_PARTIAL_READS 0x05
-#define AUTO_FLUSH 0x06
-#define RAW_IO 0x07
-#define MAXIMUM_TRANSFER_SIZE 0x08
-#define AUTO_SUSPEND 0x81
-#define SUSPEND_DELAY 0x83
-#define DEVICE_SPEED 0x01
-#define LowSpeed 0x01
-#define FullSpeed 0x02
-#define HighSpeed 0x03
+#define SHORT_PACKET_TERMINATE 0x01
+#define AUTO_CLEAR_STALL 0x02
+#define PIPE_TRANSFER_TIMEOUT 0x03
+#define IGNORE_SHORT_PACKETS 0x04
+#define ALLOW_PARTIAL_READS 0x05
+#define AUTO_FLUSH 0x06
+#define RAW_IO 0x07
+#define MAXIMUM_TRANSFER_SIZE 0x08
+#define AUTO_SUSPEND 0x81
+#define SUSPEND_DELAY 0x83
+#define DEVICE_SPEED 0x01
+#define LowSpeed 0x01
+#define FullSpeed 0x02
+#define HighSpeed 0x03
typedef enum USBD_PIPE_TYPE {
UsbdPipeTypeControl,
@@ -633,19 +639,19 @@ typedef enum USBD_PIPE_TYPE {
} USBD_PIPE_TYPE;
typedef struct {
- USBD_PIPE_TYPE PipeType;
- UCHAR PipeId;
- USHORT MaximumPacketSize;
- UCHAR Interval;
+ USBD_PIPE_TYPE PipeType;
+ UCHAR PipeId;
+ USHORT MaximumPacketSize;
+ UCHAR Interval;
} WINUSB_PIPE_INFORMATION, *PWINUSB_PIPE_INFORMATION;
#pragma pack(1)
typedef struct {
- UCHAR request_type;
- UCHAR request;
- USHORT value;
- USHORT index;
- USHORT length;
+ UCHAR request_type;
+ UCHAR request;
+ USHORT value;
+ USHORT index;
+ USHORT length;
} WINUSB_SETUP_PACKET, *PWINUSB_SETUP_PACKET;
#pragma pack()
@@ -770,8 +776,7 @@ typedef BOOL (WINAPI *WinUsb_ResetDevice_t)(
);
/* /!\ These must match the ones from the official libusbk.h */
-typedef enum _KUSB_FNID
-{
+typedef enum _KUSB_FNID {
KUSB_FNID_Init,
KUSB_FNID_Free,
KUSB_FNID_ClaimInterface,
@@ -818,7 +823,7 @@ typedef struct _KLIB_VERSION {
typedef KLIB_VERSION* PKLIB_VERSION;
typedef BOOL (WINAPI *LibK_GetProcAddress_t)(
- PVOID* ProcAddress,
+ PVOID *ProcAddress,
ULONG DriverID,
ULONG FunctionID
);
@@ -854,8 +859,8 @@ struct winusb_interface {
/* hid.dll interface */
-#define HIDP_STATUS_SUCCESS 0x110000
-typedef void* PHIDP_PREPARSED_DATA;
+#define HIDP_STATUS_SUCCESS 0x110000
+typedef void * PHIDP_PREPARSED_DATA;
#pragma pack(1)
typedef struct {
@@ -868,64 +873,64 @@ typedef struct {
typedef USHORT USAGE;
typedef struct {
- USAGE Usage;
- USAGE UsagePage;
- USHORT InputReportByteLength;
- USHORT OutputReportByteLength;
- USHORT FeatureReportByteLength;
- USHORT Reserved[17];
- USHORT NumberLinkCollectionNodes;
- USHORT NumberInputButtonCaps;
- USHORT NumberInputValueCaps;
- USHORT NumberInputDataIndices;
- USHORT NumberOutputButtonCaps;
- USHORT NumberOutputValueCaps;
- USHORT NumberOutputDataIndices;
- USHORT NumberFeatureButtonCaps;
- USHORT NumberFeatureValueCaps;
- USHORT NumberFeatureDataIndices;
+ USAGE Usage;
+ USAGE UsagePage;
+ USHORT InputReportByteLength;
+ USHORT OutputReportByteLength;
+ USHORT FeatureReportByteLength;
+ USHORT Reserved[17];
+ USHORT NumberLinkCollectionNodes;
+ USHORT NumberInputButtonCaps;
+ USHORT NumberInputValueCaps;
+ USHORT NumberInputDataIndices;
+ USHORT NumberOutputButtonCaps;
+ USHORT NumberOutputValueCaps;
+ USHORT NumberOutputDataIndices;
+ USHORT NumberFeatureButtonCaps;
+ USHORT NumberFeatureValueCaps;
+ USHORT NumberFeatureDataIndices;
} HIDP_CAPS, *PHIDP_CAPS;
typedef enum _HIDP_REPORT_TYPE {
- HidP_Input,
- HidP_Output,
- HidP_Feature
+ HidP_Input,
+ HidP_Output,
+ HidP_Feature
} HIDP_REPORT_TYPE;
typedef struct _HIDP_VALUE_CAPS {
- USAGE UsagePage;
- UCHAR ReportID;
- BOOLEAN IsAlias;
- USHORT BitField;
- USHORT LinkCollection;
- USAGE LinkUsage;
- USAGE LinkUsagePage;
- BOOLEAN IsRange;
- BOOLEAN IsStringRange;
- BOOLEAN IsDesignatorRange;
- BOOLEAN IsAbsolute;
- BOOLEAN HasNull;
- UCHAR Reserved;
- USHORT BitSize;
- USHORT ReportCount;
- USHORT Reserved2[5];
- ULONG UnitsExp;
- ULONG Units;
- LONG LogicalMin, LogicalMax;
- LONG PhysicalMin, PhysicalMax;
+ USAGE UsagePage;
+ UCHAR ReportID;
+ BOOLEAN IsAlias;
+ USHORT BitField;
+ USHORT LinkCollection;
+ USAGE LinkUsage;
+ USAGE LinkUsagePage;
+ BOOLEAN IsRange;
+ BOOLEAN IsStringRange;
+ BOOLEAN IsDesignatorRange;
+ BOOLEAN IsAbsolute;
+ BOOLEAN HasNull;
+ UCHAR Reserved;
+ USHORT BitSize;
+ USHORT ReportCount;
+ USHORT Reserved2[5];
+ ULONG UnitsExp;
+ ULONG Units;
+ LONG LogicalMin, LogicalMax;
+ LONG PhysicalMin, PhysicalMax;
union {
- struct {
- USAGE UsageMin, UsageMax;
- USHORT StringMin, StringMax;
- USHORT DesignatorMin, DesignatorMax;
- USHORT DataIndexMin, DataIndexMax;
- } Range;
- struct {
- USAGE Usage, Reserved1;
- USHORT StringIndex, Reserved2;
- USHORT DesignatorIndex, Reserved3;
- USHORT DataIndex, Reserved4;
- } NotRange;
+ struct {
+ USAGE UsageMin, UsageMax;
+ USHORT StringMin, StringMax;
+ USHORT DesignatorMin, DesignatorMax;
+ USHORT DataIndexMin, DataIndexMax;
+ } Range;
+ struct {
+ USAGE Usage, Reserved1;
+ USHORT StringIndex, Reserved2;
+ USHORT DesignatorIndex, Reserved3;
+ USHORT DataIndex, Reserved4;
+ } NotRange;
} u;
} HIDP_VALUE_CAPS, *PHIDP_VALUE_CAPS;
diff --git a/libusb/version_nano.h b/libusb/version_nano.h
index 330e456..4945a8d 100644
--- a/libusb/version_nano.h
+++ b/libusb/version_nano.h
@@ -1 +1 @@
-#define LIBUSB_NANO 11032
+#define LIBUSB_NANO 11033