PS2MouseDevice.cpp 9.8 KB

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