VirtualBox

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

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

Devices: More VALID_PTR -> RT_VALID_PTR/AssertPtr.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 23.1 KB
Line 
1/* $Id: VUSBInternal.h 90791 2021-08-23 10:28:24Z 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 TMTIMERHANDLE hResetTimer;
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
535bool vusbDevIsDescriptorInCache(PVUSBDEV pDev, PCVUSBSETUP pSetup);
536
537/**
538 * Initializes the given URB pool.
539 *
540 * @returns VBox status code.
541 * @param pUrbPool The URB pool to initialize.
542 */
543DECLHIDDEN(int) vusbUrbPoolInit(PVUSBURBPOOL pUrbPool);
544
545/**
546 * Destroy a given URB pool freeing all ressources.
547 *
548 * @returns nothing.
549 * @param pUrbPool The URB pool to destroy.
550 */
551DECLHIDDEN(void) vusbUrbPoolDestroy(PVUSBURBPOOL pUrbPool);
552
553/**
554 * Allocate a new URB from the given URB pool.
555 *
556 * @returns Pointer to the new URB or NULL if out of memory.
557 * @param pUrbPool The URB pool to allocate from.
558 * @param enmType Type of the URB.
559 * @param enmDir The direction of the URB.
560 * @param cbData The number of bytes to allocate for the data buffer.
561 * @param cbHci Size of the data private to the HCI for each URB when allocated.
562 * @param cbHciTd Size of one transfer descriptor.
563 * @param cTds Number of transfer descriptors.
564 */
565DECLHIDDEN(PVUSBURB) vusbUrbPoolAlloc(PVUSBURBPOOL pUrbPool, VUSBXFERTYPE enmType,
566 VUSBDIRECTION enmDir, size_t cbData,
567 size_t cbHci, size_t cbHciTd, unsigned cTds);
568
569/**
570 * Frees a given URB.
571 *
572 * @returns nothing.
573 * @param pUrbPool The URB pool the URB was allocated from.
574 * @param pUrb The URB to free.
575 */
576DECLHIDDEN(void) vusbUrbPoolFree(PVUSBURBPOOL pUrbPool, PVUSBURB pUrb);
577
578#ifdef LOG_ENABLED
579/**
580 * Logs an URB in the debug log.
581 *
582 * @returns nothing.
583 * @param pUrb The URB to log.
584 * @param pszMsg Additional message to log.
585 * @param fComplete Flag whther the URB is completing.
586 */
587DECLHIDDEN(void) vusbUrbTrace(PVUSBURB pUrb, const char *pszMsg, bool fComplete);
588
589/**
590 * Return the USB direction as a string from the given enum.
591 */
592DECLHIDDEN(const char *) vusbUrbDirName(VUSBDIRECTION enmDir);
593
594/**
595 * Return the URB type as string from the given enum.
596 */
597DECLHIDDEN(const char *) vusbUrbTypeName(VUSBXFERTYPE enmType);
598
599/**
600 * Return the URB status as string from the given enum.
601 */
602DECLHIDDEN(const char *) vusbUrbStatusName(VUSBSTATUS enmStatus);
603#endif
604
605DECLINLINE(void) vusbUrbUnlink(PVUSBURB pUrb)
606{
607 PVUSBDEV pDev = pUrb->pVUsb->pDev;
608
609 RTCritSectEnter(&pDev->CritSectAsyncUrbs);
610 RTListNodeRemove(&pUrb->pVUsb->NdLst);
611 RTCritSectLeave(&pDev->CritSectAsyncUrbs);
612}
613
614/** @def vusbUrbAssert
615 * Asserts that a URB is valid.
616 */
617#ifdef VBOX_STRICT
618# define vusbUrbAssert(pUrb) do { \
619 AssertPtr((pUrb)); \
620 AssertMsg((pUrb)->u32Magic == VUSBURB_MAGIC, ("%#x", (pUrb)->u32Magic)); \
621 AssertMsg((pUrb)->enmState > VUSBURBSTATE_INVALID && (pUrb)->enmState < VUSBURBSTATE_END, \
622 ("%d\n", (pUrb)->enmState)); \
623 } while (0)
624#else
625# define vusbUrbAssert(pUrb) do {} while (0)
626#endif
627
628/**
629 * @def VUSBDEV_ASSERT_VALID_STATE
630 * Asserts that the give device state is valid.
631 */
632#define VUSBDEV_ASSERT_VALID_STATE(enmState) \
633 AssertMsg((enmState) > VUSB_DEVICE_STATE_INVALID && (enmState) < VUSB_DEVICE_STATE_DESTROYED, ("enmState=%#x\n", enmState));
634
635/** Executes a function synchronously. */
636#define VUSB_DEV_IO_THREAD_EXEC_FLAGS_SYNC RT_BIT_32(0)
637
638/** @} */
639
640
641
642
643/**
644 * Addresses are between 0 and 127 inclusive
645 */
646DECLINLINE(uint8_t) vusbHashAddress(uint8_t Address)
647{
648 uint8_t u8Hash = Address;
649 u8Hash ^= (Address >> 2);
650 u8Hash ^= (Address >> 3);
651 u8Hash %= VUSB_ADDR_HASHSZ;
652 return u8Hash;
653}
654
655
656/**
657 * Gets the roothub of a device.
658 *
659 * @returns Pointer to the roothub instance the device is attached to.
660 * @returns NULL if not attached to any hub.
661 * @param pDev Pointer to the device in question.
662 */
663DECLINLINE(PVUSBROOTHUB) vusbDevGetRh(PVUSBDEV pDev)
664{
665 if (!pDev->pHub)
666 return NULL;
667 return pDev->pHub->pRootHub;
668}
669
670
671/**
672 * Returns the state of the USB device.
673 *
674 * @returns State of the USB device.
675 * @param pDev Pointer to the device.
676 */
677DECLINLINE(VUSBDEVICESTATE) vusbDevGetState(PVUSBDEV pDev)
678{
679 VUSBDEVICESTATE enmState = (VUSBDEVICESTATE)ASMAtomicReadU32((volatile uint32_t *)&pDev->enmState);
680 VUSBDEV_ASSERT_VALID_STATE(enmState);
681 return enmState;
682}
683
684
685/**
686 * Sets the given state for the USB device.
687 *
688 * @returns The old state of the device.
689 * @param pDev Pointer to the device.
690 * @param enmState The new state to set.
691 */
692DECLINLINE(VUSBDEVICESTATE) vusbDevSetState(PVUSBDEV pDev, VUSBDEVICESTATE enmState)
693{
694 VUSBDEV_ASSERT_VALID_STATE(enmState);
695 VUSBDEVICESTATE enmStateOld = (VUSBDEVICESTATE)ASMAtomicXchgU32((volatile uint32_t *)&pDev->enmState, enmState);
696 VUSBDEV_ASSERT_VALID_STATE(enmStateOld);
697 return enmStateOld;
698}
699
700
701/**
702 * Compare and exchange the states for the given USB device.
703 *
704 * @returns true if the state was changed.
705 * @returns false if the state wasn't changed.
706 * @param pDev Pointer to the device.
707 * @param enmStateNew The new state to set.
708 * @param enmStateOld The old state to compare with.
709 */
710DECLINLINE(bool) vusbDevSetStateCmp(PVUSBDEV pDev, VUSBDEVICESTATE enmStateNew, VUSBDEVICESTATE enmStateOld)
711{
712 VUSBDEV_ASSERT_VALID_STATE(enmStateNew);
713 VUSBDEV_ASSERT_VALID_STATE(enmStateOld);
714 return ASMAtomicCmpXchgU32((volatile uint32_t *)&pDev->enmState, enmStateNew, enmStateOld);
715}
716
717/**
718 * Retains the given VUSB device pointer.
719 *
720 * @returns New reference count.
721 * @param pThis The VUSB device pointer.
722 */
723DECLINLINE(uint32_t) vusbDevRetain(PVUSBDEV pThis)
724{
725 AssertPtrReturn(pThis, UINT32_MAX);
726
727 uint32_t cRefs = ASMAtomicIncU32(&pThis->cRefs);
728 AssertMsg(cRefs > 1 && cRefs < _1M, ("%#x %p\n", cRefs, pThis));
729 return cRefs;
730}
731
732/**
733 * Releases the given VUSB device pointer.
734 *
735 * @returns New reference count.
736 * @retval 0 if no onw is holding a reference anymore causing the device to be destroyed.
737 */
738DECLINLINE(uint32_t) vusbDevRelease(PVUSBDEV pThis)
739{
740 AssertPtrReturn(pThis, UINT32_MAX);
741
742 uint32_t cRefs = ASMAtomicDecU32(&pThis->cRefs);
743 AssertMsg(cRefs < _1M, ("%#x %p\n", cRefs, pThis));
744 if (cRefs == 0)
745 vusbDevDestroy(pThis);
746 return cRefs;
747}
748
749/** Strings for the CTLSTAGE enum values. */
750extern const char * const g_apszCtlStates[4];
751
752/** @} */
753RT_C_DECLS_END
754#endif /* !VBOX_INCLUDED_SRC_USB_VUSBInternal_h */
755
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