Open port before sending IOCTLs

svn path=/trunk/; revision=15297
This commit is contained in:
Hervé Poussineau 2005-05-15 09:30:25 +00:00
parent e1aa22c032
commit 5cec531950

View file

@ -61,6 +61,44 @@ SerenumDeviceIoControl(
return Status; return Status;
} }
static NTSTATUS
SerenumSendIrp(
IN PDEVICE_OBJECT DeviceObject,
IN ULONG MajorFunction)
{
KEVENT Event;
PIRP Irp;
IO_STATUS_BLOCK IoStatus;
NTSTATUS Status;
KeInitializeEvent(&Event, NotificationEvent, FALSE);
Irp = IoBuildSynchronousFsdRequest(
MajorFunction,
DeviceObject,
NULL,
0,
NULL,
&Event,
&IoStatus);
if (Irp == NULL)
{
DPRINT("Serenum: IoBuildSynchronousFsdRequest() failed\n");
return STATUS_INSUFFICIENT_RESOURCES;
}
Status = IoCallDriver(DeviceObject, Irp);
if (Status == STATUS_PENDING)
{
DPRINT("Serenum: Operation pending\n");
KeWaitForSingleObject(&Event, Suspended, KernelMode, FALSE, NULL);
Status = IoStatus.Status;
}
return Status;
}
static NTSTATUS static NTSTATUS
ReadBytes( ReadBytes(
IN PDEVICE_OBJECT LowerDevice, IN PDEVICE_OBJECT LowerDevice,
@ -224,91 +262,95 @@ SerenumDetectPnpDevice(
SERIAL_TIMEOUTS Timeouts; SERIAL_TIMEOUTS Timeouts;
SERIALPERF_STATS PerfStats; SERIALPERF_STATS PerfStats;
NTSTATUS Status; NTSTATUS Status;
/* Open port */
Status = SerenumSendIrp(LowerDevice, IRP_MJ_CREATE);
if (!NT_SUCCESS(Status)) goto ByeBye;
/* 1. COM port initialization, check for device enumerate */ /* 1. COM port initialization, check for device enumerate */
CHECKPOINT; CHECKPOINT;
Status = SerenumDeviceIoControl(LowerDevice, IOCTL_SERIAL_CLR_DTR, Status = SerenumDeviceIoControl(LowerDevice, IOCTL_SERIAL_CLR_DTR,
NULL, 0, NULL, NULL); NULL, 0, NULL, NULL);
if (!NT_SUCCESS(Status)) return Status; if (!NT_SUCCESS(Status)) goto ByeBye;
Status = SerenumDeviceIoControl(LowerDevice, IOCTL_SERIAL_CLR_RTS, Status = SerenumDeviceIoControl(LowerDevice, IOCTL_SERIAL_CLR_RTS,
NULL, 0, NULL, NULL); NULL, 0, NULL, NULL);
if (!NT_SUCCESS(Status)) return Status; if (!NT_SUCCESS(Status)) goto ByeBye;
SerenumWait(200); SerenumWait(200);
Size = sizeof(Msr); Size = sizeof(Msr);
Status = SerenumDeviceIoControl(LowerDevice, IOCTL_SERIAL_GET_MODEMSTATUS, Status = SerenumDeviceIoControl(LowerDevice, IOCTL_SERIAL_GET_MODEMSTATUS,
NULL, 0, &Msr, &Size); NULL, 0, &Msr, &Size);
if (!NT_SUCCESS(Status)) return Status; if (!NT_SUCCESS(Status)) goto ByeBye;
if ((Msr & SR_MSR_DSR) == 0) goto SerenumDisconnectIdle; if ((Msr & SR_MSR_DSR) == 0) goto SerenumDisconnectIdle;
/* 2. COM port setup, 1st phase */ /* 2. COM port setup, 1st phase */
CHECKPOINT; CHECKPOINT;
BaudRate = SERIAL_BAUD_1200; BaudRate = 1200;
Status = SerenumDeviceIoControl(LowerDevice, IOCTL_SERIAL_SET_BAUD_RATE, Status = SerenumDeviceIoControl(LowerDevice, IOCTL_SERIAL_SET_BAUD_RATE,
&BaudRate, sizeof(BaudRate), NULL, 0); &BaudRate, sizeof(BaudRate), NULL, 0);
if (!NT_SUCCESS(Status)) return Status; if (!NT_SUCCESS(Status)) goto ByeBye;
Lcr.WordLength = 7; Lcr.WordLength = 7;
Lcr.Parity = NO_PARITY; Lcr.Parity = NO_PARITY;
Lcr.StopBits = STOP_BIT_1; Lcr.StopBits = STOP_BIT_1;
Status = SerenumDeviceIoControl(LowerDevice, IOCTL_SERIAL_SET_LINE_CONTROL, Status = SerenumDeviceIoControl(LowerDevice, IOCTL_SERIAL_SET_LINE_CONTROL,
&Lcr, sizeof(Lcr), NULL, NULL); &Lcr, sizeof(Lcr), NULL, NULL);
if (!NT_SUCCESS(Status)) return Status; if (!NT_SUCCESS(Status)) goto ByeBye;
Status = SerenumDeviceIoControl(LowerDevice, IOCTL_SERIAL_CLR_DTR, Status = SerenumDeviceIoControl(LowerDevice, IOCTL_SERIAL_CLR_DTR,
NULL, 0, NULL, NULL); NULL, 0, NULL, NULL);
if (!NT_SUCCESS(Status)) return Status; if (!NT_SUCCESS(Status)) goto ByeBye;
Status = SerenumDeviceIoControl(LowerDevice, IOCTL_SERIAL_CLR_RTS, Status = SerenumDeviceIoControl(LowerDevice, IOCTL_SERIAL_CLR_RTS,
NULL, 0, NULL, NULL); NULL, 0, NULL, NULL);
if (!NT_SUCCESS(Status)) return Status; if (!NT_SUCCESS(Status)) goto ByeBye;
SerenumWait(200); SerenumWait(200);
Status = SerenumDeviceIoControl(LowerDevice, IOCTL_SERIAL_SET_DTR, Status = SerenumDeviceIoControl(LowerDevice, IOCTL_SERIAL_SET_DTR,
NULL, 0, NULL, NULL); NULL, 0, NULL, NULL);
if (!NT_SUCCESS(Status)) return Status; if (!NT_SUCCESS(Status)) goto ByeBye;
SerenumWait(200); SerenumWait(200);
/* 3. Wait for response, 1st phase */ /* 3. Wait for response, 1st phase */
CHECKPOINT; CHECKPOINT;
Status = SerenumDeviceIoControl(LowerDevice, IOCTL_SERIAL_SET_RTS, Status = SerenumDeviceIoControl(LowerDevice, IOCTL_SERIAL_SET_RTS,
NULL, 0, NULL, NULL); NULL, 0, NULL, NULL);
if (!NT_SUCCESS(Status)) return Status; if (!NT_SUCCESS(Status)) goto ByeBye;
Timeouts.ReadIntervalTimeout = 0; Timeouts.ReadIntervalTimeout = 0;
Timeouts.ReadTotalTimeoutMultiplier = 0; Timeouts.ReadTotalTimeoutMultiplier = 0;
Timeouts.ReadTotalTimeoutConstant = 200; Timeouts.ReadTotalTimeoutConstant = 200;
Timeouts.WriteTotalTimeoutMultiplier = Timeouts.WriteTotalTimeoutConstant = 0; Timeouts.WriteTotalTimeoutMultiplier = Timeouts.WriteTotalTimeoutConstant = 0;
Status = SerenumDeviceIoControl(LowerDevice, IOCTL_SERIAL_SET_TIMEOUTS, Status = SerenumDeviceIoControl(LowerDevice, IOCTL_SERIAL_SET_TIMEOUTS,
&Timeouts, sizeof(Timeouts), NULL, NULL); &Timeouts, sizeof(Timeouts), NULL, NULL);
if (!NT_SUCCESS(Status)) return Status; if (!NT_SUCCESS(Status)) goto ByeBye;
Status = ReadBytes(LowerDevice, Buffer, sizeof(Buffer), &Size); Status = ReadBytes(LowerDevice, Buffer, sizeof(Buffer), &Size);
if (!NT_SUCCESS(Status)) return Status; if (!NT_SUCCESS(Status)) goto ByeBye;
if (Size != 0) goto SerenumCollectPnpComDeviceId; if (Size != 0) goto SerenumCollectPnpComDeviceId;
/* 4. COM port setup, 2nd phase */ /* 4. COM port setup, 2nd phase */
CHECKPOINT; CHECKPOINT;
Status = SerenumDeviceIoControl(LowerDevice, IOCTL_SERIAL_CLR_DTR, Status = SerenumDeviceIoControl(LowerDevice, IOCTL_SERIAL_CLR_DTR,
NULL, 0, NULL, NULL); NULL, 0, NULL, NULL);
if (!NT_SUCCESS(Status)) return Status; if (!NT_SUCCESS(Status)) goto ByeBye;
Status = SerenumDeviceIoControl(LowerDevice, IOCTL_SERIAL_CLR_RTS, Status = SerenumDeviceIoControl(LowerDevice, IOCTL_SERIAL_CLR_RTS,
NULL, 0, NULL, NULL); NULL, 0, NULL, NULL);
if (!NT_SUCCESS(Status)) return Status; if (!NT_SUCCESS(Status)) goto ByeBye;
Purge = SERIAL_PURGE_RXABORT | SERIAL_PURGE_RXCLEAR; Purge = SERIAL_PURGE_RXABORT | SERIAL_PURGE_RXCLEAR;
Status = SerenumDeviceIoControl(LowerDevice, IOCTL_SERIAL_PURGE, Status = SerenumDeviceIoControl(LowerDevice, IOCTL_SERIAL_PURGE,
&Purge, 0, NULL, NULL); &Purge, 0, NULL, NULL);
if (!NT_SUCCESS(Status)) return Status; if (!NT_SUCCESS(Status)) goto ByeBye;
SerenumWait(200); SerenumWait(200);
/* 5. Wait for response, 2nd phase */ /* 5. Wait for response, 2nd phase */
CHECKPOINT; CHECKPOINT;
Status = SerenumDeviceIoControl(LowerDevice, IOCTL_SERIAL_SET_DTR, Status = SerenumDeviceIoControl(LowerDevice, IOCTL_SERIAL_SET_DTR,
NULL, 0, NULL, NULL); NULL, 0, NULL, NULL);
if (!NT_SUCCESS(Status)) return Status; if (!NT_SUCCESS(Status)) goto ByeBye;
Status = SerenumDeviceIoControl(LowerDevice, IOCTL_SERIAL_SET_RTS, Status = SerenumDeviceIoControl(LowerDevice, IOCTL_SERIAL_SET_RTS,
NULL, 0, NULL, NULL); NULL, 0, NULL, NULL);
if (!NT_SUCCESS(Status)) return Status; if (!NT_SUCCESS(Status)) goto ByeBye;
Status = ReadBytes(LowerDevice, Buffer, 1, &TotalBytesReceived); Status = ReadBytes(LowerDevice, Buffer, 1, &TotalBytesReceived);
if (!NT_SUCCESS(Status)) return Status; if (!NT_SUCCESS(Status)) goto ByeBye;
if (TotalBytesReceived != 0) goto SerenumCollectPnpComDeviceId; if (TotalBytesReceived != 0) goto SerenumCollectPnpComDeviceId;
Size = sizeof(Msr); Size = sizeof(Msr);
Status = SerenumDeviceIoControl(LowerDevice, IOCTL_SERIAL_GET_MODEMSTATUS, Status = SerenumDeviceIoControl(LowerDevice, IOCTL_SERIAL_GET_MODEMSTATUS,
NULL, 0, &Msr, &Size); NULL, 0, &Msr, &Size);
if (!NT_SUCCESS(Status)) return Status; if (!NT_SUCCESS(Status)) goto ByeBye;
if ((Msr & SR_MSR_DSR) == 0) goto SerenumVerifyDisconnect; else goto SerenumConnectIdle; if ((Msr & SR_MSR_DSR) == 0) goto SerenumVerifyDisconnect; else goto SerenumConnectIdle;
/* 6. Collect PnP COM device ID */ /* 6. Collect PnP COM device ID */
@ -319,14 +361,14 @@ SerenumCollectPnpComDeviceId:
Timeouts.ReadTotalTimeoutConstant = 2200; Timeouts.ReadTotalTimeoutConstant = 2200;
Status = SerenumDeviceIoControl(LowerDevice, IOCTL_SERIAL_SET_TIMEOUTS, Status = SerenumDeviceIoControl(LowerDevice, IOCTL_SERIAL_SET_TIMEOUTS,
&Timeouts, sizeof(Timeouts), NULL, NULL); &Timeouts, sizeof(Timeouts), NULL, NULL);
if (!NT_SUCCESS(Status)) return Status; if (!NT_SUCCESS(Status)) goto ByeBye;
Status = ReadBytes(LowerDevice, &Buffer[TotalBytesReceived], sizeof(Buffer) - TotalBytesReceived, &Size); Status = ReadBytes(LowerDevice, &Buffer[TotalBytesReceived], sizeof(Buffer) - TotalBytesReceived, &Size);
if (!NT_SUCCESS(Status)) return Status; if (!NT_SUCCESS(Status)) goto ByeBye;
TotalBytesReceived += Size; TotalBytesReceived += Size;
Size = sizeof(PerfStats); Size = sizeof(PerfStats);
Status = SerenumDeviceIoControl(LowerDevice, IOCTL_SERIAL_GET_STATS, Status = SerenumDeviceIoControl(LowerDevice, IOCTL_SERIAL_GET_STATS,
NULL, 0, &PerfStats, &Size); NULL, 0, &PerfStats, &Size);
if (!NT_SUCCESS(Status)) return Status; if (!NT_SUCCESS(Status)) goto ByeBye;
if (PerfStats.FrameErrorCount + PerfStats.ParityErrorCount != 0) goto SerenumConnectIdle; if (PerfStats.FrameErrorCount + PerfStats.ParityErrorCount != 0) goto SerenumConnectIdle;
for (i = 0; i < TotalBytesReceived; i++) for (i = 0; i < TotalBytesReceived; i++)
{ {
@ -336,7 +378,10 @@ SerenumCollectPnpComDeviceId:
if (TotalBytesReceived == 1 || BufferContainsEndId) if (TotalBytesReceived == 1 || BufferContainsEndId)
{ {
if (SerenumIsValidPnpIdString(Buffer, TotalBytesReceived)) if (SerenumIsValidPnpIdString(Buffer, TotalBytesReceived))
return ReportDetectedPnpDevice(Buffer, TotalBytesReceived); {
Status = ReportDetectedPnpDevice(Buffer, TotalBytesReceived);
goto ByeBye;
}
goto SerenumConnectIdle; goto SerenumConnectIdle;
} }
if (!BufferContainsBeginId) goto SerenumConnectIdle; if (!BufferContainsBeginId) goto SerenumConnectIdle;
@ -344,7 +389,7 @@ SerenumCollectPnpComDeviceId:
Size = sizeof(Msr); Size = sizeof(Msr);
Status = SerenumDeviceIoControl(LowerDevice, IOCTL_SERIAL_GET_MODEMSTATUS, Status = SerenumDeviceIoControl(LowerDevice, IOCTL_SERIAL_GET_MODEMSTATUS,
NULL, 0, &Msr, &Size); NULL, 0, &Msr, &Size);
if (!NT_SUCCESS(Status)) return Status; if (!NT_SUCCESS(Status)) goto ByeBye;
if ((Msr & SR_MSR_DSR) == 0) goto SerenumVerifyDisconnect; if ((Msr & SR_MSR_DSR) == 0) goto SerenumVerifyDisconnect;
/* 7. Verify disconnect */ /* 7. Verify disconnect */
@ -352,10 +397,10 @@ SerenumVerifyDisconnect:
CHECKPOINT; CHECKPOINT;
Status = SerenumDeviceIoControl(LowerDevice, IOCTL_SERIAL_SET_DTR, Status = SerenumDeviceIoControl(LowerDevice, IOCTL_SERIAL_SET_DTR,
NULL, 0, NULL, NULL); NULL, 0, NULL, NULL);
if (!NT_SUCCESS(Status)) return Status; if (!NT_SUCCESS(Status)) goto ByeBye;
Status = SerenumDeviceIoControl(LowerDevice, IOCTL_SERIAL_CLR_RTS, Status = SerenumDeviceIoControl(LowerDevice, IOCTL_SERIAL_CLR_RTS,
NULL, 0, NULL, NULL); NULL, 0, NULL, NULL);
if (!NT_SUCCESS(Status)) return Status; if (!NT_SUCCESS(Status)) goto ByeBye;
SerenumWait(5000); SerenumWait(5000);
goto SerenumDisconnectIdle; goto SerenumDisconnectIdle;
@ -364,24 +409,25 @@ SerenumConnectIdle:
CHECKPOINT; CHECKPOINT;
Status = SerenumDeviceIoControl(LowerDevice, IOCTL_SERIAL_SET_DTR, Status = SerenumDeviceIoControl(LowerDevice, IOCTL_SERIAL_SET_DTR,
NULL, 0, NULL, NULL); NULL, 0, NULL, NULL);
if (!NT_SUCCESS(Status)) return Status; if (!NT_SUCCESS(Status)) goto ByeBye;
Status = SerenumDeviceIoControl(LowerDevice, IOCTL_SERIAL_CLR_RTS, Status = SerenumDeviceIoControl(LowerDevice, IOCTL_SERIAL_CLR_RTS,
NULL, 0, NULL, NULL); NULL, 0, NULL, NULL);
if (!NT_SUCCESS(Status)) return Status; if (!NT_SUCCESS(Status)) goto ByeBye;
BaudRate = SERIAL_BAUD_300; BaudRate = 300;
Status = SerenumDeviceIoControl(LowerDevice, IOCTL_SERIAL_SET_BAUD_RATE, Status = SerenumDeviceIoControl(LowerDevice, IOCTL_SERIAL_SET_BAUD_RATE,
&BaudRate, sizeof(BaudRate), NULL, NULL); &BaudRate, sizeof(BaudRate), NULL, NULL);
if (!NT_SUCCESS(Status)) return Status; if (!NT_SUCCESS(Status)) goto ByeBye;
Lcr.WordLength = 7; Lcr.WordLength = 7;
Lcr.Parity = NO_PARITY; Lcr.Parity = NO_PARITY;
Lcr.StopBits = STOP_BIT_1; Lcr.StopBits = STOP_BIT_1;
Status = SerenumDeviceIoControl(LowerDevice, IOCTL_SERIAL_SET_LINE_CONTROL, Status = SerenumDeviceIoControl(LowerDevice, IOCTL_SERIAL_SET_LINE_CONTROL,
&Lcr, sizeof(Lcr), NULL, NULL); &Lcr, sizeof(Lcr), NULL, NULL);
if (!NT_SUCCESS(Status)) return Status; if (!NT_SUCCESS(Status)) goto ByeBye;
if (TotalBytesReceived == 0) if (TotalBytesReceived == 0)
return STATUS_DEVICE_NOT_CONNECTED; Status = STATUS_DEVICE_NOT_CONNECTED;
else else
return STATUS_SUCCESS; Status = STATUS_SUCCESS;
goto ByeBye;
/* 9. Disconnect idle */ /* 9. Disconnect idle */
SerenumDisconnectIdle: SerenumDisconnectIdle:
@ -389,21 +435,27 @@ SerenumDisconnectIdle:
/* FIXME: report to OS device removal, if it was present */ /* FIXME: report to OS device removal, if it was present */
Status = SerenumDeviceIoControl(LowerDevice, IOCTL_SERIAL_SET_DTR, Status = SerenumDeviceIoControl(LowerDevice, IOCTL_SERIAL_SET_DTR,
NULL, 0, NULL, NULL); NULL, 0, NULL, NULL);
if (!NT_SUCCESS(Status)) return Status; if (!NT_SUCCESS(Status)) goto ByeBye;
Status = SerenumDeviceIoControl(LowerDevice, IOCTL_SERIAL_CLR_RTS, Status = SerenumDeviceIoControl(LowerDevice, IOCTL_SERIAL_CLR_RTS,
NULL, 0, NULL, NULL); NULL, 0, NULL, NULL);
if (!NT_SUCCESS(Status)) return Status; if (!NT_SUCCESS(Status)) goto ByeBye;
BaudRate = SERIAL_BAUD_300; BaudRate = 300;
Status = SerenumDeviceIoControl(LowerDevice, IOCTL_SERIAL_SET_BAUD_RATE, Status = SerenumDeviceIoControl(LowerDevice, IOCTL_SERIAL_SET_BAUD_RATE,
&BaudRate, sizeof(BaudRate), NULL, NULL); &BaudRate, sizeof(BaudRate), NULL, NULL);
if (!NT_SUCCESS(Status)) return Status; if (!NT_SUCCESS(Status)) goto ByeBye;
Lcr.WordLength = 7; Lcr.WordLength = 7;
Lcr.Parity = NO_PARITY; Lcr.Parity = NO_PARITY;
Lcr.StopBits = STOP_BIT_1; Lcr.StopBits = STOP_BIT_1;
Status = SerenumDeviceIoControl(LowerDevice, IOCTL_SERIAL_SET_LINE_CONTROL, Status = SerenumDeviceIoControl(LowerDevice, IOCTL_SERIAL_SET_LINE_CONTROL,
&Lcr, sizeof(Lcr), NULL, NULL); &Lcr, sizeof(Lcr), NULL, NULL);
if (!NT_SUCCESS(Status)) return Status; if (!NT_SUCCESS(Status)) goto ByeBye;
return STATUS_DEVICE_NOT_CONNECTED; Status = STATUS_DEVICE_NOT_CONNECTED;
ByeBye:
/* Close port */
SerenumSendIrp(LowerDevice, IRP_MJ_CLOSE);
SerenumSendIrp(LowerDevice, IRP_MJ_CLEANUP);
return Status;
} }
NTSTATUS NTSTATUS
@ -429,13 +481,17 @@ SerenumDetectLegacyDevice(
LowerDevice); LowerDevice);
RtlZeroMemory(Buffer, sizeof(Buffer)); RtlZeroMemory(Buffer, sizeof(Buffer));
/* Open port */
Status = SerenumSendIrp(LowerDevice, IRP_MJ_CREATE);
if (!NT_SUCCESS(Status)) goto ByeBye;
/* Reset UART */ /* Reset UART */
CHECKPOINT; CHECKPOINT;
Mcr = 0; /* MCR: DTR/RTS/OUT2 off */ Mcr = 0; /* MCR: DTR/RTS/OUT2 off */
Status = SerenumDeviceIoControl(LowerDevice, IOCTL_SERIAL_SET_MODEM_CONTROL, Status = SerenumDeviceIoControl(LowerDevice, IOCTL_SERIAL_SET_MODEM_CONTROL,
&Mcr, sizeof(Mcr), NULL, NULL); &Mcr, sizeof(Mcr), NULL, NULL);
if (!NT_SUCCESS(Status)) return Status; if (!NT_SUCCESS(Status)) goto ByeBye;
/* Set communications parameters */ /* Set communications parameters */
CHECKPOINT; CHECKPOINT;
@ -443,26 +499,26 @@ SerenumDetectLegacyDevice(
Fcr = 0; Fcr = 0;
Status = SerenumDeviceIoControl(LowerDevice, IOCTL_SERIAL_SET_FIFO_CONTROL, Status = SerenumDeviceIoControl(LowerDevice, IOCTL_SERIAL_SET_FIFO_CONTROL,
&Fcr, sizeof(Fcr), NULL, NULL); &Fcr, sizeof(Fcr), NULL, NULL);
if (!NT_SUCCESS(Status)) return Status; if (!NT_SUCCESS(Status)) goto ByeBye;
/* Set serial port speed */ /* Set serial port speed */
BaudRate = SERIAL_BAUD_1200; BaudRate = 1200;
Status = SerenumDeviceIoControl(LowerDevice, IOCTL_SERIAL_SET_BAUD_RATE, Status = SerenumDeviceIoControl(LowerDevice, IOCTL_SERIAL_SET_BAUD_RATE,
&BaudRate, sizeof(BaudRate), NULL, NULL); &BaudRate, sizeof(BaudRate), NULL, NULL);
if (!NT_SUCCESS(Status)) return Status; if (!NT_SUCCESS(Status)) goto ByeBye;
/* Set LCR */ /* Set LCR */
LCR.WordLength = 7; LCR.WordLength = 7;
LCR.Parity = NO_PARITY; LCR.Parity = NO_PARITY;
LCR.StopBits = STOP_BITS_2; LCR.StopBits = STOP_BITS_2;
Status = SerenumDeviceIoControl(LowerDevice, IOCTL_SERIAL_SET_LINE_CONTROL, Status = SerenumDeviceIoControl(LowerDevice, IOCTL_SERIAL_SET_LINE_CONTROL,
&LCR, sizeof(LCR), NULL, NULL); &LCR, sizeof(LCR), NULL, NULL);
if (!NT_SUCCESS(Status)) return Status; if (!NT_SUCCESS(Status)) goto ByeBye;
/* Flush receive buffer */ /* Flush receive buffer */
CHECKPOINT; CHECKPOINT;
Command = SERIAL_PURGE_RXCLEAR; Command = SERIAL_PURGE_RXCLEAR;
Status = SerenumDeviceIoControl(LowerDevice, IOCTL_SERIAL_SET_MODEM_CONTROL, Status = SerenumDeviceIoControl(LowerDevice, IOCTL_SERIAL_SET_MODEM_CONTROL,
&Command, sizeof(Command), NULL, NULL); &Command, sizeof(Command), NULL, NULL);
if (!NT_SUCCESS(Status)) return Status; if (!NT_SUCCESS(Status)) goto ByeBye;
/* Wait 100 ms */ /* Wait 100 ms */
SerenumWait(100); SerenumWait(100);
@ -470,10 +526,10 @@ SerenumDetectLegacyDevice(
CHECKPOINT; CHECKPOINT;
Status = SerenumDeviceIoControl(LowerDevice, IOCTL_SERIAL_SET_DTR, Status = SerenumDeviceIoControl(LowerDevice, IOCTL_SERIAL_SET_DTR,
NULL, 0, NULL, NULL); NULL, 0, NULL, NULL);
if (!NT_SUCCESS(Status)) return Status; if (!NT_SUCCESS(Status)) goto ByeBye;
Status = SerenumDeviceIoControl(LowerDevice, IOCTL_SERIAL_SET_RTS, Status = SerenumDeviceIoControl(LowerDevice, IOCTL_SERIAL_SET_RTS,
NULL, 0, NULL, NULL); NULL, 0, NULL, NULL);
if (!NT_SUCCESS(Status)) return Status; if (!NT_SUCCESS(Status)) goto ByeBye;
/* Set timeout to 500 microseconds */ /* Set timeout to 500 microseconds */
CHECKPOINT; CHECKPOINT;
@ -483,12 +539,12 @@ SerenumDetectLegacyDevice(
Timeouts.WriteTotalTimeoutMultiplier = Timeouts.WriteTotalTimeoutConstant = 0; Timeouts.WriteTotalTimeoutMultiplier = Timeouts.WriteTotalTimeoutConstant = 0;
Status = SerenumDeviceIoControl(LowerDevice, IOCTL_SERIAL_SET_TIMEOUTS, Status = SerenumDeviceIoControl(LowerDevice, IOCTL_SERIAL_SET_TIMEOUTS,
&Timeouts, sizeof(Timeouts), NULL, NULL); &Timeouts, sizeof(Timeouts), NULL, NULL);
if (!NT_SUCCESS(Status)) return Status; if (!NT_SUCCESS(Status)) goto ByeBye;
/* Fill the read buffer */ /* Fill the read buffer */
CHECKPOINT; CHECKPOINT;
Status = ReadBytes(LowerDevice, Buffer, sizeof(Buffer)/sizeof(Buffer[0]), &Count); Status = ReadBytes(LowerDevice, Buffer, sizeof(Buffer)/sizeof(Buffer[0]), &Count);
if (!NT_SUCCESS(Status)) return Status; if (!NT_SUCCESS(Status)) goto ByeBye;
for (i = 0; i < Count; i++) for (i = 0; i < Count; i++)
{ {
@ -506,7 +562,7 @@ SerenumDetectLegacyDevice(
&DeviceDescription, &DeviceId, &HardwareIds, &CompatibleIds); &DeviceDescription, &DeviceId, &HardwareIds, &CompatibleIds);
RtlFreeUnicodeString(&HardwareIds); RtlFreeUnicodeString(&HardwareIds);
RtlFreeUnicodeString(&CompatibleIds); RtlFreeUnicodeString(&CompatibleIds);
return Status; goto ByeBye;
} }
else if (Buffer[i] == 'M') else if (Buffer[i] == 'M')
{ {
@ -514,7 +570,8 @@ SerenumDetectLegacyDevice(
if (i == sizeof(Buffer) - 1) if (i == sizeof(Buffer) - 1)
{ {
/* Overflow Error */ /* Overflow Error */
return STATUS_DEVICE_NOT_CONNECTED; Status = STATUS_DEVICE_NOT_CONNECTED;
goto ByeBye;
} }
switch (Buffer[i + 1]) switch (Buffer[i + 1])
{ {
@ -539,9 +596,15 @@ SerenumDetectLegacyDevice(
&DeviceDescription, &DeviceId, &HardwareIds, &CompatibleIds); &DeviceDescription, &DeviceId, &HardwareIds, &CompatibleIds);
RtlFreeUnicodeString(&HardwareIds); RtlFreeUnicodeString(&HardwareIds);
RtlFreeUnicodeString(&CompatibleIds); RtlFreeUnicodeString(&CompatibleIds);
return Status; goto ByeBye;
} }
} }
return STATUS_DEVICE_NOT_CONNECTED; Status = STATUS_DEVICE_NOT_CONNECTED;
ByeBye:
/* Close port */
SerenumSendIrp(LowerDevice, IRP_MJ_CLOSE);
SerenumSendIrp(LowerDevice, IRP_MJ_CLEANUP);
return Status;
} }