2 * Windows compat: POSIX compatibility wrapper
3 * Copyright © 2012-2013 RealVNC Ltd.
4 * Copyright © 2009-2010 Pete Batard <pete@akeo.ie>
5 * With contributions from Michael Plante, Orin Eman et al.
6 * Parts of poll implementation from libusb-win32, by Stephan Meyer et al.
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
26 // disable /W4 MSVC warnings that are benign
27 #pragma warning(disable:4127) // conditional expression is constant
30 // Handle synchronous completion through the overlapped structure
31 #if !defined(STATUS_REPARSE) // reuse the REPARSE status code
32 #define STATUS_REPARSE ((LONG)0x00000104L)
34 #define STATUS_COMPLETED_SYNCHRONOUSLY STATUS_REPARSE
35 #if defined(_WIN32_WCE)
36 // WinCE doesn't have a HasOverlappedIoCompleted() macro, so attempt to emulate it
37 #define HasOverlappedIoCompleted(lpOverlapped) (((DWORD)(lpOverlapped)->Internal) != STATUS_PENDING)
39 #define HasOverlappedIoCompletedSync(lpOverlapped) (((DWORD)(lpOverlapped)->Internal) == STATUS_COMPLETED_SYNCHRONOUSLY)
41 #define DUMMY_HANDLE ((HANDLE)(LONG_PTR)-2)
43 /* Windows versions */
44 enum windows_version {
46 WINDOWS_UNDEFINED = -1,
47 WINDOWS_UNSUPPORTED = 0,
49 WINDOWS_2003 = 0x52, // Also XP x64
53 WINDOWS_8_1_OR_LATER = 0x63,
56 extern int windows_version;
60 #define POLLIN 0x0001 /* There is data to read */
61 #define POLLPRI 0x0002 /* There is urgent data to read */
62 #define POLLOUT 0x0004 /* Writing now will not block */
63 #define POLLERR 0x0008 /* Error condition */
64 #define POLLHUP 0x0010 /* Hung up */
65 #define POLLNVAL 0x0020 /* Invalid request: fd not open */
68 int fd; /* file descriptor */
69 short events; /* requested events */
70 short revents; /* returned events */
80 // fd struct that can be used for polling on Windows
81 typedef int cancel_transfer(struct usbi_transfer *itransfer);
84 int fd; // what's exposed to libusb core
85 HANDLE handle; // what we need to attach overlapped to the I/O op, so we can poll it
86 OVERLAPPED* overlapped; // what will report our I/O status
87 struct usbi_transfer *itransfer; // Associated transfer, or NULL if completed
88 cancel_transfer *cancel_fn; // Function pointer to cancel transfer API
89 enum rw_type rw; // I/O transfer direction: read *XOR* write (NOT BOTH)
91 extern const struct winfd INVALID_WINFD;
93 int usbi_pipe(int pipefd[2]);
94 int usbi_poll(struct pollfd *fds, unsigned int nfds, int timeout);
95 ssize_t usbi_write(int fd, const void *buf, size_t count);
96 ssize_t usbi_read(int fd, void *buf, size_t count);
97 int usbi_close(int fd);
99 void init_polling(void);
100 void exit_polling(void);
101 struct winfd usbi_create_fd(HANDLE handle, int access_mode,
102 struct usbi_transfer *transfer, cancel_transfer *cancel_fn);
103 void usbi_free_fd(struct winfd* winfd);
104 struct winfd fd_to_winfd(int fd);
105 struct winfd handle_to_winfd(HANDLE handle);
106 struct winfd overlapped_to_winfd(OVERLAPPED* overlapped);
111 #if defined(DDKBUILD)
112 #include <winsock.h> // defines timeval functions on DDK
115 #if !defined(TIMESPEC_TO_TIMEVAL)
116 #define TIMESPEC_TO_TIMEVAL(tv, ts) { \
117 (tv)->tv_sec = (long)(ts)->tv_sec; \
118 (tv)->tv_usec = (long)(ts)->tv_nsec / 1000; \
121 #if !defined(timersub)
122 #define timersub(a, b, result) \
124 (result)->tv_sec = (a)->tv_sec - (b)->tv_sec; \
125 (result)->tv_usec = (a)->tv_usec - (b)->tv_usec; \
126 if ((result)->tv_usec < 0) { \
127 --(result)->tv_sec; \
128 (result)->tv_usec += 1000000; \