[55401] | 1 | /* $Id: RemoteUSBBackend.cpp 93115 2022-01-01 11:31:46Z vboxsync $ */
|
---|
[31891] | 2 | /** @file
|
---|
| 3 | * VirtualBox Remote USB backend
|
---|
| 4 | */
|
---|
| 5 |
|
---|
| 6 | /*
|
---|
[93115] | 7 | * Copyright (C) 2006-2022 Oracle Corporation
|
---|
[31891] | 8 | *
|
---|
[31892] | 9 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
| 10 | * available from http://www.virtualbox.org. This file is free software;
|
---|
| 11 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
| 12 | * General Public License (GPL) as published by the Free Software
|
---|
| 13 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
| 14 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
| 15 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
[31891] | 16 | */
|
---|
| 17 |
|
---|
[50530] | 18 | #define LOG_GROUP LOG_GROUP_USB_REMOTE
|
---|
[67914] | 19 | #include "LoggingNew.h"
|
---|
| 20 |
|
---|
[31891] | 21 | #include "ConsoleImpl.h"
|
---|
| 22 | #include "ConsoleVRDPServer.h"
|
---|
| 23 | #include "RemoteUSBBackend.h"
|
---|
| 24 | #include "RemoteUSBDeviceImpl.h"
|
---|
| 25 |
|
---|
[33004] | 26 | #include <VBox/RemoteDesktop/VRDE.h>
|
---|
[31891] | 27 | #include <VBox/vrdpusb.h>
|
---|
| 28 | #include <VBox/err.h>
|
---|
| 29 | #include <VBox/log.h>
|
---|
| 30 |
|
---|
| 31 | #include <VBox/vusb.h>
|
---|
| 32 |
|
---|
| 33 | #include <iprt/time.h>
|
---|
| 34 |
|
---|
| 35 | /** @page pg_vrdb_usb Async Remote USB
|
---|
| 36 | *
|
---|
| 37 | *
|
---|
| 38 | * USB backend functions are called in EMT so care must be taken to prevent
|
---|
| 39 | * delays in the functions execution.
|
---|
| 40 | *
|
---|
| 41 | * Among 11 backend functions 10 just return a success indicator.
|
---|
| 42 | *
|
---|
| 43 | * Such a function usually will check pending error code and if everything is ok,
|
---|
| 44 | * submit asynchronous RDP request and return success immediately.
|
---|
| 45 | *
|
---|
| 46 | * On actual completion of each request, the status will be saved as
|
---|
| 47 | * pending, so in case of an error all further functions will fail with
|
---|
| 48 | * device disconnected condition.
|
---|
| 49 | * @todo May be a device disconnect notification for console is required?
|
---|
| 50 | *
|
---|
| 51 | * The only remaining function that needs special processing is
|
---|
| 52 | * the reap_urb. It has a timeout parameter.
|
---|
| 53 | * Normally, the timeout is 0, as result of polling from VUSB frame timer.
|
---|
| 54 | * It is ok for async processing, the backend will periodically reap urbs from client.
|
---|
| 55 | * And already reaped URBs from client will be returned for the call.
|
---|
| 56 | * Exceptions:
|
---|
| 57 | * 1) during device initialization, when obtaining device descriptions
|
---|
| 58 | * the timeout is -1, and the request is expected to be processed synchronously.
|
---|
| 59 | * It looks like only 3 URBs with some information are retrieved that way.
|
---|
| 60 | * Probably, one can return this information in DEVICE_LIST together with the
|
---|
| 61 | * device description and when such request are submitted, just return
|
---|
| 62 | * the prefetched data.
|
---|
| 63 | * 2) during suspend timeout is non zero (10 or less milliseconds),
|
---|
| 64 | * and URB's are reaped for about 1 second. But here network delays
|
---|
| 65 | * will not affect the timeout, so it is ok.
|
---|
| 66 | *
|
---|
| 67 | *
|
---|
[65125] | 68 | * @section sub_vrdb_usb_dad Device attaching/detaching
|
---|
[31891] | 69 | *
|
---|
| 70 | * Devices are attached when client is connected or when a new device is connected to client.
|
---|
| 71 | * Devices are detached when client is disconnected (all devices) or a device is disconnected
|
---|
| 72 | * the client side.
|
---|
| 73 | *
|
---|
| 74 | * The backend polls the client for list of attached USB devices from RemoteUSBThread.
|
---|
| 75 | *
|
---|
| 76 | */
|
---|
| 77 |
|
---|
| 78 | /* Queued URB submitted to VRDP client. */
|
---|
| 79 | typedef struct _REMOTEUSBQURB
|
---|
| 80 | {
|
---|
| 81 | struct _REMOTEUSBQURB *next;
|
---|
| 82 | struct _REMOTEUSBQURB *prev;
|
---|
| 83 |
|
---|
| 84 | PREMOTEUSBDEVICE pDevice; /* Device, the URB is queued for. */
|
---|
| 85 |
|
---|
| 86 | uint32_t u32Handle; /* The handle of the URB. Generated by the Remote USB backend. */
|
---|
| 87 |
|
---|
| 88 | void *pvData; /* Pointer to URB data allocated by VUSB. */
|
---|
| 89 | void *pvURB; /* Pointer to URB known to VUSB. */
|
---|
| 90 |
|
---|
| 91 | uint32_t u32Len; /* Data length returned by the VRDP client. */
|
---|
| 92 | uint32_t u32Err; /* URB error code returned by the VRDP client. */
|
---|
| 93 |
|
---|
| 94 | bool fCompleted; /* The URB has been returned back by VRDP client. */
|
---|
| 95 | bool fInput; /* This URB receives data from the client. */
|
---|
| 96 |
|
---|
[33004] | 97 | uint32_t u32TransferredLen; /* For VRDE_USB_DIRECTION_OUT URBs = bytes written.
|
---|
| 98 | * For VRDE_USB_DIRECTION_IN URBs = bytes received.
|
---|
[31891] | 99 | */
|
---|
| 100 | } REMOTEUSBQURB;
|
---|
| 101 |
|
---|
| 102 | /* Remote USB device instance data. */
|
---|
| 103 | typedef struct _REMOTEUSBDEVICE
|
---|
| 104 | {
|
---|
| 105 | struct _REMOTEUSBDEVICE *prev;
|
---|
| 106 | struct _REMOTEUSBDEVICE *next;
|
---|
| 107 |
|
---|
| 108 | RemoteUSBBackend *pOwner;
|
---|
| 109 |
|
---|
[33004] | 110 | VRDEUSBDEVID id; /* The remote identifier, assigned by client. */
|
---|
[31891] | 111 |
|
---|
| 112 | uint32_t u32ClientId; /* The identifier of the remote client. */
|
---|
| 113 |
|
---|
| 114 | REMOTEUSBQURB *pHeadQURBs; /* List of URBs queued for the device. */
|
---|
| 115 | REMOTEUSBQURB *pTailQURBs;
|
---|
| 116 |
|
---|
| 117 | volatile uint32_t hURB; /* Source for URB's handles. */
|
---|
| 118 | bool fFailed; /* True if an operation has failed for the device. */
|
---|
| 119 | RTCRITSECT critsect; /* Protects the queued urb list. */
|
---|
[49814] | 120 | volatile bool fWokenUp; /* Flag whther the reaper was woken up. */
|
---|
[31891] | 121 | } REMOTEUSBDEVICE;
|
---|
| 122 |
|
---|
| 123 |
|
---|
| 124 |
|
---|
| 125 | static void requestDevice(REMOTEUSBDEVICE *pDevice)
|
---|
| 126 | {
|
---|
| 127 | int rc = RTCritSectEnter(&pDevice->critsect);
|
---|
| 128 | AssertRC(rc);
|
---|
| 129 | }
|
---|
| 130 |
|
---|
| 131 | static void releaseDevice(REMOTEUSBDEVICE *pDevice)
|
---|
| 132 | {
|
---|
| 133 | RTCritSectLeave(&pDevice->critsect);
|
---|
| 134 | }
|
---|
| 135 |
|
---|
| 136 | static REMOTEUSBQURB *qurbAlloc(PREMOTEUSBDEVICE pDevice)
|
---|
| 137 | {
|
---|
[63563] | 138 | /** @todo reuse URBs. */
|
---|
[31891] | 139 | REMOTEUSBQURB *pQURB = (REMOTEUSBQURB *)RTMemAllocZ (sizeof (REMOTEUSBQURB));
|
---|
| 140 |
|
---|
| 141 | if (pQURB)
|
---|
| 142 | {
|
---|
| 143 | pQURB->pDevice = pDevice;
|
---|
| 144 | }
|
---|
| 145 |
|
---|
| 146 | return pQURB;
|
---|
| 147 | }
|
---|
| 148 |
|
---|
| 149 | static void qurbFree (REMOTEUSBQURB *pQURB)
|
---|
| 150 | {
|
---|
| 151 | RTMemFree (pQURB);
|
---|
| 152 | return;
|
---|
| 153 | }
|
---|
| 154 |
|
---|
| 155 |
|
---|
| 156 | /* Called by VRDP server when the client responds to a request on USB channel. */
|
---|
[51092] | 157 | DECLCALLBACK(int) USBClientResponseCallback(void *pv, uint32_t u32ClientId, uint8_t code, const void *pvRet, uint32_t cbRet)
|
---|
[31891] | 158 | {
|
---|
[63256] | 159 | RT_NOREF(u32ClientId);
|
---|
[31891] | 160 | int rc = VINF_SUCCESS;
|
---|
| 161 |
|
---|
| 162 | LogFlow(("USBClientResponseCallback: id = %d, pv = %p, code = %d, pvRet = %p, cbRet = %d\n",
|
---|
| 163 | u32ClientId, pv, code, pvRet, cbRet));
|
---|
| 164 |
|
---|
| 165 | RemoteUSBBackend *pThis = (RemoteUSBBackend *)pv;
|
---|
| 166 |
|
---|
| 167 | switch (code)
|
---|
| 168 | {
|
---|
[33004] | 169 | case VRDE_USB_REQ_DEVICE_LIST:
|
---|
[31891] | 170 | {
|
---|
[51092] | 171 | rc = pThis->saveDeviceList(pvRet, cbRet);
|
---|
[31891] | 172 | } break;
|
---|
| 173 |
|
---|
[33004] | 174 | case VRDE_USB_REQ_NEGOTIATE:
|
---|
[31891] | 175 | {
|
---|
[51092] | 176 | if (pvRet && cbRet >= sizeof(VRDEUSBREQNEGOTIATERET))
|
---|
[31891] | 177 | {
|
---|
[33004] | 178 | VRDEUSBREQNEGOTIATERET *pret = (VRDEUSBREQNEGOTIATERET *)pvRet;
|
---|
[31891] | 179 |
|
---|
[51092] | 180 | rc = pThis->negotiateResponse(pret, cbRet);
|
---|
[31891] | 181 | }
|
---|
| 182 | else
|
---|
| 183 | {
|
---|
| 184 | Log(("USBClientResponseCallback: WARNING: not enough data in response: pv = %p, cb = %d, expected %d.\n",
|
---|
[51092] | 185 | pvRet, cbRet, sizeof(VRDEUSBREQNEGOTIATERET)));
|
---|
[31891] | 186 |
|
---|
| 187 | rc = VERR_INVALID_PARAMETER;
|
---|
| 188 | }
|
---|
| 189 | } break;
|
---|
| 190 |
|
---|
[33004] | 191 | case VRDE_USB_REQ_REAP_URB:
|
---|
[31891] | 192 | {
|
---|
[51092] | 193 | rc = pThis->reapURB(pvRet, cbRet);
|
---|
[31891] | 194 |
|
---|
| 195 | LogFlow(("USBClientResponseCallback: reap URB, rc = %Rrc.\n", rc));
|
---|
| 196 | } break;
|
---|
| 197 |
|
---|
[33004] | 198 | case VRDE_USB_REQ_QUEUE_URB:
|
---|
| 199 | case VRDE_USB_REQ_CLOSE:
|
---|
| 200 | case VRDE_USB_REQ_CANCEL_URB:
|
---|
[31891] | 201 | {
|
---|
| 202 | /* Do nothing, actually this should not happen. */
|
---|
| 203 | Log(("USBClientResponseCallback: WARNING: response to a request %d is not expected!!!\n", code));
|
---|
| 204 | } break;
|
---|
| 205 |
|
---|
[33004] | 206 | case VRDE_USB_REQ_OPEN:
|
---|
| 207 | case VRDE_USB_REQ_RESET:
|
---|
| 208 | case VRDE_USB_REQ_SET_CONFIG:
|
---|
| 209 | case VRDE_USB_REQ_CLAIM_INTERFACE:
|
---|
| 210 | case VRDE_USB_REQ_RELEASE_INTERFACE:
|
---|
| 211 | case VRDE_USB_REQ_INTERFACE_SETTING:
|
---|
| 212 | case VRDE_USB_REQ_CLEAR_HALTED_EP:
|
---|
[31891] | 213 | {
|
---|
| 214 | /*
|
---|
| 215 | * Device specific responses with status codes.
|
---|
| 216 | */
|
---|
[51092] | 217 | if (pvRet && cbRet >= sizeof(VRDEUSBREQRETHDR))
|
---|
[31891] | 218 | {
|
---|
[33004] | 219 | VRDEUSBREQRETHDR *pret = (VRDEUSBREQRETHDR *)pvRet;
|
---|
[31891] | 220 |
|
---|
[33004] | 221 | if (pret->status != VRDE_USB_STATUS_SUCCESS)
|
---|
[31891] | 222 | {
|
---|
[51092] | 223 | REMOTEUSBDEVICE *pDevice = pThis->deviceFromId(pret->id);
|
---|
[31891] | 224 |
|
---|
| 225 | if (!pDevice)
|
---|
| 226 | {
|
---|
| 227 | Log(("USBClientResponseCallback: WARNING: invalid device id %08X.\n", pret->id));
|
---|
| 228 | rc = VERR_INVALID_PARAMETER;
|
---|
| 229 | }
|
---|
| 230 | else
|
---|
| 231 | {
|
---|
| 232 | Log(("USBClientResponseCallback: WARNING: the operation failed, status %d\n", pret->status));
|
---|
| 233 | pDevice->fFailed = true;
|
---|
| 234 | }
|
---|
| 235 | }
|
---|
| 236 | }
|
---|
| 237 | else
|
---|
| 238 | {
|
---|
| 239 | Log(("USBClientResponseCallback: WARNING: not enough data in response: pv = %p, cb = %d, expected %d.\n",
|
---|
[51092] | 240 | pvRet, cbRet, sizeof(VRDEUSBREQRETHDR)));
|
---|
[31891] | 241 | }
|
---|
| 242 | } break;
|
---|
| 243 |
|
---|
| 244 | default:
|
---|
| 245 | {
|
---|
| 246 | Log(("USBClientResponseCallback: WARNING: invalid code %d\n", code));
|
---|
| 247 | } break;
|
---|
| 248 | }
|
---|
| 249 |
|
---|
| 250 | return rc;
|
---|
| 251 | }
|
---|
| 252 |
|
---|
| 253 | /*
|
---|
| 254 | * Backend entry points.
|
---|
| 255 | */
|
---|
[51092] | 256 | static DECLCALLBACK(int) iface_Open(PREMOTEUSBBACKEND pInstance, const char *pszAddress,
|
---|
| 257 | size_t cbAddress, PREMOTEUSBDEVICE *ppDevice)
|
---|
[31891] | 258 | {
|
---|
[63256] | 259 | RT_NOREF(cbAddress);
|
---|
[31891] | 260 | int rc = VINF_SUCCESS;
|
---|
| 261 |
|
---|
| 262 | RemoteUSBBackend *pThis = (RemoteUSBBackend *)pInstance;
|
---|
| 263 |
|
---|
[51092] | 264 | REMOTEUSBDEVICE *pDevice = (REMOTEUSBDEVICE *)RTMemAllocZ(sizeof(REMOTEUSBDEVICE));
|
---|
[31891] | 265 |
|
---|
| 266 | if (!pDevice)
|
---|
| 267 | {
|
---|
| 268 | rc = VERR_NO_MEMORY;
|
---|
| 269 | }
|
---|
| 270 | else
|
---|
| 271 | {
|
---|
| 272 | /* Parse given address string to find the device identifier.
|
---|
| 273 | * The format is "REMOTEUSB0xAAAABBBB&0xCCCCDDDD", where AAAABBBB is hex device identifier
|
---|
| 274 | * and CCCCDDDD is hex client id.
|
---|
| 275 | */
|
---|
[51092] | 276 | if (strncmp(pszAddress, REMOTE_USB_BACKEND_PREFIX_S, REMOTE_USB_BACKEND_PREFIX_LEN) != 0)
|
---|
[31891] | 277 | {
|
---|
| 278 | AssertFailed();
|
---|
| 279 | rc = VERR_INVALID_PARAMETER;
|
---|
| 280 | }
|
---|
| 281 | else
|
---|
| 282 | {
|
---|
| 283 | /* Initialize the device structure. */
|
---|
| 284 | pDevice->pOwner = pThis;
|
---|
[49814] | 285 | pDevice->fWokenUp = false;
|
---|
[31891] | 286 |
|
---|
| 287 | rc = RTCritSectInit(&pDevice->critsect);
|
---|
| 288 | AssertRC(rc);
|
---|
| 289 |
|
---|
| 290 | if (RT_SUCCESS(rc))
|
---|
| 291 | {
|
---|
[51092] | 292 | pDevice->id = RTStrToUInt32(&pszAddress[REMOTE_USB_BACKEND_PREFIX_LEN]);
|
---|
[31891] | 293 |
|
---|
[51092] | 294 | size_t l = strlen(pszAddress);
|
---|
[31891] | 295 |
|
---|
[51092] | 296 | if (l >= REMOTE_USB_BACKEND_PREFIX_LEN + strlen("0x12345678&0x87654321"))
|
---|
[31891] | 297 | {
|
---|
[51092] | 298 | const char *p = &pszAddress[REMOTE_USB_BACKEND_PREFIX_LEN + strlen("0x12345678")];
|
---|
[31891] | 299 | if (*p == '&')
|
---|
| 300 | {
|
---|
[51092] | 301 | pDevice->u32ClientId = RTStrToUInt32(p + 1);
|
---|
[31891] | 302 | }
|
---|
| 303 | else
|
---|
| 304 | {
|
---|
[51092] | 305 | AssertFailed();
|
---|
[31891] | 306 | rc = VERR_INVALID_PARAMETER;
|
---|
| 307 | }
|
---|
| 308 | }
|
---|
| 309 | else
|
---|
| 310 | {
|
---|
[51092] | 311 | AssertFailed();
|
---|
[31891] | 312 | rc = VERR_INVALID_PARAMETER;
|
---|
| 313 | }
|
---|
| 314 |
|
---|
| 315 | if (RT_SUCCESS(rc))
|
---|
| 316 | {
|
---|
[33004] | 317 | VRDE_USB_REQ_OPEN_PARM parm;
|
---|
[31891] | 318 |
|
---|
[33004] | 319 | parm.code = VRDE_USB_REQ_OPEN;
|
---|
[31891] | 320 | parm.id = pDevice->id;
|
---|
| 321 |
|
---|
[51092] | 322 | pThis->VRDPServer()->SendUSBRequest(pDevice->u32ClientId, &parm, sizeof(parm));
|
---|
[31891] | 323 | }
|
---|
| 324 | }
|
---|
| 325 | }
|
---|
| 326 | }
|
---|
| 327 |
|
---|
| 328 | if (RT_SUCCESS(rc))
|
---|
| 329 | {
|
---|
| 330 | *ppDevice = pDevice;
|
---|
| 331 |
|
---|
[51092] | 332 | pThis->addDevice(pDevice);
|
---|
[31891] | 333 | }
|
---|
| 334 | else
|
---|
| 335 | {
|
---|
[51092] | 336 | RTMemFree(pDevice);
|
---|
[31891] | 337 | }
|
---|
| 338 |
|
---|
| 339 | return rc;
|
---|
| 340 | }
|
---|
| 341 |
|
---|
[51092] | 342 | static DECLCALLBACK(void) iface_Close(PREMOTEUSBDEVICE pDevice)
|
---|
[31891] | 343 | {
|
---|
| 344 | RemoteUSBBackend *pThis = pDevice->pOwner;
|
---|
| 345 |
|
---|
[33004] | 346 | VRDE_USB_REQ_CLOSE_PARM parm;
|
---|
[31891] | 347 |
|
---|
[33004] | 348 | parm.code = VRDE_USB_REQ_CLOSE;
|
---|
[31891] | 349 | parm.id = pDevice->id;
|
---|
| 350 |
|
---|
[51092] | 351 | pThis->VRDPServer()->SendUSBRequest(pDevice->u32ClientId, &parm, sizeof(parm));
|
---|
[31891] | 352 |
|
---|
[51092] | 353 | pThis->removeDevice(pDevice);
|
---|
[31891] | 354 |
|
---|
[51092] | 355 | if (RTCritSectIsInitialized(&pDevice->critsect))
|
---|
[31891] | 356 | {
|
---|
[51092] | 357 | RTCritSectDelete(&pDevice->critsect);
|
---|
[31891] | 358 | }
|
---|
| 359 |
|
---|
[51092] | 360 | RTMemFree(pDevice);
|
---|
[31891] | 361 |
|
---|
| 362 | return;
|
---|
| 363 | }
|
---|
| 364 |
|
---|
[51092] | 365 | static DECLCALLBACK(int) iface_Reset(PREMOTEUSBDEVICE pDevice)
|
---|
[31891] | 366 | {
|
---|
| 367 | RemoteUSBBackend *pThis = pDevice->pOwner;
|
---|
| 368 |
|
---|
| 369 | if (pDevice->fFailed)
|
---|
| 370 | {
|
---|
| 371 | return VERR_VUSB_DEVICE_NOT_ATTACHED;
|
---|
| 372 | }
|
---|
| 373 |
|
---|
[33004] | 374 | VRDE_USB_REQ_RESET_PARM parm;
|
---|
[31891] | 375 |
|
---|
[33004] | 376 | parm.code = VRDE_USB_REQ_RESET;
|
---|
[31891] | 377 | parm.id = pDevice->id;
|
---|
| 378 |
|
---|
[51092] | 379 | pThis->VRDPServer()->SendUSBRequest(pDevice->u32ClientId, &parm, sizeof(parm));
|
---|
[31891] | 380 |
|
---|
| 381 | return VINF_SUCCESS;
|
---|
| 382 | }
|
---|
| 383 |
|
---|
[51092] | 384 | static DECLCALLBACK(int) iface_SetConfig(PREMOTEUSBDEVICE pDevice, uint8_t u8Cfg)
|
---|
[31891] | 385 | {
|
---|
| 386 | RemoteUSBBackend *pThis = pDevice->pOwner;
|
---|
| 387 |
|
---|
| 388 | if (pDevice->fFailed)
|
---|
| 389 | {
|
---|
| 390 | return VERR_VUSB_DEVICE_NOT_ATTACHED;
|
---|
| 391 | }
|
---|
| 392 |
|
---|
[33004] | 393 | VRDE_USB_REQ_SET_CONFIG_PARM parm;
|
---|
[31891] | 394 |
|
---|
[33004] | 395 | parm.code = VRDE_USB_REQ_SET_CONFIG;
|
---|
[31891] | 396 | parm.id = pDevice->id;
|
---|
| 397 | parm.configuration = u8Cfg;
|
---|
| 398 |
|
---|
[51092] | 399 | pThis->VRDPServer()->SendUSBRequest(pDevice->u32ClientId, &parm, sizeof(parm));
|
---|
[31891] | 400 |
|
---|
| 401 | return VINF_SUCCESS;
|
---|
| 402 | }
|
---|
| 403 |
|
---|
[51092] | 404 | static DECLCALLBACK(int) iface_ClaimInterface(PREMOTEUSBDEVICE pDevice, uint8_t u8Ifnum)
|
---|
[31891] | 405 | {
|
---|
| 406 | RemoteUSBBackend *pThis = pDevice->pOwner;
|
---|
| 407 |
|
---|
| 408 | if (pDevice->fFailed)
|
---|
| 409 | {
|
---|
| 410 | return VERR_VUSB_DEVICE_NOT_ATTACHED;
|
---|
| 411 | }
|
---|
| 412 |
|
---|
[33004] | 413 | VRDE_USB_REQ_CLAIM_INTERFACE_PARM parm;
|
---|
[31891] | 414 |
|
---|
[33004] | 415 | parm.code = VRDE_USB_REQ_CLAIM_INTERFACE;
|
---|
[31891] | 416 | parm.id = pDevice->id;
|
---|
| 417 | parm.iface = u8Ifnum;
|
---|
| 418 |
|
---|
[51092] | 419 | pThis->VRDPServer()->SendUSBRequest(pDevice->u32ClientId, &parm, sizeof(parm));
|
---|
[31891] | 420 |
|
---|
| 421 | return VINF_SUCCESS;
|
---|
| 422 | }
|
---|
| 423 |
|
---|
[51092] | 424 | static DECLCALLBACK(int) iface_ReleaseInterface(PREMOTEUSBDEVICE pDevice, uint8_t u8Ifnum)
|
---|
[31891] | 425 | {
|
---|
| 426 | RemoteUSBBackend *pThis = pDevice->pOwner;
|
---|
| 427 |
|
---|
| 428 | if (pDevice->fFailed)
|
---|
| 429 | {
|
---|
| 430 | return VERR_VUSB_DEVICE_NOT_ATTACHED;
|
---|
| 431 | }
|
---|
| 432 |
|
---|
[33004] | 433 | VRDE_USB_REQ_RELEASE_INTERFACE_PARM parm;
|
---|
[31891] | 434 |
|
---|
[33004] | 435 | parm.code = VRDE_USB_REQ_RELEASE_INTERFACE;
|
---|
[31891] | 436 | parm.id = pDevice->id;
|
---|
| 437 | parm.iface = u8Ifnum;
|
---|
| 438 |
|
---|
[51092] | 439 | pThis->VRDPServer()->SendUSBRequest(pDevice->u32ClientId, &parm, sizeof(parm));
|
---|
[31891] | 440 |
|
---|
| 441 | return VINF_SUCCESS;
|
---|
| 442 | }
|
---|
| 443 |
|
---|
[51092] | 444 | static DECLCALLBACK(int) iface_InterfaceSetting(PREMOTEUSBDEVICE pDevice, uint8_t u8Ifnum, uint8_t u8Setting)
|
---|
[31891] | 445 | {
|
---|
| 446 | RemoteUSBBackend *pThis = pDevice->pOwner;
|
---|
| 447 |
|
---|
| 448 | if (pDevice->fFailed)
|
---|
| 449 | {
|
---|
| 450 | return VERR_VUSB_DEVICE_NOT_ATTACHED;
|
---|
| 451 | }
|
---|
| 452 |
|
---|
[33004] | 453 | VRDE_USB_REQ_INTERFACE_SETTING_PARM parm;
|
---|
[31891] | 454 |
|
---|
[33004] | 455 | parm.code = VRDE_USB_REQ_INTERFACE_SETTING;
|
---|
[31891] | 456 | parm.id = pDevice->id;
|
---|
| 457 | parm.iface = u8Ifnum;
|
---|
| 458 | parm.setting = u8Setting;
|
---|
| 459 |
|
---|
[51092] | 460 | pThis->VRDPServer()->SendUSBRequest(pDevice->u32ClientId, &parm, sizeof(parm));
|
---|
[31891] | 461 |
|
---|
| 462 | return VINF_SUCCESS;
|
---|
| 463 | }
|
---|
| 464 |
|
---|
[51092] | 465 | static DECLCALLBACK(int) iface_ClearHaltedEP(PREMOTEUSBDEVICE pDevice, uint8_t u8Ep)
|
---|
[31891] | 466 | {
|
---|
| 467 | RemoteUSBBackend *pThis = pDevice->pOwner;
|
---|
| 468 |
|
---|
| 469 | if (pDevice->fFailed)
|
---|
| 470 | {
|
---|
| 471 | return VERR_VUSB_DEVICE_NOT_ATTACHED;
|
---|
| 472 | }
|
---|
| 473 |
|
---|
[33004] | 474 | VRDE_USB_REQ_CLEAR_HALTED_EP_PARM parm;
|
---|
[31891] | 475 |
|
---|
[33004] | 476 | parm.code = VRDE_USB_REQ_CLEAR_HALTED_EP;
|
---|
[31891] | 477 | parm.id = pDevice->id;
|
---|
| 478 | parm.ep = u8Ep;
|
---|
| 479 |
|
---|
[51092] | 480 | pThis->VRDPServer()->SendUSBRequest(pDevice->u32ClientId, &parm, sizeof(parm));
|
---|
[31891] | 481 |
|
---|
| 482 | return VINF_SUCCESS;
|
---|
| 483 | }
|
---|
| 484 |
|
---|
[51092] | 485 | static DECLCALLBACK(void) iface_CancelURB(PREMOTEUSBDEVICE pDevice, PREMOTEUSBQURB pRemoteURB)
|
---|
[31891] | 486 | {
|
---|
| 487 | RemoteUSBBackend *pThis = pDevice->pOwner;
|
---|
| 488 |
|
---|
[33004] | 489 | VRDE_USB_REQ_CANCEL_URB_PARM parm;
|
---|
[31891] | 490 |
|
---|
[33004] | 491 | parm.code = VRDE_USB_REQ_CANCEL_URB;
|
---|
[31891] | 492 | parm.id = pDevice->id;
|
---|
| 493 | parm.handle = pRemoteURB->u32Handle;
|
---|
| 494 |
|
---|
[51092] | 495 | pThis->VRDPServer()->SendUSBRequest(pDevice->u32ClientId, &parm, sizeof(parm));
|
---|
[31891] | 496 |
|
---|
[51092] | 497 | requestDevice(pDevice);
|
---|
[31891] | 498 |
|
---|
| 499 | /* Remove this urb from the queue. It is safe because if
|
---|
| 500 | * client will return the URB, it will be just ignored
|
---|
| 501 | * in reapURB.
|
---|
| 502 | */
|
---|
| 503 | if (pRemoteURB->prev)
|
---|
| 504 | {
|
---|
| 505 | pRemoteURB->prev->next = pRemoteURB->next;
|
---|
| 506 | }
|
---|
| 507 | else
|
---|
| 508 | {
|
---|
| 509 | pDevice->pHeadQURBs = pRemoteURB->next;
|
---|
| 510 | }
|
---|
| 511 |
|
---|
| 512 | if (pRemoteURB->next)
|
---|
| 513 | {
|
---|
| 514 | pRemoteURB->next->prev = pRemoteURB->prev;
|
---|
| 515 | }
|
---|
| 516 | else
|
---|
| 517 | {
|
---|
| 518 | pDevice->pTailQURBs = pRemoteURB->prev;
|
---|
| 519 | }
|
---|
| 520 |
|
---|
[51092] | 521 | qurbFree(pRemoteURB);
|
---|
[31891] | 522 |
|
---|
[51092] | 523 | releaseDevice(pDevice);
|
---|
[31891] | 524 |
|
---|
| 525 | return;
|
---|
| 526 | }
|
---|
| 527 |
|
---|
[51092] | 528 | static DECLCALLBACK(int) iface_QueueURB(PREMOTEUSBDEVICE pDevice, uint8_t u8Type, uint8_t u8Ep, uint8_t u8Direction,
|
---|
| 529 | uint32_t u32Len, void *pvData, void *pvURB, PREMOTEUSBQURB *ppRemoteURB)
|
---|
[31891] | 530 | {
|
---|
| 531 | int rc = VINF_SUCCESS;
|
---|
| 532 |
|
---|
| 533 | #ifdef DEBUG_sunlover
|
---|
[51092] | 534 | LogFlow(("RemoteUSBBackend::iface_QueueURB: u8Type = %d, u8Ep = %d, u8Direction = %d, data\n%.*Rhxd\n",
|
---|
| 535 | u8Type, u8Ep, u8Direction, u32Len, pvData));
|
---|
[31891] | 536 | #endif /* DEBUG_sunlover */
|
---|
| 537 |
|
---|
| 538 | if (pDevice->fFailed)
|
---|
| 539 | {
|
---|
| 540 | return VERR_VUSB_DEVICE_NOT_ATTACHED;
|
---|
| 541 | }
|
---|
| 542 |
|
---|
| 543 | RemoteUSBBackend *pThis = pDevice->pOwner;
|
---|
| 544 |
|
---|
[33004] | 545 | VRDE_USB_REQ_QUEUE_URB_PARM parm;
|
---|
[31891] | 546 | uint32_t u32Handle = 0;
|
---|
| 547 | uint32_t u32DataLen = 0;
|
---|
| 548 |
|
---|
[51092] | 549 | REMOTEUSBQURB *qurb = qurbAlloc(pDevice);
|
---|
[31891] | 550 |
|
---|
| 551 | if (qurb == NULL)
|
---|
| 552 | {
|
---|
| 553 | rc = VERR_NO_MEMORY;
|
---|
| 554 | goto l_leave;
|
---|
| 555 | }
|
---|
| 556 |
|
---|
| 557 | /*
|
---|
| 558 | * Compute length of data which need to be transferred to the client.
|
---|
| 559 | */
|
---|
| 560 | switch(u8Direction)
|
---|
| 561 | {
|
---|
| 562 | case VUSB_DIRECTION_IN:
|
---|
| 563 | {
|
---|
| 564 | if (u8Type == VUSBXFERTYPE_MSG)
|
---|
| 565 | {
|
---|
| 566 | u32DataLen = 8; /* 8 byte header. */
|
---|
[63563] | 567 | // u32DataLen = u32Len; /// @todo do messages need all information?
|
---|
[31891] | 568 | }
|
---|
| 569 | } break;
|
---|
| 570 |
|
---|
| 571 | case VUSB_DIRECTION_OUT:
|
---|
| 572 | {
|
---|
| 573 | u32DataLen = u32Len;
|
---|
| 574 | } break;
|
---|
| 575 |
|
---|
| 576 | default:
|
---|
| 577 | {
|
---|
| 578 | AssertFailed();
|
---|
| 579 | rc = VERR_INVALID_PARAMETER;
|
---|
| 580 | goto l_leave;
|
---|
| 581 | }
|
---|
| 582 | }
|
---|
| 583 |
|
---|
[33004] | 584 | parm.code = VRDE_USB_REQ_QUEUE_URB;
|
---|
[31891] | 585 | parm.id = pDevice->id;
|
---|
| 586 |
|
---|
| 587 | u32Handle = pDevice->hURB++;
|
---|
| 588 | if (u32Handle == 0)
|
---|
| 589 | {
|
---|
| 590 | u32Handle = pDevice->hURB++;
|
---|
| 591 | }
|
---|
| 592 |
|
---|
| 593 | LogFlow(("RemoteUSBBackend::iface_QueueURB: handle = %d\n", u32Handle));
|
---|
| 594 |
|
---|
| 595 | parm.handle = u32Handle;
|
---|
| 596 |
|
---|
| 597 | switch(u8Type)
|
---|
| 598 | {
|
---|
[33004] | 599 | case VUSBXFERTYPE_CTRL: parm.type = VRDE_USB_TRANSFER_TYPE_CTRL; break;
|
---|
| 600 | case VUSBXFERTYPE_ISOC: parm.type = VRDE_USB_TRANSFER_TYPE_ISOC; break;
|
---|
| 601 | case VUSBXFERTYPE_BULK: parm.type = VRDE_USB_TRANSFER_TYPE_BULK; break;
|
---|
| 602 | case VUSBXFERTYPE_INTR: parm.type = VRDE_USB_TRANSFER_TYPE_INTR; break;
|
---|
| 603 | case VUSBXFERTYPE_MSG: parm.type = VRDE_USB_TRANSFER_TYPE_MSG; break;
|
---|
[31891] | 604 | default: AssertFailed(); rc = VERR_INVALID_PARAMETER; goto l_leave;
|
---|
| 605 | }
|
---|
| 606 |
|
---|
| 607 | parm.ep = u8Ep;
|
---|
| 608 |
|
---|
| 609 | switch(u8Direction)
|
---|
| 610 | {
|
---|
[33004] | 611 | case VUSB_DIRECTION_SETUP: AssertFailed(); parm.direction = VRDE_USB_DIRECTION_SETUP; break;
|
---|
| 612 | case VUSB_DIRECTION_IN: parm.direction = VRDE_USB_DIRECTION_IN; break;
|
---|
| 613 | case VUSB_DIRECTION_OUT: parm.direction = VRDE_USB_DIRECTION_OUT; break;
|
---|
[31891] | 614 | default: AssertFailed(); rc = VERR_INVALID_PARAMETER; goto l_leave;
|
---|
| 615 | }
|
---|
| 616 |
|
---|
| 617 | parm.urblen = u32Len;
|
---|
| 618 | parm.datalen = u32DataLen;
|
---|
| 619 |
|
---|
| 620 | if (u32DataLen)
|
---|
| 621 | {
|
---|
| 622 | parm.data = pvData;
|
---|
| 623 | }
|
---|
| 624 |
|
---|
| 625 | requestDevice (pDevice);
|
---|
| 626 |
|
---|
| 627 | /* Add at tail of queued urb list. */
|
---|
| 628 | qurb->next = NULL;
|
---|
| 629 | qurb->prev = pDevice->pTailQURBs;
|
---|
[33004] | 630 | qurb->u32Err = VRDE_USB_XFER_OK;
|
---|
[31891] | 631 | qurb->u32Len = u32Len;
|
---|
| 632 | qurb->pvData = pvData;
|
---|
| 633 | qurb->pvURB = pvURB;
|
---|
| 634 | qurb->u32Handle = u32Handle;
|
---|
| 635 | qurb->fCompleted = false;
|
---|
| 636 | qurb->fInput = (u8Direction == VUSB_DIRECTION_IN);
|
---|
| 637 | qurb->u32TransferredLen = 0;
|
---|
| 638 |
|
---|
| 639 | if (pDevice->pTailQURBs)
|
---|
| 640 | {
|
---|
| 641 | Assert(pDevice->pTailQURBs->next == NULL);
|
---|
| 642 | pDevice->pTailQURBs->next = qurb;
|
---|
| 643 | }
|
---|
| 644 | else
|
---|
| 645 | {
|
---|
| 646 | /* This is the first URB to be added. */
|
---|
| 647 | Assert(pDevice->pHeadQURBs == NULL);
|
---|
| 648 | pDevice->pHeadQURBs = qurb;
|
---|
| 649 | }
|
---|
| 650 |
|
---|
| 651 | pDevice->pTailQURBs = qurb;
|
---|
| 652 |
|
---|
[51092] | 653 | releaseDevice(pDevice);
|
---|
[31891] | 654 |
|
---|
| 655 | *ppRemoteURB = qurb;
|
---|
| 656 |
|
---|
[51092] | 657 | pThis->VRDPServer()->SendUSBRequest(pDevice->u32ClientId, &parm, sizeof(parm));
|
---|
[31891] | 658 |
|
---|
| 659 | l_leave:
|
---|
| 660 | if (RT_FAILURE(rc))
|
---|
| 661 | {
|
---|
[51092] | 662 | qurbFree(qurb);
|
---|
[31891] | 663 | }
|
---|
| 664 |
|
---|
| 665 | return rc;
|
---|
| 666 | }
|
---|
| 667 |
|
---|
| 668 | /* The function checks the URB queue for completed URBs. Also if the client
|
---|
| 669 | * has requested URB polling, the function will send URB poll requests.
|
---|
| 670 | */
|
---|
[51092] | 671 | static DECLCALLBACK(int) iface_ReapURB(PREMOTEUSBDEVICE pDevice, uint32_t u32Millies, void **ppvURB,
|
---|
| 672 | uint32_t *pu32Len, uint32_t *pu32Err)
|
---|
[31891] | 673 | {
|
---|
| 674 | int rc = VINF_SUCCESS;
|
---|
| 675 |
|
---|
| 676 | LogFlow(("RemoteUSBBackend::iface_ReapURB %d ms\n", u32Millies));
|
---|
| 677 |
|
---|
| 678 | if (pDevice->fFailed)
|
---|
| 679 | {
|
---|
| 680 | return VERR_VUSB_DEVICE_NOT_ATTACHED;
|
---|
| 681 | }
|
---|
| 682 |
|
---|
| 683 | RemoteUSBBackend *pThis = pDevice->pOwner;
|
---|
| 684 |
|
---|
| 685 | /* Wait for transaction completion. */
|
---|
[51092] | 686 | uint64_t u64StartTime = RTTimeMilliTS();
|
---|
[31891] | 687 |
|
---|
[51092] | 688 | if (pThis->pollingEnabledURB())
|
---|
[31891] | 689 | {
|
---|
[33004] | 690 | VRDE_USB_REQ_REAP_URB_PARM parm;
|
---|
[31891] | 691 |
|
---|
[33004] | 692 | parm.code = VRDE_USB_REQ_REAP_URB;
|
---|
[31891] | 693 |
|
---|
[51092] | 694 | pThis->VRDPServer()->SendUSBRequest(pDevice->u32ClientId, &parm, sizeof(parm));
|
---|
[31891] | 695 | }
|
---|
| 696 |
|
---|
| 697 | REMOTEUSBQURB *qurb = NULL;
|
---|
| 698 |
|
---|
| 699 | for (;;)
|
---|
| 700 | {
|
---|
| 701 | uint32_t u32ClientId;
|
---|
| 702 |
|
---|
[49814] | 703 | if (ASMAtomicXchgBool(&pDevice->fWokenUp, false))
|
---|
| 704 | break;
|
---|
| 705 |
|
---|
[31891] | 706 | /* Scan queued URBs, look for completed. */
|
---|
[51092] | 707 | requestDevice(pDevice);
|
---|
[31891] | 708 |
|
---|
| 709 | u32ClientId = pDevice->u32ClientId;
|
---|
| 710 |
|
---|
| 711 | qurb = pDevice->pHeadQURBs;
|
---|
| 712 |
|
---|
| 713 | while (qurb)
|
---|
| 714 | {
|
---|
| 715 | if (qurb->fCompleted)
|
---|
| 716 | {
|
---|
| 717 | /* Remove this completed urb from the queue. */
|
---|
| 718 | if (qurb->prev)
|
---|
| 719 | {
|
---|
| 720 | qurb->prev->next = qurb->next;
|
---|
| 721 | }
|
---|
| 722 | else
|
---|
| 723 | {
|
---|
| 724 | pDevice->pHeadQURBs = qurb->next;
|
---|
| 725 | }
|
---|
| 726 |
|
---|
| 727 | if (qurb->next)
|
---|
| 728 | {
|
---|
| 729 | qurb->next->prev = qurb->prev;
|
---|
| 730 | }
|
---|
| 731 | else
|
---|
| 732 | {
|
---|
| 733 | pDevice->pTailQURBs = qurb->prev;
|
---|
| 734 | }
|
---|
| 735 |
|
---|
| 736 | qurb->next = NULL;
|
---|
| 737 | qurb->prev = NULL;
|
---|
| 738 |
|
---|
| 739 | break;
|
---|
| 740 | }
|
---|
| 741 |
|
---|
| 742 | qurb = qurb->next;
|
---|
| 743 | }
|
---|
| 744 |
|
---|
[51092] | 745 | releaseDevice(pDevice);
|
---|
[31891] | 746 |
|
---|
| 747 | if ( qurb
|
---|
| 748 | || !pDevice->pHeadQURBs
|
---|
| 749 | || u32Millies == 0
|
---|
| 750 | || pDevice->fFailed
|
---|
[51092] | 751 | || (RTTimeMilliTS() - u64StartTime >= (uint64_t)u32Millies))
|
---|
[31891] | 752 | {
|
---|
| 753 | /* Got an URB or do not have to wait for an URB. */
|
---|
| 754 | break;
|
---|
| 755 | }
|
---|
| 756 |
|
---|
| 757 | LogFlow(("RemoteUSBBackend::iface_ReapURB iteration.\n"));
|
---|
| 758 |
|
---|
[51092] | 759 | RTThreadSleep(10);
|
---|
[31891] | 760 |
|
---|
[51092] | 761 | if (pThis->pollingEnabledURB())
|
---|
[31891] | 762 | {
|
---|
[33004] | 763 | VRDE_USB_REQ_REAP_URB_PARM parm;
|
---|
[31891] | 764 |
|
---|
[33004] | 765 | parm.code = VRDE_USB_REQ_REAP_URB;
|
---|
[31891] | 766 |
|
---|
[51092] | 767 | pThis->VRDPServer()->SendUSBRequest(u32ClientId, &parm, sizeof(parm));
|
---|
[31891] | 768 | }
|
---|
| 769 | }
|
---|
| 770 |
|
---|
| 771 | LogFlow(("RemoteUSBBackend::iface_ReapURB completed in %lld ms, qurb = %p\n", RTTimeMilliTS () - u64StartTime, qurb));
|
---|
| 772 |
|
---|
| 773 | if (!qurb)
|
---|
| 774 | {
|
---|
| 775 | *ppvURB = NULL;
|
---|
| 776 | *pu32Len = 0;
|
---|
| 777 | *pu32Err = VUSBSTATUS_OK;
|
---|
| 778 | }
|
---|
| 779 | else
|
---|
| 780 | {
|
---|
| 781 | *ppvURB = qurb->pvURB;
|
---|
| 782 | *pu32Len = qurb->u32Len;
|
---|
| 783 | *pu32Err = qurb->u32Err;
|
---|
| 784 |
|
---|
| 785 | #ifdef LOG_ENABLED
|
---|
| 786 | Log(("URB len = %d, data = %p\n", qurb->u32Len, qurb->pvURB));
|
---|
| 787 | if (qurb->u32Len)
|
---|
| 788 | {
|
---|
| 789 | Log(("Received URB content:\n%.*Rhxd\n", qurb->u32Len, qurb->pvData));
|
---|
| 790 | }
|
---|
| 791 | #endif
|
---|
| 792 |
|
---|
[51092] | 793 | qurbFree(qurb);
|
---|
[31891] | 794 | }
|
---|
| 795 |
|
---|
| 796 | return rc;
|
---|
| 797 | }
|
---|
| 798 |
|
---|
[51092] | 799 | static DECLCALLBACK(int) iface_Wakeup(PREMOTEUSBDEVICE pDevice)
|
---|
[49814] | 800 | {
|
---|
| 801 | ASMAtomicXchgBool(&pDevice->fWokenUp, true);
|
---|
| 802 | return VINF_SUCCESS;
|
---|
| 803 | }
|
---|
| 804 |
|
---|
[51092] | 805 | void RemoteUSBBackend::AddRef(void)
|
---|
[31891] | 806 | {
|
---|
| 807 | cRefs++;
|
---|
| 808 | }
|
---|
| 809 |
|
---|
[51092] | 810 | void RemoteUSBBackend::Release(void)
|
---|
[31891] | 811 | {
|
---|
| 812 | cRefs--;
|
---|
| 813 |
|
---|
| 814 | if (cRefs <= 0)
|
---|
| 815 | {
|
---|
| 816 | delete this;
|
---|
| 817 | }
|
---|
| 818 | }
|
---|
| 819 |
|
---|
[51092] | 820 | void RemoteUSBBackend::PollRemoteDevices(void)
|
---|
[31891] | 821 | {
|
---|
| 822 | if ( mfWillBeDeleted
|
---|
| 823 | && menmPollRemoteDevicesStatus != PollRemoteDevicesStatus_Dereferenced)
|
---|
| 824 | {
|
---|
| 825 | /* Unmount all remote USB devices. */
|
---|
[51612] | 826 | mConsole->i_processRemoteUSBDevices(mu32ClientId, NULL, 0, false);
|
---|
[31891] | 827 |
|
---|
| 828 | menmPollRemoteDevicesStatus = PollRemoteDevicesStatus_Dereferenced;
|
---|
| 829 |
|
---|
[51092] | 830 | Release();
|
---|
[31891] | 831 |
|
---|
| 832 | return;
|
---|
| 833 | }
|
---|
| 834 |
|
---|
[51092] | 835 | switch(menmPollRemoteDevicesStatus)
|
---|
[31891] | 836 | {
|
---|
| 837 | case PollRemoteDevicesStatus_Negotiate:
|
---|
| 838 | {
|
---|
[33004] | 839 | VRDEUSBREQNEGOTIATEPARM parm;
|
---|
[31891] | 840 |
|
---|
[33004] | 841 | parm.code = VRDE_USB_REQ_NEGOTIATE;
|
---|
| 842 | parm.version = VRDE_USB_VERSION;
|
---|
[38911] | 843 | /* VRDE_USB_VERSION_3: support VRDE_USB_REQ_DEVICE_LIST_EXT_RET. */
|
---|
| 844 | parm.flags = VRDE_USB_SERVER_CAPS_PORT_VERSION;
|
---|
[31891] | 845 |
|
---|
[51092] | 846 | mServer->SendUSBRequest(mu32ClientId, &parm, sizeof(parm));
|
---|
[31891] | 847 |
|
---|
| 848 | /* Reference the object. When the client disconnects and
|
---|
| 849 | * the backend is about to be deleted, the method must be called
|
---|
| 850 | * to disconnect the USB devices (as stated above).
|
---|
| 851 | */
|
---|
[51092] | 852 | AddRef();
|
---|
[31891] | 853 |
|
---|
[33540] | 854 | /* Goto the disabled state. When a response will be received
|
---|
[31891] | 855 | * the state will be changed to the SendRequest.
|
---|
| 856 | */
|
---|
| 857 | menmPollRemoteDevicesStatus = PollRemoteDevicesStatus_WaitNegotiateResponse;
|
---|
| 858 | } break;
|
---|
| 859 |
|
---|
| 860 | case PollRemoteDevicesStatus_WaitNegotiateResponse:
|
---|
| 861 | {
|
---|
| 862 | LogFlow(("USB::PollRemoteDevices: WaitNegotiateResponse\n"));
|
---|
| 863 | /* Do nothing. */
|
---|
| 864 | } break;
|
---|
| 865 |
|
---|
| 866 | case PollRemoteDevicesStatus_SendRequest:
|
---|
| 867 | {
|
---|
| 868 | LogFlow(("USB::PollRemoteDevices: SendRequest\n"));
|
---|
| 869 |
|
---|
| 870 | /* Send a request for device list. */
|
---|
[33004] | 871 | VRDE_USB_REQ_DEVICE_LIST_PARM parm;
|
---|
[31891] | 872 |
|
---|
[33004] | 873 | parm.code = VRDE_USB_REQ_DEVICE_LIST;
|
---|
[31891] | 874 |
|
---|
[51092] | 875 | mServer->SendUSBRequest(mu32ClientId, &parm, sizeof(parm));
|
---|
[31891] | 876 |
|
---|
| 877 | menmPollRemoteDevicesStatus = PollRemoteDevicesStatus_WaitResponse;
|
---|
| 878 | } break;
|
---|
| 879 |
|
---|
| 880 | case PollRemoteDevicesStatus_WaitResponse:
|
---|
| 881 | {
|
---|
| 882 | LogFlow(("USB::PollRemoteDevices: WaitResponse\n"));
|
---|
| 883 |
|
---|
| 884 | if (mfHasDeviceList)
|
---|
| 885 | {
|
---|
[51612] | 886 | mConsole->i_processRemoteUSBDevices(mu32ClientId, (VRDEUSBDEVICEDESC *)mpvDeviceList, mcbDeviceList, mfDescExt);
|
---|
[31891] | 887 | LogFlow(("USB::PollRemoteDevices: WaitResponse after process\n"));
|
---|
| 888 |
|
---|
| 889 | menmPollRemoteDevicesStatus = PollRemoteDevicesStatus_SendRequest;
|
---|
| 890 |
|
---|
| 891 | mfHasDeviceList = false;
|
---|
| 892 | }
|
---|
| 893 | } break;
|
---|
| 894 |
|
---|
| 895 | case PollRemoteDevicesStatus_Dereferenced:
|
---|
| 896 | {
|
---|
| 897 | LogFlow(("USB::PollRemoteDevices: Dereferenced\n"));
|
---|
| 898 | /* Do nothing. */
|
---|
| 899 | } break;
|
---|
| 900 |
|
---|
| 901 | default:
|
---|
| 902 | {
|
---|
[51092] | 903 | AssertFailed();
|
---|
[31891] | 904 | } break;
|
---|
| 905 | }
|
---|
| 906 | }
|
---|
| 907 |
|
---|
[51092] | 908 | void RemoteUSBBackend::NotifyDelete(void)
|
---|
[31891] | 909 | {
|
---|
| 910 | mfWillBeDeleted = true;
|
---|
| 911 | }
|
---|
| 912 |
|
---|
| 913 | /*
|
---|
| 914 | * The backend maintains a list of UUIDs of devices
|
---|
| 915 | * which are managed by the backend.
|
---|
| 916 | */
|
---|
[51092] | 917 | bool RemoteUSBBackend::addUUID(const Guid *pUuid)
|
---|
[31891] | 918 | {
|
---|
| 919 | unsigned i;
|
---|
| 920 | for (i = 0; i < RT_ELEMENTS(aGuids); i++)
|
---|
| 921 | {
|
---|
[44039] | 922 | if (aGuids[i].isZero())
|
---|
[31891] | 923 | {
|
---|
| 924 | aGuids[i] = *pUuid;
|
---|
| 925 | return true;
|
---|
| 926 | }
|
---|
| 927 | }
|
---|
| 928 |
|
---|
| 929 | return false;
|
---|
| 930 | }
|
---|
| 931 |
|
---|
[51092] | 932 | bool RemoteUSBBackend::findUUID(const Guid *pUuid)
|
---|
[31891] | 933 | {
|
---|
| 934 | unsigned i;
|
---|
| 935 | for (i = 0; i < RT_ELEMENTS(aGuids); i++)
|
---|
| 936 | {
|
---|
| 937 | if (aGuids[i] == *pUuid)
|
---|
| 938 | {
|
---|
| 939 | return true;
|
---|
| 940 | }
|
---|
| 941 | }
|
---|
| 942 |
|
---|
| 943 | return false;
|
---|
| 944 | }
|
---|
| 945 |
|
---|
[51092] | 946 | void RemoteUSBBackend::removeUUID(const Guid *pUuid)
|
---|
[31891] | 947 | {
|
---|
| 948 | unsigned i;
|
---|
| 949 | for (i = 0; i < RT_ELEMENTS(aGuids); i++)
|
---|
| 950 | {
|
---|
| 951 | if (aGuids[i] == *pUuid)
|
---|
| 952 | {
|
---|
[51092] | 953 | aGuids[i].clear();
|
---|
[31891] | 954 | break;
|
---|
| 955 | }
|
---|
| 956 | }
|
---|
| 957 | }
|
---|
| 958 |
|
---|
| 959 | RemoteUSBBackend::RemoteUSBBackend(Console *console, ConsoleVRDPServer *server, uint32_t u32ClientId)
|
---|
| 960 | :
|
---|
[51092] | 961 | mConsole(console),
|
---|
| 962 | mServer(server),
|
---|
| 963 | cRefs(0),
|
---|
| 964 | mu32ClientId(u32ClientId),
|
---|
| 965 | mfHasDeviceList(false),
|
---|
| 966 | mpvDeviceList(NULL),
|
---|
| 967 | mcbDeviceList(0),
|
---|
| 968 | menmPollRemoteDevicesStatus(PollRemoteDevicesStatus_Negotiate),
|
---|
| 969 | mfPollURB(true),
|
---|
| 970 | mpDevices(NULL),
|
---|
| 971 | mfWillBeDeleted(false),
|
---|
| 972 | mClientVersion(0), /* VRDE_USB_VERSION_2: the client version. */
|
---|
| 973 | mfDescExt(false) /* VRDE_USB_VERSION_3: VRDE_USB_REQ_DEVICE_LIST_EXT_RET. */
|
---|
[31891] | 974 | {
|
---|
| 975 | Assert(console);
|
---|
| 976 | Assert(server);
|
---|
| 977 |
|
---|
[51092] | 978 | int rc = RTCritSectInit(&mCritsect);
|
---|
[31891] | 979 |
|
---|
| 980 | if (RT_FAILURE(rc))
|
---|
| 981 | {
|
---|
[51092] | 982 | AssertFailed();
|
---|
[47117] | 983 | RT_ZERO(mCritsect);
|
---|
[31891] | 984 | }
|
---|
| 985 |
|
---|
| 986 | mCallback.pInstance = (PREMOTEUSBBACKEND)this;
|
---|
| 987 | mCallback.pfnOpen = iface_Open;
|
---|
| 988 | mCallback.pfnClose = iface_Close;
|
---|
| 989 | mCallback.pfnReset = iface_Reset;
|
---|
| 990 | mCallback.pfnSetConfig = iface_SetConfig;
|
---|
| 991 | mCallback.pfnClaimInterface = iface_ClaimInterface;
|
---|
| 992 | mCallback.pfnReleaseInterface = iface_ReleaseInterface;
|
---|
| 993 | mCallback.pfnInterfaceSetting = iface_InterfaceSetting;
|
---|
| 994 | mCallback.pfnQueueURB = iface_QueueURB;
|
---|
| 995 | mCallback.pfnReapURB = iface_ReapURB;
|
---|
| 996 | mCallback.pfnClearHaltedEP = iface_ClearHaltedEP;
|
---|
| 997 | mCallback.pfnCancelURB = iface_CancelURB;
|
---|
[49814] | 998 | mCallback.pfnWakeup = iface_Wakeup;
|
---|
[31891] | 999 | }
|
---|
| 1000 |
|
---|
| 1001 | RemoteUSBBackend::~RemoteUSBBackend()
|
---|
| 1002 | {
|
---|
| 1003 | Assert(cRefs == 0);
|
---|
| 1004 |
|
---|
[51092] | 1005 | if (RTCritSectIsInitialized(&mCritsect))
|
---|
[31891] | 1006 | {
|
---|
[51092] | 1007 | RTCritSectDelete(&mCritsect);
|
---|
[31891] | 1008 | }
|
---|
| 1009 |
|
---|
[51092] | 1010 | RTMemFree(mpvDeviceList);
|
---|
[31891] | 1011 |
|
---|
[51092] | 1012 | mServer->usbBackendRemoveFromList(this);
|
---|
[31891] | 1013 | }
|
---|
| 1014 |
|
---|
[51092] | 1015 | int RemoteUSBBackend::negotiateResponse(const VRDEUSBREQNEGOTIATERET *pret, uint32_t cbRet)
|
---|
[31891] | 1016 | {
|
---|
| 1017 | int rc = VINF_SUCCESS;
|
---|
| 1018 |
|
---|
| 1019 | Log(("RemoteUSBBackend::negotiateResponse: flags = %02X.\n", pret->flags));
|
---|
| 1020 |
|
---|
| 1021 | LogRel(("Remote USB: Received negotiate response. Flags 0x%02X.\n",
|
---|
| 1022 | pret->flags));
|
---|
| 1023 |
|
---|
[33004] | 1024 | if (pret->flags & VRDE_USB_CAPS_FLAG_POLL)
|
---|
[31891] | 1025 | {
|
---|
| 1026 | Log(("RemoteUSBBackend::negotiateResponse: client requested URB polling.\n"));
|
---|
| 1027 | mfPollURB = true;
|
---|
| 1028 | }
|
---|
| 1029 | else
|
---|
| 1030 | {
|
---|
| 1031 | mfPollURB = false;
|
---|
| 1032 | }
|
---|
| 1033 |
|
---|
[33004] | 1034 | /* VRDE_USB_VERSION_2: check the client version. */
|
---|
| 1035 | if (pret->flags & VRDE_USB_CAPS2_FLAG_VERSION)
|
---|
[31891] | 1036 | {
|
---|
| 1037 | /* This could be a client version > 1. */
|
---|
[51092] | 1038 | if (cbRet >= sizeof(VRDEUSBREQNEGOTIATERET_2))
|
---|
[31891] | 1039 | {
|
---|
[33004] | 1040 | VRDEUSBREQNEGOTIATERET_2 *pret2 = (VRDEUSBREQNEGOTIATERET_2 *)pret;
|
---|
[31891] | 1041 |
|
---|
[33004] | 1042 | if (pret2->u32Version <= VRDE_USB_VERSION)
|
---|
[31891] | 1043 | {
|
---|
| 1044 | /* This is OK. The client wants a version supported by the server. */
|
---|
| 1045 | mClientVersion = pret2->u32Version;
|
---|
| 1046 | }
|
---|
| 1047 | else
|
---|
| 1048 | {
|
---|
| 1049 | LogRel(("VRDP: ERROR: unsupported remote USB protocol client version %d.\n", pret2->u32Version));
|
---|
| 1050 | rc = VERR_NOT_SUPPORTED;
|
---|
| 1051 | }
|
---|
| 1052 | }
|
---|
| 1053 | else
|
---|
| 1054 | {
|
---|
| 1055 | LogRel(("VRDP: ERROR: invalid remote USB negotiate request packet size %d.\n", cbRet));
|
---|
| 1056 | rc = VERR_NOT_SUPPORTED;
|
---|
| 1057 | }
|
---|
| 1058 | }
|
---|
| 1059 | else
|
---|
| 1060 | {
|
---|
| 1061 | /* This is a client version 1. */
|
---|
[33004] | 1062 | mClientVersion = VRDE_USB_VERSION_1;
|
---|
[31891] | 1063 | }
|
---|
| 1064 |
|
---|
| 1065 | if (RT_SUCCESS(rc))
|
---|
| 1066 | {
|
---|
| 1067 | LogRel(("VRDP: remote USB protocol version %d.\n", mClientVersion));
|
---|
| 1068 |
|
---|
[38911] | 1069 | /* VRDE_USB_VERSION_3: check the client capabilities: VRDE_USB_CLIENT_CAPS_*. */
|
---|
| 1070 | if (mClientVersion == VRDE_USB_VERSION_3)
|
---|
| 1071 | {
|
---|
[51092] | 1072 | if (cbRet >= sizeof(VRDEUSBREQNEGOTIATERET_3))
|
---|
[38911] | 1073 | {
|
---|
| 1074 | VRDEUSBREQNEGOTIATERET_3 *pret3 = (VRDEUSBREQNEGOTIATERET_3 *)pret;
|
---|
| 1075 |
|
---|
| 1076 | mfDescExt = (pret3->u32Flags & VRDE_USB_CLIENT_CAPS_PORT_VERSION) != 0;
|
---|
| 1077 | }
|
---|
| 1078 | else
|
---|
| 1079 | {
|
---|
| 1080 | LogRel(("VRDP: ERROR: invalid remote USB negotiate request packet size %d.\n", cbRet));
|
---|
| 1081 | rc = VERR_NOT_SUPPORTED;
|
---|
| 1082 | }
|
---|
| 1083 | }
|
---|
| 1084 |
|
---|
[31891] | 1085 | menmPollRemoteDevicesStatus = PollRemoteDevicesStatus_SendRequest;
|
---|
| 1086 | }
|
---|
| 1087 |
|
---|
| 1088 | return rc;
|
---|
| 1089 | }
|
---|
| 1090 |
|
---|
[51092] | 1091 | int RemoteUSBBackend::saveDeviceList(const void *pvList, uint32_t cbList)
|
---|
[31891] | 1092 | {
|
---|
| 1093 | Log(("RemoteUSBBackend::saveDeviceList: pvList = %p, cbList = %d\n", pvList, cbList));
|
---|
| 1094 |
|
---|
| 1095 | if (!mfHasDeviceList)
|
---|
| 1096 | {
|
---|
[51092] | 1097 | RTMemFree(mpvDeviceList);
|
---|
[31891] | 1098 | mpvDeviceList = NULL;
|
---|
| 1099 |
|
---|
| 1100 | mcbDeviceList = cbList;
|
---|
| 1101 |
|
---|
| 1102 | if (cbList > 0)
|
---|
| 1103 | {
|
---|
[51092] | 1104 | mpvDeviceList = RTMemAlloc(cbList);
|
---|
| 1105 | memcpy(mpvDeviceList, pvList, cbList);
|
---|
[31891] | 1106 | }
|
---|
| 1107 |
|
---|
| 1108 | mfHasDeviceList = true;
|
---|
| 1109 | }
|
---|
| 1110 |
|
---|
| 1111 | return VINF_SUCCESS;
|
---|
| 1112 | }
|
---|
| 1113 |
|
---|
[51092] | 1114 | void RemoteUSBBackend::request(void)
|
---|
[31891] | 1115 | {
|
---|
| 1116 | int rc = RTCritSectEnter(&mCritsect);
|
---|
| 1117 | AssertRC(rc);
|
---|
| 1118 | }
|
---|
| 1119 |
|
---|
[51092] | 1120 | void RemoteUSBBackend::release(void)
|
---|
[31891] | 1121 | {
|
---|
| 1122 | RTCritSectLeave(&mCritsect);
|
---|
| 1123 | }
|
---|
| 1124 |
|
---|
[51092] | 1125 | PREMOTEUSBDEVICE RemoteUSBBackend::deviceFromId(VRDEUSBDEVID id)
|
---|
[31891] | 1126 | {
|
---|
[51092] | 1127 | request();
|
---|
[31891] | 1128 |
|
---|
| 1129 | REMOTEUSBDEVICE *pDevice = mpDevices;
|
---|
| 1130 |
|
---|
| 1131 | while (pDevice && pDevice->id != id)
|
---|
| 1132 | {
|
---|
| 1133 | pDevice = pDevice->next;
|
---|
| 1134 | }
|
---|
| 1135 |
|
---|
[51092] | 1136 | release();
|
---|
[31891] | 1137 |
|
---|
| 1138 | return pDevice;
|
---|
| 1139 | }
|
---|
| 1140 |
|
---|
[51092] | 1141 | void RemoteUSBBackend::addDevice(PREMOTEUSBDEVICE pDevice)
|
---|
[31891] | 1142 | {
|
---|
[51092] | 1143 | request();
|
---|
[31891] | 1144 |
|
---|
| 1145 | pDevice->next = mpDevices;
|
---|
| 1146 |
|
---|
| 1147 | if (mpDevices)
|
---|
| 1148 | {
|
---|
| 1149 | mpDevices->prev = pDevice;
|
---|
| 1150 | }
|
---|
| 1151 |
|
---|
| 1152 | mpDevices = pDevice;
|
---|
| 1153 |
|
---|
[51092] | 1154 | release();
|
---|
[31891] | 1155 | }
|
---|
| 1156 |
|
---|
[51092] | 1157 | void RemoteUSBBackend::removeDevice(PREMOTEUSBDEVICE pDevice)
|
---|
[31891] | 1158 | {
|
---|
[51092] | 1159 | request();
|
---|
[31891] | 1160 |
|
---|
| 1161 | if (pDevice->prev)
|
---|
| 1162 | {
|
---|
| 1163 | pDevice->prev->next = pDevice->next;
|
---|
| 1164 | }
|
---|
| 1165 | else
|
---|
| 1166 | {
|
---|
| 1167 | mpDevices = pDevice->next;
|
---|
| 1168 | }
|
---|
| 1169 |
|
---|
| 1170 | if (pDevice->next)
|
---|
| 1171 | {
|
---|
| 1172 | pDevice->next->prev = pDevice->prev;
|
---|
| 1173 | }
|
---|
| 1174 |
|
---|
[51092] | 1175 | release();
|
---|
[31891] | 1176 | }
|
---|
| 1177 |
|
---|
[51092] | 1178 | int RemoteUSBBackend::reapURB(const void *pvBody, uint32_t cbBody)
|
---|
[31891] | 1179 | {
|
---|
| 1180 | int rc = VINF_SUCCESS;
|
---|
| 1181 |
|
---|
| 1182 | LogFlow(("RemoteUSBBackend::reapURB: pvBody = %p, cbBody = %d\n", pvBody, cbBody));
|
---|
| 1183 |
|
---|
[33004] | 1184 | VRDEUSBREQREAPURBBODY *pBody = (VRDEUSBREQREAPURBBODY *)pvBody;
|
---|
[31891] | 1185 |
|
---|
[51092] | 1186 | while (cbBody >= sizeof(VRDEUSBREQREAPURBBODY))
|
---|
[31891] | 1187 | {
|
---|
| 1188 | Log(("RemoteUSBBackend::reapURB: id = %d, flags = %02X, error = %d, handle %d, len = %d.\n",
|
---|
| 1189 | pBody->id, pBody->flags, pBody->error, pBody->handle, pBody->len));
|
---|
| 1190 |
|
---|
| 1191 | uint8_t fu8ReapValidFlags;
|
---|
| 1192 |
|
---|
[33004] | 1193 | if (mClientVersion == VRDE_USB_VERSION_1 || mClientVersion == VRDE_USB_VERSION_2)
|
---|
[31891] | 1194 | {
|
---|
[33004] | 1195 | fu8ReapValidFlags = VRDE_USB_REAP_VALID_FLAGS;
|
---|
[31891] | 1196 | }
|
---|
| 1197 | else
|
---|
| 1198 | {
|
---|
[33004] | 1199 | fu8ReapValidFlags = VRDE_USB_REAP_VALID_FLAGS_3;
|
---|
[31891] | 1200 | }
|
---|
| 1201 |
|
---|
| 1202 | /* Verify client's data. */
|
---|
| 1203 | if ( (pBody->flags & ~fu8ReapValidFlags) != 0
|
---|
[51092] | 1204 | || sizeof(VRDEUSBREQREAPURBBODY) > cbBody
|
---|
[31891] | 1205 | || pBody->handle == 0)
|
---|
| 1206 | {
|
---|
| 1207 | LogFlow(("RemoteUSBBackend::reapURB: WARNING: invalid reply data. Skipping the reply.\n"));
|
---|
| 1208 | rc = VERR_INVALID_PARAMETER;
|
---|
| 1209 | break;
|
---|
| 1210 | }
|
---|
| 1211 |
|
---|
[51092] | 1212 | PREMOTEUSBDEVICE pDevice = deviceFromId(pBody->id);
|
---|
[31891] | 1213 |
|
---|
| 1214 | if (!pDevice)
|
---|
| 1215 | {
|
---|
| 1216 | LogFlow(("RemoteUSBBackend::reapURB: WARNING: invalid device id. Skipping the reply.\n"));
|
---|
| 1217 | rc = VERR_INVALID_PARAMETER;
|
---|
| 1218 | break;
|
---|
| 1219 | }
|
---|
| 1220 |
|
---|
| 1221 | uint32_t cbBodyData = 0; /* Data contained in the URB body structure for input URBs. */
|
---|
| 1222 |
|
---|
[51092] | 1223 | requestDevice(pDevice);
|
---|
[31891] | 1224 |
|
---|
| 1225 | /* Search the queued URB for given handle. */
|
---|
| 1226 | REMOTEUSBQURB *qurb = pDevice->pHeadQURBs;
|
---|
| 1227 |
|
---|
| 1228 | while (qurb && qurb->u32Handle != pBody->handle)
|
---|
| 1229 | {
|
---|
| 1230 | LogFlow(("RemoteUSBBackend::reapURB: searching: %p handle = %d.\n", qurb, qurb->u32Handle));
|
---|
| 1231 | qurb = qurb->next;
|
---|
| 1232 | }
|
---|
| 1233 |
|
---|
| 1234 | if (!qurb)
|
---|
| 1235 | {
|
---|
| 1236 | LogFlow(("RemoteUSBBackend::reapURB: Queued URB not found, probably already canceled. Skipping the URB.\n"));
|
---|
| 1237 | }
|
---|
| 1238 | else
|
---|
| 1239 | {
|
---|
| 1240 | LogFlow(("RemoteUSBBackend::reapURB: qurb = %p\n", qurb));
|
---|
| 1241 |
|
---|
| 1242 | /* Update the URB error field. */
|
---|
[33004] | 1243 | if (mClientVersion == VRDE_USB_VERSION_1)
|
---|
[31891] | 1244 | {
|
---|
| 1245 | switch(pBody->error)
|
---|
| 1246 | {
|
---|
[33004] | 1247 | case VRDE_USB_XFER_OK: qurb->u32Err = VUSBSTATUS_OK; break;
|
---|
| 1248 | case VRDE_USB_XFER_STALL: qurb->u32Err = VUSBSTATUS_STALL; break;
|
---|
| 1249 | case VRDE_USB_XFER_DNR: qurb->u32Err = VUSBSTATUS_DNR; break;
|
---|
| 1250 | case VRDE_USB_XFER_CRC: qurb->u32Err = VUSBSTATUS_CRC; break;
|
---|
[31891] | 1251 | default: Log(("RemoteUSBBackend::reapURB: Invalid error %d\n", pBody->error));
|
---|
| 1252 | qurb->u32Err = VUSBSTATUS_DNR; break;
|
---|
| 1253 | }
|
---|
| 1254 | }
|
---|
[33004] | 1255 | else if ( mClientVersion == VRDE_USB_VERSION_2
|
---|
| 1256 | || mClientVersion == VRDE_USB_VERSION_3)
|
---|
[31891] | 1257 | {
|
---|
| 1258 | switch(pBody->error)
|
---|
| 1259 | {
|
---|
[33004] | 1260 | case VRDE_USB_XFER_OK: qurb->u32Err = VUSBSTATUS_OK; break;
|
---|
| 1261 | case VRDE_USB_XFER_STALL: qurb->u32Err = VUSBSTATUS_STALL; break;
|
---|
| 1262 | case VRDE_USB_XFER_DNR: qurb->u32Err = VUSBSTATUS_DNR; break;
|
---|
| 1263 | case VRDE_USB_XFER_CRC: qurb->u32Err = VUSBSTATUS_CRC; break;
|
---|
| 1264 | case VRDE_USB_XFER_DO: qurb->u32Err = VUSBSTATUS_DATA_OVERRUN; break;
|
---|
| 1265 | case VRDE_USB_XFER_DU: qurb->u32Err = VUSBSTATUS_DATA_UNDERRUN; break;
|
---|
[31891] | 1266 |
|
---|
| 1267 | /* Unmapped errors. */
|
---|
[33004] | 1268 | case VRDE_USB_XFER_BS:
|
---|
| 1269 | case VRDE_USB_XFER_DTM:
|
---|
| 1270 | case VRDE_USB_XFER_PCF:
|
---|
| 1271 | case VRDE_USB_XFER_UPID:
|
---|
| 1272 | case VRDE_USB_XFER_BO:
|
---|
| 1273 | case VRDE_USB_XFER_BU:
|
---|
| 1274 | case VRDE_USB_XFER_ERR:
|
---|
[31891] | 1275 | default: Log(("RemoteUSBBackend::reapURB: Invalid error %d\n", pBody->error));
|
---|
| 1276 | qurb->u32Err = VUSBSTATUS_DNR; break;
|
---|
| 1277 | }
|
---|
| 1278 | }
|
---|
| 1279 | else
|
---|
| 1280 | {
|
---|
| 1281 | qurb->u32Err = VUSBSTATUS_DNR;
|
---|
| 1282 | }
|
---|
| 1283 |
|
---|
| 1284 | /* Get the URB data. */
|
---|
| 1285 | bool fURBCompleted = true;
|
---|
| 1286 |
|
---|
| 1287 | if (qurb->fInput)
|
---|
| 1288 | {
|
---|
[33004] | 1289 | cbBodyData = pBody->len; /* VRDE_USB_DIRECTION_IN URBs include some data. */
|
---|
[31891] | 1290 | }
|
---|
| 1291 |
|
---|
| 1292 | if ( qurb->u32Err == VUSBSTATUS_OK
|
---|
| 1293 | && qurb->fInput)
|
---|
| 1294 | {
|
---|
| 1295 | LogFlow(("RemoteUSBBackend::reapURB: copying data %d bytes\n", pBody->len));
|
---|
| 1296 |
|
---|
| 1297 | uint32_t u32DataLen = qurb->u32TransferredLen + pBody->len;
|
---|
| 1298 |
|
---|
| 1299 | if (u32DataLen > qurb->u32Len)
|
---|
| 1300 | {
|
---|
| 1301 | /* Received more data than expected for this URB. If there more fragments follow,
|
---|
| 1302 | * they will be discarded because the URB handle will not be valid anymore.
|
---|
| 1303 | */
|
---|
| 1304 | qurb->u32Err = VUSBSTATUS_DNR;
|
---|
| 1305 | }
|
---|
| 1306 | else
|
---|
| 1307 | {
|
---|
| 1308 | memcpy ((uint8_t *)qurb->pvData + qurb->u32TransferredLen, &pBody[1], pBody->len);
|
---|
| 1309 | }
|
---|
| 1310 |
|
---|
| 1311 | if ( qurb->u32Err == VUSBSTATUS_OK
|
---|
[33004] | 1312 | && (pBody->flags & VRDE_USB_REAP_FLAG_FRAGMENT) != 0)
|
---|
[31891] | 1313 | {
|
---|
| 1314 | /* If the client sends fragmented packets, accumulate the URB data. */
|
---|
| 1315 | fURBCompleted = false;
|
---|
| 1316 | }
|
---|
| 1317 | }
|
---|
| 1318 |
|
---|
| 1319 | qurb->u32TransferredLen += pBody->len; /* Update the value for all URBs. */
|
---|
| 1320 |
|
---|
| 1321 | if (fURBCompleted)
|
---|
| 1322 | {
|
---|
[38986] | 1323 | /* Move the URB near the head of URB list, so that iface_ReapURB can
|
---|
| 1324 | * find it faster. Note that the order of completion must be preserved!
|
---|
| 1325 | */
|
---|
[31891] | 1326 | if (qurb->prev)
|
---|
| 1327 | {
|
---|
[38986] | 1328 | /* The URB is not in the head. Unlink it from its current position. */
|
---|
[31891] | 1329 | qurb->prev->next = qurb->next;
|
---|
| 1330 |
|
---|
| 1331 | if (qurb->next)
|
---|
| 1332 | {
|
---|
| 1333 | qurb->next->prev = qurb->prev;
|
---|
| 1334 | }
|
---|
| 1335 | else
|
---|
| 1336 | {
|
---|
| 1337 | pDevice->pTailQURBs = qurb->prev;
|
---|
| 1338 | }
|
---|
| 1339 |
|
---|
[38986] | 1340 | /* And insert it to its new place. */
|
---|
| 1341 | if (pDevice->pHeadQURBs->fCompleted)
|
---|
| 1342 | {
|
---|
[48955] | 1343 | /* At least one other completed URB; insert after the
|
---|
[38986] | 1344 | * last completed URB.
|
---|
| 1345 | */
|
---|
| 1346 | REMOTEUSBQURB *prev_qurb = pDevice->pHeadQURBs;
|
---|
| 1347 | while (prev_qurb->next && prev_qurb->next->fCompleted)
|
---|
| 1348 | prev_qurb = prev_qurb->next;
|
---|
[31891] | 1349 |
|
---|
[38986] | 1350 | qurb->next = prev_qurb->next;
|
---|
| 1351 | qurb->prev = prev_qurb;
|
---|
| 1352 |
|
---|
| 1353 | if (prev_qurb->next)
|
---|
| 1354 | prev_qurb->next->prev = qurb;
|
---|
| 1355 | else
|
---|
| 1356 | pDevice->pTailQURBs = qurb;
|
---|
| 1357 | prev_qurb->next = qurb;
|
---|
| 1358 | }
|
---|
| 1359 | else
|
---|
| 1360 | {
|
---|
| 1361 | /* No other completed URBs; insert at head. */
|
---|
| 1362 | qurb->next = pDevice->pHeadQURBs;
|
---|
| 1363 | qurb->prev = NULL;
|
---|
[48955] | 1364 |
|
---|
[38986] | 1365 | pDevice->pHeadQURBs->prev = qurb;
|
---|
| 1366 | pDevice->pHeadQURBs = qurb;
|
---|
| 1367 | }
|
---|
[31891] | 1368 | }
|
---|
| 1369 |
|
---|
| 1370 | qurb->u32Len = qurb->u32TransferredLen; /* Update the final length. */
|
---|
| 1371 | qurb->fCompleted = true;
|
---|
| 1372 | }
|
---|
| 1373 | }
|
---|
| 1374 |
|
---|
| 1375 | releaseDevice (pDevice);
|
---|
| 1376 |
|
---|
[33004] | 1377 | if (pBody->flags & VRDE_USB_REAP_FLAG_LAST)
|
---|
[31891] | 1378 | {
|
---|
| 1379 | break;
|
---|
| 1380 | }
|
---|
| 1381 |
|
---|
| 1382 | /* There is probably a further URB body. */
|
---|
[33004] | 1383 | uint32_t cbBodySize = sizeof (VRDEUSBREQREAPURBBODY) + cbBodyData;
|
---|
[31891] | 1384 |
|
---|
| 1385 | if (cbBodySize > cbBody)
|
---|
| 1386 | {
|
---|
| 1387 | rc = VERR_INVALID_PARAMETER;
|
---|
| 1388 | break;
|
---|
| 1389 | }
|
---|
| 1390 |
|
---|
[33004] | 1391 | pBody = (VRDEUSBREQREAPURBBODY *)((uint8_t *)pBody + cbBodySize);
|
---|
[31891] | 1392 | cbBody -= cbBodySize;
|
---|
| 1393 | }
|
---|
| 1394 |
|
---|
| 1395 | LogFlow(("RemoteUSBBackend::reapURB: returns %Rrc\n", rc));
|
---|
| 1396 |
|
---|
| 1397 | return rc;
|
---|
| 1398 | }
|
---|
| 1399 | /* vi: set tabstop=4 shiftwidth=4 expandtab: */
|
---|