PatchworkOS  28a9544
A non-POSIX operating system.
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
21#define PS2_WAIT_TIMEOUT (CLOCKS_PER_SEC / 2)
22
26#define PS2_SMALL_DELAY (CLOCKS_PER_SEC / 100)
27
31#define PS2_LARGE_DELAY (CLOCKS_PER_SEC / 5)
32
36#define PS2_COMMAND_RETRIES 10
37
41typedef enum
42{
45 PS2_PORT_CMD = 0x64
47
65
78
93
110
121
133
145
154
166
178
187#define PS2_READ(data) \
188 ({ \
189 uint64_t result = ps2_wait_until_set(PS2_STATUS_OUT_FULL); \
190 if (result != ERR) \
191 { \
192 *(data) = port_inb(PS2_PORT_DATA); \
193 } \
194 result; \
195 })
196
205#define PS2_WRITE(data) \
206 ({ \
207 uint64_t result = ps2_wait_until_clear(PS2_STATUS_IN_FULL); \
208 if (result != ERR) \
209 { \
210 port_outb(PS2_PORT_DATA, data); \
211 } \
212 result; \
213 })
214
221#define PS2_CMD(command) \
222 ({ \
223 uint64_t result = ps2_send_cmd(command); \
224 result; \
225 })
226
234#define PS2_CMD_AND_READ(command, data) \
235 ({ \
236 uint64_t result = ps2_send_cmd(command); \
237 if (result != ERR) \
238 { \
239 result = PS2_READ(data); \
240 } \
241 result; \
242 })
243
251#define PS2_CMD_AND_WRITE(command, data) \
252 ({ \
253 uint64_t result = ps2_send_cmd(command); \
254 if (result != ERR) \
255 { \
256 result = PS2_WRITE(data); \
257 } \
258 result; \
259 })
260
268#define PS2_DEV_CMD(device, command) \
269 ({ \
270 uint64_t result = ps2_send_device_cmd(device, command); \
271 result; \
272 })
273
282#define PS2_DEV_CMD_AND_READ(device, command, data) \
283 ({ \
284 uint64_t result = ps2_send_device_cmd(device, command); \
285 if (result != ERR) \
286 { \
287 result = PS2_READ(data); \
288 } \
289 result; \
290 })
291
300#define PS2_DEV_SUB_CMD(device, command, subCommand) \
301 ({ \
302 uint64_t result = PS2_DEV_CMD(device, command); \
303 if (result != ERR) \
304 { \
305 result = PS2_DEV_CMD(device, subCommand); \
306 } \
307 result; \
308 })
309
315void ps2_drain(void);
316
324
332
340
349
ps2_device_t
PS/2 device identifiers.
Definition ps2.h:115
ps2_status_bits_t
PS/2 controller status register bits.
Definition ps2.h:70
ps2_device_test_response_t
PS/2 device test responses.
Definition ps2.h:159
ps2_cmd_t
PS/2 controller commands.
Definition ps2.h:52
ps2_config_bits_t
PS/2 controller configuration bits.
Definition ps2.h:83
uint64_t ps2_wait_until_set(ps2_status_bits_t status)
Wait until status bit(s) is set.
Definition ps2.c:441
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:481
ps2_self_test_response_t
PS/2 controller self-test responses.
Definition ps2.h:150
ps2_device_type_t
PS/2 device types.
Definition ps2.h:126
uint64_t ps2_send_cmd(ps2_cmd_t command)
Send a command to the PS/2 controller.
Definition ps2.c:471
void ps2_drain(void)
Drain the PS/2 output buffer.
Definition ps2.c:431
ps2_port_t
PS/2 controller I/O ports.
Definition ps2.h:42
uint64_t ps2_wait_until_clear(ps2_status_bits_t status)
Wait until status bit(s) is clear.
Definition ps2.c:456
ps2_device_cmd_t
PS/2 device commands.
Definition ps2.h:98
ps2_device_response_t
PS/2 device command responses.
Definition ps2.h:171
@ PS2_DEV_COUNT
Total number of ports.
Definition ps2.h:119
@ PS2_DEV_NONE
No device.
Definition ps2.h:116
@ PS2_DEV_SECOND
Second PS/2 port.
Definition ps2.h:118
@ PS2_DEV_FIRST
First PS/2 port.
Definition ps2.h:117
@ PS2_STATUS_SYSTEM_FLAG
Definition ps2.h:73
@ PS2_STATUS_TIMEOUT_ERROR
Definition ps2.h:75
@ PS2_STATUS_PARITY_ERROR
Definition ps2.h:76
@ PS2_STATUS_IN_FULL
Input buffer status (0 = empty, 1 = full)
Definition ps2.h:72
@ PS2_STATUS_OUT_FULL
Output buffer status (0 = empty, 1 = full)
Definition ps2.h:71
@ PS2_STATUS_CMD_DATA
Command(1) or Data(0)
Definition ps2.h:74
@ PS2_DEV_TEST_DATA_STUCK_LOW
Definition ps2.h:163
@ PS2_DEV_TEST_PASS
Definition ps2.h:160
@ PS2_DEV_TEST_DATA_STUCK_HIGH
Definition ps2.h:164
@ PS2_DEV_TEST_CLOCK_STUCK_HIGH
Definition ps2.h:162
@ PS2_DEV_TEST_CLOCK_STUCK_LOW
Definition ps2.h:161
@ PS2_CMD_SECOND_TEST
Definition ps2.h:59
@ PS2_CMD_DUMP
Definition ps2.h:62
@ PS2_CMD_SECOND_WRITE
Definition ps2.h:63
@ PS2_CMD_SELF_TEST
Definition ps2.h:60
@ PS2_CMD_FIRST_ENABLE
Definition ps2.h:58
@ PS2_CMD_FIRST_DISABLE
Definition ps2.h:57
@ PS2_CMD_CFG_READ
Definition ps2.h:53
@ PS2_CMD_SECOND_ENABLE
Definition ps2.h:56
@ PS2_CMD_SECOND_DISABLE
Definition ps2.h:55
@ PS2_CMD_FIRST_TEST
Definition ps2.h:61
@ PS2_CMD_CFG_WRITE
Definition ps2.h:54
@ PS2_CFG_SECOND_IRQ
Second PS/2 port interrupt enable.
Definition ps2.h:85
@ PS2_CFG_FIRST_IRQ
First PS/2 port interrupt enable.
Definition ps2.h:84
@ PS2_CFG_RESERVED_7
Should be zero.
Definition ps2.h:91
@ PS2_CFG_SYSTEM_FLAG
System flag (POST passed)
Definition ps2.h:86
@ PS2_CFG_RESERVED_3
Should be zero.
Definition ps2.h:87
@ PS2_CFG_SECOND_CLOCK_DISABLE
Second PS/2 port clock disable.
Definition ps2.h:89
@ PS2_CFG_FIRST_CLOCK_DISABLE
First PS/2 port clock disable.
Definition ps2.h:88
@ PS2_CFG_FIRST_TRANSLATION
First PS/2 port translation enable.
Definition ps2.h:90
@ PS2_SELF_TEST_FAIL
Definition ps2.h:152
@ PS2_SELF_TEST_PASS
Definition ps2.h:151
@ PS2_DEV_TYPE_MOUSE_5BUTTON
Definition ps2.h:131
@ PS2_DEV_TYPE_KEYBOARD
Definition ps2.h:128
@ PS2_DEV_TYPE_MOUSE_STANDARD
Definition ps2.h:129
@ PS2_DEV_TYPE_MOUSE_SCROLL
Definition ps2.h:130
@ PS2_DEV_TYPE_UNKNOWN
Definition ps2.h:127
@ PS2_PORT_STATUS
Definition ps2.h:44
@ PS2_PORT_CMD
Definition ps2.h:45
@ PS2_PORT_DATA
Definition ps2.h:43
@ PS2_DEV_CMD_SET_DEFAULTS
Definition ps2.h:106
@ PS2_DEV_CMD_RESET
Definition ps2.h:108
@ PS2_DEV_CMD_IDENTIFY
Definition ps2.h:102
@ PS2_DEV_CMD_DISABLE_SCANNING
Definition ps2.h:105
@ PS2_DEV_CMD_ECHO
Definition ps2.h:99
@ PS2_DEV_CMD_SET_TYPEMATIC
Definition ps2.h:103
@ PS2_DEV_CMD_ENABLE_SCANNING
Definition ps2.h:104
@ PS2_DEV_CMD_SET_LEDS
Definition ps2.h:100
@ PS2_DEV_CMD_SET_SCANCODE_SET
Definition ps2.h:101
@ PS2_DEV_CMD_RESEND
Definition ps2.h:107
@ PS2_DEV_RESPONSE_RESEND
Definition ps2.h:173
@ PS2_DEV_RESPONSE_ACK
Definition ps2.h:172
@ PS2_DEV_RESPONSE_BAT_OK
Definition ps2.h:174
@ PS2_DEV_RESPONSE_KBD_EXTENDED
Indicates that the following byte is an extended scancode.
Definition ps2.h:175
@ PS2_DEV_RESPONSE_KBD_RELEASE
Indicates that the following byte is a key release code.
Definition ps2.h:176
__UINT64_TYPE__ uint64_t
Definition stdint.h:17
__UINT8_TYPE__ uint8_t
Definition stdint.h:11
PS/2 device information structure.
Definition ps2.h:138
ps2_device_t device
Definition ps2.h:139
ps2_device_type_t type
Definition ps2.h:142
uint8_t firstIdByte
Definition ps2.h:140
const char * name
Definition ps2.h:141