2 * Windows compat: POSIX compatibility wrapper
3 * Copyright © 2009-2010 Pete Batard <pbatard@gmail.com>
4 * With contributions from Michael Plante, Orin Eman et al.
5 * Parts of poll implementation from libusb-win32, by Stephan Meyer et al.
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
25 // disable /W4 MSVC warnings that are benign
26 #pragma warning(disable:4127) // conditional expression is constant
29 // Handle synchronous completion through the overlapped structure
30 #if !defined(STATUS_REPARSE) // reuse the REPARSE status code
31 #define STATUS_REPARSE ((LONG)0x00000104L)
33 #define STATUS_COMPLETED_SYNCHRONOUSLY STATUS_REPARSE
34 #define HasOverlappedIoCompletedSync(lpOverlapped) (((DWORD)(lpOverlapped)->Internal) == STATUS_COMPLETED_SYNCHRONOUSLY)
36 #define DUMMY_HANDLE ((HANDLE)(LONG_PTR)-2)
38 enum windows_version {
41 WINDOWS_2003, // also includes XP 64
42 WINDOWS_VISTA_AND_LATER,
44 extern enum windows_version windows_version;
48 #define POLLIN 0x0001 /* There is data to read */
49 #define POLLPRI 0x0002 /* There is urgent data to read */
50 #define POLLOUT 0x0004 /* Writing now will not block */
51 #define POLLERR 0x0008 /* Error condition */
52 #define POLLHUP 0x0010 /* Hung up */
53 #define POLLNVAL 0x0020 /* Invalid request: fd not open */
56 int fd; /* file descriptor */
57 short events; /* requested events */
58 short revents; /* returned events */
68 // fd struct that can be used for polling on Windows
70 int fd; // what's exposed to libusb core
71 HANDLE handle; // what we need to attach overlapped to the I/O op, so we can poll it
72 OVERLAPPED* overlapped; // what will report our I/O status
73 enum rw_type rw; // I/O transfer direction: read *XOR* write (NOT BOTH)
75 extern const struct winfd INVALID_WINFD;
77 int usbi_pipe(int pipefd[2]);
78 int usbi_poll(struct pollfd *fds, unsigned int nfds, int timeout);
79 ssize_t usbi_write(int fd, const void *buf, size_t count);
80 ssize_t usbi_read(int fd, void *buf, size_t count);
81 int usbi_close(int fd);
83 void init_polling(void);
84 void exit_polling(void);
85 struct winfd usbi_create_fd(HANDLE handle, int access_mode);
86 void usbi_free_fd(int fd);
87 struct winfd fd_to_winfd(int fd);
88 struct winfd handle_to_winfd(HANDLE handle);
89 struct winfd overlapped_to_winfd(OVERLAPPED* overlapped);
95 #include <winsock.h> // defines timeval functions on DDK
98 #if !defined(TIMESPEC_TO_TIMEVAL)
99 #define TIMESPEC_TO_TIMEVAL(tv, ts) { \
100 (tv)->tv_sec = (long)(ts)->tv_sec; \
101 (tv)->tv_usec = (long)(ts)->tv_nsec / 1000; \
104 #if !defined(timersub)
105 #define timersub(a, b, result) \
107 (result)->tv_sec = (a)->tv_sec - (b)->tv_sec; \
108 (result)->tv_usec = (a)->tv_usec - (b)->tv_usec; \
109 if ((result)->tv_usec < 0) { \
110 --(result)->tv_sec; \
111 (result)->tv_usec += 1000000; \