PS2MouseDevice.cpp 2.6 KB

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