VirtualBox

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

Last change on this file since 59720 was 59720, checked in by vboxsync, 9 years ago

VUSB: fix 32bit builds

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