PS2MouseDevice.cpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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 <AK/Singleton.h>
  8. #include <Kernel/Debug.h>
  9. #include <Kernel/Devices/HID/PS2MouseDevice.h>
  10. #include <Kernel/Devices/VMWareBackdoor.h>
  11. #include <Kernel/IO.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. void 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. 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. ScopedSpinLock 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. #if PS2MOUSE_DEBUG
  121. dbgln("PS2 Relative Mouse: Buttons {:x}", packet.buttons);
  122. dbgln("Mouse: X {}, Y {}, Z {}", packet.x, packet.y, packet.z);
  123. #endif
  124. return packet;
  125. }
  126. u8 PS2MouseDevice::get_device_id()
  127. {
  128. if (send_command(PS2MOUSE_GET_DEVICE_ID) != I8042_ACK)
  129. return 0;
  130. return read_from_device();
  131. }
  132. u8 PS2MouseDevice::read_from_device()
  133. {
  134. return m_i8042_controller->read_from_device(instrument_type());
  135. }
  136. u8 PS2MouseDevice::send_command(u8 command)
  137. {
  138. u8 response = m_i8042_controller->send_command(instrument_type(), command);
  139. if (response != I8042_ACK)
  140. dbgln("PS2MouseDevice: Command {} got {} but expected ack: {}", command, response, I8042_ACK);
  141. return response;
  142. }
  143. u8 PS2MouseDevice::send_command(u8 command, u8 data)
  144. {
  145. u8 response = m_i8042_controller->send_command(instrument_type(), command, data);
  146. if (response != I8042_ACK)
  147. dbgln("PS2MouseDevice: Command {} got {} but expected ack: {}", command, response, I8042_ACK);
  148. return response;
  149. }
  150. void PS2MouseDevice::set_sample_rate(u8 rate)
  151. {
  152. send_command(PS2MOUSE_SET_SAMPLE_RATE, rate);
  153. }
  154. UNMAP_AFTER_INIT RefPtr<PS2MouseDevice> PS2MouseDevice::try_to_initialize(const I8042Controller& ps2_controller)
  155. {
  156. auto device = adopt_ref(*new PS2MouseDevice(ps2_controller));
  157. if (device->initialize())
  158. return device;
  159. return nullptr;
  160. }
  161. UNMAP_AFTER_INIT bool PS2MouseDevice::initialize()
  162. {
  163. if (!m_i8042_controller->reset_device(instrument_type())) {
  164. dbgln("PS2MouseDevice: I8042 controller failed to reset device");
  165. return false;
  166. }
  167. u8 device_id = read_from_device();
  168. // Set default settings.
  169. if (send_command(PS2MOUSE_SET_DEFAULTS) != I8042_ACK)
  170. return false;
  171. if (send_command(PS2MOUSE_ENABLE_PACKET_STREAMING) != I8042_ACK)
  172. return false;
  173. if (device_id != PS2MOUSE_INTELLIMOUSE_ID) {
  174. // Send magical wheel initiation sequence.
  175. set_sample_rate(200);
  176. set_sample_rate(100);
  177. set_sample_rate(80);
  178. device_id = get_device_id();
  179. }
  180. if (device_id == PS2MOUSE_INTELLIMOUSE_ID) {
  181. m_has_wheel = true;
  182. dmesgln("PS2MouseDevice: Mouse wheel enabled!");
  183. } else {
  184. dmesgln("PS2MouseDevice: No mouse wheel detected!");
  185. }
  186. if (device_id == PS2MOUSE_INTELLIMOUSE_ID) {
  187. // Try to enable 5 buttons as well!
  188. set_sample_rate(200);
  189. set_sample_rate(200);
  190. set_sample_rate(80);
  191. device_id = get_device_id();
  192. }
  193. if (device_id == PS2MOUSE_INTELLIMOUSE_EXPLORER_ID) {
  194. m_has_five_buttons = true;
  195. dmesgln("PS2MouseDevice: 5 buttons enabled!");
  196. }
  197. return true;
  198. }
  199. }