VirtualBox

source: vbox/trunk/src/VBox/Devices/USB/usbip/USBProxyDevice-usbip.cpp@ 61583

Last change on this file since 61583 was 60800, checked in by vboxsync, 9 years ago

Device/USBProxyDevice-usbip: Fixes for corner cases in the protocol

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 58.3 KB
Line 
1/* $Id: USBProxyDevice-usbip.cpp 60800 2016-05-03 08:46:12Z vboxsync $ */
2/** @file
3 * USB device proxy - USB/IP backend.
4 */
5
6/*
7 * Copyright (C) 2014-2015 Oracle Corporation
8 *
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.
16 */
17
18
19/*********************************************************************************************************************************
20* Header Files *
21*********************************************************************************************************************************/
22#define LOG_GROUP LOG_GROUP_DRV_USBPROXY
23
24#include <VBox/log.h>
25#include <VBox/err.h>
26#include <VBox/vmm/pdm.h>
27
28#include <iprt/asm.h>
29#include <iprt/assert.h>
30#include <iprt/alloc.h>
31#include <iprt/string.h>
32#include <iprt/socket.h>
33#include <iprt/poll.h>
34#include <iprt/tcp.h>
35#include <iprt/pipe.h>
36#include <iprt/list.h>
37#include <iprt/semaphore.h>
38
39#include "../USBProxyDevice.h"
40
41
42/*********************************************************************************************************************************
43* Constants And Macros, Structures and Typedefs *
44*********************************************************************************************************************************/
45
46/** The USB version number used for the protocol. */
47#define USBIP_VERSION UINT16_C(0x0111)
48/** Request indicator in the command code. */
49#define USBIP_INDICATOR_REQ RT_BIT(15)
50
51/** Command/Reply code for OP_REQ/RET_DEVLIST. */
52#define USBIP_REQ_RET_DEVLIST UINT16_C(5)
53/** Command/Reply code for OP_REQ/REP_IMPORT. */
54#define USBIP_REQ_RET_IMPORT UINT16_C(3)
55/** USB submit command identifier. */
56#define USBIP_CMD_SUBMIT UINT32_C(1)
57/** USB submit status identifier. */
58#define USBIP_RET_SUBMIT UINT32_C(3)
59/** URB unlink (cancel) command identifier. */
60#define USBIP_CMD_UNLINK UINT32_C(2)
61/** URB unlink (cancel) reply identifier. */
62#define USBIP_RET_UNLINK UINT32_C(4)
63
64/** Short read is not okay for the specified URB. */
65#define USBIP_XFER_FLAGS_SHORT_NOT_OK RT_BIT_32(0)
66/** Queue the isochronous URB as soon as possible. */
67#define USBIP_XFER_FLAGS_ISO_ASAP RT_BIT_32(1)
68/** Don't use DMA mappings for this URB. */
69#define USBIP_XFER_FLAGS_NO_TRANSFER_DMA_MAP RT_BIT_32(2)
70/** Explain - only applies to UHCI. */
71#define USBIP_XFER_FLAGS_FSBR RT_BIT_32(4)
72
73/** URB direction - input. */
74#define USBIP_DIR_IN UINT32_C(1)
75/** URB direction - output. */
76#define USBIP_DIR_OUT UINT32_C(0)
77
78/** @name USB/IP error codes.
79 * @{ */
80/** Success indicator. */
81#define USBIP_STATUS_SUCCESS INT32_C(0)
82/** Pipe stalled. */
83#define USBIP_STATUS_PIPE_STALLED INT32_C(-32)
84/** URB was unlinked by a call to usb_unlink_urb(). */
85#define USBIP_STATUS_URB_UNLINKED INT32_C(-104)
86/** Short read. */
87#define USBIP_STATUS_SHORT_READ INT32_C(-121)
88/** @} */
89
90/**
91 * Exported device entry in the OP_RET_DEVLIST reply.
92 */
93#pragma pack(1)
94typedef struct UsbIpExportedDevice
95{
96 /** Path of the device, zero terminated string. */
97 char szPath[256];
98 /** Bus ID of the exported device, zero terminated string. */
99 char szBusId[32];
100 /** Bus number. */
101 uint32_t u32BusNum;
102 /** Device number. */
103 uint32_t u32DevNum;
104 /** Speed indicator of the device. */
105 uint32_t u32Speed;
106 /** Vendor ID of the device. */
107 uint16_t u16VendorId;
108 /** Product ID of the device. */
109 uint16_t u16ProductId;
110 /** Device release number. */
111 uint16_t u16BcdDevice;
112 /** Device class. */
113 uint8_t bDeviceClass;
114 /** Device Subclass. */
115 uint8_t bDeviceSubClass;
116 /** Device protocol. */
117 uint8_t bDeviceProtocol;
118 /** Configuration value. */
119 uint8_t bConfigurationValue;
120 /** Current configuration value of the device. */
121 uint8_t bNumConfigurations;
122 /** Number of interfaces for the device. */
123 uint8_t bNumInterfaces;
124} UsbIpExportedDevice;
125/** Pointer to a exported device entry. */
126typedef UsbIpExportedDevice *PUsbIpExportedDevice;
127#pragma pack()
128AssertCompileSize(UsbIpExportedDevice, 312);
129
130/**
131 * Interface descriptor entry for an exported device.
132 */
133#pragma pack(1)
134typedef struct UsbIpDeviceInterface
135{
136 /** Intefrace class. */
137 uint8_t bInterfaceClass;
138 /** Interface sub class. */
139 uint8_t bInterfaceSubClass;
140 /** Interface protocol identifier. */
141 uint8_t bInterfaceProtocol;
142 /** Padding byte for alignment. */
143 uint8_t bPadding;
144} UsbIpDeviceInterface;
145/** Pointer to an interface descriptor entry. */
146typedef UsbIpDeviceInterface *PUsbIpDeviceInterface;
147#pragma pack()
148
149/**
150 * USB/IP Import request.
151 */
152#pragma pack(1)
153typedef struct UsbIpReqImport
154{
155 /** Protocol version number. */
156 uint16_t u16Version;
157 /** Command code. */
158 uint16_t u16Cmd;
159 /** Status field, unused. */
160 int32_t u32Status;
161 /** Bus Id of the device as zero terminated string. */
162 char aszBusId[32];
163} UsbIpReqImport;
164/** Pointer to a import request. */
165typedef UsbIpReqImport *PUsbIpReqImport;
166#pragma pack()
167
168/**
169 * USB/IP Import reply.
170 *
171 * This is only the header, for successful
172 * imports the device details are sent to as
173 * defined in UsbIpExportedDevice.
174 */
175#pragma pack(1)
176typedef struct UsbIpRetImport
177{
178 /** Protocol version number. */
179 uint16_t u16Version;
180 /** Command code. */
181 uint16_t u16Cmd;
182 /** Status field, unused. */
183 int32_t u32Status;
184} UsbIpRetImport;
185/** Pointer to a import reply. */
186typedef UsbIpRetImport *PUsbIpRetImport;
187#pragma pack()
188
189/**
190 * Command/Reply header common to the submit and unlink commands
191 * replies.
192 */
193#pragma pack(1)
194typedef struct UsbIpReqRetHdr
195{
196 /** Request/Return code. */
197 uint32_t u32ReqRet;
198 /** Sequence number to identify the URB. */
199 uint32_t u32SeqNum;
200 /** Device id. */
201 uint32_t u32DevId;
202 /** Direction of the endpoint (host->device, device->host). */
203 uint32_t u32Direction;
204 /** Endpoint number. */
205 uint32_t u32Endpoint;
206} UsbIpReqRetHdr;
207/** Pointer to a request/reply header. */
208typedef UsbIpReqRetHdr *PUsbIpReqRetHdr;
209#pragma pack()
210
211/**
212 * USB/IP Submit request.
213 */
214#pragma pack(1)
215typedef struct UsbIpReqSubmit
216{
217 /** The request header. */
218 UsbIpReqRetHdr Hdr;
219 /** Transfer flags for the URB. */
220 uint32_t u32XferFlags;
221 /** Transfer buffer length. */
222 uint32_t u32TransferBufferLength;
223 /** Frame to transmit an ISO frame. */
224 uint32_t u32StartFrame;
225 /** Number of isochronous packets. */
226 uint32_t u32NumIsocPkts;
227 /** Maximum time for the request on the server side host controller. */
228 uint32_t u32Interval;
229 /** Setup data for a control URB. */
230 VUSBSETUP Setup;
231} UsbIpReqSubmit;
232/** Pointer to a submit request. */
233typedef UsbIpReqSubmit *PUsbIpReqSubmit;
234#pragma pack()
235AssertCompileSize(UsbIpReqSubmit, 48);
236
237/**
238 * USB/IP Submit reply.
239 */
240#pragma pack(1)
241typedef struct UsbIpRetSubmit
242{
243 /** The reply header. */
244 UsbIpReqRetHdr Hdr;
245 /** Status code. */
246 int32_t u32Status;
247 /** Actual length of the reply buffer. */
248 uint32_t u32ActualLength;
249 /** The actual selected frame for a isochronous transmit. */
250 uint32_t u32StartFrame;
251 /** Number of isochronous packets. */
252 uint32_t u32NumIsocPkts;
253 /** Number of failed isochronous packets. */
254 uint32_t u32ErrorCount;
255 /** Setup data for a control URB. */
256 VUSBSETUP Setup;
257} UsbIpRetSubmit;
258/** Pointer to a submit reply. */
259typedef UsbIpRetSubmit *PUsbIpRetSubmit;
260#pragma pack()
261AssertCompileSize(UsbIpRetSubmit, 48);
262
263/**
264 * Unlink URB request.
265 */
266#pragma pack(1)
267typedef struct UsbIpReqUnlink
268{
269 /** The request header. */
270 UsbIpReqRetHdr Hdr;
271 /** The sequence number to unlink. */
272 uint32_t u32SeqNum;
273 /** Padding - unused. */
274 uint8_t abPadding[24];
275} UsbIpReqUnlink;
276/** Pointer to a URB unlink request. */
277typedef UsbIpReqUnlink *PUsbIpReqUnlink;
278#pragma pack()
279AssertCompileSize(UsbIpReqUnlink, 48);
280
281/**
282 * Unlink URB reply.
283 */
284#pragma pack(1)
285typedef struct UsbIpRetUnlink
286{
287 /** The reply header. */
288 UsbIpReqRetHdr Hdr;
289 /** Status of the request. */
290 int32_t u32Status;
291 /** Padding - unused. */
292 uint8_t abPadding[24];
293} UsbIpRetUnlink;
294/** Pointer to a URB unlink request. */
295typedef UsbIpRetUnlink *PUsbIpRetUnlink;
296#pragma pack()
297AssertCompileSize(UsbIpRetUnlink, 48);
298
299/**
300 * Union of possible replies from the server during normal operation.
301 */
302#pragma pack(1)
303typedef union UsbIpRet
304{
305 /** The header. */
306 UsbIpReqRetHdr Hdr;
307 /** Submit reply. */
308 UsbIpRetSubmit RetSubmit;
309 /** Unlink reply. */
310 UsbIpRetUnlink RetUnlink;
311 /** Byte view. */
312 uint8_t abReply[1];
313} UsbIpRet;
314/** Pointer to a reply union. */
315typedef UsbIpRet *PUsbIpRet;
316#pragma pack()
317
318/**
319 * Isochronous packet descriptor.
320*/
321#pragma pack(1)
322typedef struct UsbIpIsocPktDesc
323{
324 /** Offset */
325 uint32_t u32Offset;
326 /** Length of the packet including padding. */
327 uint32_t u32Length;
328 /** Size of the transmitted data. */
329 uint32_t u32ActualLength;
330 /** Completion status for this packet. */
331 int32_t i32Status;
332} UsbIpIsocPktDesc;
333/** Pointer to a isochronous packet descriptor. */
334typedef UsbIpIsocPktDesc *PUsbIpIsocPktDesc;
335#pragma pack()
336
337/**
338 * USB/IP backend specific data for one URB.
339 * Required for tracking in flight and landed URBs.
340 */
341typedef struct USBPROXYURBUSBIP
342{
343 /** List node for the in flight or landed URB list. */
344 RTLISTNODE NodeList;
345 /** Sequence number the assigned URB is identified by. */
346 uint32_t u32SeqNumUrb;
347 /** Sequence number of the unlink command if the URB was cancelled. */
348 uint32_t u32SeqNumUrbUnlink;
349 /** Flag whether the URB was cancelled. */
350 bool fCancelled;
351 /** Pointer to the VUSB URB. */
352 PVUSBURB pVUsbUrb;
353} USBPROXYURBUSBIP;
354/** Pointer to a USB/IP URB. */
355typedef USBPROXYURBUSBIP *PUSBPROXYURBUSBIP;
356
357/**
358 * USB/IP data receive states.
359 */
360typedef enum USBPROXYUSBIPRECVSTATE
361{
362 /** Invalid receive state. */
363 USBPROXYUSBIPRECVSTATE_INVALID = 0,
364 /** Currently receiving the common header structure. */
365 USBPROXYUSBIPRECVSTATE_HDR_COMMON,
366 /** Currently receieving the rest of the header structure. */
367 USBPROXYUSBIPRECVSTATE_HDR_RESIDUAL,
368 /** Currently receiving data into the URB buffer. */
369 USBPROXYUSBIPRECVSTATE_URB_BUFFER,
370 /** Currently receiving the isochronous packet descriptors. */
371 USBPROXYUSBIPRECVSTATE_ISOC_PKT_DESCS,
372 /** Usual 32bit hack. */
373 USBPROXYUSBIPRECVSTATE_32BIT_HACK = 0x7fffffff
374} USBPROXYUSBIPRECVSTATE;
375/** Pointer to an receive state. */
376typedef USBPROXYUSBIPRECVSTATE *PUSBPROXYUSBIPRECVSTATE;
377
378/**
379 * Backend data for the USB/IP USB Proxy device backend.
380 */
381typedef struct USBPROXYDEVUSBIP
382{
383 /** IPRT socket handle. */
384 RTSOCKET hSocket;
385 /** Pollset with the wakeup pipe and socket. */
386 RTPOLLSET hPollSet;
387 /** Pipe endpoint - read (in the pollset). */
388 RTPIPE hPipeR;
389 /** Pipe endpoint - write. */
390 RTPIPE hPipeW;
391 /** Next sequence number to use for identifying submitted URBs. */
392 volatile uint32_t u32SeqNumNext;
393 /** Fast mutex protecting the lists below against concurrent access. */
394 RTSEMFASTMUTEX hMtxLists;
395 /** List of in flight URBs. */
396 RTLISTANCHOR ListUrbsInFlight;
397 /** List of landed URBs. */
398 RTLISTANCHOR ListUrbsLanded;
399 /** List of URBs to submit. */
400 RTLISTANCHOR ListUrbsToQueue;
401 /** Port of the USB/IP host to connect to. */
402 uint32_t uPort;
403 /** USB/IP host address. */
404 char *pszHost;
405 /** USB Bus ID of the device to capture. */
406 char *pszBusId;
407 /** The device ID to use to identify the device. */
408 uint32_t u32DevId;
409 /** Temporary buffer for the next reply header */
410 UsbIpRet BufRet;
411 /** Temporary buffer to hold all isochronous packet descriptors. */
412 UsbIpIsocPktDesc aIsocPktDesc[8];
413 /** Pointer to the current buffer to write received data to. */
414 uint8_t *pbRecv;
415 /** Number of bytes received so far. */
416 size_t cbRecv;
417 /** Number of bytes left to receive. until we advance the state machine and process the data */
418 size_t cbLeft;
419 /** The current receiving state. */
420 USBPROXYUSBIPRECVSTATE enmRecvState;
421 /** The URB we currently receive a response for. */
422 PUSBPROXYURBUSBIP pUrbUsbIp;
423} USBPROXYDEVUSBIP, *PUSBPROXYDEVUSBIP;
424
425/** Pollset id of the socket. */
426#define USBIP_POLL_ID_SOCKET 0
427/** Pollset id of the pipe. */
428#define USBIP_POLL_ID_PIPE 1
429
430/** USB/IP address prefix for identifcation. */
431#define USBIP_URI_PREFIX "usbip://"
432/** USB/IP address prefix length. */
433#define USBIP_URI_PREFIX_LEN (sizeof(USBIP_URI_PREFIX) - 1)
434
435/** Waking reason for the USB I/P reaper: New URBs to queue. */
436#define USBIP_REAPER_WAKEUP_REASON_QUEUE 'Q'
437/** Waking reason for the USB I/P reaper: External wakeup. */
438#define USBIP_REAPER_WAKEUP_REASON_EXTERNAL 'E'
439
440/**
441 * Converts a request/reply header from network to host endianness.
442 *
443 * @returns nothing.
444 * @param pHdr The header to convert.
445 */
446DECLINLINE(void) usbProxyUsbIpReqRetHdrN2H(PUsbIpReqRetHdr pHdr)
447{
448 pHdr->u32ReqRet = RT_H2N_U32(pHdr->u32ReqRet);
449 pHdr->u32SeqNum = RT_H2N_U32(pHdr->u32SeqNum);
450 pHdr->u32DevId = RT_H2N_U32(pHdr->u32DevId);
451 pHdr->u32Direction = RT_H2N_U32(pHdr->u32Direction);
452 pHdr->u32Endpoint = RT_H2N_U32(pHdr->u32Endpoint);
453}
454
455/**
456 * Converts a request/reply header from host to network endianness.
457 *
458 * @returns nothing.
459 * @param pHdr The header to convert.
460 */
461DECLINLINE(void) usbProxyUsbIpReqRetHdrH2N(PUsbIpReqRetHdr pHdr)
462{
463 pHdr->u32ReqRet = RT_N2H_U32(pHdr->u32ReqRet);
464 pHdr->u32SeqNum = RT_N2H_U32(pHdr->u32SeqNum);
465 pHdr->u32DevId = RT_N2H_U32(pHdr->u32DevId);
466 pHdr->u32Direction = RT_N2H_U32(pHdr->u32Direction);
467 pHdr->u32Endpoint = RT_N2H_U32(pHdr->u32Endpoint);
468}
469
470/**
471 * Converts a submit request from host to network endianness.
472 *
473 * @returns nothing.
474 * @param pReqSubmit The submit request to convert.
475 */
476DECLINLINE(void) usbProxyUsbIpReqSubmitH2N(PUsbIpReqSubmit pReqSubmit)
477{
478 usbProxyUsbIpReqRetHdrH2N(&pReqSubmit->Hdr);
479 pReqSubmit->u32XferFlags = RT_H2N_U32(pReqSubmit->u32XferFlags);
480 pReqSubmit->u32TransferBufferLength = RT_H2N_U32(pReqSubmit->u32TransferBufferLength);
481 pReqSubmit->u32StartFrame = RT_H2N_U32(pReqSubmit->u32StartFrame);
482 pReqSubmit->u32NumIsocPkts = RT_H2N_U32(pReqSubmit->u32NumIsocPkts);
483 pReqSubmit->u32Interval = RT_H2N_U32(pReqSubmit->u32Interval);
484}
485
486/**
487 * Converts a submit reply from network to host endianness.
488 *
489 * @returns nothing.
490 * @param pReqSubmit The submit reply to convert.
491 */
492DECLINLINE(void) usbProxyUsbIpRetSubmitN2H(PUsbIpRetSubmit pRetSubmit)
493{
494 usbProxyUsbIpReqRetHdrN2H(&pRetSubmit->Hdr);
495 pRetSubmit->u32Status = RT_N2H_U32(pRetSubmit->u32Status);
496 pRetSubmit->u32ActualLength = RT_N2H_U32(pRetSubmit->u32ActualLength);
497 pRetSubmit->u32StartFrame = RT_N2H_U32(pRetSubmit->u32StartFrame);
498 pRetSubmit->u32NumIsocPkts = RT_N2H_U32(pRetSubmit->u32NumIsocPkts);
499 pRetSubmit->u32ErrorCount = RT_N2H_U32(pRetSubmit->u32ErrorCount);
500}
501
502/**
503 * Converts a isochronous packet descriptor from host to network endianness.
504 *
505 * @returns nothing.
506 * @param pIsocPktDesc The packet descriptor to convert.
507 */
508DECLINLINE(void) usbProxyUsbIpIsocPktDescH2N(PUsbIpIsocPktDesc pIsocPktDesc)
509{
510 pIsocPktDesc->u32Offset = RT_H2N_U32(pIsocPktDesc->u32Offset);
511 pIsocPktDesc->u32Length = RT_H2N_U32(pIsocPktDesc->u32Length);
512 pIsocPktDesc->u32ActualLength = RT_H2N_U32(pIsocPktDesc->u32ActualLength);
513 pIsocPktDesc->i32Status = RT_H2N_U32(pIsocPktDesc->i32Status);
514}
515
516/**
517 * Converts a isochronous packet descriptor from network to host endianness.
518 *
519 * @returns nothing.
520 * @param pIsocPktDesc The packet descriptor to convert.
521 */
522DECLINLINE(void) usbProxyUsbIpIsocPktDescN2H(PUsbIpIsocPktDesc pIsocPktDesc)
523{
524 pIsocPktDesc->u32Offset = RT_N2H_U32(pIsocPktDesc->u32Offset);
525 pIsocPktDesc->u32Length = RT_N2H_U32(pIsocPktDesc->u32Length);
526 pIsocPktDesc->u32ActualLength = RT_N2H_U32(pIsocPktDesc->u32ActualLength);
527 pIsocPktDesc->i32Status = RT_N2H_U32(pIsocPktDesc->i32Status);
528}
529
530/**
531 * Converts a unlink request from host to network endianness.
532 *
533 * @returns nothing.
534 * @param pReqUnlink The unlink request to convert.
535 */
536DECLINLINE(void) usbProxyUsbIpReqUnlinkH2N(PUsbIpReqUnlink pReqUnlink)
537{
538 usbProxyUsbIpReqRetHdrH2N(&pReqUnlink->Hdr);
539 pReqUnlink->u32SeqNum = RT_H2N_U32(pReqUnlink->u32SeqNum);
540}
541
542/**
543 * Converts a unlink reply from network to host endianness.
544 *
545 * @returns nothing.
546 * @param pRetUnlink The unlink reply to convert.
547 */
548DECLINLINE(void) usbProxyUsbIpRetUnlinkN2H(PUsbIpRetUnlink pRetUnlink)
549{
550 usbProxyUsbIpReqRetHdrN2H(&pRetUnlink->Hdr);
551 pRetUnlink->u32Status = RT_N2H_U32(pRetUnlink->u32Status);
552}
553
554/**
555 * Convert the given exported device structure from host to network byte order.
556 *
557 * @returns nothing.
558 * @param pDevice The device structure to convert.
559 */
560DECLINLINE(void) usbProxyUsbIpExportedDeviceN2H(PUsbIpExportedDevice pDevice)
561{
562 pDevice->u32BusNum = RT_N2H_U32(pDevice->u32BusNum);
563 pDevice->u32DevNum = RT_N2H_U32(pDevice->u32DevNum);
564 pDevice->u32Speed = RT_N2H_U16(pDevice->u32Speed);
565 pDevice->u16VendorId = RT_N2H_U16(pDevice->u16VendorId);
566 pDevice->u16ProductId = RT_N2H_U16(pDevice->u16ProductId);
567 pDevice->u16BcdDevice = RT_N2H_U16(pDevice->u16BcdDevice);
568}
569
570/**
571 * Converts a USB/IP status code to a VBox status code.
572 *
573 * @returns VUSB status code.
574 * @param i32Status The USB/IP status code from the reply.
575 */
576DECLINLINE(int) usbProxyUsbIpStatusConvertFromStatus(int32_t i32Status)
577{
578 if (RT_LIKELY( i32Status == USBIP_STATUS_SUCCESS
579 || i32Status == USBIP_STATUS_SHORT_READ))
580 return VINF_SUCCESS;
581
582 switch (i32Status)
583 {
584 case USBIP_STATUS_PIPE_STALLED:
585 return VINF_SUCCESS;
586 case USBIP_STATUS_URB_UNLINKED:
587 return VERR_TRY_AGAIN;
588 default:
589 LogFlowFunc(("i32Status=%d\n", i32Status));
590 return VERR_INVALID_STATE;
591 }
592
593 LogFlowFunc(("i32Status=%d\n", i32Status));
594 return VERR_INVALID_STATE;
595}
596
597/**
598 * Converts a USB/IP status code to a VUSB status code.
599 *
600 * @returns VUSB status code.
601 * @param i32Status The USB/IP status code from the reply.
602 */
603DECLINLINE(VUSBSTATUS) usbProxyUsbIpVUsbStatusConvertFromStatus(int32_t i32Status)
604{
605 if (RT_LIKELY( i32Status == USBIP_STATUS_SUCCESS
606 || i32Status == USBIP_STATUS_SHORT_READ))
607 return VUSBSTATUS_OK;
608
609 switch (i32Status)
610 {
611 case USBIP_STATUS_PIPE_STALLED:
612 return VUSBSTATUS_STALL;
613 default:
614 return VUSBSTATUS_DNR;
615 }
616
617 return VUSBSTATUS_DNR;
618}
619
620/**
621 * Gets the next free sequence number.
622 *
623 * @returns Next free sequence number.
624 * @param pProxyDevUsbIp The USB/IP proxy device data.
625 */
626DECLINLINE(uint32_t) usbProxyUsbIpSeqNumGet(PUSBPROXYDEVUSBIP pProxyDevUsbIp)
627{
628 return ASMAtomicIncU32(&pProxyDevUsbIp->u32SeqNumNext);
629}
630
631/**
632 * Links a given URB into the given list.
633 *
634 * @returns nothing.
635 * @param pProxyDevUsbIp The USB/IP proxy device data.
636 * @param pList The list to link the URB into.
637 * @param pUrbUsbIp The URB to link.
638 */
639DECLINLINE(void) usbProxyUsbIpLinkUrb(PUSBPROXYDEVUSBIP pProxyDevUsbIp, PRTLISTANCHOR pList, PUSBPROXYURBUSBIP pUrbUsbIp)
640{
641 int rc = RTSemFastMutexRequest(pProxyDevUsbIp->hMtxLists);
642 AssertRC(rc);
643 RTListAppend(pList, &pUrbUsbIp->NodeList);
644 RTSemFastMutexRelease(pProxyDevUsbIp->hMtxLists);
645}
646
647/**
648 * Unlinks a given URB from the current assigned list.
649 *
650 * @returns nothing.
651 * @param pProxyDevUsbIp The USB/IP proxy device data.
652 * @param pUrbUsbIp The URB to unlink.
653 */
654DECLINLINE(void) usbProxyUsbIpUnlinkUrb(PUSBPROXYDEVUSBIP pProxyDevUsbIp, PUSBPROXYURBUSBIP pUrbUsbIp)
655{
656 int rc = RTSemFastMutexRequest(pProxyDevUsbIp->hMtxLists);
657 AssertRC(rc);
658 RTListNodeRemove(&pUrbUsbIp->NodeList);
659 RTSemFastMutexRelease(pProxyDevUsbIp->hMtxLists);
660}
661
662/**
663 * Allocates a USB/IP proxy specific URB state.
664 *
665 * @returns Pointer to the USB/IP specific URB data or NULL on failure.
666 * @param pProxyDevUsbIp The USB/IP proxy device data.
667 */
668static PUSBPROXYURBUSBIP usbProxyUsbIpUrbAlloc(PUSBPROXYDEVUSBIP pProxyDevUsbIp)
669{
670 NOREF(pProxyDevUsbIp);
671 return (PUSBPROXYURBUSBIP)RTMemAllocZ(sizeof(USBPROXYURBUSBIP));
672}
673
674/**
675 * Frees the given USB/IP URB state.
676 *
677 * @returns nothing.
678 * @param pProxyDevUsbIp The USB/IP proxy device data.
679 * @param pUrbUsbIp The USB/IP speciic URB data.
680 */
681static void usbProxyUsbIpUrbFree(PUSBPROXYDEVUSBIP pProxyDevUsbIp, PUSBPROXYURBUSBIP pUrbUsbIp)
682{
683 NOREF(pProxyDevUsbIp);
684 RTMemFree(pUrbUsbIp);
685}
686
687/**
688 * Parse the string representation of the host address.
689 *
690 * @returns VBox status code.
691 * @param pProxyDevUsbIp The USB/IP proxy device data to parse the address for.
692 * @param pszAddress The address string to parse.
693 */
694static int usbProxyUsbIpParseAddress(PUSBPROXYDEVUSBIP pProxyDevUsbIp, const char *pszAddress)
695{
696 int rc = VINF_SUCCESS;
697
698 if (!RTStrNCmp(pszAddress, USBIP_URI_PREFIX, USBIP_URI_PREFIX_LEN))
699 {
700 pszAddress += USBIP_URI_PREFIX_LEN;
701
702 const char *pszPortStart = RTStrStr(pszAddress, ":");
703 if (pszPortStart)
704 {
705 pszPortStart++;
706
707 const char *pszBusIdStart = RTStrStr(pszPortStart, ":");
708 if (pszBusIdStart)
709 {
710 size_t cbHost = pszPortStart - pszAddress - 1;
711 size_t cbBusId = strlen(pszBusIdStart);
712
713 pszBusIdStart++;
714
715 rc = RTStrToUInt32Ex(pszPortStart, NULL, 10 /* uBase */, &pProxyDevUsbIp->uPort);
716 if ( rc == VINF_SUCCESS
717 || rc == VWRN_TRAILING_CHARS)
718 {
719 rc = RTStrAllocEx(&pProxyDevUsbIp->pszHost, cbHost + 1);
720 if (RT_SUCCESS(rc))
721 rc = RTStrAllocEx(&pProxyDevUsbIp->pszBusId, cbBusId + 1);
722 if (RT_SUCCESS(rc))
723 {
724 rc = RTStrCopyEx(pProxyDevUsbIp->pszHost, cbHost + 1, pszAddress, cbHost);
725 AssertRC(rc);
726
727 rc = RTStrCopyEx(pProxyDevUsbIp->pszBusId, cbBusId + 1, pszBusIdStart, cbBusId);
728 AssertRC(rc);
729
730 return VINF_SUCCESS;
731 }
732 }
733 else
734 rc = VERR_INVALID_PARAMETER;
735 }
736 else
737 rc = VERR_INVALID_PARAMETER;
738 }
739 else
740 rc = VERR_INVALID_PARAMETER;
741 }
742 else
743 rc = VERR_INVALID_PARAMETER;
744
745 return rc;
746}
747
748/**
749 * Connects to the USB/IP host and claims the device given in the proxy device data.
750 *
751 * @returns VBox status code.
752 * @param pProxyDevUsbIp The USB/IP proxy device data.
753 */
754static int usbProxyUsbIpConnect(PUSBPROXYDEVUSBIP pProxyDevUsbIp)
755{
756 int rc = VINF_SUCCESS;
757 rc = RTTcpClientConnect(pProxyDevUsbIp->pszHost, pProxyDevUsbIp->uPort, &pProxyDevUsbIp->hSocket);
758 if (RT_SUCCESS(rc))
759 {
760 /* Disable send coalescing. */
761 rc = RTTcpSetSendCoalescing(pProxyDevUsbIp->hSocket, false);
762 if (RT_FAILURE(rc))
763 LogRel(("UsbIp: Disabling send coalescing failed (rc=%Rrc), continuing nevertheless but expect reduced performance\n", rc));
764
765 /* Import the device, i.e. claim it for our use. */
766 UsbIpReqImport ReqImport;
767 ReqImport.u16Version = RT_H2N_U16(USBIP_VERSION);
768 ReqImport.u16Cmd = RT_H2N_U16(USBIP_INDICATOR_REQ | USBIP_REQ_RET_IMPORT);
769 ReqImport.u32Status = RT_H2N_U32(USBIP_STATUS_SUCCESS);
770 rc = RTStrCopy(&ReqImport.aszBusId[0], sizeof(ReqImport.aszBusId), pProxyDevUsbIp->pszBusId);
771 if (rc == VINF_SUCCESS)
772 {
773 rc = RTTcpWrite(pProxyDevUsbIp->hSocket, &ReqImport, sizeof(ReqImport));
774 if (RT_SUCCESS(rc))
775 {
776 /* Read the reply. */
777 UsbIpRetImport RetImport;
778 rc = RTTcpRead(pProxyDevUsbIp->hSocket, &RetImport, sizeof(RetImport), NULL);
779 if (RT_SUCCESS(rc))
780 {
781 RetImport.u16Version = RT_N2H_U16(RetImport.u16Version);
782 RetImport.u16Cmd = RT_N2H_U16(RetImport.u16Cmd);
783 RetImport.u32Status = RT_N2H_U32(RetImport.u32Status);
784 if ( RetImport.u16Version == USBIP_VERSION
785 && RetImport.u16Cmd == USBIP_REQ_RET_IMPORT
786 && RetImport.u32Status == USBIP_STATUS_SUCCESS)
787 {
788 /* Read the device data. */
789 UsbIpExportedDevice Device;
790 rc = RTTcpRead(pProxyDevUsbIp->hSocket, &Device, sizeof(Device), NULL);
791 if (RT_SUCCESS(rc))
792 {
793 usbProxyUsbIpExportedDeviceN2H(&Device);
794 pProxyDevUsbIp->u32DevId = (Device.u32BusNum << 16) | Device.u32DevNum;
795
796 rc = RTPollSetAddSocket(pProxyDevUsbIp->hPollSet, pProxyDevUsbIp->hSocket,
797 RTPOLL_EVT_READ | RTPOLL_EVT_ERROR, USBIP_POLL_ID_SOCKET);
798 }
799 }
800 else
801 {
802 /* Check what went wrong and leave a meaningful error message in the log. */
803 if (RetImport.u16Version != USBIP_VERSION)
804 LogRel(("UsbIp: Unexpected protocol version received from host (%#x vs. %#x)\n",
805 RetImport.u16Version, USBIP_VERSION));
806 else if (RetImport.u16Cmd != USBIP_REQ_RET_IMPORT)
807 LogRel(("UsbIp: Unexpected reply code received from host (%#x vs. %#x)\n",
808 RetImport.u16Cmd, USBIP_REQ_RET_IMPORT));
809 else if (RetImport.u32Status != 0)
810 LogRel(("UsbIp: Claiming the device has failed on the host with an unspecified error\n"));
811 else
812 AssertMsgFailed(("Something went wrong with if condition\n"));
813 }
814 }
815 }
816 }
817 else
818 {
819 LogRel(("UsbIp: Given bus ID is exceeds permitted protocol length: %u vs %u\n",
820 strlen(pProxyDevUsbIp->pszBusId) + 1, sizeof(ReqImport.aszBusId)));
821 rc = VERR_INVALID_PARAMETER;
822 }
823
824 if (RT_FAILURE(rc))
825 RTTcpClientCloseEx(pProxyDevUsbIp->hSocket, false /*fGracefulShutdown*/);
826 }
827 if (RT_FAILURE(rc))
828 LogRel(("UsbIp: Connecting to the host %s failed with %Rrc\n", pProxyDevUsbIp->pszHost, rc));
829 return rc;
830}
831
832/**
833 * Disconnects from the USB/IP host releasing the device given in the proxy device data.
834 *
835 * @returns VBox status code.
836 * @param pProxyDevUsbIp The USB/IP proxy device data.
837 */
838static int usbProxyUsbIpDisconnect(PUSBPROXYDEVUSBIP pProxyDevUsbIp)
839{
840 int rc = VINF_SUCCESS;
841
842 rc = RTTcpClientCloseEx(pProxyDevUsbIp->hSocket, false /*fGracefulShutdown*/);
843 if (RT_SUCCESS(rc))
844 pProxyDevUsbIp->hSocket = NIL_RTSOCKET;
845 return rc;
846}
847
848/**
849 * Synchronously exchange a given control message with the remote device.
850 *
851 * @eturns VBox status code.
852 * @param pProxyDevUsbIp The USB/IP proxy device data.
853 * @param pSetup The setup message.
854 *
855 * @note This method is only used to implement the *SetConfig, *SetInterface and *ClearHaltedEp
856 * callbacks because the USB/IP protocol lacks dedicated requests for these.
857 * @remark It is assumed that this method is never called while usbProxyUsbIpUrbReap is called
858 * on another thread.
859 */
860static int usbProxyUsbIpCtrlUrbExchangeSync(PUSBPROXYDEVUSBIP pProxyDevUsbIp, PVUSBSETUP pSetup)
861{
862 int rc = VINF_SUCCESS;
863 unsigned iTry = 0;
864
865 UsbIpReqSubmit ReqSubmit;
866
867 RT_ZERO(ReqSubmit);
868
869 uint32_t u32SeqNum = usbProxyUsbIpSeqNumGet(pProxyDevUsbIp);
870 ReqSubmit.Hdr.u32ReqRet = USBIP_CMD_SUBMIT;
871 ReqSubmit.Hdr.u32SeqNum = u32SeqNum;
872 ReqSubmit.Hdr.u32DevId = pProxyDevUsbIp->u32DevId;
873 ReqSubmit.Hdr.u32Direction = USBIP_DIR_OUT;
874 ReqSubmit.Hdr.u32Endpoint = 0; /* Only default control endpoint is allowed for these kind of messages. */
875 ReqSubmit.u32XferFlags = 0;
876 ReqSubmit.u32TransferBufferLength = 0;
877 ReqSubmit.u32StartFrame = 0;
878 ReqSubmit.u32NumIsocPkts = 0;
879 ReqSubmit.u32Interval = 0;
880 memcpy(&ReqSubmit.Setup, pSetup, sizeof(ReqSubmit.Setup));
881 usbProxyUsbIpReqSubmitH2N(&ReqSubmit);
882
883 do
884 {
885 /* Send the command. */
886 rc = RTTcpWrite(pProxyDevUsbIp->hSocket, &ReqSubmit, sizeof(ReqSubmit));
887 if (RT_SUCCESS(rc))
888 {
889 /* Wait for the response. */
890 /** @todo: Don't wait indefinitely long. */
891 UsbIpRetSubmit RetSubmit;
892 rc = RTTcpRead(pProxyDevUsbIp->hSocket, &RetSubmit, sizeof(RetSubmit), NULL);
893 if (RT_SUCCESS(rc))
894 {
895 usbProxyUsbIpRetSubmitN2H(&RetSubmit);
896 rc = usbProxyUsbIpStatusConvertFromStatus(RetSubmit.u32Status);
897 }
898 }
899 } while ( rc == VERR_TRY_AGAIN
900 && iTry++ < 10);
901 return rc;
902}
903
904/**
905 * Returns the URB matching the given sequence number from the in flight list.
906 *
907 * @returns pointer to the URB matching the given sequence number or NULL
908 * @param pProxyDevUsbIp The USB/IP proxy device data.
909 * @param u32SeqNum The sequence number to search for.
910 */
911static PUSBPROXYURBUSBIP usbProxyUsbIpGetInFlightUrbFromSeqNum(PUSBPROXYDEVUSBIP pProxyDevUsbIp, uint32_t u32SeqNum)
912{
913 bool fFound = false;
914 PUSBPROXYURBUSBIP pIt;
915
916 RTListForEach(&pProxyDevUsbIp->ListUrbsInFlight, pIt, USBPROXYURBUSBIP, NodeList)
917 {
918 if (pIt->u32SeqNumUrb == u32SeqNum)
919 {
920 fFound = true;
921 break;
922 }
923 }
924
925 return fFound ? pIt : NULL;
926}
927
928/**
929 * Returns the URB matching the given sequence number from the cancel list.
930 *
931 * @returns pointer to the URB matching the given sequence number or NULL
932 * @param pProxyDevUsbIp The USB/IP proxy device data.
933 * @param u32SeqNum The sequence number to search for.
934 */
935static PUSBPROXYURBUSBIP usbProxyUsbIpGetCancelledUrbFromSeqNum(PUSBPROXYDEVUSBIP pProxyDevUsbIp, uint32_t u32SeqNum)
936{
937 bool fFound = false;
938 PUSBPROXYURBUSBIP pIt;
939
940 RTListForEach(&pProxyDevUsbIp->ListUrbsInFlight, pIt, USBPROXYURBUSBIP, NodeList)
941 {
942 if ( pIt->u32SeqNumUrbUnlink == u32SeqNum
943 && pIt->fCancelled == true)
944 {
945 fFound = true;
946 break;
947 }
948 }
949
950 return fFound ? pIt : NULL;
951}
952
953/**
954 * Resets the receive state for a new reply.
955 *
956 * @returns nothing.
957 * @param pProxyDevUsbIp The USB/IP proxy device data.
958 */
959static void usbProxyUsbIpResetRecvState(PUSBPROXYDEVUSBIP pProxyDevUsbIp)
960{
961 pProxyDevUsbIp->enmRecvState = USBPROXYUSBIPRECVSTATE_HDR_COMMON;
962 pProxyDevUsbIp->pbRecv = (uint8_t *)&pProxyDevUsbIp->BufRet;
963 pProxyDevUsbIp->cbRecv = 0;
964 pProxyDevUsbIp->cbLeft = sizeof(UsbIpReqRetHdr);
965}
966
967static void usbProxyUsbIpRecvStateAdvance(PUSBPROXYDEVUSBIP pProxyDevUsbIp, USBPROXYUSBIPRECVSTATE enmState,
968 uint8_t *pbData, size_t cbData)
969{
970 pProxyDevUsbIp->enmRecvState = enmState;
971 pProxyDevUsbIp->cbRecv = 0;
972 pProxyDevUsbIp->cbLeft = cbData;
973 pProxyDevUsbIp->pbRecv = pbData;
974}
975
976/**
977 * Handles reception of a USB/IP PDU.
978 *
979 * @returns VBox status code.
980 * @param pProxyDevUsbIp The USB/IP proxy device data.
981 * @param ppUrbUsbIp Where to store the pointer to the USB/IP URB which completed.
982 * Will be NULL if the received PDU is not complete and we have
983 * have to wait for more data or on failure.
984 */
985static int usbProxyUsbIpRecvPdu(PUSBPROXYDEVUSBIP pProxyDevUsbIp, PUSBPROXYURBUSBIP *ppUrbUsbIp)
986{
987 int rc = VINF_SUCCESS;
988 size_t cbRead = 0;
989 PUSBPROXYURBUSBIP pUrbUsbIp = NULL;
990
991 Assert(pProxyDevUsbIp->cbLeft);
992
993 /* Read any available data first. */
994 rc = RTTcpReadNB(pProxyDevUsbIp->hSocket, pProxyDevUsbIp->pbRecv, pProxyDevUsbIp->cbLeft, &cbRead);
995 if (RT_SUCCESS(rc))
996 {
997 pProxyDevUsbIp->cbRecv += cbRead;
998 pProxyDevUsbIp->cbLeft -= cbRead;
999 pProxyDevUsbIp->pbRecv += cbRead;
1000
1001 /* Process the received data if there is nothing to receive left for the current state. */
1002 if (!pProxyDevUsbIp->cbLeft)
1003 {
1004 switch (pProxyDevUsbIp->enmRecvState)
1005 {
1006 case USBPROXYUSBIPRECVSTATE_HDR_COMMON:
1007 {
1008 Assert(pProxyDevUsbIp->cbRecv == sizeof(UsbIpReqRetHdr));
1009
1010 /*
1011 * Determine the residual amount of data to receive until
1012 * the complete reply header was received.
1013 */
1014 switch (RT_N2H_U32(pProxyDevUsbIp->BufRet.Hdr.u32ReqRet))
1015 {
1016 case USBIP_RET_SUBMIT:
1017 pProxyDevUsbIp->cbLeft = sizeof(UsbIpRetSubmit) - sizeof(UsbIpReqRetHdr);
1018 pProxyDevUsbIp->enmRecvState = USBPROXYUSBIPRECVSTATE_HDR_RESIDUAL;
1019 break;
1020 case USBIP_RET_UNLINK:
1021 pProxyDevUsbIp->cbLeft = sizeof(UsbIpRetUnlink) - sizeof(UsbIpReqRetHdr);
1022 pProxyDevUsbIp->enmRecvState = USBPROXYUSBIPRECVSTATE_HDR_RESIDUAL;
1023 break;
1024 default:
1025 AssertLogRelMsgFailed(("Invalid reply header received: %d\n",
1026 pProxyDevUsbIp->BufRet.Hdr.u32ReqRet));
1027 usbProxyUsbIpResetRecvState(pProxyDevUsbIp);
1028 }
1029
1030 break;
1031 }
1032 case USBPROXYUSBIPRECVSTATE_HDR_RESIDUAL:
1033 {
1034 /** @todo: Verify that the directions match, verify that the length doesn't exceed the buffer. */
1035
1036 switch (RT_N2H_U32(pProxyDevUsbIp->BufRet.Hdr.u32ReqRet))
1037 {
1038 case USBIP_RET_SUBMIT:
1039 /* Get the URB from the in flight list. */
1040 pProxyDevUsbIp->pUrbUsbIp = usbProxyUsbIpGetInFlightUrbFromSeqNum(pProxyDevUsbIp, RT_N2H_U32(pProxyDevUsbIp->BufRet.Hdr.u32SeqNum));
1041 if (pProxyDevUsbIp->pUrbUsbIp)
1042 {
1043 usbProxyUsbIpRetSubmitN2H(&pProxyDevUsbIp->BufRet.RetSubmit);
1044
1045 /* We still have to receive the transfer buffer, even in case of an error. */
1046 pProxyDevUsbIp->pUrbUsbIp->pVUsbUrb->enmStatus = usbProxyUsbIpVUsbStatusConvertFromStatus(pProxyDevUsbIp->BufRet.RetSubmit.u32Status);
1047 if (pProxyDevUsbIp->pUrbUsbIp->pVUsbUrb->enmDir == VUSBDIRECTION_IN)
1048 {
1049 uint8_t *pbData = NULL;
1050
1051 if (pProxyDevUsbIp->pUrbUsbIp->pVUsbUrb->enmType == VUSBXFERTYPE_MSG)
1052 {
1053 /* Preserve the setup request. */
1054 pbData = &pProxyDevUsbIp->pUrbUsbIp->pVUsbUrb->abData[sizeof(VUSBSETUP)];
1055 pProxyDevUsbIp->pUrbUsbIp->pVUsbUrb->cbData = pProxyDevUsbIp->BufRet.RetSubmit.u32ActualLength + sizeof(VUSBSETUP);
1056 }
1057 else
1058 {
1059 pbData = &pProxyDevUsbIp->pUrbUsbIp->pVUsbUrb->abData[0];
1060 pProxyDevUsbIp->pUrbUsbIp->pVUsbUrb->cbData = pProxyDevUsbIp->BufRet.RetSubmit.u32ActualLength;
1061 }
1062
1063 if (pProxyDevUsbIp->BufRet.RetSubmit.u32ActualLength)
1064 usbProxyUsbIpRecvStateAdvance(pProxyDevUsbIp, USBPROXYUSBIPRECVSTATE_URB_BUFFER,
1065 pbData, pProxyDevUsbIp->BufRet.RetSubmit.u32ActualLength);
1066 else
1067 {
1068 pUrbUsbIp = pProxyDevUsbIp->pUrbUsbIp;
1069 usbProxyUsbIpResetRecvState(pProxyDevUsbIp);
1070 }
1071 }
1072 else
1073 {
1074 Assert(pProxyDevUsbIp->pUrbUsbIp->pVUsbUrb->enmDir == VUSBDIRECTION_OUT);
1075 pUrbUsbIp = pProxyDevUsbIp->pUrbUsbIp;
1076 usbProxyUsbIpResetRecvState(pProxyDevUsbIp);
1077 }
1078 }
1079 else
1080 {
1081 LogRel(("USB/IP: Received reply with sequence number %u doesn't match any local URB\n", pProxyDevUsbIp->BufRet.Hdr.u32SeqNum));
1082 usbProxyUsbIpResetRecvState(pProxyDevUsbIp);
1083 }
1084 break;
1085 case USBIP_RET_UNLINK:
1086 pProxyDevUsbIp->pUrbUsbIp = usbProxyUsbIpGetCancelledUrbFromSeqNum(pProxyDevUsbIp, RT_N2H_U32(pProxyDevUsbIp->BufRet.Hdr.u32SeqNum));
1087 if (pProxyDevUsbIp->pUrbUsbIp)
1088 {
1089 usbProxyUsbIpRetUnlinkN2H(&pProxyDevUsbIp->BufRet.RetUnlink);
1090 pUrbUsbIp = pProxyDevUsbIp->pUrbUsbIp;
1091 pUrbUsbIp->pVUsbUrb->enmStatus = usbProxyUsbIpVUsbStatusConvertFromStatus(pProxyDevUsbIp->BufRet.RetUnlink.u32Status);
1092 }
1093 /* else: Probably received the data for the URB and is complete already. */
1094
1095 usbProxyUsbIpResetRecvState(pProxyDevUsbIp);
1096 break;
1097 }
1098
1099 break;
1100 }
1101 case USBPROXYUSBIPRECVSTATE_URB_BUFFER:
1102 if (pProxyDevUsbIp->pUrbUsbIp->pVUsbUrb->enmType == VUSBXFERTYPE_ISOC)
1103 usbProxyUsbIpRecvStateAdvance(pProxyDevUsbIp, USBPROXYUSBIPRECVSTATE_ISOC_PKT_DESCS,
1104 (uint8_t *)&pProxyDevUsbIp->aIsocPktDesc[0],
1105 pProxyDevUsbIp->pUrbUsbIp->pVUsbUrb->cIsocPkts * sizeof(UsbIpIsocPktDesc));
1106 else
1107 {
1108 pUrbUsbIp = pProxyDevUsbIp->pUrbUsbIp;
1109 usbProxyUsbIpResetRecvState(pProxyDevUsbIp);
1110 }
1111 break;
1112 case USBPROXYUSBIPRECVSTATE_ISOC_PKT_DESCS:
1113 /* Process all received isochronous packet descriptors. */
1114 for (unsigned i = 0; i < pProxyDevUsbIp->pUrbUsbIp->pVUsbUrb->cIsocPkts; i++)
1115 {
1116 PVUSBURBISOCPTK pIsocPkt = &pProxyDevUsbIp->pUrbUsbIp->pVUsbUrb->aIsocPkts[i];
1117 usbProxyUsbIpIsocPktDescN2H(&pProxyDevUsbIp->aIsocPktDesc[i]);
1118 pIsocPkt->enmStatus = usbProxyUsbIpVUsbStatusConvertFromStatus(pProxyDevUsbIp->aIsocPktDesc[i].i32Status);
1119 pIsocPkt->off = pProxyDevUsbIp->aIsocPktDesc[i].u32Offset;
1120 pIsocPkt->cb = pProxyDevUsbIp->aIsocPktDesc[i].u32ActualLength;
1121 }
1122
1123 pUrbUsbIp = pProxyDevUsbIp->pUrbUsbIp;
1124 usbProxyUsbIpResetRecvState(pProxyDevUsbIp);
1125 break;
1126 default:
1127 AssertLogRelMsgFailed(("USB/IP: Invalid receive state %d\n", pProxyDevUsbIp->enmRecvState));
1128 }
1129 }
1130 }
1131 else
1132 {
1133 /** @todo: Complete all URBs with DNR error and mark device as unplugged. */
1134#if 0
1135 pUrbUsbIp = pProxyDevUsbIp->pUrbUsbIp;
1136 pUrbUsbIp->pVUsbUrb->enmStatus = VUSBSTATUS_DNR;
1137 usbProxyUsbIpResetRecvState(pProxyDevUsbIp);
1138#endif
1139 }
1140
1141 if (RT_SUCCESS(rc))
1142 *ppUrbUsbIp = pUrbUsbIp;
1143
1144 return rc;
1145}
1146
1147/**
1148 * Worker for queueing an URB on the main I/O thread.
1149 *
1150 * @returns VBox status code.
1151 * @param pProxyDevUsbIp The USB/IP proxy device data.
1152 * @param pUrbUsbIp The USB/IP URB to queue.
1153 */
1154static int usbProxyUsbIpUrbQueueWorker(PUSBPROXYDEVUSBIP pProxyDevUsbIp, PUSBPROXYURBUSBIP pUrbUsbIp)
1155{
1156 PVUSBURB pUrb = pUrbUsbIp->pVUsbUrb;
1157
1158 pUrbUsbIp->u32SeqNumUrb = usbProxyUsbIpSeqNumGet(pProxyDevUsbIp);
1159
1160 UsbIpReqSubmit ReqSubmit;
1161
1162 RT_ZERO(ReqSubmit);
1163 ReqSubmit.Hdr.u32ReqRet = USBIP_CMD_SUBMIT;
1164 ReqSubmit.Hdr.u32SeqNum = pUrbUsbIp->u32SeqNumUrb;
1165 ReqSubmit.Hdr.u32DevId = pProxyDevUsbIp->u32DevId;
1166 ReqSubmit.Hdr.u32Endpoint = pUrb->EndPt;
1167 ReqSubmit.Hdr.u32Direction = pUrb->enmDir == VUSBDIRECTION_IN ? USBIP_DIR_IN : USBIP_DIR_OUT;
1168 ReqSubmit.u32XferFlags = 0;
1169 if (pUrb->enmDir == VUSBDIRECTION_IN && pUrb->fShortNotOk)
1170 ReqSubmit.u32XferFlags |= USBIP_XFER_FLAGS_SHORT_NOT_OK;
1171
1172 ReqSubmit.u32TransferBufferLength = pUrb->cbData;
1173 ReqSubmit.u32StartFrame = 0;
1174 ReqSubmit.u32NumIsocPkts = 0;
1175 ReqSubmit.u32Interval = 0;
1176
1177 RTSGSEG aSegReq[3]; /* Maximum number of segments used for a Isochronous transfer. */
1178 UsbIpIsocPktDesc aIsocPktsDesc[8];
1179 unsigned cSegsUsed = 1;
1180 aSegReq[0].pvSeg = &ReqSubmit;
1181 aSegReq[0].cbSeg = sizeof(ReqSubmit);
1182
1183 switch (pUrb->enmType)
1184 {
1185 case VUSBXFERTYPE_MSG:
1186 memcpy(&ReqSubmit.Setup, &pUrb->abData, sizeof(ReqSubmit.Setup));
1187 ReqSubmit.u32TransferBufferLength -= sizeof(VUSBSETUP);
1188 if (pUrb->enmDir == VUSBDIRECTION_OUT)
1189 {
1190 aSegReq[cSegsUsed].cbSeg = pUrb->cbData - sizeof(VUSBSETUP);
1191 aSegReq[cSegsUsed].pvSeg = pUrb->abData + sizeof(VUSBSETUP);
1192 if (aSegReq[cSegsUsed].cbSeg)
1193 cSegsUsed++;
1194 }
1195 LogFlowFunc(("Message (Control) URB\n"));
1196 break;
1197 case VUSBXFERTYPE_ISOC:
1198 LogFlowFunc(("Isochronous URB\n"));
1199 ReqSubmit.u32XferFlags |= USBIP_XFER_FLAGS_ISO_ASAP;
1200 ReqSubmit.u32NumIsocPkts = pUrb->cIsocPkts;
1201 if (pUrb->enmDir == VUSBDIRECTION_OUT)
1202 {
1203 aSegReq[cSegsUsed].cbSeg = pUrb->cbData;
1204 aSegReq[cSegsUsed].pvSeg = pUrb->abData;
1205 cSegsUsed++;
1206 }
1207
1208 for (unsigned i = 0; i < pUrb->cIsocPkts; i++)
1209 {
1210 aIsocPktsDesc[i].u32Offset = pUrb->aIsocPkts[i].off;
1211 aIsocPktsDesc[i].u32Length = pUrb->aIsocPkts[i].cb;
1212 aIsocPktsDesc[i].u32ActualLength = 0; /** @todo */
1213 aIsocPktsDesc[i].i32Status = pUrb->aIsocPkts[i].enmStatus;
1214 usbProxyUsbIpIsocPktDescH2N(&aIsocPktsDesc[i]);
1215 }
1216
1217 if (pUrb->cIsocPkts)
1218 {
1219 aSegReq[cSegsUsed].cbSeg = pUrb->cIsocPkts * sizeof(UsbIpIsocPktDesc);
1220 aSegReq[cSegsUsed].pvSeg = &aIsocPktsDesc[0];
1221 cSegsUsed++;
1222 }
1223
1224 break;
1225 case VUSBXFERTYPE_BULK:
1226 case VUSBXFERTYPE_INTR:
1227 LogFlowFunc(("Bulk URB\n"));
1228 if (pUrb->enmDir == VUSBDIRECTION_OUT)
1229 {
1230 aSegReq[cSegsUsed].cbSeg = pUrb->cbData;
1231 aSegReq[cSegsUsed].pvSeg = pUrb->abData;
1232 cSegsUsed++;
1233 }
1234 break;
1235 default:
1236 usbProxyUsbIpUrbFree(pProxyDevUsbIp, pUrbUsbIp);
1237 return VERR_INVALID_PARAMETER; /** @todo: better status code. */
1238 }
1239
1240 usbProxyUsbIpReqSubmitH2N(&ReqSubmit);
1241
1242 Assert(cSegsUsed <= RT_ELEMENTS(aSegReq));
1243
1244 /* Send the command. */
1245 RTSGBUF SgBufReq;
1246 RTSgBufInit(&SgBufReq, &aSegReq[0], cSegsUsed);
1247
1248 int rc = RTTcpSgWrite(pProxyDevUsbIp->hSocket, &SgBufReq);
1249 if (RT_SUCCESS(rc))
1250 {
1251 /* Link the URB into the list of in flight URBs. */
1252 usbProxyUsbIpLinkUrb(pProxyDevUsbIp, &pProxyDevUsbIp->ListUrbsInFlight, pUrbUsbIp);
1253 }
1254
1255 return rc;
1256}
1257
1258/**
1259 * Queues all pending URBs from the list.
1260 *
1261 * @returns VBox status code.
1262 * @param pProxyDevUsbIp The USB/IP proxy device data.
1263 */
1264static int usbProxyUsbIpUrbsQueuePending(PUSBPROXYDEVUSBIP pProxyDevUsbIp)
1265{
1266 RTLISTANCHOR ListUrbsPending;
1267
1268 RTListInit(&ListUrbsPending);
1269 int rc = RTSemFastMutexRequest(pProxyDevUsbIp->hMtxLists);
1270 AssertRC(rc);
1271 RTListMove(&ListUrbsPending, &pProxyDevUsbIp->ListUrbsToQueue);
1272 RTSemFastMutexRelease(pProxyDevUsbIp->hMtxLists);
1273
1274 PUSBPROXYURBUSBIP pIter = NULL;
1275 PUSBPROXYURBUSBIP pIterNext = NULL;
1276 RTListForEachSafe(&ListUrbsPending, pIter, pIterNext, USBPROXYURBUSBIP, NodeList)
1277 {
1278 RTListNodeRemove(&pIter->NodeList);
1279 rc = usbProxyUsbIpUrbQueueWorker(pProxyDevUsbIp, pIter);
1280 if (RT_FAILURE(rc))
1281 {
1282 /** @todo: Complete the URB with an error. */
1283 usbProxyUsbIpUrbFree(pProxyDevUsbIp, pIter);
1284 }
1285 }
1286
1287 return VINF_SUCCESS;
1288}
1289
1290/**
1291 * Kick the reaper thread.
1292 *
1293 * @returns VBox status code.
1294 * @param pProxyDevUsbIp The USB/IP proxy device data.
1295 * @param bReason The wakeup reason.
1296 */
1297static char usbProxyReaperKick(PUSBPROXYDEVUSBIP pProxyDevUsbIp, char bReason)
1298{
1299 int rc = VINF_SUCCESS;
1300 size_t cbWritten = 0;
1301
1302 rc = RTPipeWrite(pProxyDevUsbIp->hPipeW, &bReason, 1, &cbWritten);
1303 Assert(RT_SUCCESS(rc) || cbWritten == 0);
1304
1305 return rc;
1306}
1307
1308/**
1309 * Drain the wakeup pipe.
1310 *
1311 * @returns Wakeup reason.
1312 * @param pProxyDevUsbIp The USB/IP proxy device data.
1313 */
1314static char usbProxyUsbIpWakeupPipeDrain(PUSBPROXYDEVUSBIP pProxyDevUsbIp)
1315{
1316 char bRead = 0;
1317 size_t cbRead = 0;
1318
1319 int rc = RTPipeRead(pProxyDevUsbIp->hPipeR, &bRead, 1, &cbRead);
1320 Assert(RT_SUCCESS(rc) && cbRead == 1);
1321
1322 return bRead;
1323}
1324
1325/*
1326 * The USB proxy device functions.
1327 */
1328
1329static DECLCALLBACK(int) usbProxyUsbIpOpen(PUSBPROXYDEV pProxyDev, const char *pszAddress, void *pvBackend)
1330{
1331 LogFlowFunc(("pProxyDev=%p pszAddress=%s, pvBackend=%p\n", pProxyDev, pszAddress, pvBackend));
1332
1333 PUSBPROXYDEVUSBIP pDevUsbIp = USBPROXYDEV_2_DATA(pProxyDev, PUSBPROXYDEVUSBIP);
1334 int rc = VINF_SUCCESS;
1335
1336 RTListInit(&pDevUsbIp->ListUrbsInFlight);
1337 RTListInit(&pDevUsbIp->ListUrbsLanded);
1338 RTListInit(&pDevUsbIp->ListUrbsToQueue);
1339 pDevUsbIp->hSocket = NIL_RTSOCKET;
1340 pDevUsbIp->hPollSet = NIL_RTPOLLSET;
1341 pDevUsbIp->hPipeW = NIL_RTPIPE;
1342 pDevUsbIp->hPipeR = NIL_RTPIPE;
1343 pDevUsbIp->u32SeqNumNext = 0;
1344 pDevUsbIp->pszHost = NULL;
1345 pDevUsbIp->pszBusId = NULL;
1346 usbProxyUsbIpResetRecvState(pDevUsbIp);
1347
1348 rc = RTSemFastMutexCreate(&pDevUsbIp->hMtxLists);
1349 if (RT_SUCCESS(rc))
1350 {
1351 /* Setup wakeup pipe and poll set first. */
1352 rc = RTPipeCreate(&pDevUsbIp->hPipeR, &pDevUsbIp->hPipeW, 0);
1353 if (RT_SUCCESS(rc))
1354 {
1355 rc = RTPollSetCreate(&pDevUsbIp->hPollSet);
1356 if (RT_SUCCESS(rc))
1357 {
1358 rc = RTPollSetAddPipe(pDevUsbIp->hPollSet, pDevUsbIp->hPipeR,
1359 RTPOLL_EVT_READ, USBIP_POLL_ID_PIPE);
1360 if (RT_SUCCESS(rc))
1361 {
1362 /* Connect to the USB/IP host. */
1363 rc = usbProxyUsbIpParseAddress(pDevUsbIp, pszAddress);
1364 if (RT_SUCCESS(rc))
1365 rc = usbProxyUsbIpConnect(pDevUsbIp);
1366 }
1367
1368 if (RT_FAILURE(rc))
1369 {
1370 RTPollSetRemove(pDevUsbIp->hPollSet, USBIP_POLL_ID_PIPE);
1371 int rc2 = RTPollSetDestroy(pDevUsbIp->hPollSet);
1372 AssertRC(rc2);
1373 }
1374 }
1375
1376 if (RT_FAILURE(rc))
1377 {
1378 int rc2 = RTPipeClose(pDevUsbIp->hPipeR);
1379 AssertRC(rc2);
1380 rc2 = RTPipeClose(pDevUsbIp->hPipeW);
1381 AssertRC(rc2);
1382 }
1383 }
1384 }
1385
1386 return rc;
1387}
1388
1389static DECLCALLBACK(void) usbProxyUsbIpClose(PUSBPROXYDEV pProxyDev)
1390{
1391 int rc = VINF_SUCCESS;
1392 LogFlowFunc(("pProxyDev = %p\n", pProxyDev));
1393
1394 PUSBPROXYDEVUSBIP pDevUsbIp = USBPROXYDEV_2_DATA(pProxyDev, PUSBPROXYDEVUSBIP);
1395 /* Destroy the pipe and pollset if necessary. */
1396 if (pDevUsbIp->hPollSet != NIL_RTPOLLSET)
1397 {
1398 if (pDevUsbIp->hSocket != NIL_RTSOCKET)
1399 {
1400 rc = RTPollSetRemove(pDevUsbIp->hPollSet, USBIP_POLL_ID_SOCKET);
1401 Assert(RT_SUCCESS(rc) || rc == VERR_POLL_HANDLE_ID_NOT_FOUND);
1402 }
1403 rc = RTPollSetRemove(pDevUsbIp->hPollSet, USBIP_POLL_ID_PIPE);
1404 AssertRC(rc);
1405 rc = RTPollSetDestroy(pDevUsbIp->hPollSet);
1406 AssertRC(rc);
1407 rc = RTPipeClose(pDevUsbIp->hPipeR);
1408 AssertRC(rc);
1409 rc = RTPipeClose(pDevUsbIp->hPipeW);
1410 AssertRC(rc);
1411 }
1412
1413 if (pDevUsbIp->hSocket != NIL_RTSOCKET)
1414 usbProxyUsbIpDisconnect(pDevUsbIp);
1415 if (pDevUsbIp->pszHost)
1416 RTStrFree(pDevUsbIp->pszHost);
1417 if (pDevUsbIp->pszBusId)
1418 RTStrFree(pDevUsbIp->pszBusId);
1419
1420 /* Clear the URB lists. */
1421 rc = RTSemFastMutexRequest(pDevUsbIp->hMtxLists);
1422 AssertRC(rc);
1423 PUSBPROXYURBUSBIP pIter = NULL;
1424 PUSBPROXYURBUSBIP pIterNext = NULL;
1425 RTListForEachSafe(&pDevUsbIp->ListUrbsInFlight, pIter, pIterNext, USBPROXYURBUSBIP, NodeList)
1426 {
1427 RTListNodeRemove(&pIter->NodeList);
1428 RTMemFree(pIter);
1429 }
1430
1431 RTListForEachSafe(&pDevUsbIp->ListUrbsLanded, pIter, pIterNext, USBPROXYURBUSBIP, NodeList)
1432 {
1433 RTListNodeRemove(&pIter->NodeList);
1434 RTMemFree(pIter);
1435 }
1436 RTSemFastMutexRelease(pDevUsbIp->hMtxLists);
1437 RTSemFastMutexDestroy(pDevUsbIp->hMtxLists);
1438}
1439
1440static DECLCALLBACK(int) usbProxyUsbIpReset(PUSBPROXYDEV pProxyDev, bool fResetOnLinux)
1441{
1442 LogFlowFunc(("pProxyDev = %p\n", pProxyDev));
1443
1444 int rc = VINF_SUCCESS;
1445 PUSBPROXYDEVUSBIP pProxyDevUsbIp = USBPROXYDEV_2_DATA(pProxyDev, PUSBPROXYDEVUSBIP);
1446 VUSBSETUP Setup;
1447
1448 if (fResetOnLinux)
1449 {
1450 Setup.bmRequestType = RT_BIT(5) | 0x03; /* Port request. */
1451 Setup.bRequest = 0x03; /* SET_FEATURE */
1452 Setup.wValue = 4; /* Port feature: Reset */
1453 Setup.wIndex = 0; /* Port number, irrelevant */
1454 Setup.wLength = 0;
1455 rc = usbProxyUsbIpCtrlUrbExchangeSync(pProxyDevUsbIp, &Setup);
1456 if (RT_SUCCESS(rc))
1457 {
1458 pProxyDev->iActiveCfg = -1;
1459 pProxyDev->cIgnoreSetConfigs = 2;
1460 }
1461 }
1462
1463 return rc;
1464}
1465
1466static DECLCALLBACK(int) usbProxyUsbIpSetConfig(PUSBPROXYDEV pProxyDev, int iCfg)
1467{
1468 LogFlowFunc(("pProxyDev=%s cfg=%#x\n", pProxyDev->pUsbIns->pszName, iCfg));
1469
1470 PUSBPROXYDEVUSBIP pProxyDevUsbIp = USBPROXYDEV_2_DATA(pProxyDev, PUSBPROXYDEVUSBIP);
1471 VUSBSETUP Setup;
1472
1473 Setup.bmRequestType = 0;
1474 Setup.bRequest = 0x09;
1475 Setup.wValue = iCfg;
1476 Setup.wIndex = 0;
1477 Setup.wLength = 0;
1478 return usbProxyUsbIpCtrlUrbExchangeSync(pProxyDevUsbIp, &Setup);
1479}
1480
1481static DECLCALLBACK(int) usbProxyUsbIpClaimInterface(PUSBPROXYDEV pProxyDev, int ifnum)
1482{
1483 LogFlowFunc(("pProxyDev=%s ifnum=%#x\n", pProxyDev->pUsbIns->pszName, ifnum));
1484 return VINF_SUCCESS;
1485}
1486
1487static DECLCALLBACK(int) usbProxyUsbIpReleaseInterface(PUSBPROXYDEV pProxyDev, int ifnum)
1488{
1489 LogFlowFunc(("pProxyDev=%s ifnum=%#x\n", pProxyDev->pUsbIns->pszName, ifnum));
1490 return VINF_SUCCESS;
1491}
1492
1493static DECLCALLBACK(int) usbProxyUsbIpSetInterface(PUSBPROXYDEV pProxyDev, int ifnum, int setting)
1494{
1495 LogFlowFunc(("pProxyDev=%p ifnum=%#x setting=%#x\n", pProxyDev, ifnum, setting));
1496
1497 PUSBPROXYDEVUSBIP pProxyDevUsbIp = USBPROXYDEV_2_DATA(pProxyDev, PUSBPROXYDEVUSBIP);
1498 VUSBSETUP Setup;
1499
1500 Setup.bmRequestType = 0x1;
1501 Setup.bRequest = 0x0b; /* SET_INTERFACE */
1502 Setup.wValue = setting;
1503 Setup.wIndex = ifnum;
1504 Setup.wLength = 0;
1505 return usbProxyUsbIpCtrlUrbExchangeSync(pProxyDevUsbIp, &Setup);
1506}
1507
1508static DECLCALLBACK(int) usbProxyUsbIpClearHaltedEp(PUSBPROXYDEV pProxyDev, unsigned int iEp)
1509{
1510 LogFlowFunc(("pProxyDev=%s ep=%u\n", pProxyDev->pUsbIns->pszName, iEp));
1511
1512 PUSBPROXYDEVUSBIP pProxyDevUsbIp = USBPROXYDEV_2_DATA(pProxyDev, PUSBPROXYDEVUSBIP);
1513 VUSBSETUP Setup;
1514
1515 Setup.bmRequestType = 0x2;
1516 Setup.bRequest = 0x01; /* CLEAR_FEATURE */
1517 Setup.wValue = 0x00; /* ENDPOINT_HALT */
1518 Setup.wIndex = iEp;
1519 Setup.wLength = 0;
1520 return usbProxyUsbIpCtrlUrbExchangeSync(pProxyDevUsbIp, &Setup);
1521}
1522
1523static DECLCALLBACK(int) usbProxyUsbIpUrbQueue(PUSBPROXYDEV pProxyDev, PVUSBURB pUrb)
1524{
1525 LogFlowFunc(("pUrb=%p\n", pUrb));
1526
1527 PUSBPROXYDEVUSBIP pProxyDevUsbIp = USBPROXYDEV_2_DATA(pProxyDev, PUSBPROXYDEVUSBIP);
1528
1529 /* Allocate a USB/IP Urb. */
1530 PUSBPROXYURBUSBIP pUrbUsbIp = usbProxyUsbIpUrbAlloc(pProxyDevUsbIp);
1531 if (!pUrbUsbIp)
1532 return VERR_NO_MEMORY;
1533
1534 pUrbUsbIp->fCancelled = false;
1535 pUrbUsbIp->pVUsbUrb = pUrb;
1536 pUrb->Dev.pvPrivate = pUrbUsbIp;
1537
1538 int rc = RTSemFastMutexRequest(pProxyDevUsbIp->hMtxLists);
1539 AssertRC(rc);
1540 RTListAppend(&pProxyDevUsbIp->ListUrbsToQueue, &pUrbUsbIp->NodeList);
1541 RTSemFastMutexRelease(pProxyDevUsbIp->hMtxLists);
1542
1543 return usbProxyReaperKick(pProxyDevUsbIp, USBIP_REAPER_WAKEUP_REASON_QUEUE);
1544}
1545
1546static DECLCALLBACK(PVUSBURB) usbProxyUsbIpUrbReap(PUSBPROXYDEV pProxyDev, RTMSINTERVAL cMillies)
1547{
1548 LogFlowFunc(("pProxyDev=%s\n", pProxyDev->pUsbIns->pszName));
1549
1550 PUSBPROXYDEVUSBIP pProxyDevUsbIp = USBPROXYDEV_2_DATA(pProxyDev, PUSBPROXYDEVUSBIP);
1551 PUSBPROXYURBUSBIP pUrbUsbIp = NULL;
1552 PVUSBURB pUrb = NULL;
1553 int rc = VINF_SUCCESS;
1554
1555 /* Queue new URBs first. */
1556 rc = usbProxyUsbIpUrbsQueuePending(pProxyDevUsbIp);
1557 AssertRC(rc);
1558
1559 /* Any URBs pending delivery? */
1560 if (!RTListIsEmpty(&pProxyDevUsbIp->ListUrbsLanded))
1561 pUrbUsbIp = RTListGetFirst(&pProxyDevUsbIp->ListUrbsLanded, USBPROXYURBUSBIP, NodeList);
1562
1563 while (!pUrbUsbIp && RT_SUCCESS(rc) && cMillies)
1564 {
1565 uint32_t uIdReady = 0;
1566 uint32_t fEventsRecv = 0;
1567 RTMSINTERVAL msStart = RTTimeMilliTS();
1568 RTMSINTERVAL msNow;
1569
1570 rc = RTPoll(pProxyDevUsbIp->hPollSet, cMillies, &fEventsRecv, &uIdReady);
1571 Assert(RT_SUCCESS(rc) || rc == VERR_TIMEOUT);
1572 if (RT_SUCCESS(rc))
1573 {
1574 msNow = RTTimeMilliTS();
1575 cMillies = msNow - msStart >= cMillies ? 0 : cMillies - (msNow - msStart);
1576
1577 if (uIdReady == USBIP_POLL_ID_SOCKET)
1578 rc = usbProxyUsbIpRecvPdu(pProxyDevUsbIp, &pUrbUsbIp);
1579 else
1580 {
1581 AssertLogRelMsg(uIdReady == USBIP_POLL_ID_PIPE, ("Invalid pollset ID given\n"));
1582
1583 char bReason = usbProxyUsbIpWakeupPipeDrain(pProxyDevUsbIp);
1584 if (bReason == USBIP_REAPER_WAKEUP_REASON_QUEUE)
1585 usbProxyUsbIpUrbsQueuePending(pProxyDevUsbIp);
1586 else
1587 {
1588 Assert(bReason == USBIP_REAPER_WAKEUP_REASON_EXTERNAL);
1589 break;
1590 }
1591 }
1592 }
1593 }
1594
1595 if (pUrbUsbIp)
1596 {
1597 pUrb = pUrbUsbIp->pVUsbUrb;
1598
1599 /* unlink from the pending delivery list */
1600 usbProxyUsbIpUnlinkUrb(pProxyDevUsbIp, pUrbUsbIp);
1601 usbProxyUsbIpUrbFree(pProxyDevUsbIp, pUrbUsbIp);
1602 }
1603
1604 return pUrb;
1605}
1606
1607static DECLCALLBACK(int) usbProxyUsbIpUrbCancel(PUSBPROXYDEV pProxyDev, PVUSBURB pUrb)
1608{
1609 LogFlowFunc(("pUrb=%p\n", pUrb));
1610
1611 PUSBPROXYDEVUSBIP pProxyDevUsbIp = USBPROXYDEV_2_DATA(pProxyDev, PUSBPROXYDEVUSBIP);
1612 PUSBPROXYURBUSBIP pUrbUsbIp = (PUSBPROXYURBUSBIP)pUrb->Dev.pvPrivate;
1613 UsbIpReqUnlink ReqUnlink;
1614
1615 RT_ZERO(ReqUnlink);
1616
1617 uint32_t u32SeqNum = usbProxyUsbIpSeqNumGet(pProxyDevUsbIp);
1618 ReqUnlink.Hdr.u32ReqRet = USBIP_CMD_UNLINK;
1619 ReqUnlink.Hdr.u32SeqNum = u32SeqNum;
1620 ReqUnlink.Hdr.u32DevId = pProxyDevUsbIp->u32DevId;
1621 ReqUnlink.Hdr.u32Direction = USBIP_DIR_OUT;
1622 ReqUnlink.Hdr.u32Endpoint = pUrb->EndPt;
1623 ReqUnlink.u32SeqNum = pUrbUsbIp->u32SeqNumUrb;
1624
1625 usbProxyUsbIpReqUnlinkH2N(&ReqUnlink);
1626 int rc = RTTcpWrite(pProxyDevUsbIp->hSocket, &ReqUnlink, sizeof(ReqUnlink));
1627 if (RT_SUCCESS(rc))
1628 {
1629 pUrbUsbIp->u32SeqNumUrbUnlink = u32SeqNum;
1630 pUrbUsbIp->fCancelled = true;
1631 }
1632
1633 return rc;
1634}
1635
1636static DECLCALLBACK(int) usbProxyUsbIpWakeup(PUSBPROXYDEV pProxyDev)
1637{
1638 LogFlowFunc(("pProxyDev=%s\n", pProxyDev->pUsbIns->pszName));
1639
1640 PUSBPROXYDEVUSBIP pProxyDevUsbIp = USBPROXYDEV_2_DATA(pProxyDev, PUSBPROXYDEVUSBIP);
1641 return usbProxyReaperKick(pProxyDevUsbIp, USBIP_REAPER_WAKEUP_REASON_EXTERNAL);
1642}
1643
1644/**
1645 * The USB/IP USB Proxy Backend operations.
1646 */
1647extern const USBPROXYBACK g_USBProxyDeviceUsbIp =
1648{
1649 /* pszName */
1650 "usbip",
1651 /* cbBackend */
1652 sizeof(USBPROXYDEVUSBIP),
1653 usbProxyUsbIpOpen,
1654 NULL,
1655 usbProxyUsbIpClose,
1656 usbProxyUsbIpReset,
1657 usbProxyUsbIpSetConfig,
1658 usbProxyUsbIpClaimInterface,
1659 usbProxyUsbIpReleaseInterface,
1660 usbProxyUsbIpSetInterface,
1661 usbProxyUsbIpClearHaltedEp,
1662 usbProxyUsbIpUrbQueue,
1663 usbProxyUsbIpUrbCancel,
1664 usbProxyUsbIpUrbReap,
1665 usbProxyUsbIpWakeup,
1666 0
1667};
1668
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette