summaryrefslogtreecommitdiff
path: root/Kernel/Devices/HID/VMWareMouseDevice.cpp
blob: aaeeb4f9c60d2f81c3e38d7851511f97cc7c332a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
/*
 * Copyright (c) 2021, Liav A. <liavalb@hotmail.co.il>
 *
 * SPDX-License-Identifier: BSD-2-Clause
 */

#include <Kernel/Devices/DeviceManagement.h>
#include <Kernel/Devices/HID/VMWareMouseDevice.h>
#include <Kernel/Devices/VMWareBackdoor.h>
#include <Kernel/Sections.h>

namespace Kernel {

UNMAP_AFTER_INIT ErrorOr<NonnullRefPtr<VMWareMouseDevice>> VMWareMouseDevice::try_to_initialize(const I8042Controller& ps2_controller)
{
    // FIXME: return the correct error
    if (!VMWareBackdoor::the())
        return Error::from_errno(EIO);
    if (!VMWareBackdoor::the()->vmmouse_is_absolute())
        return Error::from_errno(EIO);
    auto mouse_device = TRY(DeviceManagement::try_create_device<VMWareMouseDevice>(ps2_controller));
    TRY(mouse_device->initialize());
    return mouse_device;
}

void VMWareMouseDevice::irq_handle_byte_read(u8)
{
    auto backdoor = VMWareBackdoor::the();
    VERIFY(backdoor);
    VERIFY(backdoor->vmmouse_is_absolute());

    // We will receive 4 bytes from the I8042 controller that we are going to
    // ignore. Instead, we will check with VMWareBackdoor to see how many bytes
    // of mouse event data are waiting for us. For each multiple of 4, we
    // produce a mouse packet.
    constexpr u8 max_iterations = 128;
    u8 current_iteration = 0;
    while (++current_iteration < max_iterations) {
        auto number_of_mouse_event_bytes = backdoor->read_mouse_status_queue_size();
        if (number_of_mouse_event_bytes == 0)
            break;
        VERIFY(number_of_mouse_event_bytes % 4 == 0);

        auto mouse_packet = backdoor->receive_mouse_packet();
        m_entropy_source.add_random_event(mouse_packet);
        {
            SpinlockLocker lock(m_queue_lock);
            m_queue.enqueue(mouse_packet);
        }
    }
    evaluate_block_conditions();
}

VMWareMouseDevice::VMWareMouseDevice(const I8042Controller& ps2_controller)
    : PS2MouseDevice(ps2_controller)
{
}
VMWareMouseDevice::~VMWareMouseDevice() = default;

}