xref: /reactos/drivers/serial/serenum/detect.c (revision c2c66aff)
1*c2c66affSColin Finck /*
2*c2c66affSColin Finck  * COPYRIGHT:       See COPYING in the top level directory
3*c2c66affSColin Finck  * PROJECT:         ReactOS Serial enumerator driver
4*c2c66affSColin Finck  * FILE:            drivers/bus/serenum/detect.c
5*c2c66affSColin Finck  * PURPOSE:         Detection of serial devices
6*c2c66affSColin Finck  *
7*c2c66affSColin Finck  * PROGRAMMERS:     Jason Filby (jasonfilby@yahoo.com)
8*c2c66affSColin Finck  *                  Filip Navara (xnavara@volny.cz)
9*c2c66affSColin Finck  *                  Herv� Poussineau (hpoussin@reactos.org)
10*c2c66affSColin Finck  */
11*c2c66affSColin Finck 
12*c2c66affSColin Finck #include "serenum.h"
13*c2c66affSColin Finck 
14*c2c66affSColin Finck #include <debug.h>
15*c2c66affSColin Finck 
16*c2c66affSColin Finck static NTSTATUS
DeviceIoControl(IN PDEVICE_OBJECT DeviceObject,IN ULONG CtlCode,IN PVOID InputBuffer OPTIONAL,IN ULONG_PTR InputBufferSize,IN OUT PVOID OutputBuffer OPTIONAL,IN OUT PULONG_PTR OutputBufferSize)17*c2c66affSColin Finck DeviceIoControl(
18*c2c66affSColin Finck 	IN PDEVICE_OBJECT DeviceObject,
19*c2c66affSColin Finck 	IN ULONG CtlCode,
20*c2c66affSColin Finck 	IN PVOID InputBuffer OPTIONAL,
21*c2c66affSColin Finck 	IN ULONG_PTR InputBufferSize,
22*c2c66affSColin Finck 	IN OUT PVOID OutputBuffer OPTIONAL,
23*c2c66affSColin Finck 	IN OUT PULONG_PTR OutputBufferSize)
24*c2c66affSColin Finck {
25*c2c66affSColin Finck 	KEVENT Event;
26*c2c66affSColin Finck 	PIRP Irp;
27*c2c66affSColin Finck 	IO_STATUS_BLOCK IoStatus;
28*c2c66affSColin Finck 	NTSTATUS Status;
29*c2c66affSColin Finck 
30*c2c66affSColin Finck 	KeInitializeEvent (&Event, NotificationEvent, FALSE);
31*c2c66affSColin Finck 
32*c2c66affSColin Finck 	Irp = IoBuildDeviceIoControlRequest(CtlCode,
33*c2c66affSColin Finck 		DeviceObject,
34*c2c66affSColin Finck 		InputBuffer,
35*c2c66affSColin Finck 		InputBufferSize,
36*c2c66affSColin Finck 		OutputBuffer,
37*c2c66affSColin Finck 		(OutputBufferSize) ? *OutputBufferSize : 0,
38*c2c66affSColin Finck 		FALSE,
39*c2c66affSColin Finck 		&Event,
40*c2c66affSColin Finck 		&IoStatus);
41*c2c66affSColin Finck 	if (Irp == NULL)
42*c2c66affSColin Finck 	{
43*c2c66affSColin Finck 		WARN_(SERENUM, "IoBuildDeviceIoControlRequest() failed\n");
44*c2c66affSColin Finck 		return STATUS_INSUFFICIENT_RESOURCES;
45*c2c66affSColin Finck 	}
46*c2c66affSColin Finck 
47*c2c66affSColin Finck 	Status = IoCallDriver(DeviceObject, Irp);
48*c2c66affSColin Finck 
49*c2c66affSColin Finck 	if (Status == STATUS_PENDING)
50*c2c66affSColin Finck 	{
51*c2c66affSColin Finck 		INFO_(SERENUM, "Operation pending\n");
52*c2c66affSColin Finck 		KeWaitForSingleObject(&Event, Suspended, KernelMode, FALSE, NULL);
53*c2c66affSColin Finck 		Status = IoStatus.Status;
54*c2c66affSColin Finck 	}
55*c2c66affSColin Finck 
56*c2c66affSColin Finck 	if (OutputBufferSize)
57*c2c66affSColin Finck 	{
58*c2c66affSColin Finck 		*OutputBufferSize = IoStatus.Information;
59*c2c66affSColin Finck 	}
60*c2c66affSColin Finck 
61*c2c66affSColin Finck 	return Status;
62*c2c66affSColin Finck }
63*c2c66affSColin Finck 
64*c2c66affSColin Finck static NTSTATUS
ReadBytes(IN PDEVICE_OBJECT LowerDevice,OUT PUCHAR Buffer,IN ULONG BufferSize,OUT PULONG_PTR FilledBytes)65*c2c66affSColin Finck ReadBytes(
66*c2c66affSColin Finck 	IN PDEVICE_OBJECT LowerDevice,
67*c2c66affSColin Finck 	OUT PUCHAR Buffer,
68*c2c66affSColin Finck 	IN ULONG BufferSize,
69*c2c66affSColin Finck 	OUT PULONG_PTR FilledBytes)
70*c2c66affSColin Finck {
71*c2c66affSColin Finck 	PIRP Irp;
72*c2c66affSColin Finck 	IO_STATUS_BLOCK ioStatus;
73*c2c66affSColin Finck 	KEVENT event;
74*c2c66affSColin Finck 	LARGE_INTEGER zero;
75*c2c66affSColin Finck 	NTSTATUS Status;
76*c2c66affSColin Finck 
77*c2c66affSColin Finck 	KeInitializeEvent(&event, NotificationEvent, FALSE);
78*c2c66affSColin Finck 	zero.QuadPart = 0;
79*c2c66affSColin Finck 	Irp = IoBuildSynchronousFsdRequest(
80*c2c66affSColin Finck 		IRP_MJ_READ,
81*c2c66affSColin Finck 		LowerDevice,
82*c2c66affSColin Finck 		Buffer, BufferSize,
83*c2c66affSColin Finck 		&zero,
84*c2c66affSColin Finck 		&event,
85*c2c66affSColin Finck 		&ioStatus);
86*c2c66affSColin Finck 	if (!Irp)
87*c2c66affSColin Finck 		return FALSE;
88*c2c66affSColin Finck 
89*c2c66affSColin Finck 	Status = IoCallDriver(LowerDevice, Irp);
90*c2c66affSColin Finck 	if (Status == STATUS_PENDING)
91*c2c66affSColin Finck 	{
92*c2c66affSColin Finck 		KeWaitForSingleObject(&event, Suspended, KernelMode, FALSE, NULL);
93*c2c66affSColin Finck 		Status = ioStatus.Status;
94*c2c66affSColin Finck 	}
95*c2c66affSColin Finck 	INFO_(SERENUM, "Bytes received: %lu/%lu\n",
96*c2c66affSColin Finck 		ioStatus.Information, BufferSize);
97*c2c66affSColin Finck 	*FilledBytes = ioStatus.Information;
98*c2c66affSColin Finck 	return Status;
99*c2c66affSColin Finck }
100*c2c66affSColin Finck 
101*c2c66affSColin Finck static NTSTATUS
ReportDetectedDevice(IN PDEVICE_OBJECT DeviceObject,IN PUNICODE_STRING DeviceDescription,IN PUNICODE_STRING DeviceId,IN PUNICODE_STRING InstanceId,IN PUNICODE_STRING HardwareIds,IN PUNICODE_STRING CompatibleIds)102*c2c66affSColin Finck ReportDetectedDevice(
103*c2c66affSColin Finck 	IN PDEVICE_OBJECT DeviceObject,
104*c2c66affSColin Finck 	IN PUNICODE_STRING DeviceDescription,
105*c2c66affSColin Finck 	IN PUNICODE_STRING DeviceId,
106*c2c66affSColin Finck 	IN PUNICODE_STRING InstanceId,
107*c2c66affSColin Finck 	IN PUNICODE_STRING HardwareIds,
108*c2c66affSColin Finck 	IN PUNICODE_STRING CompatibleIds)
109*c2c66affSColin Finck {
110*c2c66affSColin Finck 	PDEVICE_OBJECT Pdo = NULL;
111*c2c66affSColin Finck 	PPDO_DEVICE_EXTENSION PdoDeviceExtension = NULL;
112*c2c66affSColin Finck 	PFDO_DEVICE_EXTENSION FdoDeviceExtension;
113*c2c66affSColin Finck 	NTSTATUS Status;
114*c2c66affSColin Finck 
115*c2c66affSColin Finck 	TRACE_(SERENUM, "ReportDetectedDevice() called with %wZ (%wZ) detected\n", DeviceId, DeviceDescription);
116*c2c66affSColin Finck 
117*c2c66affSColin Finck 	Status = IoCreateDevice(
118*c2c66affSColin Finck 		DeviceObject->DriverObject,
119*c2c66affSColin Finck 		sizeof(PDO_DEVICE_EXTENSION),
120*c2c66affSColin Finck 		NULL,
121*c2c66affSColin Finck 		FILE_DEVICE_CONTROLLER,
122*c2c66affSColin Finck 		FILE_AUTOGENERATED_DEVICE_NAME,
123*c2c66affSColin Finck 		FALSE,
124*c2c66affSColin Finck 		&Pdo);
125*c2c66affSColin Finck 	if (!NT_SUCCESS(Status)) goto ByeBye;
126*c2c66affSColin Finck 
127*c2c66affSColin Finck 	Pdo->Flags |= DO_BUS_ENUMERATED_DEVICE;
128*c2c66affSColin Finck 	Pdo->Flags |= DO_POWER_PAGABLE;
129*c2c66affSColin Finck 	PdoDeviceExtension = (PPDO_DEVICE_EXTENSION)Pdo->DeviceExtension;
130*c2c66affSColin Finck 	FdoDeviceExtension = (PFDO_DEVICE_EXTENSION)DeviceObject->DeviceExtension;
131*c2c66affSColin Finck 	RtlZeroMemory(PdoDeviceExtension, sizeof(PDO_DEVICE_EXTENSION));
132*c2c66affSColin Finck 	PdoDeviceExtension->Common.IsFDO = FALSE;
133*c2c66affSColin Finck 	Status = DuplicateUnicodeString(RTL_DUPLICATE_UNICODE_STRING_NULL_TERMINATE, DeviceDescription, &PdoDeviceExtension->DeviceDescription);
134*c2c66affSColin Finck 	if (!NT_SUCCESS(Status)) goto ByeBye;
135*c2c66affSColin Finck 	Status = DuplicateUnicodeString(RTL_DUPLICATE_UNICODE_STRING_NULL_TERMINATE, DeviceId, &PdoDeviceExtension->DeviceId);
136*c2c66affSColin Finck 	if (!NT_SUCCESS(Status)) goto ByeBye;
137*c2c66affSColin Finck 	Status = DuplicateUnicodeString(RTL_DUPLICATE_UNICODE_STRING_NULL_TERMINATE, InstanceId, &PdoDeviceExtension->InstanceId);
138*c2c66affSColin Finck 	if (!NT_SUCCESS(Status)) goto ByeBye;
139*c2c66affSColin Finck 	Status = DuplicateUnicodeString(RTL_DUPLICATE_UNICODE_STRING_NULL_TERMINATE, HardwareIds, &PdoDeviceExtension->HardwareIds);
140*c2c66affSColin Finck 	if (!NT_SUCCESS(Status)) goto ByeBye;
141*c2c66affSColin Finck 	Status = DuplicateUnicodeString(RTL_DUPLICATE_UNICODE_STRING_NULL_TERMINATE, CompatibleIds, &PdoDeviceExtension->CompatibleIds);
142*c2c66affSColin Finck 	if (!NT_SUCCESS(Status)) goto ByeBye;
143*c2c66affSColin Finck 
144*c2c66affSColin Finck 	/* Device attached to serial port (Pdo) may delegate work to
145*c2c66affSColin Finck 	 * serial port stack (Fdo = DeviceObject variable) */
146*c2c66affSColin Finck 	Pdo->StackSize = DeviceObject->StackSize + 1;
147*c2c66affSColin Finck 
148*c2c66affSColin Finck 	FdoDeviceExtension->AttachedPdo = Pdo;
149*c2c66affSColin Finck 	PdoDeviceExtension->AttachedFdo = DeviceObject;
150*c2c66affSColin Finck 
151*c2c66affSColin Finck 	Pdo->Flags |= DO_BUFFERED_IO;
152*c2c66affSColin Finck 	Pdo->Flags &= ~DO_DEVICE_INITIALIZING;
153*c2c66affSColin Finck 
154*c2c66affSColin Finck 	return STATUS_SUCCESS;
155*c2c66affSColin Finck 
156*c2c66affSColin Finck ByeBye:
157*c2c66affSColin Finck 	if (Pdo)
158*c2c66affSColin Finck 	{
159*c2c66affSColin Finck 		ASSERT(PdoDeviceExtension);
160*c2c66affSColin Finck 		if (PdoDeviceExtension->DeviceDescription.Buffer)
161*c2c66affSColin Finck 			RtlFreeUnicodeString(&PdoDeviceExtension->DeviceDescription);
162*c2c66affSColin Finck 		if (PdoDeviceExtension->DeviceId.Buffer)
163*c2c66affSColin Finck 			RtlFreeUnicodeString(&PdoDeviceExtension->DeviceId);
164*c2c66affSColin Finck 		if (PdoDeviceExtension->InstanceId.Buffer)
165*c2c66affSColin Finck 			RtlFreeUnicodeString(&PdoDeviceExtension->InstanceId);
166*c2c66affSColin Finck 		if (PdoDeviceExtension->HardwareIds.Buffer)
167*c2c66affSColin Finck 			RtlFreeUnicodeString(&PdoDeviceExtension->HardwareIds);
168*c2c66affSColin Finck 		if (PdoDeviceExtension->CompatibleIds.Buffer)
169*c2c66affSColin Finck 			RtlFreeUnicodeString(&PdoDeviceExtension->CompatibleIds);
170*c2c66affSColin Finck 		IoDeleteDevice(Pdo);
171*c2c66affSColin Finck 	}
172*c2c66affSColin Finck 	return Status;
173*c2c66affSColin Finck }
174*c2c66affSColin Finck 
175*c2c66affSColin Finck static BOOLEAN
IsValidPnpIdString(IN PUCHAR Buffer,IN ULONG BufferLength)176*c2c66affSColin Finck IsValidPnpIdString(
177*c2c66affSColin Finck 	IN PUCHAR Buffer,
178*c2c66affSColin Finck 	IN ULONG BufferLength)
179*c2c66affSColin Finck {
180*c2c66affSColin Finck 	ANSI_STRING String;
181*c2c66affSColin Finck 
182*c2c66affSColin Finck 	/* FIXME: IsValidPnpIdString not implemented */
183*c2c66affSColin Finck 	UNIMPLEMENTED;
184*c2c66affSColin Finck 	String.Length = String.MaximumLength = BufferLength;
185*c2c66affSColin Finck 	String.Buffer = (PCHAR)Buffer;
186*c2c66affSColin Finck 	ERR_(SERENUM, "Buffer %Z\n", &String);
187*c2c66affSColin Finck 	return TRUE;
188*c2c66affSColin Finck }
189*c2c66affSColin Finck 
190*c2c66affSColin Finck static NTSTATUS
ReportDetectedPnpDevice(IN PUCHAR Buffer,IN ULONG BufferLength)191*c2c66affSColin Finck ReportDetectedPnpDevice(
192*c2c66affSColin Finck 	IN PUCHAR Buffer,
193*c2c66affSColin Finck 	IN ULONG BufferLength)
194*c2c66affSColin Finck {
195*c2c66affSColin Finck 	ANSI_STRING String;
196*c2c66affSColin Finck 
197*c2c66affSColin Finck 	/* FIXME: ReportDetectedPnpDevice not implemented */
198*c2c66affSColin Finck 	UNIMPLEMENTED;
199*c2c66affSColin Finck 	String.Length = String.MaximumLength = BufferLength;
200*c2c66affSColin Finck 	String.Buffer = (PCHAR)Buffer;
201*c2c66affSColin Finck 	ERR_(SERENUM, "Buffer %Z\n", &String);
202*c2c66affSColin Finck 	/* Call ReportDetectedDevice */
203*c2c66affSColin Finck 	return STATUS_SUCCESS;
204*c2c66affSColin Finck }
205*c2c66affSColin Finck 
206*c2c66affSColin Finck #define BEGIN_ID '('
207*c2c66affSColin Finck #define END_ID ')'
208*c2c66affSColin Finck 
209*c2c66affSColin Finck static NTSTATUS
Wait(IN ULONG milliseconds)210*c2c66affSColin Finck Wait(
211*c2c66affSColin Finck 	IN ULONG milliseconds)
212*c2c66affSColin Finck {
213*c2c66affSColin Finck 	KTIMER Timer;
214*c2c66affSColin Finck 	LARGE_INTEGER DueTime;
215*c2c66affSColin Finck 
216*c2c66affSColin Finck 	DueTime.QuadPart = milliseconds * -10;
217*c2c66affSColin Finck 	KeInitializeTimer(&Timer);
218*c2c66affSColin Finck 	KeSetTimer(&Timer, DueTime, NULL);
219*c2c66affSColin Finck 	return KeWaitForSingleObject(&Timer, Executive, KernelMode, FALSE, NULL);
220*c2c66affSColin Finck }
221*c2c66affSColin Finck 
222*c2c66affSColin Finck NTSTATUS
SerenumDetectPnpDevice(IN PDEVICE_OBJECT DeviceObject,IN PDEVICE_OBJECT LowerDevice)223*c2c66affSColin Finck SerenumDetectPnpDevice(
224*c2c66affSColin Finck 	IN PDEVICE_OBJECT DeviceObject,
225*c2c66affSColin Finck 	IN PDEVICE_OBJECT LowerDevice)
226*c2c66affSColin Finck {
227*c2c66affSColin Finck 	HANDLE Handle = NULL;
228*c2c66affSColin Finck 	UCHAR Buffer[256];
229*c2c66affSColin Finck 	ULONG BaudRate;
230*c2c66affSColin Finck 	ULONG_PTR TotalBytesReceived = 0;
231*c2c66affSColin Finck 	ULONG_PTR Size;
232*c2c66affSColin Finck 	ULONG Msr, Purge;
233*c2c66affSColin Finck 	ULONG i;
234*c2c66affSColin Finck 	BOOLEAN BufferContainsBeginId = FALSE;
235*c2c66affSColin Finck 	BOOLEAN BufferContainsEndId = FALSE;
236*c2c66affSColin Finck 	SERIAL_LINE_CONTROL Lcr;
237*c2c66affSColin Finck 	SERIAL_TIMEOUTS Timeouts;
238*c2c66affSColin Finck 	SERIALPERF_STATS PerfStats;
239*c2c66affSColin Finck 	NTSTATUS Status;
240*c2c66affSColin Finck 
241*c2c66affSColin Finck 	/* Open port */
242*c2c66affSColin Finck 	Status = ObOpenObjectByPointer(
243*c2c66affSColin Finck 		LowerDevice,
244*c2c66affSColin Finck 		OBJ_KERNEL_HANDLE,
245*c2c66affSColin Finck 		NULL,
246*c2c66affSColin Finck 		0,
247*c2c66affSColin Finck 		NULL,
248*c2c66affSColin Finck 		KernelMode,
249*c2c66affSColin Finck 		&Handle);
250*c2c66affSColin Finck 	if (!NT_SUCCESS(Status)) goto ByeBye;
251*c2c66affSColin Finck 
252*c2c66affSColin Finck 	/* 1. COM port initialization, check for device enumerate */
253*c2c66affSColin Finck 	TRACE_(SERENUM, "COM port initialization, check for device enumerate\n");
254*c2c66affSColin Finck 	Status = DeviceIoControl(LowerDevice, IOCTL_SERIAL_CLR_DTR,
255*c2c66affSColin Finck 		NULL, 0, NULL, NULL);
256*c2c66affSColin Finck 	if (!NT_SUCCESS(Status)) goto ByeBye;
257*c2c66affSColin Finck 	Status = DeviceIoControl(LowerDevice, IOCTL_SERIAL_CLR_RTS,
258*c2c66affSColin Finck 		NULL, 0, NULL, NULL);
259*c2c66affSColin Finck 	if (!NT_SUCCESS(Status)) goto ByeBye;
260*c2c66affSColin Finck 	Wait(200);
261*c2c66affSColin Finck 	Size = sizeof(Msr);
262*c2c66affSColin Finck 	Status = DeviceIoControl(LowerDevice, IOCTL_SERIAL_GET_MODEMSTATUS,
263*c2c66affSColin Finck 		NULL, 0, &Msr, &Size);
264*c2c66affSColin Finck 	if (!NT_SUCCESS(Status)) goto ByeBye;
265*c2c66affSColin Finck 	if ((Msr & SERIAL_DSR_STATE) == 0) goto DisconnectIdle;
266*c2c66affSColin Finck 
267*c2c66affSColin Finck 	/* 2. COM port setup, 1st phase */
268*c2c66affSColin Finck 	TRACE_(SERENUM, "COM port setup, 1st phase\n");
269*c2c66affSColin Finck 	BaudRate = 1200;
270*c2c66affSColin Finck 	Status = DeviceIoControl(LowerDevice, IOCTL_SERIAL_SET_BAUD_RATE,
271*c2c66affSColin Finck 		&BaudRate, sizeof(BaudRate), NULL, 0);
272*c2c66affSColin Finck 	if (!NT_SUCCESS(Status)) goto ByeBye;
273*c2c66affSColin Finck 	Lcr.WordLength = 7;
274*c2c66affSColin Finck 	Lcr.Parity = NO_PARITY;
275*c2c66affSColin Finck 	Lcr.StopBits = STOP_BIT_1;
276*c2c66affSColin Finck 	Status = DeviceIoControl(LowerDevice, IOCTL_SERIAL_SET_LINE_CONTROL,
277*c2c66affSColin Finck 		&Lcr, sizeof(Lcr), NULL, NULL);
278*c2c66affSColin Finck 	if (!NT_SUCCESS(Status)) goto ByeBye;
279*c2c66affSColin Finck 	Status = DeviceIoControl(LowerDevice, IOCTL_SERIAL_CLR_DTR,
280*c2c66affSColin Finck 		NULL, 0, NULL, NULL);
281*c2c66affSColin Finck 	if (!NT_SUCCESS(Status)) goto ByeBye;
282*c2c66affSColin Finck 	Status = DeviceIoControl(LowerDevice, IOCTL_SERIAL_CLR_RTS,
283*c2c66affSColin Finck 		NULL, 0, NULL, NULL);
284*c2c66affSColin Finck 	if (!NT_SUCCESS(Status)) goto ByeBye;
285*c2c66affSColin Finck 	Wait(200);
286*c2c66affSColin Finck 	Status = DeviceIoControl(LowerDevice, IOCTL_SERIAL_SET_DTR,
287*c2c66affSColin Finck 		NULL, 0, NULL, NULL);
288*c2c66affSColin Finck 	if (!NT_SUCCESS(Status)) goto ByeBye;
289*c2c66affSColin Finck 	Wait(200);
290*c2c66affSColin Finck 
291*c2c66affSColin Finck 	/* 3. Wait for response, 1st phase */
292*c2c66affSColin Finck 	TRACE_(SERENUM, "Wait for response, 1st phase\n");
293*c2c66affSColin Finck 	Status = DeviceIoControl(LowerDevice, IOCTL_SERIAL_SET_RTS,
294*c2c66affSColin Finck 		NULL, 0, NULL, NULL);
295*c2c66affSColin Finck 	if (!NT_SUCCESS(Status)) goto ByeBye;
296*c2c66affSColin Finck 	Timeouts.ReadIntervalTimeout = 0;
297*c2c66affSColin Finck 	Timeouts.ReadTotalTimeoutMultiplier = 0;
298*c2c66affSColin Finck 	Timeouts.ReadTotalTimeoutConstant = 200;
299*c2c66affSColin Finck 	Timeouts.WriteTotalTimeoutMultiplier = Timeouts.WriteTotalTimeoutConstant = 0;
300*c2c66affSColin Finck 	Status = DeviceIoControl(LowerDevice, IOCTL_SERIAL_SET_TIMEOUTS,
301*c2c66affSColin Finck 		&Timeouts, sizeof(Timeouts), NULL, NULL);
302*c2c66affSColin Finck 	if (!NT_SUCCESS(Status)) goto ByeBye;
303*c2c66affSColin Finck 	Status = ReadBytes(LowerDevice, Buffer, sizeof(Buffer), &Size);
304*c2c66affSColin Finck 	if (!NT_SUCCESS(Status)) goto ByeBye;
305*c2c66affSColin Finck 	if (Size != 0) goto CollectPnpComDeviceId;
306*c2c66affSColin Finck 
307*c2c66affSColin Finck 	/* 4. COM port setup, 2nd phase */
308*c2c66affSColin Finck 	TRACE_(SERENUM, "COM port setup, 2nd phase\n");
309*c2c66affSColin Finck 	Status = DeviceIoControl(LowerDevice, IOCTL_SERIAL_CLR_DTR,
310*c2c66affSColin Finck 		NULL, 0, NULL, NULL);
311*c2c66affSColin Finck 	if (!NT_SUCCESS(Status)) goto ByeBye;
312*c2c66affSColin Finck 	Status = DeviceIoControl(LowerDevice, IOCTL_SERIAL_CLR_RTS,
313*c2c66affSColin Finck 		NULL, 0, NULL, NULL);
314*c2c66affSColin Finck 	if (!NT_SUCCESS(Status)) goto ByeBye;
315*c2c66affSColin Finck 	Purge = SERIAL_PURGE_RXABORT | SERIAL_PURGE_RXCLEAR;
316*c2c66affSColin Finck 	Status = DeviceIoControl(LowerDevice, IOCTL_SERIAL_PURGE,
317*c2c66affSColin Finck 		&Purge, sizeof(ULONG), NULL, NULL);
318*c2c66affSColin Finck 	if (!NT_SUCCESS(Status)) goto ByeBye;
319*c2c66affSColin Finck 	Wait(200);
320*c2c66affSColin Finck 
321*c2c66affSColin Finck 	/* 5. Wait for response, 2nd phase */
322*c2c66affSColin Finck 	TRACE_(SERENUM, "Wait for response, 2nd phase\n");
323*c2c66affSColin Finck 	Status = DeviceIoControl(LowerDevice, IOCTL_SERIAL_SET_DTR,
324*c2c66affSColin Finck 		NULL, 0, NULL, NULL);
325*c2c66affSColin Finck 	if (!NT_SUCCESS(Status)) goto ByeBye;
326*c2c66affSColin Finck 	Status = DeviceIoControl(LowerDevice, IOCTL_SERIAL_SET_RTS,
327*c2c66affSColin Finck 		NULL, 0, NULL, NULL);
328*c2c66affSColin Finck 	if (!NT_SUCCESS(Status)) goto ByeBye;
329*c2c66affSColin Finck 	Status = ReadBytes(LowerDevice, Buffer, 1, &TotalBytesReceived);
330*c2c66affSColin Finck 	if (!NT_SUCCESS(Status)) goto ByeBye;
331*c2c66affSColin Finck 	if (TotalBytesReceived != 0) goto CollectPnpComDeviceId;
332*c2c66affSColin Finck 	Size = sizeof(Msr);
333*c2c66affSColin Finck 	Status = DeviceIoControl(LowerDevice, IOCTL_SERIAL_GET_MODEMSTATUS,
334*c2c66affSColin Finck 		NULL, 0, &Msr, &Size);
335*c2c66affSColin Finck 	if (!NT_SUCCESS(Status)) goto ByeBye;
336*c2c66affSColin Finck 	if ((Msr & SERIAL_DSR_STATE) == 0) goto VerifyDisconnect; else goto ConnectIdle;
337*c2c66affSColin Finck 
338*c2c66affSColin Finck 	/* 6. Collect PnP COM device ID */
339*c2c66affSColin Finck CollectPnpComDeviceId:
340*c2c66affSColin Finck 	TRACE_(SERENUM, "Collect PnP COM device ID\n");
341*c2c66affSColin Finck 	Timeouts.ReadIntervalTimeout = 200;
342*c2c66affSColin Finck 	Timeouts.ReadTotalTimeoutMultiplier = 0;
343*c2c66affSColin Finck 	Timeouts.ReadTotalTimeoutConstant = 2200;
344*c2c66affSColin Finck 	Status = DeviceIoControl(LowerDevice, IOCTL_SERIAL_SET_TIMEOUTS,
345*c2c66affSColin Finck 		&Timeouts, sizeof(Timeouts), NULL, NULL);
346*c2c66affSColin Finck 	if (!NT_SUCCESS(Status)) goto ByeBye;
347*c2c66affSColin Finck 	Status = ReadBytes(LowerDevice, &Buffer[TotalBytesReceived], sizeof(Buffer) - TotalBytesReceived, &Size);
348*c2c66affSColin Finck 	if (!NT_SUCCESS(Status)) goto ByeBye;
349*c2c66affSColin Finck 	TotalBytesReceived += Size;
350*c2c66affSColin Finck 	Size = sizeof(PerfStats);
351*c2c66affSColin Finck 	Status = DeviceIoControl(LowerDevice, IOCTL_SERIAL_GET_STATS,
352*c2c66affSColin Finck 		NULL, 0, &PerfStats, &Size);
353*c2c66affSColin Finck 	if (!NT_SUCCESS(Status)) goto ByeBye;
354*c2c66affSColin Finck 	if (PerfStats.FrameErrorCount + PerfStats.ParityErrorCount != 0) goto ConnectIdle;
355*c2c66affSColin Finck 	for (i = 0; i < TotalBytesReceived; i++)
356*c2c66affSColin Finck 	{
357*c2c66affSColin Finck 		if (Buffer[i] == BEGIN_ID) BufferContainsBeginId = TRUE;
358*c2c66affSColin Finck 		if (Buffer[i] == END_ID) BufferContainsEndId = TRUE;
359*c2c66affSColin Finck 	}
360*c2c66affSColin Finck 	if (TotalBytesReceived == 1 || BufferContainsEndId)
361*c2c66affSColin Finck 	{
362*c2c66affSColin Finck 		if (IsValidPnpIdString(Buffer, TotalBytesReceived))
363*c2c66affSColin Finck 		{
364*c2c66affSColin Finck 			Status = ReportDetectedPnpDevice(Buffer, TotalBytesReceived);
365*c2c66affSColin Finck 			goto ByeBye;
366*c2c66affSColin Finck 		}
367*c2c66affSColin Finck 		goto ConnectIdle;
368*c2c66affSColin Finck 	}
369*c2c66affSColin Finck 	if (!BufferContainsBeginId) goto ConnectIdle;
370*c2c66affSColin Finck 	if (!BufferContainsEndId) goto ConnectIdle;
371*c2c66affSColin Finck 	Size = sizeof(Msr);
372*c2c66affSColin Finck 	Status = DeviceIoControl(LowerDevice, IOCTL_SERIAL_GET_MODEMSTATUS,
373*c2c66affSColin Finck 		NULL, 0, &Msr, &Size);
374*c2c66affSColin Finck 	if (!NT_SUCCESS(Status)) goto ByeBye;
375*c2c66affSColin Finck 	if ((Msr & SERIAL_DSR_STATE) == 0) goto VerifyDisconnect;
376*c2c66affSColin Finck 
377*c2c66affSColin Finck 	/* 7. Verify disconnect */
378*c2c66affSColin Finck VerifyDisconnect:
379*c2c66affSColin Finck 	TRACE_(SERENUM, "Verify disconnect\n");
380*c2c66affSColin Finck 	Status = DeviceIoControl(LowerDevice, IOCTL_SERIAL_SET_DTR,
381*c2c66affSColin Finck 		NULL, 0, NULL, NULL);
382*c2c66affSColin Finck 	if (!NT_SUCCESS(Status)) goto ByeBye;
383*c2c66affSColin Finck 	Status = DeviceIoControl(LowerDevice, IOCTL_SERIAL_CLR_RTS,
384*c2c66affSColin Finck 		NULL, 0, NULL, NULL);
385*c2c66affSColin Finck 	if (!NT_SUCCESS(Status)) goto ByeBye;
386*c2c66affSColin Finck 	Wait(5000);
387*c2c66affSColin Finck 	goto DisconnectIdle;
388*c2c66affSColin Finck 
389*c2c66affSColin Finck 	/* 8. Connect idle */
390*c2c66affSColin Finck ConnectIdle:
391*c2c66affSColin Finck 	TRACE_(SERENUM, "Connect idle\n");
392*c2c66affSColin Finck 	Status = DeviceIoControl(LowerDevice, IOCTL_SERIAL_SET_DTR,
393*c2c66affSColin Finck 		NULL, 0, NULL, NULL);
394*c2c66affSColin Finck 	if (!NT_SUCCESS(Status)) goto ByeBye;
395*c2c66affSColin Finck 	Status = DeviceIoControl(LowerDevice, IOCTL_SERIAL_CLR_RTS,
396*c2c66affSColin Finck 		NULL, 0, NULL, NULL);
397*c2c66affSColin Finck 	if (!NT_SUCCESS(Status)) goto ByeBye;
398*c2c66affSColin Finck 	BaudRate = 300;
399*c2c66affSColin Finck 	Status = DeviceIoControl(LowerDevice, IOCTL_SERIAL_SET_BAUD_RATE,
400*c2c66affSColin Finck 		&BaudRate, sizeof(BaudRate), NULL, NULL);
401*c2c66affSColin Finck 	if (!NT_SUCCESS(Status)) goto ByeBye;
402*c2c66affSColin Finck 	Lcr.WordLength = 7;
403*c2c66affSColin Finck 	Lcr.Parity = NO_PARITY;
404*c2c66affSColin Finck 	Lcr.StopBits = STOP_BIT_1;
405*c2c66affSColin Finck 	Status = DeviceIoControl(LowerDevice, IOCTL_SERIAL_SET_LINE_CONTROL,
406*c2c66affSColin Finck 		&Lcr, sizeof(Lcr), NULL, NULL);
407*c2c66affSColin Finck 	if (!NT_SUCCESS(Status)) goto ByeBye;
408*c2c66affSColin Finck 	if (TotalBytesReceived == 0)
409*c2c66affSColin Finck 		Status = STATUS_DEVICE_NOT_CONNECTED;
410*c2c66affSColin Finck 	else
411*c2c66affSColin Finck 		Status = STATUS_SUCCESS;
412*c2c66affSColin Finck 	goto ByeBye;
413*c2c66affSColin Finck 
414*c2c66affSColin Finck 	/* 9. Disconnect idle */
415*c2c66affSColin Finck DisconnectIdle:
416*c2c66affSColin Finck 	TRACE_(SERENUM, "Disconnect idle\n");
417*c2c66affSColin Finck 	/* FIXME: report to OS device removal, if it was present */
418*c2c66affSColin Finck 	Status = DeviceIoControl(LowerDevice, IOCTL_SERIAL_SET_DTR,
419*c2c66affSColin Finck 		NULL, 0, NULL, NULL);
420*c2c66affSColin Finck 	if (!NT_SUCCESS(Status)) goto ByeBye;
421*c2c66affSColin Finck 	Status = DeviceIoControl(LowerDevice, IOCTL_SERIAL_CLR_RTS,
422*c2c66affSColin Finck 		NULL, 0, NULL, NULL);
423*c2c66affSColin Finck 	if (!NT_SUCCESS(Status)) goto ByeBye;
424*c2c66affSColin Finck 	BaudRate = 300;
425*c2c66affSColin Finck 	Status = DeviceIoControl(LowerDevice, IOCTL_SERIAL_SET_BAUD_RATE,
426*c2c66affSColin Finck 		&BaudRate, sizeof(BaudRate), NULL, NULL);
427*c2c66affSColin Finck 	if (!NT_SUCCESS(Status)) goto ByeBye;
428*c2c66affSColin Finck 	Lcr.WordLength = 7;
429*c2c66affSColin Finck 	Lcr.Parity = NO_PARITY;
430*c2c66affSColin Finck 	Lcr.StopBits = STOP_BIT_1;
431*c2c66affSColin Finck 	Status = DeviceIoControl(LowerDevice, IOCTL_SERIAL_SET_LINE_CONTROL,
432*c2c66affSColin Finck 		&Lcr, sizeof(Lcr), NULL, NULL);
433*c2c66affSColin Finck 	if (!NT_SUCCESS(Status)) goto ByeBye;
434*c2c66affSColin Finck 	Status = STATUS_DEVICE_NOT_CONNECTED;
435*c2c66affSColin Finck 
436*c2c66affSColin Finck ByeBye:
437*c2c66affSColin Finck 	/* Close port */
438*c2c66affSColin Finck 	if (Handle)
439*c2c66affSColin Finck 		ZwClose(Handle);
440*c2c66affSColin Finck 	return Status;
441*c2c66affSColin Finck }
442*c2c66affSColin Finck 
443*c2c66affSColin Finck NTSTATUS
SerenumDetectLegacyDevice(IN PDEVICE_OBJECT DeviceObject,IN PDEVICE_OBJECT LowerDevice)444*c2c66affSColin Finck SerenumDetectLegacyDevice(
445*c2c66affSColin Finck 	IN PDEVICE_OBJECT DeviceObject,
446*c2c66affSColin Finck 	IN PDEVICE_OBJECT LowerDevice)
447*c2c66affSColin Finck {
448*c2c66affSColin Finck 	HANDLE Handle = NULL;
449*c2c66affSColin Finck 	ULONG Fcr, Mcr;
450*c2c66affSColin Finck 	ULONG BaudRate;
451*c2c66affSColin Finck 	ULONG Command;
452*c2c66affSColin Finck 	SERIAL_TIMEOUTS Timeouts;
453*c2c66affSColin Finck 	SERIAL_LINE_CONTROL LCR;
454*c2c66affSColin Finck 	ULONG i, Count = 0;
455*c2c66affSColin Finck 	UCHAR Buffer[16];
456*c2c66affSColin Finck 	UNICODE_STRING DeviceDescription;
457*c2c66affSColin Finck 	UNICODE_STRING DeviceId;
458*c2c66affSColin Finck 	UNICODE_STRING InstanceId;
459*c2c66affSColin Finck 	UNICODE_STRING HardwareIds;
460*c2c66affSColin Finck 	UNICODE_STRING CompatibleIds;
461*c2c66affSColin Finck 	NTSTATUS Status;
462*c2c66affSColin Finck 
463*c2c66affSColin Finck 	TRACE_(SERENUM, "SerenumDetectLegacyDevice(DeviceObject %p, LowerDevice %p)\n",
464*c2c66affSColin Finck 		DeviceObject,
465*c2c66affSColin Finck 		LowerDevice);
466*c2c66affSColin Finck 
467*c2c66affSColin Finck 	RtlZeroMemory(Buffer, sizeof(Buffer));
468*c2c66affSColin Finck 
469*c2c66affSColin Finck 	/* Open port */
470*c2c66affSColin Finck 	Status = ObOpenObjectByPointer(
471*c2c66affSColin Finck 		LowerDevice,
472*c2c66affSColin Finck 		OBJ_KERNEL_HANDLE,
473*c2c66affSColin Finck 		NULL,
474*c2c66affSColin Finck 		0,
475*c2c66affSColin Finck 		NULL,
476*c2c66affSColin Finck 		KernelMode,
477*c2c66affSColin Finck 		&Handle);
478*c2c66affSColin Finck 	if (!NT_SUCCESS(Status)) return Status;
479*c2c66affSColin Finck 
480*c2c66affSColin Finck 	/* Reset UART */
481*c2c66affSColin Finck 	TRACE_(SERENUM, "Reset UART\n");
482*c2c66affSColin Finck 	Mcr = 0; /* MCR: DTR/RTS/OUT2 off */
483*c2c66affSColin Finck 	Status = DeviceIoControl(LowerDevice, IOCTL_SERIAL_SET_MODEM_CONTROL,
484*c2c66affSColin Finck 		&Mcr, sizeof(Mcr), NULL, NULL);
485*c2c66affSColin Finck 	if (!NT_SUCCESS(Status)) goto ByeBye;
486*c2c66affSColin Finck 
487*c2c66affSColin Finck 	/* Set communications parameters */
488*c2c66affSColin Finck 	TRACE_(SERENUM, "Set communications parameters\n");
489*c2c66affSColin Finck 	/* DLAB off */
490*c2c66affSColin Finck 	Fcr = 0;
491*c2c66affSColin Finck 	Status = DeviceIoControl(LowerDevice, IOCTL_SERIAL_SET_FIFO_CONTROL,
492*c2c66affSColin Finck 		&Fcr, sizeof(Fcr), NULL, NULL);
493*c2c66affSColin Finck 	if (!NT_SUCCESS(Status)) goto ByeBye;
494*c2c66affSColin Finck 	/* Set serial port speed */
495*c2c66affSColin Finck 	BaudRate = 1200;
496*c2c66affSColin Finck 	Status = DeviceIoControl(LowerDevice, IOCTL_SERIAL_SET_BAUD_RATE,
497*c2c66affSColin Finck 		&BaudRate, sizeof(BaudRate), NULL, NULL);
498*c2c66affSColin Finck 	if (!NT_SUCCESS(Status)) goto ByeBye;
499*c2c66affSColin Finck 	/* Set LCR */
500*c2c66affSColin Finck 	LCR.WordLength = 7;
501*c2c66affSColin Finck 	LCR.Parity = NO_PARITY;
502*c2c66affSColin Finck 	LCR.StopBits = STOP_BITS_2;
503*c2c66affSColin Finck 	Status = DeviceIoControl(LowerDevice, IOCTL_SERIAL_SET_LINE_CONTROL,
504*c2c66affSColin Finck 		&LCR, sizeof(LCR), NULL, NULL);
505*c2c66affSColin Finck 	if (!NT_SUCCESS(Status)) goto ByeBye;
506*c2c66affSColin Finck 
507*c2c66affSColin Finck 	/* Flush receive buffer */
508*c2c66affSColin Finck 	TRACE_(SERENUM, "Flush receive buffer\n");
509*c2c66affSColin Finck 	Command = SERIAL_PURGE_RXCLEAR;
510*c2c66affSColin Finck 	Status = DeviceIoControl(LowerDevice, IOCTL_SERIAL_SET_MODEM_CONTROL,
511*c2c66affSColin Finck 		&Command, sizeof(Command), NULL, NULL);
512*c2c66affSColin Finck 	if (!NT_SUCCESS(Status)) goto ByeBye;
513*c2c66affSColin Finck 	/* Wait 100 ms */
514*c2c66affSColin Finck 	Wait(100);
515*c2c66affSColin Finck 
516*c2c66affSColin Finck 	/* Enable DTR/RTS */
517*c2c66affSColin Finck 	TRACE_(SERENUM, "Enable DTR/RTS\n");
518*c2c66affSColin Finck 	Status = DeviceIoControl(LowerDevice, IOCTL_SERIAL_SET_DTR,
519*c2c66affSColin Finck 		NULL, 0, NULL, NULL);
520*c2c66affSColin Finck 	if (!NT_SUCCESS(Status)) goto ByeBye;
521*c2c66affSColin Finck 	Status = DeviceIoControl(LowerDevice, IOCTL_SERIAL_SET_RTS,
522*c2c66affSColin Finck 		NULL, 0, NULL, NULL);
523*c2c66affSColin Finck 	if (!NT_SUCCESS(Status)) goto ByeBye;
524*c2c66affSColin Finck 
525*c2c66affSColin Finck 	/* Set timeout to 500 microseconds */
526*c2c66affSColin Finck 	TRACE_(SERENUM, "Set timeout to 500 microseconds\n");
527*c2c66affSColin Finck 	Timeouts.ReadIntervalTimeout = 100;
528*c2c66affSColin Finck 	Timeouts.ReadTotalTimeoutMultiplier = 0;
529*c2c66affSColin Finck 	Timeouts.ReadTotalTimeoutConstant = 500;
530*c2c66affSColin Finck 	Timeouts.WriteTotalTimeoutMultiplier = Timeouts.WriteTotalTimeoutConstant = 0;
531*c2c66affSColin Finck 	Status = DeviceIoControl(LowerDevice, IOCTL_SERIAL_SET_TIMEOUTS,
532*c2c66affSColin Finck 		&Timeouts, sizeof(Timeouts), NULL, NULL);
533*c2c66affSColin Finck 	if (!NT_SUCCESS(Status)) goto ByeBye;
534*c2c66affSColin Finck 
535*c2c66affSColin Finck 	/* Fill the read buffer */
536*c2c66affSColin Finck 	TRACE_(SERENUM, "Fill the read buffer\n");
537*c2c66affSColin Finck 	Status = ReadBytes(LowerDevice, Buffer, sizeof(Buffer)/sizeof(Buffer[0]), (PVOID)&Count);
538*c2c66affSColin Finck 	if (!NT_SUCCESS(Status)) goto ByeBye;
539*c2c66affSColin Finck 
540*c2c66affSColin Finck 	RtlInitUnicodeString(&DeviceId, L"Serenum\\Mouse");
541*c2c66affSColin Finck 	RtlInitUnicodeString(&InstanceId, L"0000"); /* FIXME */
542*c2c66affSColin Finck 	for (i = 0; i < Count; i++)
543*c2c66affSColin Finck 	{
544*c2c66affSColin Finck 		if (Buffer[i] == 'B')
545*c2c66affSColin Finck 		{
546*c2c66affSColin Finck 			/* Sign for Microsoft Ballpoint */
547*c2c66affSColin Finck 			/* Hardware id: *PNP0F09
548*c2c66affSColin Finck 			 * Compatible id: *PNP0F0F, SERIAL_MOUSE
549*c2c66affSColin Finck 			 */
550*c2c66affSColin Finck 			RtlInitUnicodeString(&DeviceDescription, L"Microsoft Ballpoint device");
551*c2c66affSColin Finck 			SerenumInitMultiSzString(&HardwareIds, "*PNP0F09", NULL);
552*c2c66affSColin Finck 			SerenumInitMultiSzString(&CompatibleIds, "*PNP0F0F", "SERIAL_MOUSE", NULL);
553*c2c66affSColin Finck 			Status = ReportDetectedDevice(DeviceObject,
554*c2c66affSColin Finck 				&DeviceDescription, &DeviceId, &InstanceId, &HardwareIds, &CompatibleIds);
555*c2c66affSColin Finck 			RtlFreeUnicodeString(&HardwareIds);
556*c2c66affSColin Finck 			RtlFreeUnicodeString(&CompatibleIds);
557*c2c66affSColin Finck 			goto ByeBye;
558*c2c66affSColin Finck 		}
559*c2c66affSColin Finck 		else if (Buffer[i] == 'M')
560*c2c66affSColin Finck 		{
561*c2c66affSColin Finck 			/* Sign for Microsoft Mouse protocol followed by button specifier */
562*c2c66affSColin Finck 			if (i == sizeof(Buffer) - 1)
563*c2c66affSColin Finck 			{
564*c2c66affSColin Finck 				/* Overflow Error */
565*c2c66affSColin Finck 				Status = STATUS_DEVICE_NOT_CONNECTED;
566*c2c66affSColin Finck 				goto ByeBye;
567*c2c66affSColin Finck 			}
568*c2c66affSColin Finck 			switch (Buffer[i + 1])
569*c2c66affSColin Finck 			{
570*c2c66affSColin Finck 				case '3':
571*c2c66affSColin Finck 					/* Hardware id: *PNP0F08
572*c2c66affSColin Finck 					 * Compatible id: SERIAL_MOUSE
573*c2c66affSColin Finck 					 */
574*c2c66affSColin Finck 					RtlInitUnicodeString(&DeviceDescription, L"Microsoft Mouse with 3-buttons");
575*c2c66affSColin Finck 					SerenumInitMultiSzString(&HardwareIds, "*PNP0F08", NULL);
576*c2c66affSColin Finck 					SerenumInitMultiSzString(&CompatibleIds, "SERIAL_MOUSE", NULL);
577*c2c66affSColin Finck 					break;
578*c2c66affSColin Finck 				default:
579*c2c66affSColin Finck 					/* Hardware id: *PNP0F01
580*c2c66affSColin Finck 					 * Compatible id: SERIAL_MOUSE
581*c2c66affSColin Finck 					 */
582*c2c66affSColin Finck 					RtlInitUnicodeString(&DeviceDescription, L"Microsoft Mouse with 2-buttons or Microsoft Wheel Mouse");
583*c2c66affSColin Finck 					SerenumInitMultiSzString(&HardwareIds, "*PNP0F01", NULL);
584*c2c66affSColin Finck 					SerenumInitMultiSzString(&CompatibleIds, "SERIAL_MOUSE", NULL);
585*c2c66affSColin Finck 					break;
586*c2c66affSColin Finck 			}
587*c2c66affSColin Finck 			Status = ReportDetectedDevice(DeviceObject,
588*c2c66affSColin Finck 				&DeviceDescription, &DeviceId, &InstanceId, &HardwareIds, &CompatibleIds);
589*c2c66affSColin Finck 			RtlFreeUnicodeString(&HardwareIds);
590*c2c66affSColin Finck 			RtlFreeUnicodeString(&CompatibleIds);
591*c2c66affSColin Finck 			goto ByeBye;
592*c2c66affSColin Finck 		}
593*c2c66affSColin Finck 	}
594*c2c66affSColin Finck 
595*c2c66affSColin Finck 	Status = STATUS_DEVICE_NOT_CONNECTED;
596*c2c66affSColin Finck 
597*c2c66affSColin Finck ByeBye:
598*c2c66affSColin Finck 	/* Close port */
599*c2c66affSColin Finck 	if (Handle)
600*c2c66affSColin Finck 		ZwClose(Handle);
601*c2c66affSColin Finck 	return Status;
602*c2c66affSColin Finck }
603