PS2MouseDevice.cpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <AK/Memory.h>
  7. #include <Kernel/Debug.h>
  8. #include <Kernel/Devices/HID/PS2MouseDevice.h>
  9. #include <Kernel/Devices/VMWareBackdoor.h>
  10. #include <Kernel/IO.h>
  11. #include <Kernel/Sections.h>
  12. namespace Kernel {
  13. #define IRQ_MOUSE 12
  14. #define PS2MOUSE_SET_RESOLUTION 0xE8
  15. #define PS2MOUSE_STATUS_REQUEST 0xE9
  16. #define PS2MOUSE_REQUEST_SINGLE_PACKET 0xEB
  17. #define PS2MOUSE_GET_DEVICE_ID 0xF2
  18. #define PS2MOUSE_SET_SAMPLE_RATE 0xF3
  19. #define PS2MOUSE_ENABLE_PACKET_STREAMING 0xF4
  20. #define PS2MOUSE_DISABLE_PACKET_STREAMING 0xF5
  21. #define PS2MOUSE_SET_DEFAULTS 0xF6
  22. #define PS2MOUSE_RESEND 0xFE
  23. #define PS2MOUSE_RESET 0xFF
  24. #define PS2MOUSE_INTELLIMOUSE_ID 0x03
  25. #define PS2MOUSE_INTELLIMOUSE_EXPLORER_ID 0x04
  26. UNMAP_AFTER_INIT PS2MouseDevice::PS2MouseDevice(const I8042Controller& ps2_controller)
  27. : IRQHandler(IRQ_MOUSE)
  28. , MouseDevice()
  29. , I8042Device(ps2_controller)
  30. {
  31. }
  32. UNMAP_AFTER_INIT PS2MouseDevice::~PS2MouseDevice()
  33. {
  34. }
  35. bool PS2MouseDevice::handle_irq(const RegisterState&)
  36. {
  37. // The controller will read the data and call irq_handle_byte_read
  38. // for the appropriate device
  39. return m_i8042_controller->irq_process_input_buffer(instrument_type());
  40. }
  41. void PS2MouseDevice::irq_handle_byte_read(u8 byte)
  42. {
  43. auto commit_packet = [&] {
  44. m_data_state = 0;
  45. dbgln_if(PS2MOUSE_DEBUG, "PS2Mouse: {}, {} {} {}",
  46. m_data.bytes[1],
  47. m_data.bytes[2],
  48. (m_data.bytes[0] & 1) ? "Left" : "",
  49. (m_data.bytes[0] & 2) ? "Right" : "");
  50. m_entropy_source.add_random_event(m_data.dword);
  51. {
  52. SpinlockLocker lock(m_queue_lock);
  53. m_queue.enqueue(parse_data_packet(m_data));
  54. }
  55. evaluate_block_conditions();
  56. };
  57. VERIFY(m_data_state < sizeof(m_data.bytes) / sizeof(m_data.bytes[0]));
  58. m_data.bytes[m_data_state] = byte;
  59. switch (m_data_state) {
  60. case 0:
  61. if (!(byte & 0x08)) {
  62. dbgln("PS2Mouse: Stream out of sync.");
  63. break;
  64. }
  65. ++m_data_state;
  66. break;
  67. case 1:
  68. ++m_data_state;
  69. break;
  70. case 2:
  71. if (m_has_wheel) {
  72. ++m_data_state;
  73. break;
  74. }
  75. commit_packet();
  76. break;
  77. case 3:
  78. VERIFY(m_has_wheel);
  79. commit_packet();
  80. break;
  81. }
  82. }
  83. MousePacket PS2MouseDevice::parse_data_packet(const RawPacket& raw_packet)
  84. {
  85. int x = raw_packet.bytes[1];
  86. int y = raw_packet.bytes[2];
  87. int z = 0;
  88. if (m_has_wheel) {
  89. // FIXME: For non-Intellimouse, this is a full byte.
  90. // However, for now, m_has_wheel is only set for Intellimouse.
  91. z = (char)(raw_packet.bytes[3] & 0x0f);
  92. // -1 in 4 bits
  93. if (z == 15)
  94. z = -1;
  95. }
  96. bool x_overflow = raw_packet.bytes[0] & 0x40;
  97. bool y_overflow = raw_packet.bytes[0] & 0x80;
  98. bool x_sign = raw_packet.bytes[0] & 0x10;
  99. bool y_sign = raw_packet.bytes[0] & 0x20;
  100. if (x && x_sign)
  101. x -= 0x100;
  102. if (y && y_sign)
  103. y -= 0x100;
  104. if (x_overflow || y_overflow) {
  105. x = 0;
  106. y = 0;
  107. }
  108. MousePacket packet;
  109. packet.x = x;
  110. packet.y = y;
  111. packet.z = z;
  112. packet.buttons = raw_packet.bytes[0] & 0x07;
  113. if (m_has_five_buttons) {
  114. if (raw_packet.bytes[3] & 0x10)
  115. packet.buttons |= MousePacket::BackButton;
  116. if (raw_packet.bytes[3] & 0x20)
  117. packet.buttons |= MousePacket::ForwardButton;
  118. }
  119. packet.is_relative = true;
  120. dbgln_if(PS2MOUSE_DEBUG, "PS2 Relative Mouse: Buttons {:x}", packet.buttons);
  121. dbgln_if(PS2MOUSE_DEBUG, "Mouse: X {}, Y {}, Z {}", packet.x, packet.y, packet.z);
  122. return packet;
  123. }
  124. u8 PS2MouseDevice::get_device_id()
  125. {
  126. if (send_command(PS2MOUSE_GET_DEVICE_ID) != I8042_ACK)
  127. return 0;
  128. return read_from_device();
  129. }
  130. u8 PS2MouseDevice::read_from_device()
  131. {
  132. return m_i8042_controller->read_from_device(instrument_type());
  133. }
  134. u8 PS2MouseDevice::send_command(u8 command)
  135. {
  136. u8 response = m_i8042_controller->send_command(instrument_type(), command);
  137. if (response != I8042_ACK)
  138. dbgln("PS2MouseDevice: Command {} got {} but expected ack: {}", command, response, I8042_ACK);
  139. return response;
  140. }
  141. u8 PS2MouseDevice::send_command(u8 command, u8 data)
  142. {
  143. u8 response = m_i8042_controller->send_command(instrument_type(), command, data);
  144. if (response != I8042_ACK)
  145. dbgln("PS2MouseDevice: Command {} got {} but expected ack: {}", command, response, I8042_ACK);
  146. return response;
  147. }
  148. void PS2MouseDevice::set_sample_rate(u8 rate)
  149. {
  150. send_command(PS2MOUSE_SET_SAMPLE_RATE, rate);
  151. }
  152. UNMAP_AFTER_INIT RefPtr<PS2MouseDevice> PS2MouseDevice::try_to_initialize(const I8042Controller& ps2_controller)
  153. {
  154. auto device = adopt_ref(*new PS2MouseDevice(ps2_controller));
  155. if (device->initialize())
  156. return device;
  157. return nullptr;
  158. }
  159. UNMAP_AFTER_INIT bool PS2MouseDevice::initialize()
  160. {
  161. if (!m_i8042_controller->reset_device(instrument_type())) {
  162. dbgln("PS2MouseDevice: I8042 controller failed to reset device");
  163. return false;
  164. }
  165. u8 device_id = read_from_device();
  166. // Set default settings.
  167. if (send_command(PS2MOUSE_SET_DEFAULTS) != I8042_ACK)
  168. return false;
  169. if (send_command(PS2MOUSE_ENABLE_PACKET_STREAMING) != I8042_ACK)
  170. return false;
  171. if (device_id != PS2MOUSE_INTELLIMOUSE_ID) {
  172. // Send magical wheel initiation sequence.
  173. set_sample_rate(200);
  174. set_sample_rate(100);
  175. set_sample_rate(80);
  176. device_id = get_device_id();
  177. }
  178. if (device_id == PS2MOUSE_INTELLIMOUSE_ID) {
  179. m_has_wheel = true;
  180. dmesgln("PS2MouseDevice: Mouse wheel enabled!");
  181. } else {
  182. dmesgln("PS2MouseDevice: No mouse wheel detected!");
  183. }
  184. if (device_id == PS2MOUSE_INTELLIMOUSE_ID) {
  185. // Try to enable 5 buttons as well!
  186. set_sample_rate(200);
  187. set_sample_rate(200);
  188. set_sample_rate(80);
  189. device_id = get_device_id();
  190. }
  191. if (device_id == PS2MOUSE_INTELLIMOUSE_EXPLORER_ID) {
  192. m_has_five_buttons = true;
  193. dmesgln("PS2MouseDevice: 5 buttons enabled!");
  194. }
  195. return true;
  196. }
  197. }