ca49cb7592a9e30cd44f1b1775a8cc5d362fd6e7
[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/keyboard.h>
6 #include <lunaix/clock.h>
7
8 #define PS2_PORT_DATA 0x60
9 #define PS2_PORT_STATUS 0x64
10 #define PS2_PORT_CMDREG 0x64
11
12 #define PS2_STATUS_OFULL   0x1
13 #define PS2_STATUS_IFULL   0x2
14
15 #define PS2_RESULT_ACK  0xfa
16 #define PS2_RESULT_NAK  0xfe    //resend
17 #define PS2_RESULT_ECHO 0xee    
18 #define PS2_RESULT_TEST_OK 0x55    
19
20 // PS/2 keyboard device related commands
21 #define PS2_KBD_CMD_SETLED  0xed
22 #define PS2_KBD_CMD_ECHO  0xee
23 #define PS2_KBD_CMD_SCANCODE_SET  0xf0
24 #define PS2_KBD_CMD_IDENTIFY  0xf2
25 #define PS2_KBD_CMD_SCAN_ENABLE  0xf4
26 #define PS2_KBD_CMD_SCAN_DISABLE  0xf5
27
28 // PS/2 *controller* related commands
29 #define PS2_CMD_PORT1_DISABLE  0xad
30 #define PS2_CMD_PORT1_ENABLE  0xae
31 #define PS2_CMD_PORT2_DISABLE  0xa7
32 #define PS2_CMD_PORT2_ENABLE  0xa8
33 #define PS2_CMD_SELFTEST      0xaa
34 #define PS2_CMD_SELFTEST_PORT1      0xab
35
36 #define PS2_CMD_READ_CFG       0x20
37 #define PS2_CMD_WRITE_CFG       0x60
38
39 #define PS2_CFG_P1INT             0x1
40 #define PS2_CFG_P2INT             0x2
41 #define PS2_CFG_TRANSLATION       0x40
42
43 #define PS2_DELAY       1000
44
45 #define PS2_CMD_QUEUE_SIZE 8
46 #define PS2_KBD_RECV_BUFFER_SIZE 8
47
48 #define PS2_NO_ARG 0xff00
49
50
51 #define KBD_KEY_RELEASED 0x0
52 #define KBD_KEY_PRESSED 0x1
53 #define KBD_KEY_SCRLLKED 0x2
54 #define KBD_KEY_NUMBLKED 0x4
55 #define KBD_KEY_CAPSLKED 0x8
56
57 typedef unsigned char kbd_kstate_t;
58
59 struct kdb_keyinfo_pkt {
60     char key;
61     kbd_keycode code;
62     kbd_kstate_t state;
63     time_t timestamp;
64 };
65
66 struct ps2_cmd {
67     char cmd;
68     char arg;
69 };
70
71 struct ps2_kbd_state {
72     char state;
73     kbd_keycode* translation_table;
74     kbd_kstate_t key_state;
75 };
76
77 struct ps2_cmd_queue {
78     struct ps2_cmd cmd_queue[PS2_CMD_QUEUE_SIZE];
79     int queue_ptr;
80     int queue_len;
81 };
82
83 struct ps2_key_buffer {
84     struct kdb_keyinfo_pkt key_buff[PS2_KBD_RECV_BUFFER_SIZE];
85     int buffer_ptr;
86     // We don't bother whether the buff is full or not, just override.
87 };
88
89 /**
90  * @brief 向PS/2控制器发送指令并等待返回代码。注意,对于没有返回代码的命令请使用`ps2_post_cmd`,否则会造成死锁。
91  * 
92  * @param cmd 
93  * @param args
94  */
95 static uint8_t ps2_issue_cmd(char cmd, uint16_t arg);
96
97 /**
98  * @brief 向PS/2控制器发送指令,不等待返回代码。
99  * 
100  * @param cmd 
101  * @param args 
102  * @return char 
103  */
104 static void ps2_post_cmd(char cmd, uint16_t arg);
105
106 void ps2_device_post_cmd(char cmd, char arg);
107
108 void ps2_kbd_init();
109
110 void ps2_process_cmd(void* arg);
111
112
113
114
115 #endif /* __LUNAIX_PS2KBD_H */