PS2MouseDevice.cpp 10 KB

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