diff --git a/libusb/Makefile.am b/libusb/Makefile.am index adba4d3a8..2b21e652b 100644 --- a/libusb/Makefile.am +++ b/libusb/Makefile.am @@ -27,10 +27,10 @@ OS_OPENBSD_SRC = os/openbsd_usb.c OS_SUNOS_SRC = os/sunos_usb.h os/sunos_usb.c OS_WINDOWS_SRC = libusb-1.0.def libusb-1.0.rc \ os/windows_common.h os/windows_common.c \ + os/windows_hotplug.h os/windows_hotplug.c \ os/windows_usbdk.h os/windows_usbdk.c \ - os/windows_winusb.h os/windows_winusb.c \ - os/windows_hotplug.h os/windows_hotplug.c - + os/windows_winusb.h os/windows_winusb.c + if OS_DARWIN OS_SRC = $(OS_DARWIN_SRC) endif diff --git a/libusb/hotplug.c b/libusb/hotplug.c index 4446a342d..6df2dae04 100644 --- a/libusb/hotplug.c +++ b/libusb/hotplug.c @@ -173,7 +173,7 @@ static void usbi_recursively_remove_parents(struct libusb_device *dev, struct li * equal to next_dev given we know at this point that it was * previously seen in the list. */ assert (dev->parent_dev != next_dev); - if(dev->parent_dev->list.next && dev->parent_dev->list.prev) { + if (dev->parent_dev->list.next && dev->parent_dev->list.prev) { list_del(&dev->parent_dev->list); } } diff --git a/libusb/os/windows_common.c b/libusb/os/windows_common.c index 990400d11..f3c63bbc4 100644 --- a/libusb/os/windows_common.c +++ b/libusb/os/windows_common.c @@ -567,17 +567,13 @@ static int windows_init(struct libusb_context *ctx) r = LIBUSB_SUCCESS; if (init_count == 1) { - r = windows_start_event_monitor(); - } - if (r != LIBUSB_SUCCESS) { - usbi_err(ctx, "error starting hotplug event monitor"); - - if (init_count == 1) { - windows_stop_event_monitor(); + r = windows_start_event_monitor(); // Start up hotplug event handler + if (r != LIBUSB_SUCCESS) { + usbi_err(ctx, "error starting hotplug event monitor"); + goto init_exit; } - goto init_exit; } - + windows_initial_scan_devices(ctx); init_exit: // Holds semaphore here diff --git a/libusb/os/windows_hotplug.c b/libusb/os/windows_hotplug.c index a565abec5..0a5c00022 100644 --- a/libusb/os/windows_hotplug.c +++ b/libusb/os/windows_hotplug.c @@ -14,10 +14,10 @@ * This updates the hotplug status of each device to one of three values {UNCHANGED, ARRIVED, LEFT}. * 3. According to the value, we generate events to libusb client via hotplug callbacks. */ -static HWND windows_event_hWnd; +static HWND windows_event_hwnd; static HANDLE windows_event_thread_handle; static DWORD WINAPI windows_event_thread_main(LPVOID lpParam); -static LRESULT CALLBACK windows_proc_callback(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam); +static LRESULT CALLBACK windows_proc_callback(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam); #define log_error(operation) do { \ usbi_err(NULL, "%s failed with error: %s", operation, windows_error_str(0)); \ @@ -26,15 +26,15 @@ static LRESULT CALLBACK windows_proc_callback(HWND hWnd, UINT message, WPARAM wP int windows_start_event_monitor(void) { windows_event_thread_handle = CreateThread( - NULL, - 0, + NULL, // Default security descriptor + 0, // Default stack size windows_event_thread_main, - NULL, - 0, - NULL + NULL, // No parameters to pass to the thread + 0, // Start immediately + NULL // No need to keep track of thread ID ); - if(windows_event_thread_handle == NULL) + if (windows_event_thread_handle == NULL) { log_error("CreateThread"); return LIBUSB_ERROR_OTHER; @@ -45,27 +45,27 @@ int windows_start_event_monitor(void) int windows_stop_event_monitor(void) { - if(windows_event_hWnd == NULL) + if (windows_event_hwnd == NULL) { return LIBUSB_SUCCESS; } - if(!SUCCEEDED(SendMessage(windows_event_hWnd, WM_CLOSE, 0, 0))) + if (!SUCCEEDED(SendMessage(windows_event_hwnd, WM_CLOSE, 0, 0))) { log_error("SendMessage"); } int ret = LIBUSB_SUCCESS; - if(WaitForSingleObject(windows_event_thread_handle, INFINITE) != WAIT_OBJECT_0) + if (WaitForSingleObject(windows_event_thread_handle, INFINITE) != WAIT_OBJECT_0) { log_error("WaitForSingleObject"); ret = LIBUSB_ERROR_OTHER; } - if(!CloseHandle(windows_event_thread_handle)) + if (!CloseHandle(windows_event_thread_handle)) { - log_error("WaitForSingleObject"); + log_error("CloseHandle"); ret = LIBUSB_ERROR_OTHER; } @@ -89,20 +89,22 @@ static int windows_get_device_list(struct libusb_context *ctx) return ((struct windows_context_priv *)usbi_get_context_priv(ctx))->backend->get_device_list(ctx); } -int windows_initial_scan_devices(struct libusb_context *ctx) +void windows_initial_scan_devices(struct libusb_context *ctx) { usbi_mutex_static_lock(&active_contexts_lock); - - int ret = windows_get_device_list(ctx); - + + const int ret = windows_get_device_list(ctx); + if (ret != LIBUSB_SUCCESS) + { + usbi_err(ctx, "hotplug failed to retrieve initial list with error: %s", libusb_error_name(ret)); + } usbi_mutex_static_unlock(&active_contexts_lock); - return ret; } static void windows_refresh_device_list(struct libusb_context *ctx) { - int ret = windows_get_device_list(ctx); - if(ret != LIBUSB_SUCCESS) + const int ret = windows_get_device_list(ctx); + if (ret != LIBUSB_SUCCESS) { usbi_err(ctx, "hotplug failed to retrieve current list with error: %s", libusb_error_name(ret)); return; @@ -114,12 +116,12 @@ static void windows_refresh_device_list(struct libusb_context *ctx) for_each_device_safe(ctx, dev, next_dev) { priv = usbi_get_device_priv(dev); - if(priv->hotplug_status != LEFT) + if (priv->hotplug_status != LEFT) { continue; } - if(priv->initialized) + if (priv->initialized) { usbi_disconnect_device(dev); } @@ -133,7 +135,7 @@ static void windows_refresh_device_list(struct libusb_context *ctx) { priv = usbi_get_device_priv(dev); - if(priv->hotplug_status != ARRIVED) + if (priv->hotplug_status != ARRIVED) { continue; } @@ -151,6 +153,7 @@ static void windows_refresh_device_list_for_all_ctx(void) { windows_refresh_device_list(ctx); } + usbi_mutex_static_unlock(&active_contexts_lock); } @@ -183,7 +186,7 @@ static DWORD WINAPI windows_event_thread_main(LPVOID lpParam) return (DWORD)-1; } - windows_event_hWnd = CreateWindow( + windows_event_hwnd = CreateWindow( WND_CLASS_NAME, TEXT(""), 0, @@ -192,7 +195,7 @@ static DWORD WINAPI windows_event_thread_main(LPVOID lpParam) GetModuleHandle(NULL), NULL); - if (windows_event_hWnd == NULL) + if (windows_event_hwnd == NULL) { log_error("event thread: CreateWindow"); return (DWORD)-1; @@ -201,7 +204,7 @@ static DWORD WINAPI windows_event_thread_main(LPVOID lpParam) MSG msg; BOOL ret_val; - while ((ret_val = GetMessage(&msg, windows_event_hWnd, 0, 0)) != 0) + while ((ret_val = GetMessage(&msg, windows_event_hwnd, 0, 0)) != 0) { if (ret_val == -1) { @@ -209,12 +212,12 @@ static DWORD WINAPI windows_event_thread_main(LPVOID lpParam) break; } - if(!SUCCEEDED(TranslateMessage(&msg))) + if (!SUCCEEDED(TranslateMessage(&msg))) { log_error("event thread: TranslateMessage"); } - if(!SUCCEEDED(DispatchMessage(&msg))) + if (!SUCCEEDED(DispatchMessage(&msg))) { log_error("event thread: DispatchMessage"); } @@ -227,7 +230,7 @@ static DWORD WINAPI windows_event_thread_main(LPVOID lpParam) static bool register_device_interface_to_window_handle( IN GUID interface_class_guid, - IN HWND hWnd, + IN HWND hwnd, OUT HDEVNOTIFY* device_notify_handle) { DEV_BROADCAST_DEVICEINTERFACE notificationFilter = { 0 }; @@ -236,9 +239,9 @@ static bool register_device_interface_to_window_handle( notificationFilter.dbcc_classguid = interface_class_guid; *device_notify_handle = RegisterDeviceNotification( - hWnd, + hwnd, ¬ificationFilter, - DEVICE_NOTIFY_WINDOW_HANDLE// | DEVICE_NOTIFY_ALL_INTERFACE_CLASSES// ?? for the ALL? + DEVICE_NOTIFY_WINDOW_HANDLE ); if (*device_notify_handle == NULL) @@ -251,21 +254,21 @@ static bool register_device_interface_to_window_handle( } static LRESULT CALLBACK windows_proc_callback( - HWND hWnd, + HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) { UNUSED(lParam); - + static HDEVNOTIFY device_notify_handle; - + switch (message) { case WM_CREATE: if (!register_device_interface_to_window_handle( GUID_DEVINTERFACE_USB_DEVICE, - hWnd, + hwnd, &device_notify_handle)) { return -1; @@ -291,7 +294,7 @@ static LRESULT CALLBACK windows_proc_callback( { log_error("UnregisterDeviceNotification"); } - if(!DestroyWindow(hWnd)) + if (!DestroyWindow(hwnd)) { log_error("DestroyWindow"); } @@ -302,6 +305,6 @@ static LRESULT CALLBACK windows_proc_callback( return 0; default: - return DefWindowProc(hWnd, message, wParam, lParam); + return DefWindowProc(hwnd, message, wParam, lParam); } } diff --git a/libusb/os/windows_hotplug.h b/libusb/os/windows_hotplug.h index 0e046b9cf..5e93285e2 100644 --- a/libusb/os/windows_hotplug.h +++ b/libusb/os/windows_hotplug.h @@ -4,6 +4,6 @@ int windows_start_event_monitor(void); int windows_stop_event_monitor(void); -int windows_initial_scan_devices(struct libusb_context *ctx); +void windows_initial_scan_devices(struct libusb_context *ctx); #endif \ No newline at end of file diff --git a/libusb/os/windows_winusb.c b/libusb/os/windows_winusb.c index 6fe5f0ede..a0de92d99 100644 --- a/libusb/os/windows_winusb.c +++ b/libusb/os/windows_winusb.c @@ -1953,7 +1953,7 @@ static int winusb_get_device_list(struct libusb_context *ctx) break; case GEN_PASS: port_nr = 0; - if(priv->initialized) { + if (priv->initialized) { libusb_unref_device(parent_dev); r = LIBUSB_SUCCESS; priv->hotplug_status = UNCHANGED;