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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
|
/*++
Copyright (c) Microsoft Corporation. All rights reserved.
THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY
KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR
PURPOSE.
Module Name:
Rawpdo.c
Abstract:
This modules has routines to enumerate dip switches on the board
as child devices. We use dynamic enumeration interfaces to manage
child devices.
Environment:
Kernel mode
--*/
#include <osrusbfx2.h>
#include "rawpdo.h"
#include "rawpdo.tmh"
VOID
OsrFxInitChildList(
IN PWDFDEVICE_INIT DeviceInit
)
/*++
Routine Description:
This routine is called from the EvtDeviceAdd routine to initialize
the default child list.
Arguments:
Return Value:
NT status value
--*/
{
WDF_CHILD_LIST_CONFIG config;
//
// Init the default child list so that we can enumerate a raw PDO
//
WDF_CHILD_LIST_CONFIG_INIT(&config,
sizeof(PDO_IDENTIFICATION_DESCRIPTION),
OsrEvtDeviceListCreatePdo // callback to create a child device.
);
//
// Tell the framework to use the built-in devicelist to track the state
// of the device based on the configuration we just created.
//
WdfFdoInitSetDefaultChildListConfig(DeviceInit,
&config,
WDF_NO_OBJECT_ATTRIBUTES);
return;
}
NTSTATUS
OsrEvtDeviceListCreatePdo(
WDFCHILDLIST DeviceList,
PWDF_CHILD_IDENTIFICATION_DESCRIPTION_HEADER IdentificationDescription,
PWDFDEVICE_INIT ChildInit
)
/*++
Routine Description:
Called by the framework in response to Query-Device relation when
a new PDO for a child device needs to be created.
Arguments:
DeviceList - Handle to the default WDFCHILDLIST created by the
framework as part of FDO.
IdentificationDescription - Decription of the new child device.
ChildInit - It's a opaque structure used in collecting device settings
and passed in as a parameter to CreateDevice.
Return Value:
NT Status code.
--*/
{
NTSTATUS status;
WDFDEVICE hChild = NULL;
PPDO_IDENTIFICATION_DESCRIPTION pDesc;
WDF_DEVICE_POWER_POLICY_IDLE_SETTINGS idleSettings;
DECLARE_CONST_UNICODE_STRING(deviceId, OSRUSBFX2_SWITCH_DEVICE_ID );
DECLARE_CONST_UNICODE_STRING(hardwareId, OSRUSBFX2_SWITCH_DEVICE_ID );
DECLARE_CONST_UNICODE_STRING(deviceLocation, L"OSR USB-FX2 Learning Kit" );
DECLARE_UNICODE_STRING_SIZE(buffer, DEVICE_DESC_LENGTH);
UNREFERENCED_PARAMETER(DeviceList);
pDesc = CONTAINING_RECORD(IdentificationDescription,
PDO_IDENTIFICATION_DESCRIPTION,
Header);
//
// Mark the device RAW so that the child device can be started
// and accessed without requiring a function driver. Since we are
// creating a RAW PDO, we must provide a class guid.
//
status = WdfPdoInitAssignRawDevice(ChildInit, &GUID_DEVCLASS_OSRUSBFX2);
if (!NT_SUCCESS(status)) {
goto Cleanup;
}
//
// Since our devices for switches can trigger nuclear explosion,
// we must protect them from random users sending I/Os.
//
status = WdfDeviceInitAssignSDDLString(ChildInit,
&SDDL_DEVOBJ_SYS_ALL_ADM_ALL);
if (!NT_SUCCESS(status)) {
goto Cleanup;
}
status = WdfPdoInitAssignDeviceID(ChildInit, &deviceId);
if (!NT_SUCCESS(status)) {
goto Cleanup;
}
//
// On XP and later, there is no need to provide following IDs for raw pdos.
// BusQueryHardwareIDs but on On Win2K, we must provide a HWID and a NULL
// section in the INF to get the device installed without any problem.
//
status = WdfPdoInitAddHardwareID(ChildInit, &hardwareId);
if (!NT_SUCCESS(status)) {
goto Cleanup;
}
//
// Since we are enumerating more than one children, we must
// provide a BusQueryInstanceID. If we don't, system will throw
// CA bugcheck.
//
status = RtlUnicodeStringPrintf(&buffer, L"%02u", pDesc->SwitchNumber);
if (!NT_SUCCESS(status)) {
return status;
}
status = WdfPdoInitAssignInstanceID(ChildInit, &buffer);
if (!NT_SUCCESS(status)) {
return status;
}
//
// Provide a description about the device. This text is usually read from
// the device. In the case of USB device, this text comes from the string
// descriptor. This text is displayed momentarily by the PnP manager while
// it's looking for a matching INF. If it finds one, it uses the Device
// Description from the INF file to display in the device manager.
// Since our device is raw device and we don't provide any hardware ID
// to match with an INF, this text will be displayed in the device manager.
//
status = RtlUnicodeStringPrintf(&buffer,
L"OsrUsbFX2 RawPdo For Switch %02u",
pDesc->SwitchNumber);
if (!NT_SUCCESS(status)) {
goto Cleanup;
}
//
// You can call WdfPdoInitAddDeviceText multiple times, adding device
// text for multiple locales. When the system displays the text, it
// chooses the text that matches the current locale, if available.
// Otherwise it will use the string for the default locale.
// The driver can specify the driver's default locale by calling
// WdfPdoInitSetDefaultLocale.
//
status = WdfPdoInitAddDeviceText(ChildInit,
&buffer,
&deviceLocation,
0x409);
if (!NT_SUCCESS(status)) {
goto Cleanup;
}
WdfPdoInitSetDefaultLocale(ChildInit, 0x409);
status = WdfDeviceCreate(&ChildInit, WDF_NO_OBJECT_ATTRIBUTES, &hChild);
if (!NT_SUCCESS(status)) {
goto Cleanup;
}
//
// Set idle-time out on the child device. This is required to allow
// the parent device to idle-out when there are no active I/O.
//
WDF_DEVICE_POWER_POLICY_IDLE_SETTINGS_INIT(&idleSettings, IdleCannotWakeFromS0);
idleSettings.IdleTimeout = 1000; // 1-sec
status = WdfDeviceAssignS0IdleSettings(hChild, &idleSettings);
if ( !NT_SUCCESS(status)) {
TraceEvents(TRACE_LEVEL_ERROR, DBG_PNP,
"WdfDeviceSetPowerPolicyS0IdlePolicy failed %x\n", status);
return status;
}
return status;
Cleanup:
TraceEvents(TRACE_LEVEL_ERROR, DBG_PNP,"CreatePdo failed %x\n", status);
//
// On error, framework will cleanup all the resources when it deletes
// the device. So there is nothing to do.
//
return status;
}
_IRQL_requires_max_(DISPATCH_LEVEL)
VOID
OsrFxEnumerateChildren(
_In_ WDFDEVICE Device
)
/*++
Routine Description:
This routine configures a continuous reader on the
interrupt endpoint.
Arguments:
Return Value:
NT status value
--*/
{
WDFCHILDLIST list;
UCHAR i;
NTSTATUS status;
PDEVICE_CONTEXT pDeviceContext;
pDeviceContext = GetDeviceContext(Device);
list = WdfFdoGetDefaultChildList(Device);
WdfChildListBeginScan(list);
//
// A call to WdfChildListBeginScan indicates to the framework that the
// driver is about to scan for dynamic children. If the driver doesn't
// call either WdfChildListUpdateChildDescriptionAsPresent or
// WdfChildListMarkAllChildDescriptionsPresent before WdfChildListEndScan is,
// called, all the previously reported children will be reported as missing
// to the PnP subsystem.
//
for(i=0; i< RTL_BITS_OF(UCHAR); i++) {
//
// Report every set bit in the switchstate as a child device.
//
if(pDeviceContext->CurrentSwitchState & (1<<i)) {
PDO_IDENTIFICATION_DESCRIPTION description;
//
// Initialize the description with the information about the newly
// plugged in device.
//
WDF_CHILD_IDENTIFICATION_DESCRIPTION_HEADER_INIT(
&description.Header,
sizeof(description)
);
//
// Since switches are marked in the wrong order on the board,
// we will fix it here so that the DM display matches with the
// board.
//
description.SwitchNumber = RTL_BITS_OF(UCHAR)-i;
TraceEvents(TRACE_LEVEL_INFORMATION, DBG_PNP,
"Switch %d is ON\n", description.SwitchNumber);
//
// Call the framework to add this child to the devicelist. This call
// will internaly call our DescriptionCompare callback to check
// whether this device is a new device or existing device. If
// it's a new device, the framework will call DescriptionDuplicate to create
// a copy of this description in nonpaged pool.
// The actual creation of the child device will happen when the framework
// receives QUERY_DEVICE_RELATION request from the PNP manager in
// response to InvalidateDevice relation call made as part of adding
// a new child.
//
status = WdfChildListAddOrUpdateChildDescriptionAsPresent(
list,
&description.Header,
NULL); // AddressDescription
if (status == STATUS_OBJECT_NAME_EXISTS) {
}
}
}
WdfChildListEndScan(list);
return;
}
|