1 | /* $Id: VBoxUsbDev.h 62490 2016-07-22 18:41:49Z 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 | #include "VBoxUsbCmn.h"
|
---|
21 | #include <iprt/assert.h>
|
---|
22 |
|
---|
23 | typedef struct VBOXUSB_GLOBALS
|
---|
24 | {
|
---|
25 | PDRIVER_OBJECT pDrvObj;
|
---|
26 | UNICODE_STRING RegPath;
|
---|
27 | VBOXUSBRT_IDC RtIdc;
|
---|
28 | } VBOXUSB_GLOBALS, *PVBOXUSB_GLOBALS;
|
---|
29 |
|
---|
30 | extern VBOXUSB_GLOBALS g_VBoxUsbGlobals;
|
---|
31 |
|
---|
32 | /* pnp state decls */
|
---|
33 | typedef enum
|
---|
34 | {
|
---|
35 | ENMVBOXUSB_PNPSTATE_UNKNOWN = 0,
|
---|
36 | ENMVBOXUSB_PNPSTATE_START_PENDING,
|
---|
37 | ENMVBOXUSB_PNPSTATE_STARTED,
|
---|
38 | ENMVBOXUSB_PNPSTATE_STOP_PENDING,
|
---|
39 | ENMVBOXUSB_PNPSTATE_STOPPED,
|
---|
40 | ENMVBOXUSB_PNPSTATE_SURPRISE_REMOVED,
|
---|
41 | ENMVBOXUSB_PNPSTATE_REMOVE_PENDING,
|
---|
42 | ENMVBOXUSB_PNPSTATE_REMOVED,
|
---|
43 | ENMVBOXUSB_PNPSTATE_FORSEDWORD = 0x8fffffff
|
---|
44 | } ENMVBOXUSB_PNPSTATE;
|
---|
45 | AssertCompile(sizeof (ENMVBOXUSB_PNPSTATE) == sizeof (uint32_t));
|
---|
46 |
|
---|
47 | #ifdef DEBUG
|
---|
48 | DECLHIDDEN(VOID) vboxUsbPnPStateGbgChange(ENMVBOXUSB_PNPSTATE enmOld, ENMVBOXUSB_PNPSTATE enmNew);
|
---|
49 | # define VBOXUSB_PNP_GBG_STATE_CHANGE(_old, _new) vboxUsbPnPStateGbgChange((_old), (_new))
|
---|
50 | #else
|
---|
51 | # define VBOXUSB_PNP_GBG_STATE_CHANGE(_old, _new) do { } while (0)
|
---|
52 | #endif
|
---|
53 |
|
---|
54 |
|
---|
55 | typedef struct VBOXUSB_PNPSTATE
|
---|
56 | {
|
---|
57 | /* Current state */
|
---|
58 | volatile ENMVBOXUSB_PNPSTATE Curr;
|
---|
59 | /* Previous state, used to restore state info on cancell stop device */
|
---|
60 | ENMVBOXUSB_PNPSTATE Prev;
|
---|
61 | } VBOXUSB_PNPSTATE, *PVBOXUSB_PNPSTATE;
|
---|
62 |
|
---|
63 | typedef struct VBOXUSBDEV_DDISTATE
|
---|
64 | {
|
---|
65 | /* Lock */
|
---|
66 | KSPIN_LOCK Lock;
|
---|
67 | VBOXDRVTOOL_REF Ref;
|
---|
68 | VBOXUSB_PNPSTATE PnPState;
|
---|
69 | VBOXUSB_PWRSTATE PwrState;
|
---|
70 | /* current dev caps */
|
---|
71 | DEVICE_CAPABILITIES DevCaps;
|
---|
72 | } VBOXUSBDEV_DDISTATE, *PVBOXUSBDEV_DDISTATE;
|
---|
73 |
|
---|
74 | typedef struct VBOXUSBDEV_EXT
|
---|
75 | {
|
---|
76 | PDEVICE_OBJECT pFDO;
|
---|
77 | PDEVICE_OBJECT pPDO;
|
---|
78 | PDEVICE_OBJECT pLowerDO;
|
---|
79 |
|
---|
80 | VBOXUSBDEV_DDISTATE DdiState;
|
---|
81 |
|
---|
82 | uint32_t cHandles;
|
---|
83 |
|
---|
84 | VBOXUSB_RT Rt;
|
---|
85 |
|
---|
86 | } VBOXUSBDEV_EXT, *PVBOXUSBDEV_EXT;
|
---|
87 |
|
---|
88 | /* pnp state api */
|
---|
89 | DECLINLINE(ENMVBOXUSB_PNPSTATE) vboxUsbPnPStateGet(PVBOXUSBDEV_EXT pDevExt)
|
---|
90 | {
|
---|
91 | return (ENMVBOXUSB_PNPSTATE)ASMAtomicUoReadU32((volatile uint32_t*)&pDevExt->DdiState.PnPState.Curr);
|
---|
92 | }
|
---|
93 |
|
---|
94 | DECLINLINE(ENMVBOXUSB_PNPSTATE) vboxUsbPnPStateSet(PVBOXUSBDEV_EXT pDevExt, ENMVBOXUSB_PNPSTATE enmState)
|
---|
95 | {
|
---|
96 | KIRQL Irql;
|
---|
97 | ENMVBOXUSB_PNPSTATE enmOldState;
|
---|
98 | KeAcquireSpinLock(&pDevExt->DdiState.Lock, &Irql);
|
---|
99 | pDevExt->DdiState.PnPState.Prev = (ENMVBOXUSB_PNPSTATE)ASMAtomicUoReadU32((volatile uint32_t*)&pDevExt->DdiState.PnPState.Curr);
|
---|
100 | ASMAtomicWriteU32((volatile uint32_t*)&pDevExt->DdiState.PnPState.Curr, (uint32_t)enmState);
|
---|
101 | pDevExt->DdiState.PnPState.Curr = enmState;
|
---|
102 | enmOldState = pDevExt->DdiState.PnPState.Prev;
|
---|
103 | KeReleaseSpinLock(&pDevExt->DdiState.Lock, Irql);
|
---|
104 | VBOXUSB_PNP_GBG_STATE_CHANGE(enmOldState, enmState);
|
---|
105 | return enmState;
|
---|
106 | }
|
---|
107 |
|
---|
108 | DECLINLINE(ENMVBOXUSB_PNPSTATE) vboxUsbPnPStateRestore(PVBOXUSBDEV_EXT pDevExt)
|
---|
109 | {
|
---|
110 | ENMVBOXUSB_PNPSTATE enmNewState, enmOldState;
|
---|
111 | KIRQL Irql;
|
---|
112 | KeAcquireSpinLock(&pDevExt->DdiState.Lock, &Irql);
|
---|
113 | enmOldState = pDevExt->DdiState.PnPState.Curr;
|
---|
114 | enmNewState = pDevExt->DdiState.PnPState.Prev;
|
---|
115 | ASMAtomicWriteU32((volatile uint32_t*)&pDevExt->DdiState.PnPState.Curr, (uint32_t)pDevExt->DdiState.PnPState.Prev);
|
---|
116 | KeReleaseSpinLock(&pDevExt->DdiState.Lock, Irql);
|
---|
117 | VBOXUSB_PNP_GBG_STATE_CHANGE(enmOldState, enmNewState);
|
---|
118 | Assert(enmNewState == ENMVBOXUSB_PNPSTATE_STARTED);
|
---|
119 | Assert(enmOldState == ENMVBOXUSB_PNPSTATE_STOP_PENDING
|
---|
120 | || enmOldState == ENMVBOXUSB_PNPSTATE_REMOVE_PENDING);
|
---|
121 | return enmNewState;
|
---|
122 | }
|
---|
123 |
|
---|
124 | DECLINLINE(VOID) vboxUsbPnPStateInit(PVBOXUSBDEV_EXT pDevExt)
|
---|
125 | {
|
---|
126 | pDevExt->DdiState.PnPState.Curr = pDevExt->DdiState.PnPState.Prev = ENMVBOXUSB_PNPSTATE_START_PENDING;
|
---|
127 | }
|
---|
128 |
|
---|
129 | DECLINLINE(VOID) vboxUsbDdiStateInit(PVBOXUSBDEV_EXT pDevExt)
|
---|
130 | {
|
---|
131 | KeInitializeSpinLock(&pDevExt->DdiState.Lock);
|
---|
132 | VBoxDrvToolRefInit(&pDevExt->DdiState.Ref);
|
---|
133 | vboxUsbPwrStateInit(pDevExt);
|
---|
134 | vboxUsbPnPStateInit(pDevExt);
|
---|
135 | }
|
---|
136 |
|
---|
137 | DECLINLINE(bool) vboxUsbDdiStateRetainIfStarted(PVBOXUSBDEV_EXT pDevExt)
|
---|
138 | {
|
---|
139 | KIRQL oldIrql;
|
---|
140 | bool bRetained = true;
|
---|
141 | KeAcquireSpinLock(&pDevExt->DdiState.Lock, &oldIrql);
|
---|
142 | if (vboxUsbPnPStateGet(pDevExt) == ENMVBOXUSB_PNPSTATE_STARTED)
|
---|
143 | {
|
---|
144 | VBoxDrvToolRefRetain(&pDevExt->DdiState.Ref);
|
---|
145 | }
|
---|
146 | else
|
---|
147 | {
|
---|
148 | bRetained = false;
|
---|
149 | }
|
---|
150 | KeReleaseSpinLock(&pDevExt->DdiState.Lock, oldIrql);
|
---|
151 | return bRetained;
|
---|
152 | }
|
---|
153 |
|
---|
154 | /* if device is removed - does nothing and returns zero,
|
---|
155 | * otherwise increments a ref counter and returns the current pnp state
|
---|
156 | * NOTE: never returns ENMVBOXUSB_PNPSTATE_REMOVED
|
---|
157 | * */
|
---|
158 | DECLINLINE(ENMVBOXUSB_PNPSTATE) vboxUsbDdiStateRetainIfNotRemoved(PVBOXUSBDEV_EXT pDevExt)
|
---|
159 | {
|
---|
160 | KIRQL oldIrql;
|
---|
161 | ENMVBOXUSB_PNPSTATE enmState;
|
---|
162 | KeAcquireSpinLock(&pDevExt->DdiState.Lock, &oldIrql);
|
---|
163 | enmState = vboxUsbPnPStateGet(pDevExt);
|
---|
164 | if (enmState != ENMVBOXUSB_PNPSTATE_REMOVED)
|
---|
165 | {
|
---|
166 | VBoxDrvToolRefRetain(&pDevExt->DdiState.Ref);
|
---|
167 | }
|
---|
168 | KeReleaseSpinLock(&pDevExt->DdiState.Lock, oldIrql);
|
---|
169 | return enmState != ENMVBOXUSB_PNPSTATE_REMOVED ? enmState : (ENMVBOXUSB_PNPSTATE)0;
|
---|
170 | }
|
---|
171 |
|
---|
172 | DECLINLINE(uint32_t) vboxUsbDdiStateRetain(PVBOXUSBDEV_EXT pDevExt)
|
---|
173 | {
|
---|
174 | return VBoxDrvToolRefRetain(&pDevExt->DdiState.Ref);
|
---|
175 | }
|
---|
176 |
|
---|
177 | DECLINLINE(uint32_t) vboxUsbDdiStateRelease(PVBOXUSBDEV_EXT pDevExt)
|
---|
178 | {
|
---|
179 | return VBoxDrvToolRefRelease(&pDevExt->DdiState.Ref);
|
---|
180 | }
|
---|
181 |
|
---|
182 | DECLINLINE(VOID) vboxUsbDdiStateReleaseAndWaitCompleted(PVBOXUSBDEV_EXT pDevExt)
|
---|
183 | {
|
---|
184 | VBoxDrvToolRefRelease(&pDevExt->DdiState.Ref);
|
---|
185 | VBoxDrvToolRefWaitEqual(&pDevExt->DdiState.Ref, 1);
|
---|
186 | }
|
---|
187 |
|
---|
188 | DECLINLINE(VOID) vboxUsbDdiStateReleaseAndWaitRemoved(PVBOXUSBDEV_EXT pDevExt)
|
---|
189 | {
|
---|
190 | VBoxDrvToolRefRelease(&pDevExt->DdiState.Ref);
|
---|
191 | VBoxDrvToolRefWaitEqual(&pDevExt->DdiState.Ref, 0);
|
---|
192 | }
|
---|
193 |
|
---|
194 | #endif /* #ifndef ___VBoxUsbDev_h___ */
|
---|