VirtualBox

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

Last change on this file since 93955 was 93955, checked in by vboxsync, 3 years ago

Devices/USB: Eliminiate some callback ping-pong and streamline the device attach/detach logic, bugref:10196

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 22.4 KB
Line 
1/* $Id: VUSBInternal.h 93955 2022-02-25 16:13:44Z 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-2022 Oracle Corporation
13 *
14 * This file is part of VirtualBox Open Source Edition (OSE), as
15 * available from http://www.215389.xyz. 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 of devices with the default address. */
228 PVUSBDEV pNextDefAddr;
229 /** Pointer to the hub this device is attached to. */
230 PVUSBHUB pHub;
231 /** The device state. */
232 VUSBDEVICESTATE volatile enmState;
233 /** Reference counter to protect the device structure from going away. */
234 uint32_t volatile cRefs;
235
236 /** The device address. */
237 uint8_t u8Address;
238 /** The new device address. */
239 uint8_t u8NewAddress;
240 /** The port. */
241 int16_t i16Port;
242 /** Device status. (VUSB_DEV_SELF_POWERED or not.) */
243 uint16_t u16Status;
244
245 /** Pointer to the descriptor cache.
246 * (Provided by the device thru the pfnGetDescriptorCache method.) */
247 PCPDMUSBDESCCACHE pDescCache;
248 /** Current configuration. */
249 PCVUSBDESCCONFIGEX pCurCfgDesc;
250
251 /** Current interface state (including alternate interface setting) - maximum
252 * valid index is config->bNumInterfaces
253 */
254 PVUSBINTERFACESTATE paIfStates;
255
256 /** Pipe/direction -> endpoint descriptor mapping */
257 VUSBPIPE aPipes[VUSB_PIPE_MAX];
258 /** Critical section protecting the active URB list. */
259 RTCRITSECT CritSectAsyncUrbs;
260 /** List of active async URBs. */
261 RTLISTANCHOR LstAsyncUrbs;
262
263 /** Dumper state. */
264 union VUSBDEVURBDUMPERSTATE
265 {
266 /** The current scsi command. */
267 uint8_t u8ScsiCmd;
268 } Urb;
269
270 /** The reset timer handle. */
271 TMTIMERHANDLE hResetTimer;
272 /** Reset handler arguments. */
273 void *pvArgs;
274 /** URB submit and reap thread. */
275 RTTHREAD hUrbIoThread;
276 /** Request queue for executing tasks on the I/O thread which should be done
277 * synchronous and without any other thread accessing the USB device. */
278 RTREQQUEUE hReqQueueSync;
279 /** Sniffer instance for this device if configured. */
280 VUSBSNIFFER hSniffer;
281 /** Flag whether the URB I/O thread should terminate. */
282 bool volatile fTerminate;
283 /** Flag whether the I/O thread was woken up. */
284 bool volatile fWokenUp;
285#if HC_ARCH_BITS == 32
286 /** Align the size to a 8 byte boundary. */
287 bool afAlignment0[2];
288#endif
289 /** The pool of free URBs for faster allocation. */
290 VUSBURBPOOL UrbPool;
291} VUSBDEV;
292AssertCompileSizeAlignment(VUSBDEV, 8);
293
294
295int vusbDevInit(PVUSBDEV pDev, PPDMUSBINS pUsbIns, const char *pszCaptureFilename);
296void vusbDevDestroy(PVUSBDEV pDev);
297
298DECLINLINE(bool) vusbDevIsRh(PVUSBDEV pDev)
299{
300 return (pDev->pHub == (PVUSBHUB)pDev);
301}
302
303bool vusbDevDoSelectConfig(PVUSBDEV dev, PCVUSBDESCCONFIGEX pCfg);
304void vusbDevMapEndpoint(PVUSBDEV dev, PCVUSBDESCENDPOINTEX ep);
305int vusbDevDetach(PVUSBDEV pDev);
306int vusbDevAttach(PVUSBDEV pDev, PVUSBHUB pHub);
307DECLINLINE(PVUSBROOTHUB) vusbDevGetRh(PVUSBDEV pDev);
308size_t vusbDevMaxInterfaces(PVUSBDEV dev);
309
310void vusbDevSetAddress(PVUSBDEV pDev, uint8_t u8Address);
311bool vusbDevStandardRequest(PVUSBDEV pDev, int EndPt, PVUSBSETUP pSetup, void *pvBuf, uint32_t *pcbBuf);
312
313
314/** @} */
315
316
317/** @defgroup grp_vusb_int_hub Internal Hub Operations, Structures and Constants.
318 * @{
319 */
320
321
322/** A VUSB Hub Device - Hub and roothub drivers need to use this struct
323 * @todo eliminate this (PDM / roothubs only).
324 */
325typedef struct VUSBHUB
326{
327 VUSBDEV Dev;
328 PVUSBROOTHUB pRootHub;
329 uint16_t cPorts;
330 uint16_t cDevices;
331 /** Name of the hub. Used for logging. */
332 char *pszName;
333} VUSBHUB;
334AssertCompileSizeAlignment(VUSBHUB, 8);
335
336/** @} */
337
338
339/** @defgroup grp_vusb_int_roothub Internal Root Hub Operations, Structures and Constants.
340 * @{
341 */
342
343/**
344 * Per transfer type statistics.
345 */
346typedef struct VUSBROOTHUBTYPESTATS
347{
348 STAMCOUNTER StatUrbsSubmitted;
349 STAMCOUNTER StatUrbsFailed;
350 STAMCOUNTER StatUrbsCancelled;
351
352 STAMCOUNTER StatReqBytes;
353 STAMCOUNTER StatReqReadBytes;
354 STAMCOUNTER StatReqWriteBytes;
355
356 STAMCOUNTER StatActBytes;
357 STAMCOUNTER StatActReadBytes;
358 STAMCOUNTER StatActWriteBytes;
359} VUSBROOTHUBTYPESTATS, *PVUSBROOTHUBTYPESTATS;
360
361
362
363/** Pointer to a VUSBROOTHUBLOAD struct. */
364typedef struct VUSBROOTHUBLOAD *PVUSBROOTHUBLOAD;
365
366/**
367 * The instance data of a root hub driver.
368 *
369 * This extends the generic VUSB hub.
370 *
371 * @implements VUSBIROOTHUBCONNECTOR
372 */
373typedef struct VUSBROOTHUB
374{
375 /** The HUB.
376 * @todo remove this? */
377 VUSBHUB Hub;
378 /** Pointer to the driver instance. */
379 PPDMDRVINS pDrvIns;
380 /** Pointer to the root hub port interface we're attached to. */
381 PVUSBIROOTHUBPORT pIRhPort;
382 /** Connector interface exposed upwards. */
383 VUSBIROOTHUBCONNECTOR IRhConnector;
384
385 /** Critical section protecting the device arrays. */
386 RTCRITSECT CritSectDevices;
387 /** Array of pointers to USB devices indexed by the port the device is on. */
388 PVUSBDEV apDevByPort[VUSB_DEVICES_MAX];
389 /** Array of pointers to USB devices indexed by the address assigned. */
390 PVUSBDEV apDevByAddr[VUSB_DEVICES_MAX];
391 /** Structure after a saved state load to re-attach devices. */
392 PVUSBROOTHUBLOAD pLoad;
393
394#if HC_ARCH_BITS == 32
395 uint32_t Alignment0;
396#endif
397
398 /** Availability Bitmap. */
399 VUSBPORTBITMAP Bitmap;
400
401 /** Sniffer instance for the root hub. */
402 VUSBSNIFFER hSniffer;
403 /** Version of the attached Host Controller. */
404 uint32_t fHcVersions;
405 /** Size of the HCI specific data for each URB. */
406 size_t cbHci;
407 /** Size of the HCI specific TD. */
408 size_t cbHciTd;
409
410 /** The periodic frame processing thread. */
411 R3PTRTYPE(PPDMTHREAD) hThreadPeriodFrame;
412 /** Event semaphore to interact with the periodic frame processing thread. */
413 R3PTRTYPE(RTSEMEVENTMULTI) hSemEventPeriodFrame;
414 /** Event semaphore to release the thread waiting for the periodic frame processing thread to stop. */
415 R3PTRTYPE(RTSEMEVENTMULTI) hSemEventPeriodFrameStopped;
416 /** Current default frame rate for periodic frame processing thread. */
417 volatile uint32_t uFrameRateDefault;
418 /** Current frame rate (can be lower than the default frame rate if there is no activity). */
419 uint32_t uFrameRate;
420 /** How long to wait until the next frame. */
421 uint64_t nsWait;
422 /** Timestamp when the last frame was processed. */
423 uint64_t tsFrameProcessed;
424 /** Number of USB work cycles with no transfers. */
425 uint32_t cIdleCycles;
426
427 /** Flag whether a frame is currently being processed. */
428 volatile bool fFrameProcessing;
429
430#if HC_ARCH_BITS == 32
431 uint32_t Alignment1;
432#endif
433
434#ifdef LOG_ENABLED
435 /** A serial number for URBs submitted on the roothub instance.
436 * Only logging builds. */
437 uint32_t iSerial;
438 /** Alignment */
439 uint32_t Alignment2;
440#endif
441#ifdef VBOX_WITH_STATISTICS
442 VUSBROOTHUBTYPESTATS Total;
443 VUSBROOTHUBTYPESTATS aTypes[VUSBXFERTYPE_MSG];
444 STAMCOUNTER StatIsocReqPkts;
445 STAMCOUNTER StatIsocReqReadPkts;
446 STAMCOUNTER StatIsocReqWritePkts;
447 STAMCOUNTER StatIsocActPkts;
448 STAMCOUNTER StatIsocActReadPkts;
449 STAMCOUNTER StatIsocActWritePkts;
450 struct
451 {
452 STAMCOUNTER Pkts;
453 STAMCOUNTER Ok;
454 STAMCOUNTER Ok0;
455 STAMCOUNTER DataUnderrun;
456 STAMCOUNTER DataUnderrun0;
457 STAMCOUNTER DataOverrun;
458 STAMCOUNTER NotAccessed;
459 STAMCOUNTER Misc;
460 STAMCOUNTER Bytes;
461 } aStatIsocDetails[8];
462
463 STAMPROFILE StatReapAsyncUrbs;
464 STAMPROFILE StatSubmitUrb;
465 STAMCOUNTER StatFramesProcessedClbk;
466 STAMCOUNTER StatFramesProcessedThread;
467#endif
468} VUSBROOTHUB;
469AssertCompileMemberAlignment(VUSBROOTHUB, IRhConnector, 8);
470AssertCompileMemberAlignment(VUSBROOTHUB, Bitmap, 8);
471AssertCompileMemberAlignment(VUSBROOTHUB, CritSectDevices, 8);
472#ifdef VBOX_WITH_STATISTICS
473AssertCompileMemberAlignment(VUSBROOTHUB, Total, 8);
474#endif
475
476/** Converts a pointer to VUSBROOTHUB::IRhConnector to a PVUSBROOTHUB. */
477#define VUSBIROOTHUBCONNECTOR_2_VUSBROOTHUB(pInterface) (PVUSBROOTHUB)( (uintptr_t)(pInterface) - RT_UOFFSETOF(VUSBROOTHUB, IRhConnector) )
478
479/**
480 * URB cancellation modes
481 */
482typedef enum CANCELMODE
483{
484 /** complete the URB with an error (CRC). */
485 CANCELMODE_FAIL = 0,
486 /** do not change the URB contents. */
487 CANCELMODE_UNDO
488} CANCELMODE;
489
490/** @} */
491
492
493
494/** @defgroup grp_vusb_int_urb Internal URB Operations, Structures and Constants.
495 * @{ */
496int vusbUrbSubmit(PVUSBURB pUrb);
497void vusbUrbDoReapAsync(PRTLISTANCHOR pUrbLst, RTMSINTERVAL cMillies);
498void vusbUrbDoReapAsyncDev(PVUSBDEV pDev, RTMSINTERVAL cMillies);
499void vusbUrbCancel(PVUSBURB pUrb, CANCELMODE mode);
500void vusbUrbCancelAsync(PVUSBURB pUrb, CANCELMODE mode);
501void vusbUrbRipe(PVUSBURB pUrb);
502void vusbUrbCompletionRh(PVUSBURB pUrb);
503int vusbUrbSubmitHardError(PVUSBURB pUrb);
504int vusbUrbErrorRh(PVUSBURB pUrb);
505int vusbDevUrbIoThreadWakeup(PVUSBDEV pDev);
506int vusbDevUrbIoThreadCreate(PVUSBDEV pDev);
507int vusbDevUrbIoThreadDestroy(PVUSBDEV pDev);
508DECLHIDDEN(void) vusbDevCancelAllUrbs(PVUSBDEV pDev, bool fDetaching);
509DECLHIDDEN(int) vusbDevIoThreadExecV(PVUSBDEV pDev, uint32_t fFlags, PFNRT pfnFunction, unsigned cArgs, va_list Args);
510DECLHIDDEN(int) vusbDevIoThreadExec(PVUSBDEV pDev, uint32_t fFlags, PFNRT pfnFunction, unsigned cArgs, ...);
511DECLHIDDEN(int) vusbDevIoThreadExecSync(PVUSBDEV pDev, PFNRT pfnFunction, unsigned cArgs, ...);
512DECLHIDDEN(int) vusbUrbCancelWorker(PVUSBURB pUrb, CANCELMODE enmMode);
513
514DECLHIDDEN(uint64_t) vusbRhR3ProcessFrame(PVUSBROOTHUB pThis, bool fCallback);
515
516int vusbUrbQueueAsyncRh(PVUSBURB pUrb);
517
518bool vusbDevIsDescriptorInCache(PVUSBDEV pDev, PCVUSBSETUP pSetup);
519
520/**
521 * Initializes the given URB pool.
522 *
523 * @returns VBox status code.
524 * @param pUrbPool The URB pool to initialize.
525 */
526DECLHIDDEN(int) vusbUrbPoolInit(PVUSBURBPOOL pUrbPool);
527
528/**
529 * Destroy a given URB pool freeing all ressources.
530 *
531 * @returns nothing.
532 * @param pUrbPool The URB pool to destroy.
533 */
534DECLHIDDEN(void) vusbUrbPoolDestroy(PVUSBURBPOOL pUrbPool);
535
536/**
537 * Allocate a new URB from the given URB pool.
538 *
539 * @returns Pointer to the new URB or NULL if out of memory.
540 * @param pUrbPool The URB pool to allocate from.
541 * @param enmType Type of the URB.
542 * @param enmDir The direction of the URB.
543 * @param cbData The number of bytes to allocate for the data buffer.
544 * @param cbHci Size of the data private to the HCI for each URB when allocated.
545 * @param cbHciTd Size of one transfer descriptor.
546 * @param cTds Number of transfer descriptors.
547 */
548DECLHIDDEN(PVUSBURB) vusbUrbPoolAlloc(PVUSBURBPOOL pUrbPool, VUSBXFERTYPE enmType,
549 VUSBDIRECTION enmDir, size_t cbData,
550 size_t cbHci, size_t cbHciTd, unsigned cTds);
551
552/**
553 * Frees a given URB.
554 *
555 * @returns nothing.
556 * @param pUrbPool The URB pool the URB was allocated from.
557 * @param pUrb The URB to free.
558 */
559DECLHIDDEN(void) vusbUrbPoolFree(PVUSBURBPOOL pUrbPool, PVUSBURB pUrb);
560
561#ifdef LOG_ENABLED
562/**
563 * Logs an URB in the debug log.
564 *
565 * @returns nothing.
566 * @param pUrb The URB to log.
567 * @param pszMsg Additional message to log.
568 * @param fComplete Flag whther the URB is completing.
569 */
570DECLHIDDEN(void) vusbUrbTrace(PVUSBURB pUrb, const char *pszMsg, bool fComplete);
571
572/**
573 * Return the USB direction as a string from the given enum.
574 */
575DECLHIDDEN(const char *) vusbUrbDirName(VUSBDIRECTION enmDir);
576
577/**
578 * Return the URB type as string from the given enum.
579 */
580DECLHIDDEN(const char *) vusbUrbTypeName(VUSBXFERTYPE enmType);
581
582/**
583 * Return the URB status as string from the given enum.
584 */
585DECLHIDDEN(const char *) vusbUrbStatusName(VUSBSTATUS enmStatus);
586#endif
587
588DECLINLINE(void) vusbUrbUnlink(PVUSBURB pUrb)
589{
590 PVUSBDEV pDev = pUrb->pVUsb->pDev;
591
592 RTCritSectEnter(&pDev->CritSectAsyncUrbs);
593 RTListNodeRemove(&pUrb->pVUsb->NdLst);
594 RTCritSectLeave(&pDev->CritSectAsyncUrbs);
595}
596
597/** @def vusbUrbAssert
598 * Asserts that a URB is valid.
599 */
600#ifdef VBOX_STRICT
601# define vusbUrbAssert(pUrb) do { \
602 AssertPtr((pUrb)); \
603 AssertMsg((pUrb)->u32Magic == VUSBURB_MAGIC, ("%#x", (pUrb)->u32Magic)); \
604 AssertMsg((pUrb)->enmState > VUSBURBSTATE_INVALID && (pUrb)->enmState < VUSBURBSTATE_END, \
605 ("%d\n", (pUrb)->enmState)); \
606 } while (0)
607#else
608# define vusbUrbAssert(pUrb) do {} while (0)
609#endif
610
611/**
612 * @def VUSBDEV_ASSERT_VALID_STATE
613 * Asserts that the give device state is valid.
614 */
615#define VUSBDEV_ASSERT_VALID_STATE(enmState) \
616 AssertMsg((enmState) > VUSB_DEVICE_STATE_INVALID && (enmState) < VUSB_DEVICE_STATE_DESTROYED, ("enmState=%#x\n", enmState));
617
618/** Executes a function synchronously. */
619#define VUSB_DEV_IO_THREAD_EXEC_FLAGS_SYNC RT_BIT_32(0)
620
621/** @} */
622
623
624/**
625 * Gets the roothub of a device.
626 *
627 * @returns Pointer to the roothub instance the device is attached to.
628 * @returns NULL if not attached to any hub.
629 * @param pDev Pointer to the device in question.
630 */
631DECLINLINE(PVUSBROOTHUB) vusbDevGetRh(PVUSBDEV pDev)
632{
633 if (!pDev->pHub)
634 return NULL;
635 return pDev->pHub->pRootHub;
636}
637
638
639/**
640 * Returns the state of the USB device.
641 *
642 * @returns State of the USB device.
643 * @param pDev Pointer to the device.
644 */
645DECLINLINE(VUSBDEVICESTATE) vusbDevGetState(PVUSBDEV pDev)
646{
647 VUSBDEVICESTATE enmState = (VUSBDEVICESTATE)ASMAtomicReadU32((volatile uint32_t *)&pDev->enmState);
648 VUSBDEV_ASSERT_VALID_STATE(enmState);
649 return enmState;
650}
651
652
653/**
654 * Sets the given state for the USB device.
655 *
656 * @returns The old state of the device.
657 * @param pDev Pointer to the device.
658 * @param enmState The new state to set.
659 */
660DECLINLINE(VUSBDEVICESTATE) vusbDevSetState(PVUSBDEV pDev, VUSBDEVICESTATE enmState)
661{
662 VUSBDEV_ASSERT_VALID_STATE(enmState);
663 VUSBDEVICESTATE enmStateOld = (VUSBDEVICESTATE)ASMAtomicXchgU32((volatile uint32_t *)&pDev->enmState, enmState);
664 VUSBDEV_ASSERT_VALID_STATE(enmStateOld);
665 return enmStateOld;
666}
667
668
669/**
670 * Compare and exchange the states for the given USB device.
671 *
672 * @returns true if the state was changed.
673 * @returns false if the state wasn't changed.
674 * @param pDev Pointer to the device.
675 * @param enmStateNew The new state to set.
676 * @param enmStateOld The old state to compare with.
677 */
678DECLINLINE(bool) vusbDevSetStateCmp(PVUSBDEV pDev, VUSBDEVICESTATE enmStateNew, VUSBDEVICESTATE enmStateOld)
679{
680 VUSBDEV_ASSERT_VALID_STATE(enmStateNew);
681 VUSBDEV_ASSERT_VALID_STATE(enmStateOld);
682 return ASMAtomicCmpXchgU32((volatile uint32_t *)&pDev->enmState, enmStateNew, enmStateOld);
683}
684
685/**
686 * Retains the given VUSB device pointer.
687 *
688 * @returns New reference count.
689 * @param pThis The VUSB device pointer.
690 */
691DECLINLINE(uint32_t) vusbDevRetain(PVUSBDEV pThis)
692{
693 AssertPtrReturn(pThis, UINT32_MAX);
694
695 uint32_t cRefs = ASMAtomicIncU32(&pThis->cRefs);
696 AssertMsg(cRefs > 1 && cRefs < _1M, ("%#x %p\n", cRefs, pThis));
697 return cRefs;
698}
699
700/**
701 * Releases the given VUSB device pointer.
702 *
703 * @returns New reference count.
704 * @retval 0 if no onw is holding a reference anymore causing the device to be destroyed.
705 */
706DECLINLINE(uint32_t) vusbDevRelease(PVUSBDEV pThis)
707{
708 AssertPtrReturn(pThis, UINT32_MAX);
709
710 uint32_t cRefs = ASMAtomicDecU32(&pThis->cRefs);
711 AssertMsg(cRefs < _1M, ("%#x %p\n", cRefs, pThis));
712 if (cRefs == 0)
713 vusbDevDestroy(pThis);
714 return cRefs;
715}
716
717/** Strings for the CTLSTAGE enum values. */
718extern const char * const g_apszCtlStates[4];
719
720/** @} */
721RT_C_DECLS_END
722#endif /* !VBOX_INCLUDED_SRC_USB_VUSBInternal_h */
723
Note: See TracBrowser for help on using the repository browser.

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