1 | /* $Id: VBoxUsbDev.h 62699 2016-07-29 16:01:21Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * VBoxUsbDev.h - USB device.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2011-2016 Oracle Corporation
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
10 | * available from http://www.virtualbox.org. This file is free software;
|
---|
11 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
12 | * General Public License (GPL) as published by the Free Software
|
---|
13 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
14 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
15 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
16 | */
|
---|
17 |
|
---|
18 | #ifndef ___VBoxUsbDev_h___
|
---|
19 | #define ___VBoxUsbDev_h___
|
---|
20 |
|
---|
21 | #include "VBoxUsbCmn.h"
|
---|
22 | #include <VBox/cdefs.h>
|
---|
23 | #include <iprt/assert.h>
|
---|
24 |
|
---|
25 | typedef struct VBOXUSB_GLOBALS
|
---|
26 | {
|
---|
27 | PDRIVER_OBJECT pDrvObj;
|
---|
28 | UNICODE_STRING RegPath;
|
---|
29 | VBOXUSBRT_IDC RtIdc;
|
---|
30 | } VBOXUSB_GLOBALS, *PVBOXUSB_GLOBALS;
|
---|
31 |
|
---|
32 | extern VBOXUSB_GLOBALS g_VBoxUsbGlobals;
|
---|
33 |
|
---|
34 | /* pnp state decls */
|
---|
35 | typedef enum
|
---|
36 | {
|
---|
37 | ENMVBOXUSB_PNPSTATE_UNKNOWN = 0,
|
---|
38 | ENMVBOXUSB_PNPSTATE_START_PENDING,
|
---|
39 | ENMVBOXUSB_PNPSTATE_STARTED,
|
---|
40 | ENMVBOXUSB_PNPSTATE_STOP_PENDING,
|
---|
41 | ENMVBOXUSB_PNPSTATE_STOPPED,
|
---|
42 | ENMVBOXUSB_PNPSTATE_SURPRISE_REMOVED,
|
---|
43 | ENMVBOXUSB_PNPSTATE_REMOVE_PENDING,
|
---|
44 | ENMVBOXUSB_PNPSTATE_REMOVED,
|
---|
45 | ENMVBOXUSB_PNPSTATE_FORSEDWORD = 0x8fffffff
|
---|
46 | } ENMVBOXUSB_PNPSTATE;
|
---|
47 | AssertCompile(sizeof (ENMVBOXUSB_PNPSTATE) == sizeof (uint32_t));
|
---|
48 |
|
---|
49 | #ifdef VBOX_STRICT
|
---|
50 | DECLHIDDEN(VOID) vboxUsbPnPStateGbgChange(ENMVBOXUSB_PNPSTATE enmOld, ENMVBOXUSB_PNPSTATE enmNew);
|
---|
51 | # define VBOXUSB_PNP_GBG_STATE_CHANGE(_old, _new) vboxUsbPnPStateGbgChange((_old), (_new))
|
---|
52 | #else
|
---|
53 | # define VBOXUSB_PNP_GBG_STATE_CHANGE(_old, _new) do { } while (0)
|
---|
54 | #endif
|
---|
55 |
|
---|
56 |
|
---|
57 | typedef struct VBOXUSB_PNPSTATE
|
---|
58 | {
|
---|
59 | /* Current state */
|
---|
60 | volatile ENMVBOXUSB_PNPSTATE Curr;
|
---|
61 | /* Previous state, used to restore state info on cancell stop device */
|
---|
62 | ENMVBOXUSB_PNPSTATE Prev;
|
---|
63 | } VBOXUSB_PNPSTATE, *PVBOXUSB_PNPSTATE;
|
---|
64 |
|
---|
65 | typedef struct VBOXUSBDEV_DDISTATE
|
---|
66 | {
|
---|
67 | /* Lock */
|
---|
68 | KSPIN_LOCK Lock;
|
---|
69 | VBOXDRVTOOL_REF Ref;
|
---|
70 | VBOXUSB_PNPSTATE PnPState;
|
---|
71 | VBOXUSB_PWRSTATE PwrState;
|
---|
72 | /* current dev caps */
|
---|
73 | DEVICE_CAPABILITIES DevCaps;
|
---|
74 | } VBOXUSBDEV_DDISTATE, *PVBOXUSBDEV_DDISTATE;
|
---|
75 |
|
---|
76 | typedef struct VBOXUSBDEV_EXT
|
---|
77 | {
|
---|
78 | PDEVICE_OBJECT pFDO;
|
---|
79 | PDEVICE_OBJECT pPDO;
|
---|
80 | PDEVICE_OBJECT pLowerDO;
|
---|
81 |
|
---|
82 | VBOXUSBDEV_DDISTATE DdiState;
|
---|
83 |
|
---|
84 | uint32_t cHandles;
|
---|
85 |
|
---|
86 | VBOXUSB_RT Rt;
|
---|
87 |
|
---|
88 | } VBOXUSBDEV_EXT, *PVBOXUSBDEV_EXT;
|
---|
89 |
|
---|
90 | /* pnp state api */
|
---|
91 | DECLINLINE(ENMVBOXUSB_PNPSTATE) vboxUsbPnPStateGet(PVBOXUSBDEV_EXT pDevExt)
|
---|
92 | {
|
---|
93 | return (ENMVBOXUSB_PNPSTATE)ASMAtomicUoReadU32((volatile uint32_t*)&pDevExt->DdiState.PnPState.Curr);
|
---|
94 | }
|
---|
95 |
|
---|
96 | DECLINLINE(ENMVBOXUSB_PNPSTATE) vboxUsbPnPStateSet(PVBOXUSBDEV_EXT pDevExt, ENMVBOXUSB_PNPSTATE enmState)
|
---|
97 | {
|
---|
98 | KIRQL Irql;
|
---|
99 | ENMVBOXUSB_PNPSTATE enmOldState;
|
---|
100 | KeAcquireSpinLock(&pDevExt->DdiState.Lock, &Irql);
|
---|
101 | pDevExt->DdiState.PnPState.Prev = (ENMVBOXUSB_PNPSTATE)ASMAtomicUoReadU32((volatile uint32_t*)&pDevExt->DdiState.PnPState.Curr);
|
---|
102 | ASMAtomicWriteU32((volatile uint32_t*)&pDevExt->DdiState.PnPState.Curr, (uint32_t)enmState);
|
---|
103 | pDevExt->DdiState.PnPState.Curr = enmState;
|
---|
104 | enmOldState = pDevExt->DdiState.PnPState.Prev;
|
---|
105 | KeReleaseSpinLock(&pDevExt->DdiState.Lock, Irql);
|
---|
106 | VBOXUSB_PNP_GBG_STATE_CHANGE(enmOldState, enmState);
|
---|
107 | return enmState;
|
---|
108 | }
|
---|
109 |
|
---|
110 | DECLINLINE(ENMVBOXUSB_PNPSTATE) vboxUsbPnPStateRestore(PVBOXUSBDEV_EXT pDevExt)
|
---|
111 | {
|
---|
112 | ENMVBOXUSB_PNPSTATE enmNewState, enmOldState;
|
---|
113 | KIRQL Irql;
|
---|
114 | KeAcquireSpinLock(&pDevExt->DdiState.Lock, &Irql);
|
---|
115 | enmOldState = pDevExt->DdiState.PnPState.Curr;
|
---|
116 | enmNewState = pDevExt->DdiState.PnPState.Prev;
|
---|
117 | ASMAtomicWriteU32((volatile uint32_t*)&pDevExt->DdiState.PnPState.Curr, (uint32_t)pDevExt->DdiState.PnPState.Prev);
|
---|
118 | KeReleaseSpinLock(&pDevExt->DdiState.Lock, Irql);
|
---|
119 | VBOXUSB_PNP_GBG_STATE_CHANGE(enmOldState, enmNewState);
|
---|
120 | Assert(enmNewState == ENMVBOXUSB_PNPSTATE_STARTED);
|
---|
121 | Assert(enmOldState == ENMVBOXUSB_PNPSTATE_STOP_PENDING
|
---|
122 | || enmOldState == ENMVBOXUSB_PNPSTATE_REMOVE_PENDING);
|
---|
123 | return enmNewState;
|
---|
124 | }
|
---|
125 |
|
---|
126 | DECLINLINE(VOID) vboxUsbPnPStateInit(PVBOXUSBDEV_EXT pDevExt)
|
---|
127 | {
|
---|
128 | pDevExt->DdiState.PnPState.Curr = pDevExt->DdiState.PnPState.Prev = ENMVBOXUSB_PNPSTATE_START_PENDING;
|
---|
129 | }
|
---|
130 |
|
---|
131 | DECLINLINE(VOID) vboxUsbDdiStateInit(PVBOXUSBDEV_EXT pDevExt)
|
---|
132 | {
|
---|
133 | KeInitializeSpinLock(&pDevExt->DdiState.Lock);
|
---|
134 | VBoxDrvToolRefInit(&pDevExt->DdiState.Ref);
|
---|
135 | vboxUsbPwrStateInit(pDevExt);
|
---|
136 | vboxUsbPnPStateInit(pDevExt);
|
---|
137 | }
|
---|
138 |
|
---|
139 | DECLINLINE(bool) vboxUsbDdiStateRetainIfStarted(PVBOXUSBDEV_EXT pDevExt)
|
---|
140 | {
|
---|
141 | KIRQL oldIrql;
|
---|
142 | bool bRetained = true;
|
---|
143 | KeAcquireSpinLock(&pDevExt->DdiState.Lock, &oldIrql);
|
---|
144 | if (vboxUsbPnPStateGet(pDevExt) == ENMVBOXUSB_PNPSTATE_STARTED)
|
---|
145 | {
|
---|
146 | VBoxDrvToolRefRetain(&pDevExt->DdiState.Ref);
|
---|
147 | }
|
---|
148 | else
|
---|
149 | {
|
---|
150 | bRetained = false;
|
---|
151 | }
|
---|
152 | KeReleaseSpinLock(&pDevExt->DdiState.Lock, oldIrql);
|
---|
153 | return bRetained;
|
---|
154 | }
|
---|
155 |
|
---|
156 | /* if device is removed - does nothing and returns zero,
|
---|
157 | * otherwise increments a ref counter and returns the current pnp state
|
---|
158 | * NOTE: never returns ENMVBOXUSB_PNPSTATE_REMOVED
|
---|
159 | * */
|
---|
160 | DECLINLINE(ENMVBOXUSB_PNPSTATE) vboxUsbDdiStateRetainIfNotRemoved(PVBOXUSBDEV_EXT pDevExt)
|
---|
161 | {
|
---|
162 | KIRQL oldIrql;
|
---|
163 | ENMVBOXUSB_PNPSTATE enmState;
|
---|
164 | KeAcquireSpinLock(&pDevExt->DdiState.Lock, &oldIrql);
|
---|
165 | enmState = vboxUsbPnPStateGet(pDevExt);
|
---|
166 | if (enmState != ENMVBOXUSB_PNPSTATE_REMOVED)
|
---|
167 | {
|
---|
168 | VBoxDrvToolRefRetain(&pDevExt->DdiState.Ref);
|
---|
169 | }
|
---|
170 | KeReleaseSpinLock(&pDevExt->DdiState.Lock, oldIrql);
|
---|
171 | return enmState != ENMVBOXUSB_PNPSTATE_REMOVED ? enmState : (ENMVBOXUSB_PNPSTATE)0;
|
---|
172 | }
|
---|
173 |
|
---|
174 | DECLINLINE(uint32_t) vboxUsbDdiStateRetain(PVBOXUSBDEV_EXT pDevExt)
|
---|
175 | {
|
---|
176 | return VBoxDrvToolRefRetain(&pDevExt->DdiState.Ref);
|
---|
177 | }
|
---|
178 |
|
---|
179 | DECLINLINE(uint32_t) vboxUsbDdiStateRelease(PVBOXUSBDEV_EXT pDevExt)
|
---|
180 | {
|
---|
181 | return VBoxDrvToolRefRelease(&pDevExt->DdiState.Ref);
|
---|
182 | }
|
---|
183 |
|
---|
184 | DECLINLINE(VOID) vboxUsbDdiStateReleaseAndWaitCompleted(PVBOXUSBDEV_EXT pDevExt)
|
---|
185 | {
|
---|
186 | VBoxDrvToolRefRelease(&pDevExt->DdiState.Ref);
|
---|
187 | VBoxDrvToolRefWaitEqual(&pDevExt->DdiState.Ref, 1);
|
---|
188 | }
|
---|
189 |
|
---|
190 | DECLINLINE(VOID) vboxUsbDdiStateReleaseAndWaitRemoved(PVBOXUSBDEV_EXT pDevExt)
|
---|
191 | {
|
---|
192 | VBoxDrvToolRefRelease(&pDevExt->DdiState.Ref);
|
---|
193 | VBoxDrvToolRefWaitEqual(&pDevExt->DdiState.Ref, 0);
|
---|
194 | }
|
---|
195 |
|
---|
196 | #endif /* #ifndef ___VBoxUsbDev_h___ */
|
---|