summaryrefslogtreecommitdiff
path: root/Kernel/Arch/x86_64/ISABus/I8042Controller.cpp
blob: 57f7a867987873f58ea46c3a398bfa6c24da424e (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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
/*
 * Copyright (c) 2020, the SerenityOS developers.
 *
 * SPDX-License-Identifier: BSD-2-Clause
 */

#include <Kernel/Arch/Delay.h>
#include <Kernel/Arch/x86_64/IO.h>
#include <Kernel/Arch/x86_64/ISABus/HID/PS2KeyboardDevice.h>
#include <Kernel/Arch/x86_64/ISABus/HID/PS2MouseDevice.h>
#include <Kernel/Arch/x86_64/ISABus/HID/VMWareMouseDevice.h>
#include <Kernel/Arch/x86_64/ISABus/I8042Controller.h>
#include <Kernel/Sections.h>

namespace Kernel {

UNMAP_AFTER_INIT NonnullLockRefPtr<I8042Controller> I8042Controller::initialize()
{
    return adopt_lock_ref(*new I8042Controller());
}

UNMAP_AFTER_INIT I8042Controller::I8042Controller()
{
}

UNMAP_AFTER_INIT bool I8042Controller::check_existence_via_probing(Badge<HIDManagement>)
{
    {
        u8 configuration = 0;
        SpinlockLocker lock(m_lock);

        // This drains the output buffer and serves as an existence test.
        if (auto result = drain_output_buffer(); result.is_error()) {
            dbgln("I8042: Trying to flush output buffer as an existence test failed, error {}", result.error());
            return false;
        }

        // Note: Perform controller self-test before touching the controller
        // Try to probe the controller for 10 times and give up if nothing
        // responded.
        // Some controllers will reset and behave abnormally on this, so let's ensure
        // we keep the configuration before initiating this command.

        if (auto result = do_wait_then_write(I8042Port::Command, I8042Command::ReadConfiguration); result.is_error()) {
            dbgln("I8042: Trying to read configuration failed during the existence test, error {}", result.error());
            return false;
        }

        {
            auto result = do_wait_then_read(I8042Port::Buffer);
            if (result.is_error()) {
                dbgln("I8042: Trying to read configuration failed during the existence test, error {}", result.error());
                return false;
            }
            configuration = result.release_value();
        }

        bool successful_self_test = false;
        for (int attempt = 0; attempt < 20; attempt++) {
            do_write(I8042Port::Command, I8042Command::TestPS2Controller);
            if (do_read(I8042Port::Buffer) == I8042Response::ControllerTestPassed) {
                successful_self_test = true;
                break;
            }
            // Note: Wait 500 microseconds in case the controller couldn't respond
            microseconds_delay(500);
        }
        if (!successful_self_test) {
            dbgln("I8042: Trying to probe for existence of controller failed");
            return false;
        }

        if (auto result = do_wait_then_write(I8042Port::Command, I8042Command::WriteConfiguration); result.is_error()) {
            dbgln("I8042: Trying to restore configuration after self-test failed with error {}", result.error());
            return false;
        }

        if (auto result = do_wait_then_write(I8042Port::Buffer, configuration); result.is_error()) {
            dbgln("I8042: Trying to write restored configuration after self-test failed with error {}", result.error());
            return false;
        }

        return true;
    }
}

UNMAP_AFTER_INIT ErrorOr<void> I8042Controller::detect_devices()
{

    u8 configuration;
    {
        SpinlockLocker lock(m_lock);
        // Note: This flushes all the garbage left in the controller registers
        TRY(drain_output_buffer());

        TRY(do_wait_then_write(I8042Port::Command, I8042Command::DisableFirstPS2Port));
        TRY(do_wait_then_write(I8042Port::Command, I8042Command::DisableSecondPS2Port)); // ignored if it doesn't exist

        TRY(do_wait_then_write(I8042Port::Command, I8042Command::ReadConfiguration));
        configuration = TRY(do_wait_then_read(I8042Port::Buffer));
        configuration &= ~I8042ConfigurationFlag::FirstPS2PortInterrupt;
        configuration &= ~I8042ConfigurationFlag::SecondPS2PortInterrupt;
        configuration |= I8042ConfigurationFlag::FirstPS2PortTranslation;

        TRY(do_wait_then_write(I8042Port::Command, I8042Command::WriteConfiguration));
        TRY(do_wait_then_write(I8042Port::Buffer, configuration));

        // Perform controller self-test
        TRY(do_wait_then_write(I8042Port::Command, I8042Command::TestPS2Controller));
        auto self_test_result = TRY(do_wait_then_read(I8042Port::Buffer));
        if (self_test_result == I8042Response::ControllerTestPassed) {
            // Restore configuration in case the controller reset
            TRY(do_wait_then_write(I8042Port::Command, I8042Command::WriteConfiguration));
            TRY(do_wait_then_write(I8042Port::Buffer, configuration));
        } else {
            dbgln("I8042: Controller self test failed");
            return Error::from_errno(EIO);
        }

        m_is_dual_channel = (configuration & I8042ConfigurationFlag::SecondPS2PortClock) != 0;
        dbgln("I8042: {} channel controller", m_is_dual_channel ? "Dual" : "Single");

        // Test ports and enable them if available
        TRY(do_wait_then_write(I8042Port::Command, I8042Command::TestFirstPS2Port));
        auto first_port_test_result = TRY(do_wait_then_read(I8042Port::Buffer));
        m_first_port_available = (first_port_test_result == 0);

        if (m_first_port_available) {
            TRY(do_wait_then_write(I8042Port::Command, I8042Command::EnableFirstPS2Port));
            configuration |= I8042ConfigurationFlag::FirstPS2PortInterrupt;
            configuration &= ~I8042ConfigurationFlag::FirstPS2PortClock;
        } else {
            dbgln("I8042: Keyboard port not available");
        }

        TRY(drain_output_buffer());

        if (m_is_dual_channel) {
            TRY(do_wait_then_write(I8042Port::Command, I8042Command::TestSecondPS2Port));
            auto test_second_port_result = TRY(do_wait_then_read(I8042Port::Buffer));
            m_second_port_available = (test_second_port_result == 0);
            if (m_second_port_available) {
                TRY(do_wait_then_write(I8042Port::Command, I8042Command::EnableSecondPS2Port));
                configuration |= I8042ConfigurationFlag::SecondPS2PortInterrupt;
                configuration &= ~I8042ConfigurationFlag::SecondPS2PortClock;
            } else {
                dbgln("I8042: Mouse port not available");
            }
        }

        // Enable IRQs for the ports that are usable
        if (m_first_port_available || m_second_port_available) {
            configuration &= ~I8042ConfigurationFlag::FirstPS2PortClock;
            configuration &= ~I8042ConfigurationFlag::SecondPS2PortClock;
            TRY(do_wait_then_write(I8042Port::Command, I8042Command::WriteConfiguration));
            TRY(do_wait_then_write(I8042Port::Buffer, configuration));
        }
    }

    // Try to detect and initialize the devices
    if (m_first_port_available) {
        // FIXME: Actually figure out the connected PS2 device type
        m_first_ps2_port.device_type = PS2Device::Type::Keyboard;
        auto keyboard_device = TRY(KeyboardDevice::try_to_initialize());
        auto error_or_device = PS2KeyboardDevice::try_to_initialize(*this, *keyboard_device);
        if (error_or_device.is_error()) {
            dbgln("I8042: Keyboard device failed to initialize, disable");
            m_first_port_available = false;
            configuration &= ~I8042ConfigurationFlag::FirstPS2PortInterrupt;
            configuration |= I8042ConfigurationFlag::FirstPS2PortClock;
            m_first_ps2_port.device = nullptr;
            m_first_ps2_port.device_type = {};
            SpinlockLocker lock(m_lock);
            // NOTE: Before setting the actual scan code set, stop packet streaming entirely.
            TRY(send_command(PS2Device::Type::Keyboard, I8042Command::DisablePacketStreaming));
            TRY(do_wait_then_write(I8042Port::Buffer, I8042Command::SetScanCodeSet));
            TRY(do_wait_then_write(I8042Port::Buffer, 0x2));

            TRY(do_wait_then_write(I8042Port::Command, I8042Command::WriteConfiguration));
            TRY(do_wait_then_write(I8042Port::Buffer, configuration));
        } else {
            m_first_ps2_port.device = error_or_device.release_value();
        }
    }
    if (m_second_port_available) {
        // FIXME: Actually figure out the connected PS2 device type
        m_second_ps2_port.device_type = PS2Device::Type::Mouse;
        auto mouse_device = TRY(MouseDevice::try_to_initialize());
        auto vmmouse_device_or_error = VMWareMouseDevice::try_to_initialize(*this, *mouse_device);
        if (vmmouse_device_or_error.is_error()) {
            // FIXME: is there something to do with the VMWare errors?
            auto mouse_device_or_error = PS2MouseDevice::try_to_initialize(*this, *mouse_device);
            if (mouse_device_or_error.is_error()) {
                dbgln("I8042: Mouse device failed to initialize, disable");
                m_second_port_available = false;
                configuration |= I8042ConfigurationFlag::SecondPS2PortClock;
                m_second_ps2_port.device = nullptr;
                m_second_ps2_port.device_type = {};
                SpinlockLocker lock(m_lock);
                TRY(do_wait_then_write(I8042Port::Command, I8042Command::WriteConfiguration));
                TRY(do_wait_then_write(I8042Port::Buffer, configuration));
            } else {
                m_second_ps2_port.device = mouse_device_or_error.release_value();
            }
        } else {
            m_second_ps2_port.device = vmmouse_device_or_error.release_value();
        }
    }

    // Enable IRQs after both are detected and initialized
    if (m_first_ps2_port.device)
        m_first_ps2_port.device->enable_interrupts();
    if (m_second_ps2_port.device)
        m_second_ps2_port.device->enable_interrupts();
    return {};
}

bool I8042Controller::irq_process_input_buffer(PS2Device::Type instrument_type)
{
    VERIFY(Processor::current_in_irq());

    u8 status = IO::in8(I8042Port::Status);
    if (!(status & I8042StatusFlag::OutputBuffer))
        return false;
    u8 byte = IO::in8(I8042Port::Buffer);

    PS2Port* selected_port = nullptr;
    if (m_first_ps2_port.device_type.has_value() && m_first_ps2_port.device_type.value() == instrument_type) {
        VERIFY(m_first_ps2_port.device);
        selected_port = &m_first_ps2_port;
    } else if (m_second_ps2_port.device_type.has_value() && m_second_ps2_port.device_type.value() == instrument_type) {
        VERIFY(m_second_ps2_port.device);
        selected_port = &m_second_ps2_port;
    } else {
        return false;
    }

    selected_port->device->irq_handle_byte_read(byte);
    return true;
}

ErrorOr<void> I8042Controller::drain_output_buffer()
{
    for (int attempt = 0; attempt < 50; attempt++) {
        u8 status = IO::in8(I8042Port::Status);
        if (!(status & I8042StatusFlag::OutputBuffer))
            return {};
        IO::in8(I8042Port::Buffer);

        microseconds_delay(100);
    }
    return Error::from_errno(EBUSY);
}

ErrorOr<void> I8042Controller::do_reset_device(PS2Device::Type device)
{
    VERIFY(device != PS2Device::Type::Unknown);
    VERIFY(m_lock.is_locked());

    VERIFY(!Processor::current_in_irq());
    auto reset_result = TRY(do_send_command(device, I8042Command::Reset));
    // FIXME: Is this the correct errno value for this?
    if (reset_result != I8042Response::Acknowledge)
        return Error::from_errno(EIO);
    // Wait until we get the self-test result
    auto self_test_result = TRY(do_wait_then_read(I8042Port::Buffer));

    // FIXME: Is this the correct errno value for this?
    if (self_test_result != I8042Response::Success)
        return Error::from_errno(EIO);
    return {};
}

ErrorOr<u8> I8042Controller::do_send_command(PS2Device::Type device, u8 command)
{
    VERIFY(device != PS2Device::Type::Unknown);
    VERIFY(m_lock.is_locked());

    VERIFY(!Processor::current_in_irq());

    return do_write_to_device(device, command);
}

ErrorOr<u8> I8042Controller::do_send_command(PS2Device::Type device, u8 command, u8 data)
{
    VERIFY(device != PS2Device::Type::Unknown);
    VERIFY(m_lock.is_locked());

    VERIFY(!Processor::current_in_irq());

    u8 response = TRY(do_write_to_device(device, command));
    if (response == I8042Response::Acknowledge)
        response = TRY(do_write_to_device(device, data));
    return response;
}

ErrorOr<u8> I8042Controller::do_write_to_device(PS2Device::Type device, u8 data)
{
    VERIFY(device != PS2Device::Type::Unknown);
    VERIFY(m_lock.is_locked());

    VERIFY(!Processor::current_in_irq());

    int attempts = 0;
    u8 response;
    do {
        if (device != PS2Device::Type::Keyboard) {
            TRY(prepare_for_output());
            IO::out8(I8042Port::Command, I8042Command::WriteSecondPS2PortInputBuffer);
        }
        TRY(prepare_for_output());
        IO::out8(I8042Port::Buffer, data);

        response = TRY(do_wait_then_read(I8042Port::Buffer));
    } while (response == I8042Response::Resend && ++attempts < 250);
    if (attempts >= 250)
        dbgln("Failed to write byte to device, gave up");
    return response;
}

ErrorOr<u8> I8042Controller::do_read_from_device(PS2Device::Type device)
{
    VERIFY(device != PS2Device::Type::Unknown);

    TRY(prepare_for_input(device));
    return IO::in8(I8042Port::Buffer);
}

ErrorOr<void> I8042Controller::prepare_for_input(PS2Device::Type device)
{
    VERIFY(m_lock.is_locked());
    u8 const second_port_flag = device == PS2Device::Type::Keyboard ? 0 : I8042StatusFlag::SecondPS2PortOutputBuffer;
    for (int attempt = 0; attempt < 1000; attempt++) {
        u8 status = IO::in8(I8042Port::Status);
        if (!(status & I8042StatusFlag::OutputBuffer)) {
            microseconds_delay(1000);
            continue;
        }
        if (device == PS2Device::Type::Unknown)
            return {};
        if ((status & I8042StatusFlag::SecondPS2PortOutputBuffer) == second_port_flag)
            return {};
        microseconds_delay(1000);
    }
    return Error::from_errno(EBUSY);
}

ErrorOr<void> I8042Controller::prepare_for_output()
{
    VERIFY(m_lock.is_locked());
    for (int attempt = 0; attempt < 250; attempt++) {
        u8 status = IO::in8(I8042Port::Status);
        if (!(status & I8042StatusFlag::InputBuffer))
            return {};
        microseconds_delay(1000);
    }
    return Error::from_errno(EBUSY);
}

UNMAP_AFTER_INIT void I8042Controller::do_write(u8 port, u8 data)
{
    VERIFY(m_lock.is_locked());
    IO::out8(port, data);
}

UNMAP_AFTER_INIT u8 I8042Controller::do_read(u8 port)
{
    VERIFY(m_lock.is_locked());
    return IO::in8(port);
}

ErrorOr<void> I8042Controller::do_wait_then_write(u8 port, u8 data)
{
    VERIFY(m_lock.is_locked());
    TRY(prepare_for_output());
    IO::out8(port, data);
    return {};
}

ErrorOr<u8> I8042Controller::do_wait_then_read(u8 port)
{
    VERIFY(m_lock.is_locked());
    TRY(prepare_for_input(PS2Device::Type::Unknown));
    return IO::in8(port);
}

}