I8042Controller.cpp 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. /*
  2. * Copyright (c) 2020, The SerenityOS developers.
  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/ACPI/Parser.h>
  27. #include <Kernel/Devices/KeyboardDevice.h>
  28. #include <Kernel/Devices/PS2MouseDevice.h>
  29. #include <Kernel/IO.h>
  30. namespace Kernel {
  31. static I8042Controller* s_the;
  32. UNMAP_AFTER_INIT void I8042Controller::initialize()
  33. {
  34. if (ACPI::Parser::the()->have_8042())
  35. new I8042Controller;
  36. }
  37. I8042Controller& I8042Controller::the()
  38. {
  39. VERIFY(s_the);
  40. return *s_the;
  41. }
  42. UNMAP_AFTER_INIT I8042Controller::I8042Controller()
  43. {
  44. VERIFY(!s_the);
  45. s_the = this;
  46. u8 configuration;
  47. {
  48. ScopedSpinLock lock(m_lock);
  49. // Disable devices
  50. do_wait_then_write(I8042_STATUS, 0xad);
  51. do_wait_then_write(I8042_STATUS, 0xa7); // ignored if it doesn't exist
  52. // Drain buffers
  53. do_drain();
  54. do_wait_then_write(I8042_STATUS, 0x20);
  55. configuration = do_wait_then_read(I8042_BUFFER);
  56. do_wait_then_write(I8042_STATUS, 0x60);
  57. configuration &= ~3; // Disable IRQs for all
  58. do_wait_then_write(I8042_BUFFER, configuration);
  59. m_is_dual_channel = (configuration & (1 << 5)) != 0;
  60. dbgln("I8042: {} channel controller",
  61. m_is_dual_channel ? "Dual" : "Single");
  62. // Perform controller self-test
  63. do_wait_then_write(I8042_STATUS, 0xaa);
  64. if (do_wait_then_read(I8042_BUFFER) == 0x55) {
  65. // Restore configuration in case the controller reset
  66. do_wait_then_write(I8042_STATUS, 0x60);
  67. do_wait_then_write(I8042_BUFFER, configuration);
  68. } else {
  69. dbgln("I8042: Controller self test failed");
  70. }
  71. // Test ports and enable them if available
  72. do_wait_then_write(I8042_STATUS, 0xab); // test
  73. m_devices[0].available = (do_wait_then_read(I8042_BUFFER) == 0);
  74. if (m_devices[0].available) {
  75. do_wait_then_write(I8042_STATUS, 0xae); //enable
  76. configuration |= 1;
  77. configuration &= ~(1 << 4);
  78. } else {
  79. dbgln("I8042: Keyboard port not available");
  80. }
  81. if (m_is_dual_channel) {
  82. do_wait_then_write(I8042_STATUS, 0xa9); // test
  83. m_devices[1].available = (do_wait_then_read(I8042_BUFFER) == 0);
  84. if (m_devices[1].available) {
  85. do_wait_then_write(I8042_STATUS, 0xa8); // enable
  86. configuration |= 2;
  87. configuration &= ~(1 << 5);
  88. } else {
  89. dbgln("I8042: Mouse port not available");
  90. }
  91. }
  92. // Enable IRQs for the ports that are usable
  93. if (m_devices[0].available || m_devices[1].available) {
  94. configuration &= ~0x30; // renable clocks
  95. do_wait_then_write(I8042_STATUS, 0x60);
  96. do_wait_then_write(I8042_BUFFER, configuration);
  97. }
  98. }
  99. // Try to detect and initialize the devices
  100. if (m_devices[0].available) {
  101. if (KeyboardDevice::the().initialize()) {
  102. m_devices[0].device = &KeyboardDevice::the();
  103. } else {
  104. dbgln("I8042: Keyboard device failed to initialize, disable");
  105. m_devices[0].available = false;
  106. configuration &= ~1;
  107. configuration |= 1 << 4;
  108. ScopedSpinLock lock(m_lock);
  109. do_wait_then_write(I8042_STATUS, 0x60);
  110. do_wait_then_write(I8042_BUFFER, configuration);
  111. }
  112. }
  113. if (m_devices[1].available) {
  114. if (PS2MouseDevice::the().initialize()) {
  115. m_devices[1].device = &PS2MouseDevice::the();
  116. } else {
  117. dbgln("I8042: Mouse device failed to initialize, disable");
  118. m_devices[1].available = false;
  119. configuration |= 1 << 5;
  120. ScopedSpinLock lock(m_lock);
  121. do_wait_then_write(I8042_STATUS, 0x60);
  122. do_wait_then_write(I8042_BUFFER, configuration);
  123. }
  124. }
  125. // Enable IRQs after both are detected and initialized
  126. if (m_devices[0].device)
  127. m_devices[0].device->enable_interrupts();
  128. if (m_devices[1].device)
  129. m_devices[1].device->enable_interrupts();
  130. }
  131. void I8042Controller::irq_process_input_buffer(Device)
  132. {
  133. VERIFY(Processor::current().in_irq());
  134. u8 status = IO::in8(I8042_STATUS);
  135. if (!(status & I8042_BUFFER_FULL))
  136. return;
  137. Device data_for_device = ((status & I8042_WHICH_BUFFER) == I8042_MOUSE_BUFFER) ? Device::Mouse : Device::Keyboard;
  138. u8 byte = IO::in8(I8042_BUFFER);
  139. if (auto* device = m_devices[data_for_device == Device::Keyboard ? 0 : 1].device)
  140. device->irq_handle_byte_read(byte);
  141. }
  142. void I8042Controller::do_drain()
  143. {
  144. for (;;) {
  145. u8 status = IO::in8(I8042_STATUS);
  146. if (!(status & I8042_BUFFER_FULL))
  147. return;
  148. IO::in8(I8042_BUFFER);
  149. }
  150. }
  151. bool I8042Controller::do_reset_device(Device device)
  152. {
  153. VERIFY(device != Device::None);
  154. VERIFY(m_lock.is_locked());
  155. VERIFY(!Processor::current().in_irq());
  156. if (do_send_command(device, 0xff) != I8042_ACK)
  157. return false;
  158. // Wait until we get the self-test result
  159. return do_wait_then_read(I8042_BUFFER) == 0xaa;
  160. }
  161. u8 I8042Controller::do_send_command(Device device, u8 command)
  162. {
  163. VERIFY(device != Device::None);
  164. VERIFY(m_lock.is_locked());
  165. VERIFY(!Processor::current().in_irq());
  166. return do_write_to_device(device, command);
  167. }
  168. u8 I8042Controller::do_send_command(Device device, u8 command, u8 data)
  169. {
  170. VERIFY(device != Device::None);
  171. VERIFY(m_lock.is_locked());
  172. VERIFY(!Processor::current().in_irq());
  173. u8 response = do_write_to_device(device, command);
  174. if (response == I8042_ACK)
  175. response = do_write_to_device(device, data);
  176. return response;
  177. }
  178. u8 I8042Controller::do_write_to_device(Device device, u8 data)
  179. {
  180. VERIFY(device != Device::None);
  181. VERIFY(m_lock.is_locked());
  182. VERIFY(!Processor::current().in_irq());
  183. int attempts = 0;
  184. u8 response;
  185. do {
  186. if (device != Device::Keyboard) {
  187. prepare_for_output();
  188. IO::out8(I8042_STATUS, 0xd4);
  189. }
  190. prepare_for_output();
  191. IO::out8(I8042_BUFFER, data);
  192. response = do_wait_then_read(I8042_BUFFER);
  193. } while (response == I8042_RESEND && ++attempts < 3);
  194. if (attempts >= 3)
  195. dbgln("Failed to write byte to device, gave up");
  196. return response;
  197. }
  198. u8 I8042Controller::do_read_from_device(Device device)
  199. {
  200. VERIFY(device != Device::None);
  201. prepare_for_input(device);
  202. return IO::in8(I8042_BUFFER);
  203. }
  204. void I8042Controller::prepare_for_input(Device device)
  205. {
  206. VERIFY(m_lock.is_locked());
  207. const u8 buffer_type = device == Device::Keyboard ? I8042_KEYBOARD_BUFFER : I8042_MOUSE_BUFFER;
  208. for (;;) {
  209. u8 status = IO::in8(I8042_STATUS);
  210. if ((status & I8042_BUFFER_FULL) && (device == Device::None || ((status & I8042_WHICH_BUFFER) == buffer_type)))
  211. return;
  212. }
  213. }
  214. void I8042Controller::prepare_for_output()
  215. {
  216. VERIFY(m_lock.is_locked());
  217. for (;;) {
  218. if (!(IO::in8(I8042_STATUS) & 2))
  219. return;
  220. }
  221. }
  222. void I8042Controller::do_wait_then_write(u8 port, u8 data)
  223. {
  224. VERIFY(m_lock.is_locked());
  225. prepare_for_output();
  226. IO::out8(port, data);
  227. }
  228. u8 I8042Controller::do_wait_then_read(u8 port)
  229. {
  230. VERIFY(m_lock.is_locked());
  231. prepare_for_input(Device::None);
  232. return IO::in8(port);
  233. }
  234. }