1 | /** @file
|
---|
2 | *
|
---|
3 | * Remote Desktop Protocol client:
|
---|
4 | * USB Channel Process Functions
|
---|
5 | *
|
---|
6 | */
|
---|
7 |
|
---|
8 | /*
|
---|
9 | * Copyright (C) 2006-2011 Oracle Corporation
|
---|
10 | *
|
---|
11 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
12 | * available from http://www.virtualbox.org. This file is free software;
|
---|
13 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
14 | * General Public License (GPL) as published by the Free Software
|
---|
15 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
16 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
17 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
18 | */
|
---|
19 |
|
---|
20 | /* DEBUG is defined in ../rdesktop.h */
|
---|
21 | #ifdef DEBUG
|
---|
22 | # define VBOX_DEBUG DEBUG
|
---|
23 | #endif
|
---|
24 | #include "../rdesktop.h"
|
---|
25 | #undef DEBUG
|
---|
26 | #ifdef VBOX_DEBUG
|
---|
27 | # define DEBUG VBOX_DEBUG
|
---|
28 | #endif
|
---|
29 |
|
---|
30 | #include "vrdpusb.h"
|
---|
31 | #include "USBProxyDevice.h"
|
---|
32 | #include "USBGetDevices.h"
|
---|
33 |
|
---|
34 | #include <iprt/cdefs.h>
|
---|
35 | #include <iprt/types.h>
|
---|
36 | #include <iprt/err.h>
|
---|
37 | #include <iprt/log.h>
|
---|
38 |
|
---|
39 | #include <unistd.h>
|
---|
40 | #include <ctype.h>
|
---|
41 | #include <fcntl.h>
|
---|
42 |
|
---|
43 |
|
---|
44 | #define RDPUSB_REQ_OPEN (0)
|
---|
45 | #define RDPUSB_REQ_CLOSE (1)
|
---|
46 | #define RDPUSB_REQ_RESET (2)
|
---|
47 | #define RDPUSB_REQ_SET_CONFIG (3)
|
---|
48 | #define RDPUSB_REQ_CLAIM_INTERFACE (4)
|
---|
49 | #define RDPUSB_REQ_RELEASE_INTERFACE (5)
|
---|
50 | #define RDPUSB_REQ_INTERFACE_SETTING (6)
|
---|
51 | #define RDPUSB_REQ_QUEUE_URB (7)
|
---|
52 | #define RDPUSB_REQ_REAP_URB (8)
|
---|
53 | #define RDPUSB_REQ_CLEAR_HALTED_EP (9)
|
---|
54 | #define RDPUSB_REQ_CANCEL_URB (10)
|
---|
55 | #define RDPUSB_REQ_DEVICE_LIST (11)
|
---|
56 | #define RDPUSB_REQ_NEGOTIATE (12)
|
---|
57 |
|
---|
58 | static VCHANNEL *rdpusb_channel;
|
---|
59 |
|
---|
60 | /** Well-known locations for the Linux Usbfs virtual file system */
|
---|
61 | static const struct
|
---|
62 | {
|
---|
63 | /** Expected mount location for Usbfs */
|
---|
64 | const char *pcszRoot;
|
---|
65 | /** Expected location of the "devices" file */
|
---|
66 | const char *pcszDevices;
|
---|
67 | } g_usbfsPaths[] =
|
---|
68 | {
|
---|
69 | { "/proc/bus/usb", "/proc/bus/usb/devices" },
|
---|
70 | { "/dev/bus/usb", "/dev/bus/usb/devices" }
|
---|
71 | };
|
---|
72 |
|
---|
73 | /** Location at which the USB device tree was found. NULL means not
|
---|
74 | * found. */
|
---|
75 | static const char *g_pcszDevicesRoot = NULL;
|
---|
76 | static bool g_fUseSysfs = false;
|
---|
77 |
|
---|
78 | static PUSBDEVICE g_pUsbDevices = NULL;
|
---|
79 |
|
---|
80 | /* A device list entry */
|
---|
81 | #pragma pack (1)
|
---|
82 | typedef struct _DevListEntry
|
---|
83 | {
|
---|
84 | uint16_t oNext; /* Offset of the next structure. 0 if last. */
|
---|
85 | uint32_t id; /* Identifier of the device assigned by the client. */
|
---|
86 | uint16_t bcdUSB; /* USB verion number. */
|
---|
87 | uint8_t bDeviceClass; /* Device class. */
|
---|
88 | uint8_t bDeviceSubClass; /* Device subclass. */
|
---|
89 | uint8_t bDeviceProtocol; /* Device protocol. */
|
---|
90 | uint16_t idVendor; /* Vendor id. */
|
---|
91 | uint16_t idProduct; /* Product id. */
|
---|
92 | uint16_t bcdRev; /* Revision. */
|
---|
93 | uint16_t oManufacturer; /* Offset of manufacturer string. */
|
---|
94 | uint16_t oProduct; /* Offset of product string. */
|
---|
95 | uint16_t oSerialNumber; /* Offset of serial string. */
|
---|
96 | uint16_t idPort; /* Physical USB port the device is connected to. */
|
---|
97 | } DevListEntry;
|
---|
98 | #pragma pack ()
|
---|
99 |
|
---|
100 | static inline int op_usbproxy_back_open(PUSBPROXYDEV p, const char *pszAddress)
|
---|
101 | {
|
---|
102 | return g_USBProxyDeviceHost.pfnOpen (p, pszAddress, NULL);
|
---|
103 | }
|
---|
104 |
|
---|
105 | static inline void op_usbproxy_back_close(PUSBPROXYDEV pDev)
|
---|
106 | {
|
---|
107 | return g_USBProxyDeviceHost.pfnClose (pDev);
|
---|
108 | }
|
---|
109 |
|
---|
110 | static inline int op_usbproxy_back_reset(PUSBPROXYDEV pDev)
|
---|
111 | {
|
---|
112 | return g_USBProxyDeviceHost.pfnReset (pDev, false);
|
---|
113 | }
|
---|
114 |
|
---|
115 | static inline int op_usbproxy_back_set_config(PUSBPROXYDEV pDev, int cfg)
|
---|
116 | {
|
---|
117 | return g_USBProxyDeviceHost.pfnSetConfig (pDev, cfg);
|
---|
118 | }
|
---|
119 |
|
---|
120 | static inline int op_usbproxy_back_claim_interface(PUSBPROXYDEV pDev, int ifnum)
|
---|
121 | {
|
---|
122 | return g_USBProxyDeviceHost.pfnClaimInterface (pDev, ifnum);
|
---|
123 | }
|
---|
124 |
|
---|
125 | static inline int op_usbproxy_back_release_interface(PUSBPROXYDEV pDev, int ifnum)
|
---|
126 | {
|
---|
127 | return g_USBProxyDeviceHost.pfnReleaseInterface (pDev, ifnum);
|
---|
128 | }
|
---|
129 |
|
---|
130 | static inline int op_usbproxy_back_interface_setting(PUSBPROXYDEV pDev, int ifnum, int setting)
|
---|
131 | {
|
---|
132 | return g_USBProxyDeviceHost.pfnSetInterface (pDev, ifnum, setting);
|
---|
133 | }
|
---|
134 |
|
---|
135 | static inline int op_usbproxy_back_queue_urb(PVUSBURB pUrb)
|
---|
136 | {
|
---|
137 | return g_USBProxyDeviceHost.pfnUrbQueue(pUrb);
|
---|
138 | }
|
---|
139 |
|
---|
140 | static inline PVUSBURB op_usbproxy_back_reap_urb(PUSBPROXYDEV pDev, unsigned cMillies)
|
---|
141 | {
|
---|
142 | return g_USBProxyDeviceHost.pfnUrbReap (pDev, cMillies);
|
---|
143 | }
|
---|
144 |
|
---|
145 | static inline bool op_usbproxy_back_clear_halted_ep(PUSBPROXYDEV pDev, unsigned EndPoint)
|
---|
146 | {
|
---|
147 | return g_USBProxyDeviceHost.pfnClearHaltedEndpoint (pDev, EndPoint);
|
---|
148 | }
|
---|
149 |
|
---|
150 | static inline void op_usbproxy_back_cancel_urb(PVUSBURB pUrb)
|
---|
151 | {
|
---|
152 | return g_USBProxyDeviceHost.pfnUrbCancel (pUrb);
|
---|
153 | }
|
---|
154 |
|
---|
155 |
|
---|
156 | /** Count the USB devices in a linked list of PUSBDEVICE structures. */
|
---|
157 | unsigned countUSBDevices(PUSBDEVICE pDevices)
|
---|
158 | {
|
---|
159 | unsigned i = 0;
|
---|
160 | for (; pDevices; pDevices = pDevices->pNext)
|
---|
161 | ++i;
|
---|
162 | return i;
|
---|
163 | }
|
---|
164 |
|
---|
165 |
|
---|
166 | enum {
|
---|
167 | /** The space we set aside for the USB strings. Should always be enough,
|
---|
168 | * as a USB device contains up to 256 characters of UTF-16 string data. */
|
---|
169 | MAX_STRINGS_LEN = 1024,
|
---|
170 | /** The space we reserve for each wire format device entry */
|
---|
171 | DEV_ENTRY_SIZE = sizeof(DevListEntry) + MAX_STRINGS_LEN
|
---|
172 | };
|
---|
173 |
|
---|
174 |
|
---|
175 | /**
|
---|
176 | * Add a string to the end of a wire format device entry.
|
---|
177 | * @param pBuf the start of the buffer containing the entry
|
---|
178 | * @param iBuf the index into the buffer to add the string at
|
---|
179 | * @param pcsz the string to add - optional
|
---|
180 | * @param piString where to write back @a iBuf or zero if there is no string
|
---|
181 | * @param piNext where to write back the index where the next string may
|
---|
182 | * start
|
---|
183 | */
|
---|
184 | static void addStringToEntry(char *pBuf, uint16_t iBuf, const char *pcsz,
|
---|
185 | uint16_t *piString, uint16_t *piNext)
|
---|
186 | {
|
---|
187 | size_t cch;
|
---|
188 |
|
---|
189 | *piString = 0;
|
---|
190 | *piNext = iBuf;
|
---|
191 | if (!pcsz)
|
---|
192 | return;
|
---|
193 | cch = strlen(pcsz) + 1;
|
---|
194 | if (cch > DEV_ENTRY_SIZE - iBuf)
|
---|
195 | return;
|
---|
196 | strcpy(pBuf + iBuf, pcsz);
|
---|
197 | *piString = iBuf;
|
---|
198 | *piNext = iBuf + cch;
|
---|
199 | }
|
---|
200 |
|
---|
201 |
|
---|
202 | /** Fill in a device list entry in wire format from a PUSBDEVICE and return an
|
---|
203 | * index to where the next string should start */
|
---|
204 | static void fillWireListEntry(char *pBuf, PUSBDEVICE pDevice,
|
---|
205 | uint16_t *piNext)
|
---|
206 | {
|
---|
207 | DevListEntry *pEntry;
|
---|
208 | uint16_t iNextString = sizeof(DevListEntry);
|
---|
209 |
|
---|
210 | pEntry = (DevListEntry *)pBuf;
|
---|
211 | pEntry->id = (pDevice->bPort << 8) + pDevice->bBus;
|
---|
212 | pEntry->bcdUSB = pDevice->bcdUSB;
|
---|
213 | pEntry->bDeviceClass = pDevice->bDeviceClass;
|
---|
214 | pEntry->bDeviceSubClass = pDevice->bDeviceSubClass;
|
---|
215 | pEntry->idVendor = pDevice->idVendor;
|
---|
216 | pEntry->idProduct = pDevice->idProduct;
|
---|
217 | pEntry->bcdRev = pDevice->bcdDevice;
|
---|
218 | pEntry->idPort = pDevice->bPort;
|
---|
219 | addStringToEntry(pBuf, iNextString, pDevice->pszManufacturer,
|
---|
220 | &pEntry->oManufacturer, &iNextString);
|
---|
221 | addStringToEntry(pBuf, iNextString, pDevice->pszProduct,
|
---|
222 | &pEntry->oProduct, &iNextString);
|
---|
223 | addStringToEntry(pBuf, iNextString, pDevice->pszSerialNumber,
|
---|
224 | &pEntry->oSerialNumber, &pEntry->oNext);
|
---|
225 | *piNext = pEntry->oNext;
|
---|
226 | }
|
---|
227 |
|
---|
228 |
|
---|
229 | /** Allocate (and return) a buffer for a device list in VRDP wire format,
|
---|
230 | * and populate from a PUSBDEVICE linked list. @a pLen takes the length of
|
---|
231 | * the new list.
|
---|
232 | * See @a Console::processRemoteUSBDevices for the receiving end. */
|
---|
233 | static void *buildWireListFromDevices(PUSBDEVICE pDevices, int *pLen)
|
---|
234 | {
|
---|
235 | char *pBuf;
|
---|
236 | unsigned cDevs, cbBuf, iCurrent;
|
---|
237 | uint16_t iNext;
|
---|
238 | PUSBDEVICE pCurrent;
|
---|
239 |
|
---|
240 | cDevs = countUSBDevices(pDevices);
|
---|
241 | cbBuf = cDevs * DEV_ENTRY_SIZE + 2;
|
---|
242 | pBuf = (char *)xmalloc(cbBuf);
|
---|
243 | memset(pBuf, 0, cbBuf);
|
---|
244 | for (pCurrent = pDevices, iCurrent = 0; pCurrent;
|
---|
245 | pCurrent = pCurrent->pNext, iCurrent += iNext, --cDevs)
|
---|
246 | {
|
---|
247 | unsigned i, cZeros;
|
---|
248 |
|
---|
249 | AssertReturnStmt(iCurrent + DEV_ENTRY_SIZE + 2 <= cbBuf,
|
---|
250 | free(pBuf), NULL);
|
---|
251 | fillWireListEntry(pBuf + iCurrent, pCurrent, &iNext);
|
---|
252 | DevListEntry *pEntry = (DevListEntry *)(pBuf + iCurrent);
|
---|
253 | /* Sanity tests */
|
---|
254 | for (i = iCurrent + sizeof(DevListEntry), cZeros = 0;
|
---|
255 | i < iCurrent + iNext; ++i)
|
---|
256 | if (pBuf[i] == 0)
|
---|
257 | ++cZeros;
|
---|
258 | AssertReturnStmt(cZeros == RT_BOOL(pEntry->oManufacturer)
|
---|
259 | + RT_BOOL(pEntry->oProduct)
|
---|
260 | + RT_BOOL(pEntry->oSerialNumber),
|
---|
261 | free(pBuf), NULL);
|
---|
262 | Assert(pEntry->oManufacturer == 0 || pBuf[iCurrent + pEntry->oManufacturer] != '\0');
|
---|
263 | Assert(pEntry->oProduct == 0 || pBuf[iCurrent + pEntry->oProduct] != '\0');
|
---|
264 | Assert(pEntry->oSerialNumber == 0 || pBuf[iCurrent + pEntry->oSerialNumber] != '\0');
|
---|
265 | AssertReturnStmt(cZeros == 0 || pBuf[iCurrent + iNext - 1] == '\0',
|
---|
266 | free(pBuf), NULL);
|
---|
267 | }
|
---|
268 | *pLen = iCurrent + iNext + 2;
|
---|
269 | Assert(cDevs == 0);
|
---|
270 | Assert(*pLen <= cbBuf);
|
---|
271 | return pBuf;
|
---|
272 | }
|
---|
273 |
|
---|
274 |
|
---|
275 | /** Build a list of the usable USB devices currently connected to the client
|
---|
276 | * system using the VRDP wire protocol. The structure returned must be freed
|
---|
277 | * using free(3) when it is no longer needed; returns NULL and sets *pLen to
|
---|
278 | * zero on failure. */
|
---|
279 | static void *build_device_list (int *pLen)
|
---|
280 | {
|
---|
281 | void *pvDeviceList;
|
---|
282 |
|
---|
283 | Log(("RDPUSB build_device_list"));
|
---|
284 | *pLen = 0;
|
---|
285 | if (g_pUsbDevices)
|
---|
286 | deviceListFree(&g_pUsbDevices);
|
---|
287 | g_pUsbDevices = USBProxyLinuxGetDevices(g_pcszDevicesRoot, g_fUseSysfs);
|
---|
288 | if (!g_pUsbDevices)
|
---|
289 | return NULL;
|
---|
290 | pvDeviceList = buildWireListFromDevices(g_pUsbDevices, pLen);
|
---|
291 | return pvDeviceList;
|
---|
292 | }
|
---|
293 |
|
---|
294 |
|
---|
295 | static STREAM
|
---|
296 | rdpusb_init_packet(uint32 len, uint8 code)
|
---|
297 | {
|
---|
298 | STREAM s;
|
---|
299 |
|
---|
300 | s = channel_init(rdpusb_channel, len + 5);
|
---|
301 | out_uint32_le (s, len + sizeof (code)); /* The length of data after the 'len' field. */
|
---|
302 | out_uint8(s, code);
|
---|
303 | return s;
|
---|
304 | }
|
---|
305 |
|
---|
306 | static void
|
---|
307 | rdpusb_send(STREAM s)
|
---|
308 | {
|
---|
309 | #ifdef RDPUSB_DEBUG
|
---|
310 | Log(("RDPUSB send:\n"));
|
---|
311 | hexdump(s->channel_hdr + 8, s->end - s->channel_hdr - 8);
|
---|
312 | #endif
|
---|
313 |
|
---|
314 | channel_send(s, rdpusb_channel);
|
---|
315 | }
|
---|
316 |
|
---|
317 | static void
|
---|
318 | rdpusb_send_reply (uint8_t code, uint8_t status, uint32_t devid)
|
---|
319 | {
|
---|
320 | STREAM s = rdpusb_init_packet(5, code);
|
---|
321 | out_uint8(s, status);
|
---|
322 | out_uint32_le(s, devid);
|
---|
323 | s_mark_end(s);
|
---|
324 | rdpusb_send(s);
|
---|
325 | }
|
---|
326 |
|
---|
327 | static void
|
---|
328 | rdpusb_send_access_denied (uint8_t code, uint32_t devid)
|
---|
329 | {
|
---|
330 | rdpusb_send_reply (code, VRDP_USB_STATUS_ACCESS_DENIED, devid);
|
---|
331 | }
|
---|
332 |
|
---|
333 | static inline int
|
---|
334 | vrdp_usb_status (int rc, VUSBDEV *pdev)
|
---|
335 | {
|
---|
336 | if (!rc || usbProxyFromVusbDev(pdev)->fDetached)
|
---|
337 | {
|
---|
338 | return VRDP_USB_STATUS_DEVICE_REMOVED;
|
---|
339 | }
|
---|
340 |
|
---|
341 | return VRDP_USB_STATUS_SUCCESS;
|
---|
342 | }
|
---|
343 |
|
---|
344 | static PUSBPROXYDEV g_proxies = NULL;
|
---|
345 |
|
---|
346 | static PUSBPROXYDEV
|
---|
347 | devid2proxy (uint32_t devid)
|
---|
348 | {
|
---|
349 | PUSBPROXYDEV proxy = g_proxies;
|
---|
350 |
|
---|
351 | while (proxy && proxy->devid != devid)
|
---|
352 | {
|
---|
353 | proxy = proxy->pNext;
|
---|
354 | }
|
---|
355 |
|
---|
356 | return proxy;
|
---|
357 | }
|
---|
358 |
|
---|
359 | static void
|
---|
360 | rdpusb_reap_urbs (void)
|
---|
361 | {
|
---|
362 | STREAM s;
|
---|
363 |
|
---|
364 | PVUSBURB pUrb = NULL;
|
---|
365 |
|
---|
366 | PUSBPROXYDEV proxy = g_proxies;
|
---|
367 |
|
---|
368 | while (proxy)
|
---|
369 | {
|
---|
370 | pUrb = op_usbproxy_back_reap_urb(proxy, 0);
|
---|
371 |
|
---|
372 | if (pUrb)
|
---|
373 | {
|
---|
374 | int datalen = 0;
|
---|
375 |
|
---|
376 | Log(("RDPUSB: rdpusb_reap_urbs: cbData = %d, enmStatus = %d\n", pUrb->cbData, pUrb->enmStatus));
|
---|
377 |
|
---|
378 | if (pUrb->enmDir == VUSB_DIRECTION_IN)
|
---|
379 | {
|
---|
380 | datalen = pUrb->cbData;
|
---|
381 | }
|
---|
382 |
|
---|
383 | s = rdpusb_init_packet(14 + datalen, RDPUSB_REQ_REAP_URB);
|
---|
384 | out_uint32_le(s, proxy->devid);
|
---|
385 | out_uint8(s, VRDP_USB_REAP_FLAG_LAST);
|
---|
386 | out_uint8(s, pUrb->enmStatus);
|
---|
387 | out_uint32_le(s, pUrb->handle);
|
---|
388 | out_uint32_le(s, pUrb->cbData);
|
---|
389 |
|
---|
390 | if (datalen)
|
---|
391 | {
|
---|
392 | out_uint8a (s, pUrb->abData, datalen);
|
---|
393 | }
|
---|
394 |
|
---|
395 | s_mark_end(s);
|
---|
396 | rdpusb_send(s);
|
---|
397 |
|
---|
398 | if (pUrb->pPrev || pUrb->pNext || pUrb == proxy->pUrbs)
|
---|
399 | {
|
---|
400 | /* Remove the URB from list. */
|
---|
401 | if (pUrb->pPrev)
|
---|
402 | {
|
---|
403 | pUrb->pPrev->pNext = pUrb->pNext;
|
---|
404 | }
|
---|
405 | else
|
---|
406 | {
|
---|
407 | proxy->pUrbs = pUrb->pNext;
|
---|
408 | }
|
---|
409 |
|
---|
410 | if (pUrb->pNext)
|
---|
411 | {
|
---|
412 | pUrb->pNext->pPrev = pUrb->pPrev;
|
---|
413 | }
|
---|
414 | }
|
---|
415 |
|
---|
416 | #ifdef RDPUSB_DEBUG
|
---|
417 | Log(("Going to free %p\n", pUrb));
|
---|
418 | #endif
|
---|
419 | xfree (pUrb);
|
---|
420 | #ifdef RDPUSB_DEBUG
|
---|
421 | Log(("freed %p\n", pUrb));
|
---|
422 | #endif
|
---|
423 | }
|
---|
424 |
|
---|
425 | proxy = proxy->pNext;
|
---|
426 | }
|
---|
427 |
|
---|
428 | return;
|
---|
429 | }
|
---|
430 |
|
---|
431 | static void
|
---|
432 | rdpusb_process(STREAM s)
|
---|
433 | {
|
---|
434 | int rc;
|
---|
435 |
|
---|
436 | uint32 len;
|
---|
437 | uint8 code;
|
---|
438 | uint32 devid;
|
---|
439 |
|
---|
440 | PUSBPROXYDEV proxy = NULL;
|
---|
441 |
|
---|
442 | #ifdef RDPUSB_DEBUG
|
---|
443 | Log(("RDPUSB recv:\n"));
|
---|
444 | hexdump(s->p, s->end - s->p);
|
---|
445 | #endif
|
---|
446 |
|
---|
447 | in_uint32_le (s, len);
|
---|
448 | if (len > s->end - s->p)
|
---|
449 | {
|
---|
450 | error("RDPUSB: not enough data len = %d, bytes left %d\n", len, s->end - s->p);
|
---|
451 | return;
|
---|
452 | }
|
---|
453 |
|
---|
454 | in_uint8(s, code);
|
---|
455 |
|
---|
456 | Log(("RDPUSB recv: len = %d, code = %d\n", len, code));
|
---|
457 |
|
---|
458 | switch (code)
|
---|
459 | {
|
---|
460 | case RDPUSB_REQ_OPEN:
|
---|
461 | {
|
---|
462 | PUSBDEVICE pDevice;
|
---|
463 |
|
---|
464 | in_uint32_le(s, devid);
|
---|
465 |
|
---|
466 | proxy = (PUSBPROXYDEV )xmalloc (sizeof (USBPROXYDEV));
|
---|
467 |
|
---|
468 | memset (proxy, 0, sizeof (USBPROXYDEV));
|
---|
469 |
|
---|
470 | proxy->Dev.pszName = "Remote device";
|
---|
471 | proxy->devid = devid;
|
---|
472 |
|
---|
473 | for (pDevice = g_pUsbDevices; pDevice; pDevice = pDevice->pNext)
|
---|
474 | if ((pDevice->bPort << 8) + pDevice->bBus == devid)
|
---|
475 | break;
|
---|
476 |
|
---|
477 | rc = pDevice ? op_usbproxy_back_open(proxy, pDevice->pszAddress)
|
---|
478 | : VERR_NOT_FOUND;
|
---|
479 |
|
---|
480 | if (rc != VINF_SUCCESS)
|
---|
481 | {
|
---|
482 | rdpusb_send_access_denied (code, devid);
|
---|
483 | xfree (proxy);
|
---|
484 | proxy = NULL;
|
---|
485 | }
|
---|
486 | else
|
---|
487 | {
|
---|
488 | if (g_proxies)
|
---|
489 | {
|
---|
490 | g_proxies->pPrev = proxy;
|
---|
491 | }
|
---|
492 |
|
---|
493 | proxy->pNext = g_proxies;
|
---|
494 | g_proxies = proxy;
|
---|
495 | }
|
---|
496 | } break;
|
---|
497 |
|
---|
498 | case RDPUSB_REQ_CLOSE:
|
---|
499 | {
|
---|
500 | in_uint32_le(s, devid);
|
---|
501 | proxy = devid2proxy (devid);
|
---|
502 |
|
---|
503 | if (proxy)
|
---|
504 | {
|
---|
505 | op_usbproxy_back_close(proxy);
|
---|
506 |
|
---|
507 | if (proxy->pPrev)
|
---|
508 | {
|
---|
509 | proxy->pPrev->pNext = proxy->pNext;
|
---|
510 | }
|
---|
511 | else
|
---|
512 | {
|
---|
513 | g_proxies = proxy->pNext;
|
---|
514 | }
|
---|
515 |
|
---|
516 | if (proxy->pNext)
|
---|
517 | {
|
---|
518 | proxy->pNext->pPrev = proxy->pPrev;
|
---|
519 | }
|
---|
520 |
|
---|
521 | xfree (proxy);
|
---|
522 | proxy = NULL;
|
---|
523 | }
|
---|
524 |
|
---|
525 | /* No reply. */
|
---|
526 | } break;
|
---|
527 |
|
---|
528 | case RDPUSB_REQ_RESET:
|
---|
529 | {
|
---|
530 | in_uint32_le(s, devid);
|
---|
531 | proxy = devid2proxy (devid);
|
---|
532 |
|
---|
533 | if (!proxy)
|
---|
534 | {
|
---|
535 | rdpusb_send_access_denied (code, devid);
|
---|
536 | break;
|
---|
537 | }
|
---|
538 |
|
---|
539 | rc = op_usbproxy_back_reset(proxy);
|
---|
540 |
|
---|
541 | if (rc != VINF_SUCCESS)
|
---|
542 | {
|
---|
543 | rdpusb_send_reply (code, vrdp_usb_status (!rc, &proxy->Dev), devid);
|
---|
544 | }
|
---|
545 | } break;
|
---|
546 |
|
---|
547 | case RDPUSB_REQ_SET_CONFIG:
|
---|
548 | {
|
---|
549 | uint8 cfg;
|
---|
550 |
|
---|
551 | in_uint32_le(s, devid);
|
---|
552 | proxy = devid2proxy (devid);
|
---|
553 |
|
---|
554 | if (!proxy)
|
---|
555 | {
|
---|
556 | rdpusb_send_access_denied (code, devid);
|
---|
557 | break;
|
---|
558 | }
|
---|
559 |
|
---|
560 | in_uint8(s, cfg);
|
---|
561 |
|
---|
562 | rc = op_usbproxy_back_set_config(proxy, cfg);
|
---|
563 |
|
---|
564 | if (!rc)
|
---|
565 | {
|
---|
566 | rdpusb_send_reply (code, vrdp_usb_status (rc, &proxy->Dev), devid);
|
---|
567 | }
|
---|
568 | } break;
|
---|
569 |
|
---|
570 | case RDPUSB_REQ_CLAIM_INTERFACE:
|
---|
571 | {
|
---|
572 | uint8 ifnum;
|
---|
573 |
|
---|
574 | in_uint32_le(s, devid);
|
---|
575 | proxy = devid2proxy (devid);
|
---|
576 |
|
---|
577 | if (!proxy)
|
---|
578 | {
|
---|
579 | rdpusb_send_access_denied (code, devid);
|
---|
580 | break;
|
---|
581 | }
|
---|
582 |
|
---|
583 | in_uint8(s, ifnum);
|
---|
584 |
|
---|
585 | rc = op_usbproxy_back_claim_interface(proxy, ifnum);
|
---|
586 |
|
---|
587 | if (!rc)
|
---|
588 | {
|
---|
589 | rdpusb_send_reply (code, vrdp_usb_status (rc, &proxy->Dev), devid);
|
---|
590 | }
|
---|
591 | } break;
|
---|
592 |
|
---|
593 | case RDPUSB_REQ_RELEASE_INTERFACE:
|
---|
594 | {
|
---|
595 | uint8 ifnum;
|
---|
596 |
|
---|
597 | in_uint32_le(s, devid);
|
---|
598 | proxy = devid2proxy (devid);
|
---|
599 |
|
---|
600 | if (!proxy)
|
---|
601 | {
|
---|
602 | rdpusb_send_access_denied (code, devid);
|
---|
603 | break;
|
---|
604 | }
|
---|
605 |
|
---|
606 | in_uint8(s, ifnum);
|
---|
607 |
|
---|
608 | rc = op_usbproxy_back_release_interface(proxy, ifnum);
|
---|
609 |
|
---|
610 | if (!rc)
|
---|
611 | {
|
---|
612 | rdpusb_send_reply (code, vrdp_usb_status (rc, &proxy->Dev), devid);
|
---|
613 | }
|
---|
614 | } break;
|
---|
615 |
|
---|
616 | case RDPUSB_REQ_INTERFACE_SETTING:
|
---|
617 | {
|
---|
618 | uint8 ifnum;
|
---|
619 | uint8 setting;
|
---|
620 |
|
---|
621 | in_uint32_le(s, devid);
|
---|
622 | proxy = devid2proxy (devid);
|
---|
623 |
|
---|
624 | if (!proxy)
|
---|
625 | {
|
---|
626 | rdpusb_send_access_denied (code, devid);
|
---|
627 | break;
|
---|
628 | }
|
---|
629 |
|
---|
630 | in_uint8(s, ifnum);
|
---|
631 | in_uint8(s, setting);
|
---|
632 |
|
---|
633 | rc = op_usbproxy_back_interface_setting(proxy, ifnum, setting);
|
---|
634 |
|
---|
635 | if (!rc)
|
---|
636 | {
|
---|
637 | rdpusb_send_reply (code, vrdp_usb_status (rc, &proxy->Dev), devid);
|
---|
638 | }
|
---|
639 | } break;
|
---|
640 |
|
---|
641 | case RDPUSB_REQ_QUEUE_URB:
|
---|
642 | {
|
---|
643 | uint32 handle;
|
---|
644 | uint8 type;
|
---|
645 | uint8 ep;
|
---|
646 | uint8 dir;
|
---|
647 | uint32 urblen;
|
---|
648 | uint32 datalen;
|
---|
649 |
|
---|
650 | PVUSBURB pUrb; // struct vusb_urb *urb;
|
---|
651 |
|
---|
652 | in_uint32_le(s, devid);
|
---|
653 | proxy = devid2proxy (devid);
|
---|
654 |
|
---|
655 | if (!proxy)
|
---|
656 | {
|
---|
657 | /* No reply. */
|
---|
658 | break;
|
---|
659 | }
|
---|
660 |
|
---|
661 | in_uint32(s, handle);
|
---|
662 | in_uint8(s, type);
|
---|
663 | in_uint8(s, ep);
|
---|
664 | in_uint8(s, dir);
|
---|
665 | in_uint32(s, urblen);
|
---|
666 | in_uint32(s, datalen);
|
---|
667 |
|
---|
668 | /* Allocate a single block for URB description and data buffer */
|
---|
669 | pUrb = (PVUSBURB)xmalloc (sizeof (VUSBURB) +
|
---|
670 | (urblen <= sizeof (pUrb->abData)? 0: urblen - sizeof (pUrb->abData))
|
---|
671 | );
|
---|
672 | memset (pUrb, 0, sizeof (VUSBURB));
|
---|
673 | pUrb->pDev = &proxy->Dev;
|
---|
674 | pUrb->handle = handle;
|
---|
675 | pUrb->enmType = type;
|
---|
676 | pUrb->enmStatus = 0;
|
---|
677 | pUrb->EndPt = ep;
|
---|
678 | pUrb->enmDir = dir;
|
---|
679 | pUrb->cbData = urblen;
|
---|
680 |
|
---|
681 | Log(("RDPUSB: queued URB handle = %d\n", handle));
|
---|
682 |
|
---|
683 | if (datalen)
|
---|
684 | {
|
---|
685 | in_uint8a (s, pUrb->abData, datalen);
|
---|
686 | }
|
---|
687 |
|
---|
688 | rc = op_usbproxy_back_queue_urb(pUrb);
|
---|
689 |
|
---|
690 | /* No reply required. */
|
---|
691 |
|
---|
692 | if (rc)
|
---|
693 | {
|
---|
694 | if (proxy->pUrbs)
|
---|
695 | {
|
---|
696 | proxy->pUrbs->pPrev = pUrb;
|
---|
697 | }
|
---|
698 |
|
---|
699 | pUrb->pNext = proxy->pUrbs;
|
---|
700 | proxy->pUrbs = pUrb;
|
---|
701 | }
|
---|
702 | else
|
---|
703 | {
|
---|
704 | xfree (pUrb);
|
---|
705 | }
|
---|
706 | } break;
|
---|
707 |
|
---|
708 | case RDPUSB_REQ_REAP_URB:
|
---|
709 | {
|
---|
710 | rdpusb_reap_urbs ();
|
---|
711 | } break;
|
---|
712 |
|
---|
713 | case RDPUSB_REQ_CLEAR_HALTED_EP:
|
---|
714 | {
|
---|
715 | uint8 ep;
|
---|
716 |
|
---|
717 | in_uint32_le(s, devid);
|
---|
718 | proxy = devid2proxy (devid);
|
---|
719 |
|
---|
720 | if (!proxy)
|
---|
721 | {
|
---|
722 | rdpusb_send_access_denied (code, devid);
|
---|
723 | break;
|
---|
724 | }
|
---|
725 |
|
---|
726 | in_uint8(s, ep);
|
---|
727 |
|
---|
728 | rc = op_usbproxy_back_clear_halted_ep(proxy, ep);
|
---|
729 |
|
---|
730 | if (!rc)
|
---|
731 | {
|
---|
732 | rdpusb_send_reply (code, vrdp_usb_status (rc, &proxy->Dev), devid);
|
---|
733 | }
|
---|
734 | } break;
|
---|
735 |
|
---|
736 | case RDPUSB_REQ_CANCEL_URB:
|
---|
737 | {
|
---|
738 | uint32 handle;
|
---|
739 | PVUSBURB pUrb = NULL;
|
---|
740 |
|
---|
741 | in_uint32_le(s, devid);
|
---|
742 | proxy = devid2proxy (devid);
|
---|
743 |
|
---|
744 | if (!proxy)
|
---|
745 | {
|
---|
746 | rdpusb_send_access_denied (code, devid);
|
---|
747 | break;
|
---|
748 | }
|
---|
749 |
|
---|
750 | in_uint32_le(s, handle);
|
---|
751 |
|
---|
752 | pUrb = proxy->pUrbs;
|
---|
753 |
|
---|
754 | while (pUrb && pUrb->handle != handle)
|
---|
755 | {
|
---|
756 | pUrb = pUrb->pNext;
|
---|
757 | }
|
---|
758 |
|
---|
759 | if (pUrb)
|
---|
760 | {
|
---|
761 | op_usbproxy_back_cancel_urb(pUrb);
|
---|
762 |
|
---|
763 | /* No reply required. */
|
---|
764 |
|
---|
765 | /* Remove URB from list. */
|
---|
766 | if (pUrb->pPrev)
|
---|
767 | {
|
---|
768 | pUrb->pPrev->pNext = pUrb->pNext;
|
---|
769 | }
|
---|
770 | else
|
---|
771 | {
|
---|
772 | proxy->pUrbs = pUrb->pNext;
|
---|
773 | }
|
---|
774 |
|
---|
775 | if (pUrb->pNext)
|
---|
776 | {
|
---|
777 | pUrb->pNext->pPrev = pUrb->pPrev;
|
---|
778 | }
|
---|
779 |
|
---|
780 | pUrb->pNext = pUrb->pPrev = NULL;
|
---|
781 |
|
---|
782 | Log(("Cancelled URB %p\n", pUrb));
|
---|
783 |
|
---|
784 | // xfree (pUrb);
|
---|
785 | }
|
---|
786 | } break;
|
---|
787 |
|
---|
788 | case RDPUSB_REQ_DEVICE_LIST:
|
---|
789 | {
|
---|
790 | void *buf = NULL;
|
---|
791 | int len = 0;
|
---|
792 |
|
---|
793 | buf = build_device_list (&len);
|
---|
794 |
|
---|
795 | s = rdpusb_init_packet(len? len: 2, code);
|
---|
796 | if (len)
|
---|
797 | {
|
---|
798 | out_uint8p (s, buf, len);
|
---|
799 | }
|
---|
800 | else
|
---|
801 | {
|
---|
802 | out_uint16_le(s, 0);
|
---|
803 | }
|
---|
804 | s_mark_end(s);
|
---|
805 | rdpusb_send(s);
|
---|
806 |
|
---|
807 | if (buf)
|
---|
808 | {
|
---|
809 | free (buf);
|
---|
810 | }
|
---|
811 | } break;
|
---|
812 |
|
---|
813 | case RDPUSB_REQ_NEGOTIATE:
|
---|
814 | {
|
---|
815 | s = rdpusb_init_packet(1, code);
|
---|
816 | out_uint8(s, VRDP_USB_CAPS_FLAG_ASYNC);
|
---|
817 | s_mark_end(s);
|
---|
818 | rdpusb_send(s);
|
---|
819 | } break;
|
---|
820 |
|
---|
821 | default:
|
---|
822 | unimpl("RDPUSB code %d\n", code);
|
---|
823 | break;
|
---|
824 | }
|
---|
825 | }
|
---|
826 |
|
---|
827 | void
|
---|
828 | rdpusb_add_fds(int *n, fd_set * rfds, fd_set * wfds)
|
---|
829 | {
|
---|
830 | PUSBPROXYDEV proxy = g_proxies;
|
---|
831 |
|
---|
832 | // Log(("RDPUSB: rdpusb_add_fds: begin *n = %d\n", *n));
|
---|
833 |
|
---|
834 | while (proxy)
|
---|
835 | {
|
---|
836 | int fd = USBProxyDeviceLinuxGetFD(proxy);
|
---|
837 |
|
---|
838 | if (fd != -1)
|
---|
839 | {
|
---|
840 | // Log(("RDPUSB: rdpusb_add_fds: adding %d\n", proxy->priv.File));
|
---|
841 |
|
---|
842 | FD_SET(fd, rfds);
|
---|
843 | FD_SET(fd, wfds);
|
---|
844 | *n = MAX(*n, fd);
|
---|
845 | }
|
---|
846 |
|
---|
847 | proxy = proxy->pNext;
|
---|
848 | }
|
---|
849 |
|
---|
850 | // Log(("RDPUSB: rdpusb_add_fds: end *n = %d\n", *n));
|
---|
851 |
|
---|
852 | return;
|
---|
853 | }
|
---|
854 |
|
---|
855 | void
|
---|
856 | rdpusb_check_fds(fd_set * rfds, fd_set * wfds)
|
---|
857 | {
|
---|
858 | PUSBPROXYDEV proxy = g_proxies;
|
---|
859 | unsigned found = 0;
|
---|
860 |
|
---|
861 | while (proxy)
|
---|
862 | {
|
---|
863 | int fd = USBProxyDeviceLinuxGetFD(proxy);
|
---|
864 |
|
---|
865 | if (fd != -1)
|
---|
866 | {
|
---|
867 | if (FD_ISSET(fd, rfds))
|
---|
868 | found = 1;
|
---|
869 | if (FD_ISSET(fd, wfds))
|
---|
870 | found = 1;
|
---|
871 | }
|
---|
872 |
|
---|
873 | proxy = proxy->pNext;
|
---|
874 | }
|
---|
875 |
|
---|
876 | // Log(("RDPUSB: rdpusb_check_fds: begin\n"));
|
---|
877 |
|
---|
878 | if (found)
|
---|
879 | rdpusb_reap_urbs ();
|
---|
880 |
|
---|
881 | // Log(("RDPUSB: rdpusb_check_fds: end\n"));
|
---|
882 |
|
---|
883 | return;
|
---|
884 | }
|
---|
885 |
|
---|
886 |
|
---|
887 | RD_BOOL
|
---|
888 | rdpusb_init(void)
|
---|
889 | {
|
---|
890 | bool fUseUsbfs;
|
---|
891 | if (RT_SUCCESS(USBProxyLinuxChooseMethod(&fUseUsbfs, &g_pcszDevicesRoot)))
|
---|
892 | {
|
---|
893 | g_fUseSysfs = !fUseUsbfs;
|
---|
894 | rdpusb_channel =
|
---|
895 | channel_register("vrdpusb", CHANNEL_OPTION_INITIALIZED | CHANNEL_OPTION_ENCRYPT_RDP,
|
---|
896 | rdpusb_process);
|
---|
897 | return (rdpusb_channel != NULL);
|
---|
898 | }
|
---|
899 | return false;
|
---|
900 | }
|
---|
901 |
|
---|
902 | void
|
---|
903 | rdpusb_close (void)
|
---|
904 | {
|
---|
905 | PUSBPROXYDEV proxy = g_proxies;
|
---|
906 |
|
---|
907 | while (proxy)
|
---|
908 | {
|
---|
909 | PUSBPROXYDEV pNext = proxy->pNext;
|
---|
910 |
|
---|
911 | Log(("RDPUSB: closing proxy %p\n", proxy));
|
---|
912 |
|
---|
913 | op_usbproxy_back_close(proxy);
|
---|
914 | xfree (proxy);
|
---|
915 |
|
---|
916 | proxy = pNext;
|
---|
917 | }
|
---|
918 |
|
---|
919 | return;
|
---|
920 | }
|
---|