VirtualBox

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

Last change on this file since 78397 was 76553, checked in by vboxsync, 6 years ago

scm --update-copyright-year

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 61.3 KB
Line 
1/* $Id: USBProxyDevice-usbip.cpp 76553 2019-01-01 01:45:53Z vboxsync $ */
2/** @file
3 * USB device proxy - USB/IP backend.
4 */
5
6/*
7 * Copyright (C) 2014-2019 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 /** USB xfer type. */
352 VUSBXFERTYPE enmType;
353 /** USB xfer direction. */
354 VUSBDIRECTION enmDir;
355 /** Completion status. */
356 VUSBSTATUS enmStatus;
357 /** Pointer to the VUSB URB. */
358 PVUSBURB pVUsbUrb;
359} USBPROXYURBUSBIP;
360/** Pointer to a USB/IP URB. */
361typedef USBPROXYURBUSBIP *PUSBPROXYURBUSBIP;
362
363/**
364 * USB/IP data receive states.
365 */
366typedef enum USBPROXYUSBIPRECVSTATE
367{
368 /** Invalid receive state. */
369 USBPROXYUSBIPRECVSTATE_INVALID = 0,
370 /** Currently receiving the common header structure. */
371 USBPROXYUSBIPRECVSTATE_HDR_COMMON,
372 /** Currently receieving the rest of the header structure. */
373 USBPROXYUSBIPRECVSTATE_HDR_RESIDUAL,
374 /** Currently receiving data into the URB buffer. */
375 USBPROXYUSBIPRECVSTATE_URB_BUFFER,
376 /** Currently receiving the isochronous packet descriptors. */
377 USBPROXYUSBIPRECVSTATE_ISOC_PKT_DESCS,
378 /** Usual 32bit hack. */
379 USBPROXYUSBIPRECVSTATE_32BIT_HACK = 0x7fffffff
380} USBPROXYUSBIPRECVSTATE;
381/** Pointer to an receive state. */
382typedef USBPROXYUSBIPRECVSTATE *PUSBPROXYUSBIPRECVSTATE;
383
384/**
385 * Backend data for the USB/IP USB Proxy device backend.
386 */
387typedef struct USBPROXYDEVUSBIP
388{
389 /** IPRT socket handle. */
390 RTSOCKET hSocket;
391 /** Pollset with the wakeup pipe and socket. */
392 RTPOLLSET hPollSet;
393 /** Pipe endpoint - read (in the pollset). */
394 RTPIPE hPipeR;
395 /** Pipe endpoint - write. */
396 RTPIPE hPipeW;
397 /** Next sequence number to use for identifying submitted URBs. */
398 volatile uint32_t u32SeqNumNext;
399 /** Fast mutex protecting the lists below against concurrent access. */
400 RTSEMFASTMUTEX hMtxLists;
401 /** List of in flight URBs. */
402 RTLISTANCHOR ListUrbsInFlight;
403 /** List of landed URBs. */
404 RTLISTANCHOR ListUrbsLanded;
405 /** List of URBs to submit. */
406 RTLISTANCHOR ListUrbsToQueue;
407 /** Port of the USB/IP host to connect to. */
408 uint32_t uPort;
409 /** USB/IP host address. */
410 char *pszHost;
411 /** USB Bus ID of the device to capture. */
412 char *pszBusId;
413 /** The device ID to use to identify the device. */
414 uint32_t u32DevId;
415 /** Temporary buffer for the next reply header */
416 UsbIpRet BufRet;
417 /** Temporary buffer to hold all isochronous packet descriptors. */
418 UsbIpIsocPktDesc aIsocPktDesc[8];
419 /** Pointer to the current buffer to write received data to. */
420 uint8_t *pbRecv;
421 /** Number of bytes received so far. */
422 size_t cbRecv;
423 /** Number of bytes left to receive. until we advance the state machine and process the data */
424 size_t cbLeft;
425 /** The current receiving state. */
426 USBPROXYUSBIPRECVSTATE enmRecvState;
427 /** The URB we currently receive a response for. */
428 PUSBPROXYURBUSBIP pUrbUsbIp;
429} USBPROXYDEVUSBIP, *PUSBPROXYDEVUSBIP;
430
431/** Pollset id of the socket. */
432#define USBIP_POLL_ID_SOCKET 0
433/** Pollset id of the pipe. */
434#define USBIP_POLL_ID_PIPE 1
435
436/** USB/IP address prefix for identifcation. */
437#define USBIP_URI_PREFIX "usbip://"
438/** USB/IP address prefix length. */
439#define USBIP_URI_PREFIX_LEN (sizeof(USBIP_URI_PREFIX) - 1)
440
441/** Waking reason for the USB I/P reaper: New URBs to queue. */
442#define USBIP_REAPER_WAKEUP_REASON_QUEUE 'Q'
443/** Waking reason for the USB I/P reaper: External wakeup. */
444#define USBIP_REAPER_WAKEUP_REASON_EXTERNAL 'E'
445
446/**
447 * Converts a request/reply header from network to host endianness.
448 *
449 * @returns nothing.
450 * @param pHdr The header to convert.
451 */
452DECLINLINE(void) usbProxyUsbIpReqRetHdrN2H(PUsbIpReqRetHdr pHdr)
453{
454 pHdr->u32ReqRet = RT_H2N_U32(pHdr->u32ReqRet);
455 pHdr->u32SeqNum = RT_H2N_U32(pHdr->u32SeqNum);
456 pHdr->u32DevId = RT_H2N_U32(pHdr->u32DevId);
457 pHdr->u32Direction = RT_H2N_U32(pHdr->u32Direction);
458 pHdr->u32Endpoint = RT_H2N_U32(pHdr->u32Endpoint);
459}
460
461/**
462 * Converts a request/reply header from host to network endianness.
463 *
464 * @returns nothing.
465 * @param pHdr The header to convert.
466 */
467DECLINLINE(void) usbProxyUsbIpReqRetHdrH2N(PUsbIpReqRetHdr pHdr)
468{
469 pHdr->u32ReqRet = RT_N2H_U32(pHdr->u32ReqRet);
470 pHdr->u32SeqNum = RT_N2H_U32(pHdr->u32SeqNum);
471 pHdr->u32DevId = RT_N2H_U32(pHdr->u32DevId);
472 pHdr->u32Direction = RT_N2H_U32(pHdr->u32Direction);
473 pHdr->u32Endpoint = RT_N2H_U32(pHdr->u32Endpoint);
474}
475
476/**
477 * Converts a submit request from host to network endianness.
478 *
479 * @returns nothing.
480 * @param pReqSubmit The submit request to convert.
481 */
482DECLINLINE(void) usbProxyUsbIpReqSubmitH2N(PUsbIpReqSubmit pReqSubmit)
483{
484 usbProxyUsbIpReqRetHdrH2N(&pReqSubmit->Hdr);
485 pReqSubmit->u32XferFlags = RT_H2N_U32(pReqSubmit->u32XferFlags);
486 pReqSubmit->u32TransferBufferLength = RT_H2N_U32(pReqSubmit->u32TransferBufferLength);
487 pReqSubmit->u32StartFrame = RT_H2N_U32(pReqSubmit->u32StartFrame);
488 pReqSubmit->u32NumIsocPkts = RT_H2N_U32(pReqSubmit->u32NumIsocPkts);
489 pReqSubmit->u32Interval = RT_H2N_U32(pReqSubmit->u32Interval);
490}
491
492/**
493 * Converts a submit reply from network to host endianness.
494 *
495 * @returns nothing.
496 * @param pReqSubmit The submit reply to convert.
497 */
498DECLINLINE(void) usbProxyUsbIpRetSubmitN2H(PUsbIpRetSubmit pRetSubmit)
499{
500 usbProxyUsbIpReqRetHdrN2H(&pRetSubmit->Hdr);
501 pRetSubmit->u32Status = RT_N2H_U32(pRetSubmit->u32Status);
502 pRetSubmit->u32ActualLength = RT_N2H_U32(pRetSubmit->u32ActualLength);
503 pRetSubmit->u32StartFrame = RT_N2H_U32(pRetSubmit->u32StartFrame);
504 pRetSubmit->u32NumIsocPkts = RT_N2H_U32(pRetSubmit->u32NumIsocPkts);
505 pRetSubmit->u32ErrorCount = RT_N2H_U32(pRetSubmit->u32ErrorCount);
506}
507
508/**
509 * Converts a isochronous packet descriptor from host to network endianness.
510 *
511 * @returns nothing.
512 * @param pIsocPktDesc The packet descriptor to convert.
513 */
514DECLINLINE(void) usbProxyUsbIpIsocPktDescH2N(PUsbIpIsocPktDesc pIsocPktDesc)
515{
516 pIsocPktDesc->u32Offset = RT_H2N_U32(pIsocPktDesc->u32Offset);
517 pIsocPktDesc->u32Length = RT_H2N_U32(pIsocPktDesc->u32Length);
518 pIsocPktDesc->u32ActualLength = RT_H2N_U32(pIsocPktDesc->u32ActualLength);
519 pIsocPktDesc->i32Status = RT_H2N_U32(pIsocPktDesc->i32Status);
520}
521
522/**
523 * Converts a isochronous packet descriptor from network to host endianness.
524 *
525 * @returns nothing.
526 * @param pIsocPktDesc The packet descriptor to convert.
527 */
528DECLINLINE(void) usbProxyUsbIpIsocPktDescN2H(PUsbIpIsocPktDesc pIsocPktDesc)
529{
530 pIsocPktDesc->u32Offset = RT_N2H_U32(pIsocPktDesc->u32Offset);
531 pIsocPktDesc->u32Length = RT_N2H_U32(pIsocPktDesc->u32Length);
532 pIsocPktDesc->u32ActualLength = RT_N2H_U32(pIsocPktDesc->u32ActualLength);
533 pIsocPktDesc->i32Status = RT_N2H_U32(pIsocPktDesc->i32Status);
534}
535
536/**
537 * Converts a unlink request from host to network endianness.
538 *
539 * @returns nothing.
540 * @param pReqUnlink The unlink request to convert.
541 */
542DECLINLINE(void) usbProxyUsbIpReqUnlinkH2N(PUsbIpReqUnlink pReqUnlink)
543{
544 usbProxyUsbIpReqRetHdrH2N(&pReqUnlink->Hdr);
545 pReqUnlink->u32SeqNum = RT_H2N_U32(pReqUnlink->u32SeqNum);
546}
547
548/**
549 * Converts a unlink reply from network to host endianness.
550 *
551 * @returns nothing.
552 * @param pRetUnlink The unlink reply to convert.
553 */
554DECLINLINE(void) usbProxyUsbIpRetUnlinkN2H(PUsbIpRetUnlink pRetUnlink)
555{
556 usbProxyUsbIpReqRetHdrN2H(&pRetUnlink->Hdr);
557 pRetUnlink->u32Status = RT_N2H_U32(pRetUnlink->u32Status);
558}
559
560/**
561 * Convert the given exported device structure from host to network byte order.
562 *
563 * @returns nothing.
564 * @param pDevice The device structure to convert.
565 */
566DECLINLINE(void) usbProxyUsbIpExportedDeviceN2H(PUsbIpExportedDevice pDevice)
567{
568 pDevice->u32BusNum = RT_N2H_U32(pDevice->u32BusNum);
569 pDevice->u32DevNum = RT_N2H_U32(pDevice->u32DevNum);
570 pDevice->u32Speed = RT_N2H_U16(pDevice->u32Speed);
571 pDevice->u16VendorId = RT_N2H_U16(pDevice->u16VendorId);
572 pDevice->u16ProductId = RT_N2H_U16(pDevice->u16ProductId);
573 pDevice->u16BcdDevice = RT_N2H_U16(pDevice->u16BcdDevice);
574}
575
576/**
577 * Converts a USB/IP status code to a VUSB status code.
578 *
579 * @returns VUSB status code.
580 * @param i32Status The USB/IP status code from the reply.
581 */
582DECLINLINE(VUSBSTATUS) usbProxyUsbIpVUsbStatusConvertFromStatus(int32_t i32Status)
583{
584 if (RT_LIKELY( i32Status == USBIP_STATUS_SUCCESS
585 || i32Status == USBIP_STATUS_SHORT_READ))
586 return VUSBSTATUS_OK;
587
588 switch (i32Status)
589 {
590 case USBIP_STATUS_PIPE_STALLED:
591 return VUSBSTATUS_STALL;
592 default:
593 return VUSBSTATUS_DNR;
594 }
595 /* not reached */
596}
597
598/**
599 * Gets the next free sequence number.
600 *
601 * @returns Next free sequence number.
602 * @param pProxyDevUsbIp The USB/IP proxy device data.
603 */
604DECLINLINE(uint32_t) usbProxyUsbIpSeqNumGet(PUSBPROXYDEVUSBIP pProxyDevUsbIp)
605{
606 uint32_t u32SeqNum = ASMAtomicIncU32(&pProxyDevUsbIp->u32SeqNumNext);
607 if (RT_UNLIKELY(!u32SeqNum))
608 u32SeqNum = ASMAtomicIncU32(&pProxyDevUsbIp->u32SeqNumNext);
609
610 return u32SeqNum;
611}
612
613/**
614 * Links a given URB into the given list.
615 *
616 * @returns nothing.
617 * @param pProxyDevUsbIp The USB/IP proxy device data.
618 * @param pList The list to link the URB into.
619 * @param pUrbUsbIp The URB to link.
620 */
621DECLINLINE(void) usbProxyUsbIpLinkUrb(PUSBPROXYDEVUSBIP pProxyDevUsbIp, PRTLISTANCHOR pList, PUSBPROXYURBUSBIP pUrbUsbIp)
622{
623 int rc = RTSemFastMutexRequest(pProxyDevUsbIp->hMtxLists);
624 AssertRC(rc);
625 RTListAppend(pList, &pUrbUsbIp->NodeList);
626 RTSemFastMutexRelease(pProxyDevUsbIp->hMtxLists);
627}
628
629/**
630 * Unlinks a given URB from the current assigned list.
631 *
632 * @returns nothing.
633 * @param pProxyDevUsbIp The USB/IP proxy device data.
634 * @param pUrbUsbIp The URB to unlink.
635 */
636DECLINLINE(void) usbProxyUsbIpUnlinkUrb(PUSBPROXYDEVUSBIP pProxyDevUsbIp, PUSBPROXYURBUSBIP pUrbUsbIp)
637{
638 int rc = RTSemFastMutexRequest(pProxyDevUsbIp->hMtxLists);
639 AssertRC(rc);
640 RTListNodeRemove(&pUrbUsbIp->NodeList);
641 RTSemFastMutexRelease(pProxyDevUsbIp->hMtxLists);
642}
643
644/**
645 * Allocates a USB/IP proxy specific URB state.
646 *
647 * @returns Pointer to the USB/IP specific URB data or NULL on failure.
648 * @param pProxyDevUsbIp The USB/IP proxy device data.
649 */
650static PUSBPROXYURBUSBIP usbProxyUsbIpUrbAlloc(PUSBPROXYDEVUSBIP pProxyDevUsbIp)
651{
652 NOREF(pProxyDevUsbIp);
653 return (PUSBPROXYURBUSBIP)RTMemAllocZ(sizeof(USBPROXYURBUSBIP));
654}
655
656/**
657 * Frees the given USB/IP URB state.
658 *
659 * @returns nothing.
660 * @param pProxyDevUsbIp The USB/IP proxy device data.
661 * @param pUrbUsbIp The USB/IP speciic URB data.
662 */
663static void usbProxyUsbIpUrbFree(PUSBPROXYDEVUSBIP pProxyDevUsbIp, PUSBPROXYURBUSBIP pUrbUsbIp)
664{
665 NOREF(pProxyDevUsbIp);
666 RTMemFree(pUrbUsbIp);
667}
668
669/**
670 * Parse the string representation of the host address.
671 *
672 * @returns VBox status code.
673 * @param pProxyDevUsbIp The USB/IP proxy device data to parse the address for.
674 * @param pszAddress The address string to parse.
675 */
676static int usbProxyUsbIpParseAddress(PUSBPROXYDEVUSBIP pProxyDevUsbIp, const char *pszAddress)
677{
678 int rc = VINF_SUCCESS;
679
680 if (!RTStrNCmp(pszAddress, USBIP_URI_PREFIX, USBIP_URI_PREFIX_LEN))
681 {
682 pszAddress += USBIP_URI_PREFIX_LEN;
683
684 const char *pszPortStart = RTStrStr(pszAddress, ":");
685 if (pszPortStart)
686 {
687 pszPortStart++;
688
689 const char *pszBusIdStart = RTStrStr(pszPortStart, ":");
690 if (pszBusIdStart)
691 {
692 size_t cbHost = pszPortStart - pszAddress - 1;
693 size_t cbBusId = strlen(pszBusIdStart);
694
695 pszBusIdStart++;
696
697 rc = RTStrToUInt32Ex(pszPortStart, NULL, 10 /* uBase */, &pProxyDevUsbIp->uPort);
698 if ( rc == VINF_SUCCESS
699 || rc == VWRN_TRAILING_CHARS)
700 {
701 rc = RTStrAllocEx(&pProxyDevUsbIp->pszHost, cbHost + 1);
702 if (RT_SUCCESS(rc))
703 rc = RTStrAllocEx(&pProxyDevUsbIp->pszBusId, cbBusId + 1);
704 if (RT_SUCCESS(rc))
705 {
706 rc = RTStrCopyEx(pProxyDevUsbIp->pszHost, cbHost + 1, pszAddress, cbHost);
707 AssertRC(rc);
708
709 rc = RTStrCopyEx(pProxyDevUsbIp->pszBusId, cbBusId + 1, pszBusIdStart, cbBusId);
710 AssertRC(rc);
711
712 return VINF_SUCCESS;
713 }
714 }
715 else
716 rc = VERR_INVALID_PARAMETER;
717 }
718 else
719 rc = VERR_INVALID_PARAMETER;
720 }
721 else
722 rc = VERR_INVALID_PARAMETER;
723 }
724 else
725 rc = VERR_INVALID_PARAMETER;
726
727 return rc;
728}
729
730/**
731 * Connects to the USB/IP host and claims the device given in the proxy device data.
732 *
733 * @returns VBox status code.
734 * @param pProxyDevUsbIp The USB/IP proxy device data.
735 */
736static int usbProxyUsbIpConnect(PUSBPROXYDEVUSBIP pProxyDevUsbIp)
737{
738 int rc = VINF_SUCCESS;
739 rc = RTTcpClientConnect(pProxyDevUsbIp->pszHost, pProxyDevUsbIp->uPort, &pProxyDevUsbIp->hSocket);
740 if (RT_SUCCESS(rc))
741 {
742 /* Disable send coalescing. */
743 rc = RTTcpSetSendCoalescing(pProxyDevUsbIp->hSocket, false);
744 if (RT_FAILURE(rc))
745 LogRel(("UsbIp: Disabling send coalescing failed (rc=%Rrc), continuing nevertheless but expect reduced performance\n", rc));
746
747 /* Import the device, i.e. claim it for our use. */
748 UsbIpReqImport ReqImport;
749 ReqImport.u16Version = RT_H2N_U16(USBIP_VERSION);
750 ReqImport.u16Cmd = RT_H2N_U16(USBIP_INDICATOR_REQ | USBIP_REQ_RET_IMPORT);
751 ReqImport.u32Status = RT_H2N_U32(USBIP_STATUS_SUCCESS);
752 rc = RTStrCopy(&ReqImport.aszBusId[0], sizeof(ReqImport.aszBusId), pProxyDevUsbIp->pszBusId);
753 if (rc == VINF_SUCCESS)
754 {
755 rc = RTTcpWrite(pProxyDevUsbIp->hSocket, &ReqImport, sizeof(ReqImport));
756 if (RT_SUCCESS(rc))
757 {
758 /* Read the reply. */
759 UsbIpRetImport RetImport;
760 rc = RTTcpRead(pProxyDevUsbIp->hSocket, &RetImport, sizeof(RetImport), NULL);
761 if (RT_SUCCESS(rc))
762 {
763 RetImport.u16Version = RT_N2H_U16(RetImport.u16Version);
764 RetImport.u16Cmd = RT_N2H_U16(RetImport.u16Cmd);
765 RetImport.u32Status = RT_N2H_U32(RetImport.u32Status);
766 if ( RetImport.u16Version == USBIP_VERSION
767 && RetImport.u16Cmd == USBIP_REQ_RET_IMPORT
768 && RetImport.u32Status == USBIP_STATUS_SUCCESS)
769 {
770 /* Read the device data. */
771 UsbIpExportedDevice Device;
772 rc = RTTcpRead(pProxyDevUsbIp->hSocket, &Device, sizeof(Device), NULL);
773 if (RT_SUCCESS(rc))
774 {
775 usbProxyUsbIpExportedDeviceN2H(&Device);
776 pProxyDevUsbIp->u32DevId = (Device.u32BusNum << 16) | Device.u32DevNum;
777
778 rc = RTPollSetAddSocket(pProxyDevUsbIp->hPollSet, pProxyDevUsbIp->hSocket,
779 RTPOLL_EVT_READ | RTPOLL_EVT_ERROR, USBIP_POLL_ID_SOCKET);
780 }
781 }
782 else
783 {
784 /* Check what went wrong and leave a meaningful error message in the log. */
785 if (RetImport.u16Version != USBIP_VERSION)
786 LogRel(("UsbIp: Unexpected protocol version received from host (%#x vs. %#x)\n",
787 RetImport.u16Version, USBIP_VERSION));
788 else if (RetImport.u16Cmd != USBIP_REQ_RET_IMPORT)
789 LogRel(("UsbIp: Unexpected reply code received from host (%#x vs. %#x)\n",
790 RetImport.u16Cmd, USBIP_REQ_RET_IMPORT));
791 else if (RetImport.u32Status != 0)
792 LogRel(("UsbIp: Claiming the device has failed on the host with an unspecified error\n"));
793 else
794 AssertMsgFailed(("Something went wrong with if condition\n"));
795 }
796 }
797 }
798 }
799 else
800 {
801 LogRel(("UsbIp: Given bus ID is exceeds permitted protocol length: %u vs %u\n",
802 strlen(pProxyDevUsbIp->pszBusId) + 1, sizeof(ReqImport.aszBusId)));
803 rc = VERR_INVALID_PARAMETER;
804 }
805
806 if (RT_FAILURE(rc))
807 RTTcpClientCloseEx(pProxyDevUsbIp->hSocket, false /*fGracefulShutdown*/);
808 }
809 if (RT_FAILURE(rc))
810 LogRel(("UsbIp: Connecting to the host %s failed with %Rrc\n", pProxyDevUsbIp->pszHost, rc));
811 return rc;
812}
813
814/**
815 * Disconnects from the USB/IP host releasing the device given in the proxy device data.
816 *
817 * @returns VBox status code.
818 * @param pProxyDevUsbIp The USB/IP proxy device data.
819 */
820static int usbProxyUsbIpDisconnect(PUSBPROXYDEVUSBIP pProxyDevUsbIp)
821{
822 int rc = VINF_SUCCESS;
823
824 rc = RTTcpClientCloseEx(pProxyDevUsbIp->hSocket, false /*fGracefulShutdown*/);
825 if (RT_SUCCESS(rc))
826 pProxyDevUsbIp->hSocket = NIL_RTSOCKET;
827 return rc;
828}
829
830/**
831 * Returns the URB matching the given sequence number from the in flight list.
832 *
833 * @returns pointer to the URB matching the given sequence number or NULL
834 * @param pProxyDevUsbIp The USB/IP proxy device data.
835 * @param u32SeqNum The sequence number to search for.
836 */
837static PUSBPROXYURBUSBIP usbProxyUsbIpGetInFlightUrbFromSeqNum(PUSBPROXYDEVUSBIP pProxyDevUsbIp, uint32_t u32SeqNum)
838{
839 bool fFound = false;
840
841 int rc = RTSemFastMutexRequest(pProxyDevUsbIp->hMtxLists);
842 AssertRC(rc);
843 PUSBPROXYURBUSBIP pIt;
844 RTListForEach(&pProxyDevUsbIp->ListUrbsInFlight, pIt, USBPROXYURBUSBIP, NodeList)
845 {
846 if (pIt->u32SeqNumUrb == u32SeqNum)
847 {
848 fFound = true;
849 break;
850 }
851 }
852 RTSemFastMutexRelease(pProxyDevUsbIp->hMtxLists);
853
854 return fFound ? pIt : NULL;
855}
856
857/**
858 * Returns the URB matching the given sequence number from the cancel list.
859 *
860 * @returns pointer to the URB matching the given sequence number or NULL
861 * @param pProxyDevUsbIp The USB/IP proxy device data.
862 * @param u32SeqNum The sequence number to search for.
863 */
864static PUSBPROXYURBUSBIP usbProxyUsbIpGetCancelledUrbFromSeqNum(PUSBPROXYDEVUSBIP pProxyDevUsbIp, uint32_t u32SeqNum)
865{
866 bool fFound = false;
867
868 int rc = RTSemFastMutexRequest(pProxyDevUsbIp->hMtxLists);
869 AssertRC(rc);
870 PUSBPROXYURBUSBIP pIt;
871 RTListForEach(&pProxyDevUsbIp->ListUrbsInFlight, pIt, USBPROXYURBUSBIP, NodeList)
872 {
873 if ( pIt->u32SeqNumUrbUnlink == u32SeqNum
874 && pIt->fCancelled == true)
875 {
876 fFound = true;
877 break;
878 }
879 }
880 RTSemFastMutexRelease(pProxyDevUsbIp->hMtxLists);
881
882 return fFound ? pIt : NULL;
883}
884
885/**
886 * Resets the receive state for a new reply.
887 *
888 * @returns nothing.
889 * @param pProxyDevUsbIp The USB/IP proxy device data.
890 */
891static void usbProxyUsbIpResetRecvState(PUSBPROXYDEVUSBIP pProxyDevUsbIp)
892{
893 pProxyDevUsbIp->enmRecvState = USBPROXYUSBIPRECVSTATE_HDR_COMMON;
894 pProxyDevUsbIp->pbRecv = (uint8_t *)&pProxyDevUsbIp->BufRet;
895 pProxyDevUsbIp->cbRecv = 0;
896 pProxyDevUsbIp->cbLeft = sizeof(UsbIpReqRetHdr);
897}
898
899static void usbProxyUsbIpRecvStateAdvance(PUSBPROXYDEVUSBIP pProxyDevUsbIp, USBPROXYUSBIPRECVSTATE enmState,
900 uint8_t *pbData, size_t cbData)
901{
902 pProxyDevUsbIp->enmRecvState = enmState;
903 pProxyDevUsbIp->cbRecv = 0;
904 pProxyDevUsbIp->cbLeft = cbData;
905 pProxyDevUsbIp->pbRecv = pbData;
906}
907
908/**
909 * Handles reception of a USB/IP PDU.
910 *
911 * @returns VBox status code.
912 * @param pProxyDevUsbIp The USB/IP proxy device data.
913 * @param ppUrbUsbIp Where to store the pointer to the USB/IP URB which completed.
914 * Will be NULL if the received PDU is not complete and we have
915 * have to wait for more data or on failure.
916 */
917static int usbProxyUsbIpRecvPdu(PUSBPROXYDEVUSBIP pProxyDevUsbIp, PUSBPROXYURBUSBIP *ppUrbUsbIp)
918{
919 int rc = VINF_SUCCESS;
920 size_t cbRead = 0;
921 PUSBPROXYURBUSBIP pUrbUsbIp = NULL;
922
923 Assert(pProxyDevUsbIp->cbLeft);
924
925 /* Read any available data first. */
926 rc = RTTcpReadNB(pProxyDevUsbIp->hSocket, pProxyDevUsbIp->pbRecv, pProxyDevUsbIp->cbLeft, &cbRead);
927 if (RT_SUCCESS(rc))
928 {
929 pProxyDevUsbIp->cbRecv += cbRead;
930 pProxyDevUsbIp->cbLeft -= cbRead;
931 pProxyDevUsbIp->pbRecv += cbRead;
932
933 /* Process the received data if there is nothing to receive left for the current state. */
934 if (!pProxyDevUsbIp->cbLeft)
935 {
936 switch (pProxyDevUsbIp->enmRecvState)
937 {
938 case USBPROXYUSBIPRECVSTATE_HDR_COMMON:
939 {
940 Assert(pProxyDevUsbIp->cbRecv == sizeof(UsbIpReqRetHdr));
941
942 /*
943 * Determine the residual amount of data to receive until
944 * the complete reply header was received.
945 */
946 switch (RT_N2H_U32(pProxyDevUsbIp->BufRet.Hdr.u32ReqRet))
947 {
948 case USBIP_RET_SUBMIT:
949 pProxyDevUsbIp->cbLeft = sizeof(UsbIpRetSubmit) - sizeof(UsbIpReqRetHdr);
950 pProxyDevUsbIp->enmRecvState = USBPROXYUSBIPRECVSTATE_HDR_RESIDUAL;
951 break;
952 case USBIP_RET_UNLINK:
953 pProxyDevUsbIp->cbLeft = sizeof(UsbIpRetUnlink) - sizeof(UsbIpReqRetHdr);
954 pProxyDevUsbIp->enmRecvState = USBPROXYUSBIPRECVSTATE_HDR_RESIDUAL;
955 break;
956 default:
957 AssertLogRelMsgFailed(("Invalid reply header received: %d\n",
958 pProxyDevUsbIp->BufRet.Hdr.u32ReqRet));
959 usbProxyUsbIpResetRecvState(pProxyDevUsbIp);
960 }
961
962 break;
963 }
964 case USBPROXYUSBIPRECVSTATE_HDR_RESIDUAL:
965 {
966 /** @todo Verify that the directions match, verify that the length doesn't exceed the buffer. */
967
968 switch (RT_N2H_U32(pProxyDevUsbIp->BufRet.Hdr.u32ReqRet))
969 {
970 case USBIP_RET_SUBMIT:
971 /* Get the URB from the in flight list. */
972 pProxyDevUsbIp->pUrbUsbIp = usbProxyUsbIpGetInFlightUrbFromSeqNum(pProxyDevUsbIp, RT_N2H_U32(pProxyDevUsbIp->BufRet.Hdr.u32SeqNum));
973 if (pProxyDevUsbIp->pUrbUsbIp)
974 {
975 usbProxyUsbIpRetSubmitN2H(&pProxyDevUsbIp->BufRet.RetSubmit);
976
977 /* We still have to receive the transfer buffer, even in case of an error. */
978 pProxyDevUsbIp->pUrbUsbIp->enmStatus = usbProxyUsbIpVUsbStatusConvertFromStatus(pProxyDevUsbIp->BufRet.RetSubmit.u32Status);
979 if (pProxyDevUsbIp->pUrbUsbIp->enmDir == VUSBDIRECTION_IN)
980 {
981 uint8_t *pbData = NULL;
982
983 AssertPtr(pProxyDevUsbIp->pUrbUsbIp->pVUsbUrb);
984 if (pProxyDevUsbIp->pUrbUsbIp->enmType == VUSBXFERTYPE_MSG)
985 {
986 /* Preserve the setup request. */
987 pbData = &pProxyDevUsbIp->pUrbUsbIp->pVUsbUrb->abData[sizeof(VUSBSETUP)];
988 pProxyDevUsbIp->pUrbUsbIp->pVUsbUrb->cbData = pProxyDevUsbIp->BufRet.RetSubmit.u32ActualLength + sizeof(VUSBSETUP);
989 }
990 else
991 {
992 pbData = &pProxyDevUsbIp->pUrbUsbIp->pVUsbUrb->abData[0];
993 pProxyDevUsbIp->pUrbUsbIp->pVUsbUrb->cbData = pProxyDevUsbIp->BufRet.RetSubmit.u32ActualLength;
994 }
995
996 if (pProxyDevUsbIp->BufRet.RetSubmit.u32ActualLength)
997 usbProxyUsbIpRecvStateAdvance(pProxyDevUsbIp, USBPROXYUSBIPRECVSTATE_URB_BUFFER,
998 pbData, pProxyDevUsbIp->BufRet.RetSubmit.u32ActualLength);
999 else
1000 {
1001 pUrbUsbIp = pProxyDevUsbIp->pUrbUsbIp;
1002 usbProxyUsbIpResetRecvState(pProxyDevUsbIp);
1003 }
1004 }
1005 else
1006 {
1007 Assert(pProxyDevUsbIp->pUrbUsbIp->enmDir == VUSBDIRECTION_OUT);
1008 pUrbUsbIp = pProxyDevUsbIp->pUrbUsbIp;
1009 usbProxyUsbIpResetRecvState(pProxyDevUsbIp);
1010 }
1011 }
1012 else
1013 {
1014 LogRel(("USB/IP: Received reply with sequence number %u doesn't match any local URB\n",
1015 RT_N2H_U32(pProxyDevUsbIp->BufRet.Hdr.u32SeqNum)));
1016 usbProxyUsbIpResetRecvState(pProxyDevUsbIp);
1017 }
1018 break;
1019 case USBIP_RET_UNLINK:
1020 pProxyDevUsbIp->pUrbUsbIp = usbProxyUsbIpGetCancelledUrbFromSeqNum(pProxyDevUsbIp, RT_N2H_U32(pProxyDevUsbIp->BufRet.Hdr.u32SeqNum));
1021 if (pProxyDevUsbIp->pUrbUsbIp)
1022 {
1023 usbProxyUsbIpRetUnlinkN2H(&pProxyDevUsbIp->BufRet.RetUnlink);
1024 pUrbUsbIp = pProxyDevUsbIp->pUrbUsbIp;
1025 pUrbUsbIp->pVUsbUrb->enmStatus = usbProxyUsbIpVUsbStatusConvertFromStatus(pProxyDevUsbIp->BufRet.RetUnlink.u32Status);
1026 }
1027 /* else: Probably received the data for the URB and is complete already. */
1028
1029 usbProxyUsbIpResetRecvState(pProxyDevUsbIp);
1030 break;
1031 }
1032
1033 break;
1034 }
1035 case USBPROXYUSBIPRECVSTATE_URB_BUFFER:
1036 if (pProxyDevUsbIp->pUrbUsbIp->pVUsbUrb->enmType == VUSBXFERTYPE_ISOC)
1037 usbProxyUsbIpRecvStateAdvance(pProxyDevUsbIp, USBPROXYUSBIPRECVSTATE_ISOC_PKT_DESCS,
1038 (uint8_t *)&pProxyDevUsbIp->aIsocPktDesc[0],
1039 pProxyDevUsbIp->pUrbUsbIp->pVUsbUrb->cIsocPkts * sizeof(UsbIpIsocPktDesc));
1040 else
1041 {
1042 pUrbUsbIp = pProxyDevUsbIp->pUrbUsbIp;
1043 usbProxyUsbIpResetRecvState(pProxyDevUsbIp);
1044 }
1045 break;
1046 case USBPROXYUSBIPRECVSTATE_ISOC_PKT_DESCS:
1047 /* Process all received isochronous packet descriptors. */
1048 for (unsigned i = 0; i < pProxyDevUsbIp->pUrbUsbIp->pVUsbUrb->cIsocPkts; i++)
1049 {
1050 PVUSBURBISOCPTK pIsocPkt = &pProxyDevUsbIp->pUrbUsbIp->pVUsbUrb->aIsocPkts[i];
1051 usbProxyUsbIpIsocPktDescN2H(&pProxyDevUsbIp->aIsocPktDesc[i]);
1052 pIsocPkt->enmStatus = usbProxyUsbIpVUsbStatusConvertFromStatus(pProxyDevUsbIp->aIsocPktDesc[i].i32Status);
1053 pIsocPkt->off = pProxyDevUsbIp->aIsocPktDesc[i].u32Offset;
1054 pIsocPkt->cb = pProxyDevUsbIp->aIsocPktDesc[i].u32ActualLength;
1055 }
1056
1057 pUrbUsbIp = pProxyDevUsbIp->pUrbUsbIp;
1058 usbProxyUsbIpResetRecvState(pProxyDevUsbIp);
1059 break;
1060 default:
1061 AssertLogRelMsgFailed(("USB/IP: Invalid receive state %d\n", pProxyDevUsbIp->enmRecvState));
1062 }
1063 }
1064 }
1065 else
1066 {
1067 /** @todo Complete all URBs with DNR error and mark device as unplugged. */
1068#if 0
1069 pUrbUsbIp = pProxyDevUsbIp->pUrbUsbIp;
1070 pUrbUsbIp->pVUsbUrb->enmStatus = VUSBSTATUS_DNR;
1071 usbProxyUsbIpResetRecvState(pProxyDevUsbIp);
1072#endif
1073 }
1074
1075 if (RT_SUCCESS(rc))
1076 *ppUrbUsbIp = pUrbUsbIp;
1077
1078 return rc;
1079}
1080
1081/**
1082 * Worker for queueing an URB on the main I/O thread.
1083 *
1084 * @returns VBox status code.
1085 * @param pProxyDevUsbIp The USB/IP proxy device data.
1086 * @param pUrbUsbIp The USB/IP URB to queue.
1087 */
1088static int usbProxyUsbIpUrbQueueWorker(PUSBPROXYDEVUSBIP pProxyDevUsbIp, PUSBPROXYURBUSBIP pUrbUsbIp)
1089{
1090 PVUSBURB pUrb = pUrbUsbIp->pVUsbUrb;
1091
1092 pUrbUsbIp->u32SeqNumUrb = usbProxyUsbIpSeqNumGet(pProxyDevUsbIp);
1093 pUrbUsbIp->enmType = pUrb->enmType;
1094 pUrbUsbIp->enmStatus = pUrb->enmStatus;
1095 pUrbUsbIp->enmDir = pUrb->enmDir;
1096
1097 UsbIpReqSubmit ReqSubmit;
1098
1099 RT_ZERO(ReqSubmit);
1100 ReqSubmit.Hdr.u32ReqRet = USBIP_CMD_SUBMIT;
1101 ReqSubmit.Hdr.u32SeqNum = pUrbUsbIp->u32SeqNumUrb;
1102 ReqSubmit.Hdr.u32DevId = pProxyDevUsbIp->u32DevId;
1103 ReqSubmit.Hdr.u32Endpoint = pUrb->EndPt;
1104 ReqSubmit.Hdr.u32Direction = pUrb->enmDir == VUSBDIRECTION_IN ? USBIP_DIR_IN : USBIP_DIR_OUT;
1105 ReqSubmit.u32XferFlags = 0;
1106 if (pUrb->enmDir == VUSBDIRECTION_IN && pUrb->fShortNotOk)
1107 ReqSubmit.u32XferFlags |= USBIP_XFER_FLAGS_SHORT_NOT_OK;
1108
1109 ReqSubmit.u32TransferBufferLength = pUrb->cbData;
1110 ReqSubmit.u32StartFrame = 0;
1111 ReqSubmit.u32NumIsocPkts = 0;
1112 ReqSubmit.u32Interval = 0;
1113
1114 RTSGSEG aSegReq[3]; /* Maximum number of segments used for a Isochronous transfer. */
1115 UsbIpIsocPktDesc aIsocPktsDesc[8];
1116 unsigned cSegsUsed = 1;
1117 aSegReq[0].pvSeg = &ReqSubmit;
1118 aSegReq[0].cbSeg = sizeof(ReqSubmit);
1119
1120 switch (pUrb->enmType)
1121 {
1122 case VUSBXFERTYPE_MSG:
1123 memcpy(&ReqSubmit.Setup, &pUrb->abData, sizeof(ReqSubmit.Setup));
1124 ReqSubmit.u32TransferBufferLength -= sizeof(VUSBSETUP);
1125 if (pUrb->enmDir == VUSBDIRECTION_OUT)
1126 {
1127 aSegReq[cSegsUsed].cbSeg = pUrb->cbData - sizeof(VUSBSETUP);
1128 aSegReq[cSegsUsed].pvSeg = pUrb->abData + sizeof(VUSBSETUP);
1129 if (aSegReq[cSegsUsed].cbSeg)
1130 cSegsUsed++;
1131 }
1132 LogFlowFunc(("Message (Control) URB\n"));
1133 break;
1134 case VUSBXFERTYPE_ISOC:
1135 LogFlowFunc(("Isochronous URB\n"));
1136 ReqSubmit.u32XferFlags |= USBIP_XFER_FLAGS_ISO_ASAP;
1137 ReqSubmit.u32NumIsocPkts = pUrb->cIsocPkts;
1138 if (pUrb->enmDir == VUSBDIRECTION_OUT)
1139 {
1140 aSegReq[cSegsUsed].cbSeg = pUrb->cbData;
1141 aSegReq[cSegsUsed].pvSeg = pUrb->abData;
1142 cSegsUsed++;
1143 }
1144
1145 for (unsigned i = 0; i < pUrb->cIsocPkts; i++)
1146 {
1147 aIsocPktsDesc[i].u32Offset = pUrb->aIsocPkts[i].off;
1148 aIsocPktsDesc[i].u32Length = pUrb->aIsocPkts[i].cb;
1149 aIsocPktsDesc[i].u32ActualLength = 0; /** @todo */
1150 aIsocPktsDesc[i].i32Status = pUrb->aIsocPkts[i].enmStatus;
1151 usbProxyUsbIpIsocPktDescH2N(&aIsocPktsDesc[i]);
1152 }
1153
1154 if (pUrb->cIsocPkts)
1155 {
1156 aSegReq[cSegsUsed].cbSeg = pUrb->cIsocPkts * sizeof(UsbIpIsocPktDesc);
1157 aSegReq[cSegsUsed].pvSeg = &aIsocPktsDesc[0];
1158 cSegsUsed++;
1159 }
1160
1161 break;
1162 case VUSBXFERTYPE_BULK:
1163 case VUSBXFERTYPE_INTR:
1164 LogFlowFunc(("Bulk URB\n"));
1165 if (pUrb->enmDir == VUSBDIRECTION_OUT)
1166 {
1167 aSegReq[cSegsUsed].cbSeg = pUrb->cbData;
1168 aSegReq[cSegsUsed].pvSeg = pUrb->abData;
1169 cSegsUsed++;
1170 }
1171 break;
1172 default:
1173 usbProxyUsbIpUrbFree(pProxyDevUsbIp, pUrbUsbIp);
1174 return VERR_INVALID_PARAMETER; /** @todo better status code. */
1175 }
1176
1177 usbProxyUsbIpReqSubmitH2N(&ReqSubmit);
1178
1179 Assert(cSegsUsed <= RT_ELEMENTS(aSegReq));
1180
1181 /* Send the command. */
1182 RTSGBUF SgBufReq;
1183 RTSgBufInit(&SgBufReq, &aSegReq[0], cSegsUsed);
1184
1185 int rc = RTTcpSgWrite(pProxyDevUsbIp->hSocket, &SgBufReq);
1186 if (RT_SUCCESS(rc))
1187 {
1188 /* Link the URB into the list of in flight URBs. */
1189 usbProxyUsbIpLinkUrb(pProxyDevUsbIp, &pProxyDevUsbIp->ListUrbsInFlight, pUrbUsbIp);
1190 }
1191
1192 return rc;
1193}
1194
1195/**
1196 * Queues all pending URBs from the list.
1197 *
1198 * @returns VBox status code.
1199 * @param pProxyDevUsbIp The USB/IP proxy device data.
1200 */
1201static int usbProxyUsbIpUrbsQueuePending(PUSBPROXYDEVUSBIP pProxyDevUsbIp)
1202{
1203 RTLISTANCHOR ListUrbsPending;
1204
1205 int rc = RTSemFastMutexRequest(pProxyDevUsbIp->hMtxLists);
1206 AssertRC(rc);
1207 RTListMove(&ListUrbsPending, &pProxyDevUsbIp->ListUrbsToQueue);
1208 RTSemFastMutexRelease(pProxyDevUsbIp->hMtxLists);
1209
1210 PUSBPROXYURBUSBIP pIter;
1211 PUSBPROXYURBUSBIP pIterNext;
1212 RTListForEachSafe(&ListUrbsPending, pIter, pIterNext, USBPROXYURBUSBIP, NodeList)
1213 {
1214 RTListNodeRemove(&pIter->NodeList);
1215 rc = usbProxyUsbIpUrbQueueWorker(pProxyDevUsbIp, pIter);
1216 if (RT_FAILURE(rc))
1217 {
1218 /** @todo Complete the URB with an error. */
1219 usbProxyUsbIpUrbFree(pProxyDevUsbIp, pIter);
1220 }
1221 }
1222
1223 return VINF_SUCCESS;
1224}
1225
1226/**
1227 * Kick the reaper thread.
1228 *
1229 * @returns VBox status code.
1230 * @param pProxyDevUsbIp The USB/IP proxy device data.
1231 * @param bReason The wakeup reason.
1232 */
1233static char usbProxyReaperKick(PUSBPROXYDEVUSBIP pProxyDevUsbIp, char bReason)
1234{
1235 int rc = VINF_SUCCESS;
1236 size_t cbWritten = 0;
1237
1238 rc = RTPipeWrite(pProxyDevUsbIp->hPipeW, &bReason, 1, &cbWritten);
1239 Assert(RT_SUCCESS(rc) || cbWritten == 0);
1240
1241 return rc;
1242}
1243
1244/**
1245 * Drain the wakeup pipe.
1246 *
1247 * @returns Wakeup reason.
1248 * @param pProxyDevUsbIp The USB/IP proxy device data.
1249 */
1250static char usbProxyUsbIpWakeupPipeDrain(PUSBPROXYDEVUSBIP pProxyDevUsbIp)
1251{
1252 char bRead = 0;
1253 size_t cbRead = 0;
1254 int rc = RTPipeRead(pProxyDevUsbIp->hPipeR, &bRead, 1, &cbRead);
1255 Assert(RT_SUCCESS(rc) && cbRead == 1); NOREF(rc);
1256
1257 return bRead;
1258}
1259
1260/**
1261 * Executes the poll/receive loop either until a URB is received (with an optional matching sequence number) or
1262 * the given timeout has elapsed.
1263 *
1264 * @returns Pointer to the received USB/IP URB or NULL on timeout or error.
1265 * @param pProxyDevUsbIp The USB/IP proxy device data.
1266 * @param u32SeqNumRet The sequence number of a specific reply to return the URB for, 0 if
1267 * any received URB is accepted.
1268 * @param fPollWakePipe Flag whether to poll the wakeup pipe.
1269 * @param cMillies Maximum number of milliseconds to wait for an URB to arrive.
1270 */
1271static PUSBPROXYURBUSBIP usbProxyUsbIpPollWorker(PUSBPROXYDEVUSBIP pProxyDevUsbIp, uint32_t u32SeqNumRet,
1272 bool fPollWakePipe, RTMSINTERVAL cMillies)
1273{
1274 int rc = VINF_SUCCESS;
1275 PUSBPROXYURBUSBIP pUrbUsbIp = NULL;
1276
1277 if (!fPollWakePipe)
1278 {
1279 rc = RTPollSetEventsChange(pProxyDevUsbIp->hPollSet, USBIP_POLL_ID_PIPE, RTPOLL_EVT_ERROR);
1280 AssertRC(rc);
1281 }
1282
1283 while (!pUrbUsbIp && RT_SUCCESS(rc) && cMillies)
1284 {
1285 uint32_t uIdReady = 0;
1286 uint32_t fEventsRecv = 0;
1287 RTMSINTERVAL msStart = RTTimeMilliTS();
1288 RTMSINTERVAL msNow;
1289
1290 rc = RTPoll(pProxyDevUsbIp->hPollSet, cMillies, &fEventsRecv, &uIdReady);
1291 Assert(RT_SUCCESS(rc) || rc == VERR_TIMEOUT);
1292 if (RT_SUCCESS(rc))
1293 {
1294 msNow = RTTimeMilliTS();
1295 cMillies = msNow - msStart >= cMillies ? 0 : cMillies - (msNow - msStart);
1296
1297 if (uIdReady == USBIP_POLL_ID_SOCKET)
1298 {
1299 rc = usbProxyUsbIpRecvPdu(pProxyDevUsbIp, &pUrbUsbIp);
1300 if ( RT_SUCCESS(rc)
1301 && pUrbUsbIp)
1302 {
1303 /* Link the URB into the landed list if a specifc reply is requested and the URB doesn't match. */
1304 if ( u32SeqNumRet != 0
1305 && pUrbUsbIp->u32SeqNumUrb != u32SeqNumRet)
1306 {
1307 usbProxyUsbIpUnlinkUrb(pProxyDevUsbIp, pUrbUsbIp);
1308 usbProxyUsbIpLinkUrb(pProxyDevUsbIp, &pProxyDevUsbIp->ListUrbsLanded, pUrbUsbIp);
1309 pUrbUsbIp = NULL;
1310 }
1311 }
1312 }
1313 else
1314 {
1315 AssertLogRelMsg(uIdReady == USBIP_POLL_ID_PIPE, ("Invalid pollset ID given\n"));
1316
1317 char bReason = usbProxyUsbIpWakeupPipeDrain(pProxyDevUsbIp);
1318 if (bReason == USBIP_REAPER_WAKEUP_REASON_QUEUE)
1319 usbProxyUsbIpUrbsQueuePending(pProxyDevUsbIp);
1320 else
1321 {
1322 Assert(bReason == USBIP_REAPER_WAKEUP_REASON_EXTERNAL);
1323 break;
1324 }
1325 }
1326 }
1327 }
1328
1329 if (!fPollWakePipe)
1330 {
1331 rc = RTPollSetEventsChange(pProxyDevUsbIp->hPollSet, USBIP_POLL_ID_PIPE, RTPOLL_EVT_READ);
1332 AssertRC(rc);
1333 }
1334
1335 return pUrbUsbIp;
1336}
1337
1338/**
1339 * Synchronously exchange a given control message with the remote device.
1340 *
1341 * @eturns VBox status code.
1342 * @param pProxyDevUsbIp The USB/IP proxy device data.
1343 * @param pSetup The setup message.
1344 *
1345 * @note This method is only used to implement the *SetConfig, *SetInterface and *ClearHaltedEp
1346 * callbacks because the USB/IP protocol lacks dedicated requests for these.
1347 * @remark It is assumed that this method is never called while usbProxyUsbIpUrbReap is called
1348 * on another thread.
1349 */
1350static int usbProxyUsbIpCtrlUrbExchangeSync(PUSBPROXYDEVUSBIP pProxyDevUsbIp, PVUSBSETUP pSetup)
1351{
1352 int rc = VINF_SUCCESS;
1353
1354 UsbIpReqSubmit ReqSubmit;
1355 USBPROXYURBUSBIP UsbIpUrb;
1356
1357 RT_ZERO(ReqSubmit);
1358
1359 uint32_t u32SeqNum = usbProxyUsbIpSeqNumGet(pProxyDevUsbIp);
1360 ReqSubmit.Hdr.u32ReqRet = USBIP_CMD_SUBMIT;
1361 ReqSubmit.Hdr.u32SeqNum = u32SeqNum;
1362 ReqSubmit.Hdr.u32DevId = pProxyDevUsbIp->u32DevId;
1363 ReqSubmit.Hdr.u32Direction = USBIP_DIR_OUT;
1364 ReqSubmit.Hdr.u32Endpoint = 0; /* Only default control endpoint is allowed for these kind of messages. */
1365 ReqSubmit.u32XferFlags = 0;
1366 ReqSubmit.u32TransferBufferLength = 0;
1367 ReqSubmit.u32StartFrame = 0;
1368 ReqSubmit.u32NumIsocPkts = 0;
1369 ReqSubmit.u32Interval = 0;
1370 memcpy(&ReqSubmit.Setup, pSetup, sizeof(ReqSubmit.Setup));
1371 usbProxyUsbIpReqSubmitH2N(&ReqSubmit);
1372
1373 UsbIpUrb.u32SeqNumUrb = u32SeqNum;
1374 UsbIpUrb.u32SeqNumUrbUnlink = 0;
1375 UsbIpUrb.fCancelled = false;
1376 UsbIpUrb.enmType = VUSBXFERTYPE_MSG;
1377 UsbIpUrb.enmDir = VUSBDIRECTION_OUT;
1378 UsbIpUrb.pVUsbUrb = NULL;
1379
1380 /* Send the command. */
1381 rc = RTTcpWrite(pProxyDevUsbIp->hSocket, &ReqSubmit, sizeof(ReqSubmit));
1382 if (RT_SUCCESS(rc))
1383 {
1384 usbProxyUsbIpLinkUrb(pProxyDevUsbIp, &pProxyDevUsbIp->ListUrbsInFlight, &UsbIpUrb);
1385 PUSBPROXYURBUSBIP pUrbUsbIp = usbProxyUsbIpPollWorker(pProxyDevUsbIp, u32SeqNum, false /*fPollWakePipe*/,
1386 30 * RT_MS_1SEC);
1387 Assert( !pUrbUsbIp
1388 || pUrbUsbIp == &UsbIpUrb); /* The returned URB should point to the URB we submitted. */
1389 usbProxyUsbIpUnlinkUrb(pProxyDevUsbIp, &UsbIpUrb);
1390
1391 if (!pUrbUsbIp)
1392 rc = VERR_TIMEOUT;
1393 }
1394
1395 return rc;
1396}
1397
1398
1399/*
1400 * The USB proxy device functions.
1401 */
1402
1403/**
1404 * @interface_method_impl{USBPROXYBACK,pfnOpen}
1405 */
1406static DECLCALLBACK(int) usbProxyUsbIpOpen(PUSBPROXYDEV pProxyDev, const char *pszAddress, void *pvBackend)
1407{
1408 RT_NOREF(pvBackend);
1409 LogFlowFunc(("pProxyDev=%p pszAddress=%s, pvBackend=%p\n", pProxyDev, pszAddress, pvBackend));
1410
1411 PUSBPROXYDEVUSBIP pDevUsbIp = USBPROXYDEV_2_DATA(pProxyDev, PUSBPROXYDEVUSBIP);
1412 int rc = VINF_SUCCESS;
1413
1414 RTListInit(&pDevUsbIp->ListUrbsInFlight);
1415 RTListInit(&pDevUsbIp->ListUrbsLanded);
1416 RTListInit(&pDevUsbIp->ListUrbsToQueue);
1417 pDevUsbIp->hSocket = NIL_RTSOCKET;
1418 pDevUsbIp->hPollSet = NIL_RTPOLLSET;
1419 pDevUsbIp->hPipeW = NIL_RTPIPE;
1420 pDevUsbIp->hPipeR = NIL_RTPIPE;
1421 pDevUsbIp->u32SeqNumNext = 0;
1422 pDevUsbIp->pszHost = NULL;
1423 pDevUsbIp->pszBusId = NULL;
1424 usbProxyUsbIpResetRecvState(pDevUsbIp);
1425
1426 rc = RTSemFastMutexCreate(&pDevUsbIp->hMtxLists);
1427 if (RT_SUCCESS(rc))
1428 {
1429 /* Setup wakeup pipe and poll set first. */
1430 rc = RTPipeCreate(&pDevUsbIp->hPipeR, &pDevUsbIp->hPipeW, 0);
1431 if (RT_SUCCESS(rc))
1432 {
1433 rc = RTPollSetCreate(&pDevUsbIp->hPollSet);
1434 if (RT_SUCCESS(rc))
1435 {
1436 rc = RTPollSetAddPipe(pDevUsbIp->hPollSet, pDevUsbIp->hPipeR,
1437 RTPOLL_EVT_READ, USBIP_POLL_ID_PIPE);
1438 if (RT_SUCCESS(rc))
1439 {
1440 /* Connect to the USB/IP host. */
1441 rc = usbProxyUsbIpParseAddress(pDevUsbIp, pszAddress);
1442 if (RT_SUCCESS(rc))
1443 rc = usbProxyUsbIpConnect(pDevUsbIp);
1444 }
1445
1446 if (RT_FAILURE(rc))
1447 {
1448 RTPollSetRemove(pDevUsbIp->hPollSet, USBIP_POLL_ID_PIPE);
1449 int rc2 = RTPollSetDestroy(pDevUsbIp->hPollSet);
1450 AssertRC(rc2);
1451 }
1452 }
1453
1454 if (RT_FAILURE(rc))
1455 {
1456 int rc2 = RTPipeClose(pDevUsbIp->hPipeR);
1457 AssertRC(rc2);
1458 rc2 = RTPipeClose(pDevUsbIp->hPipeW);
1459 AssertRC(rc2);
1460 }
1461 }
1462 }
1463
1464 return rc;
1465}
1466
1467
1468/**
1469 * @interface_method_impl{USBPROXYBACK,pfnClose}
1470 */
1471static DECLCALLBACK(void) usbProxyUsbIpClose(PUSBPROXYDEV pProxyDev)
1472{
1473 int rc = VINF_SUCCESS;
1474 LogFlowFunc(("pProxyDev = %p\n", pProxyDev));
1475
1476 PUSBPROXYDEVUSBIP pDevUsbIp = USBPROXYDEV_2_DATA(pProxyDev, PUSBPROXYDEVUSBIP);
1477 /* Destroy the pipe and pollset if necessary. */
1478 if (pDevUsbIp->hPollSet != NIL_RTPOLLSET)
1479 {
1480 if (pDevUsbIp->hSocket != NIL_RTSOCKET)
1481 {
1482 rc = RTPollSetRemove(pDevUsbIp->hPollSet, USBIP_POLL_ID_SOCKET);
1483 Assert(RT_SUCCESS(rc) || rc == VERR_POLL_HANDLE_ID_NOT_FOUND);
1484 }
1485 rc = RTPollSetRemove(pDevUsbIp->hPollSet, USBIP_POLL_ID_PIPE);
1486 AssertRC(rc);
1487 rc = RTPollSetDestroy(pDevUsbIp->hPollSet);
1488 AssertRC(rc);
1489 rc = RTPipeClose(pDevUsbIp->hPipeR);
1490 AssertRC(rc);
1491 rc = RTPipeClose(pDevUsbIp->hPipeW);
1492 AssertRC(rc);
1493 }
1494
1495 if (pDevUsbIp->hSocket != NIL_RTSOCKET)
1496 usbProxyUsbIpDisconnect(pDevUsbIp);
1497 if (pDevUsbIp->pszHost)
1498 RTStrFree(pDevUsbIp->pszHost);
1499 if (pDevUsbIp->pszBusId)
1500 RTStrFree(pDevUsbIp->pszBusId);
1501
1502 /* Clear the URB lists. */
1503 rc = RTSemFastMutexRequest(pDevUsbIp->hMtxLists);
1504 AssertRC(rc);
1505 PUSBPROXYURBUSBIP pIter;
1506 PUSBPROXYURBUSBIP pIterNext;
1507 RTListForEachSafe(&pDevUsbIp->ListUrbsInFlight, pIter, pIterNext, USBPROXYURBUSBIP, NodeList)
1508 {
1509 RTListNodeRemove(&pIter->NodeList);
1510 RTMemFree(pIter);
1511 }
1512
1513 RTListForEachSafe(&pDevUsbIp->ListUrbsLanded, pIter, pIterNext, USBPROXYURBUSBIP, NodeList)
1514 {
1515 RTListNodeRemove(&pIter->NodeList);
1516 RTMemFree(pIter);
1517 }
1518 RTSemFastMutexRelease(pDevUsbIp->hMtxLists);
1519 RTSemFastMutexDestroy(pDevUsbIp->hMtxLists);
1520}
1521
1522
1523/**
1524 * @interface_method_impl{USBPROXYBACK,pfnReset}
1525 */
1526static DECLCALLBACK(int) usbProxyUsbIpReset(PUSBPROXYDEV pProxyDev, bool fResetOnLinux)
1527{
1528 LogFlowFunc(("pProxyDev = %p\n", pProxyDev));
1529
1530 int rc = VINF_SUCCESS;
1531 PUSBPROXYDEVUSBIP pProxyDevUsbIp = USBPROXYDEV_2_DATA(pProxyDev, PUSBPROXYDEVUSBIP);
1532 VUSBSETUP Setup;
1533
1534 if (fResetOnLinux)
1535 {
1536 Setup.bmRequestType = RT_BIT(5) | 0x03; /* Port request. */
1537 Setup.bRequest = 0x03; /* SET_FEATURE */
1538 Setup.wValue = 4; /* Port feature: Reset */
1539 Setup.wIndex = 0; /* Port number, irrelevant */
1540 Setup.wLength = 0;
1541 rc = usbProxyUsbIpCtrlUrbExchangeSync(pProxyDevUsbIp, &Setup);
1542 if (RT_SUCCESS(rc))
1543 {
1544 pProxyDev->iActiveCfg = -1;
1545 pProxyDev->cIgnoreSetConfigs = 2;
1546 }
1547 }
1548
1549 return rc;
1550}
1551
1552
1553/**
1554 * @interface_method_impl{USBPROXYBACK,pfnSetConfig}
1555 */
1556static DECLCALLBACK(int) usbProxyUsbIpSetConfig(PUSBPROXYDEV pProxyDev, int iCfg)
1557{
1558 LogFlowFunc(("pProxyDev=%s cfg=%#x\n", pProxyDev->pUsbIns->pszName, iCfg));
1559
1560 PUSBPROXYDEVUSBIP pProxyDevUsbIp = USBPROXYDEV_2_DATA(pProxyDev, PUSBPROXYDEVUSBIP);
1561 VUSBSETUP Setup;
1562
1563 Setup.bmRequestType = 0;
1564 Setup.bRequest = 0x09;
1565 Setup.wValue = iCfg;
1566 Setup.wIndex = 0;
1567 Setup.wLength = 0;
1568 return usbProxyUsbIpCtrlUrbExchangeSync(pProxyDevUsbIp, &Setup);
1569}
1570
1571
1572/**
1573 * @interface_method_impl{USBPROXYBACK,pfnClaimInterface}
1574 */
1575static DECLCALLBACK(int) usbProxyUsbIpClaimInterface(PUSBPROXYDEV pProxyDev, int iIf)
1576{
1577 RT_NOREF(pProxyDev, iIf);
1578 LogFlowFunc(("pProxyDev=%s iIf=%#x\n", pProxyDev->pUsbIns->pszName, iIf));
1579 return VINF_SUCCESS;
1580}
1581
1582
1583/**
1584 * @interface_method_impl{USBPROXYBACK,pfnReleaseInterface}
1585 */
1586static DECLCALLBACK(int) usbProxyUsbIpReleaseInterface(PUSBPROXYDEV pProxyDev, int iIf)
1587{
1588 RT_NOREF(pProxyDev, iIf);
1589 LogFlowFunc(("pProxyDev=%s iIf=%#x\n", pProxyDev->pUsbIns->pszName, iIf));
1590 return VINF_SUCCESS;
1591}
1592
1593
1594/**
1595 * @interface_method_impl{USBPROXYBACK,pfnSetInterface}
1596 */
1597static DECLCALLBACK(int) usbProxyUsbIpSetInterface(PUSBPROXYDEV pProxyDev, int iIf, int setting)
1598{
1599 LogFlowFunc(("pProxyDev=%p iIf=%#x setting=%#x\n", pProxyDev, iIf, setting));
1600
1601 PUSBPROXYDEVUSBIP pProxyDevUsbIp = USBPROXYDEV_2_DATA(pProxyDev, PUSBPROXYDEVUSBIP);
1602 VUSBSETUP Setup;
1603
1604 Setup.bmRequestType = 0x1;
1605 Setup.bRequest = 0x0b; /* SET_INTERFACE */
1606 Setup.wValue = setting;
1607 Setup.wIndex = iIf;
1608 Setup.wLength = 0;
1609 return usbProxyUsbIpCtrlUrbExchangeSync(pProxyDevUsbIp, &Setup);
1610}
1611
1612
1613/**
1614 * @interface_method_impl{USBPROXYBACK,pfnClearHaltedEndpoint}
1615 */
1616static DECLCALLBACK(int) usbProxyUsbIpClearHaltedEp(PUSBPROXYDEV pProxyDev, unsigned int iEp)
1617{
1618 LogFlowFunc(("pProxyDev=%s ep=%u\n", pProxyDev->pUsbIns->pszName, iEp));
1619
1620 PUSBPROXYDEVUSBIP pProxyDevUsbIp = USBPROXYDEV_2_DATA(pProxyDev, PUSBPROXYDEVUSBIP);
1621 VUSBSETUP Setup;
1622
1623 Setup.bmRequestType = 0x2;
1624 Setup.bRequest = 0x01; /* CLEAR_FEATURE */
1625 Setup.wValue = 0x00; /* ENDPOINT_HALT */
1626 Setup.wIndex = iEp;
1627 Setup.wLength = 0;
1628 return usbProxyUsbIpCtrlUrbExchangeSync(pProxyDevUsbIp, &Setup);
1629}
1630
1631
1632/**
1633 * @interface_method_impl{USBPROXYBACK,pfnUrbQueue}
1634 */
1635static DECLCALLBACK(int) usbProxyUsbIpUrbQueue(PUSBPROXYDEV pProxyDev, PVUSBURB pUrb)
1636{
1637 LogFlowFunc(("pUrb=%p\n", pUrb));
1638
1639 PUSBPROXYDEVUSBIP pProxyDevUsbIp = USBPROXYDEV_2_DATA(pProxyDev, PUSBPROXYDEVUSBIP);
1640
1641 /* Allocate a USB/IP Urb. */
1642 PUSBPROXYURBUSBIP pUrbUsbIp = usbProxyUsbIpUrbAlloc(pProxyDevUsbIp);
1643 if (!pUrbUsbIp)
1644 return VERR_NO_MEMORY;
1645
1646 pUrbUsbIp->fCancelled = false;
1647 pUrbUsbIp->pVUsbUrb = pUrb;
1648 pUrb->Dev.pvPrivate = pUrbUsbIp;
1649
1650 int rc = RTSemFastMutexRequest(pProxyDevUsbIp->hMtxLists);
1651 AssertRC(rc);
1652 RTListAppend(&pProxyDevUsbIp->ListUrbsToQueue, &pUrbUsbIp->NodeList);
1653 RTSemFastMutexRelease(pProxyDevUsbIp->hMtxLists);
1654
1655 return usbProxyReaperKick(pProxyDevUsbIp, USBIP_REAPER_WAKEUP_REASON_QUEUE);
1656}
1657
1658
1659/**
1660 * @interface_method_impl{USBPROXYBACK,pfnUrbReap}
1661 */
1662static DECLCALLBACK(PVUSBURB) usbProxyUsbIpUrbReap(PUSBPROXYDEV pProxyDev, RTMSINTERVAL cMillies)
1663{
1664 LogFlowFunc(("pProxyDev=%s\n", pProxyDev->pUsbIns->pszName));
1665
1666 PUSBPROXYDEVUSBIP pProxyDevUsbIp = USBPROXYDEV_2_DATA(pProxyDev, PUSBPROXYDEVUSBIP);
1667 PUSBPROXYURBUSBIP pUrbUsbIp = NULL;
1668 PVUSBURB pUrb = NULL;
1669 int rc = VINF_SUCCESS;
1670
1671 /* Queue new URBs first. */
1672 rc = usbProxyUsbIpUrbsQueuePending(pProxyDevUsbIp);
1673 AssertRC(rc);
1674
1675 /* Any URBs pending delivery? */
1676 if (!RTListIsEmpty(&pProxyDevUsbIp->ListUrbsLanded))
1677 pUrbUsbIp = RTListGetFirst(&pProxyDevUsbIp->ListUrbsLanded, USBPROXYURBUSBIP, NodeList);
1678 else
1679 pUrbUsbIp = usbProxyUsbIpPollWorker(pProxyDevUsbIp, 0, true /*fPollWakePipe*/, cMillies);
1680
1681 if (pUrbUsbIp)
1682 {
1683 pUrb = pUrbUsbIp->pVUsbUrb;
1684 pUrb->enmStatus = pUrbUsbIp->enmStatus;
1685
1686 /* unlink from the pending delivery list */
1687 usbProxyUsbIpUnlinkUrb(pProxyDevUsbIp, pUrbUsbIp);
1688 usbProxyUsbIpUrbFree(pProxyDevUsbIp, pUrbUsbIp);
1689 }
1690
1691 return pUrb;
1692}
1693
1694
1695/**
1696 * @interface_method_impl{USBPROXYBACK,pfnUrbCancel}
1697 */
1698static DECLCALLBACK(int) usbProxyUsbIpUrbCancel(PUSBPROXYDEV pProxyDev, PVUSBURB pUrb)
1699{
1700 LogFlowFunc(("pUrb=%p\n", pUrb));
1701
1702 PUSBPROXYDEVUSBIP pProxyDevUsbIp = USBPROXYDEV_2_DATA(pProxyDev, PUSBPROXYDEVUSBIP);
1703 PUSBPROXYURBUSBIP pUrbUsbIp = (PUSBPROXYURBUSBIP)pUrb->Dev.pvPrivate;
1704 UsbIpReqUnlink ReqUnlink;
1705
1706 RT_ZERO(ReqUnlink);
1707
1708 uint32_t u32SeqNum = usbProxyUsbIpSeqNumGet(pProxyDevUsbIp);
1709 ReqUnlink.Hdr.u32ReqRet = USBIP_CMD_UNLINK;
1710 ReqUnlink.Hdr.u32SeqNum = u32SeqNum;
1711 ReqUnlink.Hdr.u32DevId = pProxyDevUsbIp->u32DevId;
1712 ReqUnlink.Hdr.u32Direction = USBIP_DIR_OUT;
1713 ReqUnlink.Hdr.u32Endpoint = pUrb->EndPt;
1714 ReqUnlink.u32SeqNum = pUrbUsbIp->u32SeqNumUrb;
1715
1716 usbProxyUsbIpReqUnlinkH2N(&ReqUnlink);
1717 int rc = RTTcpWrite(pProxyDevUsbIp->hSocket, &ReqUnlink, sizeof(ReqUnlink));
1718 if (RT_SUCCESS(rc))
1719 {
1720 pUrbUsbIp->u32SeqNumUrbUnlink = u32SeqNum;
1721 pUrbUsbIp->fCancelled = true;
1722 }
1723
1724 return rc;
1725}
1726
1727
1728/**
1729 * @interface_method_impl{USBPROXYBACK,pfnWakeup}
1730 */
1731static DECLCALLBACK(int) usbProxyUsbIpWakeup(PUSBPROXYDEV pProxyDev)
1732{
1733 LogFlowFunc(("pProxyDev=%s\n", pProxyDev->pUsbIns->pszName));
1734
1735 PUSBPROXYDEVUSBIP pProxyDevUsbIp = USBPROXYDEV_2_DATA(pProxyDev, PUSBPROXYDEVUSBIP);
1736 return usbProxyReaperKick(pProxyDevUsbIp, USBIP_REAPER_WAKEUP_REASON_EXTERNAL);
1737}
1738
1739
1740/**
1741 * The USB/IP USB Proxy Backend operations.
1742 */
1743extern const USBPROXYBACK g_USBProxyDeviceUsbIp =
1744{
1745 /* pszName */
1746 "usbip",
1747 /* cbBackend */
1748 sizeof(USBPROXYDEVUSBIP),
1749 usbProxyUsbIpOpen,
1750 NULL,
1751 usbProxyUsbIpClose,
1752 usbProxyUsbIpReset,
1753 usbProxyUsbIpSetConfig,
1754 usbProxyUsbIpClaimInterface,
1755 usbProxyUsbIpReleaseInterface,
1756 usbProxyUsbIpSetInterface,
1757 usbProxyUsbIpClearHaltedEp,
1758 usbProxyUsbIpUrbQueue,
1759 usbProxyUsbIpUrbCancel,
1760 usbProxyUsbIpUrbReap,
1761 usbProxyUsbIpWakeup,
1762 0
1763};
1764
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