configure.ac: Cleanup and refactoring

Make the formatting consistent across the entire file. In particular:

  - Always quote strings whose values are derived
  - Use tabs consistently
  - Wrap all arguments with square brackets

Replace the use of '-a' with '&&' to be more portable.

Rearrange some of the feature checks to be conditional upon the platform
or backend. For example, there is no need to check for nfds_t on Windows
because poll() doesn't exist there. Similarly we now only check for
timerfd on Linux and Solaris. This translates into slightly faster
configure times.

Explicitly define tokens for both the poll and thread implementations.
This makes the preprocessor conditionals much nicer since it is not
necessary to enumerate all possible OS_* tokens. Also replace
POLL_NFDS_TYPE with a proper typedef that is based on the availability
of the nfds_t type.

Migrate to config definition names that are more consistent with
autoconf. The check for timerfd actually verifies the presence of the
library function instead of just the header definitions, and the token
USBI_TIMERFD_AVAILABLE is now HAVE_TIMERFD. Similarly the check for
syslog results in a definition of HAVE_SYSLOG.

Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
diff --git a/Xcode/config.h b/Xcode/config.h
index c280d9f..694fd9a 100644
--- a/Xcode/config.h
+++ b/Xcode/config.h
@@ -6,14 +6,17 @@
 /* Message logging */
 #define ENABLE_LOGGING 1
 
+/* Define to 1 if the system has the type `nfds_t'. */
+#define HAVE_NFDS_T 1
+
 /* Define to 1 if you have the <sys/time.h> header file. */
 #define HAVE_SYS_TIME_H 1
 
 /* Darwin backend */
 #define OS_DARWIN 1
 
-/* type of second poll() argument */
-#define POLL_NFDS_TYPE nfds_t
+/* Use POSIX poll() implementation */
+#define POLL_POSIX 1
 
 /* Use POSIX Threads */
 #define THREADS_POSIX 1
diff --git a/android/config.h b/android/config.h
index e880d1e..9b95bb2 100644
--- a/android/config.h
+++ b/android/config.h
@@ -29,14 +29,20 @@
 /* Define to 1 if you have the <asm/types.h> header file. */
 #define HAVE_ASM_TYPES_H 1
 
+/* Define to 1 if the system has the type `nfds_t'. */
+#define HAVE_NFDS_T 1
+
+/* Define to 1 if you have the `pipe2' function. */
+#define HAVE_PIPE2 1
+
 /* Define to 1 if you have the <sys/time.h> header file. */
 #define HAVE_SYS_TIME_H 1
 
 /* Linux backend */
 #define OS_LINUX 1
 
-/* type of second poll() argument */
-#define POLL_NFDS_TYPE nfds_t
+/* Use POSIX poll() implementation */
+#define POLL_POSIX 1
 
 /* Use POSIX Threads */
 #define THREADS_POSIX 1
diff --git a/configure.ac b/configure.ac
index 0bd2cd8..4b8752a 100644
--- a/configure.ac
+++ b/configure.ac
@@ -17,10 +17,10 @@
 
 AC_INIT([libusb],[LIBUSB_MAJOR[.]LIBUSB_MINOR[.]LIBUSB_MICRO[]LIBUSB_RC],[libusb-devel@lists.sourceforge.net],[libusb],[http://libusb.info])
 
-# Library versioning
-# These numbers should be tweaked on every release. Read carefully:
-# http://www.gnu.org/software/libtool/manual/html_node/Updating-version-info.html
-# http://sourceware.org/autobook/autobook/autobook_91.html
+dnl Library versioning
+dnl These numbers should be tweaked on every release. Read carefully:
+dnl http://www.gnu.org/software/libtool/manual/html_node/Updating-version-info.html
+dnl http://sourceware.org/autobook/autobook/autobook_91.html
 lt_current=2
 lt_revision=0
 lt_age=2
@@ -40,294 +40,301 @@
 LT_LANG([Windows Resource])
 AC_C_INLINE
 AM_PROG_CC_C_O
-AC_DEFINE([_GNU_SOURCE], 1, [Use GNU extensions])
+AC_DEFINE([_GNU_SOURCE], [1], [Use GNU extensions])
 
 LTLDFLAGS="${LTLDFLAGS} -no-undefined"
 
 AC_MSG_CHECKING([operating system])
-
-dnl on linux-android platform, some functions are in different places
 case $host in
-*-linux-android*)
-	AC_MSG_RESULT([This is a Linux-Android system])
-	is_backend_android=yes
-	;;
-*)
-	is_backend_android=no
-esac
-
-case $host in
-*-linux* | *-uclinux*)
-	AC_MSG_RESULT([Linux])
-	backend=linux
-	threads=posix
-	;;
 *-darwin*)
 	AC_MSG_RESULT([Darwin/Mac OS X])
 	backend=darwin
-	threads=posix
-	;;
-*-openbsd*)
-	AC_MSG_RESULT([OpenBSD])
-	backend=openbsd
-	threads=posix
-	;;
-*-netbsd*)
-	AC_MSG_RESULT([NetBSD])
-	backend=netbsd
-	threads=posix
-	;;
-*-mingw* | *msys*)
-	AC_MSG_RESULT([Windows])
-	backend=windows
-	threads=windows
-	create_import_lib=yes
-	AM_CFLAGS="${AM_CFLAGS} -fno-omit-frame-pointer"
-	;;
-*-cygwin*)
-	AC_MSG_RESULT([Cygwin (using Windows backend)])
-	backend=windows
+	poll=posix
 	threads=posix
 	;;
 *-haiku*)
 	AC_MSG_RESULT([Haiku])
 	backend=haiku
+	poll=posix
+	threads=posix
+	;;
+*-linux* | *-uclinux*)
+	dnl on linux-android platform, some functions are in different places
+	case $host in
+	*-linux-android*)
+		AC_MSG_RESULT([Linux (Android system)])
+		is_backend_android=yes
+		;;
+	*)
+		AC_MSG_RESULT([Linux])
+		is_backend_android=no
+		;;
+	esac
+	backend=linux
+	poll=posix
+	threads=posix
+	;;
+*-netbsd*)
+	AC_MSG_RESULT([NetBSD])
+	backend=netbsd
+	poll=posix
+	threads=posix
+	;;
+*-openbsd*)
+	AC_MSG_RESULT([OpenBSD])
+	backend=openbsd
+	poll=posix
 	threads=posix
 	;;
 *-solaris*)
 	AC_MSG_RESULT([SunOS])
 	backend=sunos
+	poll=posix
 	threads=posix
 	;;
+*-cygwin*)
+	AC_MSG_RESULT([Windows (using Cygwin)])
+	backend=windows
+	poll=windows
+	threads=posix
+	;;
+*-mingw* | *msys*)
+	AC_MSG_RESULT([Windows])
+	backend=windows
+	poll=windows
+	threads=windows
+	create_import_lib=yes
+	AM_CFLAGS="${AM_CFLAGS} -fno-omit-frame-pointer"
+	;;
 *)
 	AC_MSG_RESULT([Null])
 	backend="null"
+	poll=posix
 	threads="posix"
