PatchworkOS  28a9544
A non-POSIX operating system.
Loading...
Searching...
No Matches

IBM Personal Computer/2 ports. More...

Modules

 PS/2 Keyboard Driver
 PS/2 Keyboard Driver.
 
 PS/2 Mouse Driver
 PS/2 Mouse Driver.
 
 PS/2 Scanmap
 PS/2 Scanmap.
 

Data Structures

struct  ps2_device_info_t
 PS/2 device information structure. More...
 

Macros

#define PS2_WAIT_TIMEOUT   (CLOCKS_PER_SEC / 2)
 Wait timeout for PS/2 controller.
 
#define PS2_SMALL_DELAY   (CLOCKS_PER_SEC / 100)
 Small delay for various operations.
 
#define PS2_LARGE_DELAY   (CLOCKS_PER_SEC / 5)
 Large delay for various operations.
 
#define PS2_COMMAND_RETRIES   10
 Number of retries for commands.
 
#define PS2_READ(data)
 Read data from PS/2 controller.
 
#define PS2_WRITE(data)
 Write data to PS/2 controller.
 
#define PS2_CMD(command)
 Send a command to the PS/2 controller without reading response.
 
#define PS2_CMD_AND_READ(command, data)
 Send a command to the PS/2 controller and read response.
 
#define PS2_CMD_AND_WRITE(command, data)
 Send a command to the PS/2 controller and write data.
 
#define PS2_DEV_CMD(device, command)
 Send a command to a PS/2 device without reading response.
 
#define PS2_DEV_CMD_AND_READ(device, command, data)
 Send a command to a PS/2 device and read response.
 
#define PS2_DEV_SUB_CMD(device, command, subCommand)
 Send a command and a subcommand to a PS/2 device.
 

Enumerations

enum  ps2_port_t {
  PS2_PORT_DATA = 0x60 ,
  PS2_PORT_STATUS = 0x64 ,
  PS2_PORT_CMD = 0x64
}
 PS/2 controller I/O ports. More...
 
enum  ps2_cmd_t {
  PS2_CMD_CFG_READ = 0x20 ,
  PS2_CMD_CFG_WRITE = 0x60 ,
  PS2_CMD_SECOND_DISABLE = 0xA7 ,
  PS2_CMD_SECOND_ENABLE = 0xA8 ,
  PS2_CMD_FIRST_DISABLE = 0xAD ,
  PS2_CMD_FIRST_ENABLE = 0xAE ,
  PS2_CMD_SECOND_TEST = 0xA9 ,
  PS2_CMD_SELF_TEST = 0xAA ,
  PS2_CMD_FIRST_TEST = 0xAB ,
  PS2_CMD_DUMP = 0xAC ,
  PS2_CMD_SECOND_WRITE = 0xD4
}
 PS/2 controller commands. More...
 
enum  ps2_status_bits_t {
  PS2_STATUS_OUT_FULL = (1 << 0) ,
  PS2_STATUS_IN_FULL = (1 << 1) ,
  PS2_STATUS_SYSTEM_FLAG = (1 << 2) ,
  PS2_STATUS_CMD_DATA = (1 << 3) ,
  PS2_STATUS_TIMEOUT_ERROR = (1 << 6) ,
  PS2_STATUS_PARITY_ERROR = (1 << 7)
}
 PS/2 controller status register bits. More...
 
enum  ps2_config_bits_t {
  PS2_CFG_FIRST_IRQ = (1 << 0) ,
  PS2_CFG_SECOND_IRQ = (1 << 1) ,
  PS2_CFG_SYSTEM_FLAG = (1 << 2) ,
  PS2_CFG_RESERVED_3 = (1 << 3) ,
  PS2_CFG_FIRST_CLOCK_DISABLE = (1 << 4) ,
  PS2_CFG_SECOND_CLOCK_DISABLE = (1 << 5) ,
  PS2_CFG_FIRST_TRANSLATION = (1 << 6) ,
  PS2_CFG_RESERVED_7 = (1 << 7)
}
 PS/2 controller configuration bits. More...
 
