1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
|
/*++
Copyright (C) Microsoft Corporation, All Rights Reserved.
Module Name:
Device.cpp
Abstract:
This module contains the implementation of the UMDF sample
driver's device callback object.
Environment:
Windows User-Mode Driver Framework (WUDF)
--*/
#include "internal.h"
#include "device.tmh"
HRESULT
CMyDevice::Initialize(
_In_ IWDFDriver* FxDriver,
_In_ IWDFDeviceInitialize* FxDeviceInit
)
/*++
Routine Description:
This method initializes the device callback object and creates the
partner device object.
The method should perform any device-specific configuration that:
* could fail (these can't be done in the constructor)
* must be done before the partner object is created -or-
* can be done after the partner object is created and which aren't
influenced by any device-level parameters the parent (the driver
in this case) might set.
Arguments:
FxDeviceInit - the settings for this device.
FxDriver - IWDF Driver for this device.
Return Value:
status.
--*/
{
MethodEntry("...");
CComPtr<IWDFDevice> fxDevice;
HRESULT hr;
//
// Configure things like the locking model before we go to create our
// partner device.
//
//
// Set the locking model
//
FxDeviceInit->SetLockingConstraint(None);
//
// TODO: Any per-device initialization which must be done before
// creating the partner object.
//
//
// Create a new FX device object and assign the new callback object to
// handle any device level events that occur.
//
//
// QueryIUnknown references the IUnknown interface that it returns
// (which is the same as referencing the device). We pass that to
// CreateDevice, which takes its own reference if everything works.
//
CComPtr<IUnknown> pUnk;
HRESULT hrQI = this->QueryInterface(__uuidof(IUnknown),(void**)&pUnk);
WUDF_SAMPLE_DRIVER_ASSERT(SUCCEEDED(hrQI));
hr = FxDriver->CreateDevice(FxDeviceInit, pUnk, &fxDevice);
//
// If that succeeded then set our FxDevice member variable.
//
if (SUCCEEDED(hr))
{
m_FxDevice = fxDevice;
}
MethodReturnHR(hr);
}
HRESULT
CMyDevice::Configure(
VOID
)
/*++
Routine Description:
This method is called after the device callback object has been initialized
and returned to the driver. It would setup the device's queues and their
corresponding callback objects.
Arguments:
FxDevice - the framework device object for which we're handling events.
Return Value:
status
--*/
{
MethodEntry("void");
//
// Create a new instance of our Queue callback object
//
CComObject<CMyQueue> * defaultQueue = NULL;
HRESULT hr = CComObject<CMyQueue>::CreateInstance(&defaultQueue);
if (SUCCEEDED(hr))
{
defaultQueue->AddRef();
hr = defaultQueue->Initialize(this);
}
if (SUCCEEDED(hr))
{
hr = defaultQueue->Configure();
}
//
// Create and Enable Device Interfaces for this device.
//
if (SUCCEEDED(hr))
{
hr = m_FxDevice->CreateDeviceInterface(&GUID_DEVINTERFACE_NETNFP,
NULL);
}
if (SUCCEEDED(hr))
{
hr = m_FxDevice->AssignDeviceInterfaceState(&GUID_DEVINTERFACE_NETNFP,
NULL,
TRUE);
}
if (SUCCEEDED(hr))
{
hr = m_FxDevice->CreateDeviceInterface(&GUID_DEVINTERFACE_NFP,
NULL);
}
if (SUCCEEDED(hr))
{
hr = m_FxDevice->AssignDeviceInterfaceState(&GUID_DEVINTERFACE_NFP,
NULL,
TRUE);
}
if (SUCCEEDED(hr))
{
//
// Save a pointer to our queue, so we can lock it during file cleanup
//
m_MyQueue = defaultQueue;
}
//
// Release the reference we took on the queue object.
// The framework took its own references on the object's callback interfaces
// when we called m_FxDevice->CreateIoQueue, and will manage the object's lifetime.
//
SAFE_RELEASE(defaultQueue);
MethodReturnHR(hr);
}
STDMETHODIMP_(void)
CMyDevice::OnCloseFile(
_In_ IWDFFile* pWdfFileObject
)
{
MethodEntry("...");
m_MyQueue->OnCloseFile(pWdfFileObject);
MethodReturnVoid();
}
STDMETHODIMP_(void)
CMyDevice::OnCleanupFile(
_In_ IWDFFile* /*pWdfFileObject*/
)
/*++
Routine Description:
This method is when app with open handle device terminates.
Arguments:
pWdfFileObject - the framework file object for which close is handled.
Return Value:
None
--*/
{
}
STDMETHODIMP_(void)
CMyDevice::OnCleanup(
_In_ IWDFObject* pWdfObject
)
/*++
Routine Description:
This device callback method is invoked by the framework when the WdfObject
is about to be released by the framework.
Arguments:
pWdfObject - the framework device object for which OnCleanup.
Return Value:
None
--*/
{
MethodEntry("...");
WUDF_SAMPLE_DRIVER_ASSERT(pWdfObject == m_FxDevice);
m_MyQueue = NULL;
//
// CMyDevice has a reference to framework device object via m_Device.
// Framework device object has a reference to CMyDevice object via the callbacks.
// This leads to circular reference and both the objects can't be destroyed until this circular reference is broken.
// To break the circular reference we release the reference to the framework device object here in OnCleanup.
//
m_FxDevice = NULL;
MethodReturnVoid();
}
|