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