enum  ps2_device_cmd_t {
  PS2_DEV_CMD_ECHO = 0xEE ,
  PS2_DEV_CMD_SET_LEDS = 0xED ,
  PS2_DEV_CMD_SET_SCANCODE_SET = 0xF0 ,
  PS2_DEV_CMD_IDENTIFY = 0xF2 ,
  PS2_DEV_CMD_SET_TYPEMATIC = 0xF3 ,
  PS2_DEV_CMD_ENABLE_SCANNING = 0xF4 ,
  PS2_DEV_CMD_DISABLE_SCANNING = 0xF5 ,
  PS2_DEV_CMD_SET_DEFAULTS = 0xF6 ,
  PS2_DEV_CMD_RESEND = 0xFE ,
  PS2_DEV_CMD_RESET = 0xFF
}
 PS/2 device commands. More...
 
enum  ps2_device_t {
  PS2_DEV_NONE = -1 ,
  PS2_DEV_FIRST = 0 ,
  PS2_DEV_SECOND = 1 ,
  PS2_DEV_COUNT = 2
}
 PS/2 device identifiers. More...
 
enum  ps2_device_type_t {
  PS2_DEV_TYPE_UNKNOWN ,
  PS2_DEV_TYPE_KEYBOARD ,
  PS2_DEV_TYPE_MOUSE_STANDARD ,
  PS2_DEV_TYPE_MOUSE_SCROLL ,
  PS2_DEV_TYPE_MOUSE_5BUTTON
}
 PS/2 device types. More...
 
enum  ps2_self_test_response_t {
  PS2_SELF_TEST_PASS = 0x55 ,
  PS2_SELF_TEST_FAIL = 0xFC
}
 PS/2 controller self-test responses. More...
 
enum  ps2_device_test_response_t {
  PS2_DEV_TEST_PASS = 0x00 ,
  PS2_DEV_TEST_CLOCK_STUCK_LOW = 0x01 ,
  PS2_DEV_TEST_CLOCK_STUCK_HIGH = 0x02 ,
  PS2_DEV_TEST_DATA_STUCK_LOW = 0x03 ,
  PS2_DEV_TEST_DATA_STUCK_HIGH = 0x04
}
 PS/2 device test responses. More...
 
enum  ps2_device_response_t {
  PS2_DEV_RESPONSE_ACK = 0xFA ,
  PS2_DEV_RESPONSE_RESEND = 0xFE ,
  PS2_DEV_RESPONSE_BAT_OK = 0xAA ,
  PS2_DEV_RESPONSE_KBD_EXTENDED = 0xE0 ,
  PS2_DEV_RESPONSE_KBD_RELEASE = 0xF0
}
 PS/2 device command responses. More...
 

Functions

void ps2_drain (void)
 Drain the PS/2 output buffer.
 
uint64_t ps2_wait_until_set (ps2_status_bits_t status)
 Wait until status bit(s) is set.
 
uint64_t ps2_wait_until_clear (ps2_status_bits_t status)
 Wait until status bit(s) is clear.
 
uint64_t ps2_send_cmd (ps2_cmd_t command)
 Send a command to the PS/2 controller.
 
uint64_t ps2_send_device_cmd (ps2_device_t device, ps2_device_cmd_t command)
 Send a command to a PS/2 device.
 

Detailed Description

IBM Personal Computer/2 ports.

See also
https://wiki.osdev.org/I8042_PS/2_Controller
https://www-ug.eecg.toronto.edu/msl/nios_devices/datasheets/PS2%20Keyboard%20Protocol.htm

Macro Definition Documentation

◆ PS2_CMD

#define PS2_CMD (   command)
Value:
({ \
uint64_t result = ps2_send_cmd(command); \
result; \
})
uint64_t ps2_send_cmd(ps2_cmd_t command)
Send a command to the PS/2 controller.
Definition ps2.c:471
__UINT64_TYPE__ uint64_t
Definition stdint.h:17

Send a command to the PS/2 controller without reading response.

Parameters
commandCommand to send.
Returns
On success, 0. On failure, ERR and errno is set to ETIMEOUT if timeout occurs.

Definition at line 221 of file ps2.h.

◆ PS2_CMD_AND_READ

#define PS2_CMD_AND_READ (   command,
  data 
)
Value:
({ \
uint64_t result = ps2_send_cmd(command); \
if (result != ERR) \
{ \
result = PS2_READ(data); \
} \
result; \
})
static fd_t data
Definition dwm.c:21
#define ERR
Integer error value.
Definition ERR.h:17
#define PS2_READ(data)
Read data from PS/2 controller.
Definition ps2.h:187

