PS2KeyboardDevice.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2021, Liav A. <liavalb@hotmail.co.il>
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #include <AK/ByteBuffer.h>
  8. #include <AK/Singleton.h>
  9. #include <AK/Types.h>
  10. #include <Kernel/Debug.h>
  11. #include <Kernel/Devices/HID/HIDManagement.h>
  12. #include <Kernel/Devices/HID/PS2KeyboardDevice.h>
  13. #include <Kernel/IO.h>
  14. #include <Kernel/Sections.h>
  15. #include <Kernel/TTY/ConsoleManagement.h>
  16. #include <Kernel/WorkQueue.h>
  17. namespace Kernel {
  18. #define IRQ_KEYBOARD 1
  19. void PS2KeyboardDevice::irq_handle_byte_read(u8 byte)
  20. {
  21. u8 ch = byte & 0x7f;
  22. bool pressed = !(byte & 0x80);
  23. m_entropy_source.add_random_event(byte);
  24. if (byte == 0xe0) {
  25. m_has_e0_prefix = true;
  26. return;
  27. }
  28. if ((m_modifiers == (Mod_Alt | Mod_Shift) || m_modifiers == (Mod_Ctrl | Mod_Alt | Mod_Shift)) && byte == 0x58) {
  29. // Alt+Shift+F12 pressed, dump some kernel state to the debug console.
  30. ConsoleManagement::the().switch_to_debug();
  31. Scheduler::dump_scheduler_state(m_modifiers == (Mod_Ctrl | Mod_Alt | Mod_Shift));
  32. }
  33. dbgln_if(KEYBOARD_DEBUG, "Keyboard::irq_handle_byte_read: {:#02x} {}", ch, (pressed ? "down" : "up"));
  34. switch (ch) {
  35. case 0x38:
  36. if (m_has_e0_prefix)
  37. update_modifier(Mod_AltGr, pressed);
  38. else
  39. update_modifier(Mod_Alt, pressed);
  40. break;
  41. case 0x1d:
  42. update_modifier(Mod_Ctrl, pressed);
  43. break;
  44. case 0x5b:
  45. update_modifier(Mod_Super, pressed);
  46. break;
  47. case 0x2a:
  48. m_left_shift_pressed = pressed;
  49. update_modifier(Mod_Shift, m_left_shift_pressed || m_right_shift_pressed);
  50. break;
  51. case 0x36:
  52. m_right_shift_pressed = pressed;
  53. update_modifier(Mod_Shift, m_left_shift_pressed || m_right_shift_pressed);
  54. break;
  55. }
  56. switch (ch) {
  57. case I8042_ACK:
  58. break;
  59. default:
  60. if ((m_modifiers & Mod_Alt) != 0 && ch >= 2 && ch <= ConsoleManagement::s_max_virtual_consoles + 1) {
  61. g_io_work->queue([ch]() {
  62. ConsoleManagement::the().switch_to(ch - 0x02);
  63. });
  64. }
  65. key_state_changed(ch, pressed);
  66. }
  67. }
  68. bool PS2KeyboardDevice::handle_irq(const RegisterState&)
  69. {
  70. // The controller will read the data and call irq_handle_byte_read
  71. // for the appropriate device
  72. return m_i8042_controller->irq_process_input_buffer(HIDDevice::Type::Keyboard);
  73. }
  74. UNMAP_AFTER_INIT RefPtr<PS2KeyboardDevice> PS2KeyboardDevice::try_to_initialize(const I8042Controller& ps2_controller)
  75. {
  76. auto device = adopt_ref(*new PS2KeyboardDevice(ps2_controller));
  77. if (device->initialize())
  78. return device;
  79. return nullptr;
  80. }
  81. UNMAP_AFTER_INIT bool PS2KeyboardDevice::initialize()
  82. {
  83. if (!m_i8042_controller->reset_device(HIDDevice::Type::Keyboard)) {
  84. dbgln("KeyboardDevice: I8042 controller failed to reset device");
  85. return false;
  86. }
  87. return true;
  88. }
  89. // FIXME: UNMAP_AFTER_INIT might not be correct, because in practice PS/2 devices
  90. // are hot pluggable.
  91. UNMAP_AFTER_INIT PS2KeyboardDevice::PS2KeyboardDevice(const I8042Controller& ps2_controller)
  92. : IRQHandler(IRQ_KEYBOARD)
  93. , KeyboardDevice()
  94. , I8042Device(ps2_controller)
  95. {
  96. }
  97. // FIXME: UNMAP_AFTER_INIT might not be correct, because in practice PS/2 devices
  98. // are hot pluggable.
  99. UNMAP_AFTER_INIT PS2KeyboardDevice::~PS2KeyboardDevice()
  100. {
  101. }
  102. }