1 | /* $Id: VBoxUsbDev.h 106061 2024-09-16 14:03:52Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * VBoxUsbDev.h - USB device.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2011-2024 Oracle and/or its affiliates.
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox base platform packages, as
|
---|
10 | * available from https://www.virtualbox.org.
|
---|
11 | *
|
---|
12 | * This program is free software; you can redistribute it and/or
|
---|
13 | * modify it under the terms of the GNU General Public License
|
---|
14 | * as published by the Free Software Foundation, in version 3 of the
|
---|
15 | * License.
|
---|
16 | *
|
---|
17 | * This program is distributed in the hope that it will be useful, but
|
---|
18 | * WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
19 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
---|
20 | * General Public License for more details.
|
---|
21 | *
|
---|
22 | * You should have received a copy of the GNU General Public License
|
---|
23 | * along with this program; if not, see <https://www.gnu.org/licenses>.
|
---|
24 | *
|
---|
25 | * The contents of this file may alternatively be used under the terms
|
---|
26 | * of the Common Development and Distribution License Version 1.0
|
---|
27 | * (CDDL), a copy of it is provided in the "COPYING.CDDL" file included
|
---|
28 | * in the VirtualBox distribution, in which case the provisions of the
|
---|
29 | * CDDL are applicable instead of those of the GPL.
|
---|
30 | *
|
---|
31 | * You may elect to license modified versions of this file under the
|
---|
32 | * terms and conditions of either the GPL or the CDDL or both.
|
---|
33 | *
|
---|
34 | * SPDX-License-Identifier: GPL-3.0-only OR CDDL-1.0
|
---|
35 | */
|
---|
36 |
|
---|
37 | #ifndef VBOX_INCLUDED_SRC_VBoxUSB_win_dev_VBoxUsbDev_h
|
---|
38 | #define VBOX_INCLUDED_SRC_VBoxUSB_win_dev_VBoxUsbDev_h
|
---|
39 | #ifndef RT_WITHOUT_PRAGMA_ONCE
|
---|
40 | # pragma once
|
---|
41 | #endif
|
---|
42 |
|
---|
43 | #include "VBoxUsbCmn.h"
|
---|
44 | #include <VBox/cdefs.h>
|
---|
45 | #include <iprt/assert.h>
|
---|
46 |
|
---|
47 | typedef struct VBOXUSB_GLOBALS
|
---|
48 | {
|
---|
49 | PDRIVER_OBJECT pDrvObj;
|
---|
50 | UNICODE_STRING RegPath;
|
---|
51 | VBOXUSBRT_IDC RtIdc;
|
---|
52 | } VBOXUSB_GLOBALS, *PVBOXUSB_GLOBALS;
|
---|
53 |
|
---|
54 | extern VBOXUSB_GLOBALS g_VBoxUsbGlobals;
|
---|
55 |
|
---|
56 | /* pnp state decls */
|
---|
57 | typedef enum
|
---|
58 | {
|
---|
59 | ENMVBOXUSB_PNPSTATE_UNKNOWN = 0,
|
---|
60 | ENMVBOXUSB_PNPSTATE_START_PENDING,
|
---|
61 | ENMVBOXUSB_PNPSTATE_STARTED,
|
---|
62 | ENMVBOXUSB_PNPSTATE_STOP_PENDING,
|
---|
63 | ENMVBOXUSB_PNPSTATE_STOPPED,
|
---|
64 | ENMVBOXUSB_PNPSTATE_SURPRISE_REMOVED,
|
---|
65 | ENMVBOXUSB_PNPSTATE_REMOVE_PENDING,
|
---|
66 | ENMVBOXUSB_PNPSTATE_REMOVED,
|
---|
67 | ENMVBOXUSB_PNPSTATE_FORSEDWORD = 0x8fffffff
|
---|
68 | } ENMVBOXUSB_PNPSTATE;
|
---|
69 | AssertCompile(sizeof (ENMVBOXUSB_PNPSTATE) == sizeof (uint32_t));
|
---|
70 |
|
---|
71 | #ifdef VBOX_STRICT
|
---|
72 | DECLHIDDEN(VOID) vboxUsbPnPStateGbgChange(ENMVBOXUSB_PNPSTATE enmOld, ENMVBOXUSB_PNPSTATE enmNew);
|
---|
73 | # define VBOXUSB_PNP_GBG_STATE_CHANGE(_old, _new) vboxUsbPnPStateGbgChange((_old), (_new))
|
---|
74 | #else
|
---|
75 | # define VBOXUSB_PNP_GBG_STATE_CHANGE(_old, _new) do { } while (0)
|
---|
76 | #endif
|
---|
77 |
|
---|
78 |
|
---|
79 | typedef struct VBOXUSB_PNPSTATE
|
---|
80 | {
|
---|
81 | /* Current state */
|
---|
82 | volatile ENMVBOXUSB_PNPSTATE Curr;
|
---|
83 | /* Previous state, used to restore state info on cancell stop device */
|
---|
84 | ENMVBOXUSB_PNPSTATE Prev;
|
---|
85 | } VBOXUSB_PNPSTATE, *PVBOXUSB_PNPSTATE;
|
---|
86 |
|
---|
87 | typedef struct VBOXUSBDEV_DDISTATE
|
---|
88 | {
|
---|
89 | /* Lock */
|
---|
90 | KSPIN_LOCK Lock;
|
---|
91 | VBOXDRVTOOL_REF Ref;
|
---|
92 | VBOXUSB_PNPSTATE PnPState;
|
---|
93 | VBOXUSB_PWRSTATE PwrState;
|
---|
94 | /* current dev caps */
|
---|
95 | DEVICE_CAPABILITIES DevCaps;
|
---|
96 | } VBOXUSBDEV_DDISTATE, *PVBOXUSBDEV_DDISTATE;
|
---|
97 |
|
---|
98 | typedef struct VBOXUSBDEV_EXT
|
---|
99 | {
|
---|
100 | PDEVICE_OBJECT pFDO;
|
---|
101 | PDEVICE_OBJECT pPDO;
|
---|
102 | PDEVICE_OBJECT pLowerDO;
|
---|
103 |
|
---|
104 | VBOXUSBDEV_DDISTATE DdiState;
|
---|
105 |
|
---|
106 | uint32_t cHandles;
|
---|
107 |
|
---|
108 | VBOXUSB_RT Rt;
|
---|
109 |
|
---|
110 | } VBOXUSBDEV_EXT, *PVBOXUSBDEV_EXT;
|
---|
111 |
|
---|
112 | /* pnp state api */
|
---|
113 | DECLINLINE(ENMVBOXUSB_PNPSTATE) vboxUsbPnPStateGet(PVBOXUSBDEV_EXT pDevExt)
|
---|
114 | {
|
---|
115 | return (ENMVBOXUSB_PNPSTATE)ASMAtomicUoReadU32((volatile uint32_t*)&pDevExt->DdiState.PnPState.Curr);
|
---|
116 | }
|
---|
117 |
|
---|
118 | DECLINLINE(ENMVBOXUSB_PNPSTATE) vboxUsbPnPStateSet(PVBOXUSBDEV_EXT pDevExt, ENMVBOXUSB_PNPSTATE enmState)
|
---|
119 | {
|
---|
120 | KIRQL Irql;
|
---|
121 | ENMVBOXUSB_PNPSTATE enmOldState;
|
---|
122 | KeAcquireSpinLock(&pDevExt->DdiState.Lock, &Irql);
|
---|
123 | pDevExt->DdiState.PnPState.Prev = (ENMVBOXUSB_PNPSTATE)ASMAtomicUoReadU32((volatile uint32_t*)&pDevExt->DdiState.PnPState.Curr);
|
---|
124 | ASMAtomicWriteU32((volatile uint32_t*)&pDevExt->DdiState.PnPState.Curr, (uint32_t)enmState);
|
---|
125 | pDevExt->DdiState.PnPState.Curr = enmState;
|
---|
126 | enmOldState = pDevExt->DdiState.PnPState.Prev;
|
---|
127 | KeReleaseSpinLock(&pDevExt->DdiState.Lock, Irql);
|
---|
128 | VBOXUSB_PNP_GBG_STATE_CHANGE(enmOldState, enmState);
|
---|
129 | return enmState;
|
---|
130 | }
|
---|
131 |
|
---|
132 | DECLINLINE(ENMVBOXUSB_PNPSTATE) vboxUsbPnPStateRestore(PVBOXUSBDEV_EXT pDevExt)
|
---|
133 | {
|
---|
134 | ENMVBOXUSB_PNPSTATE enmNewState, enmOldState;
|
---|
135 | KIRQL Irql;
|
---|
136 | KeAcquireSpinLock(&pDevExt->DdiState.Lock, &Irql);
|
---|
137 | enmOldState = pDevExt->DdiState.PnPState.Curr;
|
---|
138 | enmNewState = pDevExt->DdiState.PnPState.Prev;
|
---|
139 | ASMAtomicWriteU32((volatile uint32_t*)&pDevExt->DdiState.PnPState.Curr, (uint32_t)pDevExt->DdiState.PnPState.Prev);
|
---|
140 | KeReleaseSpinLock(&pDevExt->DdiState.Lock, Irql);
|
---|
141 | VBOXUSB_PNP_GBG_STATE_CHANGE(enmOldState, enmNewState);
|
---|
142 | Assert(enmNewState == ENMVBOXUSB_PNPSTATE_STARTED);
|
---|
143 | Assert(enmOldState == ENMVBOXUSB_PNPSTATE_STOP_PENDING
|
---|
144 | || enmOldState == ENMVBOXUSB_PNPSTATE_REMOVE_PENDING);
|
---|
145 | return enmNewState;
|
---|
146 | }
|
---|
147 |
|
---|
148 | DECLINLINE(VOID) vboxUsbPnPStateInit(PVBOXUSBDEV_EXT pDevExt)
|
---|
149 | {
|
---|
150 | pDevExt->DdiState.PnPState.Curr = pDevExt->DdiState.PnPState.Prev = ENMVBOXUSB_PNPSTATE_START_PENDING;
|
---|
151 | }
|
---|
152 |
|
---|
153 | DECLINLINE(VOID) vboxUsbDdiStateInit(PVBOXUSBDEV_EXT pDevExt)
|
---|
154 | {
|
---|
155 | KeInitializeSpinLock(&pDevExt->DdiState.Lock);
|
---|
156 | VBoxDrvToolRefInit(&pDevExt->DdiState.Ref);
|
---|
157 | vboxUsbPwrStateInit(pDevExt);
|
---|
158 | vboxUsbPnPStateInit(pDevExt);
|
---|
159 | }
|
---|
160 |
|
---|
161 | DECLINLINE(bool) vboxUsbDdiStateRetainIfStarted(PVBOXUSBDEV_EXT pDevExt)
|
---|
162 | {
|
---|
163 | KIRQL oldIrql;
|
---|
164 | bool bRetained = true;
|
---|
165 | KeAcquireSpinLock(&pDevExt->DdiState.Lock, &oldIrql);
|
---|
166 | if (vboxUsbPnPStateGet(pDevExt) == ENMVBOXUSB_PNPSTATE_STARTED)
|
---|
167 | {
|
---|
168 | VBoxDrvToolRefRetain(&pDevExt->DdiState.Ref);
|
---|
169 | }
|
---|
170 | else
|
---|
171 | {
|
---|
172 | bRetained = false;
|
---|
173 | }
|
---|
174 | KeReleaseSpinLock(&pDevExt->DdiState.Lock, oldIrql);
|
---|
175 | return bRetained;
|
---|
176 | }
|
---|
177 |
|
---|
178 | /* if device is removed - does nothing and returns zero,
|
---|
179 | * otherwise increments a ref counter and returns the current pnp state
|
---|
180 | * NOTE: never returns ENMVBOXUSB_PNPSTATE_REMOVED
|
---|
181 | * */
|
---|
182 | DECLINLINE(ENMVBOXUSB_PNPSTATE) vboxUsbDdiStateRetainIfNotRemoved(PVBOXUSBDEV_EXT pDevExt)
|
---|
183 | {
|
---|
184 | KIRQL oldIrql;
|
---|
185 | ENMVBOXUSB_PNPSTATE enmState;
|
---|
186 | KeAcquireSpinLock(&pDevExt->DdiState.Lock, &oldIrql);
|
---|
187 | enmState = vboxUsbPnPStateGet(pDevExt);
|
---|
188 | if (enmState != ENMVBOXUSB_PNPSTATE_REMOVED)
|
---|
189 | {
|
---|
190 | VBoxDrvToolRefRetain(&pDevExt->DdiState.Ref);
|
---|
191 | }
|
---|
192 | KeReleaseSpinLock(&pDevExt->DdiState.Lock, oldIrql);
|
---|
193 | return enmState != ENMVBOXUSB_PNPSTATE_REMOVED ? enmState : (ENMVBOXUSB_PNPSTATE)0;
|
---|
194 | }
|
---|
195 |
|
---|
196 | DECLINLINE(uint32_t) vboxUsbDdiStateRetain(PVBOXUSBDEV_EXT pDevExt)
|
---|
197 | {
|
---|
198 | return VBoxDrvToolRefRetain(&pDevExt->DdiState.Ref);
|
---|
199 | }
|
---|
200 |
|
---|
201 | DECLINLINE(uint32_t) vboxUsbDdiStateRelease(PVBOXUSBDEV_EXT pDevExt)
|
---|
202 | {
|
---|
203 | return VBoxDrvToolRefRelease(&pDevExt->DdiState.Ref);
|
---|
204 | }
|
---|
205 |
|
---|
206 | DECLINLINE(VOID) vboxUsbDdiStateReleaseAndWaitCompleted(PVBOXUSBDEV_EXT pDevExt)
|
---|
207 | {
|
---|
208 | VBoxDrvToolRefRelease(&pDevExt->DdiState.Ref);
|
---|
209 | VBoxDrvToolRefWaitEqual(&pDevExt->DdiState.Ref, 1);
|
---|
210 | }
|
---|
211 |
|
---|
212 | DECLINLINE(VOID) vboxUsbDdiStateReleaseAndWaitRemoved(PVBOXUSBDEV_EXT pDevExt)
|
---|
213 | {
|
---|
214 | VBoxDrvToolRefRelease(&pDevExt->DdiState.Ref);
|
---|
215 | VBoxDrvToolRefWaitEqual(&pDevExt->DdiState.Ref, 0);
|
---|
216 | }
|
---|
217 |
|
---|
218 | #endif /* !VBOX_INCLUDED_SRC_VBoxUSB_win_dev_VBoxUsbDev_h */
|
---|