VirtualBox

source: vbox/trunk/src/VBox/Devices/USB/VUSBInternal.h@ 85194

Last change on this file since 85194 was 85121, checked in by vboxsync, 4 years ago

iprt/cdefs.h: Refactored the typedef use of DECLCALLBACK as well as DECLCALLBACKMEMBER to wrap the whole expression, similar to the DECLR?CALLBACKMEMBER macros. This allows adding a throw() at the end when compiling with the VC++ compiler to indicate that the callbacks won't throw anything, so we can stop supressing the C5039 warning about passing functions that can potential throw C++ exceptions to extern C code that can't necessarily cope with such (unwind,++). Introduced a few _EX variations that allows specifying different/no calling convention too, as that's handy when dynamically resolving host APIs. Fixed numerous places missing DECLCALLBACK and such. Left two angry @todos regarding use of CreateThread. bugref:9794

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 23.0 KB
Line 
1/* $Id: VUSBInternal.h 85121 2020-07-08 19:33:26Z vboxsync $ */
2/** @file
3 * Virtual USB - Internal header.
4 *
5 * This subsystem implements USB devices in a host controller independent
6 * way. All the host controller code has to do is use VUSBHUB for its
7 * root hub implementation and any emulated USB device may be plugged into
8 * the virtual bus.
9 */
10
11/*
12 * Copyright (C) 2006-2020 Oracle Corporation
13 *
14 * This file is part of VirtualBox Open Source Edition (OSE), as
15 * available from http://www.virtualbox.org. This file is free software;
16 * you can redistribute it and/or modify it under the terms of the GNU
17 * General Public License (GPL) as published by the Free Software
18 * Foundation, in version 2 as it comes in the "COPYING" file of the
19 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
20 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
21 */
22
23#ifndef VBOX_INCLUDED_SRC_USB_VUSBInternal_h
24#define VBOX_INCLUDED_SRC_USB_VUSBInternal_h
25#ifndef RT_WITHOUT_PRAGMA_ONCE
26# pragma once
27#endif
28
29#include <VBox/cdefs.h>
30#include <VBox/types.h>
31#include <VBox/vusb.h>
32#include <VBox/vmm/stam.h>
33#include <VBox/vmm/pdm.h>
34#include <VBox/vmm/vmapi.h>
35#include <VBox/vmm/pdmusb.h>
36#include <iprt/asm.h>
37#include <iprt/assert.h>
38#include <iprt/req.h>
39#include <iprt/asm.h>
40#include <iprt/list.h>
41
42#include "VUSBSniffer.h"
43
44RT_C_DECLS_BEGIN
45
46
47/** @defgroup grp_vusb_int VUSB Internals.
48 * @ingroup grp_vusb
49 * @internal
50 * @{
51 */
52
53/** @defgroup grp_vusb_int_dev Internal Device Operations, Structures and Constants.
54 * @{
55 */
56
57/** Pointer to a Virtual USB device (core). */
58typedef struct VUSBDEV *PVUSBDEV;
59/** Pointer to a VUSB hub device. */
60typedef struct VUSBHUB *PVUSBHUB;
61/** Pointer to a VUSB root hub. */
62typedef struct VUSBROOTHUB *PVUSBROOTHUB;
63
64
65/** Number of the default control endpoint */
66#define VUSB_PIPE_DEFAULT 0
67
68/** @name Device addresses
69 * @{ */
70#define VUSB_DEFAULT_ADDRESS 0
71#define VUSB_INVALID_ADDRESS UINT8_C(0xff)
72/** @} */
73
74/** @name Feature bits (1<<FEATURE for the u16Status bit)
75 * @{ */
76#define VUSB_DEV_SELF_POWERED 0
77#define VUSB_DEV_REMOTE_WAKEUP 1
78#define VUSB_EP_HALT 0
79/** @} */
80
81/** Maximum number of endpoint addresses */
82#define VUSB_PIPE_MAX 16
83
84/**
85 * The VUSB URB data.
86 */
87typedef struct VUSBURBVUSBINT
88{
89 /** Node for one of the lists the URB can be in. */
90 RTLISTNODE NdLst;
91 /** Pointer to the URB this structure is part of. */
92 PVUSBURB pUrb;
93 /** Pointer to the original for control messages. */
94 PVUSBURB pCtrlUrb;
95 /** Pointer to the VUSB device.
96 * This may be NULL if the destination address is invalid. */
97 PVUSBDEV pDev;
98 /** Specific to the pfnFree function. */
99 void *pvFreeCtx;
100 /**
101 * Callback which will free the URB once it's reaped and completed.
102 * @param pUrb The URB.
103 */
104 DECLCALLBACKMEMBER(void, pfnFree,(PVUSBURB pUrb));
105 /** Submit timestamp. (logging only) */
106 uint64_t u64SubmitTS;
107} VUSBURBVUSBINT;
108
109/**
110 * Control-pipe stages.
111 */
112typedef enum CTLSTAGE
113{
114 /** the control pipe is in the setup stage. */
115 CTLSTAGE_SETUP = 0,
116 /** the control pipe is in the data stage. */
117 CTLSTAGE_DATA,
118 /** the control pipe is in the status stage. */
119 CTLSTAGE_STATUS
120} CTLSTAGE;
121
122/**
123 * Extra data for a control pipe.
124 *
125 * This is state information needed for the special multi-stage
126 * transfers performed on this kind of pipes.
127 */
128typedef struct vusb_ctrl_extra
129{
130 /** Current pipe stage. */
131 CTLSTAGE enmStage;
132 /** Success indicator. */
133 bool fOk;
134 /** Set if the message URB has been submitted. */
135 bool fSubmitted;
136 /** Pointer to the SETUP.
137 * This is a pointer to Urb->abData[0]. */
138 PVUSBSETUP pMsg;
139 /** Current DATA pointer.
140 * This starts at pMsg + 1 and is incremented at we read/write data. */
141 uint8_t *pbCur;
142 /** The amount of data left to read on IN operations.
143 * On OUT operations this is not used. */
144 uint32_t cbLeft;
145 /** The amount of data we can house.
146 * This starts at the default 8KB, and this structure will be reallocated to
147 * accommodate any larger request (unlikely). */
148 uint32_t cbMax;
149 /** VUSB internal data for the extra URB. */
150 VUSBURBVUSBINT VUsbExtra;
151 /** The message URB. */
152 VUSBURB Urb;
153} VUSBCTRLEXTRA, *PVUSBCTRLEXTRA;
154
155void vusbMsgFreeExtraData(PVUSBCTRLEXTRA pExtra);
156void vusbMsgResetExtraData(PVUSBCTRLEXTRA pExtra);
157
158/**
159 * A VUSB pipe
160 */
161typedef struct vusb_pipe
162{
163 PCVUSBDESCENDPOINTEX in;
164 PCVUSBDESCENDPOINTEX out;
165 /** Pointer to the extra state data required to run a control pipe. */
166 PVUSBCTRLEXTRA pCtrl;
167 /** Critical section serializing access to the extra state data for a control pipe. */
168 RTCRITSECT CritSectCtrl;
169 /** Count of active async transfers. */
170 volatile uint32_t async;
171 /** Last scheduled frame - only valid for isochronous IN endpoints. */
172 uint32_t uLastFrameIn;
173 /** Last scheduled frame - only valid for isochronous OUT endpoints. */
174 uint32_t uLastFrameOut;
175} VUSBPIPE;
176/** Pointer to a VUSB pipe structure. */
177typedef VUSBPIPE *PVUSBPIPE;
178
179
180/**
181 * Interface state and possible settings.
182 */
183typedef struct vusb_interface_state
184{
185 /** Pointer to the interface descriptor of the currently selected (active)
186 * interface. */
187 PCVUSBDESCINTERFACEEX pCurIfDesc;
188 /** Pointer to the interface settings. */
189 PCVUSBINTERFACE pIf;
190} VUSBINTERFACESTATE;
191/** Pointer to interface state. */
192typedef VUSBINTERFACESTATE *PVUSBINTERFACESTATE;
193/** Pointer to const interface state. */
194typedef const VUSBINTERFACESTATE *PCVUSBINTERFACESTATE;
195
196
197/**
198 * VUSB URB pool.
199 */
200typedef struct VUSBURBPOOL
201{
202 /** Critical section protecting the pool. */
203 RTCRITSECT CritSectPool;
204 /** Chain of free URBs by type. (Singly linked) */
205 RTLISTANCHOR aLstFreeUrbs[VUSBXFERTYPE_ELEMENTS];
206 /** The number of URBs in the pool. */
207 volatile uint32_t cUrbsInPool;
208 /** Align the size to a 8 byte boundary. */
209 uint32_t Alignment0;
210} VUSBURBPOOL;
211/** Pointer to a VUSB URB pool. */
212typedef VUSBURBPOOL *PVUSBURBPOOL;
213
214AssertCompileSizeAlignment(VUSBURBPOOL, 8);
215
216/**
217 * A Virtual USB device (core).
218 *
219 * @implements VUSBIDEVICE
220 */
221typedef struct VUSBDEV
222{
223 /** The device interface exposed to the HCI. */
224 VUSBIDEVICE IDevice;
225 /** Pointer to the PDM USB device instance. */
226 PPDMUSBINS pUsbIns;
227 /** Next device in the chain maintained by the roothub. */
228 PVUSBDEV pNext;
229 /** Pointer to the next device with the same address hash. */
230 PVUSBDEV pNextHash;
231 /** Pointer to the hub this device is attached to. */
232 PVUSBHUB pHub;
233 /** The device state. */
234 VUSBDEVICESTATE volatile enmState;
235 /** Reference counter to protect the device structure from going away. */
236 uint32_t volatile cRefs;
237
238 /** The device address. */
239 uint8_t u8Address;
240 /** The new device address. */
241 uint8_t u8NewAddress;
242 /** The port. */
243 int16_t i16Port;
244 /** Device status. (VUSB_DEV_SELF_POWERED or not.) */
245 uint16_t u16Status;
246
247 /** Pointer to the descriptor cache.
248 * (Provided by the device thru the pfnGetDescriptorCache method.) */
249 PCPDMUSBDESCCACHE pDescCache;
250 /** Current configuration. */
251 PCVUSBDESCCONFIGEX pCurCfgDesc;
252
253 /** Current interface state (including alternate interface setting) - maximum
254 * valid index is config->bNumInterfaces
255 */
256 PVUSBINTERFACESTATE paIfStates;
257
258 /** Pipe/direction -> endpoint descriptor mapping */
259 VUSBPIPE aPipes[VUSB_PIPE_MAX];
260 /** Critical section protecting the active URB list. */
261 RTCRITSECT CritSectAsyncUrbs;
262 /** List of active async URBs. */
263 RTLISTANCHOR LstAsyncUrbs;
264
265 /** Dumper state. */
266 union VUSBDEVURBDUMPERSTATE
267 {
268 /** The current scsi command. */
269 uint8_t u8ScsiCmd;
270 } Urb;
271
272 /** The reset timer handle. */
273 PTMTIMER pResetTimer;
274 /** Reset handler arguments. */
275 void *pvArgs;
276 /** URB submit and reap thread. */
277 RTTHREAD hUrbIoThread;
278 /** Request queue for executing tasks on the I/O thread which should be done
279 * synchronous and without any other thread accessing the USB device. */
280 RTREQQUEUE hReqQueueSync;
281 /** Sniffer instance for this device if configured. */
282 VUSBSNIFFER hSniffer;
283 /** Flag whether the URB I/O thread should terminate. */
284 bool volatile fTerminate;
285 /** Flag whether the I/O thread was woken up. */
286 bool volatile fWokenUp;
287#if HC_ARCH_BITS == 32
288 /** Align the size to a 8 byte boundary. */
289 bool afAlignment0[2];
290#endif
291 /** The pool of free URBs for faster allocation. */
292 VUSBURBPOOL UrbPool;
293} VUSBDEV;
294AssertCompileSizeAlignment(VUSBDEV, 8);
295
296
297int vusbDevInit(PVUSBDEV pDev, PPDMUSBINS pUsbIns, const char *pszCaptureFilename);
298void vusbDevDestroy(PVUSBDEV pDev);
299
300DECLINLINE(bool) vusbDevIsRh(PVUSBDEV pDev)
301{
302 return (pDev->pHub == (PVUSBHUB)pDev);
303}
304
305bool vusbDevDoSelectConfig(PVUSBDEV dev, PCVUSBDESCCONFIGEX pCfg);
306void vusbDevMapEndpoint(PVUSBDEV dev, PCVUSBDESCENDPOINTEX ep);
307int vusbDevDetach(PVUSBDEV pDev);
308int vusbDevAttach(PVUSBDEV pDev, PVUSBHUB pHub);
309DECLINLINE(PVUSBROOTHUB) vusbDevGetRh(PVUSBDEV pDev);
310size_t vusbDevMaxInterfaces(PVUSBDEV dev);
311
312void vusbDevSetAddress(PVUSBDEV pDev, uint8_t u8Address);
313bool vusbDevStandardRequest(PVUSBDEV pDev, int EndPt, PVUSBSETUP pSetup, void *pvBuf, uint32_t *pcbBuf);
314
315
316/** @} */
317
318
319/** @defgroup grp_vusb_int_hub Internal Hub Operations, Structures and Constants.
320 * @{
321 */
322
323
324/** Virtual method table for USB hub devices.
325 * Hub and roothub drivers need to implement these functions in addition to the
326 * vusb_dev_ops.
327 */
328typedef struct VUSBHUBOPS
329{
330 int (*pfnAttach)(PVUSBHUB pHub, PVUSBDEV pDev);
331 void (*pfnDetach)(PVUSBHUB pHub, PVUSBDEV pDev);
332} VUSBHUBOPS;
333/** Pointer to a const HUB method table. */
334typedef const VUSBHUBOPS *PCVUSBHUBOPS;
335
336/** A VUSB Hub Device - Hub and roothub drivers need to use this struct
337 * @todo eliminate this (PDM / roothubs only).
338 */
339typedef struct VUSBHUB
340{
341 VUSBDEV Dev;
342 PCVUSBHUBOPS pOps;
343 PVUSBROOTHUB pRootHub;
344 uint16_t cPorts;
345 uint16_t cDevices;
346 /** Name of the hub. Used for logging. */
347 char *pszName;
348} VUSBHUB;
349AssertCompileMemberAlignment(VUSBHUB, pOps, 8);
350AssertCompileSizeAlignment(VUSBHUB, 8);
351
352/** @} */
353
354
355/** @defgroup grp_vusb_int_roothub Internal Root Hub Operations, Structures and Constants.
356 * @{
357 */
358
359/**
360 * Per transfer type statistics.
361 */
362typedef struct VUSBROOTHUBTYPESTATS
363{
364 STAMCOUNTER StatUrbsSubmitted;
365 STAMCOUNTER StatUrbsFailed;
366 STAMCOUNTER StatUrbsCancelled;
367
368 STAMCOUNTER StatReqBytes;
369 STAMCOUNTER StatReqReadBytes;
370 STAMCOUNTER StatReqWriteBytes;
371
372 STAMCOUNTER StatActBytes;
373 STAMCOUNTER StatActReadBytes;
374 STAMCOUNTER StatActWriteBytes;
375} VUSBROOTHUBTYPESTATS, *PVUSBROOTHUBTYPESTATS;
376
377
378
379/** The address hash table size. */
380#define VUSB_ADDR_HASHSZ 5
381
382/**
383 * The instance data of a root hub driver.
384 *
385 * This extends the generic VUSB hub.
386 *
387 * @implements VUSBIROOTHUBCONNECTOR
388 */
389typedef struct VUSBROOTHUB
390{
391 /** The HUB.
392 * @todo remove this? */
393 VUSBHUB Hub;
394 /** Address hash table. */
395 PVUSBDEV apAddrHash[VUSB_ADDR_HASHSZ];
396 /** The default address. */
397 PVUSBDEV pDefaultAddress;
398
399 /** Pointer to the driver instance. */
400 PPDMDRVINS pDrvIns;
401 /** Pointer to the root hub port interface we're attached to. */
402 PVUSBIROOTHUBPORT pIRhPort;
403 /** Connector interface exposed upwards. */
404 VUSBIROOTHUBCONNECTOR IRhConnector;
405
406 /** Critical section protecting the device list. */
407 RTCRITSECT CritSectDevices;
408 /** Chain of devices attached to this hub. */
409 PVUSBDEV pDevices;
410
411#if HC_ARCH_BITS == 32
412 uint32_t Alignment0;
413#endif
414
415 /** Availability Bitmap. */
416 VUSBPORTBITMAP Bitmap;
417
418 /** Sniffer instance for the root hub. */
419 VUSBSNIFFER hSniffer;
420 /** Version of the attached Host Controller. */
421 uint32_t fHcVersions;
422 /** Size of the HCI specific data for each URB. */
423 size_t cbHci;
424 /** Size of the HCI specific TD. */
425 size_t cbHciTd;
426
427 /** The periodic frame processing thread. */
428 R3PTRTYPE(PPDMTHREAD) hThreadPeriodFrame;
429 /** Event semaphore to interact with the periodic frame processing thread. */
430 R3PTRTYPE(RTSEMEVENTMULTI) hSemEventPeriodFrame;
431 /** Event semaphore to release the thread waiting for the periodic frame processing thread to stop. */
432 R3PTRTYPE(RTSEMEVENTMULTI) hSemEventPeriodFrameStopped;
433 /** Current default frame rate for periodic frame processing thread. */
434 volatile uint32_t uFrameRateDefault;
435 /** Current frame rate (can be lower than the default frame rate if there is no activity). */
436 uint32_t uFrameRate;
437 /** How long to wait until the next frame. */
438 uint64_t nsWait;
439 /** Timestamp when the last frame was processed. */
440 uint64_t tsFrameProcessed;
441 /** Number of USB work cycles with no transfers. */
442 uint32_t cIdleCycles;
443
444 /** Flag whether a frame is currently being processed. */
445 volatile bool fFrameProcessing;
446
447#if HC_ARCH_BITS == 32
448 uint32_t Alignment1;
449#endif
450
451#ifdef LOG_ENABLED
452 /** A serial number for URBs submitted on the roothub instance.
453 * Only logging builds. */
454 uint32_t iSerial;
455 /** Alignment */
456 uint32_t Alignment2;
457#endif
458#ifdef VBOX_WITH_STATISTICS
459 VUSBROOTHUBTYPESTATS Total;
460 VUSBROOTHUBTYPESTATS aTypes[VUSBXFERTYPE_MSG];
461 STAMCOUNTER StatIsocReqPkts;
462 STAMCOUNTER StatIsocReqReadPkts;
463 STAMCOUNTER StatIsocReqWritePkts;
464 STAMCOUNTER StatIsocActPkts;
465 STAMCOUNTER StatIsocActReadPkts;
466 STAMCOUNTER StatIsocActWritePkts;
467 struct
468 {
469 STAMCOUNTER Pkts;
470 STAMCOUNTER Ok;
471 STAMCOUNTER Ok0;
472 STAMCOUNTER DataUnderrun;
473 STAMCOUNTER DataUnderrun0;
474 STAMCOUNTER DataOverrun;
475 STAMCOUNTER NotAccessed;
476 STAMCOUNTER Misc;
477 STAMCOUNTER Bytes;
478 } aStatIsocDetails[8];
479
480 STAMPROFILE StatReapAsyncUrbs;
481 STAMPROFILE StatSubmitUrb;
482 STAMCOUNTER StatFramesProcessedClbk;
483 STAMCOUNTER StatFramesProcessedThread;
484#endif
485} VUSBROOTHUB;
486AssertCompileMemberAlignment(VUSBROOTHUB, IRhConnector, 8);
487AssertCompileMemberAlignment(VUSBROOTHUB, Bitmap, 8);
488AssertCompileMemberAlignment(VUSBROOTHUB, CritSectDevices, 8);
489#ifdef VBOX_WITH_STATISTICS
490AssertCompileMemberAlignment(VUSBROOTHUB, Total, 8);
491#endif
492
493/** Converts a pointer to VUSBROOTHUB::IRhConnector to a PVUSBROOTHUB. */
494#define VUSBIROOTHUBCONNECTOR_2_VUSBROOTHUB(pInterface) (PVUSBROOTHUB)( (uintptr_t)(pInterface) - RT_UOFFSETOF(VUSBROOTHUB, IRhConnector) )
495
496/**
497 * URB cancellation modes
498 */
499typedef enum CANCELMODE
500{
501 /** complete the URB with an error (CRC). */
502 CANCELMODE_FAIL = 0,
503 /** do not change the URB contents. */
504 CANCELMODE_UNDO
505} CANCELMODE;
506
507/** @} */
508
509
510
511/** @defgroup grp_vusb_int_urb Internal URB Operations, Structures and Constants.
512 * @{ */
513int vusbUrbSubmit(PVUSBURB pUrb);
514void vusbUrbDoReapAsync(PRTLISTANCHOR pUrbLst, RTMSINTERVAL cMillies);
515void vusbUrbDoReapAsyncDev(PVUSBDEV pDev, RTMSINTERVAL cMillies);
516void vusbUrbCancel(PVUSBURB pUrb, CANCELMODE mode);
517void vusbUrbCancelAsync(PVUSBURB pUrb, CANCELMODE mode);
518void vusbUrbRipe(PVUSBURB pUrb);
519void vusbUrbCompletionRh(PVUSBURB pUrb);
520int vusbUrbSubmitHardError(PVUSBURB pUrb);
521int vusbUrbErrorRh(PVUSBURB pUrb);
522int vusbDevUrbIoThreadWakeup(PVUSBDEV pDev);
523int vusbDevUrbIoThreadCreate(PVUSBDEV pDev);
524int vusbDevUrbIoThreadDestroy(PVUSBDEV pDev);
525DECLHIDDEN(void) vusbDevCancelAllUrbs(PVUSBDEV pDev, bool fDetaching);
526DECLHIDDEN(int) vusbDevIoThreadExecV(PVUSBDEV pDev, uint32_t fFlags, PFNRT pfnFunction, unsigned cArgs, va_list Args);
527DECLHIDDEN(int) vusbDevIoThreadExec(PVUSBDEV pDev, uint32_t fFlags, PFNRT pfnFunction, unsigned cArgs, ...);
528DECLHIDDEN(int) vusbDevIoThreadExecSync(PVUSBDEV pDev, PFNRT pfnFunction, unsigned cArgs, ...);
529DECLHIDDEN(int) vusbUrbCancelWorker(PVUSBURB pUrb, CANCELMODE enmMode);
530
531DECLHIDDEN(uint64_t) vusbRhR3ProcessFrame(PVUSBROOTHUB pThis, bool fCallback);
532
533int vusbUrbQueueAsyncRh(PVUSBURB pUrb);
534
535/**
536 * Initializes the given URB pool.
537 *
538 * @returns VBox status code.
539 * @param pUrbPool The URB pool to initialize.
540 */
541DECLHIDDEN(int) vusbUrbPoolInit(PVUSBURBPOOL pUrbPool);
542
543/**
544 * Destroy a given URB pool freeing all ressources.
545 *
546 * @returns nothing.
547 * @param pUrbPool The URB pool to destroy.
548 */
549DECLHIDDEN(void) vusbUrbPoolDestroy(PVUSBURBPOOL pUrbPool);
550
551/**
552 * Allocate a new URB from the given URB pool.
553 *
554 * @returns Pointer to the new URB or NULL if out of memory.
555 * @param pUrbPool The URB pool to allocate from.
556 * @param enmType Type of the URB.
557 * @param enmDir The direction of the URB.
558 * @param cbData The number of bytes to allocate for the data buffer.
559 * @param cbHci Size of the data private to the HCI for each URB when allocated.
560 * @param cbHciTd Size of one transfer descriptor.
561 * @param cTds Number of transfer descriptors.
562 */
563DECLHIDDEN(PVUSBURB) vusbUrbPoolAlloc(PVUSBURBPOOL pUrbPool, VUSBXFERTYPE enmType,
564 VUSBDIRECTION enmDir, size_t cbData,
565 size_t cbHci, size_t cbHciTd, unsigned cTds);
566
567/**
568 * Frees a given URB.
569 *
570 * @returns nothing.
571 * @param pUrbPool The URB pool the URB was allocated from.
572 * @param pUrb The URB to free.
573 */
574DECLHIDDEN(void) vusbUrbPoolFree(PVUSBURBPOOL pUrbPool, PVUSBURB pUrb);
575
576#ifdef LOG_ENABLED
577/**
578 * Logs an URB in the debug log.
579 *
580 * @returns nothing.
581 * @param pUrb The URB to log.
582 * @param pszMsg Additional message to log.
583 * @param fComplete Flag whther the URB is completing.
584 */
585DECLHIDDEN(void) vusbUrbTrace(PVUSBURB pUrb, const char *pszMsg, bool fComplete);
586
587/**
588 * Return the USB direction as a string from the given enum.
589 */
590DECLHIDDEN(const char *) vusbUrbDirName(VUSBDIRECTION enmDir);
591
592/**
593 * Return the URB type as string from the given enum.
594 */
595DECLHIDDEN(const char *) vusbUrbTypeName(VUSBXFERTYPE enmType);
596
597/**
598 * Return the URB status as string from the given enum.
599 */
600DECLHIDDEN(const char *) vusbUrbStatusName(VUSBSTATUS enmStatus);
601#endif
602
603DECLINLINE(void) vusbUrbUnlink(PVUSBURB pUrb)
604{
605 PVUSBDEV pDev = pUrb->pVUsb->pDev;
606
607 RTCritSectEnter(&pDev->CritSectAsyncUrbs);
608 RTListNodeRemove(&pUrb->pVUsb->NdLst);
609 RTCritSectLeave(&pDev->CritSectAsyncUrbs);
610}
611
612/** @def vusbUrbAssert
613 * Asserts that a URB is valid.
614 */
615#ifdef VBOX_STRICT
616# define vusbUrbAssert(pUrb) do { \
617 AssertMsg(VALID_PTR((pUrb)), ("%p\n", (pUrb))); \
618 AssertMsg((pUrb)->u32Magic == VUSBURB_MAGIC, ("%#x", (pUrb)->u32Magic)); \
619 AssertMsg((pUrb)->enmState > VUSBURBSTATE_INVALID && (pUrb)->enmState < VUSBURBSTATE_END, \
620 ("%d\n", (pUrb)->enmState)); \
621 } while (0)
622#else
623# define vusbUrbAssert(pUrb) do {} while (0)
624#endif
625
626/**
627 * @def VUSBDEV_ASSERT_VALID_STATE
628 * Asserts that the give device state is valid.
629 */
630#define VUSBDEV_ASSERT_VALID_STATE(enmState) \
631 AssertMsg((enmState) > VUSB_DEVICE_STATE_INVALID && (enmState) < VUSB_DEVICE_STATE_DESTROYED, ("enmState=%#x\n", enmState));
632
633/** Executes a function synchronously. */
634#define VUSB_DEV_IO_THREAD_EXEC_FLAGS_SYNC RT_BIT_32(0)
635
636/** @} */
637
638
639
640
641/**
642 * Addresses are between 0 and 127 inclusive
643 */
644DECLINLINE(uint8_t) vusbHashAddress(uint8_t Address)
645{
646 uint8_t u8Hash = Address;
647 u8Hash ^= (Address >> 2);
648 u8Hash ^= (Address >> 3);
649 u8Hash %= VUSB_ADDR_HASHSZ;
650 return u8Hash;
651}
652
653
654/**
655 * Gets the roothub of a device.
656 *
657 * @returns Pointer to the roothub instance the device is attached to.
658 * @returns NULL if not attached to any hub.
659 * @param pDev Pointer to the device in question.
660 */
661DECLINLINE(PVUSBROOTHUB) vusbDevGetRh(PVUSBDEV pDev)
662{
663 if (!pDev->pHub)
664 return NULL;
665 return pDev->pHub->pRootHub;
666}
667
668
669/**
670 * Returns the state of the USB device.
671 *
672 * @returns State of the USB device.
673 * @param pDev Pointer to the device.
674 */
675DECLINLINE(VUSBDEVICESTATE) vusbDevGetState(PVUSBDEV pDev)
676{
677 VUSBDEVICESTATE enmState = (VUSBDEVICESTATE)ASMAtomicReadU32((volatile uint32_t *)&pDev->enmState);
678 VUSBDEV_ASSERT_VALID_STATE(enmState);
679 return enmState;
680}
681
682
683/**
684 * Sets the given state for the USB device.
685 *
686 * @returns The old state of the device.
687 * @param pDev Pointer to the device.
688 * @param enmState The new state to set.
689 */
690DECLINLINE(VUSBDEVICESTATE) vusbDevSetState(PVUSBDEV pDev, VUSBDEVICESTATE enmState)
691{
692 VUSBDEV_ASSERT_VALID_STATE(enmState);
693 VUSBDEVICESTATE enmStateOld = (VUSBDEVICESTATE)ASMAtomicXchgU32((volatile uint32_t *)&pDev->enmState, enmState);
694 VUSBDEV_ASSERT_VALID_STATE(enmStateOld);
695 return enmStateOld;
696}
697
698
699/**
700 * Compare and exchange the states for the given USB device.
701 *
702 * @returns true if the state was changed.
703 * @returns false if the state wasn't changed.
704 * @param pDev Pointer to the device.
705 * @param enmStateNew The new state to set.
706 * @param enmStateOld The old state to compare with.
707 */
708DECLINLINE(bool) vusbDevSetStateCmp(PVUSBDEV pDev, VUSBDEVICESTATE enmStateNew, VUSBDEVICESTATE enmStateOld)
709{
710 VUSBDEV_ASSERT_VALID_STATE(enmStateNew);
711 VUSBDEV_ASSERT_VALID_STATE(enmStateOld);
712 return ASMAtomicCmpXchgU32((volatile uint32_t *)&pDev->enmState, enmStateNew, enmStateOld);
713}
714
715/**
716 * Retains the given VUSB device pointer.
717 *
718 * @returns New reference count.
719 * @param pThis The VUSB device pointer.
720 */
721DECLINLINE(uint32_t) vusbDevRetain(PVUSBDEV pThis)
722{
723 AssertPtrReturn(pThis, UINT32_MAX);
724
725 uint32_t cRefs = ASMAtomicIncU32(&pThis->cRefs);
726 AssertMsg(cRefs > 1 && cRefs < _1M, ("%#x %p\n", cRefs, pThis));
727 return cRefs;
728}
729
730/**
731 * Releases the given VUSB device pointer.
732 *
733 * @returns New reference count.
734 * @retval 0 if no onw is holding a reference anymore causing the device to be destroyed.
735 */
736DECLINLINE(uint32_t) vusbDevRelease(PVUSBDEV pThis)
737{
738 AssertPtrReturn(pThis, UINT32_MAX);
739
740 uint32_t cRefs = ASMAtomicDecU32(&pThis->cRefs);
741 AssertMsg(cRefs < _1M, ("%#x %p\n", cRefs, pThis));
742 if (cRefs == 0)
743 vusbDevDestroy(pThis);
744 return cRefs;
745}
746
747/** Strings for the CTLSTAGE enum values. */
748extern const char * const g_apszCtlStates[4];
749
750/** @} */
751RT_C_DECLS_END
752#endif /* !VBOX_INCLUDED_SRC_USB_VUSBInternal_h */
753
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