PS2MouseDevice.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  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 <Kernel/Devices/PS2MouseDevice.h>
  28. #include <Kernel/Devices/VMWareBackdoor.h>
  29. #include <LibBareMetal/IO.h>
  30. namespace Kernel {
  31. #define IRQ_MOUSE 12
  32. #define I8042_BUFFER 0x60
  33. #define I8042_STATUS 0x64
  34. #define I8042_ACK 0xFA
  35. #define I8042_BUFFER_FULL 0x01
  36. #define I8042_WHICH_BUFFER 0x20
  37. #define I8042_MOUSE_BUFFER 0x20
  38. #define I8042_KEYBOARD_BUFFER 0x00
  39. #define PS2MOUSE_SET_RESOLUTION 0xE8
  40. #define PS2MOUSE_STATUS_REQUEST 0xE9
  41. #define PS2MOUSE_REQUEST_SINGLE_PACKET 0xEB
  42. #define PS2MOUSE_GET_DEVICE_ID 0xF2
  43. #define PS2MOUSE_SET_SAMPLE_RATE 0xF3
  44. #define PS2MOUSE_ENABLE_PACKET_STREAMING 0xF4
  45. #define PS2MOUSE_DISABLE_PACKET_STREAMING 0xF5
  46. #define PS2MOUSE_SET_DEFAULTS 0xF6
  47. #define PS2MOUSE_RESEND 0xFE
  48. #define PS2MOUSE_RESET 0xFF
  49. #define PS2MOUSE_LEFT_CLICK 0x01
  50. #define PS2MOUSE_RIGHT_CLICK 0x02
  51. #define PS2MOUSE_MIDDLE_CLICK 0x04
  52. #define VMMOUSE_LEFT_CLICK 0x20
  53. #define VMMOUSE_RIGHT_CLICK 0x10
  54. #define VMMOUSE_MIDDLE_CLICK 0x08
  55. #define PS2MOUSE_INTELLIMOUSE_ID 0x03
  56. //#define PS2MOUSE_DEBUG
  57. static PS2MouseDevice* s_the;
  58. PS2MouseDevice::PS2MouseDevice()
  59. : IRQHandler(IRQ_MOUSE)
  60. , CharacterDevice(10, 1)
  61. {
  62. s_the = this;
  63. initialize();
  64. }
  65. PS2MouseDevice::~PS2MouseDevice()
  66. {
  67. }
  68. PS2MouseDevice& PS2MouseDevice::the()
  69. {
  70. return *s_the;
  71. }
  72. void PS2MouseDevice::handle_vmmouse_absolute_pointer()
  73. {
  74. VMWareCommand command;
  75. command.bx = 0;
  76. command.command = VMMOUSE_STATUS;
  77. VMWareBackdoor::the().send(command);
  78. if (command.ax == 0xFFFF0000) {
  79. #ifdef PS2MOUSE_DEBUG
  80. dbg() << "Reset vmmouse.";
  81. #endif
  82. VMWareBackdoor::the().disable_absolute_vmmouse();
  83. VMWareBackdoor::the().enable_absolute_vmmouse();
  84. return;
  85. }
  86. int words = command.ax & 0xFFFF;
  87. if (!words || words % 4)
  88. return;
  89. command.size = 4;
  90. command.command = VMMOUSE_DATA;
  91. VMWareBackdoor::the().send(command);
  92. int buttons = (command.ax & 0xFFFF);
  93. int x = (command.bx);
  94. int y = (command.cx);
  95. int z = (command.dx);
  96. #ifdef PS2MOUSE_DEBUG
  97. dbg() << "Absolute Mouse: Buttons " << String::format("%x", buttons);
  98. dbg() << "Mouse: X " << x << ", Y " << y << ", Z " << z;
  99. #endif
  100. MousePacket packet;
  101. packet.x = x;
  102. packet.y = y;
  103. packet.z = z;
  104. if (buttons & VMMOUSE_LEFT_CLICK) {
  105. packet.buttons |= PS2MOUSE_LEFT_CLICK;
  106. }
  107. if (buttons & VMMOUSE_RIGHT_CLICK) {
  108. packet.buttons |= PS2MOUSE_RIGHT_CLICK;
  109. }
  110. if (buttons & VMMOUSE_MIDDLE_CLICK) {
  111. packet.buttons |= PS2MOUSE_MIDDLE_CLICK;
  112. }
  113. packet.is_relative = false;
  114. m_queue.enqueue(packet);
  115. }
  116. void PS2MouseDevice::handle_irq(const RegisterState&)
  117. {
  118. if (VMWareBackdoor::the().vmmouse_is_absolute()) {
  119. #ifdef PS2MOUSE_DEBUG
  120. dbg() << "Handling PS2 vmmouse.";
  121. #endif
  122. IO::in8(I8042_BUFFER);
  123. handle_vmmouse_absolute_pointer();
  124. return;
  125. }
  126. #ifdef PS2MOUSE_DEBUG
  127. dbg() << "Handling classical PS2 mouse.";
  128. #endif
  129. for (;;) {
  130. u8 status = IO::in8(I8042_STATUS);
  131. if (!(((status & I8042_WHICH_BUFFER) == I8042_MOUSE_BUFFER) && (status & I8042_BUFFER_FULL)))
  132. return;
  133. u8 data = IO::in8(I8042_BUFFER);
  134. m_data[m_data_state] = data;
  135. auto commit_packet = [&] {
  136. m_data_state = 0;
  137. #ifdef PS2MOUSE_DEBUG
  138. dbg() << "PS2Mouse: " << m_data[1] << ", " << m_data[2] << " " << ((m_data[0] & 1) ? "Left" : "") << " " << ((m_data[0] & 2) ? "Right" : "") << " (buffered: " << m_queue.size() << ")";
  139. #endif
  140. parse_data_packet();
  141. };
  142. switch (m_data_state) {
  143. case 0:
  144. if (!(data & 0x08)) {
  145. dbg() << "PS2Mouse: Stream out of sync.";
  146. break;
  147. }
  148. ++m_data_state;
  149. break;
  150. case 1:
  151. ++m_data_state;
  152. break;
  153. case 2:
  154. if (m_has_wheel) {
  155. ++m_data_state;
  156. break;
  157. }
  158. commit_packet();
  159. break;
  160. case 3:
  161. ASSERT(m_has_wheel);
  162. commit_packet();
  163. break;
  164. }
  165. }
  166. }
  167. void PS2MouseDevice::parse_data_packet()
  168. {
  169. int x = m_data[1];
  170. int y = m_data[2];
  171. int z = 0;
  172. if (m_has_wheel)
  173. z = (char)m_data[3];
  174. bool x_overflow = m_data[0] & 0x40;
  175. bool y_overflow = m_data[0] & 0x80;
  176. bool x_sign = m_data[0] & 0x10;
  177. bool y_sign = m_data[0] & 0x20;
  178. if (x && x_sign)
  179. x -= 0x100;
  180. if (y && y_sign)
  181. y -= 0x100;
  182. if (x_overflow || y_overflow) {
  183. x = 0;
  184. y = 0;
  185. }
  186. MousePacket packet;
  187. packet.x = x;
  188. packet.y = y;
  189. packet.z = z;
  190. packet.buttons = m_data[0] & 0x07;
  191. packet.is_relative = true;
  192. #ifdef PS2MOUSE_DEBUG
  193. dbg() << "PS2 Relative Mouse: Buttons " << String::format("%x", packet.buttons);
  194. dbg() << "Mouse: X " << packet.x << ", Y " << packet.y << ", Z " << packet.z;
  195. #endif
  196. m_queue.enqueue(packet);
  197. }
  198. void PS2MouseDevice::wait_then_write(u8 port, u8 data)
  199. {
  200. prepare_for_output();
  201. IO::out8(port, data);
  202. }
  203. u8 PS2MouseDevice::wait_then_read(u8 port)
  204. {
  205. prepare_for_input();
  206. return IO::in8(port);
  207. }
  208. void PS2MouseDevice::initialize()
  209. {
  210. // Enable PS aux port
  211. wait_then_write(I8042_STATUS, 0xa8);
  212. check_device_presence();
  213. if (m_device_present)
  214. initialize_device();
  215. }
  216. void PS2MouseDevice::check_device_presence()
  217. {
  218. mouse_write(PS2MOUSE_REQUEST_SINGLE_PACKET);
  219. u8 maybe_ack = mouse_read();
  220. if (maybe_ack == I8042_ACK) {
  221. m_device_present = true;
  222. klog() << "PS2MouseDevice: Device detected";
  223. // the mouse will send a packet of data, since that's what we asked
  224. // for. we don't care about the content.
  225. mouse_read();
  226. mouse_read();
  227. mouse_read();
  228. } else {
  229. m_device_present = false;
  230. klog() << "PS2MouseDevice: Device not detected";
  231. }
  232. }
  233. void PS2MouseDevice::initialize_device()
  234. {
  235. if (!m_device_present)
  236. return;
  237. // Enable interrupts
  238. wait_then_write(I8042_STATUS, 0x20);
  239. // Enable the PS/2 mouse IRQ (12).
  240. // NOTE: The keyboard uses IRQ 1 (and is enabled by bit 0 in this register).
  241. u8 status = wait_then_read(I8042_BUFFER) | 2;
  242. wait_then_write(I8042_STATUS, 0x60);
  243. wait_then_write(I8042_BUFFER, status);
  244. // Set default settings.
  245. mouse_write(PS2MOUSE_SET_DEFAULTS);
  246. expect_ack();
  247. // Enable.
  248. mouse_write(PS2MOUSE_ENABLE_PACKET_STREAMING);
  249. expect_ack();
  250. mouse_write(PS2MOUSE_GET_DEVICE_ID);
  251. expect_ack();
  252. u8 device_id = mouse_read();
  253. if (device_id != PS2MOUSE_INTELLIMOUSE_ID) {
  254. // Send magical wheel initiation sequence.
  255. mouse_write(PS2MOUSE_SET_SAMPLE_RATE);
  256. expect_ack();
  257. mouse_write(200);
  258. expect_ack();
  259. mouse_write(PS2MOUSE_SET_SAMPLE_RATE);
  260. expect_ack();
  261. mouse_write(100);
  262. expect_ack();
  263. mouse_write(PS2MOUSE_SET_SAMPLE_RATE);
  264. expect_ack();
  265. mouse_write(80);
  266. expect_ack();
  267. mouse_write(PS2MOUSE_GET_DEVICE_ID);
  268. expect_ack();
  269. device_id = mouse_read();
  270. }
  271. if (device_id == PS2MOUSE_INTELLIMOUSE_ID) {
  272. m_has_wheel = true;
  273. klog() << "PS2MouseDevice: Mouse wheel enabled!";
  274. } else {
  275. klog() << "PS2MouseDevice: No mouse wheel detected!";
  276. }
  277. enable_irq();
  278. }
  279. void PS2MouseDevice::expect_ack()
  280. {
  281. u8 data = mouse_read();
  282. ASSERT(data == I8042_ACK);
  283. }
  284. void PS2MouseDevice::prepare_for_input()
  285. {
  286. for (;;) {
  287. if (IO::in8(I8042_STATUS) & 1)
  288. return;
  289. }
  290. }
  291. void PS2MouseDevice::prepare_for_output()
  292. {
  293. for (;;) {
  294. if (!(IO::in8(I8042_STATUS) & 2))
  295. return;
  296. }
  297. }
  298. void PS2MouseDevice::mouse_write(u8 data)
  299. {
  300. prepare_for_output();
  301. IO::out8(I8042_STATUS, 0xd4);
  302. prepare_for_output();
  303. IO::out8(I8042_BUFFER, data);
  304. }
  305. u8 PS2MouseDevice::mouse_read()
  306. {
  307. prepare_for_input();
  308. return IO::in8(I8042_BUFFER);
  309. }
  310. bool PS2MouseDevice::can_read(const FileDescription&) const
  311. {
  312. return !m_queue.is_empty();
  313. }
  314. ssize_t PS2MouseDevice::read(FileDescription&, u8* buffer, ssize_t size)
  315. {
  316. ASSERT(size > 0);
  317. size_t nread = 0;
  318. size_t remaining_space_in_buffer = static_cast<size_t>(size) - nread;
  319. while (!m_queue.is_empty() && remaining_space_in_buffer) {
  320. auto packet = m_queue.dequeue();
  321. #ifdef PS2MOUSE_DEBUG
  322. dbg() << "PS2 Mouse Read: Buttons " << String::format("%x", packet.buttons);
  323. dbg() << "PS2 Mouse: X " << packet.x << ", Y " << packet.y << ", Z " << packet.z << " Relative " << packet.buttons;
  324. dbg() << "PS2 Mouse Read: Filter packets";
  325. #endif
  326. size_t bytes_read_from_packet = min(remaining_space_in_buffer, sizeof(MousePacket));
  327. memcpy(buffer + nread, &packet, bytes_read_from_packet);
  328. nread += bytes_read_from_packet;
  329. remaining_space_in_buffer -= bytes_read_from_packet;
  330. }
  331. return nread;
  332. }
  333. ssize_t PS2MouseDevice::write(FileDescription&, const u8*, ssize_t)
  334. {
  335. return 0;
  336. }
  337. }