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
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
|
/*++
Copyright (c) 1997 Microsoft Corporation
Module Name:
serenum.h
Abstract:
This module contains the common private declarations for the serial port
enumerator.
Environment:
kernel mode only
Notes:
Revision History:
--*/
#ifndef SERENUM_H
#define SERENUM_H
#define MAX_DEVNODE_NAME 256 // Total size of Device ID
#define NAME_SERENUM "SERENUM\\"
#define MAX_DEVNAME (MAX_DEVNODE_NAME + 1 - sizeof(NAME_SERENUM) )
#define SERENUM_QDR_LOCK 0x00000001
#define SERENUM_OPEN_LOCK 0x00000002
#define SERENUM_POWER_LOCK 0x00000004
#define SERENUM_STOP_LOCK 0x00000008
#define SERENUM_EXPOSE_LOCK 0x00000010
//#define SERENUM_COMPATIBLE_IDS L"SerialPort\\SerialDevice\0\0"
//#define SERENUM_COMPATIBLE_IDS_LENGTH 25 // NB wide characters.
#define SERENUM_INSTANCE_IDS L"0000"
#define SERENUM_INSTANCE_IDS_LENGTH 5
//
// Default wait between line state changes in pnp enum protocol
//
#define SERENUM_DEFAULT_WAIT 2000000
//
// Timeout to read data from a serial port (in ms)
//
#define SERENUM_SERIAL_READ_TIME 240
//#define SERENUM_INSTANCE_ID_BASE L"Serenum\\Inst_000"
//#define SERENUM_INSTANCE_ID_BASE_LENGTH 12
//#define SERENUM_INSTANCE_ID_BASE_PORT_INDEX 10
#define SERENUM_PDO_NAME_BASE L"\\Serial\\"
#define SERENUM_POOL_TAG (ULONG)'mneS'
#pragma warning(error:4100) // Unreferenced formal parameter
#pragma warning(error:4705) // Statement has no effect
// disable warnings
#if _MSC_VER >= 1200
#pragma warning(push)
#endif
#pragma warning(disable:4201) // nameless struct/union
#pragma warning(disable:28718)
//
// Debugging Output Levels
//
#define SER_DBG_STARTUP_SHUTDOWN_MASK 0x0000000F
#define SER_DBG_SS_NOISE 0x00000001
#define SER_DBG_SS_TRACE 0x00000002
#define SER_DBG_SS_INFO 0x00000004
#define SER_DBG_SS_ERROR 0x00000008
#define SER_DBG_PNP_MASK 0x000000F0
#define SER_DBG_PNP_NOISE 0x00000010
#define SER_DBG_PNP_TRACE 0x00000020
#define SER_DBG_PNP_INFO 0x00000040
#define SER_DBG_PNP_ERROR 0x00000080
#define SER_DBG_PNP_DUMP_PACKET 0x00000100
#define SER_DEFAULT_DEBUG_OUTPUT_LEVEL 0
#if DBG
#define Serenum_KdPrint(_d_,_l_, _x_) \
if ((_d_)->DebugLevel & (_l_)) { \
DbgPrint ("SerEnum.SYS: "); \
DbgPrint _x_; \
}
//#define Serenum_KdPrint_Cont(_d_,_l_, _x_) \
// if ((_d_)->DebugLevel & (_l_)) { \
// DbgPrint _x_; \
// }
#define Serenum_KdPrint_Def(_l_, _x_) \
if (SER_DEFAULT_DEBUG_OUTPUT_LEVEL & (_l_)) { \
DbgPrint ("SerEnum.SYS: "); \
DbgPrint _x_; \
}
#define TRAP() DbgBreakPoint()
#define DbgRaiseIrql(_x_,_y_) KeRaiseIrql(_x_,_y_)
#define DbgLowerIrql(_x_) KeLowerIrql(_x_)
#else
#define Serenum_KdPrint(_d_, _l_, _x_)
#define Serenum_KdPrint_Cont(_d_, _l_, _x_)
#define Serenum_KdPrint_Def(_l_, _x_)
#define TRAP()
#define DbgRaiseIrql(_x_,_y_)
#define DbgLowerIrql(_x_)
#endif
#if !defined(MIN)
#define MIN(_A_,_B_) (((_A_) < (_B_)) ? (_A_) : (_B_))
#endif
#define POOL_ZERO_DOWN_LEVEL_SUPPORT
//
// A common header for the device extensions of the PDOs and FDO
//
typedef struct _COMMON_DEVICE_DATA
{
PDEVICE_OBJECT Self;
// A backpointer to the device object for which this is the extension
BOOLEAN IsFDO;
// Are we currently in a query power state?
BOOLEAN Removed;
// Has this device been removed? Should we fail any requests?
//
// Since we may hold onto irps for an arbitrarily time
// we need a remove lock so that our device does not get removed
// while an irp is being processed.
IO_REMOVE_LOCK RemoveLock;
ULONG DebugLevel;
SYSTEM_POWER_STATE SystemState;
DEVICE_POWER_STATE DeviceState;
} COMMON_DEVICE_DATA, *PCOMMON_DEVICE_DATA;
//
// The device extension for the PDOs.
// That is the serial ports of which this bus driver enumerates.
// (IE there is a PDO for the 201 serial port).
//
typedef struct _PDO_DEVICE_DATA
{
COMMON_DEVICE_DATA;
PDEVICE_OBJECT ParentFdo;
// A back pointer to the bus
UNICODE_STRING HardwareIDs;
// Either in the form of bus\device
// or *PNPXXXX - meaning root enumerated
UNICODE_STRING CompIDs;
// compatible ids to the hardware id
UNICODE_STRING DeviceIDs;
// Format: bus\device
//
// Text describing device
//
UNICODE_STRING DevDesc;
UNICODE_STRING SerialNo;
UNICODE_STRING PnPRev;
BOOLEAN Started;
BOOLEAN Attached;
// When a device (PDO) is found on a bus and presented as a device relation
// to the PlugPlay system, Attached is set to TRUE, and Removed to FALSE.
// When the bus driver determines that this PDO is no longer valid, because
// the device has gone away, it informs the PlugPlay system of the new
// device relastions, but it does not delete the device object at that time.
// The PDO is deleted only when the PlugPlay system has sent a remove IRP,
// and there is no longer a device on the bus.
//
// If the PlugPlay system sends a remove IRP then the Removed field is set
// to true, and all client (non PlugPlay system) accesses are failed.
// If the device is removed from the bus Attached is set to FALSE.
//
// During a query relations Irp Minor call, only the PDOs that are
// attached to the bus (and all that are attached to the bus) are returned
// (even if they have been removed).
//
// During a remove device Irp Minor call, if and only if, attached is set
// to FALSE, the PDO is deleted.
//
} PDO_DEVICE_DATA, *PPDO_DEVICE_DATA;
//
// The device extension of the bus itself. From whence the PDO's are born.
//
typedef struct _FDO_DEVICE_DATA
{
COMMON_DEVICE_DATA;
UCHAR PdoIndex;
// A number to keep track of the Pdo we're allocating.
// Increment every time we create a new PDO. It's ok that it wraps.
BOOLEAN Started;
// Are we on, have resources, etc?
BOOLEAN NumPDOs;
// The PDOs currently enumerated.
BOOLEAN NewNumPDOs;
BOOLEAN NewPDOForcedRemove;
BOOLEAN PDOForcedRemove;
// Was the last PDO removed by force using the internal ioctl?
// If so, when the next Query Device Relations is called, return only the
// currently enumerated pdos
//
// Our child PDO; we're hoping he will be a doctor someday
//
PDEVICE_OBJECT AttachedPDO;
//
// The new PDO after we've done enumeration
//
PDEVICE_OBJECT NewPDO;
PPDO_DEVICE_DATA PdoData;
PPDO_DEVICE_DATA NewPdoData;
PDEVICE_OBJECT UnderlyingPDO;
PDEVICE_OBJECT TopOfStack;
// the underlying bus PDO and the actual device object to which our
// FDO is attached
// the number of IRPs sent from the bus to the underlying device object
UNICODE_STRING DevClassAssocName;
// The name returned from IoRegisterDeviceClass Association,
// which is used as a handle for IoSetDev... and friends.
SYSTEM_POWER_STATE SystemWake;
DEVICE_POWER_STATE DeviceWake;
KSEMAPHORE CreateSemaphore;
PRKSEMAPHORE PCreateSemaphore;
//
// How many times we should skip enumerating devices
// after our stack is built
//
ULONG SkipEnumerations;
//
// Spinlock to protect enumerations
//
KSPIN_LOCK EnumerationLock;
//
// Flags containing state of enumeration
//
ULONG EnumFlags;
//
// Pointer to protocol thread object
//
PVOID ThreadObj;
//
// Pointer to work item to clean up thread ref
//
PIO_WORKITEM EnumWorkItem;
//
// Status of last thread create or STATUS_UNSUCCESSFUL if just not pending
//
NTSTATUS ProtocolThreadStatus;
//
// Count of running protocol threads
//
LONG ProtocolThreadCount;
} FDO_DEVICE_DATA, *PFDO_DEVICE_DATA;
typedef struct _SERENUM_RELEASE_CONTEXT {
PVOID ThreadObject;
PIO_WORKITEM WorkItem;
} SERENUM_RELEASE_CONTEXT, *PSERENUM_RELEASE_CONTEXT;
#if _MSC_VER >= 1200
#pragma warning(pop)
#endif
#pragma warning(default:4201) // nameless struct/union
#define SERENUM_ENUMFLAG_CLEAN 0x0L
#define SERENUM_ENUMFLAG_PENDING 0x1L
#define SERENUM_ENUMFLAG_DIRTY 0x2L
#define SERENUM_ENUMFLAG_REMOVED 0x4L
//
// Free the buffer associated with a Unicode string
// and re-init it to NULL
//
#define SerenumFreeUnicodeString(PStr) \
{ \
if ((PStr)->Buffer != NULL) { \
ExFreePool((PStr)->Buffer); \
} \
RtlInitUnicodeString((PStr), NULL); \
}
//
// Prototypes
//
_Dispatch_type_(IRP_MJ_CREATE)
_Dispatch_type_(IRP_MJ_CLOSE)
DRIVER_DISPATCH Serenum_CreateClose;
_Dispatch_type_(IRP_MJ_DEVICE_CONTROL)
DRIVER_DISPATCH Serenum_IoCtl;
_Dispatch_type_(IRP_MJ_INTERNAL_DEVICE_CONTROL)
DRIVER_DISPATCH Serenum_InternIoCtl;
DRIVER_UNLOAD Serenum_DriverUnload;
_Dispatch_type_(IRP_MJ_PNP)
DRIVER_DISPATCH Serenum_PnP;
_Dispatch_type_(IRP_MJ_POWER)
DRIVER_DISPATCH Serenum_Power;
DRIVER_ADD_DEVICE Serenum_AddDevice;
NTSTATUS
Serenum_PnPRemove (
PDEVICE_OBJECT Device,
PPDO_DEVICE_DATA PdoData
);
NTSTATUS
Serenum_FDO_PnP (
IN PDEVICE_OBJECT DeviceObject,
IN PIRP Irp,
IN PIO_STACK_LOCATION IrpStack,
IN PFDO_DEVICE_DATA DeviceData
);
NTSTATUS
Serenum_PDO_PnP (
IN PDEVICE_OBJECT DeviceObject,
IN PIRP Irp,
IN PIO_STACK_LOCATION IrpStack,
IN PPDO_DEVICE_DATA DeviceData
);
_Dispatch_type_(IRP_MJ_OTHER)
DRIVER_DISPATCH Serenum_DispatchPassThrough;
NTSTATUS
Serenum_Generic_FireAndRelease_RemoveLock(
IN PDEVICE_OBJECT DeviceObject,
IN PIRP Irp
);
NTSTATUS
Serenum_ReenumerateDevices(
IN PIRP Irp,
IN PFDO_DEVICE_DATA DeviceData,
IN PBOOLEAN PSameDevice
);
IO_COMPLETION_ROUTINE
Serenum_EnumComplete;
IO_COMPLETION_ROUTINE
Serenum_CompletionRoutine;
IO_COMPLETION_ROUTINE
SerenumStartDeviceCompletion;
IO_WORKITEM_ROUTINE_EX
SerenumStartDeviceWorker;
NTSTATUS
Serenum_DispatchPassThroughWithoutAcquire(
IN PDEVICE_OBJECT DeviceObject,
IN PIRP Irp
);
NTSTATUS
Serenum_InitMultiString(PFDO_DEVICE_DATA FdoData, PUNICODE_STRING MultiString,
...);
int
Serenum_GetDevOtherID(
_In_reads_(len) PCHAR input,
int len,
_Out_writes_(MAX_DEVNODE_NAME) LPSTR output
);
NTSTATUS
Serenum_GetDevPnPRev(
PFDO_DEVICE_DATA FdoData,
_In_reads_(len) PCHAR input,
int len,
_Out_writes_(MAX_DEVNODE_NAME) LPSTR output,
_Inout_ int *start
);
void Serenum_GetDevName(
_In_reads_(len) PCHAR input,
int len,
_Out_writes_(MAX_DEVNAME) PCHAR output,
_Inout_ int *start
);
void Serenum_GetDevSerialNo(
_In_reads_(len) PCHAR input,
int len,
_Out_writes_(MAX_DEVNODE_NAME) LPSTR output,
_Inout_ int *start
);
void Serenum_GetDevClass(
_In_reads_(len) PCHAR input,
int len,
_Out_writes_(MAX_DEVNODE_NAME) LPSTR output,
_Inout_ int *start
);
void Serenum_GetDevCompId(
_In_reads_(len) PCHAR input,
int len,
_Out_writes_(MAX_DEVNODE_NAME) LPSTR output,
_Inout_ int *start
);
void Serenum_GetDevDesc(
_In_reads_(len) PCHAR input,
int len,
_Out_writes_(MAX_DEVNODE_NAME) LPSTR output,
_Inout_ int *start
);
NTSTATUS
Serenum_ParseData( PFDO_DEVICE_DATA FdoData,
_In_reads_(BufferLen) PCHAR ReadBuffer, ULONG BufferLen,
PUNICODE_STRING hardwareIDs, PUNICODE_STRING compIDs,
PUNICODE_STRING deviceIDs, PUNICODE_STRING PDeviceDesc,
PUNICODE_STRING serialNo, PUNICODE_STRING pnpRev
);
NTSTATUS
Serenum_ReadSerialPort(
_Out_writes_(Buflen) OUT PCHAR PReadBuffer, IN USHORT Buflen,
IN ULONG Timeout, OUT PUSHORT nActual,
OUT PIO_STATUS_BLOCK IoStatusBlock,
IN const PFDO_DEVICE_DATA FdoData);
NTSTATUS
Serenum_Wait (
IN PKTIMER Timer,
IN LARGE_INTEGER DueTime );
#define Serenum_IoSyncIoctl(Ioctl, Internal, PDevObj, PEvent) \
Serenum_IoSyncIoctlEx((Ioctl), (Internal), (PDevObj), (PEvent), \
NULL, 0, NULL, 0)
NTSTATUS
Serenum_IoSyncIoctlEx(ULONG Ioctl, BOOLEAN Internal, PDEVICE_OBJECT PDevObj,
PKEVENT PEvent, PVOID PInBuffer, ULONG InBufferLen,
PVOID POutBuffer, ULONG OutBufferLen);
NTSTATUS
Serenum_IoSyncReqWithIrp(
PIRP Irp,
UCHAR MajorFunction,
PKEVENT event,
PDEVICE_OBJECT devobj );
NTSTATUS
Serenum_IoSyncReq(
PDEVICE_OBJECT Target,
IN PIRP Irp,
PKEVENT event
);
NTSTATUS
Serenum_CopyUniString (
PUNICODE_STRING source,
PUNICODE_STRING dest);
void
Serenum_FixptToAscii(
int n,
_Out_writes_(MAX_DEVNODE_NAME) LPSTR output);
int
Serenum_HToI(char c);
int
Serenum_SzCopy (
_In_ LPSTR source,
_Out_writes_(len) PCHAR dest,
int len
);
void
Serenum_PDO_EnumMarkMissing(
PFDO_DEVICE_DATA FdoData,
PPDO_DEVICE_DATA PdoData);
int
Serenum_StrLen (
_In_ LPSTR string);
NTSTATUS
Serenum_GetRegistryKeyValue (
_In_ HANDLE Handle,
_In_reads_bytes_(KeyNameStringLength) PWCHAR KeyNameString,
_In_ ULONG KeyNameStringLength,
_Out_writes_bytes_to_(DataLength, *ActualLength) PVOID Data,
_In_ ULONG DataLength,
_Out_ PULONG ActualLength
);
void
Serenum_InitPDO (
_In_ __drv_aliasesMem PDEVICE_OBJECT pdoData,
PFDO_DEVICE_DATA fdoData
);
void
SerenumScanOtherIdForMouse(
_In_reads_(BufLen) PCHAR PBuffer,
_In_ ULONG BufLen,
_Outptr_result_buffer_maybenull_(*PmouseIdLen) PCHAR *PpMouseId,
_Out_ ULONG *PmouseIdLen
);
IO_COMPLETION_ROUTINE
SerenumSyncCompletion;
NTSTATUS
SerenumDoEnumProtocol(
PFDO_DEVICE_DATA PFdoData,
_Outptr_result_buffer_(*PNBytes) PUCHAR *PpBuf,
PUSHORT PNBytes,
PBOOLEAN PDSRMissing);
BOOLEAN
SerenumValidateID(IN PUNICODE_STRING PId);
BOOLEAN
SerenumCheckForLegacyDevice(IN PFDO_DEVICE_DATA PFdoData,
_In_reads_(BufferLen) IN PCHAR PReadBuf,
IN ULONG BufferLen,
IN OUT PUNICODE_STRING PHardwareIDs,
IN OUT PUNICODE_STRING PCompIDs,
IN OUT PUNICODE_STRING PDeviceIDs);
NTSTATUS
SerenumStartProtocolThread(IN PFDO_DEVICE_DATA PFdoData);
VOID
SerenumReleaseThreadReference(IN PDEVICE_OBJECT PDevObj, IN PVOID PContext);
VOID
SerenumWaitForEnumThreadTerminate(IN PFDO_DEVICE_DATA PFdoData);
#endif // ndef SERENUM_H
|