helos1/driver/irq/pic/ps2/ps2.c

112 lines
3.5 KiB
C
Raw Normal View History

2021-10-10 14:39:17 +08:00
#include "ps2.h"
#include "../pic.h"
#include "../internal.h"
#include "internal.h"
#include "../../../../interrupt/interrupt.h"
#include "../../../../runtime/panic_assert.h"
#include "../../../../runtime/stdio.h"
#include "../../../../graphics/graphics.h"
#include "../../../../driver/input/source.h"
2021-10-10 14:39:17 +08:00
2021-11-04 22:31:28 +08:00
bool pic_ps2_HasMouse;
bool pic_ps2_Mouse4Bytes; // the mouse has 4-byte data packages instead of 3
2021-10-10 14:39:17 +08:00
2021-11-04 22:31:28 +08:00
queue pic_ps2_QueueKeyboard, pic_ps2_QueueMouse;
uint8_t __pic_ps2_QueueBufferK[PIC_PS2_QUEUESIZE_KEYBOARD], __pic_ps2_QueueBufferM[PIC_PS2_QUEUESIZE_MOUSE];
2021-10-10 14:39:17 +08:00
2021-11-04 22:31:28 +08:00
void pic_ps2_Init() {
assert(irq_pic_Enabled && "pic_ps2_Init() requires PIC to be enabled");
2021-10-10 14:39:17 +08:00
// init the Keyboard and Mouse queues
2021-11-04 22:31:28 +08:00
queue_InitBuffered(&pic_ps2_QueueKeyboard, __pic_ps2_QueueBufferK, PIC_PS2_QUEUESIZE_KEYBOARD);
queue_InitBuffered(&pic_ps2_QueueMouse, __pic_ps2_QueueBufferM, PIC_PS2_QUEUESIZE_MOUSE);
2021-10-10 14:39:17 +08:00
uint8_t data;
INTERRUPT_DISABLE;
2021-11-04 22:31:28 +08:00
irq_pic_IRQHandler[PIC_PS2_KEYBOARD] = pic_ps2_IRQHandlerK;
irq_pic_Mask(PIC_PS2_KEYBOARD, false);
2021-10-10 14:39:17 +08:00
// enable second PS/2 port
io_WriteConsoleASCII("ENABLE_MOUSE... ");
2021-11-04 22:31:28 +08:00
__ps2_WriteCommand(PIC_PS2_CMD_ENABLE_MOUSE);
2021-10-10 14:39:17 +08:00
io_WriteConsoleASCII("CONTROLLER_READ_CONFIGBYTE... ");
2021-11-04 22:31:28 +08:00
__ps2_WriteCommand(PIC_PS2_CMD_READ_CONFIGBYTE);
uint8_t config = __ps2_ReadData();
// write controller mode (|= Port1Translation)
io_WriteConsoleASCII("CONTROLLER_WRITE_CONFIGBYTE... ");
2021-11-04 22:31:28 +08:00
__ps2_WriteCommandData(PIC_PS2_CMD_WRITE_CONFIGBYTE, config | PIC_PS2_CONFIG_PORT1_TRANSLATION);
2021-11-04 22:31:28 +08:00
if (config & PIC_PS2_CONFIG_PORT1_CLOCK) { // mouse not present
pic_ps2_HasMouse = false;
io_WriteConsoleASCII("PS/2 Controller has no mouse\n");
INTERRUPT_RESTORE;
return; // early out
}
2021-10-10 14:39:17 +08:00
// initialize the mouse
2021-10-10 14:39:17 +08:00
// reset mouse
io_WriteConsoleASCII("DEVICE_RESET... ");
2021-11-04 22:31:28 +08:00
__ps2_WriteCommandData(PIC_PS2_CMD_SEND_MOUSE, PIC_PS2_CMD_DEVICE_RESET);
while ((data = __ps2_ReadData()) != PIC_PS2_RESET_OK) {
2021-10-10 14:39:17 +08:00
io_Printf("%X ", data);
}
io_Printf("%X ", data);
// enable 4-byte mode for mouse, pure magic!
2021-11-04 22:31:28 +08:00
pic_ps2_Mouse4Bytes = false;
2021-10-10 14:39:17 +08:00
__ps2_SetMouseRate(200);
__ps2_SetMouseRate(100);
__ps2_SetMouseRate(80);
io_WriteConsoleASCII("SEND_MOUSE(PS2_DEVICE_ID)");
2021-11-04 22:31:28 +08:00
__ps2_WriteCommandData(PIC_PS2_CMD_SEND_MOUSE, 0xf2); // get device ID
2021-10-10 14:39:17 +08:00
__ps2_ReadACK();
uint8_t id = __ps2_ReadData(); // receive device ID
io_Printf(", MOUSE PS/2 ID=%d\n", id);
2021-11-04 22:31:28 +08:00
pic_ps2_Mouse4Bytes = (id == 3); // Z-axis is enabled
2021-10-10 14:39:17 +08:00
2021-11-04 22:31:28 +08:00
irq_pic_IRQHandler[PIC_PS2_MOUSE] = pic_ps2_IRQHandlerM;
irq_pic_Mask(PIC_PS2_MOUSE, false);
2021-10-10 14:39:17 +08:00
// set the actual mouse sample rate
2021-11-04 22:31:28 +08:00
__ps2_SetMouseRate(PIC_PS2_MOUSE_SAMPLERATE);
2021-10-10 14:39:17 +08:00
// enable mouse reporting
io_WriteConsoleASCII("MOUSE_ENABLE_REPORTING... ");
2021-11-04 22:31:28 +08:00
__ps2_WriteCommandData(PIC_PS2_CMD_SEND_MOUSE, PIC_PS2_CMD_DEVICE_MOUSE_ENABLE_REPORTING);
__ps2_ReadACK(); // receive ACK
2021-10-10 14:39:17 +08:00
INTERRUPT_RESTORE;
}
2021-11-04 22:31:28 +08:00
SYSV_ABI void pic_ps2_IRQHandlerK() {
queue_PushByte(&pic_ps2_QueueKeyboard, inb(PIC_PS2_IOPORT));
2021-10-10 14:39:17 +08:00
}
2021-11-04 22:31:28 +08:00
SYSV_ABI void pic_ps2_IRQHandlerM() {
queue_PushByte(&pic_ps2_QueueMouse, inb(PIC_PS2_IOPORT));
2021-11-04 22:31:28 +08:00
while (queue_Size(&pic_ps2_QueueMouse) && !(queue_FrontByte(&pic_ps2_QueueMouse) & (1u << 3)))
queue_PopByte(&pic_ps2_QueueMouse);
2021-11-04 22:31:28 +08:00
while (queue_Size(&pic_ps2_QueueMouse) >= (pic_ps2_Mouse4Bytes ? 4 : 3)) {
unsigned int moveX, moveY, state;
2021-11-04 22:31:28 +08:00
state = queue_PopByte(&pic_ps2_QueueMouse);
2021-11-04 22:31:28 +08:00
unsigned int d = queue_PopByte(&pic_ps2_QueueMouse);
moveX = d - ((state << 4) & 0x100);
2021-11-04 22:31:28 +08:00
d = queue_PopByte(&pic_ps2_QueueMouse);
moveY = d - ((state << 3) & 0x100);
input_source_MoveMouse(moveX, -moveY);
2021-11-04 22:31:28 +08:00
if (pic_ps2_Mouse4Bytes)
queue_PopByte(&pic_ps2_QueueMouse);
}
2021-10-10 14:39:17 +08:00
}