Kernel: Add I8042Controller to detect and manage PS/2 devices

Rework the PS/2 keyboard and mouse drivers to use a common 8042
controller driver. Also, reset and reconfigure the 8042 controller
as they are not guaranteed to be in the state that we expect.
This commit is contained in:
Tom 2020-11-07 12:09:28 -07:00 committed by Andreas Kling
parent e1c27c16d8
commit 91db31880f
Notes: sideshowbarker 2024-07-19 01:25:22 +09:00
8 changed files with 617 additions and 261 deletions

View file

@ -22,6 +22,7 @@ set(KERNEL_SOURCES
Devices/EBRPartitionTable.cpp
Devices/FullDevice.cpp
Devices/GPTPartitionTable.cpp
Devices/I8042Controller.cpp
Devices/KeyboardDevice.cpp
Devices/MBRPartitionTable.cpp
Devices/MBVGADevice.cpp

View file

@ -0,0 +1,272 @@
/*
* Copyright (c) 2020, The SerenityOS developers.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <Kernel/ACPI/Parser.h>
#include <Kernel/Devices/KeyboardDevice.h>
#include <Kernel/Devices/PS2MouseDevice.h>
#include <Kernel/IO.h>
namespace Kernel {
static I8042Controller* s_the;
void I8042Controller::initialize()
{
if (ACPI::Parser::the()->have_8042())
new I8042Controller;
}
I8042Controller& I8042Controller::the()
{
ASSERT(s_the);
return *s_the;
}
I8042Controller::I8042Controller()
{
ASSERT(!s_the);
s_the = this;
u8 configuration;
{
ScopedSpinLock lock(m_lock);
// Disable devices
do_wait_then_write(I8042_STATUS, 0xad);
do_wait_then_write(I8042_STATUS, 0xa7); // ignored if it doesn't exist
// Drain buffers
do_drain();
do_wait_then_write(I8042_STATUS, 0x20);
configuration = do_wait_then_read(I8042_BUFFER);
do_wait_then_write(I8042_STATUS, 0x60);
configuration &= ~3; // Disable IRQs for all
do_wait_then_write(I8042_BUFFER, configuration);
m_is_dual_channel = (configuration & (1 << 5)) != 0;
dbg() << "I8042: " << (m_is_dual_channel ? "Dual" : "Single") << " channel controller";
// Perform controller self-test
do_wait_then_write(I8042_STATUS, 0xaa);
if (do_wait_then_read(I8042_BUFFER) == 0x55) {
// Restore configuration in case the controller reset
do_wait_then_write(I8042_STATUS, 0x60);
do_wait_then_write(I8042_BUFFER, configuration);
} else {
dbg() << "I8042: Controller self test failed";
}
// Test ports and enable them if available
do_wait_then_write(I8042_STATUS, 0xab); // test
m_devices[0].available = (do_wait_then_read(I8042_BUFFER) == 0);
if (m_devices[0].available) {
do_wait_then_write(I8042_STATUS, 0xae); //enable
configuration |= 1;
configuration &= ~(1 << 4);
} else {
dbg() << "I8042: Keyboard port not available";
}
if (m_is_dual_channel) {
do_wait_then_write(I8042_STATUS, 0xa9); // test
m_devices[1].available = (do_wait_then_read(I8042_BUFFER) == 0);
if (m_devices[1].available) {
do_wait_then_write(I8042_STATUS, 0xa8); // enable
configuration |= 2;
configuration &= ~(1 << 5);
} else {
dbg() << "I8042: Mouse port not available";
}
}
// Enable IRQs for the ports that are usable
if (m_devices[0].available || m_devices[1].available) {
configuration &= ~0x30; // renable clocks
do_wait_then_write(I8042_STATUS, 0x60);
do_wait_then_write(I8042_BUFFER, configuration);
}
}
// Try to detect and initialize the devices
if (m_devices[0].available) {
if (KeyboardDevice::the().initialize()) {
m_devices[0].device = &KeyboardDevice::the();
} else {
dbg() << "I8042: Keyboard device failed to initialize, disable";
m_devices[0].available = false;
configuration &= ~1;
configuration |= 1 << 4;
ScopedSpinLock lock(m_lock);
do_wait_then_write(I8042_STATUS, 0x60);
do_wait_then_write(I8042_BUFFER, configuration);
}
}
if (m_devices[1].available) {
if (PS2MouseDevice::the().initialize()) {
m_devices[1].device = &PS2MouseDevice::the();
} else {
dbg() << "I8042: Mouse device failed to initialize, disable";
m_devices[1].available = false;
configuration |= 1 << 5;
ScopedSpinLock lock(m_lock);
do_wait_then_write(I8042_STATUS, 0x60);
do_wait_then_write(I8042_BUFFER, configuration);
}
}
// Enable IRQs after both are detected and initialized
if (m_devices[0].device)
m_devices[0].device->enable_interrupts();
if (m_devices[1].device)
m_devices[1].device->enable_interrupts();
}
void I8042Controller::irq_process_input_buffer(Device)
{
ASSERT(Processor::current().in_irq());
u8 status = IO::in8(I8042_STATUS);
if (!(status & I8042_BUFFER_FULL))
return;
Device data_for_device = ((status & I8042_WHICH_BUFFER) == I8042_MOUSE_BUFFER) ? Device::Mouse : Device::Keyboard;
u8 byte = IO::in8(I8042_BUFFER);
if (auto* device = m_devices[data_for_device == Device::Keyboard ? 0 : 1].device)
device->irq_handle_byte_read(byte);
}
void I8042Controller::do_drain()
{
for (;;) {
u8 status = IO::in8(I8042_STATUS);
if (!(status & I8042_BUFFER_FULL))
return;
IO::in8(I8042_BUFFER);
}
}
bool I8042Controller::do_reset_device(Device device)
{
ASSERT(device != Device::None);
ASSERT(m_lock.is_locked());
ASSERT(!Processor::current().in_irq());
if (do_send_command(device, 0xff) != I8042_ACK)
return false;
// Wait until we get the self-test result
return do_wait_then_read(I8042_BUFFER) == 0xaa;
}
u8 I8042Controller::do_send_command(Device device, u8 command)
{
ASSERT(device != Device::None);
ASSERT(m_lock.is_locked());
ASSERT(!Processor::current().in_irq());
return do_write_to_device(device, command);
}
u8 I8042Controller::do_send_command(Device device, u8 command, u8 data)
{
ASSERT(device != Device::None);
ASSERT(m_lock.is_locked());
ASSERT(!Processor::current().in_irq());
u8 response = do_write_to_device(device, command);
if (response == I8042_ACK)
response = do_write_to_device(device, data);
return response;
}
u8 I8042Controller::do_write_to_device(Device device, u8 data)
{
ASSERT(device != Device::None);
ASSERT(m_lock.is_locked());
ASSERT(!Processor::current().in_irq());
int attempts = 0;
u8 response;
do {
if (device != Device::Keyboard) {
prepare_for_output();
IO::out8(I8042_STATUS, 0xd4);
}
prepare_for_output();
IO::out8(I8042_BUFFER, data);
response = do_wait_then_read(I8042_BUFFER);
} while (response == I8042_RESEND && ++attempts < 3);
if (attempts >= 3)
dbg() << "Failed to write byte to device, gave up";
return response;
}
u8 I8042Controller::do_read_from_device(Device device)
{
ASSERT(device != Device::None);
prepare_for_input(device);
return IO::in8(I8042_BUFFER);
}
void I8042Controller::prepare_for_input(Device device)
{
ASSERT(m_lock.is_locked());
const u8 buffer_type = device == Device::Keyboard ? I8042_KEYBOARD_BUFFER : I8042_MOUSE_BUFFER;
for (;;) {
u8 status = IO::in8(I8042_STATUS);
if ((status & I8042_BUFFER_FULL) && (device == Device::None || ((status & I8042_WHICH_BUFFER) == buffer_type)))
return;
}
}
void I8042Controller::prepare_for_output()
{
ASSERT(m_lock.is_locked());
for (;;) {
if (!(IO::in8(I8042_STATUS) & 2))
return;
}
}
void I8042Controller::do_wait_then_write(u8 port, u8 data)
{
ASSERT(m_lock.is_locked());
prepare_for_output();
IO::out8(port, data);
}
u8 I8042Controller::do_wait_then_read(u8 port)
{
ASSERT(m_lock.is_locked());
prepare_for_input(Device::None);
return IO::in8(port);
}
}

View file

@ -0,0 +1,130 @@
/*
* Copyright (c) 2020, The SerenityOS developers.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#pragma once
#include <Kernel/SpinLock.h>
namespace Kernel {
#define I8042_BUFFER 0x60
#define I8042_STATUS 0x64
#define I8042_ACK 0xFA
#define I8042_RESEND 0xFE
#define I8042_BUFFER_FULL 0x01
#define I8042_WHICH_BUFFER 0x20
#define I8042_KEYBOARD_BUFFER 0x00
#define I8042_MOUSE_BUFFER 0x20
class I8042Device {
public:
virtual ~I8042Device() { }
virtual void irq_handle_byte_read(u8 byte) = 0;
virtual void enable_interrupts() = 0;
};
class I8042Controller {
public:
enum class Device {
None = 0,
Keyboard,
Mouse
};
static void initialize();
static I8042Controller& the();
bool reset_device(Device device)
{
ScopedSpinLock lock(m_lock);
return do_reset_device(device);
}
u8 send_command(Device device, u8 command)
{
ScopedSpinLock lock(m_lock);
return do_send_command(device, command);
}
u8 send_command(Device device, u8 command, u8 data)
{
ScopedSpinLock lock(m_lock);
return do_send_command(device, command, data);
}
u8 read_from_device(Device device)
{
ScopedSpinLock lock(m_lock);
return do_read_from_device(device);
}
void wait_then_write(u8 port, u8 data)
{
ScopedSpinLock lock(m_lock);
do_wait_then_write(port, data);
}
u8 wait_then_read(u8 port)
{
ScopedSpinLock lock(m_lock);
return do_wait_then_read(port);
}
void prepare_for_output();
void prepare_for_input(Device);
void irq_process_input_buffer(Device);
private:
I8042Controller();
void do_drain();
bool do_reset_device(Device device);
u8 do_send_command(Device device, u8 data);
u8 do_send_command(Device device, u8 command, u8 data);
u8 do_write_to_device(Device device, u8 data);
u8 do_read_from_device(Device device);
void do_wait_then_write(u8 port, u8 data);
u8 do_wait_then_read(u8 port);
static int device_to_deviceinfo_index(Device device)
{
ASSERT(device != Device::None);
return (device == Device::Keyboard) ? 0 : 1;
}
struct DeviceInfo {
I8042Device* device { nullptr };
bool available { false };
};
SpinLock<u8> m_lock;
bool m_is_dual_channel { false };
DeviceInfo m_devices[2];
};
}

View file

@ -39,13 +39,6 @@
namespace Kernel {
#define IRQ_KEYBOARD 1
#define I8042_BUFFER 0x60
#define I8042_STATUS 0x64
#define I8042_ACK 0xFA
#define I8042_BUFFER_FULL 0x01
#define I8042_WHICH_BUFFER 0x20
#define I8042_MOUSE_BUFFER 0x20
#define I8042_KEYBOARD_BUFFER 0x00
static const KeyCode unshifted_key_map[0x80] = {
Key_Invalid,
@ -273,76 +266,75 @@ void KeyboardDevice::key_state_changed(u8 scan_code, bool pressed)
if (m_client)
m_client->on_key_pressed(event);
m_queue.enqueue(event);
{
ScopedSpinLock lock(m_queue_lock);
m_queue.enqueue(event);
}
m_has_e0_prefix = false;
}
void KeyboardDevice::handle_irq(const RegisterState&)
{
for (;;) {
u8 status = IO::in8(I8042_STATUS);
if (!(((status & I8042_WHICH_BUFFER) == I8042_KEYBOARD_BUFFER) && (status & I8042_BUFFER_FULL)))
return;
u8 raw = IO::in8(I8042_BUFFER);
u8 ch = raw & 0x7f;
bool pressed = !(raw & 0x80);
// The controller will read the data and call irq_handle_byte_read
// for the appropriate device
m_controller.irq_process_input_buffer(I8042Controller::Device::Keyboard);
}
m_entropy_source.add_random_event(raw);
void KeyboardDevice::irq_handle_byte_read(u8 byte)
{
u8 ch = byte & 0x7f;
bool pressed = !(byte & 0x80);
if (raw == 0xe0) {
m_has_e0_prefix = true;
return;
}
m_entropy_source.add_random_event(byte);
if (byte == 0xe0) {
m_has_e0_prefix = true;
return;
}
#ifdef KEYBOARD_DEBUG
dbg() << "Keyboard::handle_irq: " << String::format("%b", ch) << " " << (pressed ? "down" : "up");
dbg() << "Keyboard::irq_handle_byte_read: " << String::format("%b", ch) << " " << (pressed ? "down" : "up");
#endif
switch (ch) {
case 0x38:
if (m_has_e0_prefix)
update_modifier(Mod_AltGr, pressed);
else
update_modifier(Mod_Alt, pressed);
break;
case 0x1d:
update_modifier(Mod_Ctrl, pressed);
break;
case 0x5b:
update_modifier(Mod_Logo, pressed);
break;
case 0x2a:
case 0x36:
update_modifier(Mod_Shift, pressed);
break;
}
switch (ch) {
case I8042_ACK:
break;
default:
if (m_modifiers & Mod_Alt) {
switch (ch) {
case 0x02 ... 0x07: // 1 to 6
VirtualConsole::switch_to(ch - 0x02);
break;
default:
key_state_changed(ch, pressed);
break;
}
} else {
switch (ch) {
case 0x38:
if (m_has_e0_prefix)
update_modifier(Mod_AltGr, pressed);
else
update_modifier(Mod_Alt, pressed);
break;
case 0x1d:
update_modifier(Mod_Ctrl, pressed);
break;
case 0x5b:
update_modifier(Mod_Logo, pressed);
break;
case 0x2a:
case 0x36:
update_modifier(Mod_Shift, pressed);
break;
}
switch (ch) {
case I8042_ACK:
break;
default:
if (m_modifiers & Mod_Alt) {
switch (ch) {
case 0x02 ... 0x07: // 1 to 6
VirtualConsole::switch_to(ch - 0x02);
break;
default:
key_state_changed(ch, pressed);
break;
}
} else {
key_state_changed(ch, pressed);
}
}
}
static AK::Singleton<KeyboardDevice> s_the;
void KeyboardDevice::initialize()
{
s_the.ensure_instance();
}
KeyboardDevice& KeyboardDevice::the()
{
return *s_the;
@ -351,19 +343,23 @@ KeyboardDevice& KeyboardDevice::the()
KeyboardDevice::KeyboardDevice()
: IRQHandler(IRQ_KEYBOARD)
, CharacterDevice(85, 1)
, m_controller(I8042Controller::the())
{
// Empty the buffer of any pending data.
// I don't care what you've been pressing until now!
while (IO::in8(I8042_STATUS) & I8042_BUFFER_FULL)
IO::in8(I8042_BUFFER);
enable_irq();
}
KeyboardDevice::~KeyboardDevice()
{
}
bool KeyboardDevice::initialize()
{
if (!m_controller.reset_device(I8042Controller::Device::Keyboard)) {
dbg() << "KeyboardDevice: I8042 controller failed to reset device";
return false;
}
return true;
}
bool KeyboardDevice::can_read(const FileDescription&, size_t) const
{
return !m_queue.is_empty();
@ -372,6 +368,7 @@ bool KeyboardDevice::can_read(const FileDescription&, size_t) const
KResultOr<size_t> KeyboardDevice::read(FileDescription&, size_t, UserOrKernelBuffer& buffer, size_t size)
{
size_t nread = 0;
ScopedSpinLock lock(m_queue_lock);
while (nread < size) {
if (m_queue.is_empty())
break;
@ -379,6 +376,9 @@ KResultOr<size_t> KeyboardDevice::read(FileDescription&, size_t, UserOrKernelBuf
if ((size - nread) < (ssize_t)sizeof(Event))
break;
auto event = m_queue.dequeue();
lock.unlock();
ssize_t n = buffer.write_buffered<sizeof(Event)>(sizeof(Event), [&](u8* data, size_t data_bytes) {
memcpy(data, &event, sizeof(Event));
return (ssize_t)data_bytes;
@ -387,6 +387,8 @@ KResultOr<size_t> KeyboardDevice::read(FileDescription&, size_t, UserOrKernelBuf
return KResult(n);
ASSERT((size_t)n == sizeof(Event));
nread += sizeof(Event);
lock.lock();
}
return nread;
}

View file

@ -31,6 +31,7 @@
#include <AK/Types.h>
#include <Kernel/API/KeyCode.h>
#include <Kernel/Devices/CharacterDevice.h>
#include <Kernel/Devices/I8042Controller.h>
#include <Kernel/Interrupts/IRQHandler.h>
#include <Kernel/Random.h>
#include <LibKeyboard/CharacterMap.h>
@ -40,17 +41,19 @@ namespace Kernel {
class KeyboardClient;
class KeyboardDevice final : public IRQHandler
, public CharacterDevice {
, public CharacterDevice
, public I8042Device {
AK_MAKE_ETERNAL
public:
using Event = KeyEvent;
static void initialize();
static KeyboardDevice& the();
virtual ~KeyboardDevice() override;
KeyboardDevice();
bool initialize();
void set_client(KeyboardClient* client) { m_client = client; }
void set_maps(const Keyboard::CharacterMapData& character_map, const String& character_map_name);
@ -64,6 +67,13 @@ public:
virtual const char* purpose() const override { return class_name(); }
// ^I8042Device
virtual void irq_handle_byte_read(u8 byte) override;
virtual void enable_interrupts() override
{
enable_irq();
}
private:
// ^IRQHandler
virtual void handle_irq(const RegisterState&) override;
@ -80,7 +90,9 @@ private:
m_modifiers &= ~modifier;
}
I8042Controller& m_controller;
KeyboardClient* m_client { nullptr };
mutable SpinLock<u8> m_queue_lock;
CircularQueue<Event, 16> m_queue;
u8 m_modifiers { 0 };
bool m_caps_lock_on { false };

View file

@ -33,13 +33,6 @@
namespace Kernel {
#define IRQ_MOUSE 12
#define I8042_BUFFER 0x60
#define I8042_STATUS 0x64
#define I8042_ACK 0xFA
#define I8042_BUFFER_FULL 0x01
#define I8042_WHICH_BUFFER 0x20
#define I8042_MOUSE_BUFFER 0x20
#define I8042_KEYBOARD_BUFFER 0x00
#define PS2MOUSE_SET_RESOLUTION 0xE8
#define PS2MOUSE_STATUS_REQUEST 0xE9
@ -62,19 +55,14 @@ static AK::Singleton<PS2MouseDevice> s_the;
PS2MouseDevice::PS2MouseDevice()
: IRQHandler(IRQ_MOUSE)
, CharacterDevice(10, 1)
, m_controller(I8042Controller::the())
{
initialize();
}
PS2MouseDevice::~PS2MouseDevice()
{
}
void PS2MouseDevice::create()
{
s_the.ensure_instance();
}
PS2MouseDevice& PS2MouseDevice::the()
{
return *s_the;
@ -82,78 +70,69 @@ PS2MouseDevice& PS2MouseDevice::the()
void PS2MouseDevice::handle_irq(const RegisterState&)
{
if (auto* backdoor = VMWareBackdoor::the()) {
if (backdoor->vmmouse_is_absolute()) {
IO::in8(I8042_BUFFER);
auto packet = backdoor->receive_mouse_packet();
m_entropy_source.add_random_event(packet);
if (packet.has_value())
m_queue.enqueue(packet.value());
return;
}
}
for (;;) {
u8 status = IO::in8(I8042_STATUS);
if (!(((status & I8042_WHICH_BUFFER) == I8042_MOUSE_BUFFER) && (status & I8042_BUFFER_FULL)))
return;
// The controller will read the data and call irq_handle_byte_read
// for the appropriate device
m_controller.irq_process_input_buffer(I8042Controller::Device::Mouse);
}
u8 data = IO::in8(I8042_BUFFER);
m_data[m_data_state] = data;
void PS2MouseDevice::irq_handle_byte_read(u8 byte)
{
m_data.bytes[m_data_state] = byte;
auto commit_packet = [&] {
m_data_state = 0;
auto commit_packet = [&] {
m_data_state = 0;
#ifdef PS2MOUSE_DEBUG
dbg() << "PS2Mouse: " << m_data[1] << ", " << m_data[2] << " " << ((m_data[0] & 1) ? "Left" : "") << " " << ((m_data[0] & 2) ? "Right" : "") << " (buffered: " << m_queue.size() << ")";
dbg() << "PS2Mouse: " << m_data.bytes[1] << ", " << m_data.bytes[2] << " " << ((m_data.bytes[0] & 1) ? "Left" : "") << " " << ((m_data.bytes[0] & 2) ? "Right" : "") << " (buffered: " << m_queue.size() << ")";
#endif
m_entropy_source.add_random_event(*(u32*)m_data);
m_entropy_source.add_random_event(m_data.dword);
parse_data_packet();
};
ScopedSpinLock lock(m_queue_lock);
m_queue.enqueue(m_data);
};
switch (m_data_state) {
case 0:
if (!(data & 0x08)) {
dbg() << "PS2Mouse: Stream out of sync.";
break;
}
++m_data_state;
break;
case 1:
++m_data_state;
break;
case 2:
if (m_has_wheel) {
++m_data_state;
break;
}
commit_packet();
break;
case 3:
ASSERT(m_has_wheel);
commit_packet();
switch (m_data_state) {
case 0:
if (!(byte & 0x08)) {
dbg() << "PS2Mouse: Stream out of sync.";
break;
}
++m_data_state;
break;
case 1:
++m_data_state;
break;
case 2:
if (m_has_wheel) {
++m_data_state;
break;
}
commit_packet();
break;
case 3:
ASSERT(m_has_wheel);
commit_packet();
break;
}
}
void PS2MouseDevice::parse_data_packet()
MousePacket PS2MouseDevice::parse_data_packet(const RawPacket& raw_packet)
{
int x = m_data[1];
int y = m_data[2];
int x = raw_packet.bytes[1];
int y = raw_packet.bytes[2];
int z = 0;
if (m_has_wheel) {
// FIXME: For non-Intellimouse, this is a full byte.
// However, for now, m_has_wheel is only set for Intellimouse.
z = (char)(m_data[3] & 0x0f);
z = (char)(raw_packet.bytes[3] & 0x0f);
// -1 in 4 bits
if (z == 15)
z = -1;
}
bool x_overflow = m_data[0] & 0x40;
bool y_overflow = m_data[0] & 0x80;
bool x_sign = m_data[0] & 0x10;
bool y_sign = m_data[0] & 0x20;
bool x_overflow = raw_packet.bytes[0] & 0x40;
bool y_overflow = raw_packet.bytes[0] & 0x80;
bool x_sign = raw_packet.bytes[0] & 0x10;
bool y_sign = raw_packet.bytes[0] & 0x20;
if (x && x_sign)
x -= 0x100;
if (y && y_sign)
@ -166,12 +145,12 @@ void PS2MouseDevice::parse_data_packet()
packet.x = x;
packet.y = y;
packet.z = z;
packet.buttons = m_data[0] & 0x07;
packet.buttons = raw_packet.bytes[0] & 0x07;
if (m_has_five_buttons) {
if (m_data[3] & 0x10)
if (raw_packet.bytes[3] & 0x10)
packet.buttons |= MousePacket::BackButton;
if (m_data[3] & 0x20)
if (raw_packet.bytes[3] & 0x20)
packet.buttons |= MousePacket::ForwardButton;
}
@ -180,89 +159,57 @@ void PS2MouseDevice::parse_data_packet()
dbg() << "PS2 Relative Mouse: Buttons " << String::format("%x", packet.buttons);
dbg() << "Mouse: X " << packet.x << ", Y " << packet.y << ", Z " << packet.z;
#endif
m_queue.enqueue(packet);
}
void PS2MouseDevice::wait_then_write(u8 port, u8 data)
{
prepare_for_output();
IO::out8(port, data);
}
u8 PS2MouseDevice::wait_then_read(u8 port)
{
prepare_for_input();
return IO::in8(port);
}
void PS2MouseDevice::initialize()
{
// Enable PS aux port
wait_then_write(I8042_STATUS, 0xa8);
check_device_presence();
if (m_device_present)
initialize_device();
}
void PS2MouseDevice::check_device_presence()
{
mouse_write(PS2MOUSE_REQUEST_SINGLE_PACKET);
u8 maybe_ack = mouse_read();
if (maybe_ack == I8042_ACK) {
m_device_present = true;
klog() << "PS2MouseDevice: Device detected";
// the mouse will send a packet of data, since that's what we asked
// for. we don't care about the content.
mouse_read();
mouse_read();
mouse_read();
} else {
m_device_present = false;
klog() << "PS2MouseDevice: Device not detected";
}
return packet;
}
u8 PS2MouseDevice::get_device_id()
{
mouse_write(PS2MOUSE_GET_DEVICE_ID);
expect_ack();
return mouse_read();
if (send_command(PS2MOUSE_GET_DEVICE_ID) != I8042_ACK)
return 0;
return read_from_device();
}
u8 PS2MouseDevice::read_from_device()
{
return m_controller.read_from_device(I8042Controller::Device::Mouse);
}
u8 PS2MouseDevice::send_command(u8 command)
{
u8 response = m_controller.send_command(I8042Controller::Device::Mouse, command);
if (response != I8042_ACK)
dbg() << "PS2MouseDevice: Command " << (int)command << " got " << (int)response << " but expected ack: " << (int)I8042_ACK;
return response;
}
u8 PS2MouseDevice::send_command(u8 command, u8 data)
{
u8 response = m_controller.send_command(I8042Controller::Device::Mouse, command, data);
if (response != I8042_ACK)
dbg() << "PS2MouseDevice: Command " << (int)command << " got " << (int)response << " but expected ack: " << (int)I8042_ACK;
return response;
}
void PS2MouseDevice::set_sample_rate(u8 rate)
{
mouse_write(PS2MOUSE_SET_SAMPLE_RATE);
expect_ack();
mouse_write(rate);
expect_ack();
send_command(PS2MOUSE_SET_SAMPLE_RATE, rate);
}
void PS2MouseDevice::initialize_device()
bool PS2MouseDevice::initialize()
{
if (!m_device_present)
return;
if (!m_controller.reset_device(I8042Controller::Device::Mouse)) {
dbg() << "PS2MouseDevice: I8042 controller failed to reset device";
return false;
}
// Enable interrupts
wait_then_write(I8042_STATUS, 0x20);
// Enable the PS/2 mouse IRQ (12).
// NOTE: The keyboard uses IRQ 1 (and is enabled by bit 0 in this register).
u8 status = wait_then_read(I8042_BUFFER) | 2;
wait_then_write(I8042_STATUS, 0x60);
wait_then_write(I8042_BUFFER, status);
u8 device_id = read_from_device();
// Set default settings.
mouse_write(PS2MOUSE_SET_DEFAULTS);
expect_ack();
if (send_command(PS2MOUSE_SET_DEFAULTS) != I8042_ACK)
return false;
// Enable.
mouse_write(PS2MOUSE_ENABLE_PACKET_STREAMING);
expect_ack();
u8 device_id = get_device_id();
if (send_command(PS2MOUSE_ENABLE_PACKET_STREAMING) != I8042_ACK)
return false;
if (device_id != PS2MOUSE_INTELLIMOUSE_ID) {
// Send magical wheel initiation sequence.
@ -271,7 +218,6 @@ void PS2MouseDevice::initialize_device()
set_sample_rate(80);
device_id = get_device_id();
}
if (device_id == PS2MOUSE_INTELLIMOUSE_ID) {
m_has_wheel = true;
klog() << "PS2MouseDevice: Mouse wheel enabled!";
@ -291,48 +237,12 @@ void PS2MouseDevice::initialize_device()
m_has_five_buttons = true;
klog() << "PS2MouseDevice: 5 buttons enabled!";
}
enable_irq();
}
void PS2MouseDevice::expect_ack()
{
u8 data = mouse_read();
ASSERT(data == I8042_ACK);
}
void PS2MouseDevice::prepare_for_input()
{
for (;;) {
if (IO::in8(I8042_STATUS) & 1)
return;
}
}
void PS2MouseDevice::prepare_for_output()
{
for (;;) {
if (!(IO::in8(I8042_STATUS) & 2))
return;
}
}
void PS2MouseDevice::mouse_write(u8 data)
{
prepare_for_output();
IO::out8(I8042_STATUS, 0xd4);
prepare_for_output();
IO::out8(I8042_BUFFER, data);
}
u8 PS2MouseDevice::mouse_read()
{
prepare_for_input();
return IO::in8(I8042_BUFFER);
return true;
}
bool PS2MouseDevice::can_read(const FileDescription&, size_t) const
{
ScopedSpinLock lock(m_queue_lock);
return !m_queue.is_empty();
}
@ -341,8 +251,26 @@ KResultOr<size_t> PS2MouseDevice::read(FileDescription&, size_t, UserOrKernelBuf
ASSERT(size > 0);
size_t nread = 0;
size_t remaining_space_in_buffer = static_cast<size_t>(size) - nread;
auto* backdoor = VMWareBackdoor::the();
ScopedSpinLock lock(m_queue_lock);
while (!m_queue.is_empty() && remaining_space_in_buffer) {
auto packet = m_queue.dequeue();
auto raw_packet = m_queue.dequeue();
lock.unlock();
MousePacket packet;
bool is_parsed = false;
if (backdoor && backdoor->vmmouse_is_absolute()) {
auto mouse_packet = backdoor->receive_mouse_packet();
m_entropy_source.add_random_event(packet);
if (mouse_packet.has_value()) {
packet = mouse_packet.value();
is_parsed = true;
}
}
if (!is_parsed)
packet = parse_data_packet(raw_packet);
#ifdef PS2MOUSE_DEBUG
dbg() << "PS2 Mouse Read: Buttons " << String::format("%x", packet.buttons);
dbg() << "PS2 Mouse: X " << packet.x << ", Y " << packet.y << ", Z " << packet.z << " Relative " << packet.buttons;
@ -353,6 +281,8 @@ KResultOr<size_t> PS2MouseDevice::read(FileDescription&, size_t, UserOrKernelBuf
return KResult(-EFAULT);
nread += bytes_read_from_packet;
remaining_space_in_buffer -= bytes_read_from_packet;
lock.lock();
}
return nread;
}

View file

@ -29,20 +29,23 @@
#include <AK/CircularQueue.h>
#include <Kernel/API/MousePacket.h>
#include <Kernel/Devices/CharacterDevice.h>
#include <Kernel/Devices/I8042Controller.h>
#include <Kernel/Interrupts/IRQHandler.h>
#include <Kernel/Random.h>
namespace Kernel {
class PS2MouseDevice final : public IRQHandler
, public CharacterDevice {
, public CharacterDevice
, public I8042Device {
public:
PS2MouseDevice();
virtual ~PS2MouseDevice() override;
static void create();
static PS2MouseDevice& the();
bool initialize();
// ^CharacterDevice
virtual bool can_read(const FileDescription&, size_t) const override;
virtual KResultOr<size_t> read(FileDescription&, size_t, UserOrKernelBuffer&, size_t) override;
@ -51,6 +54,13 @@ public:
virtual const char* purpose() const override { return class_name(); }
// ^I8042Device
virtual void irq_handle_byte_read(u8 byte) override;
virtual void enable_interrupts() override
{
enable_irq();
}
private:
// ^IRQHandler
void handle_vmmouse_absolute_pointer();
@ -59,24 +69,25 @@ private:
// ^CharacterDevice
virtual const char* class_name() const override { return "PS2MouseDevice"; }
void initialize();
void check_device_presence();
void initialize_device();
void prepare_for_input();
void prepare_for_output();
void mouse_write(u8);
u8 mouse_read();
void wait_then_write(u8 port, u8 data);
u8 wait_then_read(u8 port);
void parse_data_packet();
void expect_ack();
struct RawPacket {
union {
u32 dword;
u8 bytes[4];
};
};
u8 read_from_device();
u8 send_command(u8 command);
u8 send_command(u8 command, u8 data);
MousePacket parse_data_packet(const RawPacket&);
void set_sample_rate(u8);
u8 get_device_id();
bool m_device_present { false };
CircularQueue<MousePacket, 100> m_queue;
I8042Controller& m_controller;
mutable SpinLock<u8> m_queue_lock;
CircularQueue<RawPacket, 100> m_queue;
u8 m_data_state { 0 };
u8 m_data[4];
RawPacket m_data;
bool m_has_wheel { false };
bool m_has_five_buttons { false };
EntropySource m_entropy_source;

View file

@ -36,13 +36,12 @@
#include <Kernel/Devices/EBRPartitionTable.h>
#include <Kernel/Devices/FullDevice.h>
#include <Kernel/Devices/GPTPartitionTable.h>
#include <Kernel/Devices/KeyboardDevice.h>
#include <Kernel/Devices/I8042Controller.h>
#include <Kernel/Devices/MBRPartitionTable.h>
#include <Kernel/Devices/MBVGADevice.h>
#include <Kernel/Devices/NullDevice.h>
#include <Kernel/Devices/PATAChannel.h>
#include <Kernel/Devices/PATADiskDevice.h>
#include <Kernel/Devices/PS2MouseDevice.h>
#include <Kernel/Devices/RandomDevice.h>
#include <Kernel/Devices/SB16.h>
#include <Kernel/Devices/SerialDevice.h>
@ -141,8 +140,7 @@ extern "C" [[noreturn]] void init()
ACPI::initialize();
VFS::initialize();
KeyboardDevice::initialize();
PS2MouseDevice::create();
I8042Controller::initialize();
Console::initialize();
klog() << "Starting SerenityOS...";