2019-01-11 02:28:53 +01:00
|
|
|
#include "PS2MouseDevice.h"
|
|
|
|
#include "IO.h"
|
|
|
|
|
2019-01-12 03:20:48 +01:00
|
|
|
//#define PS2MOUSE_DEBUG
|
|
|
|
|
2019-01-11 03:52:09 +01:00
|
|
|
static PS2MouseDevice* s_the;
|
|
|
|
|
2019-01-11 02:28:53 +01:00
|
|
|
PS2MouseDevice::PS2MouseDevice()
|
|
|
|
: IRQHandler(12)
|
|
|
|
, CharacterDevice(10, 1)
|
|
|
|
{
|
2019-01-11 03:52:09 +01:00
|
|
|
s_the = this;
|
2019-01-11 02:28:53 +01:00
|
|
|
initialize();
|
|
|
|
}
|
|
|
|
|
|
|
|
PS2MouseDevice::~PS2MouseDevice()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2019-01-11 03:52:09 +01:00
|
|
|
PS2MouseDevice& PS2MouseDevice::the()
|
|
|
|
{
|
|
|
|
return *s_the;
|
|
|
|
}
|
|
|
|
|
2019-01-11 02:28:53 +01:00
|
|
|
void PS2MouseDevice::handle_irq()
|
|
|
|
{
|
2019-01-11 02:45:06 +01:00
|
|
|
byte data = IO::in8(0x60);
|
|
|
|
m_data[m_data_state] = data;
|
2019-01-11 02:28:53 +01:00
|
|
|
switch (m_data_state) {
|
|
|
|
case 0:
|
2019-01-11 02:45:06 +01:00
|
|
|
ASSERT(data & 0x08);
|
|
|
|
++m_data_state;
|
|
|
|
break;
|
2019-01-11 02:28:53 +01:00
|
|
|
case 1:
|
|
|
|
++m_data_state;
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
m_data_state = 0;
|
2019-01-12 03:20:48 +01:00
|
|
|
#ifdef PS2MOUSE_DEBUG
|
2019-01-11 02:45:06 +01:00
|
|
|
dbgprintf("PS2Mouse: %d, %d %s %s\n",
|
|
|
|
m_data[1],
|
|
|
|
m_data[2],
|
|
|
|
(m_data[0] & 1) ? "Left" : "",
|
|
|
|
(m_data[0] & 2) ? "Right" : ""
|
|
|
|
);
|
2019-01-12 03:20:48 +01:00
|
|
|
#endif
|
2019-01-12 05:20:56 +01:00
|
|
|
m_buffer.write((const byte*)m_data, 3);
|
2019-01-11 02:28:53 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void PS2MouseDevice::wait_then_write(byte port, byte data)
|
|
|
|
{
|
|
|
|
prepare_for_output();
|
|
|
|
IO::out8(port, data);
|
|
|
|
}
|
|
|
|
|
|
|
|
byte PS2MouseDevice::wait_then_read(byte port)
|
|
|
|
{
|
|
|
|
prepare_for_input();
|
|
|
|
return IO::in8(port);
|
|
|
|
}
|
|
|
|
|
|
|
|
void PS2MouseDevice::initialize()
|
|
|
|
{
|
|
|
|
// Enable PS aux port
|
|
|
|
wait_then_write(0x64, 0xa8);
|
|
|
|
|
|
|
|
// Enable interrupts
|
|
|
|
wait_then_write(0x64, 0x20);
|
|
|
|
|
|
|
|
byte status = wait_then_read(0x60) | 2;
|
|
|
|
wait_then_write(0x64, 0x60);
|
|
|
|
wait_then_write(0x60, status);
|
|
|
|
|
|
|
|
// Set default settings.
|
|
|
|
mouse_write(0xf6);
|
|
|
|
byte ack1 = mouse_read();
|
|
|
|
ASSERT(ack1 == 0xfa);
|
|
|
|
|
|
|
|
// Enable.
|
|
|
|
mouse_write(0xf4);
|
|
|
|
byte ack2 = mouse_read();
|
|
|
|
ASSERT(ack2 == 0xfa);
|
|
|
|
|
|
|
|
enable_irq();
|
|
|
|
}
|
|
|
|
|
|
|
|
void PS2MouseDevice::prepare_for_input()
|
|
|
|
{
|
|
|
|
for (;;) {
|
|
|
|
if (IO::in8(0x64) & 1)
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void PS2MouseDevice::prepare_for_output()
|
|
|
|
{
|
|
|
|
for (;;) {
|
|
|
|
if (!(IO::in8(0x64) & 2))
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void PS2MouseDevice::mouse_write(byte data)
|
|
|
|
{
|
|
|
|
prepare_for_output();
|
|
|
|
IO::out8(0x64, 0xd4);
|
|
|
|
prepare_for_output();
|
|
|
|
IO::out8(0x60, data);
|
|
|
|
}
|
|
|
|
|
|
|
|
byte PS2MouseDevice::mouse_read()
|
|
|
|
{
|
|
|
|
prepare_for_input();
|
|
|
|
return IO::in8(0x60);
|
|
|
|
}
|
|
|
|
|
2019-01-14 14:21:51 +01:00
|
|
|
bool PS2MouseDevice::has_data_available_for_reading(Process&) const
|
2019-01-11 02:28:53 +01:00
|
|
|
{
|
2019-01-12 05:20:56 +01:00
|
|
|
return !m_buffer.is_empty();
|
2019-01-11 02:28:53 +01:00
|
|
|
}
|
|
|
|
|
2019-01-12 05:20:56 +01:00
|
|
|
ssize_t PS2MouseDevice::read(byte* buffer, size_t size)
|
2019-01-11 02:28:53 +01:00
|
|
|
{
|
2019-01-12 05:20:56 +01:00
|
|
|
return m_buffer.read(buffer, size);
|
2019-01-11 02:28:53 +01:00
|
|
|
}
|
|
|
|
|
2019-01-12 05:20:56 +01:00
|
|
|
ssize_t PS2MouseDevice::write(const byte*, size_t)
|
2019-01-11 02:28:53 +01:00
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|