diff options
author | Pete Batard <pbatard@gmail.com> | 2010-10-28 11:33:17 +0100 |
---|---|---|
committer | Pete Batard <pbatard@gmail.com> | 2010-10-28 11:33:17 +0100 |
commit | 1b538f0e2bd84f500e02a9abdf7d0489fc1b5c0e (patch) | |
tree | a1131d6f4c8390576c76c825b4f1291a8af11edf /libusb/os/windows_usb.c | |
parent | 80c0b7e852bee78bcff78b78b35105f9b504d1d9 (diff) | |
download | libusb-1b538f0e2bd84f500e02a9abdf7d0489fc1b5c0e.tar.gz |
merge -> pbr319, part 1
Diffstat (limited to 'libusb/os/windows_usb.c')
-rw-r--r-- | libusb/os/windows_usb.c | 74 |
1 files changed, 41 insertions, 33 deletions
diff --git a/libusb/os/windows_usb.c b/libusb/os/windows_usb.c index 6019bbb..0093bb8 100644 --- a/libusb/os/windows_usb.c +++ b/libusb/os/windows_usb.c @@ -41,7 +41,7 @@ #include <process.h> #include <stdio.h> #include <inttypes.h> -#include <objbase.h> // for string to GUID conv. requires libole32.a +#include <objbase.h> #include <winioctl.h> #include <dbt.h> @@ -197,7 +197,7 @@ static char err_string[ERR_BUFFER_SIZE]; safe_sprintf(err_string, ERR_BUFFER_SIZE, "[%d] ", error_code); size = FormatMessageA(FORMAT_MESSAGE_FROM_SYSTEM, NULL, error_code, - MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPTSTR) &err_string[safe_strlen(err_string)], + 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(); @@ -259,15 +259,23 @@ static char* sanitize_path(const char* path) } /* - * Cfgmgr32 API functions + * Cfgmgr32, OLE32 and SetupAPI DLL functions */ -static int Cfgmgr32_init(void) +static int init_dlls(void) { DLL_LOAD(Cfgmgr32.dll, CM_Get_Parent, TRUE); DLL_LOAD(Cfgmgr32.dll, CM_Get_Child, TRUE); DLL_LOAD(Cfgmgr32.dll, CM_Get_Sibling, TRUE); DLL_LOAD(Cfgmgr32.dll, CM_Get_Device_IDA, TRUE); - + // Prefixed to avoid conflict with header files + DLL_LOAD_PREFIXED(OLE32.dll, p, CLSIDFromString, TRUE); + DLL_LOAD_PREFIXED(SetupAPI.dll, p, SetupDiGetClassDevsA, TRUE); + DLL_LOAD_PREFIXED(SetupAPI.dll, p, SetupDiEnumDeviceInfo, TRUE); + DLL_LOAD_PREFIXED(SetupAPI.dll, p, SetupDiEnumDeviceInterfaces, TRUE); + DLL_LOAD_PREFIXED(SetupAPI.dll, p, SetupDiGetDeviceInterfaceDetailA, TRUE); + DLL_LOAD_PREFIXED(SetupAPI.dll, p, SetupDiDestroyDeviceInfoList, TRUE); + DLL_LOAD_PREFIXED(SetupAPI.dll, p, SetupDiOpenDevRegKey, TRUE); + DLL_LOAD_PREFIXED(SetupAPI.dll, p, SetupDiGetDeviceRegistryPropertyA, TRUE); return LIBUSB_SUCCESS; } @@ -288,19 +296,19 @@ bool get_devinfo_data(struct libusb_context *ctx, HDEVINFO *dev_info, SP_DEVINFO_DATA *dev_info_data, unsigned _index) { if (_index <= 0) { - *dev_info = SetupDiGetClassDevs(NULL, "USB", NULL, DIGCF_PRESENT|DIGCF_ALLCLASSES); + *dev_info = pSetupDiGetClassDevsA(NULL, "USB", NULL, DIGCF_PRESENT|DIGCF_ALLCLASSES); if (*dev_info == INVALID_HANDLE_VALUE) { return false; } } dev_info_data->cbSize = sizeof(SP_DEVINFO_DATA); - if (!SetupDiEnumDeviceInfo(*dev_info, _index, dev_info_data)) { + if (!pSetupDiEnumDeviceInfo(*dev_info, _index, dev_info_data)) { if (GetLastError() != ERROR_NO_MORE_ITEMS) { usbi_err(ctx, "Could not obtain device info data for index %u: %s", _index, windows_error_str(0)); } - SetupDiDestroyDeviceInfoList(*dev_info); + pSetupDiDestroyDeviceInfoList(*dev_info); *dev_info = INVALID_HANDLE_VALUE; return false; } @@ -320,15 +328,15 @@ bool get_devinfo_data(struct libusb_context *ctx, * structure returned and call this function repeatedly using the same guid (with an * incremented index starting at zero) until all interfaces have been returned. */ -SP_DEVICE_INTERFACE_DETAIL_DATA *get_interface_details(struct libusb_context *ctx, +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) { SP_DEVICE_INTERFACE_DATA dev_interface_data; - SP_DEVICE_INTERFACE_DETAIL_DATA *dev_interface_details = NULL; + SP_DEVICE_INTERFACE_DETAIL_DATA_A *dev_interface_details = NULL; DWORD size; if (_index <= 0) { - *dev_info = SetupDiGetClassDevs(guid, NULL, NULL, DIGCF_PRESENT|DIGCF_DEVICEINTERFACE); + *dev_info = pSetupDiGetClassDevsA(guid, NULL, NULL, DIGCF_PRESENT|DIGCF_DEVICEINTERFACE); } if (*dev_info == INVALID_HANDLE_VALUE) { return NULL; @@ -336,30 +344,30 @@ SP_DEVICE_INTERFACE_DETAIL_DATA *get_interface_details(struct libusb_context *ct if (dev_info_data != NULL) { dev_info_data->cbSize = sizeof(SP_DEVINFO_DATA); - if (!SetupDiEnumDeviceInfo(*dev_info, _index, dev_info_data)) { + if (!pSetupDiEnumDeviceInfo(*dev_info, _index, dev_info_data)) { if (GetLastError() != ERROR_NO_MORE_ITEMS) { usbi_err(ctx, "Could not obtain device info data for index %u: %s", _index, windows_error_str(0)); } - SetupDiDestroyDeviceInfoList(*dev_info); + pSetupDiDestroyDeviceInfoList(*dev_info); *dev_info = INVALID_HANDLE_VALUE; return NULL; } } dev_interface_data.cbSize = sizeof(SP_DEVICE_INTERFACE_DATA); - if (!SetupDiEnumDeviceInterfaces(*dev_info, NULL, guid, _index, &dev_interface_data)) { + if (!pSetupDiEnumDeviceInterfaces(*dev_info, NULL, guid, _index, &dev_interface_data)) { if (GetLastError() != ERROR_NO_MORE_ITEMS) { usbi_err(ctx, "Could not obtain interface data for index %u: %s", _index, windows_error_str(0)); } - SetupDiDestroyDeviceInfoList(*dev_info); + pSetupDiDestroyDeviceInfoList(*dev_info); *dev_info = INVALID_HANDLE_VALUE; return NULL; } // Read interface data (dummy + actual) to access the device path - if (!SetupDiGetDeviceInterfaceDetail(*dev_info, &dev_interface_data, NULL, 0, &size, NULL)) { + if (!pSetupDiGetDeviceInterfaceDetailA(*dev_info, &dev_interface_data, NULL, 0, &size, NULL)) { // The dummy call should fail with ERROR_INSUFFICIENT_BUFFER if (GetLastError() != ERROR_INSUFFICIENT_BUFFER) { usbi_err(ctx, "could not access interface data (dummy) for index %u: %s", @@ -376,8 +384,8 @@ SP_DEVICE_INTERFACE_DETAIL_DATA *get_interface_details(struct libusb_context *ct goto err_exit; } - dev_interface_details->cbSize = sizeof(SP_DEVICE_INTERFACE_DETAIL_DATA); - if (!SetupDiGetDeviceInterfaceDetail(*dev_info, &dev_interface_data, + dev_interface_details->cbSize = sizeof(SP_DEVICE_INTERFACE_DETAIL_DATA_A); + 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)); @@ -386,7 +394,7 @@ SP_DEVICE_INTERFACE_DETAIL_DATA *get_interface_details(struct libusb_context *ct return dev_interface_details; err_exit: - SetupDiDestroyDeviceInfoList(*dev_info); + pSetupDiDestroyDeviceInfoList(*dev_info); *dev_info = INVALID_HANDLE_VALUE; return NULL; } @@ -743,10 +751,10 @@ static int windows_init(struct libusb_context *ctx) int i, r = LIBUSB_ERROR_OTHER; OSVERSIONINFO os_version; HANDLE semaphore; - TCHAR 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()&0xFFFFFFFF); - semaphore = CreateSemaphore(NULL, 1, 1, sem_name); + semaphore = CreateSemaphoreA(NULL, 1, 1, sem_name); if (semaphore == NULL) { usbi_err(ctx, "could not create semaphore: %s", windows_error_str(0)); return LIBUSB_ERROR_NO_MEM; @@ -790,9 +798,9 @@ static int windows_init(struct libusb_context *ctx) // Initialize pollable file descriptors init_polling(); - // Load missing CFGMGR32.DLL imports - if (Cfgmgr32_init() != LIBUSB_SUCCESS) { - usbi_err(ctx, "could not resolve Cfgmgr32.dll functions"); + // Load DLL imports + if (init_dlls() != LIBUSB_SUCCESS) { + usbi_err(ctx, "could not resolve DLL functions"); return LIBUSB_ERROR_NOT_FOUND; } @@ -1101,7 +1109,7 @@ static uint8_t get_api_type(struct libusb_context *ctx, // Check the service & filter names to know the API we should use for (k=0; k<3; k++) { - if (SetupDiGetDeviceRegistryPropertyA(*dev_info, dev_info_data, lookup[k].reg_prop, + if (pSetupDiGetDeviceRegistryPropertyA(*dev_info, dev_info_data, lookup[k].reg_prop, ®_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) { @@ -1214,7 +1222,7 @@ static int windows_get_device_list(struct libusb_context *ctx, struct discovered struct discovered_devs *discdevs = *_discdevs; HDEVINFO dev_info; SP_DEVINFO_DATA dev_info_data; - SP_DEVICE_INTERFACE_DETAIL_DATA *dev_interface_details = NULL; + SP_DEVICE_INTERFACE_DETAIL_DATA_A *dev_interface_details = NULL; GUID hid_guid; #define MAX_ENUM_GUIDS 64 const GUID* guid[MAX_ENUM_GUIDS]; @@ -1318,7 +1326,7 @@ 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 ( (!SetupDiGetDeviceRegistryProperty(dev_info, &dev_info_data, SPDRP_ADDRESS, + if ( (!pSetupDiGetDeviceRegistryPropertyA(dev_info, &dev_info_data, SPDRP_ADDRESS, ®_type, (BYTE*)&port_nr, 4, &size)) || (size != 4) ) { usbi_warn(ctx, "could not retrieve port number for device %s, skipping: %s", @@ -1336,13 +1344,13 @@ static int windows_get_device_list(struct libusb_context *ctx, struct discovered case GEN_PASS: // We use the GEN pass to detect driverless devices... size = sizeof(strbuf); - if (!SetupDiGetDeviceRegistryPropertyA(dev_info, &dev_info_data, SPDRP_DRIVER, + if (!pSetupDiGetDeviceRegistryPropertyA(dev_info, &dev_info_data, SPDRP_DRIVER, ®_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."); } // ...and to add the additional device interface GUIDs - key = SetupDiOpenDevRegKey(dev_info, &dev_info_data, DICS_FLAG_GLOBAL, 0, DIREG_DEV, KEY_READ); + key = pSetupDiOpenDevRegKey(dev_info, &dev_info_data, DICS_FLAG_GLOBAL, 0, DIREG_DEV, KEY_READ); if (key != INVALID_HANDLE_VALUE) { size = sizeof(guid_string_w); s = RegQueryValueExW(key, L"DeviceInterfaceGUIDs", NULL, ®_type, @@ -1355,7 +1363,7 @@ static int windows_get_device_list(struct libusb_context *ctx, struct discovered LOOP_BREAK(LIBUSB_ERROR_OVERFLOW); } if_guid = calloc(1, sizeof(GUID)); - CLSIDFromString(guid_string_w, if_guid); + pCLSIDFromString(guid_string_w, if_guid); guid[nb_guids++] = if_guid; usbi_dbg("extra GUID: %s", guid_to_string(if_guid)); } @@ -1366,7 +1374,7 @@ static int windows_get_device_list(struct libusb_context *ctx, struct discovered break; default: // DEV_PASS or EXT_PASS // Get the API type (after checking that the driver installation is OK) - if ( (!SetupDiGetDeviceRegistryProperty(dev_info, &dev_info_data, SPDRP_INSTALL_STATE, + if ( (!pSetupDiGetDeviceRegistryPropertyA(dev_info, &dev_info_data, SPDRP_INSTALL_STATE, ®_type, (BYTE*)&install_state, 4, &size)) || (size != 4) ){ usbi_warn(ctx, "could not detect installation state of driver for '%s': %s", @@ -1559,10 +1567,10 @@ static void windows_exit(struct libusb_context *ctx) struct windows_context_priv *ctx_priv = __context_priv(ctx); int i; HANDLE semaphore; - TCHAR 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()&0xFFFFFFFF); - semaphore = CreateSemaphore(NULL, 1, 1, sem_name); + semaphore = CreateSemaphoreA(NULL, 1, 1, sem_name); if (semaphore == NULL) { return; } |