PS2MouseDevice.cpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. #include "PS2MouseDevice.h"
  2. #include "IO.h"
  3. //#define PS2MOUSE_DEBUG
  4. static PS2MouseDevice* s_the;
  5. PS2MouseDevice::PS2MouseDevice()
  6. : IRQHandler(12)
  7. , CharacterDevice(10, 1)
  8. {
  9. s_the = this;
  10. initialize();
  11. }
  12. PS2MouseDevice::~PS2MouseDevice()
  13. {
  14. }
  15. PS2MouseDevice& PS2MouseDevice::the()
  16. {
  17. return *s_the;
  18. }
  19. void PS2MouseDevice::handle_irq()
  20. {
  21. byte data = IO::in8(0x60);
  22. m_data[m_data_state] = data;
  23. switch (m_data_state) {
  24. case 0:
  25. if (!(data & 0x08)) {
  26. dbgprintf("PS2Mouse: Stream out of sync.\n");
  27. break;
  28. }
  29. ++m_data_state;
  30. break;
  31. case 1:
  32. ++m_data_state;
  33. break;
  34. case 2:
  35. m_data_state = 0;
  36. #ifdef PS2MOUSE_DEBUG
  37. dbgprintf("PS2Mouse: %d, %d %s %s\n",
  38. m_data[1],
  39. m_data[2],
  40. (m_data[0] & 1) ? "Left" : "",
  41. (m_data[0] & 2) ? "Right" : ""
  42. );
  43. #endif
  44. m_queue.enqueue(m_data[0]);
  45. m_queue.enqueue(m_data[1]);
  46. m_queue.enqueue(m_data[2]);
  47. break;
  48. }
  49. }
  50. void PS2MouseDevice::wait_then_write(byte port, byte data)
  51. {
  52. prepare_for_output();
  53. IO::out8(port, data);
  54. }
  55. byte PS2MouseDevice::wait_then_read(byte port)
  56. {
  57. prepare_for_input();
  58. return IO::in8(port);
  59. }
  60. void PS2MouseDevice::initialize()
  61. {
  62. // Enable PS aux port
  63. wait_then_write(0x64, 0xa8);
  64. // Enable interrupts
  65. wait_then_write(0x64, 0x20);
  66. byte status = wait_then_read(0x60) | 2;
  67. wait_then_write(0x64, 0x60);
  68. wait_then_write(0x60, status);
  69. // Set default settings.
  70. mouse_write(0xf6);
  71. byte ack1 = mouse_read();
  72. ASSERT(ack1 == 0xfa);
  73. // Enable.
  74. mouse_write(0xf4);
  75. byte ack2 = mouse_read();
  76. ASSERT(ack2 == 0xfa);
  77. enable_irq();
  78. }
  79. void PS2MouseDevice::prepare_for_input()
  80. {
  81. for (;;) {
  82. if (IO::in8(0x64) & 1)
  83. return;
  84. }
  85. }
  86. void PS2MouseDevice::prepare_for_output()
  87. {
  88. for (;;) {
  89. if (!(IO::in8(0x64) & 2))
  90. return;
  91. }
  92. }
  93. void PS2MouseDevice::mouse_write(byte data)
  94. {
  95. prepare_for_output();
  96. IO::out8(0x64, 0xd4);
  97. prepare_for_output();
  98. IO::out8(0x60, data);
  99. }
  100. byte PS2MouseDevice::mouse_read()
  101. {
  102. prepare_for_input();
  103. return IO::in8(0x60);
  104. }
  105. bool PS2MouseDevice::can_read(Process&) const
  106. {
  107. return !m_queue.is_empty();
  108. }
  109. ssize_t PS2MouseDevice::read(Process&, byte* buffer, size_t size)
  110. {
  111. ssize_t nread = 0;
  112. while ((size_t)nread < size) {
  113. if (m_queue.is_empty())
  114. break;
  115. // FIXME: Don't return partial data frames.
  116. buffer[nread++] = m_queue.dequeue();
  117. }
  118. return nread;
  119. }
  120. ssize_t PS2MouseDevice::write(Process&, const byte*, size_t)
  121. {
  122. return 0;
  123. }