1 | /* $Id: USBProxyBackendUsbIp.cpp 98288 2023-01-24 15:32:43Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * VirtualBox USB Proxy Backend, USB/IP.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2015-2023 Oracle and/or its affiliates.
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox base platform packages, as
|
---|
10 | * available from https://www.virtualbox.org.
|
---|
11 | *
|
---|
12 | * This program is free software; you can redistribute it and/or
|
---|
13 | * modify it under the terms of the GNU General Public License
|
---|
14 | * as published by the Free Software Foundation, in version 3 of the
|
---|
15 | * License.
|
---|
16 | *
|
---|
17 | * This program is distributed in the hope that it will be useful, but
|
---|
18 | * WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
19 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
---|
20 | * General Public License for more details.
|
---|
21 | *
|
---|
22 | * You should have received a copy of the GNU General Public License
|
---|
23 | * along with this program; if not, see <https://www.gnu.org/licenses>.
|
---|
24 | *
|
---|
25 | * SPDX-License-Identifier: GPL-3.0-only
|
---|
26 | */
|
---|
27 |
|
---|
28 |
|
---|
29 | /*********************************************************************************************************************************
|
---|
30 | * Header Files *
|
---|
31 | *********************************************************************************************************************************/
|
---|
32 | #define LOG_GROUP LOG_GROUP_MAIN_USBPROXYBACKEND
|
---|
33 | #include "USBProxyService.h"
|
---|
34 | #include "USBGetDevices.h"
|
---|
35 | #include "LoggingNew.h"
|
---|
36 |
|
---|
37 | #include <VBox/usb.h>
|
---|
38 | #include <VBox/usblib.h>
|
---|
39 | #include <VBox/err.h>
|
---|
40 |
|
---|
41 | #include <iprt/string.h>
|
---|
42 | #include <iprt/alloc.h>
|
---|
43 | #include <iprt/assert.h>
|
---|
44 | #include <iprt/ctype.h>
|
---|
45 | #include <iprt/tcp.h>
|
---|
46 | #include <iprt/env.h>
|
---|
47 | #include <iprt/err.h>
|
---|
48 | #include <iprt/mem.h>
|
---|
49 | #include <iprt/pipe.h>
|
---|
50 | #include <iprt/asm.h>
|
---|
51 | #include <iprt/cdefs.h>
|
---|
52 | #include <iprt/time.h>
|
---|
53 |
|
---|
54 | /** The USB/IP default port to connect to. */
|
---|
55 | #define USBIP_PORT_DEFAULT 3240
|
---|
56 | /** The USB version number used for the protocol. */
|
---|
57 | #define USBIP_VERSION UINT16_C(0x0111)
|
---|
58 | /** Request indicator in the command code. */
|
---|
59 | #define USBIP_INDICATOR_REQ RT_BIT(15)
|
---|
60 |
|
---|
61 | /** Command/Reply code for OP_REQ/RET_DEVLIST. */
|
---|
62 | #define USBIP_REQ_RET_DEVLIST UINT16_C(5)
|
---|
63 |
|
---|
64 | /** @todo Duplicate code in USBProxyDevice-usbip.cpp */
|
---|
65 | /**
|
---|
66 | * Exported device entry in the OP_RET_DEVLIST reply.
|
---|
67 | */
|
---|
68 | #pragma pack(1)
|
---|
69 | typedef struct UsbIpExportedDevice
|
---|
70 | {
|
---|
71 | /** Path of the device, zero terminated string. */
|
---|
72 | char szPath[256];
|
---|
73 | /** Bus ID of the exported device, zero terminated string. */
|
---|
74 | char szBusId[32];
|
---|
75 | /** Bus number. */
|
---|
76 | uint32_t u32BusNum;
|
---|
77 | /** Device number. */
|
---|
78 | uint32_t u32DevNum;
|
---|
79 | /** Speed indicator of the device. */
|
---|
80 | uint32_t u32Speed;
|
---|
81 | /** Vendor ID of the device. */
|
---|
82 | uint16_t u16VendorId;
|
---|
83 | /** Product ID of the device. */
|
---|
84 | uint16_t u16ProductId;
|
---|
85 | /** Device release number. */
|
---|
86 | uint16_t u16BcdDevice;
|
---|
87 | /** Device class. */
|
---|
88 | uint8_t bDeviceClass;
|
---|
89 | /** Device Subclass. */
|
---|
90 | uint8_t bDeviceSubClass;
|
---|
91 | /** Device protocol. */
|
---|
92 | uint8_t bDeviceProtocol;
|
---|
93 | /** Configuration value. */
|
---|
94 | uint8_t bConfigurationValue;
|
---|
95 | /** Current configuration value of the device. */
|
---|
96 | uint8_t bNumConfigurations;
|
---|
97 | /** Number of interfaces for the device. */
|
---|
98 | uint8_t bNumInterfaces;
|
---|
99 | } UsbIpExportedDevice;
|
---|
100 | /** Pointer to a exported device entry. */
|
---|
101 | typedef UsbIpExportedDevice *PUsbIpExportedDevice;
|
---|
102 | #pragma pack()
|
---|
103 | AssertCompileSize(UsbIpExportedDevice, 312);
|
---|
104 |
|
---|
105 | /**
|
---|
106 | * Interface descriptor entry for an exported device.
|
---|
107 | */
|
---|
108 | #pragma pack(1)
|
---|
109 | typedef struct UsbIpDeviceInterface
|
---|
110 | {
|
---|
111 | /** Intefrace class. */
|
---|
112 | uint8_t bInterfaceClass;
|
---|
113 | /** Interface sub class. */
|
---|
114 | uint8_t bInterfaceSubClass;
|
---|
115 | /** Interface protocol identifier. */
|
---|
116 | uint8_t bInterfaceProtocol;
|
---|
117 | /** Padding byte for alignment. */
|
---|
118 | uint8_t bPadding;
|
---|
119 | } UsbIpDeviceInterface;
|
---|
120 | /** Pointer to an interface descriptor entry. */
|
---|
121 | typedef UsbIpDeviceInterface *PUsbIpDeviceInterface;
|
---|
122 | #pragma pack()
|
---|
123 |
|
---|
124 | /**
|
---|
125 | * USB/IP device list request.
|
---|
126 | */
|
---|
127 | #pragma pack(1)
|
---|
128 | typedef struct UsbIpReqDevList
|
---|
129 | {
|
---|
130 | /** Protocol version number. */
|
---|
131 | uint16_t u16Version;
|
---|
132 | /** Command code. */
|
---|
133 | uint16_t u16Cmd;
|
---|
134 | /** Status field, unused. */
|
---|
135 | int32_t i32Status;
|
---|
136 | } UsbIpReqDevList;
|
---|
137 | /** Pointer to a device list request. */
|
---|
138 | typedef UsbIpReqDevList *PUsbIpReqDevList;
|
---|
139 | #pragma pack()
|
---|
140 |
|
---|
141 | /**
|
---|
142 | * USB/IP Import reply.
|
---|
143 | *
|
---|
144 | * This is only the header, for successful
|
---|
145 | * requests the device details are sent to as
|
---|
146 | * defined in UsbIpExportedDevice.
|
---|
147 | */
|
---|
148 | #pragma pack(1)
|
---|
149 | typedef struct UsbIpRetDevList
|
---|
150 | {
|
---|
151 | /** Protocol version number. */
|
---|
152 | uint16_t u16Version;
|
---|
153 | /** Command code. */
|
---|
154 | uint16_t u16Cmd;
|
---|
155 | /** Status field, unused. */
|
---|
156 | int32_t i32Status;
|
---|
157 | /** Number of exported devices. */
|
---|
158 | uint32_t u32DevicesExported;
|
---|
159 | } UsbIpRetDevList;
|
---|
160 | /** Pointer to a import reply. */
|
---|
161 | typedef UsbIpRetDevList *PUsbIpRetDevList;
|
---|
162 | #pragma pack()
|
---|
163 |
|
---|
164 | /** Pollset id of the socket. */
|
---|
165 | #define USBIP_POLL_ID_SOCKET 0
|
---|
166 | /** Pollset id of the pipe. */
|
---|
167 | #define USBIP_POLL_ID_PIPE 1
|
---|
168 |
|
---|
169 | /** @name USB/IP error codes.
|
---|
170 | * @{ */
|
---|
171 | /** Success indicator. */
|
---|
172 | #define USBIP_STATUS_SUCCESS INT32_C(0)
|
---|
173 | /** @} */
|
---|
174 |
|
---|
175 | /** @name USB/IP device speeds.
|
---|
176 | * @{ */
|
---|
177 | /** Unknown speed. */
|
---|
178 | #define USBIP_SPEED_UNKNOWN 0
|
---|
179 | /** Low (1.0) speed. */
|
---|
180 | #define USBIP_SPEED_LOW 1
|
---|
181 | /** Full (1.1) speed. */
|
---|
182 | #define USBIP_SPEED_FULL 2
|
---|
183 | /** High (2.0) speed. */
|
---|
184 | #define USBIP_SPEED_HIGH 3
|
---|
185 | /** Variable (CWUSB) speed. */
|
---|
186 | #define USBIP_SPEED_WIRELESS 4
|
---|
187 | /** Super (3.0) speed. */
|
---|
188 | #define USBIP_SPEED_SUPER 5
|
---|
189 | /** @} */
|
---|
190 |
|
---|
191 | /**
|
---|
192 | * Private USB/IP proxy backend data.
|
---|
193 | */
|
---|
194 | struct USBProxyBackendUsbIp::Data
|
---|
195 | {
|
---|
196 | Data()
|
---|
197 | : hSocket(NIL_RTSOCKET),
|
---|
198 | hWakeupPipeR(NIL_RTPIPE),
|
---|
199 | hWakeupPipeW(NIL_RTPIPE),
|
---|
200 | hPollSet(NIL_RTPOLLSET),
|
---|
201 | uPort(USBIP_PORT_DEFAULT),
|
---|
202 | pszHost(NULL),
|
---|
203 | hMtxDevices(NIL_RTSEMFASTMUTEX),
|
---|
204 | cUsbDevicesCur(0),
|
---|
205 | pUsbDevicesCur(NULL),
|
---|
206 | enmRecvState(kUsbIpRecvState_Invalid),
|
---|
207 | cbResidualRecv(0),
|
---|
208 | pbRecvBuf(NULL),
|
---|
209 | cDevicesLeft(0),
|
---|
210 | pHead(NULL),
|
---|
211 | ppNext(&pHead)
|
---|
212 | { }
|
---|
213 |
|
---|
214 | /** Socket handle to the server. */
|
---|
215 | RTSOCKET hSocket;
|
---|
216 | /** Pipe used to interrupt wait(), the read end. */
|
---|
217 | RTPIPE hWakeupPipeR;
|
---|
218 | /** Pipe used to interrupt wait(), the write end. */
|
---|
219 | RTPIPE hWakeupPipeW;
|
---|
220 | /** Pollset for the socket and wakeup pipe. */
|
---|
221 | RTPOLLSET hPollSet;
|
---|
222 | /** Port of the USB/IP host to connect to. */
|
---|
223 | uint32_t uPort;
|
---|
224 | /** USB/IP host address. */
|
---|
225 | char *pszHost;
|
---|
226 | /** Mutex protecting the device list against concurrent access. */
|
---|
227 | RTSEMFASTMUTEX hMtxDevices;
|
---|
228 | /** Number of devices in the list. */
|
---|
229 | uint32_t cUsbDevicesCur;
|
---|
230 | /** The current list of devices to compare with. */
|
---|
231 | PUSBDEVICE pUsbDevicesCur;
|
---|
232 | /** Current receive state. */
|
---|
233 | USBIPRECVSTATE enmRecvState;
|
---|
234 | /** Scratch space for holding the data until it was completely received.
|
---|
235 | * Which one to access is based on the current receive state. */
|
---|
236 | union
|
---|
237 | {
|
---|
238 | UsbIpRetDevList RetDevList;
|
---|
239 | UsbIpExportedDevice ExportedDevice;
|
---|
240 | UsbIpDeviceInterface DeviceInterface;
|
---|
241 | /** Byte view. */
|
---|
242 | uint8_t abRecv[1];
|
---|
243 | } Scratch;
|
---|
244 | /** Residual number of bytes to receive before we can work with the data. */
|
---|
245 | size_t cbResidualRecv;
|
---|
246 | /** Current pointer into the scratch buffer. */
|
---|
247 | uint8_t *pbRecvBuf;
|
---|
248 | /** Number of devices left to receive for the current request. */
|
---|
249 | uint32_t cDevicesLeft;
|
---|
250 | /** Number of interfaces to skip during receive. */
|
---|
251 | uint32_t cInterfacesLeft;
|
---|
252 | /** The current head pointer for the new device list. */
|
---|
253 | PUSBDEVICE pHead;
|
---|
254 | /** The next pointer to add a device to. */
|
---|
255 | PUSBDEVICE *ppNext;
|
---|
256 | /** Current amount of devices in the list. */
|
---|
257 | uint32_t cDevicesCur;
|
---|
258 | /** Timestamp of the last time we successfully connected. */
|
---|
259 | uint64_t tsConnectSuccessLast;
|
---|
260 | };
|
---|
261 |
|
---|
262 | /**
|
---|
263 | * Convert the given exported device structure from host to network byte order.
|
---|
264 | *
|
---|
265 | * @returns nothing.
|
---|
266 | * @param pDevice The device structure to convert.
|
---|
267 | */
|
---|
268 | DECLINLINE(void) usbProxyBackendUsbIpExportedDeviceN2H(PUsbIpExportedDevice pDevice)
|
---|
269 | {
|
---|
270 | pDevice->u32BusNum = RT_N2H_U32(pDevice->u32BusNum);
|
---|
271 | pDevice->u32DevNum = RT_N2H_U32(pDevice->u32DevNum);
|
---|
272 | pDevice->u32Speed = RT_N2H_U32(pDevice->u32Speed);
|
---|
273 | pDevice->u16VendorId = RT_N2H_U16(pDevice->u16VendorId);
|
---|
274 | pDevice->u16ProductId = RT_N2H_U16(pDevice->u16ProductId);
|
---|
275 | pDevice->u16BcdDevice = RT_N2H_U16(pDevice->u16BcdDevice);
|
---|
276 | }
|
---|
277 |
|
---|
278 | /**
|
---|
279 | * Initialize data members.
|
---|
280 | */
|
---|
281 | USBProxyBackendUsbIp::USBProxyBackendUsbIp()
|
---|
282 | : USBProxyBackend()
|
---|
283 | {
|
---|
284 | }
|
---|
285 |
|
---|
286 | USBProxyBackendUsbIp::~USBProxyBackendUsbIp()
|
---|
287 | {
|
---|
288 |
|
---|
289 | }
|
---|
290 |
|
---|
291 | /**
|
---|
292 | * Initializes the object (called right after construction).
|
---|
293 | *
|
---|
294 | * @returns S_OK on success and non-fatal failures, some COM error otherwise.
|
---|
295 | */
|
---|
296 | int USBProxyBackendUsbIp::init(USBProxyService *pUsbProxyService, const com::Utf8Str &strId,
|
---|
297 | const com::Utf8Str &strAddress, bool fLoadingSettings)
|
---|
298 | {
|
---|
299 | int vrc = VINF_SUCCESS;
|
---|
300 |
|
---|
301 | USBProxyBackend::init(pUsbProxyService, strId, strAddress, fLoadingSettings);
|
---|
302 |
|
---|
303 | unconst(m_strBackend) = Utf8Str("USBIP");
|
---|
304 |
|
---|
305 | m = new Data;
|
---|
306 |
|
---|
307 | m->tsConnectSuccessLast = 0;
|
---|
308 |
|
---|
309 | /* Split address into hostname and port. */
|
---|
310 | RTCList<RTCString> lstAddress = strAddress.split(":");
|
---|
311 | if (lstAddress.size() < 1)
|
---|
312 | return VERR_INVALID_PARAMETER;
|
---|
313 | m->pszHost = RTStrDup(lstAddress[0].c_str());
|
---|
314 | if (!m->pszHost)
|
---|
315 | return VERR_NO_STR_MEMORY;
|
---|
316 | if (lstAddress.size() == 2)
|
---|
317 | {
|
---|
318 | m->uPort = lstAddress[1].toUInt32();
|
---|
319 | if (!m->uPort)
|
---|
320 | return VERR_INVALID_PARAMETER;
|
---|
321 | }
|
---|
322 |
|
---|
323 | /* Setup wakeup pipe and poll set first. */
|
---|
324 | vrc = RTSemFastMutexCreate(&m->hMtxDevices);
|
---|
325 | if (RT_SUCCESS(vrc))
|
---|
326 | {
|
---|
327 | vrc = RTPipeCreate(&m->hWakeupPipeR, &m->hWakeupPipeW, 0);
|
---|
328 | if (RT_SUCCESS(vrc))
|
---|
329 | {
|
---|
330 | vrc = RTPollSetCreate(&m->hPollSet);
|
---|
331 | if (RT_SUCCESS(vrc))
|
---|
332 | {
|
---|
333 | vrc = RTPollSetAddPipe(m->hPollSet, m->hWakeupPipeR, RTPOLL_EVT_READ, USBIP_POLL_ID_PIPE);
|
---|
334 | if (RT_SUCCESS(vrc))
|
---|
335 | {
|
---|
336 | /*
|
---|
337 | * Connect to the USB/IP host. Be more graceful to connection errors
|
---|
338 | * if we are instantiated while the settings are loaded to let
|
---|
339 | * VBoxSVC start.
|
---|
340 | *
|
---|
341 | * The worker thread keeps trying to connect every few seconds until
|
---|
342 | * either the USB source is removed by the user or the USB server is
|
---|
343 | * reachable.
|
---|
344 | */
|
---|
345 | vrc = reconnect();
|
---|
346 | if (RT_SUCCESS(vrc) || fLoadingSettings)
|
---|
347 | vrc = start(); /* Start service thread. */
|
---|
348 | }
|
---|
349 |
|
---|
350 | if (RT_FAILURE(vrc))
|
---|
351 | {
|
---|
352 | RTPollSetRemove(m->hPollSet, USBIP_POLL_ID_PIPE);
|
---|
353 | int rc2 = RTPollSetDestroy(m->hPollSet);
|
---|
354 | AssertRC(rc2);
|
---|
355 | m->hPollSet = NIL_RTPOLLSET;
|
---|
356 | }
|
---|
357 | }
|
---|
358 |
|
---|
359 | if (RT_FAILURE(vrc))
|
---|
360 | {
|
---|
361 | int rc2 = RTPipeClose(m->hWakeupPipeR);
|
---|
362 | AssertRC(rc2);
|
---|
363 | rc2 = RTPipeClose(m->hWakeupPipeW);
|
---|
364 | AssertRC(rc2);
|
---|
365 | m->hWakeupPipeR = m->hWakeupPipeW = NIL_RTPIPE;
|
---|
366 | }
|
---|
367 | }
|
---|
368 | if (RT_FAILURE(vrc))
|
---|
369 | {
|
---|
370 | RTSemFastMutexDestroy(m->hMtxDevices);
|
---|
371 | m->hMtxDevices = NIL_RTSEMFASTMUTEX;
|
---|
372 | }
|
---|
373 | }
|
---|
374 |
|
---|
375 | return vrc;
|
---|
376 | }
|
---|
377 |
|
---|
378 | /**
|
---|
379 | * Stop all service threads and free the device chain.
|
---|
380 | */
|
---|
381 | void USBProxyBackendUsbIp::uninit()
|
---|
382 | {
|
---|
383 | LogFlowThisFunc(("\n"));
|
---|
384 |
|
---|
385 | /*
|
---|
386 | * Stop the service.
|
---|
387 | */
|
---|
388 | if (isActive())
|
---|
389 | stop();
|
---|
390 |
|
---|
391 | /*
|
---|
392 | * Free resources.
|
---|
393 | */
|
---|
394 | if (m->hPollSet != NIL_RTPOLLSET)
|
---|
395 | {
|
---|
396 | disconnect();
|
---|
397 |
|
---|
398 | int vrc = RTPollSetRemove(m->hPollSet, USBIP_POLL_ID_PIPE);
|
---|
399 | AssertRC(vrc);
|
---|
400 | vrc = RTPollSetDestroy(m->hPollSet);
|
---|
401 | AssertRC(vrc);
|
---|
402 | vrc = RTPipeClose(m->hWakeupPipeR);
|
---|
403 | AssertRC(vrc);
|
---|
404 | vrc = RTPipeClose(m->hWakeupPipeW);
|
---|
405 | AssertRC(vrc);
|
---|
406 |
|
---|
407 | m->hPollSet = NIL_RTPOLLSET;
|
---|
408 | m->hWakeupPipeR = NIL_RTPIPE;
|
---|
409 | m->hWakeupPipeW = NIL_RTPIPE;
|
---|
410 | }
|
---|
411 |
|
---|
412 | if (m->pszHost)
|
---|
413 | RTStrFree(m->pszHost);
|
---|
414 | if (m->hMtxDevices != NIL_RTSEMFASTMUTEX)
|
---|
415 | {
|
---|
416 | RTSemFastMutexDestroy(m->hMtxDevices);
|
---|
417 | m->hMtxDevices = NIL_RTSEMFASTMUTEX;
|
---|
418 | }
|
---|
419 |
|
---|
420 | delete m;
|
---|
421 | USBProxyBackend::uninit();
|
---|
422 | }
|
---|
423 |
|
---|
424 |
|
---|
425 | int USBProxyBackendUsbIp::captureDevice(HostUSBDevice *aDevice)
|
---|
426 | {
|
---|
427 | AssertReturn(aDevice, VERR_GENERAL_FAILURE);
|
---|
428 | AssertReturn(!aDevice->isWriteLockOnCurrentThread(), VERR_GENERAL_FAILURE);
|
---|
429 |
|
---|
430 | AutoReadLock devLock(aDevice COMMA_LOCKVAL_SRC_POS);
|
---|
431 | LogFlowThisFunc(("aDevice=%s\n", aDevice->i_getName().c_str()));
|
---|
432 |
|
---|
433 | /*
|
---|
434 | * We don't need to do anything when the device is held... fake it.
|
---|
435 | */
|
---|
436 | Assert(aDevice->i_getUnistate() == kHostUSBDeviceState_Capturing);
|
---|
437 | devLock.release();
|
---|
438 |
|
---|
439 | return VINF_SUCCESS;
|
---|
440 | }
|
---|
441 |
|
---|
442 |
|
---|
443 | int USBProxyBackendUsbIp::releaseDevice(HostUSBDevice *aDevice)
|
---|
444 | {
|
---|
445 | AssertReturn(aDevice, VERR_GENERAL_FAILURE);
|
---|
446 | AssertReturn(!aDevice->isWriteLockOnCurrentThread(), VERR_GENERAL_FAILURE);
|
---|
447 |
|
---|
448 | AutoReadLock devLock(aDevice COMMA_LOCKVAL_SRC_POS);
|
---|
449 | LogFlowThisFunc(("aDevice=%s\n", aDevice->i_getName().c_str()));
|
---|
450 |
|
---|
451 | /*
|
---|
452 | * We're not really holding it atm., just fake it.
|
---|
453 | */
|
---|
454 | Assert(aDevice->i_getUnistate() == kHostUSBDeviceState_ReleasingToHost);
|
---|
455 | devLock.release();
|
---|
456 |
|
---|
457 | return VINF_SUCCESS;
|
---|
458 | }
|
---|
459 |
|
---|
460 |
|
---|
461 | bool USBProxyBackendUsbIp::isFakeUpdateRequired()
|
---|
462 | {
|
---|
463 | return true;
|
---|
464 | }
|
---|
465 |
|
---|
466 |
|
---|
467 | int USBProxyBackendUsbIp::wait(RTMSINTERVAL aMillies)
|
---|
468 | {
|
---|
469 | int vrc = VINF_SUCCESS;
|
---|
470 | bool fDeviceListChangedOrWokenUp = false;
|
---|
471 |
|
---|
472 | /* Don't start any possibly lengthy operation if we are supposed to return immediately again. */
|
---|
473 | if (!aMillies)
|
---|
474 | return VINF_SUCCESS;
|
---|
475 |
|
---|
476 | /* Try to reconnect once when we enter if we lost the connection earlier. */
|
---|
477 | if (m->hSocket == NIL_RTSOCKET)
|
---|
478 | reconnect();
|
---|
479 |
|
---|
480 | /* Query a new device list upon entering. */
|
---|
481 | if ( m->hSocket != NIL_RTSOCKET
|
---|
482 | && m->enmRecvState == kUsbIpRecvState_None)
|
---|
483 | {
|
---|
484 | vrc = startListExportedDevicesReq();
|
---|
485 | if (RT_FAILURE(vrc))
|
---|
486 | disconnect();
|
---|
487 | }
|
---|
488 |
|
---|
489 | /*
|
---|
490 | * Because the USB/IP protocol doesn't specify a way to get notified about
|
---|
491 | * new or removed exported devices we have to poll the host periodically for
|
---|
492 | * a new device list and compare it with the previous one notifying the proxy
|
---|
493 | * service about changes.
|
---|
494 | */
|
---|
495 | while ( !fDeviceListChangedOrWokenUp
|
---|
496 | && (aMillies == RT_INDEFINITE_WAIT || aMillies > 0)
|
---|
497 | && RT_SUCCESS(vrc))
|
---|
498 | {
|
---|
499 | RTMSINTERVAL msWait = aMillies;
|
---|
500 | uint64_t msPollStart = RTTimeMilliTS();
|
---|
501 | uint32_t uIdReady = 0;
|
---|
502 | uint32_t fEventsRecv = 0;
|
---|
503 |
|
---|
504 | /* Limit the waiting time to 3sec so we can either reconnect or get a new device list. */
|
---|
505 | if (m->hSocket == NIL_RTSOCKET || m->enmRecvState == kUsbIpRecvState_None)
|
---|
506 | msWait = RT_MIN(3000, aMillies);
|
---|
507 |
|
---|
508 | vrc = RTPoll(m->hPollSet, msWait, &fEventsRecv, &uIdReady);
|
---|
509 | if (RT_SUCCESS(vrc))
|
---|
510 | {
|
---|
511 | if (uIdReady == USBIP_POLL_ID_PIPE)
|
---|
512 | {
|
---|
513 | /* Drain the wakeup pipe. */
|
---|
514 | char bRead = 0;
|
---|
515 | size_t cbRead = 0;
|
---|
516 |
|
---|
517 | vrc = RTPipeRead(m->hWakeupPipeR, &bRead, 1, &cbRead);
|
---|
518 | Assert(RT_SUCCESS(vrc) && cbRead == 1);
|
---|
519 | fDeviceListChangedOrWokenUp = true;
|
---|
520 | }
|
---|
521 | else if (uIdReady == USBIP_POLL_ID_SOCKET)
|
---|
522 | {
|
---|
523 | if (fEventsRecv & RTPOLL_EVT_READ)
|
---|
524 | vrc = receiveData();
|
---|
525 | if ( RT_SUCCESS(vrc)
|
---|
526 | && (fEventsRecv & RTPOLL_EVT_ERROR))
|
---|
527 | vrc = VERR_NET_SHUTDOWN;
|
---|
528 |
|
---|
529 | /*
|
---|
530 | * If we are in the none state again we received the previous request
|
---|
531 | * and have a new device list to compare the old against.
|
---|
532 | */
|
---|
533 | if (m->enmRecvState == kUsbIpRecvState_None)
|
---|
534 | {
|
---|
535 | if (hasDevListChanged(m->pHead))
|
---|
536 | fDeviceListChangedOrWokenUp = true;
|
---|
537 |
|
---|
538 | /* Update to the new list in any case now that we have it anyway. */
|
---|
539 | RTSemFastMutexRequest(m->hMtxDevices);
|
---|
540 | freeDeviceList(m->pUsbDevicesCur);
|
---|
541 | m->cUsbDevicesCur = m->cDevicesCur;
|
---|
542 | m->pUsbDevicesCur = m->pHead;
|
---|
543 | RTSemFastMutexRelease(m->hMtxDevices);
|
---|
544 |
|
---|
545 | m->pHead = NULL;
|
---|
546 | resetRecvState();
|
---|
547 | }
|
---|
548 |
|
---|
549 | /* Current USB/IP server closes the connection after each request, don't abort but try again. */
|
---|
550 | if (vrc == VERR_NET_SHUTDOWN || vrc == VERR_BROKEN_PIPE || vrc == VERR_NET_CONNECTION_RESET_BY_PEER)
|
---|
551 | {
|
---|
552 | Log(("USB/IP: Lost connection to host \"%s\", trying to reconnect...\n", m->pszHost));
|
---|
553 | disconnect();
|
---|
554 | vrc = VINF_SUCCESS;
|
---|
555 | }
|
---|
556 | }
|
---|
557 | else
|
---|
558 | {
|
---|
559 | AssertMsgFailed(("Invalid poll ID returned\n"));
|
---|
560 | vrc = VERR_INVALID_STATE;
|
---|
561 | }
|
---|
562 | aMillies -= (RTMSINTERVAL)(RTTimeMilliTS() - msPollStart);
|
---|
563 | }
|
---|
564 | else if (vrc == VERR_TIMEOUT)
|
---|
565 | {
|
---|
566 | aMillies -= msWait;
|
---|
567 | if (aMillies)
|
---|
568 | {
|
---|
569 | /* Try to reconnect and start a new request if we lost the connection before. */
|
---|
570 | if (m->hSocket == NIL_RTSOCKET)
|
---|
571 | {
|
---|
572 | vrc = reconnect();
|
---|
573 | if (RT_SUCCESS(vrc))
|
---|
574 | vrc = startListExportedDevicesReq();
|
---|
575 | else if ( vrc == VERR_NET_SHUTDOWN
|
---|
576 | || vrc == VERR_BROKEN_PIPE
|
---|
577 | || vrc == VERR_NET_CONNECTION_RESET_BY_PEER
|
---|
578 | || vrc == VERR_NET_CONNECTION_REFUSED)
|
---|
579 | {
|
---|
580 | if (hasDevListChanged(m->pHead))
|
---|
581 | fDeviceListChangedOrWokenUp = true;
|
---|
582 | vrc = VINF_SUCCESS;
|
---|
583 | }
|
---|
584 | }
|
---|
585 | }
|
---|
586 | }
|
---|
587 | }
|
---|
588 |
|
---|
589 | LogFlowFunc(("return vrc=%Rrc\n", vrc));
|
---|
590 | return vrc;
|
---|
591 | }
|
---|
592 |
|
---|
593 |
|
---|
594 | int USBProxyBackendUsbIp::interruptWait(void)
|
---|
595 | {
|
---|
596 | AssertReturn(!isWriteLockOnCurrentThread(), VERR_GENERAL_FAILURE);
|
---|
597 |
|
---|
598 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
599 |
|
---|
600 | int vrc = RTPipeWriteBlocking(m->hWakeupPipeW, "", 1, NULL);
|
---|
601 | if (RT_SUCCESS(vrc))
|
---|
602 | RTPipeFlush(m->hWakeupPipeW);
|
---|
603 | LogFlowFunc(("returning %Rrc\n", vrc));
|
---|
604 | return vrc;
|
---|
605 | }
|
---|
606 |
|
---|
607 |
|
---|
608 | PUSBDEVICE USBProxyBackendUsbIp::getDevices(void)
|
---|
609 | {
|
---|
610 | PUSBDEVICE pFirst = NULL;
|
---|
611 | PUSBDEVICE *ppNext = &pFirst;
|
---|
612 |
|
---|
613 | LogFlowThisFunc(("\n"));
|
---|
614 |
|
---|
615 | /* Create a deep copy of the device list. */
|
---|
616 | RTSemFastMutexRequest(m->hMtxDevices);
|
---|
617 | PUSBDEVICE pCur = m->pUsbDevicesCur;
|
---|
618 | while (pCur)
|
---|
619 | {
|
---|
620 | PUSBDEVICE pNew = (PUSBDEVICE)RTMemAllocZ(sizeof(USBDEVICE));
|
---|
621 | if (pNew)
|
---|
622 | {
|
---|
623 | pNew->pszManufacturer = RTStrDup(pCur->pszManufacturer);
|
---|
624 | pNew->pszProduct = RTStrDup(pCur->pszProduct);
|
---|
625 | if (pCur->pszSerialNumber)
|
---|
626 | pNew->pszSerialNumber = RTStrDup(pCur->pszSerialNumber);
|
---|
627 | pNew->pszBackend = RTStrDup(pCur->pszBackend);
|
---|
628 | pNew->pszAddress = RTStrDup(pCur->pszAddress);
|
---|
629 |
|
---|
630 | pNew->idVendor = pCur->idVendor;
|
---|
631 | pNew->idProduct = pCur->idProduct;
|
---|
632 | pNew->bcdDevice = pCur->bcdDevice;
|
---|
633 | pNew->bcdUSB = pCur->bcdUSB;
|
---|
634 | pNew->bDeviceClass = pCur->bDeviceClass;
|
---|
635 | pNew->bDeviceSubClass = pCur->bDeviceSubClass;
|
---|
636 | pNew->bDeviceProtocol = pCur->bDeviceProtocol;
|
---|
637 | pNew->bNumConfigurations = pCur->bNumConfigurations;
|
---|
638 | pNew->enmState = pCur->enmState;
|
---|
639 | pNew->u64SerialHash = pCur->u64SerialHash;
|
---|
640 | pNew->bBus = pCur->bBus;
|
---|
641 | pNew->bPort = pCur->bPort;
|
---|
642 | pNew->enmSpeed = pCur->enmSpeed;
|
---|
643 |
|
---|
644 | /* link it */
|
---|
645 | pNew->pNext = NULL;
|
---|
646 | pNew->pPrev = *ppNext;
|
---|
647 | *ppNext = pNew;
|
---|
648 | ppNext = &pNew->pNext;
|
---|
649 | }
|
---|
650 |
|
---|
651 | pCur = pCur->pNext;
|
---|
652 | }
|
---|
653 | RTSemFastMutexRelease(m->hMtxDevices);
|
---|
654 |
|
---|
655 | LogFlowThisFunc(("returning %#p\n", pFirst));
|
---|
656 | return pFirst;
|
---|
657 | }
|
---|
658 |
|
---|
659 | /**
|
---|
660 | * Frees a given device list.
|
---|
661 | *
|
---|
662 | * @returns nothing.
|
---|
663 | * @param pHead The head of the device list to free.
|
---|
664 | */
|
---|
665 | void USBProxyBackendUsbIp::freeDeviceList(PUSBDEVICE pHead)
|
---|
666 | {
|
---|
667 | PUSBDEVICE pNext = pHead;
|
---|
668 | while (pNext)
|
---|
669 | {
|
---|
670 | PUSBDEVICE pFree = pNext;
|
---|
671 | pNext = pNext->pNext;
|
---|
672 | freeDevice(pFree);
|
---|
673 | }
|
---|
674 | }
|
---|
675 |
|
---|
676 | /**
|
---|
677 | * Resets the receive state to the idle state.
|
---|
678 | *
|
---|
679 | * @returns nothing.
|
---|
680 | */
|
---|
681 | void USBProxyBackendUsbIp::resetRecvState()
|
---|
682 | {
|
---|
683 | LogFlowFunc(("\n"));
|
---|
684 | freeDeviceList(m->pHead);
|
---|
685 | m->pHead = NULL;
|
---|
686 | m->ppNext = &m->pHead;
|
---|
687 | m->cDevicesCur = 0;
|
---|
688 | m->enmRecvState = kUsbIpRecvState_None;
|
---|
689 | m->cbResidualRecv = 0;
|
---|
690 | m->pbRecvBuf = &m->Scratch.abRecv[0];
|
---|
691 | m->cDevicesLeft = 0;
|
---|
692 | LogFlowFunc(("\n"));
|
---|
693 | }
|
---|
694 |
|
---|
695 | /**
|
---|
696 | * Disconnects from the host and resets the receive state.
|
---|
697 | *
|
---|
698 | * @returns nothing.
|
---|
699 | */
|
---|
700 | void USBProxyBackendUsbIp::disconnect()
|
---|
701 | {
|
---|
702 | LogFlowFunc(("\n"));
|
---|
703 |
|
---|
704 | if (m->hSocket != NIL_RTSOCKET)
|
---|
705 | {
|
---|
706 | int vrc = RTPollSetRemove(m->hPollSet, USBIP_POLL_ID_SOCKET);
|
---|
707 | NOREF(vrc);
|
---|
708 | Assert(RT_SUCCESS(vrc) || vrc == VERR_POLL_HANDLE_ID_NOT_FOUND);
|
---|
709 |
|
---|
710 | RTTcpClientCloseEx(m->hSocket, false /*fGracefulShutdown*/);
|
---|
711 | m->hSocket = NIL_RTSOCKET;
|
---|
712 | }
|
---|
713 |
|
---|
714 | resetRecvState();
|
---|
715 | LogFlowFunc(("returns\n"));
|
---|
716 | }
|
---|
717 |
|
---|
718 | /**
|
---|
719 | * Tries to reconnect to the USB/IP host.
|
---|
720 | *
|
---|
721 | * @returns VBox status code.
|
---|
722 | */
|
---|
723 | int USBProxyBackendUsbIp::reconnect()
|
---|
724 | {
|
---|
725 | LogFlowFunc(("\n"));
|
---|
726 |
|
---|
727 | /* Make sure we are disconnected. */
|
---|
728 | disconnect();
|
---|
729 |
|
---|
730 | /* Connect to the USB/IP host. */
|
---|
731 | int vrc = RTTcpClientConnect(m->pszHost, m->uPort, &m->hSocket);
|
---|
732 | if (RT_SUCCESS(vrc))
|
---|
733 | {
|
---|
734 | vrc = RTTcpSetSendCoalescing(m->hSocket, false);
|
---|
735 | if (RT_FAILURE(vrc))
|
---|
736 | LogRelMax(5, ("USB/IP: Disabling send coalescing failed (vrc=%Rrc), continuing nevertheless but expect increased latency\n", vrc));
|
---|
737 |
|
---|
738 | vrc = RTPollSetAddSocket(m->hPollSet, m->hSocket, RTPOLL_EVT_READ | RTPOLL_EVT_ERROR, USBIP_POLL_ID_SOCKET);
|
---|
739 | if (RT_FAILURE(vrc))
|
---|
740 | {
|
---|
741 | RTTcpClientCloseEx(m->hSocket, false /*fGracefulShutdown*/);
|
---|
742 | m->hSocket = NIL_RTSOCKET;
|
---|
743 | }
|
---|
744 | else
|
---|
745 | {
|
---|
746 | LogFlowFunc(("Connected to host \"%s\"\n", m->pszHost));
|
---|
747 | m->tsConnectSuccessLast = RTTimeMilliTS();
|
---|
748 | }
|
---|
749 | }
|
---|
750 | else if (m->tsConnectSuccessLast + 10 * RT_MS_1SEC < RTTimeMilliTS())
|
---|
751 | {
|
---|
752 | /* Make sure the device list is clear if we failed to reconnect for some time. */
|
---|
753 | RTSemFastMutexRequest(m->hMtxDevices);
|
---|
754 | if (m->pUsbDevicesCur)
|
---|
755 | {
|
---|
756 | freeDeviceList(m->pUsbDevicesCur);
|
---|
757 | m->cUsbDevicesCur = 0;
|
---|
758 | m->pUsbDevicesCur = NULL;
|
---|
759 | }
|
---|
760 | RTSemFastMutexRelease(m->hMtxDevices);
|
---|
761 | }
|
---|
762 |
|
---|
763 | LogFlowFunc(("returns vrc=%Rrc\n", vrc));
|
---|
764 | return vrc;
|
---|
765 | }
|
---|
766 |
|
---|
767 | /**
|
---|
768 | * Initiates a new List Exported Devices request.
|
---|
769 | *
|
---|
770 | * @returns VBox status code.
|
---|
771 | */
|
---|
772 | int USBProxyBackendUsbIp::startListExportedDevicesReq()
|
---|
773 | {
|
---|
774 | int vrc = VINF_SUCCESS;
|
---|
775 |
|
---|
776 | LogFlowFunc(("\n"));
|
---|
777 |
|
---|
778 | /*
|
---|
779 | * Reset the current state and reconnect in case we were called in the middle
|
---|
780 | * of another transfer (which should not happen).
|
---|
781 | */
|
---|
782 | Assert(m->enmRecvState == kUsbIpRecvState_None);
|
---|
783 | if (m->enmRecvState != kUsbIpRecvState_None)
|
---|
784 | vrc = reconnect();
|
---|
785 |
|
---|
786 | if (RT_SUCCESS(vrc))
|
---|
787 | {
|
---|
788 | /* Send of the request. */
|
---|
789 | UsbIpReqDevList ReqDevList;
|
---|
790 | ReqDevList.u16Version = RT_H2N_U16(USBIP_VERSION);
|
---|
791 | ReqDevList.u16Cmd = RT_H2N_U16(USBIP_INDICATOR_REQ | USBIP_REQ_RET_DEVLIST);
|
---|
792 | ReqDevList.i32Status = RT_H2N_S32(0);
|
---|
793 |
|
---|
794 | vrc = RTTcpWrite(m->hSocket, &ReqDevList, sizeof(ReqDevList));
|
---|
795 | if (RT_SUCCESS(vrc))
|
---|
796 | advanceState(kUsbIpRecvState_Hdr);
|
---|
797 | }
|
---|
798 |
|
---|
799 | LogFlowFunc(("returns vrc=%Rrc\n", vrc));
|
---|
800 | return vrc;
|
---|
801 | }
|
---|
802 |
|
---|
803 | /**
|
---|
804 | * Advances the state machine to the given state.
|
---|
805 | *
|
---|
806 | * @returns nothing.
|
---|
807 | * @param enmRecvState The new receive state.
|
---|
808 | */
|
---|
809 | void USBProxyBackendUsbIp::advanceState(USBIPRECVSTATE enmRecvState)
|
---|
810 | {
|
---|
811 | LogFlowFunc(("enmRecvState=%u\n", enmRecvState));
|
---|
812 |
|
---|
813 | switch (enmRecvState)
|
---|
814 | {
|
---|
815 | case kUsbIpRecvState_None:
|
---|
816 | break;
|
---|
817 | case kUsbIpRecvState_Hdr:
|
---|
818 | {
|
---|
819 | m->cbResidualRecv = sizeof(UsbIpRetDevList);
|
---|
820 | m->pbRecvBuf = (uint8_t *)&m->Scratch.RetDevList;
|
---|
821 | break;
|
---|
822 | }
|
---|
823 | case kUsbIpRecvState_ExportedDevice:
|
---|
824 | {
|
---|
825 | m->cbResidualRecv = sizeof(UsbIpExportedDevice);
|
---|
826 | m->pbRecvBuf = (uint8_t *)&m->Scratch.ExportedDevice;
|
---|
827 | break;
|
---|
828 | }
|
---|
829 | case kUsbIpRecvState_DeviceInterface:
|
---|
830 | {
|
---|
831 | m->cbResidualRecv = sizeof(UsbIpDeviceInterface);
|
---|
832 | m->pbRecvBuf = (uint8_t *)&m->Scratch.DeviceInterface;
|
---|
833 | break;
|
---|
834 | }
|
---|
835 | default:
|
---|
836 | AssertMsgFailed(("Invalid USB/IP receive state %d\n", enmRecvState));
|
---|
837 | return;
|
---|
838 | }
|
---|
839 |
|
---|
840 | m->enmRecvState = enmRecvState;
|
---|
841 | LogFlowFunc(("returns\n"));
|
---|
842 | }
|
---|
843 |
|
---|
844 | /**
|
---|
845 | * Receives data from the USB/IP host and processes it when everything for the current
|
---|
846 | * state was received.
|
---|
847 | *
|
---|
848 | * @returns VBox status code.
|
---|
849 | */
|
---|
850 | int USBProxyBackendUsbIp::receiveData()
|
---|
851 | {
|
---|
852 | int vrc = VINF_SUCCESS;
|
---|
853 | size_t cbRecvd = 0;
|
---|
854 |
|
---|
855 | LogFlowFunc(("\n"));
|
---|
856 |
|
---|
857 | do
|
---|
858 | {
|
---|
859 | vrc = RTTcpReadNB(m->hSocket, m->pbRecvBuf, m->cbResidualRecv, &cbRecvd);
|
---|
860 |
|
---|
861 | LogFlowFunc(("RTTcpReadNB(%#p, %#p, %zu, %zu) -> %Rrc\n", m->hSocket, m->pbRecvBuf, m->cbResidualRecv, cbRecvd, vrc));
|
---|
862 |
|
---|
863 | if ( vrc == VINF_SUCCESS
|
---|
864 | && cbRecvd > 0)
|
---|
865 | {
|
---|
866 | m->cbResidualRecv -= cbRecvd;
|
---|
867 | m->pbRecvBuf += cbRecvd;
|
---|
868 | /* In case we received everything for the current state process the data. */
|
---|
869 | if (!m->cbResidualRecv)
|
---|
870 | {
|
---|
871 | vrc = processData();
|
---|
872 | if ( RT_SUCCESS(vrc)
|
---|
873 | && m->enmRecvState == kUsbIpRecvState_None)
|
---|
874 | break;
|
---|
875 | }
|
---|
876 | }
|
---|
877 | else if (vrc == VINF_TRY_AGAIN)
|
---|
878 | Assert(!cbRecvd);
|
---|
879 |
|
---|
880 | } while (vrc == VINF_SUCCESS && cbRecvd > 0);
|
---|
881 |
|
---|
882 | if (vrc == VINF_TRY_AGAIN)
|
---|
883 | vrc = VINF_SUCCESS;
|
---|
884 |
|
---|
885 | LogFlowFunc(("returns vrc=%Rrc\n", vrc));
|
---|
886 | return vrc;
|
---|
887 | }
|
---|
888 |
|
---|
889 | /**
|
---|
890 | * Processes the data in the scratch buffer based on the current state.
|
---|
891 | *
|
---|
892 | * @returns VBox status code.
|
---|
893 | */
|
---|
894 | int USBProxyBackendUsbIp::processData()
|
---|
895 | {
|
---|
896 | int vrc = VINF_SUCCESS;
|
---|
897 |
|
---|
898 | switch (m->enmRecvState)
|
---|
899 | {
|
---|
900 | case kUsbIpRecvState_Hdr:
|
---|
901 | {
|
---|
902 | /* Check that the reply matches our expectations. */
|
---|
903 | if ( RT_N2H_U16(m->Scratch.RetDevList.u16Version) == USBIP_VERSION
|
---|
904 | && RT_N2H_U16(m->Scratch.RetDevList.u16Cmd) == USBIP_REQ_RET_DEVLIST
|
---|
905 | && RT_N2H_S32(m->Scratch.RetDevList.i32Status) == USBIP_STATUS_SUCCESS)
|
---|
906 |
|
---|
907 | {
|
---|
908 | /* Populate the number of exported devices in the list and go to the next state. */
|
---|
909 | m->cDevicesLeft = RT_N2H_U32(m->Scratch.RetDevList.u32DevicesExported);
|
---|
910 | if (m->cDevicesLeft)
|
---|
911 | advanceState(kUsbIpRecvState_ExportedDevice);
|
---|
912 | else
|
---|
913 | advanceState(kUsbIpRecvState_None);
|
---|
914 | }
|
---|
915 | else
|
---|
916 | {
|
---|
917 | LogRelMax(10, ("USB/IP: Host sent an invalid reply to the list exported device request (Version: %#x Cmd: %#x Status: %#x)\n",
|
---|
918 | RT_N2H_U16(m->Scratch.RetDevList.u16Version), RT_N2H_U16(m->Scratch.RetDevList.u16Cmd),
|
---|
919 | RT_N2H_S32(m->Scratch.RetDevList.i32Status)));
|
---|
920 | /* Disconnect and start over. */
|
---|
921 | advanceState(kUsbIpRecvState_None);
|
---|
922 | disconnect();
|
---|
923 | vrc = VERR_NET_SHUTDOWN;
|
---|
924 | }
|
---|
925 | break;
|
---|
926 | }
|
---|
927 | case kUsbIpRecvState_ExportedDevice:
|
---|
928 | {
|
---|
929 | /* Create a new device and add it to the list. */
|
---|
930 | usbProxyBackendUsbIpExportedDeviceN2H(&m->Scratch.ExportedDevice);
|
---|
931 | vrc = addDeviceToList(&m->Scratch.ExportedDevice);
|
---|
932 | if (RT_SUCCESS(vrc))
|
---|
933 | {
|
---|
934 | m->cInterfacesLeft = m->Scratch.ExportedDevice.bNumInterfaces;
|
---|
935 | if (m->cInterfacesLeft)
|
---|
936 | advanceState(kUsbIpRecvState_DeviceInterface);
|
---|
937 | else
|
---|
938 | {
|
---|
939 | m->cDevicesLeft--;
|
---|
940 | if (m->cDevicesLeft)
|
---|
941 | advanceState(kUsbIpRecvState_ExportedDevice);
|
---|
942 | else
|
---|
943 | advanceState(kUsbIpRecvState_None);
|
---|
944 | }
|
---|
945 | }
|
---|
946 | break;
|
---|
947 | }
|
---|
948 | case kUsbIpRecvState_DeviceInterface:
|
---|
949 | {
|
---|
950 | /*
|
---|
951 | * If all interfaces for the current device were received receive the next device
|
---|
952 | * if there is another one left, if not we are done with the current request.
|
---|
953 | */
|
---|
954 | m->cInterfacesLeft--;
|
---|
955 | if (m->cInterfacesLeft)
|
---|
956 | advanceState(kUsbIpRecvState_DeviceInterface);
|
---|
957 | else
|
---|
958 | {
|
---|
959 | m->cDevicesLeft--;
|
---|
960 | if (m->cDevicesLeft)
|
---|
961 | advanceState(kUsbIpRecvState_ExportedDevice);
|
---|
962 | else
|
---|
963 | advanceState(kUsbIpRecvState_None);
|
---|
964 | }
|
---|
965 | break;
|
---|
966 | }
|
---|
967 | case kUsbIpRecvState_None:
|
---|
968 | default:
|
---|
969 | AssertMsgFailed(("Invalid USB/IP receive state %d\n", m->enmRecvState));
|
---|
970 | return VERR_INVALID_STATE;
|
---|
971 | }
|
---|
972 |
|
---|
973 | return vrc;
|
---|
974 | }
|
---|
975 |
|
---|
976 | /**
|
---|
977 | * Creates a new USB device and adds it to the list.
|
---|
978 | *
|
---|
979 | * @returns VBox status code.
|
---|
980 | * @param pDev Pointer to the USB/IP exported device structure to take
|
---|
981 | * the information for the new device from.
|
---|
982 | */
|
---|
983 | int USBProxyBackendUsbIp::addDeviceToList(PUsbIpExportedDevice pDev)
|
---|
984 | {
|
---|
985 | int vrc = VINF_SUCCESS;
|
---|
986 | PUSBDEVICE pNew = (PUSBDEVICE)RTMemAllocZ(sizeof(USBDEVICE));
|
---|
987 | if (!pNew)
|
---|
988 | return VERR_NO_MEMORY;
|
---|
989 |
|
---|
990 | pNew->pszManufacturer = RTStrDup("");
|
---|
991 | pNew->pszProduct = RTStrDup("");
|
---|
992 | pNew->pszSerialNumber = NULL;
|
---|
993 | pNew->pszBackend = RTStrDup("usbip");
|
---|
994 |
|
---|
995 | /* Make sure the Bus id is 0 terminated. */
|
---|
996 | pDev->szBusId[31] = '\0';
|
---|
997 | pNew->pszAddress = RTStrAPrintf2("usbip://%s:%u:%s", m->pszHost, m->uPort, &pDev->szBusId[0]);
|
---|
998 | if (RT_LIKELY(pNew->pszAddress))
|
---|
999 | {
|
---|
1000 | pNew->idVendor = pDev->u16VendorId;
|
---|
1001 | pNew->idProduct = pDev->u16ProductId;
|
---|
1002 | pNew->bcdDevice = pDev->u16BcdDevice;
|
---|
1003 | pNew->bDeviceClass = pDev->bDeviceClass;
|
---|
1004 | pNew->bDeviceSubClass = pDev->bDeviceSubClass;
|
---|
1005 | pNew->bDeviceProtocol = pDev->bDeviceProtocol;
|
---|
1006 | pNew->bNumConfigurations = pDev->bNumConfigurations;
|
---|
1007 | pNew->enmState = USBDEVICESTATE_USED_BY_HOST_CAPTURABLE;
|
---|
1008 | pNew->u64SerialHash = 0;
|
---|
1009 | /** @todo The following is not correct but is required to to get USB testing working
|
---|
1010 | * because only the port can be part of a filter (adding the required attributes for the bus
|
---|
1011 | * breaks API and ABI compatibility).
|
---|
1012 | * Filtering by port number is required for USB testing to connect to the correct device
|
---|
1013 | * in case there are multiple ones.
|
---|
1014 | */
|
---|
1015 | pNew->bBus = (uint8_t)pDev->u32DevNum;
|
---|
1016 | pNew->bPort = (uint8_t)pDev->u32BusNum;
|
---|
1017 |
|
---|
1018 | switch (pDev->u32Speed)
|
---|
1019 | {
|
---|
1020 | case USBIP_SPEED_LOW:
|
---|
1021 | pNew->enmSpeed = USBDEVICESPEED_LOW;
|
---|
1022 | pNew->bcdUSB = 1 << 8;
|
---|
1023 | break;
|
---|
1024 | case USBIP_SPEED_FULL:
|
---|
1025 | pNew->enmSpeed = USBDEVICESPEED_FULL;
|
---|
1026 | pNew->bcdUSB = 1 << 8;
|
---|
1027 | break;
|
---|
1028 | case USBIP_SPEED_HIGH:
|
---|
1029 | pNew->enmSpeed = USBDEVICESPEED_HIGH;
|
---|
1030 | pNew->bcdUSB = 2 << 8;
|
---|
1031 | break;
|
---|
1032 | case USBIP_SPEED_WIRELESS:
|
---|
1033 | pNew->enmSpeed = USBDEVICESPEED_VARIABLE;
|
---|
1034 | pNew->bcdUSB = 1 << 8;
|
---|
1035 | break;
|
---|
1036 | case USBIP_SPEED_SUPER:
|
---|
1037 | pNew->enmSpeed = USBDEVICESPEED_SUPER;
|
---|
1038 | pNew->bcdUSB = 3 << 8;
|
---|
1039 | break;
|
---|
1040 | case USBIP_SPEED_UNKNOWN:
|
---|
1041 | default:
|
---|
1042 | pNew->bcdUSB = 1 << 8;
|
---|
1043 | pNew->enmSpeed = USBDEVICESPEED_UNKNOWN;
|
---|
1044 | }
|
---|
1045 |
|
---|
1046 | /* link it */
|
---|
1047 | pNew->pNext = NULL;
|
---|
1048 | pNew->pPrev = *m->ppNext;
|
---|
1049 | *m->ppNext = pNew;
|
---|
1050 | m->ppNext = &pNew->pNext;
|
---|
1051 | m->cDevicesCur++;
|
---|
1052 | }
|
---|
1053 | else
|
---|
1054 | vrc = VERR_NO_STR_MEMORY;
|
---|
1055 |
|
---|
1056 | if (RT_FAILURE(vrc))
|
---|
1057 | {
|
---|
1058 | if (pNew->pszManufacturer)
|
---|
1059 | RTStrFree((char *)pNew->pszManufacturer);
|
---|
1060 | if (pNew->pszProduct)
|
---|
1061 | RTStrFree((char *)pNew->pszProduct);
|
---|
1062 | if (pNew->pszBackend)
|
---|
1063 | RTStrFree((char *)pNew->pszBackend);
|
---|
1064 | if (pNew->pszAddress)
|
---|
1065 | RTStrFree((char *)pNew->pszAddress);
|
---|
1066 | RTMemFree(pNew);
|
---|
1067 | }
|
---|
1068 |
|
---|
1069 | return vrc;
|
---|
1070 | }
|
---|
1071 |
|
---|
1072 | /**
|
---|
1073 | * Compares the given device list with the current one and returns whether it has
|
---|
1074 | * changed.
|
---|
1075 | *
|
---|
1076 | * @returns flag whether the device list has changed compared to the current one.
|
---|
1077 | * @param pDevices The device list to compare the current one against.
|
---|
1078 | */
|
---|
1079 | bool USBProxyBackendUsbIp::hasDevListChanged(PUSBDEVICE pDevices)
|
---|
1080 | {
|
---|
1081 | /** @todo */
|
---|
1082 | NOREF(pDevices);
|
---|
1083 | return true;
|
---|
1084 | }
|
---|
1085 |
|
---|