VirtualBox

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

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

VUSB: Some structural cleanup (#7 Move the debug log trace related bits into a separate file)

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 21.0 KB
Line 
1/* $Id: VUSBInternal.h 59738 2016-02-19 11:55:56Z 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.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 ___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#include <iprt/list.h>
35
36#include "VUSBSniffer.h"
37
38RT_C_DECLS_BEGIN
39
40
41/** @name Internal Device Operations, Structures and Constants.
42 * @{
43 */
44
45/** Pointer to a Virtual USB device (core). */
46typedef struct VUSBDEV *PVUSBDEV;
47/** Pointer to a VUSB hub device. */
48typedef struct VUSBHUB *PVUSBHUB;
49/** Pointer to a VUSB root hub. */
50typedef struct VUSBROOTHUB *PVUSBROOTHUB;
51
52
53/** Number of the default control endpoint */
54#define VUSB_PIPE_DEFAULT 0
55
56/** @name Device addresses
57 * @{ */
58#define VUSB_DEFAULT_ADDRESS 0
59#define VUSB_INVALID_ADDRESS UINT8_C(0xff)
60/** @} */
61
62/** @name Feature bits (1<<FEATURE for the u16Status bit)
63 * @{ */
64#define VUSB_DEV_SELF_POWERED 0
65#define VUSB_DEV_REMOTE_WAKEUP 1
66#define VUSB_EP_HALT 0
67/** @} */
68
69/** Maximum number of endpoint addresses */
70#define VUSB_PIPE_MAX 16
71
72/**
73 * The VUSB URB data.
74 */
75typedef struct VUSBURBVUSBINT
76{
77 /** Node for one of the lists the URB can be in. */
78 RTLISTNODE NdLst;
79 /** Pointer to the URB this structure is part of. */
80 PVUSBURB pUrb;
81 /** Pointer to the original for control messages. */
82 PVUSBURB pCtrlUrb;
83 /** Pointer to the VUSB device.
84 * This may be NULL if the destination address is invalid. */
85 PVUSBDEV pDev;
86 /** Specific to the pfnFree function. */
87 void *pvFreeCtx;
88 /**
89 * Callback which will free the URB once it's reaped and completed.
90 * @param pUrb The URB.
91 */
92 DECLCALLBACKMEMBER(void, pfnFree)(PVUSBURB pUrb);
93 /** Submit timestamp. (logging only) */
94 uint64_t u64SubmitTS;
95 /** Opaque data holder when this is a read-ahead URB. */
96 void *pvReadAhead;
97} VUSBURBVUSBINT;
98
99/**
100 * Control-pipe stages.
101 */
102typedef enum CTLSTAGE
103{
104 /** the control pipe is in the setup stage. */
105 CTLSTAGE_SETUP = 0,
106 /** the control pipe is in the data stage. */
107 CTLSTAGE_DATA,
108 /** the control pipe is in the status stage. */
109 CTLSTAGE_STATUS
110} CTLSTAGE;
111
112/**
113 * Extra data for a control pipe.
114 *
115 * This is state information needed for the special multi-stage
116 * transfers performed on this kind of pipes.
117 */
118typedef struct vusb_ctrl_extra
119{
120 /** Current pipe stage. */
121 CTLSTAGE enmStage;
122 /** Success indicator. */
123 bool fOk;
124 /** Set if the message URB has been submitted. */
125 bool fSubmitted;
126 /** Pointer to the SETUP.
127 * This is a pointer to Urb->abData[0]. */
128 PVUSBSETUP pMsg;
129 /** Current DATA pointer.
130 * This starts at pMsg + 1 and is incremented at we read/write data. */
131 uint8_t *pbCur;
132 /** The amount of data left to read on IN operations.
133 * On OUT operations this is not used. */
134 uint32_t cbLeft;
135 /** The amount of data we can house.
136 * This starts at the default 8KB, and this structure will be reallocated to
137 * accommodate any larger request (unlikely). */
138 uint32_t cbMax;
139 /** The message URB. */
140 VUSBURB Urb;
141} VUSBCTRLEXTRA, *PVUSBCTRLEXTRA;
142
143void vusbMsgFreeExtraData(PVUSBCTRLEXTRA pExtra);
144void vusbMsgResetExtraData(PVUSBCTRLEXTRA pExtra);
145
146/** Opaque VUSB read ahead buffer management handle. */
147typedef struct VUSBREADAHEADINT *VUSBREADAHEAD;
148
149/**
150 * A VUSB pipe
151 */
152typedef struct vusb_pipe
153{
154 PCVUSBDESCENDPOINTEX in;
155 PCVUSBDESCENDPOINTEX out;
156 /** Pointer to the extra state data required to run a control pipe. */
157 PVUSBCTRLEXTRA pCtrl;
158 /** Critical section serializing access to the extra state data for a control pipe. */
159 RTCRITSECT CritSectCtrl;
160 /** Count of active async transfers. */
161 volatile uint32_t async;
162 /** Read ahead handle. */
163 VUSBREADAHEAD hReadAhead;
164} VUSBPIPE;
165/** Pointer to a VUSB pipe structure. */
166typedef VUSBPIPE *PVUSBPIPE;
167
168
169/**
170 * Interface state and possible settings.
171 */
172typedef struct vusb_interface_state
173{
174 /** Pointer to the interface descriptor of the currently selected (active)
175 * interface. */
176 PCVUSBDESCINTERFACEEX pCurIfDesc;
177 /** Pointer to the interface settings. */
178 PCVUSBINTERFACE pIf;
179} VUSBINTERFACESTATE;
180/** Pointer to interface state. */
181typedef VUSBINTERFACESTATE *PVUSBINTERFACESTATE;
182/** Pointer to const interface state. */
183typedef const VUSBINTERFACESTATE *PCVUSBINTERFACESTATE;
184
185
186/**
187 * VUSB URB pool.
188 */
189typedef struct VUSBURBPOOL
190{
191 /** Critical section protecting the pool. */
192 RTCRITSECT CritSectPool;
193 /** Chain of free URBs by type. (Singly linked) */
194 RTLISTANCHOR aLstFreeUrbs[VUSBXFERTYPE_ELEMENTS];
195 /** The number of URBs in the pool. */
196 volatile uint32_t cUrbsInPool;
197 /** Align the size to a 8 byte boundary. */
198 uint32_t Alignment0;
199} VUSBURBPOOL;
200/** Pointer to a VUSB URB pool. */
201typedef VUSBURBPOOL *PVUSBURBPOOL;
202
203AssertCompileSizeAlignment(VUSBURBPOOL, 8);
204
205/**
206 * A Virtual USB device (core).
207 *
208 * @implements VUSBIDEVICE
209 */
210typedef struct VUSBDEV
211{
212 /** The device interface exposed to the HCI. */
213 VUSBIDEVICE IDevice;
214 /** Pointer to the PDM USB device instance. */
215 PPDMUSBINS pUsbIns;
216 /** Next device in the chain maintained by the roothub. */
217 PVUSBDEV pNext;
218 /** Pointer to the next device with the same address hash. */
219 PVUSBDEV pNextHash;
220 /** Pointer to the hub this device is attached to. */
221 PVUSBHUB pHub;
222 /** The device state. */
223 VUSBDEVICESTATE volatile enmState;
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#if HC_ARCH_BITS == 32
252 /** Align the size to a 8 byte boundary. */
253 uint32_t u32Alignment0;
254#endif
255
256 /** Dumper state. */
257 union VUSBDEVURBDUMPERSTATE
258 {
259 /** The current scsi command. */
260 uint8_t u8ScsiCmd;
261 } Urb;
262
263 /** The reset timer handle. */
264 PTMTIMER pResetTimer;
265 /** Reset handler arguments. */
266 void *pvArgs;
267 /** URB submit and reap thread. */
268 RTTHREAD hUrbIoThread;
269 /** Request queue for executing tasks on the I/O thread which should be done
270 * synchronous and without any other thread accessing the USB device. */
271 RTREQQUEUE hReqQueueSync;
272 /** Sniffer instance for this device if configured. */
273 VUSBSNIFFER hSniffer;
274 /** Flag whether the URB I/O thread should terminate. */
275 bool volatile fTerminate;
276 /** Flag whether the I/O thread was woken up. */
277 bool volatile fWokenUp;
278#if HC_ARCH_BITS == 32
279 /** Align the size to a 8 byte boundary. */
280 bool afAlignment0[2];
281#endif
282 /** The pool of free URBs for faster allocation. */
283 VUSBURBPOOL UrbPool;
284} VUSBDEV;
285AssertCompileSizeAlignment(VUSBDEV, 8);
286
287
288int vusbDevInit(PVUSBDEV pDev, PPDMUSBINS pUsbIns, const char *pszCaptureFilename);
289void vusbDevDestroy(PVUSBDEV pDev);
290
291DECLINLINE(bool) vusbDevIsRh(PVUSBDEV pDev)
292{
293 return (pDev->pHub == (PVUSBHUB)pDev);
294}
295
296bool vusbDevDoSelectConfig(PVUSBDEV dev, PCVUSBDESCCONFIGEX pCfg);
297void vusbDevMapEndpoint(PVUSBDEV dev, PCVUSBDESCENDPOINTEX ep);
298int vusbDevDetach(PVUSBDEV pDev);
299DECLINLINE(PVUSBROOTHUB) vusbDevGetRh(PVUSBDEV pDev);
300size_t vusbDevMaxInterfaces(PVUSBDEV dev);
301
302void vusbDevSetAddress(PVUSBDEV pDev, uint8_t u8Address);
303bool vusbDevStandardRequest(PVUSBDEV pDev, int EndPt, PVUSBSETUP pSetup, void *pvBuf, uint32_t *pcbBuf);
304
305
306/** @} */
307
308
309/** @name Internal Hub Operations, Structures and Constants.
310 * @{
311 */
312
313
314/** Virtual method table for USB hub devices.
315 * Hub and roothub drivers need to implement these functions in addition to the
316 * vusb_dev_ops.
317 */
318typedef struct VUSBHUBOPS
319{
320 int (*pfnAttach)(PVUSBHUB pHub, PVUSBDEV pDev);
321 void (*pfnDetach)(PVUSBHUB pHub, PVUSBDEV pDev);
322} VUSBHUBOPS;
323/** Pointer to a const HUB method table. */
324typedef const VUSBHUBOPS *PCVUSBHUBOPS;
325
326/** A VUSB Hub Device - Hub and roothub drivers need to use this struct
327 * @todo eliminate this (PDM / roothubs only).
328 */
329typedef struct VUSBHUB
330{
331 VUSBDEV Dev;
332 PCVUSBHUBOPS pOps;
333 PVUSBROOTHUB pRootHub;
334 uint16_t cPorts;
335 uint16_t cDevices;
336 /** Name of the hub. Used for logging. */
337 char *pszName;
338} VUSBHUB;
339AssertCompileMemberAlignment(VUSBHUB, pOps, 8);
340AssertCompileSizeAlignment(VUSBHUB, 8);
341
342/** @} */
343
344
345/** @name Internal Root Hub Operations, Structures and Constants.
346 * @{
347 */
348
349/**
350 * Per transfer type statistics.
351 */
352typedef struct VUSBROOTHUBTYPESTATS
353{
354 STAMCOUNTER StatUrbsSubmitted;
355 STAMCOUNTER StatUrbsFailed;
356 STAMCOUNTER StatUrbsCancelled;
357
358 STAMCOUNTER StatReqBytes;
359 STAMCOUNTER StatReqReadBytes;
360 STAMCOUNTER StatReqWriteBytes;
361
362 STAMCOUNTER StatActBytes;
363 STAMCOUNTER StatActReadBytes;
364 STAMCOUNTER StatActWriteBytes;
365} VUSBROOTHUBTYPESTATS, *PVUSBROOTHUBTYPESTATS;
366
367
368
369/** The address hash table size. */
370#define VUSB_ADDR_HASHSZ 5
371
372/**
373 * The instance data of a root hub driver.
374 *
375 * This extends the generic VUSB hub.
376 *
377 * @implements VUSBIROOTHUBCONNECTOR
378 */
379typedef struct VUSBROOTHUB
380{
381 /** The HUB.
382 * @todo remove this? */
383 VUSBHUB Hub;
384 /** Address hash table. */
385 PVUSBDEV apAddrHash[VUSB_ADDR_HASHSZ];
386 /** The default address. */
387 PVUSBDEV pDefaultAddress;
388
389 /** Pointer to the driver instance. */
390 PPDMDRVINS pDrvIns;
391 /** Pointer to the root hub port interface we're attached to. */
392 PVUSBIROOTHUBPORT pIRhPort;
393 /** Connector interface exposed upwards. */
394 VUSBIROOTHUBCONNECTOR IRhConnector;
395
396#if HC_ARCH_BITS == 32
397 uint32_t Alignment0;
398#endif
399
400 /** Critical section protecting the device list. */
401 RTCRITSECT CritSectDevices;
402 /** Chain of devices attached to this hub. */
403 PVUSBDEV pDevices;
404
405#if HC_ARCH_BITS == 32
406 uint32_t Alignment1;
407#endif
408
409 /** Availability Bitmap. */
410 VUSBPORTBITMAP Bitmap;
411
412 /** Sniffer instance for the root hub. */
413 VUSBSNIFFER hSniffer;
414 /** Version of the attached Host Controller. */
415 uint32_t fHcVersions;
416 /** Size of the HCI specific data for each URB. */
417 size_t cbHci;
418 /** Size of the HCI specific TD. */
419 size_t cbHciTd;
420#ifdef LOG_ENABLED
421 /** A serial number for URBs submitted on the roothub instance.
422 * Only logging builds. */
423 uint32_t iSerial;
424 /** Alignment */
425 uint32_t Alignment2;
426#endif
427#ifdef VBOX_WITH_STATISTICS
428 VUSBROOTHUBTYPESTATS Total;
429 VUSBROOTHUBTYPESTATS aTypes[VUSBXFERTYPE_MSG];
430 STAMCOUNTER StatIsocReqPkts;
431 STAMCOUNTER StatIsocReqReadPkts;
432 STAMCOUNTER StatIsocReqWritePkts;
433 STAMCOUNTER StatIsocActPkts;
434 STAMCOUNTER StatIsocActReadPkts;
435 STAMCOUNTER StatIsocActWritePkts;
436 struct
437 {
438 STAMCOUNTER Pkts;
439 STAMCOUNTER Ok;
440 STAMCOUNTER Ok0;
441 STAMCOUNTER DataUnderrun;
442 STAMCOUNTER DataUnderrun0;
443 STAMCOUNTER DataOverrun;
444 STAMCOUNTER NotAccessed;
445 STAMCOUNTER Misc;
446 STAMCOUNTER Bytes;
447 } aStatIsocDetails[8];
448
449 STAMPROFILE StatReapAsyncUrbs;
450 STAMPROFILE StatSubmitUrb;
451#endif
452} VUSBROOTHUB;
453AssertCompileMemberAlignment(VUSBROOTHUB, IRhConnector, 8);
454AssertCompileMemberAlignment(VUSBROOTHUB, Bitmap, 8);
455AssertCompileMemberAlignment(VUSBROOTHUB, CritSectDevices, 8);
456#ifdef VBOX_WITH_STATISTICS
457AssertCompileMemberAlignment(VUSBROOTHUB, Total, 8);
458#endif
459
460/** Converts a pointer to VUSBROOTHUB::IRhConnector to a PVUSBROOTHUB. */
461#define VUSBIROOTHUBCONNECTOR_2_VUSBROOTHUB(pInterface) (PVUSBROOTHUB)( (uintptr_t)(pInterface) - RT_OFFSETOF(VUSBROOTHUB, IRhConnector) )
462
463/**
464 * URB cancellation modes
465 */
466typedef enum CANCELMODE
467{
468 /** complete the URB with an error (CRC). */
469 CANCELMODE_FAIL = 0,
470 /** do not change the URB contents. */
471 CANCELMODE_UNDO
472} CANCELMODE;
473
474/* @} */
475
476
477
478/** @name Internal URB Operations, Structures and Constants.
479 * @{ */
480int vusbUrbSubmit(PVUSBURB pUrb);
481void vusbUrbDoReapAsync(PRTLISTANCHOR pUrbLst, 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#ifdef LOG_ENABLED
548/**
549 * Logs an URB in the debug log.
550 *
551 * @returns nothing.
552 * @param pUrb The URB to log.
553 * @param pszMsg Additional message to log.
554 * @param fComplete Flag whther the URB is completing.
555 */
556DECLHIDDEN(void) vusbUrbTrace(PVUSBURB pUrb, const char *pszMsg, bool fComplete);
557
558/**
559 * Return the USB direction as a string from the given enum.
560 */
561DECLHIDDEN(const char *) vusbUrbDirName(VUSBDIRECTION enmDir);
562
563/**
564 * Return the URB type as string from the given enum.
565 */
566DECLHIDDEN(const char *) vusbUrbTypeName(VUSBXFERTYPE enmType);
567
568/**
569 * Return the URB status as string from the given enum.
570 */
571DECLHIDDEN(const char *) vusbUrbStatusName(VUSBSTATUS enmStatus);
572#endif
573
574DECLINLINE(void) vusbUrbUnlink(PVUSBURB pUrb)
575{
576 PVUSBDEV pDev = pUrb->pVUsb->pDev;
577
578 RTCritSectEnter(&pDev->CritSectAsyncUrbs);
579 RTListNodeRemove(&pUrb->pVUsb->NdLst);
580 RTCritSectLeave(&pDev->CritSectAsyncUrbs);
581}
582
583/** @def vusbUrbAssert
584 * Asserts that a URB is valid.
585 */
586#ifdef VBOX_STRICT
587# define vusbUrbAssert(pUrb) do { \
588 AssertMsg(VALID_PTR((pUrb)), ("%p\n", (pUrb))); \
589 AssertMsg((pUrb)->u32Magic == VUSBURB_MAGIC, ("%#x", (pUrb)->u32Magic)); \
590 AssertMsg((pUrb)->enmState > VUSBURBSTATE_INVALID && (pUrb)->enmState < VUSBURBSTATE_END, \
591 ("%d\n", (pUrb)->enmState)); \
592 } while (0)
593#else
594# define vusbUrbAssert(pUrb) do {} while (0)
595#endif
596
597/**
598 * @def VUSBDEV_ASSERT_VALID_STATE
599 * Asserts that the give device state is valid.
600 */
601#define VUSBDEV_ASSERT_VALID_STATE(enmState) \
602 AssertMsg((enmState) > VUSB_DEVICE_STATE_INVALID && (enmState) < VUSB_DEVICE_STATE_DESTROYED, ("enmState=%#x\n", enmState));
603
604/** Executes a function synchronously. */
605#define VUSB_DEV_IO_THREAD_EXEC_FLAGS_SYNC RT_BIT_32(0)
606
607/** @} */
608
609
610
611
612/**
613 * Addresses are between 0 and 127 inclusive
614 */
615DECLINLINE(uint8_t) vusbHashAddress(uint8_t Address)
616{
617 uint8_t u8Hash = Address;
618 u8Hash ^= (Address >> 2);
619 u8Hash ^= (Address >> 3);
620 u8Hash %= VUSB_ADDR_HASHSZ;
621 return u8Hash;
622}
623
624
625/**
626 * Gets the roothub of a device.
627 *
628 * @returns Pointer to the roothub instance the device is attached to.
629 * @returns NULL if not attached to any hub.
630 * @param pDev Pointer to the device in question.
631 */
632DECLINLINE(PVUSBROOTHUB) vusbDevGetRh(PVUSBDEV pDev)
633{
634 if (!pDev->pHub)
635 return NULL;
636 return pDev->pHub->pRootHub;
637}
638
639
640/**
641 * Returns the state of the USB device.
642 *
643 * @returns State of the USB device.
644 * @param pDev Pointer to the device.
645 */
646DECLINLINE(VUSBDEVICESTATE) vusbDevGetState(PVUSBDEV pDev)
647{
648 VUSBDEVICESTATE enmState = (VUSBDEVICESTATE)ASMAtomicReadU32((volatile uint32_t *)&pDev->enmState);
649 VUSBDEV_ASSERT_VALID_STATE(enmState);
650 return enmState;
651}
652
653
654/**
655 * Sets the given state for the USB device.
656 *
657 * @returns The old state of the device.
658 * @param pDev Pointer to the device.
659 * @param enmState The new state to set.
660 */
661DECLINLINE(VUSBDEVICESTATE) vusbDevSetState(PVUSBDEV pDev, VUSBDEVICESTATE enmState)
662{
663 VUSBDEV_ASSERT_VALID_STATE(enmState);
664 VUSBDEVICESTATE enmStateOld = (VUSBDEVICESTATE)ASMAtomicXchgU32((volatile uint32_t *)&pDev->enmState, enmState);
665 VUSBDEV_ASSERT_VALID_STATE(enmStateOld);
666 return enmStateOld;
667}
668
669
670/**
671 * Compare and exchange the states for the given USB device.
672 *
673 * @returns true if the state was changed.
674 * @returns false if the state wasn't changed.
675 * @param pDev Pointer to the device.
676 * @param enmStateNew The new state to set.
677 * @param enmStateOld The old state to compare with.
678 */
679DECLINLINE(bool) vusbDevSetStateCmp(PVUSBDEV pDev, VUSBDEVICESTATE enmStateNew, VUSBDEVICESTATE enmStateOld)
680{
681 VUSBDEV_ASSERT_VALID_STATE(enmStateNew);
682 VUSBDEV_ASSERT_VALID_STATE(enmStateOld);
683 return ASMAtomicCmpXchgU32((volatile uint32_t *)&pDev->enmState, enmStateNew, enmStateOld);
684}
685
686/** Strings for the CTLSTAGE enum values. */
687extern const char * const g_apszCtlStates[4];
688/** Default message pipe. */
689extern const VUSBDESCENDPOINTEX g_Endpoint0;
690/** Default configuration. */
691extern const VUSBDESCCONFIGEX g_Config0;
692
693RT_C_DECLS_END
694#endif
695
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