e53af2d5eb242697f51bf1f9d882891ae32ff882
[platform/upstream/libusb.git] / libusb / os / poll_windows.h
1 /*
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.
6  *
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.
11  *
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.
16  *
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
20  *
21  */
22 #pragma once
23
24 #if defined(_MSC_VER)
25 // disable /W4 MSVC warnings that are benign
26 #pragma warning(disable:4127) // conditional expression is constant
27 #endif
28
29 // Handle synchronous completion through the overlapped structure
30 #if !defined(STATUS_REPARSE)    // reuse the REPARSE status code
31 #define STATUS_REPARSE ((LONG)0x00000104L)
32 #endif
33 #define STATUS_COMPLETED_SYNCHRONOUSLY  STATUS_REPARSE
34 #define HasOverlappedIoCompletedSync(lpOverlapped)      (((DWORD)(lpOverlapped)->Internal) == STATUS_COMPLETED_SYNCHRONOUSLY)
35
36 #define DUMMY_HANDLE ((HANDLE)(LONG_PTR)-2)
37
38 enum windows_version {
39         WINDOWS_UNSUPPORTED,
40         WINDOWS_XP,
41         WINDOWS_2003,   // also includes XP 64
42         WINDOWS_VISTA_AND_LATER,
43 };
44 extern enum windows_version windows_version;
45
46 #define MAX_FDS     256
47
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 */
54
55 struct pollfd {
56     int fd;           /* file descriptor */
57     short events;     /* requested events */
58     short revents;    /* returned events */
59 };
60
61 // access modes
62 enum rw_type {
63         RW_NONE,
64         RW_READ,
65         RW_WRITE,
66 };
67
68 // fd struct that can be used for polling on Windows
69 struct winfd {
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)
74 };
75 extern const struct winfd INVALID_WINFD;
76
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);
82
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);
90
91 /*
92  * Timeval operations
93  */
94 #if defined(DDKBUILD)
95 #include <winsock.h>    // defines timeval functions on DDK
96 #endif
97
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;         \
102 }
103 #endif
104 #if !defined(timersub)
105 #define timersub(a, b, result)                          \
106 do {                                                    \
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;                   \
112         }                                                   \
113 } while (0)
114 #endif