summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--Kernel/CMakeLists.txt10
-rw-r--r--Kernel/Debug.h.in4
-rw-r--r--Kernel/Devices/HID/HIDDevice.h54
-rw-r--r--Kernel/Devices/HID/HIDManagement.cpp150
-rw-r--r--Kernel/Devices/HID/HIDManagement.h84
-rw-r--r--Kernel/Devices/HID/I8042Controller.cpp (renamed from Kernel/Devices/I8042Controller.cpp)123
-rw-r--r--Kernel/Devices/HID/I8042Controller.h (renamed from Kernel/Devices/I8042Controller.h)70
-rw-r--r--Kernel/Devices/HID/KeyboardDevice.cpp (renamed from Kernel/Devices/KeyboardDevice.cpp)163
-rw-r--r--Kernel/Devices/HID/KeyboardDevice.h (renamed from Kernel/Devices/KeyboardDevice.h)61
-rw-r--r--Kernel/Devices/HID/MouseDevice.cpp77
-rw-r--r--Kernel/Devices/HID/MouseDevice.h70
-rw-r--r--Kernel/Devices/HID/PS2KeyboardDevice.cpp133
-rw-r--r--Kernel/Devices/HID/PS2KeyboardDevice.h69
-rw-r--r--Kernel/Devices/HID/PS2MouseDevice.cpp (renamed from Kernel/Devices/PS2MouseDevice.cpp)92
-rw-r--r--Kernel/Devices/HID/PS2MouseDevice.h (renamed from Kernel/Devices/PS2MouseDevice.h)36
-rw-r--r--Kernel/Devices/HID/VMWareMouseDevice.cpp74
-rw-r--r--Kernel/Devices/HID/VMWareMouseDevice.h50
-rw-r--r--Kernel/FileSystem/ProcFS.cpp4
-rw-r--r--Kernel/Syscalls/keymap.cpp9
-rw-r--r--Kernel/TTY/VirtualConsole.cpp8
-rw-r--r--Kernel/TTY/VirtualConsole.h4
-rw-r--r--Kernel/init.cpp6
-rw-r--r--Meta/CMake/all_the_debug_macros.cmake1
23 files changed, 942 insertions, 410 deletions
diff --git a/Kernel/CMakeLists.txt b/Kernel/CMakeLists.txt
index 949dc37e2c..c8b3cc46f4 100644
--- a/Kernel/CMakeLists.txt
+++ b/Kernel/CMakeLists.txt
@@ -32,19 +32,23 @@ set(KERNEL_SOURCES
Devices/CharacterDevice.cpp
Devices/Device.cpp
Devices/FullDevice.cpp
- Devices/I8042Controller.cpp
- Devices/KeyboardDevice.cpp
Devices/MBVGADevice.cpp
Devices/MemoryDevice.cpp
Devices/NullDevice.cpp
Devices/PCSpeaker.cpp
- Devices/PS2MouseDevice.cpp
Devices/RandomDevice.cpp
Devices/SB16.cpp
Devices/SerialDevice.cpp
Devices/USB/UHCIController.cpp
Devices/VMWareBackdoor.cpp
Devices/ZeroDevice.cpp
+ Devices/HID/I8042Controller.cpp
+ Devices/HID/HIDManagement.cpp
+ Devices/HID/KeyboardDevice.cpp
+ Devices/HID/MouseDevice.cpp
+ Devices/HID/PS2KeyboardDevice.cpp
+ Devices/HID/PS2MouseDevice.cpp
+ Devices/HID/VMWareMouseDevice.cpp
Storage/Partition/DiskPartition.cpp
Storage/Partition/DiskPartitionMetadata.cpp
Storage/Partition/EBRPartitionTable.cpp
diff --git a/Kernel/Debug.h.in b/Kernel/Debug.h.in
index 9a7172def1..6858889c81 100644
--- a/Kernel/Debug.h.in
+++ b/Kernel/Debug.h.in
@@ -178,6 +178,10 @@
#cmakedefine01 MBR_DEBUG
#endif
+#ifndef MOUSE_DEBUG
+#cmakedefine01 MOUSE_DEBUG
+#endif
+
#ifndef MULTIPROCESSOR_DEBUG
#cmakedefine01 MULTIPROCESSOR_DEBUG
#endif
diff --git a/Kernel/Devices/HID/HIDDevice.h b/Kernel/Devices/HID/HIDDevice.h
new file mode 100644
index 0000000000..ae0340de22
--- /dev/null
+++ b/Kernel/Devices/HID/HIDDevice.h
@@ -0,0 +1,54 @@
+/*
+ * Copyright (c) 2021, Liav A. <liavalb@hotmail.co.il>
+ * 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/Devices/CharacterDevice.h>
+#include <Kernel/Random.h>
+
+namespace Kernel {
+
+class HIDDevice : public CharacterDevice {
+public:
+ enum class Type {
+ Unknown = 0,
+ Keyboard,
+ Mouse,
+ };
+
+ virtual Type instrument_type() const = 0;
+ virtual void enable_interrupts() = 0;
+
+protected:
+ HIDDevice(unsigned major, unsigned minor)
+ : CharacterDevice(major, minor)
+ {
+ }
+
+ EntropySource m_entropy_source;
+};
+
+}
diff --git a/Kernel/Devices/HID/HIDManagement.cpp b/Kernel/Devices/HID/HIDManagement.cpp
new file mode 100644
index 0000000000..c16dabce12
--- /dev/null
+++ b/Kernel/Devices/HID/HIDManagement.cpp
@@ -0,0 +1,150 @@
+/*
+ * Copyright (c) 2021, Liav A. <liavalb@hotmail.co.il>
+ * 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 <AK/Singleton.h>
+#include <Kernel/ACPI/Parser.h>
+#include <Kernel/Devices/HID/HIDManagement.h>
+#include <Kernel/Devices/HID/I8042Controller.h>
+
+namespace Kernel {
+
+static AK::Singleton<HIDManagement> s_the;
+
+// clang-format off
+static const Keyboard::CharacterMapData DEFAULT_CHARACTER_MAP =
+{
+ .map = {
+ 0, '\033', '1', '2', '3', '4', '5', '6', '7', '8', '9', '0', '-', '=', 0x08,
+ '\t', 'q', 'w', 'e', 'r', 't', 'y', 'u', 'i', 'o', 'p', '[', ']', '\n',
+ 0, 'a', 's', 'd', 'f', 'g', 'h', 'j', 'k', 'l', ';', '\'', '`', 0,
+ '\\', 'z', 'x', 'c', 'v', 'b', 'n', 'm', ',', '.', '/', 0, '*', 0,
+ ' ', 0, 0,
+ //60 70 80
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, '7', '8', '9', '-', '4', '5', '6', '+', '1', '2', '3', '0', '.', 0, 0, '\\', 0, 0, 0,
+
+ },
+
+ .shift_map = {
+ 0, '\033', '!', '@', '#', '$', '%', '^', '&', '*', '(', ')', '_', '+', 0x08,
+ '\t', 'Q', 'W', 'E', 'R', 'T', 'Y', 'U', 'I', 'O', 'P', '{', '}', '\n',
+ 0, 'A', 'S', 'D', 'F', 'G', 'H', 'J', 'K', 'L', ':', '"', '~', 0,
+ '|', 'Z', 'X', 'C', 'V', 'B', 'N', 'M', '<', '>', '?', 0, '*', 0,
+ ' ', 0, 0,
+ //60 70 80
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, '7', '8', '9', '-', '4', '5', '6', '+', '1', '2', '3', '0', '.', 0, 0, '|', 0, 0, 0,
+
+ },
+
+ .alt_map = {
+ 0, '\033', '1', '2', '3', '4', '5', '6', '7', '8', '9', '0', '-', '=', 0x08,
+ '\t', 'q', 'w', 'e', 'r', 't', 'y', 'u', 'i', 'o', 'p', '[', ']', '\n',
+ 0, 'a', 's', 'd', 'f', 'g', 'h', 'j', 'k', 'l', ';', '\'', '`', 0,
+ '\\', 'z', 'x', 'c', 'v', 'b', 'n', 'm', ',', '.', '/', 0, '*', 0,
+ ' ', 0, 0,
+
+ //60 70 80
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, '7', '8', '9', '-', '4', '5', '6', '+', '1', '2', '3', '0', '.', 0, 0, '\\', 0, 0, 0,
+
+ },
+
+ .altgr_map = {
+ 0, '\033', '1', '2', '3', '4', '5', '6', '7', '8', '9', '0', '-', '=', 0x08,
+ '\t', 'q', 'w', 'e', 'r', 't', 'y', 'u', 'i', 'o', 'p', '[', ']', '\n',
+ 0, 'a', 's', 'd', 'f', 'g', 'h', 'j', 'k', 'l', ';', '\'', '`', 0,
+ '\\', 'z', 'x', 'c', 'v', 'b', 'n', 'm', ',', '.', '/', 0, '*', 0,
+ ' ', 0, 0,
+ //60 70 80
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, '7', '8', '9', '-', '4', '5', '6', '+', '1', '2', '3', '0', '.', 0, 0, '\\', 0, 0, 0,
+ },
+
+ .shift_altgr_map = {
+ 0, '\033', '1', '2', '3', '4', '5', '6', '7', '8', '9', '0', '-', '=', 0x08,
+ '\t', 'q', 'w', 'e', 'r', 't', 'y', 'u', 'i', 'o', 'p', '[', ']', '\n',
+ 0, 'a', 's', 'd', 'f', 'g', 'h', 'j', 'k', 'l', ';', '\'', '`', 0,
+ '\\', 'z', 'x', 'c', 'v', 'b', 'n', 'm', ',', '.', '/', 0, '*', 0,
+ ' ', 0, 0,
+ //60 70 80
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, '7', '8', '9', '-', '4', '5', '6', '+', '1', '2', '3', '0', '.', 0, 0, '\\', 0, 0, 0,
+ },
+};
+// clang-format on
+
+KeyboardClient::~KeyboardClient()
+{
+}
+
+size_t HIDManagement::generate_minor_device_number_for_mouse()
+{
+ // FIXME: Lock this to prevent race conditions with hot-plugging devices!
+ return m_mouse_minor_number++;
+}
+size_t HIDManagement::generate_minor_device_number_for_keyboard()
+{
+ // FIXME: Lock this to prevent race conditions with hot-plugging devices!
+ return m_keyboard_minor_number++;
+}
+
+UNMAP_AFTER_INIT HIDManagement::HIDManagement()
+ : m_character_map("en-us", DEFAULT_CHARACTER_MAP)
+{
+}
+
+void HIDManagement::set_maps(const Keyboard::CharacterMapData& character_map_data, const String& character_map_name)
+{
+ m_character_map.set_character_map_data(character_map_data);
+ m_character_map.set_character_map_name(character_map_name);
+ dbgln("New Character map '{}' passed in by client.", character_map_name);
+}
+
+UNMAP_AFTER_INIT void HIDManagement::enumerate()
+{
+ // FIXME: When we have USB HID support, we should ensure that we disable
+ // emulation of the PS/2 controller if it was set by the BIOS.
+ // If ACPI indicates we have an i8042 controller and the USB controller was
+ // set to emulate PS/2, we should not initialize the PS/2 controller.
+ if (!ACPI::Parser::the()->have_8042())
+ return;
+ m_i8042_controller = I8042Controller::initialize();
+ m_i8042_controller->detect_devices();
+ if (m_i8042_controller->mouse())
+ m_hid_devices.append(m_i8042_controller->mouse().release_nonnull());
+ if (m_i8042_controller->keyboard())
+ m_hid_devices.append(m_i8042_controller->keyboard().release_nonnull());
+}
+
+UNMAP_AFTER_INIT void HIDManagement::initialize()
+{
+ VERIFY(!s_the.is_initialized());
+ s_the.ensure_instance();
+ s_the->enumerate();
+}
+
+HIDManagement& HIDManagement::the()
+{
+ return *s_the;
+}
+
+}
diff --git a/Kernel/Devices/HID/HIDManagement.h b/Kernel/Devices/HID/HIDManagement.h
new file mode 100644
index 0000000000..00173f02dd
--- /dev/null
+++ b/Kernel/Devices/HID/HIDManagement.h
@@ -0,0 +1,84 @@
+/*
+ * Copyright (c) 2021, Liav A. <liavalb@hotmail.co.il>
+ * 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 <AK/CircularQueue.h>
+#include <AK/NonnullRefPtrVector.h>
+#include <AK/RefPtr.h>
+#include <AK/Time.h>
+#include <AK/Types.h>
+#include <Kernel/API/KeyCode.h>
+#include <Kernel/API/MousePacket.h>
+#include <Kernel/KResult.h>
+#include <Kernel/SpinLock.h>
+#include <Kernel/UnixTypes.h>
+#include <LibKeyboard/CharacterMap.h>
+
+namespace Kernel {
+
+class HIDDevice;
+class I8042Controller;
+class MouseDevice;
+class KeyboardDevice;
+class KeyboardClient;
+class HIDManagement {
+ friend class KeyboardDevice;
+ friend class MouseDevice;
+ AK_MAKE_ETERNAL;
+
+public:
+ HIDManagement();
+ static void initialize();
+ static HIDManagement& the();
+
+ void enumerate();
+
+ const String& keymap_name() const { return m_character_map.character_map_name(); }
+ const Keyboard::CharacterMapData& character_maps() const { return m_character_map.character_map_data(); }
+ const Keyboard::CharacterMap& character_map() const { return m_character_map; }
+ void set_client(KeyboardClient* client) { m_client = client; }
+ void set_maps(const Keyboard::CharacterMapData& character_map, const String& character_map_name);
+
+private:
+ size_t generate_minor_device_number_for_mouse();
+ size_t generate_minor_device_number_for_keyboard();
+
+ size_t m_mouse_minor_number { 0 };
+ size_t m_keyboard_minor_number { 0 };
+ Keyboard::CharacterMap m_character_map;
+ KeyboardClient* m_client { nullptr };
+ RefPtr<I8042Controller> m_i8042_controller;
+ NonnullRefPtrVector<HIDDevice> m_hid_devices;
+};
+
+class KeyboardClient {
+public:
+ virtual ~KeyboardClient();
+ virtual void on_key_pressed(KeyEvent) = 0;
+};
+
+}
diff --git a/Kernel/Devices/I8042Controller.cpp b/Kernel/Devices/HID/I8042Controller.cpp
index 9877df385c..522bd14007 100644
--- a/Kernel/Devices/I8042Controller.cpp
+++ b/Kernel/Devices/HID/I8042Controller.cpp
@@ -24,32 +24,34 @@
* 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/Devices/HID/I8042Controller.h>
+#include <Kernel/Devices/HID/PS2KeyboardDevice.h>
+#include <Kernel/Devices/HID/PS2MouseDevice.h>
+#include <Kernel/Devices/HID/VMWareMouseDevice.h>
#include <Kernel/IO.h>
namespace Kernel {
-static I8042Controller* s_the;
-
-UNMAP_AFTER_INIT void I8042Controller::initialize()
+UNMAP_AFTER_INIT NonnullRefPtr<I8042Controller> I8042Controller::initialize()
{
- if (ACPI::Parser::the()->have_8042())
- new I8042Controller;
+ return adopt(*new I8042Controller());
}
-I8042Controller& I8042Controller::the()
+RefPtr<MouseDevice> I8042Controller::mouse() const
+{
+ return m_mouse_device;
+}
+RefPtr<KeyboardDevice> I8042Controller::keyboard() const
{
- VERIFY(s_the);
- return *s_the;
+ return m_keyboard_device;
}
UNMAP_AFTER_INIT I8042Controller::I8042Controller()
{
- VERIFY(!s_the);
- s_the = this;
+}
+UNMAP_AFTER_INIT void I8042Controller::detect_devices()
+{
u8 configuration;
{
ScopedSpinLock lock(m_lock);
@@ -82,9 +84,9 @@ UNMAP_AFTER_INIT I8042Controller::I8042Controller()
// 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);
+ m_first_port_available = (do_wait_then_read(I8042_BUFFER) == 0);
- if (m_devices[0].available) {
+ if (m_first_port_available) {
do_wait_then_write(I8042_STATUS, 0xae); //enable
configuration |= 1;
configuration &= ~(1 << 4);
@@ -94,8 +96,8 @@ UNMAP_AFTER_INIT I8042Controller::I8042Controller()
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) {
+ m_second_port_available = (do_wait_then_read(I8042_BUFFER) == 0);
+ if (m_second_port_available) {
do_wait_then_write(I8042_STATUS, 0xa8); // enable
configuration |= 2;
configuration &= ~(1 << 5);
@@ -105,7 +107,7 @@ UNMAP_AFTER_INIT I8042Controller::I8042Controller()
}
// Enable IRQs for the ports that are usable
- if (m_devices[0].available || m_devices[1].available) {
+ if (m_first_port_available || m_second_port_available) {
configuration &= ~0x30; // renable clocks
do_wait_then_write(I8042_STATUS, 0x60);
do_wait_then_write(I8042_BUFFER, configuration);
@@ -113,12 +115,11 @@ UNMAP_AFTER_INIT I8042Controller::I8042Controller()
}
// Try to detect and initialize the devices
- if (m_devices[0].available) {
- if (KeyboardDevice::the().initialize()) {
- m_devices[0].device = &KeyboardDevice::the();
- } else {
+ if (m_first_port_available) {
+ m_keyboard_device = PS2KeyboardDevice::try_to_initialize(*this);
+ if (!m_keyboard_device) {
dbgln("I8042: Keyboard device failed to initialize, disable");
- m_devices[0].available = false;
+ m_first_port_available = false;
configuration &= ~1;
configuration |= 1 << 4;
ScopedSpinLock lock(m_lock);
@@ -126,37 +127,47 @@ UNMAP_AFTER_INIT I8042Controller::I8042Controller()
do_wait_then_write(I8042_BUFFER, configuration);
}
}
- if (m_devices[1].available) {
- if (PS2MouseDevice::the().initialize()) {
- m_devices[1].device = &PS2MouseDevice::the();
- } else {
- dbgln("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);
+ if (m_second_port_available) {
+ m_mouse_device = VMWareMouseDevice::try_to_initialize(*this);
+ if (!m_mouse_device) {
+ m_mouse_device = PS2MouseDevice::try_to_initialize(*this);
+ if (!m_mouse_device) {
+ dbgln("I8042: Mouse device failed to initialize, disable");
+ m_second_port_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();
+ if (m_keyboard_device)
+ m_keyboard_device->enable_interrupts();
+ if (m_mouse_device)
+ m_mouse_device->enable_interrupts();
}
-void I8042Controller::irq_process_input_buffer(Device)
+void I8042Controller::irq_process_input_buffer(HIDDevice::Type)
{
VERIFY(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;
+ HIDDevice::Type data_for_device = ((status & I8042_WHICH_BUFFER) == I8042_MOUSE_BUFFER) ? HIDDevice::Type::Mouse : HIDDevice::Type::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);
+ if (data_for_device == HIDDevice::Type::Mouse) {
+ VERIFY(m_mouse_device);
+ static_cast<PS2MouseDevice&>(*m_mouse_device).irq_handle_byte_read(byte);
+ return;
+ }
+ if (data_for_device == HIDDevice::Type::Keyboard) {
+ VERIFY(m_keyboard_device);
+ static_cast<PS2KeyboardDevice&>(*m_keyboard_device).irq_handle_byte_read(byte);
+ return;
+ }
}
void I8042Controller::do_drain()
@@ -169,9 +180,9 @@ void I8042Controller::do_drain()
}
}
-bool I8042Controller::do_reset_device(Device device)
+bool I8042Controller::do_reset_device(HIDDevice::Type device)
{
- VERIFY(device != Device::None);
+ VERIFY(device != HIDDevice::Type::Unknown);
VERIFY(m_lock.is_locked());
VERIFY(!Processor::current().in_irq());
@@ -181,9 +192,9 @@ bool I8042Controller::do_reset_device(Device device)
return do_wait_then_read(I8042_BUFFER) == 0xaa;
}
-u8 I8042Controller::do_send_command(Device device, u8 command)
+u8 I8042Controller::do_send_command(HIDDevice::Type device, u8 command)
{
- VERIFY(device != Device::None);
+ VERIFY(device != HIDDevice::Type::Unknown);
VERIFY(m_lock.is_locked());
VERIFY(!Processor::current().in_irq());
@@ -191,9 +202,9 @@ u8 I8042Controller::do_send_command(Device device, u8 command)
return do_write_to_device(device, command);
}
-u8 I8042Controller::do_send_command(Device device, u8 command, u8 data)
+u8 I8042Controller::do_send_command(HIDDevice::Type device, u8 command, u8 data)
{
- VERIFY(device != Device::None);
+ VERIFY(device != HIDDevice::Type::Unknown);
VERIFY(m_lock.is_locked());
VERIFY(!Processor::current().in_irq());
@@ -204,9 +215,9 @@ u8 I8042Controller::do_send_command(Device device, u8 command, u8 data)
return response;
}
-u8 I8042Controller::do_write_to_device(Device device, u8 data)
+u8 I8042Controller::do_write_to_device(HIDDevice::Type device, u8 data)
{
- VERIFY(device != Device::None);
+ VERIFY(device != HIDDevice::Type::Unknown);
VERIFY(m_lock.is_locked());
VERIFY(!Processor::current().in_irq());
@@ -214,7 +225,7 @@ u8 I8042Controller::do_write_to_device(Device device, u8 data)
int attempts = 0;
u8 response;
do {
- if (device != Device::Keyboard) {
+ if (device != HIDDevice::Type::Keyboard) {
prepare_for_output();
IO::out8(I8042_STATUS, 0xd4);
}
@@ -228,21 +239,21 @@ u8 I8042Controller::do_write_to_device(Device device, u8 data)
return response;
}
-u8 I8042Controller::do_read_from_device(Device device)
+u8 I8042Controller::do_read_from_device(HIDDevice::Type device)
{
- VERIFY(device != Device::None);
+ VERIFY(device != HIDDevice::Type::Unknown);
prepare_for_input(device);
return IO::in8(I8042_BUFFER);
}
-void I8042Controller::prepare_for_input(Device device)
+void I8042Controller::prepare_for_input(HIDDevice::Type device)
{
VERIFY(m_lock.is_locked());
- const u8 buffer_type = device == Device::Keyboard ? I8042_KEYBOARD_BUFFER : I8042_MOUSE_BUFFER;
+ const u8 buffer_type = device == HIDDevice::Type::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)))
+ if ((status & I8042_BUFFER_FULL) && (device == HIDDevice::Type::Unknown || ((status & I8042_WHICH_BUFFER) == buffer_type)))
return;
}
}
@@ -266,7 +277,7 @@ void I8042Controller::do_wait_then_write(u8 port, u8 data)
u8 I8042Controller::do_wait_then_read(u8 port)
{
VERIFY(m_lock.is_locked());
- prepare_for_input(Device::None);
+ prepare_for_input(HIDDevice::Type::Unknown);
return IO::in8(port);
}
diff --git a/Kernel/Devices/I8042Controller.h b/Kernel/Devices/HID/I8042Controller.h
index 944be124c0..7b51cd7d65 100644
--- a/Kernel/Devices/I8042Controller.h
+++ b/Kernel/Devices/HID/I8042Controller.h
@@ -26,6 +26,9 @@
#pragma once
+#include <AK/RefCounted.h>
+#include <Kernel/Devices/HID/KeyboardDevice.h>
+#include <Kernel/Devices/HID/MouseDevice.h>
#include <Kernel/SpinLock.h>
namespace Kernel {
@@ -41,43 +44,51 @@ namespace Kernel {
#define I8042_KEYBOARD_BUFFER 0x00
#define I8042_MOUSE_BUFFER 0x20
+class I8042Controller;
class I8042Device {
public:
virtual ~I8042Device() = default;
virtual void irq_handle_byte_read(u8 byte) = 0;
- virtual void enable_interrupts() = 0;
+
+protected:
+ explicit I8042Device(const I8042Controller& ps2_controller)
+ : m_i8042_controller(ps2_controller)
+ {
+ }
+
+ NonnullRefPtr<I8042Controller> m_i8042_controller;
};
-class I8042Controller {
+class PS2KeyboardDevice;
+class PS2MouseDevice;
+class I8042Controller : public RefCounted<I8042Controller> {
+ friend class PS2KeyboardDevice;
+ friend class PS2MouseDevice;
+
public:
- enum class Device {
- None = 0,
- Keyboard,
- Mouse
- };
+ static NonnullRefPtr<I8042Controller> initialize();
- static void initialize();
- static I8042Controller& the();
+ void detect_devices();
- bool reset_device(Device device)
+ bool reset_device(HIDDevice::Type device)
{
ScopedSpinLock lock(m_lock);
return do_reset_device(device);
}
- u8 send_command(Device device, u8 command)
+ u8 send_command(HIDDevice::Type device, u8 command)
{
ScopedSpinLock lock(m_lock);
return do_send_command(device, command);
}
- u8 send_command(Device device, u8 command, u8 data)
+ u8 send_command(HIDDevice::Type device, u8 command, u8 data)
{
ScopedSpinLock lock(m_lock);
return do_send_command(device, command, data);
}
- u8 read_from_device(Device device)
+ u8 read_from_device(HIDDevice::Type device)
{
ScopedSpinLock lock(m_lock);
return do_read_from_device(device);
@@ -96,35 +107,30 @@ public:
}
void prepare_for_output();
- void prepare_for_input(Device);
+ void prepare_for_input(HIDDevice::Type);
- void irq_process_input_buffer(Device);
+ void irq_process_input_buffer(HIDDevice::Type);
+
+ RefPtr<MouseDevice> mouse() const;
+ RefPtr<KeyboardDevice> keyboard() const;
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);
+ bool do_reset_device(HIDDevice::Type);
+ u8 do_send_command(HIDDevice::Type type, u8 data);
+ u8 do_send_command(HIDDevice::Type device, u8 command, u8 data);
+ u8 do_write_to_device(HIDDevice::Type device, u8 data);
+ u8 do_read_from_device(HIDDevice::Type 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)
- {
- VERIFY(device != Device::None);
- return (device == Device::Keyboard) ? 0 : 1;
- }
-
- struct DeviceInfo {
- I8042Device* device { nullptr };
- bool available { false };
- };
-
SpinLock<u8> m_lock;
+ bool m_first_port_available { false };
+ bool m_second_port_available { false };
bool m_is_dual_channel { false };
- DeviceInfo m_devices[2];
+ RefPtr<MouseDevice> m_mouse_device;
+ RefPtr<KeyboardDevice> m_keyboard_device;
};
}
diff --git a/Kernel/Devices/KeyboardDevice.cpp b/Kernel/Devices/HID/KeyboardDevice.cpp
index 428f6a9ac4..c1c27b02ea 100644
--- a/Kernel/Devices/KeyboardDevice.cpp
+++ b/Kernel/Devices/HID/KeyboardDevice.cpp
@@ -1,5 +1,6 @@
/*
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
+ * Copyright (c) 2021, Liav A. <liavalb@hotmail.co.il>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
@@ -31,7 +32,7 @@
#include <AK/Types.h>
#include <Kernel/Arch/x86/CPU.h>
#include <Kernel/Debug.h>
-#include <Kernel/Devices/KeyboardDevice.h>
+#include <Kernel/Devices/HID/KeyboardDevice.h>
#include <Kernel/IO.h>
#include <Kernel/TTY/VirtualConsole.h>
@@ -262,12 +263,12 @@ void KeyboardDevice::key_state_changed(u8 scan_code, bool pressed)
event.flags = m_modifiers;
event.e0_prefix = m_has_e0_prefix;
event.caps_lock_on = m_caps_lock_on;
- event.code_point = m_character_map.get_char(event);
+ event.code_point = HIDManagement::the().character_map().get_char(event);
if (pressed)
event.flags |= Is_Press;
- if (m_client)
- m_client->on_key_pressed(event);
+ if (HIDManagement::the().m_client)
+ HIDManagement::the().m_client->on_key_pressed(event);
{
ScopedSpinLock lock(m_queue_lock);
@@ -279,152 +280,19 @@ void KeyboardDevice::key_state_changed(u8 scan_code, bool pressed)
evaluate_block_conditions();
}
-void KeyboardDevice::handle_irq(const RegisterState&)
-{
- // 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);
-}
-
-void KeyboardDevice::irq_handle_byte_read(u8 byte)
-{
- u8 ch = byte & 0x7f;
- bool pressed = !(byte & 0x80);
-
- m_entropy_source.add_random_event(byte);
-
- if (byte == 0xe0) {
- m_has_e0_prefix = true;
- return;
- }
-
-#if KEYBOARD_DEBUG
- dbgln("Keyboard::irq_handle_byte_read: {:#02x} {}", 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_Super, 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;
-
-KeyboardDevice& KeyboardDevice::the()
-{
- return *s_the;
-}
-
-// clang-format off
-static const Keyboard::CharacterMapData DEFAULT_CHARACTER_MAP =
-{
- .map = {
- 0, '\033', '1', '2', '3', '4', '5', '6', '7', '8', '9', '0', '-', '=', 0x08,
- '\t', 'q', 'w', 'e', 'r', 't', 'y', 'u', 'i', 'o', 'p', '[', ']', '\n',
- 0, 'a', 's', 'd', 'f', 'g', 'h', 'j', 'k', 'l', ';', '\'', '`', 0,
- '\\', 'z', 'x', 'c', 'v', 'b', 'n', 'm', ',', '.', '/', 0, '*', 0,
- ' ', 0, 0,
- //60 70 80
- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, '7', '8', '9', '-', '4', '5', '6', '+', '1', '2', '3', '0', '.', 0, 0, '\\', 0, 0, 0,
-
- },
-
- .shift_map = {
- 0, '\033', '!', '@', '#', '$', '%', '^', '&', '*', '(', ')', '_', '+', 0x08,
- '\t', 'Q', 'W', 'E', 'R', 'T', 'Y', 'U', 'I', 'O', 'P', '{', '}', '\n',
- 0, 'A', 'S', 'D', 'F', 'G', 'H', 'J', 'K', 'L', ':', '"', '~', 0,
- '|', 'Z', 'X', 'C', 'V', 'B', 'N', 'M', '<', '>', '?', 0, '*', 0,
- ' ', 0, 0,
- //60 70 80
- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, '7', '8', '9', '-', '4', '5', '6', '+', '1', '2', '3', '0', '.', 0, 0, '|', 0, 0, 0,
-
- },
-
- .alt_map = {
- 0, '\033', '1', '2', '3', '4', '5', '6', '7', '8', '9', '0', '-', '=', 0x08,
- '\t', 'q', 'w', 'e', 'r', 't', 'y', 'u', 'i', 'o', 'p', '[', ']', '\n',
- 0, 'a', 's', 'd', 'f', 'g', 'h', 'j', 'k', 'l', ';', '\'', '`', 0,
- '\\', 'z', 'x', 'c', 'v', 'b', 'n', 'm', ',', '.', '/', 0, '*', 0,
- ' ', 0, 0,
-
- //60 70 80
- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, '7', '8', '9', '-', '4', '5', '6', '+', '1', '2', '3', '0', '.', 0, 0, '\\', 0, 0, 0,
-
- },
-
- .altgr_map = {
- 0, '\033', '1', '2', '3', '4', '5', '6', '7', '8', '9', '0', '-', '=', 0x08,
- '\t', 'q', 'w', 'e', 'r', 't', 'y', 'u', 'i', 'o', 'p', '[', ']', '\n',
- 0, 'a', 's', 'd', 'f', 'g', 'h', 'j', 'k', 'l', ';', '\'', '`', 0,
- '\\', 'z', 'x', 'c', 'v', 'b', 'n', 'm', ',', '.', '/', 0, '*', 0,
- ' ', 0, 0,
- //60 70 80
- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, '7', '8', '9', '-', '4', '5', '6', '+', '1', '2', '3', '0', '.', 0, 0, '\\', 0, 0, 0,
- },
-
- .shift_altgr_map = {
- 0, '\033', '1', '2', '3', '4', '5', '6', '7', '8', '9', '0', '-', '=', 0x08,
- '\t', 'q', 'w', 'e', 'r', 't', 'y', 'u', 'i', 'o', 'p', '[', ']', '\n',
- 0, 'a', 's', 'd', 'f', 'g', 'h', 'j', 'k', 'l', ';', '\'', '`', 0,
- '\\', 'z', 'x', 'c', 'v', 'b', 'n', 'm', ',', '.', '/', 0, '*', 0,
- ' ', 0, 0,
- //60 70 80
- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, '7', '8', '9', '-', '4', '5', '6', '+', '1', '2', '3', '0', '.', 0, 0, '\\', 0, 0, 0,
- },
-};
-// clang-format on
-
+// FIXME: UNMAP_AFTER_INIT is fine for now, but for hot-pluggable devices
+// like USB keyboards, we need to remove this
UNMAP_AFTER_INIT KeyboardDevice::KeyboardDevice()
- : IRQHandler(IRQ_KEYBOARD)
- , CharacterDevice(85, 1)
- , m_controller(I8042Controller::the())
- , m_character_map("en-us", DEFAULT_CHARACTER_MAP)
+ : HIDDevice(85, HIDManagement::the().generate_minor_device_number_for_keyboard())
{
}
+// FIXME: UNMAP_AFTER_INIT is fine for now, but for hot-pluggable devices
+// like USB keyboards, we need to remove this
UNMAP_AFTER_INIT KeyboardDevice::~KeyboardDevice()
{
}
-UNMAP_AFTER_INIT bool KeyboardDevice::initialize()
-{
- if (!m_controller.reset_device(I8042Controller::Device::Keyboard)) {
- dbgln("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();
@@ -463,15 +331,4 @@ KResultOr<size_t> KeyboardDevice::write(FileDescription&, u64, const UserOrKerne
return 0;
}
-KeyboardClient::~KeyboardClient()
-{
-}
-
-void KeyboardDevice::set_maps(const Keyboard::CharacterMapData& character_map_data, const String& character_map_name)
-{
- m_character_map.set_character_map_data(character_map_data);
- m_character_map.set_character_map_name(character_map_name);
- dbgln("New Character map '{}' passed in by client.", character_map_name);
-}
-
}
diff --git a/Kernel/Devices/KeyboardDevice.h b/Kernel/Devices/HID/KeyboardDevice.h
index 00afb18136..3d06571ba5 100644
--- a/Kernel/Devices/KeyboardDevice.h
+++ b/Kernel/Devices/HID/KeyboardDevice.h
@@ -1,5 +1,5 @@
/*
- * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
+ * Copyright (c) 2021, Liav A. <liavalb@hotmail.co.il>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
@@ -31,34 +31,17 @@
#include <AK/Types.h>
#include <Kernel/API/KeyCode.h>
#include <Kernel/Devices/CharacterDevice.h>
-#include <Kernel/Devices/I8042Controller.h>
+#include <Kernel/Devices/HID/HIDDevice.h>
#include <Kernel/Interrupts/IRQHandler.h>
#include <Kernel/Random.h>
-#include <LibKeyboard/CharacterMap.h>
namespace Kernel {
-class KeyboardClient;
-
-class KeyboardDevice final : public IRQHandler
- , public CharacterDevice
- , public I8042Device {
- AK_MAKE_ETERNAL
+class KeyboardDevice : public HIDDevice {
public:
using Event = KeyEvent;
- 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);
- const Keyboard::CharacterMapData& character_maps() const { return m_character_map.character_map_data(); }
-
- const String keymap_name() { return m_character_map.character_map_name(); }
// ^CharacterDevice
virtual KResultOr<size_t> read(FileDescription&, u64, UserOrKernelBuffer&, size_t) override;
@@ -66,27 +49,16 @@ public:
virtual KResultOr<size_t> write(FileDescription&, u64, const UserOrKernelBuffer&, size_t) override;
virtual bool can_write(const FileDescription&, size_t) const override { return true; }
- 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();
- }
+ // ^HIDDevice
+ virtual Type instrument_type() const { return Type::Keyboard; }
// ^Device
virtual mode_t required_mode() const override { return 0440; }
- virtual String device_name() const override { return "keyboard"; }
-
-private:
- // ^IRQHandler
- virtual void handle_irq(const RegisterState&) override;
- // ^CharacterDevice
- virtual const char* class_name() const override { return "KeyboardDevice"; }
+ //FIXME: It should be something like String::formatted("keyboard{}", minor())
+ // instead of a fixed string like this
+ virtual String device_name() const override { return "keyboard"; }
- void key_state_changed(u8 raw, bool pressed);
void update_modifier(u8 modifier, bool state)
{
if (state)
@@ -95,23 +67,18 @@ private:
m_modifiers &= ~modifier;
}
- I8042Controller& m_controller;
- KeyboardClient* m_client { nullptr };
+protected:
+ KeyboardDevice();
mutable SpinLock<u8> m_queue_lock;
CircularQueue<Event, 16> m_queue;
+ // ^CharacterDevice
+ virtual const char* class_name() const override { return "KeyboardDevice"; }
+
u8 m_modifiers { 0 };
bool m_caps_lock_on { false };
bool m_num_lock_on { false };
bool m_has_e0_prefix { false };
- EntropySource m_entropy_source;
-
- Keyboard::CharacterMap m_character_map;
-};
-class KeyboardClient {
-public:
- virtual ~KeyboardClient();
- virtual void on_key_pressed(KeyboardDevice::Event) = 0;
+ void key_state_changed(u8 raw, bool pressed);
};
-
}
diff --git a/Kernel/Devices/HID/MouseDevice.cpp b/Kernel/Devices/HID/MouseDevice.cpp
new file mode 100644
index 0000000000..592a36208d
--- /dev/null
+++ b/Kernel/Devices/HID/MouseDevice.cpp
@@ -0,0 +1,77 @@
+/*
+ * Copyright (c) 2021, Liav A. <liavalb@hotmail.co.il>
+ * 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/Devices/HID/HIDManagement.h>
+#include <Kernel/Devices/HID/MouseDevice.h>
+
+namespace Kernel {
+
+MouseDevice::MouseDevice()
+ : HIDDevice(10, HIDManagement::the().generate_minor_device_number_for_mouse())
+{
+}
+
+MouseDevice::~MouseDevice()
+{
+}
+
+bool MouseDevice::can_read(const FileDescription&, size_t) const
+{
+ ScopedSpinLock lock(m_queue_lock);
+ return !m_queue.is_empty();
+}
+
+KResultOr<size_t> MouseDevice::read(FileDescription&, u64, UserOrKernelBuffer& buffer, size_t size)
+{
+ VERIFY(size > 0);
+ size_t nread = 0;
+ size_t remaining_space_in_buffer = static_cast<size_t>(size) - nread;
+ ScopedSpinLock lock(m_queue_lock);
+ while (!m_queue.is_empty() && remaining_space_in_buffer) {
+ auto packet = m_queue.dequeue();
+ lock.unlock();
+
+ dbgln_if(MOUSE_DEBUG, "Mouse Read: Buttons {:x}", packet.buttons);
+ dbgln_if(MOUSE_DEBUG, "PS2 Mouse: X {}, Y {}, Z {}, Relative {}", packet.x, packet.y, packet.z, packet.buttons);
+ dbgln_if(MOUSE_DEBUG, "PS2 Mouse Read: Filter packets");
+
+ size_t bytes_read_from_packet = min(remaining_space_in_buffer, sizeof(MousePacket));
+ if (!buffer.write(&packet, nread, bytes_read_from_packet))
+ return EFAULT;
+ nread += bytes_read_from_packet;
+ remaining_space_in_buffer -= bytes_read_from_packet;
+
+ lock.lock();
+ }
+ return nread;
+}
+
+KResultOr<size_t> MouseDevice::write(FileDescription&, u64, const UserOrKernelBuffer&, size_t)
+{
+ return 0;
+}
+
+}
diff --git a/Kernel/Devices/HID/MouseDevice.h b/Kernel/Devices/HID/MouseDevice.h
new file mode 100644
index 0000000000..1351d82bbb
--- /dev/null
+++ b/Kernel/Devices/HID/MouseDevice.h
@@ -0,0 +1,70 @@
+/*
+ * Copyright (c) 2021, Liav A. <liavalb@hotmail.co.il>
+ * 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 <AK/CircularQueue.h>
+#include <AK/DoublyLinkedList.h>
+#include <AK/Types.h>
+#include <Kernel/API/KeyCode.h>
+#include <Kernel/API/MousePacket.h>
+#include <Kernel/Devices/CharacterDevice.h>
+#include <Kernel/Devices/HID/HIDDevice.h>
+#include <Kernel/Interrupts/IRQHandler.h>
+#include <Kernel/Random.h>
+
+namespace Kernel {
+
+class MouseDevice : public HIDDevice {
+public:
+ virtual ~MouseDevice() override;
+
+ // ^CharacterDevice
+ virtual KResultOr<size_t> read(FileDescription&, u64, UserOrKernelBuffer&, size_t) override;
+ virtual bool can_read(const FileDescription&, size_t) const override;
+ virtual KResultOr<size_t> write(FileDescription&, u64, const UserOrKernelBuffer&, size_t) override;
+ virtual bool can_write(const FileDescription&, size_t) const override { return true; }
+
+ // ^HIDDevice
+ virtual Type instrument_type() const { return Type::Mouse; }
+
+ // ^Device
+ virtual mode_t required_mode() const override { return 0440; }
+
+ //FIXME: It should be something like String::formatted("mouse{}", minor())
+ // instead of a fixed string like this
+ virtual String device_name() const override { return "mouse"; }
+
+protected:
+ MouseDevice();
+ // ^CharacterDevice
+ virtual const char* class_name() const override { return "MouseDevice"; }
+
+ mutable SpinLock<u8> m_queue_lock;
+ CircularQueue<MousePacket, 100> m_queue;
+};
+
+}
diff --git a/Kernel/Devices/HID/PS2KeyboardDevice.cpp b/Kernel/Devices/HID/PS2KeyboardDevice.cpp
new file mode 100644
index 0000000000..f2a822214c
--- /dev/null
+++ b/Kernel/Devices/HID/PS2KeyboardDevice.cpp
@@ -0,0 +1,133 @@
+/*
+ * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
+ * Copyright (c) 2021, Liav A. <liavalb@hotmail.co.il>
+ * 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 <AK/Assertions.h>
+#include <AK/ByteBuffer.h>
+#include <AK/Singleton.h>
+#include <AK/StringView.h>
+#include <AK/Types.h>
+#include <Kernel/Arch/x86/CPU.h>
+#include <Kernel/Debug.h>
+#include <Kernel/Devices/HID/HIDManagement.h>
+#include <Kernel/Devices/HID/PS2KeyboardDevice.h>
+#include <Kernel/IO.h>
+#include <Kernel/TTY/VirtualConsole.h>
+
+namespace Kernel {
+
+#define IRQ_KEYBOARD 1
+
+void PS2KeyboardDevice::irq_handle_byte_read(u8 byte)
+{
+ u8 ch = byte & 0x7f;
+ bool pressed = !(byte & 0x80);
+
+ m_entropy_source.add_random_event(byte);
+
+ if (byte == 0xe0) {
+ m_has_e0_prefix = true;
+ return;
+ }
+
+ dbgln_if(KEYBOARD_DEBUG, "Keyboard::irq_handle_byte_read: {:#02x} {}", ch, (pressed ? "down" : "up"));
+ 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_Super, 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);
+ }
+ }
+}
+
+void PS2KeyboardDevice::handle_irq(const RegisterState&)
+{
+ // The controller will read the data and call irq_handle_byte_read
+ // for the appropriate device
+ m_i8042_controller->irq_process_input_buffer(HIDDevice::Type::Keyboard);
+}
+
+UNMAP_AFTER_INIT RefPtr<PS2KeyboardDevice> PS2KeyboardDevice::try_to_initialize(const I8042Controller& ps2_controller)
+{
+ auto device = adopt(*new PS2KeyboardDevice(ps2_controller));
+ if (device->initialize())
+ return device;
+ return nullptr;
+}
+
+UNMAP_AFTER_INIT bool PS2KeyboardDevice::initialize()
+{
+ if (!m_i8042_controller->reset_device(HIDDevice::Type::Keyboard)) {
+ dbgln("KeyboardDevice: I8042 controller failed to reset device");
+ return false;
+ }
+ return true;
+}
+
+// FIXME: UNMAP_AFTER_INIT might not be correct, because in practice PS/2 devices
+// are hot pluggable.
+UNMAP_AFTER_INIT PS2KeyboardDevice::PS2KeyboardDevice(const I8042Controller& ps2_controller)
+ : IRQHandler(IRQ_KEYBOARD)
+ , KeyboardDevice()
+ , I8042Device(ps2_controller)
+{
+}
+
+// FIXME: UNMAP_AFTER_INIT might not be correct, because in practice PS/2 devices
+// are hot pluggable.
+UNMAP_AFTER_INIT PS2KeyboardDevice::~PS2KeyboardDevice()
+{
+}
+
+}
diff --git a/Kernel/Devices/HID/PS2KeyboardDevice.h b/Kernel/Devices/HID/PS2KeyboardDevice.h
new file mode 100644
index 0000000000..028c651696
--- /dev/null
+++ b/Kernel/Devices/HID/PS2KeyboardDevice.h
@@ -0,0 +1,69 @@
+/*
+ * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
+ * 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 <AK/CircularQueue.h>
+#include <AK/DoublyLinkedList.h>
+#include <AK/Types.h>
+#include <Kernel/API/KeyCode.h>
+#include <Kernel/Devices/HID/I8042Controller.h>
+#include <Kernel/Devices/HID/KeyboardDevice.h>
+#include <Kernel/Interrupts/IRQHandler.h>
+#include <Kernel/Random.h>
+#include <LibKeyboard/CharacterMap.h>
+
+namespace Kernel {
+
+class PS2KeyboardDevice final : public IRQHandler
+ , public KeyboardDevice
+ , public I8042Device {
+ AK_MAKE_ETERNAL
+public:
+ static RefPtr<PS2KeyboardDevice> try_to_initialize(const I8042Controller&);
+ virtual ~PS2KeyboardDevice() override;
+ bool initialize();
+
+ 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:
+ explicit PS2KeyboardDevice(const I8042Controller&);
+
+ // ^IRQHandler
+ virtual void handle_irq(const RegisterState&) override;
+
+ // ^CharacterDevice
+ virtual const char* class_name() const override { return "KeyboardDevice"; }
+};
+
+}
diff --git a/Kernel/Devices/PS2MouseDevice.cpp b/Kernel/Devices/HID/PS2MouseDevice.cpp
index aadffead5c..0bda38d9fd 100644
--- a/Kernel/Devices/PS2MouseDevice.cpp
+++ b/Kernel/Devices/HID/PS2MouseDevice.cpp
@@ -27,7 +27,7 @@
#include <AK/Memory.h>
#include <AK/Singleton.h>
#include <Kernel/Debug.h>
-#include <Kernel/Devices/PS2MouseDevice.h>
+#include <Kernel/Devices/HID/PS2MouseDevice.h>
#include <Kernel/Devices/VMWareBackdoor.h>
#include <Kernel/IO.h>
@@ -49,12 +49,10 @@ namespace Kernel {
#define PS2MOUSE_INTELLIMOUSE_ID 0x03
#define PS2MOUSE_INTELLIMOUSE_EXPLORER_ID 0x04
-static AK::Singleton<PS2MouseDevice> s_the;
-
-UNMAP_AFTER_INIT PS2MouseDevice::PS2MouseDevice()
+UNMAP_AFTER_INIT PS2MouseDevice::PS2MouseDevice(const I8042Controller& ps2_controller)
: IRQHandler(IRQ_MOUSE)
- , CharacterDevice(10, 1)
- , m_controller(I8042Controller::the())
+ , MouseDevice()
+ , I8042Device(ps2_controller)
{
}
@@ -62,41 +60,15 @@ UNMAP_AFTER_INIT PS2MouseDevice::~PS2MouseDevice()
{
}
-PS2MouseDevice& PS2MouseDevice::the()
-{
- return *s_the;
-}
-
void PS2MouseDevice::handle_irq(const RegisterState&)
{
// 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);
+ m_i8042_controller->irq_process_input_buffer(instrument_type());
}
void PS2MouseDevice::irq_handle_byte_read(u8 byte)
{
- auto* backdoor = VMWareBackdoor::the();
-
- if (backdoor && backdoor->vmmouse_is_absolute()) {
- // We won't receive complete packets with the backdoor enabled,
- // we will only get one byte for each event, which we'll just
- // discard. If we were to wait until we *think* that we got a
- // full PS/2 packet then we would create a backlog in the VM
- // because we wouldn't read the appropriate number of mouse
- // packets from VMWareBackdoor.
- auto mouse_packet = backdoor->receive_mouse_packet();
- if (mouse_packet.has_value()) {
- m_entropy_source.add_random_event(mouse_packet.value());
- {
- ScopedSpinLock lock(m_queue_lock);
- m_queue.enqueue(mouse_packet.value());
- }
- evaluate_block_conditions();
- }
- return;
- }
-
auto commit_packet = [&] {
m_data_state = 0;
dbgln_if(PS2MOUSE_DEBUG, "PS2Mouse: {}, {} {} {}",
@@ -198,12 +170,12 @@ u8 PS2MouseDevice::get_device_id()
u8 PS2MouseDevice::read_from_device()
{
- return m_controller.read_from_device(I8042Controller::Device::Mouse);
+ return m_i8042_controller->read_from_device(instrument_type());
}
u8 PS2MouseDevice::send_command(u8 command)
{
- u8 response = m_controller.send_command(I8042Controller::Device::Mouse, command);
+ u8 response = m_i8042_controller->send_command(instrument_type(), command);
if (response != I8042_ACK)
dbgln("PS2MouseDevice: Command {} got {} but expected ack: {}", command, response, I8042_ACK);
return response;
@@ -211,7 +183,7 @@ u8 PS2MouseDevice::send_command(u8 command)
u8 PS2MouseDevice::send_command(u8 command, u8 data)
{
- u8 response = m_controller.send_command(I8042Controller::Device::Mouse, command, data);
+ u8 response = m_i8042_controller->send_command(instrument_type(), command, data);
if (response != I8042_ACK)
dbgln("PS2MouseDevice: Command {} got {} but expected ack: {}", command, response, I8042_ACK);
return response;
@@ -222,9 +194,17 @@ void PS2MouseDevice::set_sample_rate(u8 rate)
send_command(PS2MOUSE_SET_SAMPLE_RATE, rate);
}
+UNMAP_AFTER_INIT RefPtr<PS2MouseDevice> PS2MouseDevice::try_to_initialize(const I8042Controller& ps2_controller)
+{
+ auto device = adopt(*new PS2MouseDevice(ps2_controller));
+ if (device->initialize())
+ return device;
+ return nullptr;
+}
+
UNMAP_AFTER_INIT bool PS2MouseDevice::initialize()
{
- if (!m_controller.reset_device(I8042Controller::Device::Mouse)) {
+ if (!m_i8042_controller->reset_device(instrument_type())) {
dbgln("PS2MouseDevice: I8042 controller failed to reset device");
return false;
}
@@ -267,42 +247,4 @@ UNMAP_AFTER_INIT bool PS2MouseDevice::initialize()
return true;
}
-bool PS2MouseDevice::can_read(const FileDescription&, size_t) const
-{
- ScopedSpinLock lock(m_queue_lock);
- return !m_queue.is_empty();
-}
-
-KResultOr<size_t> PS2MouseDevice::read(FileDescription&, u64, UserOrKernelBuffer& buffer, size_t size)
-{
- VERIFY(size > 0);
- size_t nread = 0;
- size_t remaining_space_in_buffer = static_cast<size_t>(size) - nread;
- ScopedSpinLock lock(m_queue_lock);
- while (!m_queue.is_empty() && remaining_space_in_buffer) {
- auto packet = m_queue.dequeue();
- lock.unlock();
-
- if constexpr (PS2MOUSE_DEBUG) {
- dbgln("PS2 Mouse Read: Buttons {:x}", packet.buttons);
- dbgln("PS2 Mouse: X {}, Y {}, Z {}, Relative {}", packet.x, packet.y, packet.z, packet.buttons);
- dbgln("PS2 Mouse Read: Filter packets");
- }
-
- size_t bytes_read_from_packet = min(remaining_space_in_buffer, sizeof(MousePacket));
- if (!buffer.write(&packet, nread, bytes_read_from_packet))
- return EFAULT;
- nread += bytes_read_from_packet;
- remaining_space_in_buffer -= bytes_read_from_packet;
-
- lock.lock();
- }
- return nread;
-}
-
-KResultOr<size_t> PS2MouseDevice::write(FileDescription&, u64, const UserOrKernelBuffer&, size_t)
-{
- return 0;
-}
-
}
diff --git a/Kernel/Devices/PS2MouseDevice.h b/Kernel/Devices/HID/PS2MouseDevice.h
index cc595eafc0..e7e761d4cb 100644
--- a/Kernel/Devices/PS2MouseDevice.h
+++ b/Kernel/Devices/HID/PS2MouseDevice.h
@@ -28,29 +28,20 @@
#include <AK/CircularQueue.h>
#include <Kernel/API/MousePacket.h>
-#include <Kernel/Devices/CharacterDevice.h>
-#include <Kernel/Devices/I8042Controller.h>
+#include <Kernel/Devices/HID/I8042Controller.h>
+#include <Kernel/Devices/HID/MouseDevice.h>
#include <Kernel/Interrupts/IRQHandler.h>
#include <Kernel/Random.h>
namespace Kernel {
-
-class PS2MouseDevice final : public IRQHandler
- , public CharacterDevice
+class PS2MouseDevice : public IRQHandler
+ , public MouseDevice
, public I8042Device {
public:
- PS2MouseDevice();
- virtual ~PS2MouseDevice() override;
-
- static PS2MouseDevice& the();
-
+ static RefPtr<PS2MouseDevice> try_to_initialize(const I8042Controller&);
bool initialize();
- // ^CharacterDevice
- virtual bool can_read(const FileDescription&, size_t) const override;
- virtual KResultOr<size_t> read(FileDescription&, u64, UserOrKernelBuffer&, size_t) override;
- virtual KResultOr<size_t> write(FileDescription&, u64, const UserOrKernelBuffer&, size_t) override;
- virtual bool can_write(const FileDescription&, size_t) const override { return true; }
+ virtual ~PS2MouseDevice() override;
virtual const char* purpose() const override { return class_name(); }
@@ -61,18 +52,11 @@ public:
enable_irq();
}
- // ^Device
- virtual mode_t required_mode() const override { return 0440; }
- virtual String device_name() const override { return "mouse"; }
-
-private:
+protected:
+ explicit PS2MouseDevice(const I8042Controller&);
// ^IRQHandler
- void handle_vmmouse_absolute_pointer();
virtual void handle_irq(const RegisterState&) override;
- // ^CharacterDevice
- virtual const char* class_name() const override { return "PS2MouseDevice"; }
-
struct RawPacket {
union {
u32 dword;
@@ -87,14 +71,10 @@ private:
void set_sample_rate(u8);
u8 get_device_id();
- I8042Controller& m_controller;
- mutable SpinLock<u8> m_queue_lock;
- CircularQueue<MousePacket, 100> m_queue;
u8 m_data_state { 0 };
RawPacket m_data;
bool m_has_wheel { false };
bool m_has_five_buttons { false };
- EntropySource m_entropy_source;
};
}
diff --git a/Kernel/Devices/HID/VMWareMouseDevice.cpp b/Kernel/Devices/HID/VMWareMouseDevice.cpp
new file mode 100644
index 0000000000..3d4bbe48e7
--- /dev/null
+++ b/Kernel/Devices/HID/VMWareMouseDevice.cpp
@@ -0,0 +1,74 @@
+/*
+ * Copyright (c) 2021, Liav A. <liavalb@hotmail.co.il>
+ * 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/Devices/HID/VMWareMouseDevice.h>
+#include <Kernel/Devices/VMWareBackdoor.h>
+
+namespace Kernel {
+
+UNMAP_AFTER_INIT RefPtr<VMWareMouseDevice> VMWareMouseDevice::try_to_initialize(const I8042Controller& ps2_controller)
+{
+ if (!VMWareBackdoor::the())
+ return {};
+ if (!VMWareBackdoor::the()->vmmouse_is_absolute())
+ return {};
+ auto device = adopt(*new VMWareMouseDevice(ps2_controller));
+ if (device->initialize())
+ return device;
+ return {};
+}
+
+void VMWareMouseDevice::irq_handle_byte_read(u8)
+{
+ VERIFY(VMWareBackdoor::the());
+ VERIFY(VMWareBackdoor::the()->vmmouse_is_absolute());
+ // We won't receive complete packets with the backdoor enabled,
+ // we will only get one byte for each event, which we'll just
+ // discard. If we were to wait until we *think* that we got a
+ // full PS/2 packet then we would create a backlog in the VM
+ // because we wouldn't read the appropriate number of mouse
+ // packets from VMWareBackdoor.
+ auto mouse_packet = VMWareBackdoor::the()->receive_mouse_packet();
+ if (mouse_packet.has_value()) {
+ m_entropy_source.add_random_event(mouse_packet.value());
+ {
+ ScopedSpinLock lock(m_queue_lock);
+ m_queue.enqueue(mouse_packet.value());
+ }
+ evaluate_block_conditions();
+ }
+ return;
+}
+
+VMWareMouseDevice::VMWareMouseDevice(const I8042Controller& ps2_controller)
+ : PS2MouseDevice(ps2_controller)
+{
+}
+VMWareMouseDevice::~VMWareMouseDevice()
+{
+}
+
+}
diff --git a/Kernel/Devices/HID/VMWareMouseDevice.h b/Kernel/Devices/HID/VMWareMouseDevice.h
new file mode 100644
index 0000000000..1df3747358
--- /dev/null
+++ b/Kernel/Devices/HID/VMWareMouseDevice.h
@@ -0,0 +1,50 @@
+/*
+ * Copyright (c) 2021, Liav A. <liavalb@hotmail.co.il>
+ * 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 <AK/CircularQueue.h>
+#include <Kernel/API/MousePacket.h>
+#include <Kernel/Devices/HID/I8042Controller.h>
+#include <Kernel/Devices/HID/PS2MouseDevice.h>
+#include <Kernel/Interrupts/IRQHandler.h>
+#include <Kernel/Random.h>
+
+namespace Kernel {
+
+class VMWareMouseDevice final : public PS2MouseDevice {
+public:
+ static RefPtr<VMWareMouseDevice> try_to_initialize(const I8042Controller&);
+ virtual ~VMWareMouseDevice() override;
+
+ // ^I8042Device
+ virtual void irq_handle_byte_read(u8 byte) override;
+
+private:
+ explicit VMWareMouseDevice(const I8042Controller&);
+};
+
+}
diff --git a/Kernel/FileSystem/ProcFS.cpp b/Kernel/FileSystem/ProcFS.cpp
index a8cdfc5aa2..7158c7f1ed 100644
--- a/Kernel/FileSystem/ProcFS.cpp
+++ b/Kernel/FileSystem/ProcFS.cpp
@@ -36,7 +36,7 @@
#include <Kernel/DMI.h>
#include <Kernel/Debug.h>
#include <Kernel/Devices/BlockDevice.h>
-#include <Kernel/Devices/KeyboardDevice.h>
+#include <Kernel/Devices/HID/HIDManagement.h>
#include <Kernel/FileSystem/Custody.h>
#include <Kernel/FileSystem/FileBackedFileSystem.h>
#include <Kernel/FileSystem/FileDescription.h>
@@ -418,7 +418,7 @@ static bool procfs$interrupts(InodeIdentifier, KBufferBuilder& builder)
static bool procfs$keymap(InodeIdentifier, KBufferBuilder& builder)
{
JsonObjectSerializer<KBufferBuilder> json { builder };
- json.add("keymap", KeyboardDevice::the().keymap_name());
+ json.add("keymap", HIDManagement::the().keymap_name());
json.finish();
return true;
}
diff --git a/Kernel/Syscalls/keymap.cpp b/Kernel/Syscalls/keymap.cpp
index e22a8dd377..d9eae8be9a 100644
--- a/Kernel/Syscalls/keymap.cpp
+++ b/Kernel/Syscalls/keymap.cpp
@@ -24,7 +24,7 @@
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
-#include <Kernel/Devices/KeyboardDevice.h>
+#include <Kernel/Devices/HID/HIDManagement.h>
#include <Kernel/Process.h>
namespace Kernel {
@@ -62,8 +62,7 @@ KResultOr<int> Process::sys$setkeymap(Userspace<const Syscall::SC_setkeymap_para
if (map_name.value().length() > map_name_max_size) {
return ENAMETOOLONG;
}
-
- KeyboardDevice::the().set_maps(character_map_data, map_name.value());
+ HIDManagement::the().set_maps(character_map_data, map_name.value());
return 0;
}
@@ -75,8 +74,8 @@ KResultOr<int> Process::sys$getkeymap(Userspace<const Syscall::SC_getkeymap_para
if (!copy_from_user(&params, user_params))
return EFAULT;
- String keymap_name = KeyboardDevice::the().keymap_name();
- const Keyboard::CharacterMapData& character_maps = KeyboardDevice::the().character_maps();
+ String keymap_name = HIDManagement::the().keymap_name();
+ const Keyboard::CharacterMapData& character_maps = HIDManagement::the().character_maps();
if (!copy_to_user(params.map, character_maps.map, CHAR_MAP_SIZE * sizeof(u32)))
return EFAULT;
diff --git a/Kernel/TTY/VirtualConsole.cpp b/Kernel/TTY/VirtualConsole.cpp
index 252d0bc6f5..3a94b29996 100644
--- a/Kernel/TTY/VirtualConsole.cpp
+++ b/Kernel/TTY/VirtualConsole.cpp
@@ -28,7 +28,7 @@
#include "VirtualConsole.h"
#include <AK/String.h>
#include <Kernel/Arch/x86/CPU.h>
-#include <Kernel/Devices/KeyboardDevice.h>
+#include <Kernel/Devices/HID/HIDManagement.h>
#include <Kernel/Heap/kmalloc.h>
#include <Kernel/IO.h>
#include <Kernel/StdLib.h>
@@ -116,12 +116,12 @@ void VirtualConsole::set_active(bool active)
if (active) {
set_vga_start_row(0);
- KeyboardDevice::the().set_client(this);
+ HIDManagement::the().set_client(this);
m_terminal.m_need_full_flush = true;
flush_dirty_lines();
} else {
- KeyboardDevice::the().set_client(nullptr);
+ HIDManagement::the().set_client(nullptr);
}
}
@@ -220,7 +220,7 @@ void VirtualConsole::clear_vga_row(u16 row)
linemem[i] = 0x0720;
}
-void VirtualConsole::on_key_pressed(KeyboardDevice::Event event)
+void VirtualConsole::on_key_pressed(KeyEvent event)
{
// Ignore keyboard in graphical mode.
if (m_graphical)
diff --git a/Kernel/TTY/VirtualConsole.h b/Kernel/TTY/VirtualConsole.h
index 20fe63c821..d2829fd993 100644
--- a/Kernel/TTY/VirtualConsole.h
+++ b/Kernel/TTY/VirtualConsole.h
@@ -27,7 +27,7 @@
#pragma once
#include <Kernel/Console.h>
-#include <Kernel/Devices/KeyboardDevice.h>
+#include <Kernel/Devices/HID/HIDManagement.h>
#include <Kernel/TTY/TTY.h>
#include <LibVT/Terminal.h>
@@ -51,7 +51,7 @@ public:
private:
// ^KeyboardClient
- virtual void on_key_pressed(KeyboardDevice::Event) override;
+ virtual void on_key_pressed(KeyEvent) override;
// ^TTY
virtual ssize_t on_tty_write(const UserOrKernelBuffer&, ssize_t) override;
diff --git a/Kernel/init.cpp b/Kernel/init.cpp
index 847bf7ad34..4d13c7384e 100644
--- a/Kernel/init.cpp
+++ b/Kernel/init.cpp
@@ -34,7 +34,7 @@
#include <Kernel/DMI.h>
#include <Kernel/Devices/BXVGADevice.h>
#include <Kernel/Devices/FullDevice.h>
-#include <Kernel/Devices/I8042Controller.h>
+#include <Kernel/Devices/HID/HIDManagement.h>
#include <Kernel/Devices/MBVGADevice.h>
#include <Kernel/Devices/MemoryDevice.h>
#include <Kernel/Devices/NullDevice.h>
@@ -163,7 +163,6 @@ extern "C" UNMAP_AFTER_INIT [[noreturn]] void init()
ACPI::initialize();
VFS::initialize();
- I8042Controller::initialize();
Console::initialize();
dmesgln("Starting SerenityOS...");
@@ -179,6 +178,8 @@ extern "C" UNMAP_AFTER_INIT [[noreturn]] void init()
new SerialDevice(SERIAL_COM3_ADDR, 66);
new SerialDevice(SERIAL_COM4_ADDR, 67);
+ VMWareBackdoor::the(); // don't wait until first mouse packet
+ HIDManagement::initialize();
VirtualConsole::initialize();
tty0 = new VirtualConsole(0);
for (unsigned i = 1; i < s_max_virtual_consoles; i++) {
@@ -295,7 +296,6 @@ void init_stage2(void*)
new RandomDevice;
PTYMultiplexer::initialize();
SB16::detect();
- VMWareBackdoor::the(); // don't wait until first mouse packet
StorageManagement::initialize(kernel_command_line().root_device(), kernel_command_line().is_force_pio());
if (!VFS::the().mount_root(StorageManagement::the().root_filesystem())) {
diff --git a/Meta/CMake/all_the_debug_macros.cmake b/Meta/CMake/all_the_debug_macros.cmake
index f533be05a9..37cadb50b0 100644
--- a/Meta/CMake/all_the_debug_macros.cmake
+++ b/Meta/CMake/all_the_debug_macros.cmake
@@ -14,6 +14,7 @@ set(CONTEXT_SWITCH_DEBUG ON)
set(SMP_DEBUG ON)
set(BXVGA_DEBUG ON)
set(PS2MOUSE_DEBUG ON)
+set(MOUSE_DEBUG ON)
set(VMWARE_BACKDOOR_DEBUG ON)
set(FILEDESCRIPTION_DEBUG ON)
set(PROCFS_DEBUG ON)