diff --git a/main/adapter/ps.c b/main/adapter/ps.c index 2f2f6091..b14c8a2f 100644 --- a/main/adapter/ps.c +++ b/main/adapter/ps.c @@ -7,6 +7,7 @@ #include "../zephyr/types.h" #include "../util.h" #include "config.h" +#include "kb_monitor.h" #include "ps.h" enum { @@ -102,10 +103,49 @@ static const uint32_t ps_mouse_btns_mask[32] = { BIT(PS_R1), 0, 0, 0, }; +static const uint32_t ps_kb_mask[4] = {0xE6FF0F0F, 0xFFFFFFFF, 0xFFFFFFFF, 0x0007FFFF}; +static const uint32_t ps_kb_desc[4] = {0x00000000, 0x00000000, 0x00000000, 0x00000000}; +static const uint8_t ps_kb_scancode[KBM_MAX] = { + /* KB_A, KB_D, KB_S, KB_W, MOUSE_X_LEFT, MOUSE_X_RIGHT, MOUSE_Y_DOWN MOUSE_Y_UP */ + 0x1C, 0x23, 0x1B, 0x1D, 0x00, 0x00, 0x00, 0x00, + /* KB_LEFT, KB_RIGHT, KB_DOWN, KB_UP, MOUSE_WX_LEFT, MOUSE_WX_RIGHT, MOUSE_WY_DOWN, MOUSE_WY_UP */ + 0x86, 0x8D, 0x8A, 0x89, 0x00, 0x00, 0x00, 0x00, + /* KB_Q, KB_R, KB_E, KB_F, KB_ESC, KB_ENTER, KB_LWIN, KB_HASH */ + 0x15, 0x2D, 0x24, 0x2B, 0x76, 0x5A, 0x1F, 0x0E, + /* MOUSE_RIGHT, KB_Z, KB_LCTRL, MOUSE_MIDDLE, MOUSE_LEFT, KB_X, KB_LSHIFT, KB_SPACE */ + 0x00, 0x1A, 0x14, 0x00, 0x00, 0x22, 0x12, 0x29, + + /* KB_B, KB_C, KB_G, KB_H, KB_I, KB_J, KB_K, KB_L */ + 0x32, 0x21, 0x34, 0x33, 0x43, 0x3B, 0x42, 0x4B, + /* KB_M, KB_N, KB_O, KB_P, KB_T, KB_U, KB_V, KB_Y */ + 0x3A, 0x31, 0x44, 0x4D, 0x2C, 0x3C, 0x2A, 0x35, + /* KB_1, KB_2, KB_3, KB_4, KB_5, KB_6, KB_7, KB_8 */ + 0x16, 0x1E, 0x26, 0x25, 0x2E, 0x36, 0x3D, 0x3E, + /* KB_9, KB_0, KB_BACKSPACE, KB_TAB, KB_MINUS, KB_EQUAL, KB_LEFTBRACE, KB_RIGHTBRACE */ + 0x46, 0x45, 0x66, 0x0D, 0x4E, 0x55, 0x54, 0x5B, + + /* KB_BACKSLASH, KB_SEMICOLON, KB_APOSTROPHE, KB_GRAVE, KB_COMMA, KB_DOT, KB_SLASH, KB_CAPSLOCK */ + 0x5D, 0x4C, 0x51, 0x00, 0x41, 0x49, 0x4A, 0x58, + /* KB_F1, KB_F2, KB_F3, KB_F4, KB_F5, KB_F6, KB_F7, KB_F8 */ + 0x05, 0x06, 0x04, 0x0C, 0x03, 0x0B, 0x83, 0x0A, + /* KB_F9, KB_F10, KB_F11, KB_F12, KB_PSCREEN, KB_SCROLL, KB_PAUSE, KB_INSERT */ + 0x01, 0x09, 0x78, 0x07, 0x84, 0x7E, 0x82, 0x81, + /* KB_HOME, KB_PAGEUP, KB_DEL, KB_END, KB_PAGEDOWN, KB_NUMLOCK, KB_KP_DIV, KB_KP_MULTI */ + 0x87, 0x8B, 0x85, 0x88, 0x8C, 0x77, 0x80, 0x7C, + + /* KB_KP_MINUS, KB_KP_PLUS, KB_KP_ENTER, KB_KP_1, KB_KP_2, KB_KP_3, KB_KP_4, KB_KP_5 */ + 0x7B, 0x79, 0x19, 0x69, 0x72, 0x7A, 0x6B, 0x73, + /* KB_KP_6, KB_KP_7, KB_KP_8, KB_KP_9, KB_KP_0, KB_KP_DOT, KB_LALT, KB_RCTRL */ + 0x74, 0x6C, 0x75, 0x7D, 0x70, 0x71, 0x11, 0x18, + /* KB_RSHIFT, KB_RALT, KB_RWIN */ + 0x59, 0x17, 0x00, +}; + void ps_init_buffer(int32_t dev_mode, struct wired_data *wired_data) { switch (dev_mode) { case DEV_KB: { + memset(wired_data->output, 0x00, 12); break; } case DEV_MOUSE: @@ -142,6 +182,8 @@ void ps_meta_init(struct generic_ctrl *ctrl_data) { for (uint32_t j = 0; j < ADAPTER_MAX_AXES; j++) { switch (config.out_cfg[i].dev_mode) { case DEV_KB: + ctrl_data[i].mask = ps_kb_mask; + ctrl_data[i].desc = ps_kb_desc; goto exit_axes_loop; case DEV_MOUSE: ctrl_data[i].mask = ps_mouse_mask; @@ -244,6 +286,29 @@ static void ps_mouse_from_generic(struct generic_ctrl *ctrl_data, struct wired_d } static void ps_kb_from_generic(struct generic_ctrl *ctrl_data, struct wired_data *wired_data) { + kbmon_update(ctrl_data->index, ctrl_data); +} + +void ps_kb_id_to_scancode(uint8_t dev_id, uint8_t type, uint8_t id) { + uint8_t kb_buf[12] = {0}; + uint8_t scancode = ps_kb_scancode[id]; + uint32_t idx = 0; + + if (scancode) { + switch (type) { + case KBMON_TYPE_MAKE: + kb_buf[idx++] = 0x01; + break; + case KBMON_TYPE_BREAK: + kb_buf[idx++] = 0x02; + kb_buf[idx++] = 0xF0; + break; + } + if (id < KBM_MAX) { + kb_buf[idx++] = scancode; + } + kbmon_set_code(dev_id, kb_buf, 12); + } } void ps_from_generic(int32_t dev_mode, struct generic_ctrl *ctrl_data, struct wired_data *wired_data) { diff --git a/main/adapter/ps.h b/main/adapter/ps.h index 7503d5f8..05334a47 100644 --- a/main/adapter/ps.h +++ b/main/adapter/ps.h @@ -10,6 +10,7 @@ void ps_meta_init(struct generic_ctrl *ctrl_data); void ps_init_buffer(int32_t dev_mode, struct wired_data *wired_data); void ps_from_generic(int32_t dev_mode, struct generic_ctrl *ctrl_data, struct wired_data *wired_data); +void ps_kb_id_to_scancode(uint8_t dev_id, uint8_t type, uint8_t id); void ps_fb_to_generic(int32_t dev_mode, uint8_t *raw_fb_data, uint32_t raw_fb_len, struct generic_fb *fb_data); #endif /* _PS_H_ */ diff --git a/main/wired/ps_spi.c b/main/wired/ps_spi.c index f46bffa6..3d8a7d6d 100644 --- a/main/wired/ps_spi.c +++ b/main/wired/ps_spi.c @@ -13,6 +13,8 @@ #include "../util.h" #include "../adapter/adapter.h" #include "../adapter/config.h" +#include "../adapter/kb_monitor.h" +#include "../adapter/ps.h" #include "ps_spi.h" enum { @@ -276,7 +278,14 @@ static void IRAM_ATTR ps_cmd_rsp_hdlr(struct ps_ctrl_port *port, uint8_t id, uin if (size < 6) { size = 6; } - memcpy(rsp, wired_adapter.data[id + port->mt_first_port].output, size); + if (port->dev_type == DEV_PSX_PS_2_KB_MOUSE_ADAPTER) { + uint32_t len = 0; + memset(rsp, 0x00, size); + kbmon_get_code(id + port->mt_first_port, rsp, &len); + } + else { + memcpy(rsp, wired_adapter.data[id + port->mt_first_port].output, size); + } if (port->dev_type == DEV_PSX_MOUSE) { wired_adapter.data[id + port->mt_first_port].output[2] = 0x00; wired_adapter.data[id + port->mt_first_port].output[3] = 0x00; @@ -565,6 +574,9 @@ void ps_spi_init(void) { break; case DEV_KB: ps_ctrl_ports[i].dev_type = DEV_PSX_PS_2_KB_MOUSE_ADAPTER; + ps_ctrl_ports[i].dev_id[0] = 0x96; + kbmon_init(i, ps_kb_id_to_scancode); + kbmon_set_typematic(i, 1, 500000, 90000); break; case DEV_MOUSE: ps_ctrl_ports[i].dev_type = DEV_PSX_MOUSE;