PatchworkOS
Loading...
Searching...
No Matches
ps2.h
Go to the documentation of this file.
1#pragma once
2
3#include <kernel/cpu/port.h>
4
5#include <stdbool.h>
6#include <stdint.h>
7
32#define PS2_WAIT_TIMEOUT (CLOCKS_PER_SEC / 2)
33
37#define PS2_SMALL_DELAY (CLOCKS_PER_SEC / 100)
38
42#define PS2_LARGE_DELAY (CLOCKS_PER_SEC / 5)
43
47#define PS2_COMMAND_RETRIES 3
48
52typedef enum
53{
56 PS2_PORT_CMD = 0x64
58
76
89
104
121
132
144
148typedef struct
149{
150 ps2_device_t device;
151 uint8_t firstIdByte;
152 const char* name;
154 bool active;
156
165
177
189
198#define PS2_READ(data) \
199 ({ \
200 uint64_t result = ps2_wait_until_set(PS2_STATUS_OUT_FULL); \
201 if (result != ERR) \
202 { \
203 *(data) = port_inb(PS2_PORT_DATA); \
204 } \
205 result; \
206 })
207
216#define PS2_WRITE(data) \
217 ({ \
218 uint64_t result = ps2_wait_until_clear(PS2_STATUS_IN_FULL); \
219 if (result != ERR) \
220 { \
221 port_outb(PS2_PORT_DATA, data); \
222 } \
223 result; \
224 })
225
232#define PS2_CMD(command) \
233 ({ \
234 uint64_t result = ps2_send_cmd(command); \
235 result; \
236 })
237
245#define PS2_CMD_AND_READ(command, data) \
246 ({ \
247 uint64_t result = ps2_send_cmd(command); \
248 if (result != ERR) \
249 { \
250 result = PS2_READ(data); \
251 } \
252 result; \
253 })
254
262#define PS2_CMD_AND_WRITE(command, data) \
263 ({ \
264 uint64_t result = ps2_send_cmd(command); \
265 if (result != ERR) \
266 { \
267 result = PS2_WRITE(data); \
268 } \
269 result; \
270 })
271
279#define PS2_DEV_CMD(device, command) \
280 ({ \
281 uint64_t result = ps2_send_device_cmd(device, command); \
282 result; \
283 })
284
293#define PS2_DEV_CMD_AND_READ(device, command, data) \
294 ({ \
295 uint64_t result = ps2_send_device_cmd(device, command); \
296 if (result != ERR) \
297 { \
298 result = PS2_READ(data); \
299 } \
300 result; \
301 })
302
311#define PS2_DEV_SUB_CMD(device, command, subCommand) \
312 ({ \
313 uint64_t result = PS2_DEV_CMD(device, command); \
314 if (result != ERR) \
315 { \
316 result = PS2_DEV_CMD(device, subCommand); \
317 } \
318 result; \
319 })
320
327void ps2_init(void);
328
334void ps2_drain(void);
335
343
351
359
368
ps2_device_t
PS/2 device identifiers.
Definition ps2.h:126
ps2_status_bits_t
PS/2 controller status register bits.
Definition ps2.h:81
ps2_device_test_response_t
PS/2 device test responses.
Definition ps2.h:170
ps2_cmd_t
PS/2 controller commands.
Definition ps2.h:63
ps2_config_bits_t
PS/2 controller configuration bits.
Definition ps2.h:94
uint64_t ps2_wait_until_set(ps2_status_bits_t status)
Wait until status bit(s) is set.
Definition ps2.c:422
uint64_t ps2_send_device_cmd(ps2_device_t device, ps2_device_cmd_t command)
Send a command to a PS/2 device.
Definition ps2.c:462
ps2_self_test_response_t
PS/2 controller self-test responses.
Definition ps2.h:161
ps2_device_type_t
PS/2 device types.
Definition ps2.h:137
uint64_t ps2_send_cmd(ps2_cmd_t command)
Send a command to the PS/2 controller.
Definition ps2.c:452
void ps2_drain(void)
Drain the PS/2 output buffer.
Definition ps2.c:412
ps2_port_t
PS/2 controller I/O ports.
Definition ps2.h:53
uint64_t ps2_wait_until_clear(ps2_status_bits_t status)
Wait until status bit(s) is clear.
Definition ps2.c:437
ps2_device_cmd_t
PS/2 device commands.
Definition ps2.h:109
ps2_device_response_t
PS/2 device command responses.
Definition ps2.h:182
void ps2_init(void)
Initialize the PS/2 controller.
Definition ps2.c:324
@ PS2_DEV_COUNT
Total number of ports.
Definition ps2.h:130
@ PS2_DEV_NONE
No device.
Definition ps2.h:127
@ PS2_DEV_SECOND
Second PS/2 port.
Definition ps2.h:129
@ PS2_DEV_FIRST
First PS/2 port.
Definition ps2.h:128
@ PS2_STATUS_SYSTEM_FLAG
Definition ps2.h:84
@ PS2_STATUS_TIMEOUT_ERROR
Definition ps2.h:86
@ PS2_STATUS_PARITY_ERROR
Definition ps2.h:87
@ PS2_STATUS_IN_FULL
Input buffer status (0 = empty, 1 = full)
Definition ps2.h:83
@ PS2_STATUS_OUT_FULL
Output buffer status (0 = empty, 1 = full)
Definition ps2.h:82
@ PS2_STATUS_CMD_DATA
Command(1) or Data(0)
Definition ps2.h:85
@ PS2_DEV_TEST_DATA_STUCK_LOW
Definition ps2.h:174
@ PS2_DEV_TEST_PASS
Definition ps2.h:171
@ PS2_DEV_TEST_DATA_STUCK_HIGH
Definition ps2.h:175
@ PS2_DEV_TEST_CLOCK_STUCK_HIGH
Definition ps2.h:173
@ PS2_DEV_TEST_CLOCK_STUCK_LOW
Definition ps2.h:172
@ PS2_CMD_SECOND_TEST
Definition ps2.h:70
@ PS2_CMD_DUMP
Definition ps2.h:73
@ PS2_CMD_SECOND_WRITE
Definition ps2.h:74
@ PS2_CMD_SELF_TEST
Definition ps2.h:71
@ PS2_CMD_FIRST_ENABLE
Definition ps2.h:69
@ PS2_CMD_FIRST_DISABLE
Definition ps2.h:68
@ PS2_CMD_CFG_READ
Definition ps2.h:64
@ PS2_CMD_SECOND_ENABLE
Definition ps2.h:67
@ PS2_CMD_SECOND_DISABLE
Definition ps2.h:66
@ PS2_CMD_FIRST_TEST
Definition ps2.h:72
@ PS2_CMD_CFG_WRITE
Definition ps2.h:65
@ PS2_CFG_SECOND_IRQ
Second PS/2 port interrupt enable.
Definition ps2.h:96
@ PS2_CFG_FIRST_IRQ
First PS/2 port interrupt enable.
Definition ps2.h:95
@ PS2_CFG_RESERVED_7
Should be zero.
Definition ps2.h:102
@ PS2_CFG_SYSTEM_FLAG
System flag (POST passed)
Definition ps2.h:97
@ PS2_CFG_RESERVED_3
Should be zero.
Definition ps2.h:98
@ PS2_CFG_SECOND_CLOCK_DISABLE
Second PS/2 port clock disable.
Definition ps2.h:100
@ PS2_CFG_FIRST_CLOCK_DISABLE
First PS/2 port clock disable.
Definition ps2.h:99
@ PS2_CFG_FIRST_TRANSLATION
First PS/2 port translation enable.
Definition ps2.h:101
@ PS2_SELF_TEST_FAIL
Definition ps2.h:163
@ PS2_SELF_TEST_PASS
Definition ps2.h:162
@ PS2_DEV_TYPE_MOUSE_5BUTTON
Definition ps2.h:142
@ PS2_DEV_TYPE_KEYBOARD
Definition ps2.h:139
@ PS2_DEV_TYPE_MOUSE_STANDARD
Definition ps2.h:140
@ PS2_DEV_TYPE_MOUSE_SCROLL
Definition ps2.h:141
@ PS2_DEV_TYPE_UNKNOWN
Definition ps2.h:138
@ PS2_PORT_STATUS
Definition ps2.h:55
@ PS2_PORT_CMD
Definition ps2.h:56
@ PS2_PORT_DATA
Definition ps2.h:54
@ PS2_DEV_CMD_SET_DEFAULTS
Definition ps2.h:117
@ PS2_DEV_CMD_RESET
Definition ps2.h:119
@ PS2_DEV_CMD_IDENTIFY
Definition ps2.h:113
@ PS2_DEV_CMD_DISABLE_SCANNING
Definition ps2.h:116
@ PS2_DEV_CMD_ECHO
Definition ps2.h:110
@ PS2_DEV_CMD_SET_TYPEMATIC
Definition ps2.h:114
@ PS2_DEV_CMD_ENABLE_SCANNING
Definition ps2.h:115
@ PS2_DEV_CMD_SET_LEDS
Definition ps2.h:111
@ PS2_DEV_CMD_SET_SCANCODE_SET
Definition ps2.h:112
@ PS2_DEV_CMD_RESEND
Definition ps2.h:118
@ PS2_DEV_RESPONSE_RESEND
Definition ps2.h:184
@ PS2_DEV_RESPONSE_ACK
Definition ps2.h:183
@ PS2_DEV_RESPONSE_BAT_OK
Definition ps2.h:185
@ PS2_DEV_RESPONSE_KBD_EXTENDED
Indicates that the following byte is an extended scancode.
Definition ps2.h:186
@ PS2_DEV_RESPONSE_KBD_RELEASE
Indicates that the following byte is a key release code.
Definition ps2.h:187
__UINT64_TYPE__ uint64_t
Definition stdint.h:17
__UINT8_TYPE__ uint8_t
Definition stdint.h:11
PS/2 device information structure.
Definition ps2.h:149