1 | /** @file
|
---|
2 | *
|
---|
3 | */
|
---|
4 |
|
---|
5 | /*
|
---|
6 | * Copyright (C) 2006-2007 innotek GmbH
|
---|
7 | *
|
---|
8 | * innotek GmbH confidential
|
---|
9 | * All rights reserved
|
---|
10 | */
|
---|
11 |
|
---|
12 | #ifndef __VRDPUSB__H
|
---|
13 | #define __VRDPUSB__H
|
---|
14 |
|
---|
15 | #include <endian.h>
|
---|
16 | #include <byteswap.h>
|
---|
17 |
|
---|
18 | #ifndef cpu_to_le16
|
---|
19 | #if __BYTE_ORDER == __LITTLE_ENDIAN
|
---|
20 | #define cpu_to_le16(_le16) (_le16)
|
---|
21 | #elif __BYTE_ORDER == __BIG_ENDIAN
|
---|
22 | #define cpu_to_le16(_le16) bswap_16(_le16)
|
---|
23 | #else
|
---|
24 | #error Unsupported host byte order!
|
---|
25 | #endif
|
---|
26 | #endif /* cpu_to_le16 */
|
---|
27 |
|
---|
28 | //#include <asm/byteorder.h>
|
---|
29 | //#define cpu_to_le16 __cpu_to_le16
|
---|
30 |
|
---|
31 | #define uint8_t uint8
|
---|
32 | #define uint16_t uint16
|
---|
33 | #define uint32_t uint32
|
---|
34 | #define uint64_t long long
|
---|
35 | #define bool int
|
---|
36 | #define false 0
|
---|
37 | #define true 1
|
---|
38 |
|
---|
39 | #define OPSTATIC
|
---|
40 | #ifdef RDPUSB_DEBUG
|
---|
41 | #define LogFlow(a) printf a
|
---|
42 | #define Log(a) printf a
|
---|
43 | #define Log2(a) printf a
|
---|
44 | #else
|
---|
45 | #define LogFlow(a)
|
---|
46 | #define Log(a)
|
---|
47 | #define Log2(a)
|
---|
48 | #endif
|
---|
49 |
|
---|
50 | #define LogRel(a) printf a
|
---|
51 |
|
---|
52 | /* Runtime wrappers. */
|
---|
53 | #define RTMemAlloc xmalloc
|
---|
54 | #define RTMemRealloc xrealloc
|
---|
55 | #define RTMemFree xfree
|
---|
56 |
|
---|
57 | #define _1K 1024
|
---|
58 |
|
---|
59 | #define RT_LE2H_U16(_le16) (cpu_to_le16 (_le16))
|
---|
60 |
|
---|
61 | #define VINF_SUCCESS 0
|
---|
62 | #define VERR_NO_MEMORY (-8)
|
---|
63 | #define VERR_NOT_SUPPORTED (-37)
|
---|
64 | #define VERR_ACCESS_DENIED (-38)
|
---|
65 | #define VERR_VUSB_USBFS_PERMISSION (-2005)
|
---|
66 |
|
---|
67 | #define VBOX_SUCCESS(_rc) ((_rc) >= 0)
|
---|
68 |
|
---|
69 | #define RTFILE int
|
---|
70 | #define RTCRITSECT void *
|
---|
71 |
|
---|
72 | #define Assert(_expr) do { \
|
---|
73 | if (!(_expr)) \
|
---|
74 | { \
|
---|
75 | Log(("Assertion failed: {%s}!!!\n", #_expr)); \
|
---|
76 | } \
|
---|
77 | } while (0)
|
---|
78 |
|
---|
79 | #define AssertMsgFailed(_msg) do { \
|
---|
80 | Log(("Assertion failed msg:!!!\n")); \
|
---|
81 | Log(_msg); \
|
---|
82 | } while (0)
|
---|
83 |
|
---|
84 | #define AssertReturn(_expr, _retval) do { \
|
---|
85 | if (!(_expr)) \
|
---|
86 | { \
|
---|
87 | Log(("Assertion failed: {%s}, returning 0x%08X!!!\n", #_expr, _retval)); \
|
---|
88 | return (_retval); \
|
---|
89 | } \
|
---|
90 | } while (0)
|
---|
91 |
|
---|
92 | #define AssertRC(_rc) Assert(VBOX_SUCCESS(_rc))
|
---|
93 |
|
---|
94 | #define RT_FAILURE(_rc) (!VBOX_SUCCESS(_rc))
|
---|
95 |
|
---|
96 | #define NOREF(_a) ((void)_a)
|
---|
97 |
|
---|
98 | static inline int RTCritSectInit (RTCRITSECT *pCritSect)
|
---|
99 | {
|
---|
100 | return VINF_SUCCESS;
|
---|
101 | }
|
---|
102 |
|
---|
103 | static inline int RTCritSectDelete (RTCRITSECT *pCritSect)
|
---|
104 | {
|
---|
105 | return VINF_SUCCESS;
|
---|
106 | }
|
---|
107 |
|
---|
108 | static inline int RTCritSectEnter (RTCRITSECT *pCritSect)
|
---|
109 | {
|
---|
110 | return VINF_SUCCESS;
|
---|
111 | }
|
---|
112 |
|
---|
113 | static inline int RTCritSectLeave (RTCRITSECT *pCritSect)
|
---|
114 | {
|
---|
115 | return VINF_SUCCESS;
|
---|
116 | }
|
---|
117 |
|
---|
118 | static inline void *RTMemDupEx (const void *pvSrc, size_t cbSrc, size_t cbExtra)
|
---|
119 | {
|
---|
120 | void *p = RTMemAlloc (cbSrc + cbExtra);
|
---|
121 |
|
---|
122 | if (p)
|
---|
123 | {
|
---|
124 | memcpy (p, pvSrc, cbSrc);
|
---|
125 | memset ((char *)p + cbSrc, 0, cbExtra);
|
---|
126 | }
|
---|
127 |
|
---|
128 | return p;
|
---|
129 | }
|
---|
130 |
|
---|
131 | static inline void *RTMemAllocZ (size_t cb)
|
---|
132 | {
|
---|
133 | void *p = RTMemAlloc (cb);
|
---|
134 |
|
---|
135 | if (p)
|
---|
136 | {
|
---|
137 | memset (p, 0, cb);
|
---|
138 | }
|
---|
139 |
|
---|
140 | return p;
|
---|
141 | }
|
---|
142 |
|
---|
143 | static inline int RTStrToUInt32Ex (const char *pszValue, char **ppszNext, unsigned uBase, uint32_t *pu32)
|
---|
144 | {
|
---|
145 | *pu32 = strtoul (pszValue, ppszNext, uBase);
|
---|
146 | return VINF_SUCCESS;
|
---|
147 | }
|
---|
148 |
|
---|
149 | #define PRTSTREAM FILE *
|
---|
150 |
|
---|
151 | static inline int RTStrmOpen (const char *pszFileName, const char *pszMode, PRTSTREAM *ppStream)
|
---|
152 | {
|
---|
153 | *ppStream = fopen (pszFileName, pszMode);
|
---|
154 |
|
---|
155 | if (*ppStream)
|
---|
156 | {
|
---|
157 | return VINF_SUCCESS;
|
---|
158 | }
|
---|
159 |
|
---|
160 | return VERR_NOT_SUPPORTED;
|
---|
161 | }
|
---|
162 |
|
---|
163 | static inline int RTStrmClose (PRTSTREAM pStream)
|
---|
164 | {
|
---|
165 | fclose (pStream);
|
---|
166 | return VINF_SUCCESS;
|
---|
167 | }
|
---|
168 |
|
---|
169 | static inline int RTStrmGetLine (PRTSTREAM pStream, char *pszString, size_t cchString)
|
---|
170 | {
|
---|
171 | if (fgets (pszString, cchString, pStream))
|
---|
172 | {
|
---|
173 | return VINF_SUCCESS;
|
---|
174 | }
|
---|
175 |
|
---|
176 | return VERR_NOT_SUPPORTED;
|
---|
177 | }
|
---|
178 |
|
---|
179 | static inline char *RTStrStripL (const char *psz)
|
---|
180 | {
|
---|
181 | while (isspace (*psz))
|
---|
182 | psz++;
|
---|
183 | return (char *)psz;
|
---|
184 | }
|
---|
185 |
|
---|
186 | #define NIL_RTFILE -1
|
---|
187 |
|
---|
188 | #define RTFILE_O_READWRITE 0x00000003
|
---|
189 | #define RTFILE_O_OPEN 0x00000000
|
---|
190 | #define RTFILE_O_DENY_NONE 0x00000000
|
---|
191 |
|
---|
192 | static inline int RTFileOpen (RTFILE *pFile, const char *pszFileName, unsigned fOpen)
|
---|
193 | {
|
---|
194 | Assert (fOpen == RTFILE_O_READWRITE);
|
---|
195 |
|
---|
196 | *pFile = open (pszFileName, O_RDWR, 00600);
|
---|
197 |
|
---|
198 | if (*pFile != -1)
|
---|
199 | {
|
---|
200 | return VINF_SUCCESS;
|
---|
201 | }
|
---|
202 |
|
---|
203 | return VERR_ACCESS_DENIED;
|
---|
204 | }
|
---|
205 |
|
---|
206 | static inline int RTFileClose (RTFILE file)
|
---|
207 | {
|
---|
208 | close (file);
|
---|
209 | return VINF_SUCCESS;
|
---|
210 | }
|
---|
211 |
|
---|
212 | static inline uint64_t RTTimeMilliTS (void)
|
---|
213 | {
|
---|
214 | struct timeval tv;
|
---|
215 | gettimeofday (&tv, NULL);
|
---|
216 | return (uint64_t)tv.tv_sec * (uint64_t)(1000)
|
---|
217 | + (uint64_t)(tv.tv_usec / 1000);
|
---|
218 | }
|
---|
219 |
|
---|
220 | #define VRDP_USB_STATUS_SUCCESS 0
|
---|
221 | #define VRDP_USB_STATUS_ACCESS_DENIED 1
|
---|
222 | #define VRDP_USB_STATUS_DEVICE_REMOVED 2
|
---|
223 |
|
---|
224 | #define VRDP_USB_REAP_FLAG_CONTINUED (0)
|
---|
225 | #define VRDP_USB_REAP_FLAG_LAST (1)
|
---|
226 |
|
---|
227 | #define VRDP_USB_CAPS_FLAG_ASYNC (0)
|
---|
228 | #define VRDP_USB_CAPS_FLAG_POLL (1)
|
---|
229 |
|
---|
230 | #pragma pack(1)
|
---|
231 |
|
---|
232 | #include "vusb.h"
|
---|
233 |
|
---|
234 | typedef struct VUSBDEV
|
---|
235 | {
|
---|
236 | char* pszName;
|
---|
237 | int request_detach;
|
---|
238 | } VUSBDEV, *PVUSBDEV;
|
---|
239 |
|
---|
240 | typedef struct usb_proxy {
|
---|
241 | /* Note: the backend code assumes that the dev member is the first in the structure. */
|
---|
242 | VUSBDEV Dev;
|
---|
243 | /* 'union' because backend accesses the file handle as priv.File. */
|
---|
244 | union {
|
---|
245 | void *pv;
|
---|
246 | int File;
|
---|
247 | } Backend;
|
---|
248 |
|
---|
249 | struct usb_proxy *next;
|
---|
250 | struct usb_proxy *prev;
|
---|
251 |
|
---|
252 | uint32_t devid;
|
---|
253 |
|
---|
254 | PVUSBURB urbs;
|
---|
255 |
|
---|
256 | int iActiveCfg;
|
---|
257 | int cIgnoreSetConfigs;
|
---|
258 | } *PUSBPROXYDEV;
|
---|
259 |
|
---|
260 | typedef struct vusb_setup {
|
---|
261 | uint8_t bmRequestType;
|
---|
262 | uint8_t bRequest;
|
---|
263 | uint16_t wValue;
|
---|
264 | uint16_t wIndex;
|
---|
265 | uint16_t wLength;
|
---|
266 | } VUSBSETUP, *PVUSBSETUP;
|
---|
267 | #pragma pack()
|
---|
268 |
|
---|
269 |
|
---|
270 | static inline void vusbDevUnplugged(PVUSBDEV dev)
|
---|
271 | {
|
---|
272 | dev->request_detach = 1;
|
---|
273 | }
|
---|
274 |
|
---|
275 | int dev2fd (PUSBPROXYDEV pProxyDev);
|
---|
276 |
|
---|
277 |
|
---|
278 | typedef struct USBPROXYBACK
|
---|
279 | {
|
---|
280 | /** Name of the backend. */
|
---|
281 | const char *pszName;
|
---|
282 |
|
---|
283 | int (* pfnOpen)(PUSBPROXYDEV pProxyDev, const char *pszAddress, void *pvBackend);
|
---|
284 | void (* pfnClose)(PUSBPROXYDEV pProxyDev);
|
---|
285 | int (* pfnReset)(PUSBPROXYDEV pProxyDev);
|
---|
286 | int (* pfnSetConfig)(PUSBPROXYDEV pProxyDev, int iCfg);
|
---|
287 | int (* pfnClaimInterface)(PUSBPROXYDEV pProxyDev, int iIf);
|
---|
288 | int (* pfnReleaseInterface)(PUSBPROXYDEV pProxyDev, int iIf);
|
---|
289 | int (* pfnSetInterface)(PUSBPROXYDEV pProxyDev, int iIf, int setting);
|
---|
290 | bool (* pfnClearHaltedEndpoint)(PUSBPROXYDEV pDev, unsigned int iEp);
|
---|
291 | int (* pfnUrbQueue)(PVUSBURB urb);
|
---|
292 | void (* pfnUrbCancel)(PVUSBURB pUrb);
|
---|
293 | PVUSBURB (* pfnUrbReap)(PUSBPROXYDEV pProxyDev, unsigned cMillies);
|
---|
294 | uint32_t uDummy;
|
---|
295 | } USBPROXYBACK;
|
---|
296 |
|
---|
297 | typedef USBPROXYBACK *PUSBPROXYBACK;
|
---|
298 |
|
---|
299 | extern const USBPROXYBACK g_USBProxyDeviceHost;
|
---|
300 |
|
---|
301 | static inline int op_usbproxy_back_open(struct usb_proxy *p, const char *pszAddress)
|
---|
302 | {
|
---|
303 | return g_USBProxyDeviceHost.pfnOpen (p, pszAddress, NULL);
|
---|
304 | }
|
---|
305 |
|
---|
306 | static inline void op_usbproxy_back_close(PUSBPROXYDEV pDev)
|
---|
307 | {
|
---|
308 | return g_USBProxyDeviceHost.pfnClose (pDev);
|
---|
309 | }
|
---|
310 |
|
---|
311 | static inline int op_usbproxy_back_reset(PUSBPROXYDEV pDev)
|
---|
312 | {
|
---|
313 | return g_USBProxyDeviceHost.pfnReset (pDev);
|
---|
314 | }
|
---|
315 |
|
---|
316 | static inline int op_usbproxy_back_set_config(PUSBPROXYDEV pDev, int cfg)
|
---|
317 | {
|
---|
318 | return g_USBProxyDeviceHost.pfnSetConfig (pDev, cfg);
|
---|
319 | }
|
---|
320 |
|
---|
321 | static inline int op_usbproxy_back_claim_interface(PUSBPROXYDEV pDev, int ifnum)
|
---|
322 | {
|
---|
323 | return g_USBProxyDeviceHost.pfnClaimInterface (pDev, ifnum);
|
---|
324 | }
|
---|
325 |
|
---|
326 | static inline int op_usbproxy_back_release_interface(PUSBPROXYDEV pDev, int ifnum)
|
---|
327 | {
|
---|
328 | return g_USBProxyDeviceHost.pfnReleaseInterface (pDev, ifnum);
|
---|
329 | }
|
---|
330 |
|
---|
331 | static inline int op_usbproxy_back_interface_setting(PUSBPROXYDEV pDev, int ifnum, int setting)
|
---|
332 | {
|
---|
333 | return g_USBProxyDeviceHost.pfnSetInterface (pDev, ifnum, setting);
|
---|
334 | }
|
---|
335 |
|
---|
336 | static inline int op_usbproxy_back_queue_urb(PVUSBURB pUrb)
|
---|
337 | {
|
---|
338 | return g_USBProxyDeviceHost.pfnUrbQueue(pUrb);
|
---|
339 | }
|
---|
340 |
|
---|
341 | static inline PVUSBURB op_usbproxy_back_reap_urb(PUSBPROXYDEV pDev, unsigned cMillies)
|
---|
342 | {
|
---|
343 | return g_USBProxyDeviceHost.pfnUrbReap (pDev, cMillies);
|
---|
344 | }
|
---|
345 |
|
---|
346 | static inline bool op_usbproxy_back_clear_halted_ep(PUSBPROXYDEV pDev, unsigned EndPoint)
|
---|
347 | {
|
---|
348 | return g_USBProxyDeviceHost.pfnClearHaltedEndpoint (pDev, EndPoint);
|
---|
349 | }
|
---|
350 |
|
---|
351 | static inline void op_usbproxy_back_cancel_urb(PVUSBURB pUrb)
|
---|
352 | {
|
---|
353 | return g_USBProxyDeviceHost.pfnUrbCancel (pUrb);
|
---|
354 | }
|
---|
355 |
|
---|
356 | #endif /* __VRDPUSB__H */
|
---|