Send a command to the PS/2 controller and read response.

Parameters
commandCommand to send.
dataPointer to store the response byte
Returns
On success, 0. On failure, ERR and errno is set to ETIMEOUT if timeout occurs.

Definition at line 234 of file ps2.h.

◆ PS2_CMD_AND_WRITE

#define PS2_CMD_AND_WRITE (   command,
  data 
)
Value:
({ \
uint64_t result = ps2_send_cmd(command); \
if (result != ERR) \
{ \
result = PS2_WRITE(data); \
} \
result; \
})
#define PS2_WRITE(data)
Write data to PS/2 controller.
Definition ps2.h:205

Send a command to the PS/2 controller and write data.

Parameters
commandCommand to send.
dataData to write.
Returns
On success, 0. On failure, ERR and errno is set to ETIMEOUT if timeout occurs.

Definition at line 251 of file ps2.h.

◆ PS2_COMMAND_RETRIES

#define PS2_COMMAND_RETRIES   10

Number of retries for commands.

Definition at line 36 of file ps2.h.

◆ PS2_DEV_CMD

#define PS2_DEV_CMD (   device,
  command 
)
Value:
({ \
uint64_t result = ps2_send_device_cmd(device, command); \
result; \
})
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

Send a command to a PS/2 device without reading response.

Parameters
deviceDevice to send command to, specified by its port.
commandCommand to send.
Returns
On success, 0. On failure, ERR and errno is set to ETIMEOUT if timeout occurs.

Definition at line 268 of file ps2.h.

◆ PS2_DEV_CMD_AND_READ

#define PS2_DEV_CMD_AND_READ (   device,
  command,
  data 
)
Value:
({ \
uint64_t result = ps2_send_device_cmd(device, command); \
if (result != ERR) \
{ \
result = PS2_READ(data); \
} \
result; \
})

Send a command to a PS/2 device and read response.

Parameters
deviceDevice to send command to, specified by its port.
commandCommand to send.
dataPointer to store the response byte
Returns
On success, 0. On failure, ERR and errno is set to ETIMEOUT if timeout occurs.

Definition at line 282 of file ps2.h.

◆ PS2_DEV_SUB_CMD

#define PS2_DEV_SUB_CMD (   device,
  command,
  subCommand 
)
Value:
({ \
uint64_t result = PS2_DEV_CMD(device, command); \
if (result != ERR) \
{ \
result = PS2_DEV_CMD(device, subCommand); \
} \
result; \
})
#define PS2_DEV_CMD(device, command)
Send a command to a PS/2 device without reading response.
Definition ps2.h:268

Send a command and a subcommand to a PS/2 device.

Parameters
deviceDevice to send command to, specified by its port.
commandCommand to send.
subCommandSubcommand to send.
Returns
On success, 0. On failure, ERR and errno is set to ETIMEOUT if timeout occurs.

Definition at line 300 of file ps2.h.

◆ PS2_LARGE_DELAY

#define PS2_LARGE_DELAY   (CLOCKS_PER_SEC / 5)

Large delay for various operations.

Definition at line 31 of file ps2.h.

◆ PS2_READ

#define PS2_READ (   data)
Value:
({ \
if (result != ERR) \
{ \
} \
result; \
})
static uint8_t port_inb(uint16_t port)
Definition port.h:34
uint64_t ps2_wait_until_set(ps2_status_bits_t status)
Wait until status bit(s) is set.
Definition ps2.c:441
@ PS2_STATUS_OUT_FULL
Output buffer status (0 = empty, 1 = full)
Definition ps2.h:71
@ PS2_PORT_DATA
Definition ps2.h:43

Read data from PS/2 controller.

Waits for the output buffer to be full, then reads a byte from the data port.

Parameters
dataPointer to store the read byte.
Returns
On success, 0. On failure, ERR and errno is set to ETIMEOUT if timeout occurs.

Definition at line 187 of file ps2.h.

◆ PS2_SMALL_DELAY

#define PS2_SMALL_DELAY   (CLOCKS_PER_SEC / 100)

Small delay for various operations.

Definition at line 26 of file ps2.h.

◆ PS2_WAIT_TIMEOUT

#define PS2_WAIT_TIMEOUT   (CLOCKS_PER_SEC / 2)