+	;;
 esac
 
 case $backend in
+darwin)
+	AC_DEFINE([OS_DARWIN], [1], [Darwin backend])
+	LIBS="-lobjc -Wl,-framework,IOKit -Wl,-framework,CoreFoundation"
+	LTLDFLAGS="${LTLDFLAGS} -Wl,-prebind"
+	;;
+haiku)
+	AC_DEFINE([OS_HAIKU], [1], [Haiku backend])
+	LIBS="-lbe"
+	;;
 linux)
-	AC_DEFINE(OS_LINUX, 1, [Linux backend])
-	AC_SUBST(OS_LINUX)
+	AC_DEFINE([OS_LINUX], [1], [Linux backend])
 	AC_SEARCH_LIBS([clock_gettime], [rt], [], [], [-pthread])
 	AC_ARG_ENABLE([udev],
 		[AC_HELP_STRING([--enable-udev], [use udev for device enumeration and hotplug support (recommended) [default=yes]])],
 		[], [enable_udev=yes])
-		if test "x$enable_udev" = xyes ; then
-			# system has udev. use it or fail!
-			AC_CHECK_HEADERS([libudev.h], [], [AC_MSG_ERROR([udev support requested but libudev header not installed])])
-			AC_CHECK_LIB([udev], [udev_new], [], [AC_MSG_ERROR([udev support requested but libudev not installed])])
-			AC_DEFINE(USE_UDEV, 1, [Use udev for device enumeration/hotplug])
-		else
-			AC_CHECK_HEADERS([asm/types.h], [], [])
-			AC_CHECK_HEADERS([sys/socket.h linux/netlink.h], [], [AC_MSG_ERROR([Linux netlink headers not found])], [
-#ifdef HAVE_ASM_TYPES_H
-#include <asm/types.h>
-#endif
-#include <sys/socket.h>
-])
-		fi
-		AC_SUBST(USE_UDEV)
-
+	if test "x$enable_udev" = xyes ; then
+		dnl system has udev. use it or fail!
+		AC_CHECK_HEADER([libudev.h], [], [AC_MSG_ERROR([udev support requested but libudev header not installed])])
+		AC_CHECK_LIB([udev], [udev_new], [], [AC_MSG_ERROR([udev support requested but libudev not installed])])
+	else
+		AC_CHECK_HEADERS([asm/types.h])
+		AC_CHECK_HEADER([linux/netlink.h], [], [AC_MSG_ERROR([Linux netlink header not found])])
+		AC_CHECK_HEADER([sys/socket.h], [], [AC_MSG_ERROR([Linux socket header not found])])
+	fi
 	if test "x$is_backend_android" != xyes; then
 		THREAD_CFLAGS="-pthread"
 		LIBS="${LIBS} -pthread"
 	fi
-
-	AC_CHECK_HEADERS([poll.h])
-	AC_DEFINE([POLL_NFDS_TYPE],[nfds_t],[type of second poll() argument])
-	;;
-darwin)
-	AC_DEFINE(OS_DARWIN, 1, [Darwin backend])
-	AC_SUBST(OS_DARWIN)
-	LIBS="-lobjc -Wl,-framework,IOKit -Wl,-framework,CoreFoundation"
-	LTLDFLAGS="${LTLDFLAGS} -Wl,-prebind"
-	AC_CHECK_HEADERS([poll.h])
-	AC_CHECK_TYPE([nfds_t],
-		[AC_DEFINE([POLL_NFDS_TYPE],[nfds_t],[type of second poll() argument])],
-		[AC_DEFINE([POLL_NFDS_TYPE],[unsigned int],[type of second poll() argument])],
-		[#include <poll.h>])
-	;;
-openbsd)
-	AC_DEFINE(OS_OPENBSD, 1, [OpenBSD backend])
-	AC_SUBST(OS_OPENBSD)
-	THREAD_CFLAGS="-pthread"
-	LIBS="-pthread"
-	AC_CHECK_HEADERS([poll.h])
-	AC_DEFINE([POLL_NFDS_TYPE],[nfds_t],[type of second poll() argument])
-	;;
-sunos)
-	AC_DEFINE(OS_SUNOS, 1, [SunOS backend])
-	AC_SUBST(OS_SUNOS)
-	THREAD_CFLAGS="-pthread"
-	LIBS="-pthread -ldevinfo"
-	AC_CHECK_HEADERS([poll.h])
-	AC_DEFINE([POLL_NFDS_TYPE],[nfds_t],[type of second poll() argument])
 	;;
 netbsd)
-	AC_DEFINE(OS_NETBSD, 1, [NetBSD backend])
-	AC_SUBST(OS_NETBSD)
+	AC_DEFINE([OS_NETBSD], [1], [NetBSD backend])
 	THREAD_CFLAGS="-pthread"
 	LIBS="-pthread"
-	AC_CHECK_HEADERS([poll.h])
-	AC_DEFINE([POLL_NFDS_TYPE],[nfds_t],[type of second poll() argument])
-	;;
-windows)
-	AC_DEFINE(OS_WINDOWS, 1, [Windows backend])
-	AC_SUBST(OS_WINDOWS)
-	LIBS=""
-	LTLDFLAGS="${LTLDFLAGS} -avoid-version -Wl,--add-stdcall-alias"
-	AC_DEFINE([POLL_NFDS_TYPE],[unsigned int],[type of second poll() argument])
-	AC_DEFINE([_WIN32_WINNT], [_WIN32_WINNT_VISTA], [Oldest Windows version supported (Vista)])
-	;;
-haiku)
-	AC_DEFINE(OS_HAIKU, 1, [Haiku backend])
-	AC_SUBST(OS_HAIKU)
-	LIBS="${LIBS} -lbe"
-	AC_CHECK_HEADERS([poll.h])
-	AC_DEFINE([POLL_NFDS_TYPE],[nfds_t],[type of second poll() argument])
 	;;
 null)
-	AC_DEFINE(OS_NULL, 1, [Null backend])
-	AC_SUBST(OS_NULL)
+	AC_DEFINE([OS_NULL], [1], [Null backend])
 	THREAD_CFLAGS="-pthread"
 	LIBS="-pthread"
