fix: refining the tty input
[lunaix-os.git] / lunaix-os / includes / lunaix / peripheral / ps2kbd.h
1 #ifndef __LUNAIX_PS2KBD_H
2 #define __LUNAIX_PS2KBD_H
3
4 #include <hal/io.h>
5 #include <lunaix/ds/mutex.h>
6 #include <lunaix/keyboard.h>
7
8 #define PS2_PORT_ENC_DATA 0x60
9 #define PS2_PORT_ENC_CMDREG 0x60
10 #define PS2_PORT_CTRL_STATUS 0x64
11 #define PS2_PORT_CTRL_CMDREG 0x64
12
13 #define PS2_STATUS_OFULL 0x1
14 #define PS2_STATUS_IFULL 0x2
15
16 #define PS2_RESULT_ACK 0xfa
17 #define PS2_RESULT_NAK 0xfe // resend
18 #define PS2_RESULT_ECHO 0xee
19 #define PS2_RESULT_TEST_OK 0x55
20
21 // PS/2 keyboard device related commands
22 #define PS2_KBD_CMD_SETLED 0xed
23 #define PS2_KBD_CMD_ECHO 0xee
24 #define PS2_KBD_CMD_SCANCODE_SET 0xf0
25 #define PS2_KBD_CMD_IDENTIFY 0xf2
26 #define PS2_KBD_CMD_SCAN_ENABLE 0xf4
27 #define PS2_KBD_CMD_SCAN_DISABLE 0xf5
28
29 // PS/2 *controller* related commands
30 #define PS2_CMD_PORT1_DISABLE 0xad
31 #define PS2_CMD_PORT1_ENABLE 0xae
32 #define PS2_CMD_PORT2_DISABLE 0xa7
33 #define PS2_CMD_PORT2_ENABLE 0xa8
34 #define PS2_CMD_SELFTEST 0xaa
35 #define PS2_CMD_SELFTEST_PORT1 0xab
36
37 #define PS2_CMD_READ_CFG 0x20
38 #define PS2_CMD_WRITE_CFG 0x60
39
40 #define PS2_CFG_P1INT 0x1
41 #define PS2_CFG_P2INT 0x2
42 #define PS2_CFG_TRANSLATION 0x40
43
44 #define PS2_DELAY 1000
45
46 #define PS2_CMD_QUEUE_SIZE 8
47 #define PS2_KBD_RECV_BUFFER_SIZE 8
48
49 #define PS2_NO_ARG 0xff00
50
51 struct ps2_cmd
52 {
53     char cmd;
54     char arg;
55 };
56
57 struct ps2_kbd_state
58 {
59     volatile char state;
60     volatile char masked;
61     volatile kbd_kstate_t key_state;
62     kbd_keycode_t* translation_table;
63 };
64
65 struct ps2_cmd_queue
66 {
67     struct ps2_cmd cmd_queue[PS2_CMD_QUEUE_SIZE];
68     int queue_ptr;
69     int queue_len;
70     mutex_t mutex;
71 };
72
73 struct ps2_key_buffer
74 {
75     struct kdb_keyinfo_pkt buffer[PS2_KBD_RECV_BUFFER_SIZE];
76     int read_ptr;
77     int buffered_len;
78     mutex_t mutex;
79 };
80
81 /**
82  * @brief 向PS/2控制器的控制端口(0x64)发送指令并等待返回代码。
83  * 注意,对于没有返回代码的命令请使用`ps2_post_cmd`,否则会造成死锁。
84  * 通过调用该方法向控制器发送指令,请区别 ps2_issue_dev_cmd
85  *
86  * @param cmd
87  * @param args
88  */
89 static uint8_t
90 ps2_issue_cmd(char cmd, uint16_t arg);
91
92 /**
93  * @brief 向PS/2控制器的编码器端口(0x60)发送指令并等待返回代码。
94  * 注意,对于没有返回代码的命令请使用`ps2_post_cmd`,否则会造成死锁。
95  * 通过调用该方法向PS/2设备发送指令,请区别 ps2_issue_cmd
96  *
97  * @param cmd
98  * @param args
99  */
100 static uint8_t
101 ps2_issue_dev_cmd(char cmd, uint16_t arg);
102
103 /**
104  * @brief 向PS/2控制器发送指令,不等待返回代码。
105  *
106  * @param port 端口号
107  * @param cmd
108  * @param args
109  * @return char
110  */
111 static void
112 ps2_post_cmd(uint8_t port, char cmd, uint16_t arg);
113
114 void
115 ps2_device_post_cmd(char cmd, char arg);
116
117 void
118 ps2_kbd_init();
119
120 void
121 ps2_process_cmd(void* arg);
122
123 #endif /* __LUNAIX_PS2KBD_H */