PS2MouseDevice.cpp 7.8 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 "PS2MouseDevice.h"
  27. #include "IO.h"
  28. #define IRQ_MOUSE 12
  29. #define I8042_BUFFER 0x60
  30. #define I8042_STATUS 0x64
  31. #define I8042_ACK 0xFA
  32. #define I8042_BUFFER_FULL 0x01
  33. #define I8042_WHICH_BUFFER 0x20
  34. #define I8042_MOUSE_BUFFER 0x20
  35. #define I8042_KEYBOARD_BUFFER 0x00
  36. #define PS2MOUSE_SET_RESOLUTION 0xE8
  37. #define PS2MOUSE_STATUS_REQUEST 0xE9
  38. #define PS2MOUSE_REQUEST_SINGLE_PACKET 0xEB
  39. #define PS2MOUSE_GET_DEVICE_ID 0xF2
  40. #define PS2MOUSE_SET_SAMPLE_RATE 0xF3
  41. #define PS2MOUSE_ENABLE_PACKET_STREAMING 0xF4
  42. #define PS2MOUSE_DISABLE_PACKET_STREAMING 0xF5
  43. #define PS2MOUSE_SET_DEFAULTS 0xF6
  44. #define PS2MOUSE_RESEND 0xFE
  45. #define PS2MOUSE_RESET 0xFF
  46. #define PS2MOUSE_INTELLIMOUSE_ID 0x03
  47. //#define PS2MOUSE_DEBUG
  48. static PS2MouseDevice* s_the;
  49. PS2MouseDevice::PS2MouseDevice()
  50. : IRQHandler(IRQ_MOUSE)
  51. , CharacterDevice(10, 1)
  52. {
  53. s_the = this;
  54. initialize();
  55. }
  56. PS2MouseDevice::~PS2MouseDevice()
  57. {
  58. }
  59. PS2MouseDevice& PS2MouseDevice::the()
  60. {
  61. return *s_the;
  62. }
  63. void PS2MouseDevice::handle_irq()
  64. {
  65. for (;;) {
  66. u8 status = IO::in8(I8042_STATUS);
  67. if (!(((status & I8042_WHICH_BUFFER) == I8042_MOUSE_BUFFER) && (status & I8042_BUFFER_FULL)))
  68. return;
  69. u8 data = IO::in8(I8042_BUFFER);
  70. m_data[m_data_state] = data;
  71. auto commit_packet = [&] {
  72. m_data_state = 0;
  73. #ifdef PS2MOUSE_DEBUG
  74. dbgprintf("PS2Mouse: %d, %d %s %s (buffered: %u)\n",
  75. m_data[1],
  76. m_data[2],
  77. (m_data[0] & 1) ? "Left" : "",
  78. (m_data[0] & 2) ? "Right" : "",
  79. m_queue.size());
  80. #endif
  81. parse_data_packet();
  82. };
  83. switch (m_data_state) {
  84. case 0:
  85. if (!(data & 0x08)) {
  86. dbgprintf("PS2Mouse: Stream out of sync.\n");
  87. break;
  88. }
  89. ++m_data_state;
  90. break;
  91. case 1:
  92. ++m_data_state;
  93. break;
  94. case 2:
  95. if (m_has_wheel) {
  96. ++m_data_state;
  97. break;
  98. }
  99. commit_packet();
  100. break;
  101. case 3:
  102. ASSERT(m_has_wheel);
  103. commit_packet();
  104. break;
  105. }
  106. }
  107. }
  108. void PS2MouseDevice::parse_data_packet()
  109. {
  110. int x = m_data[1];
  111. int y = m_data[2];
  112. int z = 0;
  113. if (m_has_wheel)
  114. z = (char)m_data[3];
  115. bool x_overflow = m_data[0] & 0x40;
  116. bool y_overflow = m_data[0] & 0x80;
  117. bool x_sign = m_data[0] & 0x10;
  118. bool y_sign = m_data[0] & 0x20;
  119. if (x && x_sign)
  120. x -= 0x100;
  121. if (y && y_sign)
  122. y -= 0x100;
  123. if (x_overflow || y_overflow) {
  124. x = 0;
  125. y = 0;
  126. }
  127. MousePacket packet;
  128. packet.dx = x;
  129. packet.dy = y;
  130. packet.dz = z;
  131. packet.buttons = m_data[0] & 0x07;
  132. m_queue.enqueue(packet);
  133. }
  134. void PS2MouseDevice::wait_then_write(u8 port, u8 data)
  135. {
  136. prepare_for_output();
  137. IO::out8(port, data);
  138. }
  139. u8 PS2MouseDevice::wait_then_read(u8 port)
  140. {
  141. prepare_for_input();
  142. return IO::in8(port);
  143. }
  144. void PS2MouseDevice::initialize()
  145. {
  146. // Enable PS aux port
  147. wait_then_write(I8042_STATUS, 0xa8);
  148. check_device_presence();
  149. if (m_device_present)
  150. initialize_device();
  151. }
  152. void PS2MouseDevice::check_device_presence()
  153. {
  154. mouse_write(PS2MOUSE_REQUEST_SINGLE_PACKET);
  155. u8 maybe_ack = mouse_read();
  156. if (maybe_ack == I8042_ACK) {
  157. m_device_present = true;
  158. kprintf("PS2MouseDevice: Device detected\n");
  159. // the mouse will send a packet of data, since that's what we asked
  160. // for. we don't care about the content.
  161. mouse_read();
  162. mouse_read();
  163. mouse_read();
  164. } else {
  165. m_device_present = false;
  166. kprintf("PS2MouseDevice: Device not detected\n");
  167. }
  168. }
  169. void PS2MouseDevice::initialize_device()
  170. {
  171. if (!m_device_present)
  172. return;
  173. // Enable interrupts
  174. wait_then_write(I8042_STATUS, 0x20);
  175. // Enable the PS/2 mouse IRQ (12).
  176. // NOTE: The keyboard uses IRQ 1 (and is enabled by bit 0 in this register).
  177. u8 status = wait_then_read(I8042_BUFFER) | 2;
  178. wait_then_write(I8042_STATUS, 0x60);
  179. wait_then_write(I8042_BUFFER, status);
  180. // Set default settings.
  181. mouse_write(PS2MOUSE_SET_DEFAULTS);
  182. expect_ack();
  183. // Enable.
  184. mouse_write(PS2MOUSE_ENABLE_PACKET_STREAMING);
  185. expect_ack();
  186. mouse_write(PS2MOUSE_GET_DEVICE_ID);
  187. expect_ack();
  188. u8 device_id = mouse_read();
  189. if (device_id != PS2MOUSE_INTELLIMOUSE_ID) {
  190. // Send magical wheel initiation sequence.
  191. mouse_write(PS2MOUSE_SET_SAMPLE_RATE);
  192. expect_ack();
  193. mouse_write(200);
  194. expect_ack();
  195. mouse_write(PS2MOUSE_SET_SAMPLE_RATE);
  196. expect_ack();
  197. mouse_write(100);
  198. expect_ack();
  199. mouse_write(PS2MOUSE_SET_SAMPLE_RATE);
  200. expect_ack();
  201. mouse_write(80);
  202. expect_ack();
  203. mouse_write(PS2MOUSE_GET_DEVICE_ID);
  204. expect_ack();
  205. device_id = mouse_read();
  206. }
  207. if (device_id == PS2MOUSE_INTELLIMOUSE_ID) {
  208. m_has_wheel = true;
  209. kprintf("PS2MouseDevice: Mouse wheel enabled!\n");
  210. } else {
  211. kprintf("PS2MouseDevice: No mouse wheel detected!\n");
  212. }
  213. enable_irq();
  214. }
  215. void PS2MouseDevice::expect_ack()
  216. {
  217. u8 data = mouse_read();
  218. ASSERT(data == I8042_ACK);
  219. }
  220. void PS2MouseDevice::prepare_for_input()
  221. {
  222. for (;;) {
  223. if (IO::in8(I8042_STATUS) & 1)
  224. return;
  225. }
  226. }
  227. void PS2MouseDevice::prepare_for_output()
  228. {
  229. for (;;) {
  230. if (!(IO::in8(I8042_STATUS) & 2))
  231. return;
  232. }
  233. }
  234. void PS2MouseDevice::mouse_write(u8 data)
  235. {
  236. prepare_for_output();
  237. IO::out8(I8042_STATUS, 0xd4);
  238. prepare_for_output();
  239. IO::out8(I8042_BUFFER, data);
  240. }
  241. u8 PS2MouseDevice::mouse_read()
  242. {
  243. prepare_for_input();
  244. return IO::in8(I8042_BUFFER);
  245. }
  246. bool PS2MouseDevice::can_read(const FileDescription&) const
  247. {
  248. return !m_queue.is_empty();
  249. }
  250. ssize_t PS2MouseDevice::read(FileDescription&, u8* buffer, ssize_t size)
  251. {
  252. ssize_t nread = 0;
  253. while (nread < size) {
  254. if (m_queue.is_empty())
  255. break;
  256. // Don't return partial data frames.
  257. if ((size - nread) < (ssize_t)sizeof(MousePacket))
  258. break;
  259. auto packet = m_queue.dequeue();
  260. memcpy(buffer, &packet, sizeof(MousePacket));
  261. nread += sizeof(MousePacket);
  262. }
  263. return nread;
  264. }
  265. ssize_t PS2MouseDevice::write(FileDescription&, const u8*, ssize_t)
  266. {
  267. return 0;
  268. }