-	AC_CHECK_HEADERS([poll.h])
-	AC_DEFINE([POLL_NFDS_TYPE],[nfds_t],[type of second poll() argument])
+	;;
+openbsd)
+	AC_DEFINE([OS_OPENBSD], [1], [OpenBSD backend])
+	THREAD_CFLAGS="-pthread"
+	LIBS="-pthread"
+	;;
+sunos)
+	AC_DEFINE([OS_SUNOS], [1], [SunOS backend])
+	THREAD_CFLAGS="-pthread"
+	LIBS="-pthread -ldevinfo"
+	;;
+windows)
+	AC_DEFINE([OS_WINDOWS], [1], [Windows backend])
+	AC_CHECK_TYPES([struct timespec], [], [], [[#include <time.h>]])
+	LIBS=""
+	LTLDFLAGS="${LTLDFLAGS} -avoid-version -Wl,--add-stdcall-alias"
+	AC_DEFINE([_WIN32_WINNT], [_WIN32_WINNT_VISTA], [Oldest Windows version supported (Vista)])
+	;;
+*)
+	AC_MSG_ERROR([Unknown backend])
 	;;
 esac
 
 AC_SUBST(LIBS)
 
-AM_CONDITIONAL(OS_LINUX, test "x$backend" = xlinux)
-AM_CONDITIONAL(OS_DARWIN, test "x$backend" = xdarwin)
-AM_CONDITIONAL(OS_OPENBSD, test "x$backend" = xopenbsd)
-AM_CONDITIONAL(OS_SUNOS, test "x$backend" = xsunos)
-AM_CONDITIONAL(OS_NETBSD, test "x$backend" = xnetbsd)
-AM_CONDITIONAL(OS_WINDOWS, test "x$backend" = xwindows)
-AM_CONDITIONAL(OS_HAIKU, test "x$backend" = xhaiku)
-AM_CONDITIONAL(OS_NULL, test "x$backend" = xnull)
-AM_CONDITIONAL(THREADS_POSIX, test "x$threads" = xposix)
-AM_CONDITIONAL(CREATE_IMPORT_LIB, test "x$create_import_lib" = xyes)
-AM_CONDITIONAL(USE_UDEV, test "x$enable_udev" = xyes)
-if test "x$threads" = xposix; then
-	AC_DEFINE(THREADS_POSIX, 1, [Use POSIX Threads])
-fi
+AM_CONDITIONAL([OS_DARWIN], [test "x$backend" = xdarwin])
+AM_CONDITIONAL([OS_HAIKU], [test "x$backend" = xhaiku])
+AM_CONDITIONAL([OS_LINUX], [test "x$backend" = xlinux])
+AM_CONDITIONAL([OS_NETBSD], [test "x$backend" = xnetbsd])
+AM_CONDITIONAL([OS_NULL], [test "x$backend" = xnull])
+AM_CONDITIONAL([OS_OPENBSD], [test "x$backend" = xopenbsd])
+AM_CONDITIONAL([OS_SUNOS], [test "x$backend" = xsunos])
+AM_CONDITIONAL([OS_WINDOWS], [test "x$backend" = xwindows])
+AM_CONDITIONAL([POLL_POSIX], [test "x$threads" = xposix])
+AM_CONDITIONAL([POLL_WINDOWS], [test "x$threads" = xwindows])
+AM_CONDITIONAL([THREADS_POSIX], [test "x$threads" = xposix])
+AM_CONDITIONAL([THREADS_WINDOWS], [test "x$threads" = xwindows])
+AM_CONDITIONAL([CREATE_IMPORT_LIB], [test "x$create_import_lib" = xyes])
+AM_CONDITIONAL([USE_UDEV], [test "x$enable_udev" = xyes])
 
-# timerfd
-AC_CHECK_HEADER([sys/timerfd.h], [timerfd_h=1], [timerfd_h=0])
-AC_ARG_ENABLE([timerfd],
-	[AS_HELP_STRING([--enable-timerfd],
-		[use timerfd for timing [default=auto]])],
-	[use_timerfd=$enableval], [use_timerfd=auto])
-
-if test "x$use_timerfd" = xyes -a "x$timerfd_h" = x0; then
-	AC_MSG_ERROR([timerfd header not available; glibc 2.9+ required])
-fi
-
-AC_CHECK_DECLS([TFD_NONBLOCK, TFD_CLOEXEC], [tfd_hdr_ok=yes], [tfd_hdr_ok=no], [#include <sys/timerfd.h>])
-if test "x$use_timerfd" = xyes -a "x$tfd_hdr_ok" = xno; then
-	AC_MSG_ERROR([timerfd header not usable; glibc 2.9+ required])
-fi
-
-AC_MSG_CHECKING([whether to use timerfd for timing])
-if test "x$use_timerfd" = xno; then
-	AC_MSG_RESULT([no (disabled by user)])
+if test "x$poll" = xposix; then
+	AC_DEFINE([POLL_POSIX], [1], [Use POSIX poll() implementation])
+	AC_CHECK_TYPES([nfds_t], [], [], [[#include <poll.h>]])
+	AC_CHECK_FUNCS([pipe2])
+elif test "x$poll" = xwindows; then
+	AC_DEFINE([POLL_WINDOWS], [1], [Use Windows poll() implementation])
 else
-	if test "x$timerfd_h" = x1 -a "x$tfd_hdr_ok" = xyes; then
-		AC_MSG_RESULT([yes])
-		AC_DEFINE(USBI_TIMERFD_AVAILABLE, 1, [timerfd headers available])
-	else
+	AC_MSG_ERROR([Unknown poll implementation])
+fi
+
+if test "x$threads" = xposix; then
+	AC_DEFINE([THREADS_POSIX], [1], [Use POSIX Threads])
+elif test "x$threads" = xwindows; then
+	AC_DEFINE([THREADS_WINDOWS], [1], [Use Windows Threads])
+else
+	AC_MSG_ERROR([Unknown threads implementation])
+fi
+
+dnl headers not available on all platforms but required on others
+AC_CHECK_HEADERS([sys/time.h])
+
+dnl timerfd support
+if test "x$backend" = xlinux || test "x$backend" = xsunos; then
+	AC_ARG_ENABLE([timerfd],
+		[AS_HELP_STRING([--enable-timerfd], [use timerfd for timing [default=auto]])],
+		[use_timerfd=$enableval],
+		[use_timerfd=auto])
+	if test "x$use_timerfd" != xno; then
+		AC_CHECK_HEADER([sys/timerfd.h], [timerfd_h=yes], [timerfd_h=no])
+		if test "x$timerfd_h" = xyes; then
+			AC_CHECK_DECLS([TFD_NONBLOCK, TFD_CLOEXEC], [timerfd_h_ok=yes], [timerfd_h_ok=no], [[#include <sys/timerfd.h>]])
+			if test "x$timerfd_h_ok" = xyes; then
+				AC_CHECK_FUNC([timerfd_create], [timerfd_ok=yes], [timerfd_ok=no])
+				if test "x$timerfd_ok" = xyes; then
+					AC_DEFINE([HAVE_TIMERFD], [1], [Define if the system has timerfd functionality])
+				elif test "x$use_timerfd" = xyes; then
+					AC_MSG_ERROR([timerfd_create() function not found; glibc 2.9+ required])
+				fi
+			elif test "x$use_timerfd" = xyes; then
+				AC_MSG_ERROR([timerfd header not usable; glibc 2.9+ required])
+			fi
+		elif test "x$use_timerfd" = xyes; then
+			AC_MSG_ERROR([timerfd header not available; glibc 2.9+ required])
+		fi
+	fi
+	AC_MSG_CHECKING([whether to use timerfd for timing])
+	if test "x$use_timerfd" = xno; then
+		AC_MSG_RESULT([no (disabled by user)])
+	elif test "x$timerfd_h" = xno; then
+		AC_MSG_RESULT([no (header not usable)])
+	elif test "x$timerfd_h_ok" = xno; then
 		AC_MSG_RESULT([no (header not available)])
+	elif test "x$timerfd_ok" = xno; then
+		AC_MSG_RESULT([no (functions not available)])
+	else
+		AC_MSG_RESULT([yes])
 	fi
 fi
 
-AC_CHECK_FUNCS([pipe2])
-AC_CHECK_TYPES([struct timespec])
-
-# Message logging
-AC_ARG_ENABLE([log], [AS_HELP_STRING([--disable-log], [disable all logging])],
+dnl Message logging
+AC_ARG_ENABLE([log],
+	[AS_HELP_STRING([--disable-log], [disable all logging])],
 	[log_enabled=$enableval],
 	[log_enabled=yes])
 if test "x$log_enabled" != xno; then
-	AC_DEFINE([ENABLE_LOGGING], 1, [Message logging])
+	AC_DEFINE([ENABLE_LOGGING], [1], [Message logging])
 fi
 
-AC_ARG_ENABLE([debug-log], [AS_HELP_STRING([--enable-debug-log],
-	[start with debug message logging enabled [default=no]])],
+AC_ARG_ENABLE([debug-log],
+	[AS_HELP_STRING([--enable-debug-log], [start with debug message logging enabled [default=no]])],
 	[debug_log_enabled=$enableval],
 	[debug_log_enabled=no])
 if test "x$debug_log_enabled" != xno; then
-	AC_DEFINE([ENABLE_DEBUG_LOGGING], 1, [Start with debug message logging enabled])
+	AC_DEFINE([ENABLE_DEBUG_LOGGING], [1], [Start with debug message logging enabled])
 fi
 
-AC_ARG_ENABLE([system-log], [AS_HELP_STRING([--enable-system-log],
-	[output logging messages to system wide log, if supported by the OS [default=no]])],
+AC_ARG_ENABLE([system-log],
+	[AS_HELP_STRING([--enable-system-log], [output logging messages to system wide log, if supported by the OS [default=no]])],
 	[system_log_enabled=$enableval],
 	[system_log_enabled=no])
 if test "x$system_log_enabled" != xno; then
-	AC_DEFINE([USE_SYSTEM_LOGGING_FACILITY], 1, [Enable output to system log])
-
-	# Check if syslog is available in standard C library
-	AC_CHECK_HEADERS(syslog.h)
-	AC_CHECK_FUNC([syslog], [have_syslog=yes], [have_syslog=no])
-	if test "x$have_syslog" != xno; then
-		AC_DEFINE([HAVE_SYSLOG_FUNC], 1, [syslog() function available])
+	AC_DEFINE([USE_SYSTEM_LOGGING_FACILITY], [1], [Enable output to system log])
+	if test "x$backend" != xwindows && test "x$is_backend_android" != xyes; then
+		dnl Check if syslog is available in standard C library
+		AC_CHECK_HEADER([syslog.h], [syslog_h=yes], [syslog_h=no])
+		if test "x$syslog_h" = xyes; then
+			AC_CHECK_FUNCS([syslog])
+		fi
 	fi
 fi
 
-# Examples build
-AC_ARG_ENABLE([examples-build], [AS_HELP_STRING([--enable-examples-build],
-	[build example applications [default=no]])],
+dnl Examples build
+AC_ARG_ENABLE([examples-build],
+	[AS_HELP_STRING([--enable-examples-build], [build example applications [default=no]])],
 	[build_examples=$enableval],
 	[build_examples=no])
-AM_CONDITIONAL(BUILD_EXAMPLES, test "x$build_examples" != xno)
+AM_CONDITIONAL([BUILD_EXAMPLES], [test "x$build_examples" != xno])
 
-# Tests build
-AC_ARG_ENABLE([tests-build], [AS_HELP_STRING([--enable-tests-build],
-	[build test applications [default=no]])],
+dnl Tests build
+AC_ARG_ENABLE([tests-build],
+	[AS_HELP_STRING([--enable-tests-build], [build test applications [default=no]])],
 	[build_tests=$enableval],
 	[build_tests=no])
-AM_CONDITIONAL(BUILD_TESTS, test "x$build_tests" != xno)
+AM_CONDITIONAL([BUILD_TESTS], [test "x$build_tests" != xno])
 
-# headers not available on all platforms but required on others
-AC_CHECK_HEADERS([sys/time.h])
+dnl sigaction needed for some example programs
+if test "x$build_examples" != xno; then
+	AC_CHECK_FUNC([sigaction], [have_sigaction=yes], [have_sigaction=no])
+fi
+AM_CONDITIONAL([HAVE_SIGACTION], [test "x$have_sigaction" = xyes])
 
-# sigaction not available on MinGW
-AC_CHECK_FUNC([sigaction], [have_sigaction=yes], [have_sigaction=no])
-AM_CONDITIONAL(HAVE_SIGACTION, test "x$have_sigaction" = xyes)
-
-# check for -fvisibility=hidden compiler support (GCC >= 3.4)
-saved_cflags="$CFLAGS"
-# -Werror required for cygwin
-CFLAGS="$CFLAGS -Werror -fvisibility=hidden"
+dnl check for -fvisibility=hidden compiler support (GCC >= 3.4)
+saved_CFLAGS="${CFLAGS}"
+dnl -Werror required for cygwin
+CFLAGS="${CFLAGS} -Werror -fvisibility=hidden"
 AC_COMPILE_IFELSE([AC_LANG_PROGRAM([])],
 	[VISIBILITY_CFLAGS="-fvisibility=hidden"
 	 AC_DEFINE([DEFAULT_VISIBILITY], [__attribute__((visibility("default")))], [Default visibility])],
 	[VISIBILITY_CFLAGS=""
 	 AC_DEFINE([DEFAULT_VISIBILITY], [], [Default visibility])],
 	])
-CFLAGS="$saved_cflags"
+CFLAGS="${saved_CFLAGS}"
 
-# check for -Wno-pointer-sign compiler support (GCC >= 4)
-saved_cflags="$CFLAGS"
-CFLAGS="$CFLAGS -Wno-pointer-sign"
+dnl check for -Wno-pointer-sign compiler support (GCC >= 4)
+saved_CFLAGS="${CFLAGS}"
+CFLAGS="${CFLAGS} -Wno-pointer-sign"
 AC_COMPILE_IFELSE([AC_LANG_PROGRAM([])],
 	nopointersign_cflags="-Wno-pointer-sign", nopointersign_cflags="")
-CFLAGS="$saved_cflags"
+CFLAGS="${saved_CFLAGS}"
 
-# check for -std=gnu99 compiler support
-saved_cflags="$CFLAGS"
+dnl check for -std=gnu99 compiler support
+saved_CFLAGS="${CFLAGS}"
 CFLAGS="-std=gnu99"
 AC_MSG_CHECKING([whether CC supports -std=gnu99])
 AC_COMPILE_IFELSE([AC_LANG_PROGRAM([])],
-    [AC_MSG_RESULT([yes])]
-    [AM_CFLAGS="${AM_CFLAGS} -std=gnu99"],
-    [AC_MSG_RESULT([no])]
+	[AC_MSG_RESULT([yes])
+	 AM_CFLAGS="${AM_CFLAGS} -std=gnu99"],
+	[AC_MSG_RESULT([no])]
 )
-CFLAGS="$saved_cflags"
+CFLAGS="${saved_CFLAGS}"
 
-AM_CFLAGS="${AM_CFLAGS} -Wall -Wundef -Wunused -Wstrict-prototypes -Werror-implicit-function-declaration $nopointersign_cflags -Wshadow ${THREAD_CFLAGS} ${VISIBILITY_CFLAGS}"
+AM_CFLAGS="${AM_CFLAGS} -Wall -Wundef -Wunused -Wstrict-prototypes -Werror-implicit-function-declaration ${nopointersign_cflags} -Wshadow ${THREAD_CFLAGS} ${VISIBILITY_CFLAGS}"
 
 AC_SUBST(AM_CFLAGS)
 AC_SUBST(LTLDFLAGS)
diff --git a/libusb/Makefile.am b/libusb/Makefile.am
index 7a6b530..1e09222 100644
--- a/libusb/Makefile.am
+++ b/libusb/Makefile.am
@@ -5,69 +5,86 @@
 lib_LTLIBRARIES = libusb-1.0.la
 
 POSIX_POLL_SRC = os/poll_posix.h os/poll_posix.c
-POSIX_THREADS_SRC = os/threads_posix.h os/threads_posix.c
 WINDOWS_POLL_SRC = os/poll_windows.h os/poll_windows.c
+
+if POLL_POSIX
+POLL_SRC = $(POSIX_POLL_SRC)
+else
+POLL_SRC = $(WINDOWS_POLL_SRC)
+endif
+
+POSIX_THREADS_SRC = os/threads_posix.h os/threads_posix.c
 WINDOWS_THREADS_SRC = os/threads_windows.h os/threads_windows.c
-LINUX_USBFS_SRC = os/linux_usbfs.h os/linux_usbfs.c
-DARWIN_USB_SRC = os/darwin_usb.h os/darwin_usb.c
-OPENBSD_USB_SRC = os/openbsd_usb.c
-NETBSD_USB_SRC = os/netbsd_usb.c
-SUNOS_USB_SRC = os/sunos_usb.c os/sunos_usb.h
-WINDOWS_USB_SRC = libusb-1.0.def libusb-1.0.rc \
-		os/windows_common.h os/windows_common.c \
-		os/windows_usbdk.h os/windows_usbdk.c \
-		os/windows_winusb.h os/windows_winusb.c
-HAIKU_USB_SRC = os/haiku_usb.h os/haiku_usb_backend.cpp \
-		os/haiku_usb_raw.h os/haiku_usb_raw.cpp os/haiku_pollfs.cpp
-NULL_USB_SRC = os/null_usb.c
+
+if THREADS_POSIX
+THREADS_SRC = $(POSIX_THREADS_SRC)
+else
+THREADS_SRC = $(WINDOWS_THREADS_SRC)
+endif
+
+OS_DARWIN_SRC = os/darwin_usb.h os/darwin_usb.c
+OS_HAIKU_SRC = os/haiku_usb.h os/haiku_usb_backend.cpp \
+	       os/haiku_pollfs.cpp os/haiku_usb_raw.h os/haiku_usb_raw.cpp
+OS_LINUX_SRC = os/linux_usbfs.h os/linux_usbfs.c
+OS_NETBSD_SRC = os/netbsd_usb.c
+OS_NULL_SRC = os/null_usb.c
+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 ps/windows_common.c \
+		 os/windows_usbdk.h os/windows_usbdk.c \
+		 os/windows_winusb.h os/windows_winusb.c
 
 EXTRA_DIST = $(POSIX_POLL_SRC) $(POSIX_THREADS_SRC) \
 	$(WINDOWS_POLL_SRC) $(WINDOWS_THREADS_SRC) \
-	$(LINUX_USBFS_SRC) $(DARWIN_USB_SRC) \
-	$(OPENBSD_USB_SRC) $(NETBSD_USB_SRC) \
-	$(WINDOWS_USB_SRC) \
-	$(HAIKU_USB_SRC) $(NULL_USB_SRC) \
-	os/linux_udev.c os/linux_netlink.c
-
-if OS_LINUX
-
-if USE_UDEV
-OS_SRC = $(LINUX_USBFS_SRC) os/linux_udev.c
-else
-OS_SRC = $(LINUX_USBFS_SRC) os/linux_netlink.c
-endif
-
-endif
+	$(OS_DARWIN_SRC) \
+	$(OS_HAIKU_SRC) \
+	$(OS_LINUX_SRC) \
+	$(OS_NETBSD_SRC) \
+	$(OS_NULL_SRC) \
+	$(OS_OPENBSD_SRC) \
+	$(OS_SUNOS_SRC) \
+	$(OS_WINDOWS_SRC) \
+	os/linux_netlink.c os/linux_udev.c
 
 if OS_DARWIN
-OS_SRC = $(DARWIN_USB_SRC)
+OS_SRC = $(OS_DARWIN_SRC)
 AM_CFLAGS_EXT = -no-cpp-precomp
 endif
 
-if OS_OPENBSD
-OS_SRC = $(OPENBSD_USB_SRC)
-endif
-
-if OS_NETBSD
-OS_SRC = $(NETBSD_USB_SRC)
-endif
-
-if OS_SUNOS
-OS_SRC = $(SUNOS_USB_SRC)
-endif
-
 if OS_HAIKU
 noinst_LTLIBRARIES = libusb_haiku.la
-libusb_haiku_la_SOURCES = $(HAIKU_USB_SRC)
+libusb_haiku_la_SOURCES = $(OS_HAIKU_SRC)
 libusb_1_0_la_LIBADD = libusb_haiku.la
 endif
 
+if OS_LINUX
+OS_SRC = $(OS_LINUX_SRC)
+if USE_UDEV
+OS_SRC += os/linux_udev.c
+else
+OS_SRC += os/linux_netlink.c
+endif
+endif
+
+if OS_NETBSD
+OS_SRC = $(OS_NETBSD_SRC)
+endif
+
 if OS_NULL
-OS_SRC = $(NULL_USB_SRC)
+OS_SRC = $(OS_NULL_SRC)
+endif
+
+if OS_OPENBSD
+OS_SRC = $(OS_OPENBSD_SRC)
+endif
+
+if OS_SUNOS
+OS_SRC = $(OS_SUNOS_SRC)
 endif
 
 if OS_WINDOWS
-OS_SRC = $(WINDOWS_USB_SRC)
+OS_SRC = $(OS_WINDOWS_SRC)
 
 .rc.lo:
 	$(AM_V_GEN)$(LIBTOOL) $(AM_V_lt) $(AM_LIBTOOLFLAGS) $(LIBTOOLFLAGS) --tag=RC --mode=compile $(RC) $(RCFLAGS) -i $< -o $@
@@ -81,18 +98,6 @@
 	$(AM_V_GEN)$(DLLTOOL) $(DLLTOOLFLAGS) --kill-at --input-def $(srcdir)/libusb-1.0.def --dllname $@ --output-lib .libs/$@.a
 endif
 
-if OS_WINDOWS
-POLL_SRC = $(WINDOWS_POLL_SRC)
-else
-POLL_SRC = $(POSIX_POLL_SRC)
-endif
-
-if THREADS_POSIX
-THREADS_SRC = $(POSIX_THREADS_SRC)
-else
-THREADS_SRC = $(WINDOWS_THREADS_SRC)
-endif
-
 libusb_1_0_la_CFLAGS = $(AM_CFLAGS)
 libusb_1_0_la_LDFLAGS = $(LTLDFLAGS)
 libusb_1_0_la_SOURCES = libusbi.h libusb.h version.h version_nano.h \
diff --git a/libusb/core.c b/libusb/core.c
index bf68984..9ec9246 100644
--- a/libusb/core.c
+++ b/libusb/core.c
@@ -28,19 +28,19 @@
 #endif
 #include <stdio.h>
 #include <string.h>
-#ifdef HAVE_SYSLOG_H
+#ifdef HAVE_SYSLOG
 #include <syslog.h>
 #endif
 
-struct libusb_context *usbi_default_context = NULL;
+struct libusb_context *usbi_default_context;
 static const struct libusb_version libusb_version_internal =
 	{ LIBUSB_MAJOR, LIBUSB_MINOR, LIBUSB_MICRO, LIBUSB_NANO,
 	  LIBUSB_RC, "http://libusb.info" };
-static int default_context_refcnt = 0;
+static int default_context_refcnt;
 static usbi_mutex_static_t default_context_lock = USBI_MUTEX_INITIALIZER;
-static struct timespec timestamp_origin = { 0, 0 };
-#ifndef USE_SYSTEM_LOGGING_FACILITY
-static libusb_log_cb log_handler = NULL;
+static struct timespec timestamp_origin;
+#if defined(ENABLE_LOGGING) && !defined(USE_SYSTEM_LOGGING_FACILITY)
+static libusb_log_cb log_handler;
 #endif
 
 usbi_mutex_static_t active_contexts_lock = USBI_MUTEX_INITIALIZER;
@@ -2130,23 +2130,24 @@
 void API_EXPORTED libusb_set_log_cb(libusb_context *ctx, libusb_log_cb cb,
 	int mode)
 {
+#if defined(ENABLE_LOGGING) && (!defined(ENABLE_DEBUG_LOGGING) || !defined(USE_SYSTEM_LOGGING_FACILITY))
 #if !defined(USE_SYSTEM_LOGGING_FACILITY)
-	if (mode & LIBUSB_LOG_CB_GLOBAL) {
+	if (mode & LIBUSB_LOG_CB_GLOBAL)
 		log_handler = cb;
-	}
 #endif
-#if defined(ENABLE_LOGGING) && !defined(ENABLE_DEBUG_LOGGING)
+#if !defined(ENABLE_DEBUG_LOGGING)
 	if (mode & LIBUSB_LOG_CB_CONTEXT) {
 		USBI_GET_CONTEXT(ctx);
 		ctx->log_handler = cb;
 	}
 #else
 	UNUSED(ctx);
-#if defined(USE_SYSTEM_LOGGING_FACILITY)
+#endif
+#else
+	UNUSED(ctx);
 	UNUSED(cb);
 	UNUSED(mode);
 #endif
-#endif
 }
 
 /** \ingroup libusb_lib
@@ -2506,7 +2507,7 @@
 static void usbi_log_str(enum libusb_log_level level, const char *str)
 {
 #if defined(USE_SYSTEM_LOGGING_FACILITY)
-#if defined(OS_WINDOWS) || defined(OS_WINCE)
+#if defined(OS_WINDOWS)
 #if !defined(UNICODE)
 	OutputDebugStringA(str);
 #else
@@ -2524,7 +2525,7 @@
 	case LIBUSB_LOG_LEVEL_DEBUG: priority = ANDROID_LOG_DEBUG; break;
 	}
 	__android_log_write(priority, "libusb", str);
-#elif defined(HAVE_SYSLOG_FUNC)
+#elif defined(HAVE_SYSLOG)
 	int syslog_level = LOG_INFO;
 	switch (level) {
 	case LIBUSB_LOG_LEVEL_NONE: return;
diff --git a/libusb/io.c b/libusb/io.c
index af6c1ae..4d0971b 100644
--- a/libusb/io.c
+++ b/libusb/io.c
@@ -25,7 +25,7 @@
 #include "hotplug.h"
 
 #include <errno.h>
-#ifdef USBI_TIMERFD_AVAILABLE
+#ifdef HAVE_TIMERFD
 #include <sys/timerfd.h>
 #include <unistd.h>
 #endif
@@ -1137,7 +1137,7 @@
 	if (r < 0)
 		goto err_close_pipe;
 
-#ifdef USBI_TIMERFD_AVAILABLE
+#ifdef HAVE_TIMERFD
 	ctx->timerfd = timerfd_create(usbi_backend.get_timerfd_clockid(),
 		TFD_NONBLOCK | TFD_CLOEXEC);
 	if (ctx->timerfd >= 0) {
@@ -1152,7 +1152,7 @@
 
 	return 0;
 
-#ifdef USBI_TIMERFD_AVAILABLE
+#ifdef HAVE_TIMERFD
 err_close_timerfd:
 	close(ctx->timerfd);
 	usbi_remove_pollfd(ctx, ctx->event_pipe[0]);
@@ -1184,7 +1184,7 @@
 	usbi_remove_pollfd(ctx, ctx->event_pipe[0]);
 	usbi_close(ctx->event_pipe[0]);
 	usbi_close(ctx->event_pipe[1]);
-#ifdef USBI_TIMERFD_AVAILABLE
+#ifdef HAVE_TIMERFD
 	if (usbi_using_timerfd(ctx)) {
 		usbi_remove_pollfd(ctx, ctx->timerfd);
 		close(ctx->timerfd);
@@ -1313,7 +1313,7 @@
 	free(itransfer);
 }
 
-#ifdef USBI_TIMERFD_AVAILABLE
+#ifdef HAVE_TIMERFD
 static int disarm_timerfd(struct libusb_context *ctx)
 {
 	const struct itimerspec disarm_timer = { { 0, 0 }, { 0, 0 } };
@@ -1414,7 +1414,7 @@
 	/* otherwise we need to be inserted at the end */
 	list_add_tail(&transfer->list, &ctx->flying_transfers);
 out:
-#ifdef USBI_TIMERFD_AVAILABLE
+#ifdef HAVE_TIMERFD
 	if (first && usbi_using_timerfd(ctx) && timerisset(timeout)) {
 		/* if this transfer has the lowest timeout of all active transfers,
 		 * rearm the timerfd with this transfer's timeout */
@@ -2059,7 +2059,7 @@
 	return r;
 }
 
-#ifdef USBI_TIMERFD_AVAILABLE
+#ifdef HAVE_TIMERFD
 static int handle_timerfd_trigger(struct libusb_context *ctx)
 {
 	int r;
@@ -2086,8 +2086,8 @@
 {
 	int r;
 	struct usbi_pollfd *ipollfd;
-	POLL_NFDS_TYPE nfds = 0;
-	POLL_NFDS_TYPE internal_nfds;
+	usbi_nfds_t nfds = 0;
+	usbi_nfds_t internal_nfds;
 	struct pollfd *fds = NULL;
 	int timeout_ms;
 
@@ -2266,7 +2266,7 @@
 			goto done;
 	}
 
-#ifdef USBI_TIMERFD_AVAILABLE
+#ifdef HAVE_TIMERFD
 	/* on timerfd configurations, fds[1] is the timerfd */
 	if (usbi_using_timerfd(ctx) && fds[1].revents) {
 		/* timerfd indicates that a timeout has expired */
@@ -2286,7 +2286,7 @@
 #endif
 
 	list_for_each_entry(ipollfd, &ctx->removed_ipollfds, list, struct usbi_pollfd) {
-		POLL_NFDS_TYPE n;
+		usbi_nfds_t n;
 
 		for (n = internal_nfds ; n < nfds ; n++) {
 			if (ipollfd->pollfd.fd == fds[n].fd) {
@@ -2546,7 +2546,7 @@
  */
 int API_EXPORTED libusb_pollfds_handle_timeouts(libusb_context *ctx)
 {
-#if defined(USBI_TIMERFD_AVAILABLE)
+#ifdef HAVE_TIMERFD
 	USBI_GET_CONTEXT(ctx);
 	return usbi_using_timerfd(ctx);
 #else
diff --git a/libusb/libusbi.h b/libusb/libusbi.h
index c31b49b..68bbaae 100644
--- a/libusb/libusbi.h
+++ b/libusb/libusbi.h
@@ -76,17 +76,16 @@
 #endif
 
 /* Internal abstraction for poll */
-#if defined(OS_LINUX) || defined(OS_DARWIN) || defined(OS_OPENBSD) || defined(OS_NETBSD) || \
-	defined(OS_HAIKU) || defined(OS_SUNOS) || defined(OS_NULL)
+#if defined(POLL_POSIX)
 #include "os/poll_posix.h"
-#elif defined(OS_WINDOWS)
+#elif defined(POLL_WINDOWS)
 #include "os/poll_windows.h"
 #endif
 
 /* Internal abstraction for thread synchronization */
 #if defined(THREADS_POSIX)
 #include "os/threads_posix.h"
-#elif defined(OS_WINDOWS)
+#elif defined(THREADS_WINDOWS)
 #include "os/threads_windows.h"
 #endif
 
@@ -351,7 +350,7 @@
          * between the poll call and */
         struct list_head removed_ipollfds;
 	struct pollfd *pollfds;
-	POLL_NFDS_TYPE pollfds_cnt;
+	usbi_nfds_t pollfds_cnt;
 
 	/* A list of pending hotplug messages. Protected by event_data_lock. */
 	struct list_head hotplug_msgs;
@@ -359,7 +358,7 @@
 	/* A list of pending completed transfers. Protected by event_data_lock. */
 	struct list_head completed_transfers;
 
-#ifdef USBI_TIMERFD_AVAILABLE
+#ifdef HAVE_TIMERFD
 	/* used for timeout handling, if supported by OS.
 	 * this timerfd is maintained to trigger on the next pending timeout */
 	int timerfd;
@@ -396,7 +395,7 @@
 	((ctx)->event_flags || (ctx)->device_close \
 	 || !list_empty(&(ctx)->hotplug_msgs) || !list_empty(&(ctx)->completed_transfers))
 
-#ifdef USBI_TIMERFD_AVAILABLE
+#ifdef HAVE_TIMERFD
 #define usbi_using_timerfd(ctx) ((ctx)->timerfd >= 0)
 #else
 #define usbi_using_timerfd(ctx) (0)
@@ -1105,7 +1104,7 @@
 	 * Return 0 on success, or a LIBUSB_ERROR code on failure.
 	 */
 	int (*handle_events)(struct libusb_context *ctx,
-		struct pollfd *fds, POLL_NFDS_TYPE nfds, int num_ready);
+		struct pollfd *fds, usbi_nfds_t nfds, int num_ready);
 
 	/* Handle transfer completion. Optional.
 	 *
@@ -1142,7 +1141,7 @@
 	 */
 	int (*clock_gettime)(int clkid, struct timespec *tp);
 
-#ifdef USBI_TIMERFD_AVAILABLE
+#ifdef HAVE_TIMERFD
 	/* clock ID of the clock that should be used for timerfd */
 	clockid_t (*get_timerfd_clockid)(void);
 #endif
diff --git a/libusb/os/linux_usbfs.c b/libusb/os/linux_usbfs.c
index 1b73fde..33affb0 100644
--- a/libusb/os/linux_usbfs.c
+++ b/libusb/os/linux_usbfs.c
@@ -145,7 +145,7 @@
 static int sysfs_scan_device(struct libusb_context *ctx, const char *devname);
 static int detach_kernel_driver_and_claim(struct libusb_device_handle *, int);
 
-#if !defined(USE_UDEV)
+#if !defined(HAVE_LIBUDEV)
 static int linux_default_scan_devices (struct libusb_context *ctx);
 #endif
 
@@ -344,7 +344,7 @@
  * simply assume /dev/bus/usb rather then making libusb_init fail.
  * Make the same assumption for Android where SELinux policies might block us
  * from reading /dev on newer devices. */
-#if defined(USE_UDEV) || defined(__ANDROID__)
+#if defined(HAVE_LIBUDEV) || defined(__ANDROID__)
 	if (ret == NULL)
 		ret = "/dev/bus/usb";
 #endif
@@ -532,7 +532,7 @@
 
 static int linux_start_event_monitor(void)
 {
-#if defined(USE_UDEV)
+#if defined(HAVE_LIBUDEV)
 	return linux_udev_start_event_monitor();
 #elif !defined(__ANDROID__)
 	return linux_netlink_start_event_monitor();
@@ -543,7 +543,7 @@
 
 static int linux_stop_event_monitor(void)
 {
-#if defined(USE_UDEV)
+#if defined(HAVE_LIBUDEV)
 	return linux_udev_stop_event_monitor();
 #elif !defined(__ANDROID__)
 	return linux_netlink_stop_event_monitor();
@@ -558,7 +558,7 @@
 
 	usbi_mutex_static_lock(&linux_hotplug_lock);
 
-#if defined(USE_UDEV)
+#if defined(HAVE_LIBUDEV)
 	ret = linux_udev_scan_devices(ctx);
 #else
 	ret = linux_default_scan_devices(ctx);
@@ -571,7 +571,7 @@
 
 static void op_hotplug_poll(void)
 {
-#if defined(USE_UDEV)
+#if defined(HAVE_LIBUDEV)
 	linux_udev_hotplug_poll();
 #elif !defined(__ANDROID__)
 	linux_netlink_hotplug_poll();
@@ -1229,7 +1229,7 @@
 	usbi_mutex_static_unlock(&active_contexts_lock);
 }
 
-#if !defined(USE_UDEV)
+#if !defined(HAVE_LIBUDEV)
 /* open a bus directory and adds all discovered devices to the context */
 static int usbfs_scan_busdir(struct libusb_context *ctx, uint8_t busnum)
 {
@@ -1332,7 +1332,7 @@
 		devname);
 }
 
-#if !defined(USE_UDEV)
+#if !defined(HAVE_LIBUDEV)
 static int sysfs_get_device_list(struct libusb_context *ctx)
 {
 	DIR *devices = opendir(SYSFS_DEVICE_PATH);
@@ -2760,14 +2760,14 @@
 }
 
 static int op_handle_events(struct libusb_context *ctx,
-	struct pollfd *fds, POLL_NFDS_TYPE nfds, int num_ready)
+	struct pollfd *fds, usbi_nfds_t nfds, int num_ready)
 {
+	usbi_nfds_t n;
 	int r;
-	unsigned int i = 0;
 
 	usbi_mutex_lock(&ctx->open_devs_lock);
-	for (i = 0; i < nfds && num_ready > 0; i++) {
-		struct pollfd *pollfd = &fds[i];
+	for (n = 0; n < nfds && num_ready > 0; n++) {
+		struct pollfd *pollfd = &fds[n];
 		struct libusb_device_handle *handle;
 		struct linux_device_handle_priv *hpriv = NULL;
 
@@ -2839,7 +2839,7 @@
   }
 }
 
-#ifdef USBI_TIMERFD_AVAILABLE
+#ifdef HAVE_TIMERFD
 static clockid_t op_get_timerfd_clockid(void)
 {
 	return monotonic_clkid;
@@ -2890,7 +2890,7 @@
 
 	.clock_gettime = op_clock_gettime,
 
-#ifdef USBI_TIMERFD_AVAILABLE
+#ifdef HAVE_TIMERFD
 	.get_timerfd_clockid = op_get_timerfd_clockid,
 #endif
 
diff --git a/libusb/os/poll_posix.h b/libusb/os/poll_posix.h
index 01702f3..48c4904 100644
--- a/libusb/os/poll_posix.h
+++ b/libusb/os/poll_posix.h
@@ -4,6 +4,12 @@
 #include <poll.h>
 #include <unistd.h>
 
+#ifdef HAVE_NFDS_T
+typedef nfds_t usbi_nfds_t;
+#else
+typedef unsigned int usbi_nfds_t;
+#endif
+
 #define usbi_write	write
 #define usbi_read	read
 #define usbi_close	close
diff --git a/libusb/os/poll_windows.c b/libusb/os/poll_windows.c
index 21a1363..bd8ac67 100644
--- a/libusb/os/poll_windows.c
+++ b/libusb/os/poll_windows.c
@@ -334,12 +334,12 @@
  * Currently, this function only accepts one of POLLIN or POLLOUT per fd
  * (but you can create multiple fds from the same handle for read and write)
  */
-int usbi_poll(struct pollfd *fds, unsigned int nfds, int timeout)
+int usbi_poll(struct pollfd *fds, usbi_nfds_t nfds, int timeout)
 {
 	struct file_descriptor **fds_array;
 	HANDLE *handles_array;
 	struct file_descriptor *fd;
-	unsigned int n;
+	usbi_nfds_t n;
 	int nready;
 
 	if (nfds <= MAXIMUM_WAIT_OBJECTS) {
diff --git a/libusb/os/poll_windows.h b/libusb/os/poll_windows.h
index 7decd4a..c78c9d3 100644
--- a/libusb/os/poll_windows.h
+++ b/libusb/os/poll_windows.h
@@ -41,6 +41,8 @@
 #define POLLHUP		0x0010	/* Hung up */
 #define POLLNVAL	0x0020	/* Invalid request: fd not open */
 
+typedef unsigned int usbi_nfds_t;
+
 struct pollfd {
 	int fd;		/* file descriptor */
 	short events;	/* requested events */
@@ -57,7 +59,7 @@
 struct winfd usbi_create_fd(void);
 
 int usbi_pipe(int pipefd[2]);
-int usbi_poll(struct pollfd *fds, unsigned int nfds, int timeout);
+int usbi_poll(struct pollfd *fds, usbi_nfds_t nfds, int timeout);
 ssize_t usbi_write(int fd, const void *buf, size_t count);
 ssize_t usbi_read(int fd, void *buf, size_t count);
 int usbi_close(int fd);
diff --git a/libusb/os/sunos_usb.c b/libusb/os/sunos_usb.c
index 9d42b8a..85d0fdf 100644
--- a/libusb/os/sunos_usb.c
+++ b/libusb/os/sunos_usb.c
@@ -1641,7 +1641,7 @@
 	return (status);
 }
 
-#ifdef USBI_TIMERFD_AVAILABLE
+#ifdef HAVE_TIMERFD
 static clockid_t op_get_timerfd_clockid(void)
 {
        return CLOCK_MONOTONIC;
@@ -1672,7 +1672,7 @@
         .cancel_transfer = sunos_cancel_transfer,
         .handle_transfer_completion = sunos_handle_transfer_completion,
         .clock_gettime = sunos_clock_gettime,
-#ifdef USBI_TIMERFD_AVAILABLE
+#ifdef HAVE_TIMERFD
         .get_timerfd_clockid = op_get_timerfd_clockid,
 #endif
         .device_priv_size = sizeof(sunos_dev_priv_t),
diff --git a/libusb/os/windows_common.c b/libusb/os/windows_common.c
index a8ee237..df05eaf 100644
--- a/libusb/os/windows_common.c
+++ b/libusb/os/windows_common.c
@@ -678,12 +678,12 @@
 	return priv->backend->cancel_transfer(itransfer);
 }
 
-static int windows_handle_events(struct libusb_context *ctx, struct pollfd *fds, POLL_NFDS_TYPE nfds, int num_ready)
+static int windows_handle_events(struct libusb_context *ctx, struct pollfd *fds, usbi_nfds_t nfds, int num_ready)
 {
 	struct windows_context_priv *priv = _context_priv(ctx);
 	struct usbi_transfer *itransfer;
 	DWORD io_size, io_result;
-	POLL_NFDS_TYPE i;
+	usbi_nfds_t i;
 	bool found;
 	int transfer_fd;
 	int r = LIBUSB_SUCCESS;
diff --git a/libusb/version_nano.h b/libusb/version_nano.h
index 5ad7260..f27fa5e 100644
--- a/libusb/version_nano.h
+++ b/libusb/version_nano.h
@@ -1 +1 @@
-#define LIBUSB_NANO 11443
+#define LIBUSB_NANO 11444
diff --git a/msvc/config.h b/msvc/config.h
index ad3dcaa..840c8df 100644
--- a/msvc/config.h
+++ b/msvc/config.h
@@ -41,8 +41,11 @@
 /* Windows backend */
 #define OS_WINDOWS 1
 
-/* type of second poll() argument */
-#define POLL_NFDS_TYPE unsigned int
+/* Use Windows poll() implementation */
+#define POLL_WINDOWS 1
+
+/* Use Windows Threads */
+#define THREADS_WINDOWS 1
 
 /* Uncomment to enabling output to system log */
 // #define USE_SYSTEM_LOGGING_FACILITY