2020-01-18 11:38:21 +03:00
|
|
|
/*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
2020-03-08 14:33:14 +03:00
|
|
|
#include <AK/Memory.h>
|
2020-02-04 04:00:50 +03:00
|
|
|
#include <Kernel/Devices/PS2MouseDevice.h>
|
|
|
|
#include <Kernel/Devices/VMWareBackdoor.h>
|
2020-02-09 17:47:15 +03:00
|
|
|
#include <LibBareMetal/IO.h>
|
2019-01-11 04:28:53 +03:00
|
|
|
|
2020-02-16 03:27:42 +03:00
|
|
|
namespace Kernel {
|
|
|
|
|
2020-01-05 12:37:25 +03:00
|
|
|
#define IRQ_MOUSE 12
|
2019-06-07 12:43:58 +03:00
|
|
|
#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
|
2019-02-07 09:55:53 +03:00
|
|
|
|
2019-08-12 13:49:17 +03:00
|
|
|
#define PS2MOUSE_SET_RESOLUTION 0xE8
|
|
|
|
#define PS2MOUSE_STATUS_REQUEST 0xE9
|
|
|
|
#define PS2MOUSE_REQUEST_SINGLE_PACKET 0xEB
|
2019-06-07 12:43:58 +03:00
|
|
|
#define PS2MOUSE_GET_DEVICE_ID 0xF2
|
2019-05-13 20:48:14 +03:00
|
|
|
#define PS2MOUSE_SET_SAMPLE_RATE 0xF3
|
2019-08-12 13:49:17 +03:00
|
|
|
#define PS2MOUSE_ENABLE_PACKET_STREAMING 0xF4
|
|
|
|
#define PS2MOUSE_DISABLE_PACKET_STREAMING 0xF5
|
|
|
|
#define PS2MOUSE_SET_DEFAULTS 0xF6
|
|
|
|
#define PS2MOUSE_RESEND 0xFE
|
|
|
|
#define PS2MOUSE_RESET 0xFF
|
2019-05-13 20:48:14 +03:00
|
|
|
|
|
|
|
#define PS2MOUSE_INTELLIMOUSE_ID 0x03
|
2020-05-02 23:06:17 +03:00
|
|
|
#define PS2MOUSE_INTELLIMOUSE_EXPLORER_ID 0x04
|
2019-05-13 20:48:14 +03:00
|
|
|
|
2019-01-12 05:20:48 +03:00
|
|
|
//#define PS2MOUSE_DEBUG
|
|
|
|
|
2019-01-11 05:52:09 +03:00
|
|
|
static PS2MouseDevice* s_the;
|
|
|
|
|
2019-01-11 04:28:53 +03:00
|
|
|
PS2MouseDevice::PS2MouseDevice()
|
2020-01-23 00:23:50 +03:00
|
|
|
: IRQHandler(IRQ_MOUSE)
|
2019-01-11 04:28:53 +03:00
|
|
|
, CharacterDevice(10, 1)
|
|
|
|
{
|
2019-01-11 05:52:09 +03:00
|
|
|
s_the = this;
|
2019-01-11 04:28:53 +03:00
|
|
|
initialize();
|
|
|
|
}
|
|
|
|
|
|
|
|
PS2MouseDevice::~PS2MouseDevice()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2019-01-11 05:52:09 +03:00
|
|
|
PS2MouseDevice& PS2MouseDevice::the()
|
|
|
|
{
|
|
|
|
return *s_the;
|
|
|
|
}
|
|
|
|
|
2020-03-09 17:24:29 +03:00
|
|
|
void PS2MouseDevice::handle_irq(const RegisterState&)
|
2019-01-11 04:28:53 +03:00
|
|
|
{
|
2020-04-08 17:35:00 +03:00
|
|
|
if (auto* backdoor = VMWareBackdoor::the()) {
|
|
|
|
if (backdoor->vmmouse_is_absolute()) {
|
|
|
|
IO::in8(I8042_BUFFER);
|
|
|
|
auto packet = backdoor->receive_mouse_packet();
|
|
|
|
if (packet.has_value())
|
|
|
|
m_queue.enqueue(packet.value());
|
|
|
|
return;
|
|
|
|
}
|
2020-02-04 04:00:50 +03:00
|
|
|
}
|
2019-02-07 09:55:53 +03:00
|
|
|
for (;;) {
|
2019-07-03 22:17:35 +03:00
|
|
|
u8 status = IO::in8(I8042_STATUS);
|
2019-02-07 09:55:53 +03:00
|
|
|
if (!(((status & I8042_WHICH_BUFFER) == I8042_MOUSE_BUFFER) && (status & I8042_BUFFER_FULL)))
|
|
|
|
return;
|
|
|
|
|
2019-07-03 22:17:35 +03:00
|
|
|
u8 data = IO::in8(I8042_BUFFER);
|
2019-02-07 09:55:53 +03:00
|
|
|
m_data[m_data_state] = data;
|
2019-05-13 20:48:14 +03:00
|
|
|
|
|
|
|
auto commit_packet = [&] {
|
|
|
|
m_data_state = 0;
|
|
|
|
#ifdef PS2MOUSE_DEBUG
|
2020-03-07 00:15:41 +03:00
|
|
|
dbg() << "PS2Mouse: " << m_data[1] << ", " << m_data[2] << " " << ((m_data[0] & 1) ? "Left" : "") << " " << ((m_data[0] & 2) ? "Right" : "") << " (buffered: " << m_queue.size() << ")";
|
2019-05-13 20:48:14 +03:00
|
|
|
#endif
|
|
|
|
parse_data_packet();
|
|
|
|
};
|
|
|
|
|
2019-02-07 09:55:53 +03:00
|
|
|
switch (m_data_state) {
|
|
|
|
case 0:
|
|
|
|
if (!(data & 0x08)) {
|
2020-02-24 17:58:59 +03:00
|
|
|
dbg() << "PS2Mouse: Stream out of sync.";
|
2019-02-07 09:55:53 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
++m_data_state;
|
2019-01-17 04:13:21 +03:00
|
|
|
break;
|
2019-02-07 09:55:53 +03:00
|
|
|
case 1:
|
|
|
|
++m_data_state;
|
|
|
|
break;
|
|
|
|
case 2:
|
2019-05-13 20:48:14 +03:00
|
|
|
if (m_has_wheel) {
|
|
|
|
++m_data_state;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
commit_packet();
|
|
|
|
break;
|
|
|
|
case 3:
|
|
|
|
ASSERT(m_has_wheel);
|
|
|
|
commit_packet();
|
2019-02-07 09:55:53 +03:00
|
|
|
break;
|
|
|
|
}
|
2019-01-11 04:28:53 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-05 15:59:44 +03:00
|
|
|
void PS2MouseDevice::parse_data_packet()
|
|
|
|
{
|
|
|
|
int x = m_data[1];
|
|
|
|
int y = m_data[2];
|
2019-05-13 20:48:14 +03:00
|
|
|
int z = 0;
|
|
|
|
if (m_has_wheel)
|
|
|
|
z = (char)m_data[3];
|
2019-03-05 15:59:44 +03:00
|
|
|
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;
|
|
|
|
if (x && x_sign)
|
|
|
|
x -= 0x100;
|
|
|
|
if (y && y_sign)
|
|
|
|
y -= 0x100;
|
|
|
|
if (x_overflow || y_overflow) {
|
|
|
|
x = 0;
|
|
|
|
y = 0;
|
|
|
|
}
|
|
|
|
MousePacket packet;
|
2020-02-03 19:29:40 +03:00
|
|
|
packet.x = x;
|
|
|
|
packet.y = y;
|
|
|
|
packet.z = z;
|
2019-03-05 15:59:44 +03:00
|
|
|
packet.buttons = m_data[0] & 0x07;
|
2020-05-02 23:06:17 +03:00
|
|
|
|
|
|
|
if (m_has_five_buttons) {
|
|
|
|
if (m_data[3] & 0x10)
|
|
|
|
packet.buttons |= MousePacket::BackButton;
|
|
|
|
if (m_data[3] & 0x20)
|
|
|
|
packet.buttons |= MousePacket::ForwardButton;
|
|
|
|
}
|
|
|
|
|
2020-02-04 04:00:50 +03:00
|
|
|
packet.is_relative = true;
|
|
|
|
#ifdef PS2MOUSE_DEBUG
|
2020-02-24 17:58:59 +03:00
|
|
|
dbg() << "PS2 Relative Mouse: Buttons " << String::format("%x", packet.buttons);
|
|
|
|
dbg() << "Mouse: X " << packet.x << ", Y " << packet.y << ", Z " << packet.z;
|
2020-02-04 04:00:50 +03:00
|
|
|
#endif
|
2019-03-05 15:59:44 +03:00
|
|
|
m_queue.enqueue(packet);
|
|
|
|
}
|
|
|
|
|
2019-07-03 22:17:35 +03:00
|
|
|
void PS2MouseDevice::wait_then_write(u8 port, u8 data)
|
2019-01-11 04:28:53 +03:00
|
|
|
{
|
|
|
|
prepare_for_output();
|
|
|
|
IO::out8(port, data);
|
|
|
|
}
|
|
|
|
|
2019-07-03 22:17:35 +03:00
|
|
|
u8 PS2MouseDevice::wait_then_read(u8 port)
|
2019-01-11 04:28:53 +03:00
|
|
|
{
|
|
|
|
prepare_for_input();
|
|
|
|
return IO::in8(port);
|
|
|
|
}
|
|
|
|
|
|
|
|
void PS2MouseDevice::initialize()
|
|
|
|
{
|
|
|
|
// Enable PS aux port
|
2019-08-12 13:49:17 +03:00
|
|
|
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;
|
2020-03-01 22:45:39 +03:00
|
|
|
klog() << "PS2MouseDevice: Device detected";
|
2019-08-12 13:49:17 +03:00
|
|
|
|
|
|
|
// 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;
|
2020-03-01 22:45:39 +03:00
|
|
|
klog() << "PS2MouseDevice: Device not detected";
|
2019-08-12 13:49:17 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-02 23:06:17 +03:00
|
|
|
u8 PS2MouseDevice::get_device_id()
|
|
|
|
{
|
|
|
|
mouse_write(PS2MOUSE_GET_DEVICE_ID);
|
|
|
|
expect_ack();
|
|
|
|
return mouse_read();
|
|
|
|
}
|
|
|
|
|
|
|
|
void PS2MouseDevice::set_sample_rate(u8 rate)
|
|
|
|
{
|
|
|
|
mouse_write(PS2MOUSE_SET_SAMPLE_RATE);
|
|
|
|
expect_ack();
|
|
|
|
mouse_write(rate);
|
|
|
|
expect_ack();
|
|
|
|
}
|
|
|
|
|
2019-08-12 13:49:17 +03:00
|
|
|
void PS2MouseDevice::initialize_device()
|
|
|
|
{
|
|
|
|
if (!m_device_present)
|
|
|
|
return;
|
2019-01-11 04:28:53 +03:00
|
|
|
|
|
|
|
// Enable interrupts
|
2019-08-12 13:49:17 +03:00
|
|
|
wait_then_write(I8042_STATUS, 0x20);
|
2019-01-11 04:28:53 +03:00
|
|
|
|
2019-02-07 09:55:53 +03:00
|
|
|
// Enable the PS/2 mouse IRQ (12).
|
|
|
|
// NOTE: The keyboard uses IRQ 1 (and is enabled by bit 0 in this register).
|
2019-08-12 13:49:17 +03:00
|
|
|
u8 status = wait_then_read(I8042_BUFFER) | 2;
|
|
|
|
wait_then_write(I8042_STATUS, 0x60);
|
|
|
|
wait_then_write(I8042_BUFFER, status);
|
2019-01-11 04:28:53 +03:00
|
|
|
|
|
|
|
// Set default settings.
|
2019-08-12 13:49:17 +03:00
|
|
|
mouse_write(PS2MOUSE_SET_DEFAULTS);
|
2019-05-13 20:48:14 +03:00
|
|
|
expect_ack();
|
2019-01-11 04:28:53 +03:00
|
|
|
|
|
|
|
// Enable.
|
2019-08-12 13:49:17 +03:00
|
|
|
mouse_write(PS2MOUSE_ENABLE_PACKET_STREAMING);
|
2019-05-13 20:48:14 +03:00
|
|
|
expect_ack();
|
|
|
|
|
2020-05-02 23:06:17 +03:00
|
|
|
u8 device_id = get_device_id();
|
2019-05-13 20:48:14 +03:00
|
|
|
|
|
|
|
if (device_id != PS2MOUSE_INTELLIMOUSE_ID) {
|
|
|
|
// Send magical wheel initiation sequence.
|
2020-05-02 23:06:17 +03:00
|
|
|
set_sample_rate(200);
|
|
|
|
set_sample_rate(100);
|
|
|
|
set_sample_rate(80);
|
|
|
|
device_id = get_device_id();
|
2019-05-13 20:48:14 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (device_id == PS2MOUSE_INTELLIMOUSE_ID) {
|
|
|
|
m_has_wheel = true;
|
2020-03-01 22:45:39 +03:00
|
|
|
klog() << "PS2MouseDevice: Mouse wheel enabled!";
|
2019-05-13 20:48:14 +03:00
|
|
|
} else {
|
2020-03-01 22:45:39 +03:00
|
|
|
klog() << "PS2MouseDevice: No mouse wheel detected!";
|
2019-05-13 20:48:14 +03:00
|
|
|
}
|
2019-01-11 04:28:53 +03:00
|
|
|
|
2020-05-02 23:06:17 +03:00
|
|
|
if (device_id == PS2MOUSE_INTELLIMOUSE_ID) {
|
|
|
|
// Try to enable 5 buttons as well!
|
|
|
|
set_sample_rate(200);
|
|
|
|
set_sample_rate(200);
|
|
|
|
set_sample_rate(80);
|
|
|
|
device_id = get_device_id();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (device_id == PS2MOUSE_INTELLIMOUSE_EXPLORER_ID) {
|
|
|
|
m_has_five_buttons = true;
|
|
|
|
klog() << "PS2MouseDevice: 5 buttons enabled!";
|
|
|
|
}
|
|
|
|
|
2020-01-23 00:23:50 +03:00
|
|
|
enable_irq();
|
2019-01-11 04:28:53 +03:00
|
|
|
}
|
|
|
|
|
2019-05-13 20:48:14 +03:00
|
|
|
void PS2MouseDevice::expect_ack()
|
|
|
|
{
|
2019-07-03 22:17:35 +03:00
|
|
|
u8 data = mouse_read();
|
2019-05-13 20:48:14 +03:00
|
|
|
ASSERT(data == I8042_ACK);
|
|
|
|
}
|
|
|
|
|
2019-01-11 04:28:53 +03:00
|
|
|
void PS2MouseDevice::prepare_for_input()
|
|
|
|
{
|
|
|
|
for (;;) {
|
2019-08-12 13:49:17 +03:00
|
|
|
if (IO::in8(I8042_STATUS) & 1)
|
2019-01-11 04:28:53 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void PS2MouseDevice::prepare_for_output()
|
|
|
|
{
|
|
|
|
for (;;) {
|
2019-08-12 13:49:17 +03:00
|
|
|
if (!(IO::in8(I8042_STATUS) & 2))
|
2019-01-11 04:28:53 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-03 22:17:35 +03:00
|
|
|
void PS2MouseDevice::mouse_write(u8 data)
|
2019-01-11 04:28:53 +03:00
|
|
|
{
|
|
|
|
prepare_for_output();
|
2019-08-12 13:49:17 +03:00
|
|
|
IO::out8(I8042_STATUS, 0xd4);
|
2019-01-11 04:28:53 +03:00
|
|
|
prepare_for_output();
|
2019-08-12 13:49:17 +03:00
|
|
|
IO::out8(I8042_BUFFER, data);
|
2019-01-11 04:28:53 +03:00
|
|
|
}
|
|
|
|
|
2019-07-03 22:17:35 +03:00
|
|
|
u8 PS2MouseDevice::mouse_read()
|
2019-01-11 04:28:53 +03:00
|
|
|
{
|
|
|
|
prepare_for_input();
|
2019-08-12 13:49:17 +03:00
|
|
|
return IO::in8(I8042_BUFFER);
|
2019-01-11 04:28:53 +03:00
|
|
|
}
|
|
|
|
|
2020-04-10 12:44:42 +03:00
|
|
|
bool PS2MouseDevice::can_read(const FileDescription&, size_t) const
|
2019-01-11 04:28:53 +03:00
|
|
|
{
|
2019-01-16 03:52:39 +03:00
|
|
|
return !m_queue.is_empty();
|
2019-01-11 04:28:53 +03:00
|
|
|
}
|
|
|
|
|
2020-04-10 12:44:42 +03:00
|
|
|
ssize_t PS2MouseDevice::read(FileDescription&, size_t, u8* buffer, ssize_t size)
|
2019-01-11 04:28:53 +03:00
|
|
|
{
|
2020-02-07 13:25:12 +03:00
|
|
|
ASSERT(size > 0);
|
|
|
|
size_t nread = 0;
|
|
|
|
size_t remaining_space_in_buffer = static_cast<size_t>(size) - nread;
|
|
|
|
while (!m_queue.is_empty() && remaining_space_in_buffer) {
|
2019-03-05 15:59:44 +03:00
|
|
|
auto packet = m_queue.dequeue();
|
2020-02-04 04:00:50 +03:00
|
|
|
#ifdef PS2MOUSE_DEBUG
|
2020-02-24 17:58:59 +03:00
|
|
|
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;
|
|
|
|
dbg() << "PS2 Mouse Read: Filter packets";
|
2020-02-04 04:00:50 +03:00
|
|
|
#endif
|
2020-02-07 13:25:12 +03:00
|
|
|
size_t bytes_read_from_packet = min(remaining_space_in_buffer, sizeof(MousePacket));
|
|
|
|
memcpy(buffer + nread, &packet, bytes_read_from_packet);
|
|
|
|
nread += bytes_read_from_packet;
|
|
|
|
remaining_space_in_buffer -= bytes_read_from_packet;
|
2019-01-16 03:52:39 +03:00
|
|
|
}
|
|
|
|
return nread;
|
2019-01-11 04:28:53 +03:00
|
|
|
}
|
|
|
|
|
2020-04-10 12:44:42 +03:00
|
|
|
ssize_t PS2MouseDevice::write(FileDescription&, size_t, const u8*, ssize_t)
|
2019-01-11 04:28:53 +03:00
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
2020-02-16 03:27:42 +03:00
|
|
|
|
|
|
|
}
|