2005-11-09 11:57:58 +00:00
|
|
|
|
/*
|
2006-04-23 08:46:25 +00:00
|
|
|
|
* PROJECT: ReactOS Serial mouse driver
|
|
|
|
|
* LICENSE: GPL - See COPYING in the top level directory
|
|
|
|
|
* FILE: drivers/input/sermouse/detect.c
|
|
|
|
|
* PURPOSE: Detect serial mouse type
|
|
|
|
|
* PROGRAMMERS: Copyright Jason Filby (jasonfilby@yahoo.com)
|
|
|
|
|
Copyright Filip Navara (xnavara@volny.cz)
|
|
|
|
|
Copyright 2005-2006 Herv<EFBFBD> Poussineau (hpoussin@reactos.org)
|
2005-11-09 11:57:58 +00:00
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include "sermouse.h"
|
|
|
|
|
|
2014-01-29 10:56:25 +00:00
|
|
|
|
#include <ntifs.h>
|
|
|
|
|
#include <debug.h>
|
|
|
|
|
|
2005-11-09 11:57:58 +00:00
|
|
|
|
/* Most of this file is ripped from reactos/drivers/bus/serenum/detect.c */
|
|
|
|
|
|
|
|
|
|
static NTSTATUS
|
2006-04-23 08:46:25 +00:00
|
|
|
|
DeviceIoControl(
|
2005-11-09 11:57:58 +00:00
|
|
|
|
IN PDEVICE_OBJECT DeviceObject,
|
|
|
|
|
IN ULONG CtlCode,
|
|
|
|
|
IN PVOID InputBuffer OPTIONAL,
|
2007-03-18 00:27:02 +00:00
|
|
|
|
IN SIZE_T InputBufferSize,
|
2005-11-09 11:57:58 +00:00
|
|
|
|
IN OUT PVOID OutputBuffer OPTIONAL,
|
2007-03-18 00:27:02 +00:00
|
|
|
|
IN OUT PSIZE_T OutputBufferSize)
|
2005-11-09 11:57:58 +00:00
|
|
|
|
{
|
|
|
|
|
KEVENT Event;
|
|
|
|
|
PIRP Irp;
|
|
|
|
|
IO_STATUS_BLOCK IoStatus;
|
|
|
|
|
NTSTATUS Status;
|
|
|
|
|
|
|
|
|
|
KeInitializeEvent (&Event, NotificationEvent, FALSE);
|
|
|
|
|
|
|
|
|
|
Irp = IoBuildDeviceIoControlRequest(CtlCode,
|
|
|
|
|
DeviceObject,
|
|
|
|
|
InputBuffer,
|
2007-03-18 00:27:02 +00:00
|
|
|
|
(ULONG)InputBufferSize,
|
2005-11-09 11:57:58 +00:00
|
|
|
|
OutputBuffer,
|
2007-03-18 00:27:02 +00:00
|
|
|
|
(OutputBufferSize) ? (ULONG)*OutputBufferSize : 0,
|
2005-11-09 11:57:58 +00:00
|
|
|
|
FALSE,
|
|
|
|
|
&Event,
|
|
|
|
|
&IoStatus);
|
|
|
|
|
if (Irp == NULL)
|
|
|
|
|
{
|
2007-12-28 21:10:07 +00:00
|
|
|
|
WARN_(SERMOUSE, "IoBuildDeviceIoControlRequest() failed\n");
|
2005-11-09 11:57:58 +00:00
|
|
|
|
return STATUS_INSUFFICIENT_RESOURCES;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Status = IoCallDriver(DeviceObject, Irp);
|
|
|
|
|
|
|
|
|
|
if (Status == STATUS_PENDING)
|
|
|
|
|
{
|
2007-12-28 21:10:07 +00:00
|
|
|
|
INFO_(SERMOUSE, "Operation pending\n");
|
2005-11-09 11:57:58 +00:00
|
|
|
|
KeWaitForSingleObject(&Event, Suspended, KernelMode, FALSE, NULL);
|
|
|
|
|
Status = IoStatus.Status;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (OutputBufferSize)
|
|
|
|
|
{
|
2007-03-18 00:27:02 +00:00
|
|
|
|
*OutputBufferSize = (SIZE_T)IoStatus.Information;
|
2005-11-09 11:57:58 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return Status;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static NTSTATUS
|
|
|
|
|
ReadBytes(
|
|
|
|
|
IN PDEVICE_OBJECT LowerDevice,
|
|
|
|
|
OUT PUCHAR Buffer,
|
|
|
|
|
IN ULONG BufferSize,
|
2006-06-07 17:33:57 +00:00
|
|
|
|
OUT PULONG_PTR FilledBytes)
|
2005-11-09 11:57:58 +00:00
|
|
|
|
{
|
|
|
|
|
PIRP Irp;
|
|
|
|
|
IO_STATUS_BLOCK ioStatus;
|
|
|
|
|
KEVENT event;
|
|
|
|
|
LARGE_INTEGER zero;
|
|
|
|
|
NTSTATUS Status;
|
|
|
|
|
|
|
|
|
|
KeInitializeEvent(&event, NotificationEvent, FALSE);
|
|
|
|
|
zero.QuadPart = 0;
|
|
|
|
|
Irp = IoBuildSynchronousFsdRequest(
|
|
|
|
|
IRP_MJ_READ,
|
|
|
|
|
LowerDevice,
|
|
|
|
|
Buffer, BufferSize,
|
|
|
|
|
&zero,
|
|
|
|
|
&event,
|
|
|
|
|
&ioStatus);
|
|
|
|
|
if (!Irp)
|
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
|
|
Status = IoCallDriver(LowerDevice, Irp);
|
|
|
|
|
if (Status == STATUS_PENDING)
|
|
|
|
|
{
|
|
|
|
|
KeWaitForSingleObject(&event, Suspended, KernelMode, FALSE, NULL);
|
|
|
|
|
Status = ioStatus.Status;
|
|
|
|
|
}
|
2007-12-28 21:10:07 +00:00
|
|
|
|
INFO_(SERMOUSE, "Bytes received: %lu/%lu\n",
|
2005-11-09 11:57:58 +00:00
|
|
|
|
ioStatus.Information, BufferSize);
|
|
|
|
|
*FilledBytes = ioStatus.Information;
|
|
|
|
|
return Status;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static NTSTATUS
|
2006-04-23 08:46:25 +00:00
|
|
|
|
Wait(
|
|
|
|
|
IN ULONG milliseconds)
|
2005-11-09 11:57:58 +00:00
|
|
|
|
{
|
|
|
|
|
KTIMER Timer;
|
|
|
|
|
LARGE_INTEGER DueTime;
|
|
|
|
|
|
|
|
|
|
DueTime.QuadPart = milliseconds * -10;
|
|
|
|
|
KeInitializeTimer(&Timer);
|
|
|
|
|
KeSetTimer(&Timer, DueTime, NULL);
|
|
|
|
|
return KeWaitForSingleObject(&Timer, Executive, KernelMode, FALSE, NULL);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
SERMOUSE_MOUSE_TYPE
|
|
|
|
|
SermouseDetectLegacyDevice(
|
|
|
|
|
IN PDEVICE_OBJECT LowerDevice)
|
|
|
|
|
{
|
2006-06-30 21:47:42 +00:00
|
|
|
|
HANDLE Handle;
|
2005-11-09 11:57:58 +00:00
|
|
|
|
ULONG Fcr, Mcr;
|
|
|
|
|
ULONG BaudRate;
|
|
|
|
|
ULONG Command;
|
|
|
|
|
SERIAL_TIMEOUTS Timeouts;
|
|
|
|
|
SERIAL_LINE_CONTROL LCR;
|
2006-06-07 17:33:57 +00:00
|
|
|
|
ULONG_PTR i, Count = 0;
|
2005-11-09 11:57:58 +00:00
|
|
|
|
UCHAR Buffer[16];
|
|
|
|
|
SERMOUSE_MOUSE_TYPE MouseType = mtNone;
|
|
|
|
|
NTSTATUS Status;
|
|
|
|
|
|
2007-12-28 21:10:07 +00:00
|
|
|
|
TRACE_(SERMOUSE, "SermouseDetectLegacyDevice(LowerDevice %p)\n", LowerDevice);
|
2006-04-23 08:46:25 +00:00
|
|
|
|
|
2005-11-09 11:57:58 +00:00
|
|
|
|
RtlZeroMemory(Buffer, sizeof(Buffer));
|
|
|
|
|
|
|
|
|
|
/* Open port */
|
2006-06-30 21:47:42 +00:00
|
|
|
|
Status = ObOpenObjectByPointer(
|
|
|
|
|
LowerDevice,
|
2009-01-02 22:12:48 +00:00
|
|
|
|
OBJ_KERNEL_HANDLE,
|
2006-06-30 21:47:42 +00:00
|
|
|
|
NULL,
|
|
|
|
|
0,
|
|
|
|
|
NULL,
|
|
|
|
|
KernelMode,
|
|
|
|
|
&Handle);
|
2005-11-09 11:57:58 +00:00
|
|
|
|
if (!NT_SUCCESS(Status)) return mtNone;
|
|
|
|
|
|
|
|
|
|
/* Reset UART */
|
2008-01-07 12:57:32 +00:00
|
|
|
|
TRACE_(SERMOUSE, "Reset UART\n");
|
2005-11-09 11:57:58 +00:00
|
|
|
|
Mcr = 0; /* MCR: DTR/RTS/OUT2 off */
|
2006-04-23 08:46:25 +00:00
|
|
|
|
Status = DeviceIoControl(LowerDevice, IOCTL_SERIAL_SET_MODEM_CONTROL,
|
2005-11-09 11:57:58 +00:00
|
|
|
|
&Mcr, sizeof(Mcr), NULL, NULL);
|
|
|
|
|
if (!NT_SUCCESS(Status)) goto ByeBye;
|
|
|
|
|
|
|
|
|
|
/* Set communications parameters */
|
2008-01-07 12:57:32 +00:00
|
|
|
|
TRACE_(SERMOUSE, "Set communications parameters\n");
|
2005-11-09 11:57:58 +00:00
|
|
|
|
/* DLAB off */
|
|
|
|
|
Fcr = 0;
|
2006-04-23 08:46:25 +00:00
|
|
|
|
Status = DeviceIoControl(LowerDevice, IOCTL_SERIAL_SET_FIFO_CONTROL,
|
2005-11-09 11:57:58 +00:00
|
|
|
|
&Fcr, sizeof(Fcr), NULL, NULL);
|
|
|
|
|
if (!NT_SUCCESS(Status)) goto ByeBye;
|
|
|
|
|
/* Set serial port speed */
|
2005-12-18 23:00:00 +00:00
|
|
|
|
BaudRate = 1200;
|
2006-04-23 08:46:25 +00:00
|
|
|
|
Status = DeviceIoControl(LowerDevice, IOCTL_SERIAL_SET_BAUD_RATE,
|
2005-11-09 11:57:58 +00:00
|
|
|
|
&BaudRate, sizeof(BaudRate), NULL, NULL);
|
|
|
|
|
if (!NT_SUCCESS(Status)) goto ByeBye;
|
|
|
|
|
/* Set LCR */
|
|
|
|
|
LCR.WordLength = 7;
|
|
|
|
|
LCR.Parity = NO_PARITY;
|
|
|
|
|
LCR.StopBits = STOP_BITS_2;
|
2006-04-23 08:46:25 +00:00
|
|
|
|
Status = DeviceIoControl(LowerDevice, IOCTL_SERIAL_SET_LINE_CONTROL,
|
2005-11-09 11:57:58 +00:00
|
|
|
|
&LCR, sizeof(LCR), NULL, NULL);
|
|
|
|
|
if (!NT_SUCCESS(Status)) goto ByeBye;
|
|
|
|
|
|
|
|
|
|
/* Flush receive buffer */
|
2008-01-07 12:57:32 +00:00
|
|
|
|
TRACE_(SERMOUSE, "Flush receive buffer\n");
|
2005-11-09 11:57:58 +00:00
|
|
|
|
Command = SERIAL_PURGE_RXCLEAR;
|
2006-04-23 08:46:25 +00:00
|
|
|
|
Status = DeviceIoControl(LowerDevice, IOCTL_SERIAL_SET_MODEM_CONTROL,
|
2005-11-09 11:57:58 +00:00
|
|
|
|
&Command, sizeof(Command), NULL, NULL);
|
|
|
|
|
if (!NT_SUCCESS(Status)) goto ByeBye;
|
|
|
|
|
/* Wait 100 ms */
|
2006-04-23 08:46:25 +00:00
|
|
|
|
Wait(100);
|
2005-11-09 11:57:58 +00:00
|
|
|
|
|
|
|
|
|
/* Enable DTR/RTS */
|
2008-01-07 12:57:32 +00:00
|
|
|
|
TRACE_(SERMOUSE, "Enable DTR/RTS\n");
|
2006-04-23 08:46:25 +00:00
|
|
|
|
Status = DeviceIoControl(LowerDevice, IOCTL_SERIAL_SET_DTR,
|
2005-11-09 11:57:58 +00:00
|
|
|
|
NULL, 0, NULL, NULL);
|
|
|
|
|
if (!NT_SUCCESS(Status)) goto ByeBye;
|
2006-04-23 08:46:25 +00:00
|
|
|
|
Status = DeviceIoControl(LowerDevice, IOCTL_SERIAL_SET_RTS,
|
2005-11-09 11:57:58 +00:00
|
|
|
|
NULL, 0, NULL, NULL);
|
|
|
|
|
if (!NT_SUCCESS(Status)) goto ByeBye;
|
|
|
|
|
|
|
|
|
|
/* Set timeout to 500 microseconds */
|
2008-01-07 12:57:32 +00:00
|
|
|
|
TRACE_(SERMOUSE, "Set timeout to 500 microseconds\n");
|
2006-04-23 08:46:25 +00:00
|
|
|
|
Timeouts.ReadIntervalTimeout = 100;
|
2005-11-09 11:57:58 +00:00
|
|
|
|
Timeouts.ReadTotalTimeoutMultiplier = 0;
|
|
|
|
|
Timeouts.ReadTotalTimeoutConstant = 500;
|
|
|
|
|
Timeouts.WriteTotalTimeoutMultiplier = Timeouts.WriteTotalTimeoutConstant = 0;
|
2006-04-23 08:46:25 +00:00
|
|
|
|
Status = DeviceIoControl(LowerDevice, IOCTL_SERIAL_SET_TIMEOUTS,
|
2005-11-09 11:57:58 +00:00
|
|
|
|
&Timeouts, sizeof(Timeouts), NULL, NULL);
|
|
|
|
|
if (!NT_SUCCESS(Status)) goto ByeBye;
|
|
|
|
|
|
|
|
|
|
/* Fill the read buffer */
|
2008-01-07 12:57:32 +00:00
|
|
|
|
TRACE_(SERMOUSE, "Fill the read buffer\n");
|
2005-11-09 11:57:58 +00:00
|
|
|
|
Status = ReadBytes(LowerDevice, Buffer, sizeof(Buffer)/sizeof(Buffer[0]), &Count);
|
|
|
|
|
if (!NT_SUCCESS(Status)) goto ByeBye;
|
|
|
|
|
|
|
|
|
|
for (i = 0; i < Count; i++)
|
|
|
|
|
{
|
|
|
|
|
if (Buffer[i] == 'B')
|
|
|
|
|
{
|
|
|
|
|
/* Sign for Microsoft Ballpoint */
|
2007-12-28 21:10:07 +00:00
|
|
|
|
ERR_(SERMOUSE, "Microsoft Ballpoint device detected. THIS DEVICE IS NOT YET SUPPORTED");
|
2005-11-09 11:57:58 +00:00
|
|
|
|
MouseType = mtNone;
|
|
|
|
|
goto ByeBye;
|
|
|
|
|
}
|
|
|
|
|
else if (Buffer[i] == 'M')
|
|
|
|
|
{
|
|
|
|
|
/* Sign for Microsoft Mouse protocol followed by button specifier */
|
|
|
|
|
if (i == sizeof(Buffer) - 1)
|
|
|
|
|
{
|
|
|
|
|
/* Overflow Error */
|
|
|
|
|
goto ByeBye;
|
|
|
|
|
}
|
|
|
|
|
switch (Buffer[i + 1])
|
|
|
|
|
{
|
|
|
|
|
case '3':
|
2007-12-28 21:10:07 +00:00
|
|
|
|
INFO_(SERMOUSE, "Microsoft Mouse with 3-buttons detected\n");
|
2005-11-09 11:57:58 +00:00
|
|
|
|
MouseType = mtLogitech;
|
2009-01-05 20:36:39 +00:00
|
|
|
|
break;
|
2005-11-09 11:57:58 +00:00
|
|
|
|
case 'Z':
|
2007-12-28 21:10:07 +00:00
|
|
|
|
INFO_(SERMOUSE, "Microsoft Wheel Mouse detected\n");
|
2005-11-09 11:57:58 +00:00
|
|
|
|
MouseType = mtWheelZ;
|
2009-01-05 20:36:39 +00:00
|
|
|
|
break;
|
2005-11-09 11:57:58 +00:00
|
|
|
|
default:
|
2007-12-28 21:10:07 +00:00
|
|
|
|
INFO_(SERMOUSE, "Microsoft Mouse with 2-buttons detected\n");
|
2005-11-09 11:57:58 +00:00
|
|
|
|
MouseType = mtMicrosoft;
|
2009-01-05 20:36:39 +00:00
|
|
|
|
break;
|
2005-11-09 11:57:58 +00:00
|
|
|
|
}
|
|
|
|
|
goto ByeBye;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ByeBye:
|
|
|
|
|
/* Close port */
|
2006-06-30 21:47:42 +00:00
|
|
|
|
if (Handle)
|
|
|
|
|
ZwClose(Handle);
|
2005-11-09 11:57:58 +00:00
|
|
|
|
return MouseType;
|
|
|
|
|
}
|