Wait timeout for PS/2 controller.

Definition at line 21 of file ps2.h.

◆ PS2_WRITE

#define PS2_WRITE (   data)
Value:
({ \
if (result != ERR) \
{ \
port_outb(PS2_PORT_DATA, data); \
} \
result; \
})
uint64_t ps2_wait_until_clear(ps2_status_bits_t status)
Wait until status bit(s) is clear.
Definition ps2.c:456
@ PS2_STATUS_IN_FULL
Input buffer status (0 = empty, 1 = full)
Definition ps2.h:72

Write data to PS/2 controller.

Waits for the input buffer to be empty, then writes a byte to the data port.

Parameters
dataByte to write.
Returns
On success, 0. On failure, ERR and errno is set to ETIMEOUT if timeout occurs.

Definition at line 205 of file ps2.h.

Enumeration Type Documentation

◆ ps2_cmd_t

enum ps2_cmd_t

PS/2 controller commands.

Enumerator
PS2_CMD_CFG_READ 
PS2_CMD_CFG_WRITE 
PS2_CMD_SECOND_DISABLE 
PS2_CMD_SECOND_ENABLE 
PS2_CMD_FIRST_DISABLE 
PS2_CMD_FIRST_ENABLE 
PS2_CMD_SECOND_TEST 
PS2_CMD_SELF_TEST 
PS2_CMD_FIRST_TEST 
PS2_CMD_DUMP 
PS2_CMD_SECOND_WRITE 

Definition at line 51 of file ps2.h.

◆ ps2_config_bits_t

PS/2 controller configuration bits.

Enumerator
PS2_CFG_FIRST_IRQ 

First PS/2 port interrupt enable.

PS2_CFG_SECOND_IRQ 

Second PS/2 port interrupt enable.

PS2_CFG_SYSTEM_FLAG 

System flag (POST passed)

PS2_CFG_RESERVED_3 

Should be zero.

PS2_CFG_FIRST_CLOCK_DISABLE 

First PS/2 port clock disable.

PS2_CFG_SECOND_CLOCK_DISABLE 

Second PS/2 port clock disable.

PS2_CFG_FIRST_TRANSLATION 

First PS/2 port translation enable.

PS2_CFG_RESERVED_7 

Should be zero.

Definition at line 82 of file ps2.h.

◆ ps2_device_cmd_t

PS/2 device commands.

Enumerator
PS2_DEV_CMD_ECHO 
PS2_DEV_CMD_SET_LEDS 
PS2_DEV_CMD_SET_SCANCODE_SET 
PS2_DEV_CMD_IDENTIFY 
PS2_DEV_CMD_SET_TYPEMATIC 
PS2_DEV_CMD_ENABLE_SCANNING 
PS2_DEV_CMD_DISABLE_SCANNING 
PS2_DEV_CMD_SET_DEFAULTS 
PS2_DEV_CMD_RESEND 
PS2_DEV_CMD_RESET 

Definition at line 97 of file ps2.h.

◆ ps2_device_response_t

PS/2 device command responses.

Enumerator
PS2_DEV_RESPONSE_ACK 
PS2_DEV_RESPONSE_RESEND 
PS2_DEV_RESPONSE_BAT_OK 
PS2_DEV_RESPONSE_KBD_EXTENDED 

Indicates that the following byte is an extended scancode.

PS2_DEV_RESPONSE_KBD_RELEASE 

Indicates that the following byte is a key release code.

Definition at line 170 of file ps2.h.

◆ ps2_device_t

PS/2 device identifiers.

Enumerator
PS2_DEV_NONE 

No device.

PS2_DEV_FIRST 

First PS/2 port.

PS2_DEV_SECOND 

Second PS/2 port.

PS2_DEV_COUNT 

Total number of ports.

Definition at line 114 of file ps2.h.

◆ ps2_device_test_response_t

PS/2 device test responses.

Enumerator
PS2_DEV_TEST_PASS 
PS2_DEV_TEST_CLOCK_STUCK_LOW 
PS2_DEV_TEST_CLOCK_STUCK_HIGH 
PS2_DEV_TEST_DATA_STUCK_LOW 
PS2_DEV_TEST_DATA_STUCK_HIGH 

Definition at line 158 of file ps2.h.

◆ ps2_device_type_t

