2007-10-31 08:58:04 +00:00
|
|
|
|
/*
|
|
|
|
|
* PROJECT: ReactOS i8042 (ps/2 keyboard-mouse controller) driver
|
|
|
|
|
* LICENSE: GPL - See COPYING in the top level directory
|
|
|
|
|
* FILE: drivers/input/i8042prt/readwrite.c
|
|
|
|
|
* PURPOSE: Read/write port functions
|
|
|
|
|
* PROGRAMMERS: Copyright Victor Kirhenshtein (sauros@iname.com)
|
|
|
|
|
Copyright Jason Filby (jasonfilby@yahoo.com)
|
|
|
|
|
Copyright Martijn Vernooij (o112w8r02@sneakemail.com)
|
|
|
|
|
Copyright 2006-2007 Herv<EFBFBD> Poussineau (hpoussin@reactos.org)
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
/* INCLUDES ******************************************************************/
|
|
|
|
|
|
|
|
|
|
#include "i8042prt.h"
|
|
|
|
|
|
2014-01-02 11:11:12 +00:00
|
|
|
|
#include <debug.h>
|
|
|
|
|
|
2007-10-31 08:58:04 +00:00
|
|
|
|
/* FUNCTIONS *****************************************************************/
|
|
|
|
|
|
|
|
|
|
VOID
|
2009-02-11 17:21:10 +00:00
|
|
|
|
i8042Flush(
|
|
|
|
|
IN PPORT_DEVICE_EXTENSION DeviceExtension)
|
2007-10-31 08:58:04 +00:00
|
|
|
|
{
|
2009-02-11 17:21:10 +00:00
|
|
|
|
UCHAR Ignore;
|
|
|
|
|
|
|
|
|
|
/* Flush output buffer */
|
|
|
|
|
while (NT_SUCCESS(i8042ReadData(DeviceExtension, KBD_OBF /* | MOU_OBF*/, &Ignore))) {
|
|
|
|
|
KeStallExecutionProcessor(50);
|
|
|
|
|
TRACE_(I8042PRT, "Output data flushed\n");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Flush input buffer */
|
|
|
|
|
while (NT_SUCCESS(i8042ReadData(DeviceExtension, KBD_IBF, &Ignore))) {
|
|
|
|
|
KeStallExecutionProcessor(50);
|
|
|
|
|
TRACE_(I8042PRT, "Input data flushed\n");
|
|
|
|
|
}
|
2007-10-31 08:58:04 +00:00
|
|
|
|
}
|
|
|
|
|
|
2008-03-31 12:40:31 +00:00
|
|
|
|
BOOLEAN
|
2009-02-11 17:21:10 +00:00
|
|
|
|
i8042IsrWritePort(
|
|
|
|
|
IN PPORT_DEVICE_EXTENSION DeviceExtension,
|
|
|
|
|
IN UCHAR Value,
|
|
|
|
|
IN UCHAR SelectCmd OPTIONAL)
|
2007-10-31 08:58:04 +00:00
|
|
|
|
{
|
2009-02-11 17:21:10 +00:00
|
|
|
|
if (SelectCmd)
|
|
|
|
|
if (!i8042Write(DeviceExtension, DeviceExtension->ControlPort, SelectCmd))
|
|
|
|
|
return FALSE;
|
2007-10-31 08:58:04 +00:00
|
|
|
|
|
2009-02-11 17:21:10 +00:00
|
|
|
|
return i8042Write(DeviceExtension, DeviceExtension->DataPort, Value);
|
2007-10-31 08:58:04 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
2009-02-11 17:21:10 +00:00
|
|
|
|
* FUNCTION: Read data from port 0x60
|
|
|
|
|
*/
|
2007-10-31 08:58:04 +00:00
|
|
|
|
NTSTATUS
|
2009-02-11 17:21:10 +00:00
|
|
|
|
i8042ReadData(
|
|
|
|
|
IN PPORT_DEVICE_EXTENSION DeviceExtension,
|
|
|
|
|
IN UCHAR StatusFlags,
|
|
|
|
|
OUT PUCHAR Data)
|
2007-10-31 08:58:04 +00:00
|
|
|
|
{
|
2009-02-11 17:21:10 +00:00
|
|
|
|
UCHAR PortStatus;
|
|
|
|
|
NTSTATUS Status;
|
|
|
|
|
|
|
|
|
|
Status = i8042ReadStatus(DeviceExtension, &PortStatus);
|
|
|
|
|
if (!NT_SUCCESS(Status))
|
|
|
|
|
return Status;
|
|
|
|
|
|
|
|
|
|
// If data is available
|
|
|
|
|
if (PortStatus & StatusFlags)
|
|
|
|
|
{
|
|
|
|
|
*Data = READ_PORT_UCHAR(DeviceExtension->DataPort);
|
|
|
|
|
INFO_(I8042PRT, "Read: 0x%02x (status: 0x%x)\n", Data[0], PortStatus);
|
|
|
|
|
|
|
|
|
|
// If the data is valid (not timeout, not parity error)
|
|
|
|
|
if ((PortStatus & KBD_PERR) == 0)
|
|
|
|
|
return STATUS_SUCCESS;
|
|
|
|
|
}
|
|
|
|
|
return STATUS_UNSUCCESSFUL;
|
2007-10-31 08:58:04 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
NTSTATUS
|
2009-02-11 17:21:10 +00:00
|
|
|
|
i8042ReadStatus(
|
|
|
|
|
IN PPORT_DEVICE_EXTENSION DeviceExtension,
|
|
|
|
|
OUT PUCHAR Status)
|
2007-10-31 08:58:04 +00:00
|
|
|
|
{
|
2009-02-11 17:21:10 +00:00
|
|
|
|
ASSERT(DeviceExtension->ControlPort != NULL);
|
|
|
|
|
*Status = READ_PORT_UCHAR(DeviceExtension->ControlPort);
|
|
|
|
|
return STATUS_SUCCESS;
|
2007-10-31 08:58:04 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
2009-02-11 17:21:10 +00:00
|
|
|
|
* FUNCTION: Read data from data port
|
|
|
|
|
*/
|
2007-10-31 08:58:04 +00:00
|
|
|
|
NTSTATUS
|
2009-02-11 17:21:10 +00:00
|
|
|
|
i8042ReadDataWait(
|
|
|
|
|
IN PPORT_DEVICE_EXTENSION DeviceExtension,
|
|
|
|
|
OUT PUCHAR Data)
|
2007-10-31 08:58:04 +00:00
|
|
|
|
{
|
2009-02-11 17:21:10 +00:00
|
|
|
|
ULONG Counter;
|
|
|
|
|
NTSTATUS Status;
|
2007-10-31 08:58:04 +00:00
|
|
|
|
|
2009-02-11 17:21:10 +00:00
|
|
|
|
Counter = DeviceExtension->Settings.PollingIterations;
|
2007-10-31 08:58:04 +00:00
|
|
|
|
|
2009-02-11 17:21:10 +00:00
|
|
|
|
while (Counter--)
|
|
|
|
|
{
|
|
|
|
|
Status = i8042ReadKeyboardData(DeviceExtension, Data);
|
2007-10-31 08:58:04 +00:00
|
|
|
|
|
2009-02-11 17:21:10 +00:00
|
|
|
|
if (NT_SUCCESS(Status))
|
|
|
|
|
return Status;
|
2007-10-31 08:58:04 +00:00
|
|
|
|
|
2009-02-11 17:21:10 +00:00
|
|
|
|
KeStallExecutionProcessor(50);
|
|
|
|
|
}
|
2007-10-31 08:58:04 +00:00
|
|
|
|
|
2009-02-11 17:21:10 +00:00
|
|
|
|
/* Timed out */
|
|
|
|
|
return STATUS_IO_TIMEOUT;
|
2007-10-31 08:58:04 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
2009-02-11 17:21:10 +00:00
|
|
|
|
* This one reads a value from the port; You don't have to specify
|
|
|
|
|
* which one, it'll always be from the one you talked to, so one function
|
|
|
|
|
* is enough this time. Note how MSDN specifies the
|
|
|
|
|
* WaitForAck parameter to be ignored.
|
|
|
|
|
*/
|
2007-11-22 11:27:17 +00:00
|
|
|
|
NTSTATUS NTAPI
|
2009-02-11 17:21:10 +00:00
|
|
|
|
i8042SynchReadPort(
|
|
|
|
|
IN PVOID Context,
|
|
|
|
|
OUT PUCHAR Value,
|
|
|
|
|
IN BOOLEAN WaitForAck)
|
2007-10-31 08:58:04 +00:00
|
|
|
|
{
|
2009-02-11 17:21:10 +00:00
|
|
|
|
PPORT_DEVICE_EXTENSION DeviceExtension;
|
2007-10-31 08:58:04 +00:00
|
|
|
|
|
2009-06-19 00:17:15 +00:00
|
|
|
|
UNREFERENCED_PARAMETER(WaitForAck);
|
|
|
|
|
|
2009-02-11 17:21:10 +00:00
|
|
|
|
DeviceExtension = (PPORT_DEVICE_EXTENSION)Context;
|
2007-10-31 08:58:04 +00:00
|
|
|
|
|
2009-02-11 17:21:10 +00:00
|
|
|
|
return i8042ReadDataWait(DeviceExtension, Value);
|
2007-10-31 08:58:04 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
2009-02-11 17:21:10 +00:00
|
|
|
|
* These functions are callbacks for filter driver custom
|
|
|
|
|
* initialization routines.
|
|
|
|
|
*/
|
2007-10-31 08:58:04 +00:00
|
|
|
|
NTSTATUS NTAPI
|
2009-02-11 17:21:10 +00:00
|
|
|
|
i8042SynchWritePort(
|
|
|
|
|
IN PPORT_DEVICE_EXTENSION DeviceExtension,
|
|
|
|
|
IN UCHAR Port,
|
|
|
|
|
IN UCHAR Value,
|
|
|
|
|
IN BOOLEAN WaitForAck)
|
2007-10-31 08:58:04 +00:00
|
|
|
|
{
|
2009-02-11 17:21:10 +00:00
|
|
|
|
NTSTATUS Status;
|
|
|
|
|
UCHAR Ack;
|
|
|
|
|
ULONG ResendIterations;
|
|
|
|
|
|
|
|
|
|
ResendIterations = DeviceExtension->Settings.ResendIterations + 1;
|
|
|
|
|
|
|
|
|
|
do
|
|
|
|
|
{
|
|
|
|
|
if (Port)
|
|
|
|
|
if (!i8042Write(DeviceExtension, DeviceExtension->DataPort, Port))
|
|
|
|
|
{
|
|
|
|
|
WARN_(I8042PRT, "Failed to write Port\n");
|
|
|
|
|
return STATUS_IO_TIMEOUT;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!i8042Write(DeviceExtension, DeviceExtension->DataPort, Value))
|
|
|
|
|
{
|
|
|
|
|
WARN_(I8042PRT, "Failed to write Value\n");
|
|
|
|
|
return STATUS_IO_TIMEOUT;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (WaitForAck)
|
|
|
|
|
{
|
|
|
|
|
Status = i8042ReadDataWait(DeviceExtension, &Ack);
|
|
|
|
|
if (!NT_SUCCESS(Status))
|
|
|
|
|
{
|
|
|
|
|
WARN_(I8042PRT, "Failed to read Ack\n");
|
|
|
|
|
return Status;
|
|
|
|
|
}
|
|
|
|
|
if (Ack == KBD_ACK)
|
|
|
|
|
return STATUS_SUCCESS;
|
|
|
|
|
else if (Ack == KBD_RESEND)
|
|
|
|
|
INFO_(I8042PRT, "i8042 asks for a data resend\n");
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
return STATUS_SUCCESS;
|
|
|
|
|
}
|
|
|
|
|
TRACE_(I8042PRT, "Reiterating\n");
|
|
|
|
|
ResendIterations--;
|
|
|
|
|
} while (ResendIterations);
|
|
|
|
|
|
|
|
|
|
return STATUS_IO_TIMEOUT;
|
2007-10-31 08:58:04 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
2009-02-11 17:21:10 +00:00
|
|
|
|
* FUNCTION: Write data to a port, waiting first for it to become ready
|
|
|
|
|
*/
|
2007-10-31 08:58:04 +00:00
|
|
|
|
BOOLEAN
|
2009-02-11 17:21:10 +00:00
|
|
|
|
i8042Write(
|
|
|
|
|
IN PPORT_DEVICE_EXTENSION DeviceExtension,
|
|
|
|
|
IN PUCHAR addr,
|
|
|
|
|
IN UCHAR data)
|
2007-10-31 08:58:04 +00:00
|
|
|
|
{
|
2009-02-11 17:21:10 +00:00
|
|
|
|
ULONG Counter;
|
|
|
|
|
|
|
|
|
|
ASSERT(addr);
|
|
|
|
|
ASSERT(DeviceExtension->ControlPort != NULL);
|
|
|
|
|
|
|
|
|
|
Counter = DeviceExtension->Settings.PollingIterations;
|
|
|
|
|
|
|
|
|
|
while ((KBD_IBF & READ_PORT_UCHAR(DeviceExtension->ControlPort)) &&
|
|
|
|
|
(Counter--))
|
|
|
|
|
{
|
|
|
|
|
KeStallExecutionProcessor(50);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (Counter)
|
|
|
|
|
{
|
|
|
|
|
WRITE_PORT_UCHAR(addr, data);
|
|
|
|
|
INFO_(I8042PRT, "Sent 0x%x to port %p\n", data, addr);
|
|
|
|
|
return TRUE;
|
|
|
|
|
}
|
|
|
|
|
return FALSE;
|
2007-10-31 08:58:04 +00:00
|
|
|
|
}
|