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 #if defined(_WIN32_WCE)
35 // WinCE doesn't have a HasOverlappedIoCompleted() macro, so attempt to emulate it
36 #define HasOverlappedIoCompleted(lpOverlapped) (((DWORD)(lpOverlapped)->Internal) != STATUS_PENDING)
38 #define HasOverlappedIoCompletedSync(lpOverlapped) (((DWORD)(lpOverlapped)->Internal) == STATUS_COMPLETED_SYNCHRONOUSLY)
40 #define DUMMY_HANDLE ((HANDLE)(LONG_PTR)-2)
42 enum windows_version {
46 WINDOWS_2003, // also includes XP 64
47 WINDOWS_VISTA_AND_LATER,
49 extern enum windows_version windows_version;
53 #define POLLIN 0x0001 /* There is data to read */
54 #define POLLPRI 0x0002 /* There is urgent data to read */
55 #define POLLOUT 0x0004 /* Writing now will not block */
56 #define POLLERR 0x0008 /* Error condition */
57 #define POLLHUP 0x0010 /* Hung up */
58 #define POLLNVAL 0x0020 /* Invalid request: fd not open */
61 int fd; /* file descriptor */
62 short events; /* requested events */
63 short revents; /* returned events */
73 // fd struct that can be used for polling on Windows
74 typedef int cancel_transfer(struct usbi_transfer *itransfer);
77 int fd; // what's exposed to libusb core
78 HANDLE handle; // what we need to attach overlapped to the I/O op, so we can poll it
79 OVERLAPPED* overlapped; // what will report our I/O status
80 struct usbi_transfer *itransfer; // Associated transfer, or NULL if completed
81 cancel_transfer *cancel_fn; // Function pointer to cancel transfer API
82 enum rw_type rw; // I/O transfer direction: read *XOR* write (NOT BOTH)
84 extern const struct winfd INVALID_WINFD;
86 int usbi_pipe(int pipefd[2]);
87 int usbi_poll(struct pollfd *fds, unsigned int nfds, int timeout);
88 ssize_t usbi_write(int fd, const void *buf, size_t count);
89 ssize_t usbi_read(int fd, void *buf, size_t count);
90 int usbi_close(int fd);
92 void init_polling(void);
93 void exit_polling(void);
94 struct winfd usbi_create_fd(HANDLE handle, int access_mode,
95 struct usbi_transfer *transfer, cancel_transfer *cancel_fn);
96 void usbi_free_fd(int fd);
97 struct winfd fd_to_winfd(int fd);
98 struct winfd handle_to_winfd(HANDLE handle);
99 struct winfd overlapped_to_winfd(OVERLAPPED* overlapped);
104 #if defined(DDKBUILD)
105 #include <winsock.h> // defines timeval functions on DDK
108 #if !defined(TIMESPEC_TO_TIMEVAL)
109 #define TIMESPEC_TO_TIMEVAL(tv, ts) { \
110 (tv)->tv_sec = (long)(ts)->tv_sec; \
111 (tv)->tv_usec = (long)(ts)->tv_nsec / 1000; \
114 #if !defined(timersub)
115 #define timersub(a, b, result) \
117 (result)->tv_sec = (a)->tv_sec - (b)->tv_sec; \
118 (result)->tv_usec = (a)->tv_usec - (b)->tv_usec; \
119 if ((result)->tv_usec < 0) { \
120 --(result)->tv_sec; \
121 (result)->tv_usec += 1000000; \