PS2MouseDevice.cpp 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  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/Devices/PS2MouseDevice.h>
  29. #include <Kernel/Devices/VMWareBackdoor.h>
  30. #include <Kernel/IO.h>
  31. namespace Kernel {
  32. #define IRQ_MOUSE 12
  33. #define PS2MOUSE_SET_RESOLUTION 0xE8
  34. #define PS2MOUSE_STATUS_REQUEST 0xE9
  35. #define PS2MOUSE_REQUEST_SINGLE_PACKET 0xEB
  36. #define PS2MOUSE_GET_DEVICE_ID 0xF2
  37. #define PS2MOUSE_SET_SAMPLE_RATE 0xF3
  38. #define PS2MOUSE_ENABLE_PACKET_STREAMING 0xF4
  39. #define PS2MOUSE_DISABLE_PACKET_STREAMING 0xF5
  40. #define PS2MOUSE_SET_DEFAULTS 0xF6
  41. #define PS2MOUSE_RESEND 0xFE
  42. #define PS2MOUSE_RESET 0xFF
  43. #define PS2MOUSE_INTELLIMOUSE_ID 0x03
  44. #define PS2MOUSE_INTELLIMOUSE_EXPLORER_ID 0x04
  45. //#define PS2MOUSE_DEBUG
  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. #ifdef PS2MOUSE_DEBUG
  90. dbg() << "PS2Mouse: " << m_data.bytes[1] << ", " << m_data.bytes[2] << " " << ((m_data.bytes[0] & 1) ? "Left" : "") << " " << ((m_data.bytes[0] & 2) ? "Right" : "");
  91. #endif
  92. m_entropy_source.add_random_event(m_data.dword);
  93. {
  94. ScopedSpinLock lock(m_queue_lock);
  95. m_queue.enqueue(parse_data_packet(m_data));
  96. }
  97. evaluate_block_conditions();
  98. };
  99. ASSERT(m_data_state < sizeof(m_data.bytes) / sizeof(m_data.bytes[0]));
  100. m_data.bytes[m_data_state] = byte;
  101. switch (m_data_state) {
  102. case 0:
  103. if (!(byte & 0x08)) {
  104. dbgln("PS2Mouse: Stream out of sync.");
  105. break;
  106. }
  107. ++m_data_state;
  108. break;
  109. case 1:
  110. ++m_data_state;
  111. break;
  112. case 2:
  113. if (m_has_wheel) {
  114. ++m_data_state;
  115. break;
  116. }
  117. commit_packet();
  118. break;
  119. case 3:
  120. ASSERT(m_has_wheel);
  121. commit_packet();
  122. break;
  123. }
  124. }
  125. MousePacket PS2MouseDevice::parse_data_packet(const RawPacket& raw_packet)
  126. {
  127. int x = raw_packet.bytes[1];
  128. int y = raw_packet.bytes[2];
  129. int z = 0;
  130. if (m_has_wheel) {
  131. // FIXME: For non-Intellimouse, this is a full byte.
  132. // However, for now, m_has_wheel is only set for Intellimouse.
  133. z = (char)(raw_packet.bytes[3] & 0x0f);
  134. // -1 in 4 bits
  135. if (z == 15)
  136. z = -1;
  137. }
  138. bool x_overflow = raw_packet.bytes[0] & 0x40;
  139. bool y_overflow = raw_packet.bytes[0] & 0x80;
  140. bool x_sign = raw_packet.bytes[0] & 0x10;
  141. bool y_sign = raw_packet.bytes[0] & 0x20;
  142. if (x && x_sign)
  143. x -= 0x100;
  144. if (y && y_sign)
  145. y -= 0x100;
  146. if (x_overflow || y_overflow) {
  147. x = 0;
  148. y = 0;
  149. }
  150. MousePacket packet;
  151. packet.x = x;
  152. packet.y = y;
  153. packet.z = z;
  154. packet.buttons = raw_packet.bytes[0] & 0x07;
  155. if (m_has_five_buttons) {
  156. if (raw_packet.bytes[3] & 0x10)
  157. packet.buttons |= MousePacket::BackButton;
  158. if (raw_packet.bytes[3] & 0x20)
  159. packet.buttons |= MousePacket::ForwardButton;
  160. }
  161. packet.is_relative = true;
  162. #ifdef PS2MOUSE_DEBUG
  163. dbg() << "PS2 Relative Mouse: Buttons " << String::format("%x", packet.buttons);
  164. dbg() << "Mouse: X " << packet.x << ", Y " << packet.y << ", Z " << packet.z;
  165. #endif
  166. return packet;
  167. }
  168. u8 PS2MouseDevice::get_device_id()
  169. {
  170. if (send_command(PS2MOUSE_GET_DEVICE_ID) != I8042_ACK)
  171. return 0;
  172. return read_from_device();
  173. }
  174. u8 PS2MouseDevice::read_from_device()
  175. {
  176. return m_controller.read_from_device(I8042Controller::Device::Mouse);
  177. }
  178. u8 PS2MouseDevice::send_command(u8 command)
  179. {
  180. u8 response = m_controller.send_command(I8042Controller::Device::Mouse, command);
  181. if (response != I8042_ACK)
  182. dbgln("PS2MouseDevice: Command {} got {} but expected ack: {}", command, response, I8042_ACK);
  183. return response;
  184. }
  185. u8 PS2MouseDevice::send_command(u8 command, u8 data)
  186. {
  187. u8 response = m_controller.send_command(I8042Controller::Device::Mouse, command, data);
  188. if (response != I8042_ACK)
  189. dbgln("PS2MouseDevice: Command {} got {} but expected ack: {}", command, response, I8042_ACK);
  190. return response;
  191. }
  192. void PS2MouseDevice::set_sample_rate(u8 rate)
  193. {
  194. send_command(PS2MOUSE_SET_SAMPLE_RATE, rate);
  195. }
  196. bool PS2MouseDevice::initialize()
  197. {
  198. if (!m_controller.reset_device(I8042Controller::Device::Mouse)) {
  199. dbgln("PS2MouseDevice: I8042 controller failed to reset device");
  200. return false;
  201. }
  202. u8 device_id = read_from_device();
  203. // Set default settings.
  204. if (send_command(PS2MOUSE_SET_DEFAULTS) != I8042_ACK)
  205. return false;
  206. if (send_command(PS2MOUSE_ENABLE_PACKET_STREAMING) != I8042_ACK)
  207. return false;
  208. if (device_id != PS2MOUSE_INTELLIMOUSE_ID) {
  209. // Send magical wheel initiation sequence.
  210. set_sample_rate(200);
  211. set_sample_rate(100);
  212. set_sample_rate(80);
  213. device_id = get_device_id();
  214. }
  215. if (device_id == PS2MOUSE_INTELLIMOUSE_ID) {
  216. m_has_wheel = true;
  217. klog() << "PS2MouseDevice: Mouse wheel enabled!";
  218. } else {
  219. klog() << "PS2MouseDevice: No mouse wheel detected!";
  220. }
  221. if (device_id == PS2MOUSE_INTELLIMOUSE_ID) {
  222. // Try to enable 5 buttons as well!
  223. set_sample_rate(200);
  224. set_sample_rate(200);
  225. set_sample_rate(80);
  226. device_id = get_device_id();
  227. }
  228. if (device_id == PS2MOUSE_INTELLIMOUSE_EXPLORER_ID) {
  229. m_has_five_buttons = true;
  230. klog() << "PS2MouseDevice: 5 buttons enabled!";
  231. }
  232. return true;
  233. }
  234. bool PS2MouseDevice::can_read(const FileDescription&, size_t) const
  235. {
  236. ScopedSpinLock lock(m_queue_lock);
  237. return !m_queue.is_empty();
  238. }
  239. KResultOr<size_t> PS2MouseDevice::read(FileDescription&, size_t, UserOrKernelBuffer& buffer, size_t size)
  240. {
  241. ASSERT(size > 0);
  242. size_t nread = 0;
  243. size_t remaining_space_in_buffer = static_cast<size_t>(size) - nread;
  244. ScopedSpinLock lock(m_queue_lock);
  245. while (!m_queue.is_empty() && remaining_space_in_buffer) {
  246. auto packet = m_queue.dequeue();
  247. lock.unlock();
  248. #ifdef PS2MOUSE_DEBUG
  249. dbg() << "PS2 Mouse Read: Buttons " << String::format("%x", packet.buttons);
  250. dbg() << "PS2 Mouse: X " << packet.x << ", Y " << packet.y << ", Z " << packet.z << " Relative " << packet.buttons;
  251. dbgln("PS2 Mouse Read: Filter packets");
  252. #endif
  253. size_t bytes_read_from_packet = min(remaining_space_in_buffer, sizeof(MousePacket));
  254. if (!buffer.write(&packet, nread, bytes_read_from_packet))
  255. return KResult(-EFAULT);
  256. nread += bytes_read_from_packet;
  257. remaining_space_in_buffer -= bytes_read_from_packet;
  258. lock.lock();
  259. }
  260. return nread;
  261. }
  262. KResultOr<size_t> PS2MouseDevice::write(FileDescription&, size_t, const UserOrKernelBuffer&, size_t)
  263. {
  264. return 0;
  265. }
  266. }