VirtualBox

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

最後變更 在這個檔案從66347是 66347,由 vboxsync 提交於 8 年 前

EHCI: Improved transfer type determination heuristic.

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

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