PS/2 device types.

Enumerator
PS2_DEV_TYPE_UNKNOWN 
PS2_DEV_TYPE_KEYBOARD 
PS2_DEV_TYPE_MOUSE_STANDARD 
PS2_DEV_TYPE_MOUSE_SCROLL 
PS2_DEV_TYPE_MOUSE_5BUTTON 

Definition at line 125 of file ps2.h.

◆ ps2_port_t

enum ps2_port_t

PS/2 controller I/O ports.

Enumerator
PS2_PORT_DATA 
PS2_PORT_STATUS 
PS2_PORT_CMD 

Definition at line 41 of file ps2.h.

◆ ps2_self_test_response_t

PS/2 controller self-test responses.

Enumerator
PS2_SELF_TEST_PASS 
PS2_SELF_TEST_FAIL 

Definition at line 149 of file ps2.h.

◆ ps2_status_bits_t

PS/2 controller status register bits.

Enumerator
PS2_STATUS_OUT_FULL 

Output buffer status (0 = empty, 1 = full)

PS2_STATUS_IN_FULL 

Input buffer status (0 = empty, 1 = full)

PS2_STATUS_SYSTEM_FLAG 
PS2_STATUS_CMD_DATA 

Command(1) or Data(0)

PS2_STATUS_TIMEOUT_ERROR 
PS2_STATUS_PARITY_ERROR 

Definition at line 69 of file ps2.h.

Function Documentation

◆ ps2_drain()

void ps2_drain ( void  )

Drain the PS/2 output buffer.

Reads and discards any data in the PS/2 output buffer.

Definition at line 431 of file ps2.c.

References port_inb(), PS2_PORT_DATA, PS2_PORT_STATUS, PS2_SMALL_DELAY, PS2_STATUS_OUT_FULL, and sys_time_wait().

Referenced by ps2_device_init(), and ps2_init().

◆ ps2_send_cmd()

uint64_t ps2_send_cmd ( ps2_cmd_t  command)

Send a command to the PS/2 controller.

Parameters
commandCommand to send.
Returns
On success, 0. On failure, ERR and errno is set to ETIMEOUT if timeout occurs.

Definition at line 471 of file ps2.c.

References ERR, port_outb(), PS2_PORT_CMD, PS2_STATUS_IN_FULL, and ps2_wait_until_clear().

Referenced by ps2_check_if_dual_channel(), ps2_init(), and ps2_send_device_cmd().

◆ ps2_send_device_cmd()

uint64_t ps2_send_device_cmd ( ps2_device_t  device,
ps2_device_cmd_t  command 
)

Send a command to a PS/2 device.

Parameters
deviceDevice to send command to, specified by its port.
commandCommand to send.
Returns
On success, 0. On failure, ERR and errno is set to ETIMEOUT if timeout occurs.

Definition at line 481 of file ps2.c.

References ERR, LOG_ERR, LOG_WARN, PS2_CMD_SECOND_WRITE, PS2_COMMAND_RETRIES, PS2_DEV_RESPONSE_ACK, PS2_DEV_RESPONSE_RESEND, PS2_DEV_SECOND, ps2_device_to_string(), PS2_READ, ps2_send_cmd(), and PS2_WRITE.

◆ ps2_wait_until_clear()

uint64_t ps2_wait_until_clear ( ps2_status_bits_t  status)

Wait until status bit(s) is clear.

Parameters
statusStatus bit(s) to wait for.
Returns
On success, 0. On failure, ERR and errno is set to ETIMEOUT if timeout occurs.

Definition at line 456 of file ps2.c.

References ERR, errno, ETIMEDOUT, port_inb(), PS2_PORT_STATUS, PS2_WAIT_TIMEOUT, startTime, and sys_time_uptime().

Referenced by ps2_send_cmd().

◆ ps2_wait_until_set()

uint64_t ps2_wait_until_set ( ps2_status_bits_t  status)

Wait until status bit(s) is set.

Parameters
statusStatus bit(s) to wait for.
Returns
On success, 0. On failure, ERR and errno is set to ETIMEOUT if timeout occurs.

Definition at line 441 of file ps2.c.

References ERR, errno, ETIMEDOUT, port_inb(), PS2_PORT_STATUS, PS2_WAIT_TIMEOUT, startTime, and sys_time_uptime().