/
device.c
449 lines (361 loc) · 11.7 KB
/
device.c
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
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
/*++
Copyright (C) Microsoft Corporation, All Rights Reserved.
Module Name:
Device.c
Abstract:
This module contains the implementation of the VirtualSerial sample
driver's device callback object.
The VirtualSerial sample device does very little. It does not implement
either of the PNP interfaces so once the device is setup, it won't ever get
any callbacks until the device is removed.
Environment:
Windows Driver Framework
--*/
#include "internal.h"
NTSTATUS
DeviceCreate(
_In_ WDFDRIVER Driver,
_In_ PWDFDEVICE_INIT DeviceInit,
_Out_ PDEVICE_CONTEXT *DeviceContext
)
/*++
Routine Description:
This method creates and initializs an instance of the VirtualSerial driver's
device callback object.
Arguments:
FxDeviceInit - the settings for the device.
Device - a location to store the referenced pointer to the device object.
Return Value:
Status
--*/
{
NTSTATUS status;
WDF_OBJECT_ATTRIBUTES deviceAttributes;
WDFDEVICE device;
PDEVICE_CONTEXT deviceContext;
UNREFERENCED_PARAMETER (Driver);
WDF_OBJECT_ATTRIBUTES_INIT_CONTEXT_TYPE(
&deviceAttributes,
DEVICE_CONTEXT);
deviceAttributes.SynchronizationScope = WdfSynchronizationScopeDevice;
deviceAttributes.EvtCleanupCallback = EvtDeviceCleanup;
status = WdfDeviceCreate(&DeviceInit,
&deviceAttributes,
&device);
if (!NT_SUCCESS(status)) {
Trace(TRACE_LEVEL_ERROR,
"Error: WdfDeviceCreate failed 0x%x", status);
return status;
}
deviceContext = GetDeviceContext(device);
deviceContext->Device = device;
*DeviceContext = deviceContext;
return status;
}
NTSTATUS
DeviceConfigure(
_In_ PDEVICE_CONTEXT DeviceContext
)
/*++
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
--*/
{
NTSTATUS status;
WDFDEVICE device = DeviceContext->Device;
WDFKEY key;
LPGUID guid;
errno_t errorNo;
DECLARE_CONST_UNICODE_STRING(portName, REG_VALUENAME_PORTNAME);
DECLARE_UNICODE_STRING_SIZE (comPort, 10);
DECLARE_UNICODE_STRING_SIZE (symbolicLinkName, SYMBOLIC_LINK_NAME_LENGTH);
#ifdef _FAKE_MODEM
//
// Compiled as fake modem
//
guid = (LPGUID) &GUID_DEVINTERFACE_MODEM;
#else
//
// Compiled as virtual serial port
//
guid = (LPGUID) &GUID_DEVINTERFACE_COMPORT;
#endif
//
// Create device interface
//
status = WdfDeviceCreateDeviceInterface(
device,
guid,
NULL);
if (!NT_SUCCESS(status)) {
Trace(TRACE_LEVEL_ERROR,
"Error: Cannot create device interface");
goto Exit;
}
//
// Read the COM port number from the registry, which has been automatically
// created by "MsPorts!PortsClassInstaller" if INF file says "Class=Ports"
//
status = WdfDeviceOpenRegistryKey(
device,
PLUGPLAY_REGKEY_DEVICE,
KEY_QUERY_VALUE,
WDF_NO_OBJECT_ATTRIBUTES,
&key);
if (!NT_SUCCESS(status)) {
Trace(TRACE_LEVEL_ERROR,
"Error: Failed to retrieve device hardware key root");
goto Exit;
}
status = WdfRegistryQueryUnicodeString(
key,
&portName,
NULL,
&comPort);
if (!NT_SUCCESS(status)) {
Trace(TRACE_LEVEL_ERROR,
"Error: Failed to read PortName");
goto Exit;
}
//
// Manually create the symbolic link name. Length is the length in
// bytes not including the NULL terminator.
//
// 6054 and 26035 are code analysis warnings that comPort.Buffer might
// not be NULL terminated, while we know that they are.
//
#pragma warning(suppress: 6054 26035)
symbolicLinkName.Length = (USHORT)((wcslen(comPort.Buffer) * sizeof(wchar_t))
+ sizeof(SYMBOLIC_LINK_NAME_PREFIX) - sizeof(UNICODE_NULL));
if (symbolicLinkName.Length >= symbolicLinkName.MaximumLength) {
Trace(TRACE_LEVEL_ERROR, "Error: Buffer overflow when creating COM port name. Size"
" is %d, buffer length is %d", symbolicLinkName.Length, symbolicLinkName.MaximumLength);
status = STATUS_BUFFER_OVERFLOW;
goto Exit;
}
errorNo = wcscpy_s(symbolicLinkName.Buffer,
SYMBOLIC_LINK_NAME_LENGTH,
SYMBOLIC_LINK_NAME_PREFIX);
if (errorNo != 0) {
Trace(TRACE_LEVEL_ERROR,
"Failed to copy %ws to buffer with error %d",
SYMBOLIC_LINK_NAME_PREFIX, errorNo);
status = STATUS_INVALID_PARAMETER;
goto Exit;
}
errorNo = wcscat_s(symbolicLinkName.Buffer,
SYMBOLIC_LINK_NAME_LENGTH,
comPort.Buffer);
if (errorNo != 0) {
Trace(TRACE_LEVEL_ERROR,
"Failed to copy %ws to buffer with error %d",
comPort.Buffer, errorNo);
status = STATUS_INVALID_PARAMETER;
goto Exit;
}
//
// Create symbolic link
//
status = WdfDeviceCreateSymbolicLink(
device,
&symbolicLinkName);
if (!NT_SUCCESS(status)) {
Trace(TRACE_LEVEL_ERROR,
"Error: Cannot create symbolic link %ws", symbolicLinkName.Buffer);
goto Exit;
}
status = DeviceGetPdoName(DeviceContext);
if (!NT_SUCCESS(status)) {
goto Exit;
}
status = DeviceWriteLegacyHardwareKey(
DeviceContext->PdoName,
comPort.Buffer,
DeviceContext->Device);
if (NT_SUCCESS(status)) {
DeviceContext->CreatedLegacyHardwareKey = TRUE;
}
status = QueueCreate(DeviceContext);
if (!NT_SUCCESS(status)) {
goto Exit;
}
Exit:
return status;
}
NTSTATUS
DeviceGetPdoName(
_In_ PDEVICE_CONTEXT DeviceContext
)
{
NTSTATUS status;
WDFDEVICE device = DeviceContext->Device;
WDF_OBJECT_ATTRIBUTES attributes;
WDFMEMORY memory;
WDF_OBJECT_ATTRIBUTES_INIT(&attributes);
attributes.ParentObject = device;
status = WdfDeviceAllocAndQueryProperty(
device,
DevicePropertyPhysicalDeviceObjectName,
NonPagedPoolNx,
&attributes,
&memory);
if (!NT_SUCCESS(status)) {
Trace(TRACE_LEVEL_ERROR,
"Error: Failed to query PDO name");
goto Exit;
}
DeviceContext->PdoName = (PWCHAR) WdfMemoryGetBuffer(memory, NULL);
Trace(TRACE_LEVEL_ERROR,
"PDO Name is %ws", DeviceContext->PdoName);
Exit:
return status;
}
NTSTATUS
DeviceWriteLegacyHardwareKey(
_In_ PWSTR PdoName,
_In_ PWSTR ComPort,
_In_ WDFDEVICE Device
)
{
WDFKEY key = NULL;
NTSTATUS status;
UNICODE_STRING pdoString = {0};
UNICODE_STRING comPort = {0};
DECLARE_CONST_UNICODE_STRING(deviceSubkey, SERIAL_DEVICE_MAP);
RtlInitUnicodeString(&pdoString, PdoName);
RtlInitUnicodeString(&comPort, ComPort);
status = WdfDeviceOpenDevicemapKey(Device,
&deviceSubkey,
KEY_SET_VALUE,
WDF_NO_OBJECT_ATTRIBUTES,
&key);
if (!NT_SUCCESS(status)) {
Trace(TRACE_LEVEL_ERROR,
"Error: Failed to open DEVICEMAP\\SERIALCOMM key 0x%x", status);
goto exit;
}
status = WdfRegistryAssignUnicodeString(key,
&pdoString,
&comPort);
if (!NT_SUCCESS(status)) {
Trace(TRACE_LEVEL_ERROR,
"Error: Failed to write to DEVICEMAP\\SERIALCOMM key 0x%x", status);
goto exit;
}
exit:
if (key != NULL) {
WdfRegistryClose(key);
key = NULL;
}
return status;
}
VOID
EvtDeviceCleanup(
_In_ WDFOBJECT Object
)
{
WDFDEVICE device = (WDFDEVICE) Object;
PDEVICE_CONTEXT deviceContext = GetDeviceContext(device);
NTSTATUS status;
WDFKEY key = NULL;
UNICODE_STRING pdoString = {0};
DECLARE_CONST_UNICODE_STRING(deviceSubkey, SERIAL_DEVICE_MAP);
if (deviceContext->CreatedLegacyHardwareKey == TRUE) {
RtlInitUnicodeString(&pdoString, deviceContext->PdoName);
status = WdfDeviceOpenDevicemapKey(device,
&deviceSubkey,
KEY_SET_VALUE,
WDF_NO_OBJECT_ATTRIBUTES,
&key);
if (!NT_SUCCESS(status)) {
Trace(TRACE_LEVEL_ERROR,
"Error: Failed to open DEVICEMAP\\SERIALCOMM key 0x%x", status);
goto exit;
}
status = WdfRegistryRemoveValue(key,
&pdoString);
if (!NT_SUCCESS(status)) {
Trace(TRACE_LEVEL_ERROR,
"Error: Failed to delete %S key, 0x%x", pdoString.Buffer, status);
goto exit;
}
status = WdfRegistryRemoveKey(key);
if (!NT_SUCCESS(status)) {
Trace(TRACE_LEVEL_ERROR,
"Error: Failed to delete %S, 0x%x", SERIAL_DEVICE_MAP, status);
goto exit;
}
}
exit:
if (key != NULL) {
WdfRegistryClose(key);
key = NULL;
}
return;
}
ULONG
GetBaudRate(
_In_ PDEVICE_CONTEXT DeviceContext
)
{
return DeviceContext->BaudRate;
}
VOID
SetBaudRate(
_In_ PDEVICE_CONTEXT DeviceContext,
_In_ ULONG BaudRate
)
{
DeviceContext->BaudRate = BaudRate;
}
ULONG *
GetModemControlRegisterPtr(
_In_ PDEVICE_CONTEXT DeviceContext
)
{
return &DeviceContext->ModemControlRegister;
}
ULONG *
GetFifoControlRegisterPtr(
_In_ PDEVICE_CONTEXT DeviceContext
)
{
return &DeviceContext->FifoControlRegister;
}
ULONG *
GetLineControlRegisterPtr(
_In_ PDEVICE_CONTEXT DeviceContext
)
{
return &DeviceContext->LineControlRegister;
}
VOID
SetValidDataMask(
_In_ PDEVICE_CONTEXT DeviceContext,
_In_ UCHAR Mask
)
{
DeviceContext->ValidDataMask = Mask;
}
VOID
SetTimeouts(
_In_ PDEVICE_CONTEXT DeviceContext,
_In_ SERIAL_TIMEOUTS Timeouts
)
{
DeviceContext->Timeouts = Timeouts;
}
VOID
GetTimeouts(
_In_ PDEVICE_CONTEXT DeviceContext,
_Out_ SERIAL_TIMEOUTS *Timeouts
)
{
*Timeouts = DeviceContext->Timeouts;
}