PS2MouseDevice.cpp 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include <AK/Memory.h>
  27. #include <AK/Singleton.h>
  28. #include <Kernel/Debug.h>
  29. #include <Kernel/Devices/PS2MouseDevice.h>
  30. #include <Kernel/Devices/VMWareBackdoor.h>
  31. #include <Kernel/IO.h>
  32. namespace Kernel {
  33. #define IRQ_MOUSE 12
  34. #define PS2MOUSE_SET_RESOLUTION 0xE8
  35. #define PS2MOUSE_STATUS_REQUEST 0xE9
  36. #define PS2MOUSE_REQUEST_SINGLE_PACKET 0xEB
  37. #define PS2MOUSE_GET_DEVICE_ID 0xF2
  38. #define PS2MOUSE_SET_SAMPLE_RATE 0xF3
  39. #define PS2MOUSE_ENABLE_PACKET_STREAMING 0xF4
  40. #define PS2MOUSE_DISABLE_PACKET_STREAMING 0xF5
  41. #define PS2MOUSE_SET_DEFAULTS 0xF6
  42. #define PS2MOUSE_RESEND 0xFE
  43. #define PS2MOUSE_RESET 0xFF
  44. #define PS2MOUSE_INTELLIMOUSE_ID 0x03
  45. #define PS2MOUSE_INTELLIMOUSE_EXPLORER_ID 0x04
  46. static AK::Singleton<PS2MouseDevice> s_the;
  47. PS2MouseDevice::PS2MouseDevice()
  48. : IRQHandler(IRQ_MOUSE)
  49. , CharacterDevice(10, 1)
  50. , m_controller(I8042Controller::the())
  51. {
  52. }
  53. PS2MouseDevice::~PS2MouseDevice()
  54. {
  55. }
  56. PS2MouseDevice& PS2MouseDevice::the()
  57. {
  58. return *s_the;
  59. }
  60. void PS2MouseDevice::handle_irq(const RegisterState&)
  61. {
  62. // The controller will read the data and call irq_handle_byte_read
  63. // for the appropriate device
  64. m_controller.irq_process_input_buffer(I8042Controller::Device::Mouse);
  65. }
  66. void PS2MouseDevice::irq_handle_byte_read(u8 byte)
  67. {
  68. auto* backdoor = VMWareBackdoor::the();
  69. if (backdoor && backdoor->vmmouse_is_absolute()) {
  70. // We won't receive complete packets with the backdoor enabled,
  71. // we will only get one byte for each event, which we'll just
  72. // discard. If we were to wait until we *think* that we got a
  73. // full PS/2 packet then we would create a backlog in the VM
  74. // because we wouldn't read the appropriate number of mouse
  75. // packets from VMWareBackdoor.
  76. auto mouse_packet = backdoor->receive_mouse_packet();
  77. if (mouse_packet.has_value()) {
  78. m_entropy_source.add_random_event(mouse_packet.value());
  79. {
  80. ScopedSpinLock lock(m_queue_lock);
  81. m_queue.enqueue(mouse_packet.value());
  82. }
  83. evaluate_block_conditions();
  84. }
  85. return;
  86. }
  87. auto commit_packet = [&] {
  88. m_data_state = 0;
  89. dbgln<PS2MOUSE_DEBUG>("PS2Mouse: {}, {} {} {}",
  90. m_data.bytes[1],
  91. m_data.bytes[2],
  92. (m_data.bytes[0] & 1) ? "Left" : "",
  93. (m_data.bytes[0] & 2) ? "Right" : "");
  94. m_entropy_source.add_random_event(m_data.dword);
  95. {
  96. ScopedSpinLock lock(m_queue_lock);
  97. m_queue.enqueue(parse_data_packet(m_data));
  98. }
  99. evaluate_block_conditions();
  100. };
  101. ASSERT(m_data_state < sizeof(m_data.bytes) / sizeof(m_data.bytes[0]));
  102. m_data.bytes[m_data_state] = byte;
  103. switch (m_data_state) {
  104. case 0:
  105. if (!(byte & 0x08)) {
  106. dbgln("PS2Mouse: Stream out of sync.");
  107. break;
  108. }
  109. ++m_data_state;
  110. break;
  111. case 1:
  112. ++m_data_state;
  113. break;
  114. case 2:
  115. if (m_has_wheel) {
  116. ++m_data_state;
  117. break;
  118. }
  119. commit_packet();
  120. break;
  121. case 3:
  122. ASSERT(m_has_wheel);
  123. commit_packet();
  124. break;
  125. }
  126. }
  127. MousePacket PS2MouseDevice::parse_data_packet(const RawPacket& raw_packet)
  128. {
  129. int x = raw_packet.bytes[1];
  130. int y = raw_packet.bytes[2];
  131. int z = 0;
  132. if (m_has_wheel) {
  133. // FIXME: For non-Intellimouse, this is a full byte.
  134. // However, for now, m_has_wheel is only set for Intellimouse.
  135. z = (char)(raw_packet.bytes[3] & 0x0f);
  136. // -1 in 4 bits
  137. if (z == 15)
  138. z = -1;
  139. }
  140. bool x_overflow = raw_packet.bytes[0] & 0x40;
  141. bool y_overflow = raw_packet.bytes[0] & 0x80;
  142. bool x_sign = raw_packet.bytes[0] & 0x10;
  143. bool y_sign = raw_packet.bytes[0] & 0x20;
  144. if (x && x_sign)
  145. x -= 0x100;
  146. if (y && y_sign)
  147. y -= 0x100;
  148. if (x_overflow || y_overflow) {
  149. x = 0;
  150. y = 0;
  151. }
  152. MousePacket packet;
  153. packet.x = x;
  154. packet.y = y;
  155. packet.z = z;
  156. packet.buttons = raw_packet.bytes[0] & 0x07;
  157. if (m_has_five_buttons) {
  158. if (raw_packet.bytes[3] & 0x10)
  159. packet.buttons |= MousePacket::BackButton;
  160. if (raw_packet.bytes[3] & 0x20)
  161. packet.buttons |= MousePacket::ForwardButton;
  162. }
  163. packet.is_relative = true;
  164. #if PS2MOUSE_DEBUG
  165. dbgln("PS2 Relative Mouse: Buttons {:x}", packet.buttons);
  166. dbgln("Mouse: X {}, Y {}, Z {}", packet.x, packet.y, packet.z);
  167. #endif
  168. return packet;
  169. }
  170. u8 PS2MouseDevice::get_device_id()
  171. {
  172. if (send_command(PS2MOUSE_GET_DEVICE_ID) != I8042_ACK)
  173. return 0;
  174. return read_from_device();
  175. }
  176. u8 PS2MouseDevice::read_from_device()
  177. {
  178. return m_controller.read_from_device(I8042Controller::Device::Mouse);
  179. }
  180. u8 PS2MouseDevice::send_command(u8 command)
  181. {
  182. u8 response = m_controller.send_command(I8042Controller::Device::Mouse, command);
  183. if (response != I8042_ACK)
  184. dbgln("PS2MouseDevice: Command {} got {} but expected ack: {}", command, response, I8042_ACK);
  185. return response;
  186. }
  187. u8 PS2MouseDevice::send_command(u8 command, u8 data)
  188. {
  189. u8 response = m_controller.send_command(I8042Controller::Device::Mouse, command, data);
  190. if (response != I8042_ACK)
  191. dbgln("PS2MouseDevice: Command {} got {} but expected ack: {}", command, response, I8042_ACK);
  192. return response;
  193. }
  194. void PS2MouseDevice::set_sample_rate(u8 rate)
  195. {
  196. send_command(PS2MOUSE_SET_SAMPLE_RATE, rate);
  197. }
  198. bool PS2MouseDevice::initialize()
  199. {
  200. if (!m_controller.reset_device(I8042Controller::Device::Mouse)) {
  201. dbgln("PS2MouseDevice: I8042 controller failed to reset device");
  202. return false;
  203. }
  204. u8 device_id = read_from_device();
  205. // Set default settings.
  206. if (send_command(PS2MOUSE_SET_DEFAULTS) != I8042_ACK)
  207. return false;
  208. if (send_command(PS2MOUSE_ENABLE_PACKET_STREAMING) != I8042_ACK)
  209. return false;
  210. if (device_id != PS2MOUSE_INTELLIMOUSE_ID) {
  211. // Send magical wheel initiation sequence.
  212. set_sample_rate(200);
  213. set_sample_rate(100);
  214. set_sample_rate(80);
  215. device_id = get_device_id();
  216. }
  217. if (device_id == PS2MOUSE_INTELLIMOUSE_ID) {
  218. m_has_wheel = true;
  219. klog() << "PS2MouseDevice: Mouse wheel enabled!";
  220. } else {
  221. klog() << "PS2MouseDevice: No mouse wheel detected!";
  222. }
  223. if (device_id == PS2MOUSE_INTELLIMOUSE_ID) {
  224. // Try to enable 5 buttons as well!
  225. set_sample_rate(200);
  226. set_sample_rate(200);
  227. set_sample_rate(80);
  228. device_id = get_device_id();
  229. }
  230. if (device_id == PS2MOUSE_INTELLIMOUSE_EXPLORER_ID) {
  231. m_has_five_buttons = true;
  232. klog() << "PS2MouseDevice: 5 buttons enabled!";
  233. }
  234. return true;
  235. }
  236. bool PS2MouseDevice::can_read(const FileDescription&, size_t) const
  237. {
  238. ScopedSpinLock lock(m_queue_lock);
  239. return !m_queue.is_empty();
  240. }
  241. KResultOr<size_t> PS2MouseDevice::read(FileDescription&, size_t, UserOrKernelBuffer& buffer, size_t size)
  242. {
  243. ASSERT(size > 0);
  244. size_t nread = 0;
  245. size_t remaining_space_in_buffer = static_cast<size_t>(size) - nread;
  246. ScopedSpinLock lock(m_queue_lock);
  247. while (!m_queue.is_empty() && remaining_space_in_buffer) {
  248. auto packet = m_queue.dequeue();
  249. lock.unlock();
  250. if constexpr (PS2MOUSE_DEBUG) {
  251. dbgln("PS2 Mouse Read: Buttons {:x}", packet.buttons);
  252. dbgln("PS2 Mouse: X {}, Y {}, Z {}, Relative {}", packet.x, packet.y, packet.z, packet.buttons);
  253. dbgln("PS2 Mouse Read: Filter packets");
  254. }
  255. size_t bytes_read_from_packet = min(remaining_space_in_buffer, sizeof(MousePacket));
  256. if (!buffer.write(&packet, nread, bytes_read_from_packet))
  257. return EFAULT;
  258. nread += bytes_read_from_packet;
  259. remaining_space_in_buffer -= bytes_read_from_packet;
  260. lock.lock();
  261. }
  262. return nread;
  263. }
  264. KResultOr<size_t> PS2MouseDevice::write(FileDescription&, size_t, const UserOrKernelBuffer&, size_t)
  265. {
  266. return 0;
  267. }
  268. }