VirtualBox

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

Last change on this file since 77807 was 77000, checked in by vboxsync, 6 years ago

Devices/USB: Create the USB I/O thread for each device during the attach phase (as it was destructed during a detach) to fix a debug assertion when saving a VM state where the device gets reattached during the process

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