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

IBM Personal Computer/2 ports. More...

Collaboration diagram for PS/2:

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

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_known_device_t
 Known PS/2 device structure. More...
 
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_KEYBOARD_PNP_IDS
 All known PS/2 keyboard PNP IDs.
 
#define PS2_MOUSE_PNP_IDS
 All known PS/2 mouse PNP IDs.
 

Enumerations

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_NONE , 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) are set.
 
uint64_t ps2_wait_until_clear (ps2_status_bits_t status)
 Wait until status bit(s) are clear.
 
uint64_t ps2_read (void)
 Read from the PS/2 controllers data port.
 
uint64_t ps2_read_no_wait (void)
 Read from the PS/2 controllers data port without waiting.
 
uint64_t ps2_write (uint8_t data)
 Write to the PS/2 controllers data port.
 
uint64_t ps2_cmd (ps2_cmd_t command)
 Send a command to the PS/2 controller without reading response.
 
uint64_t ps2_cmd_and_read (ps2_cmd_t command)
 Send a command to the PS/2 controller and read response.
 
uint64_t ps2_cmd_and_write (ps2_cmd_t command, uint8_t data)
 Send a command to the PS/2 controller and write data.
 
uint64_t ps2_device_cmd (ps2_device_t device, ps2_device_cmd_t command)
 Send a command to a PS/2 device without reading response.
 
uint64_t ps2_device_cmd_and_read (ps2_device_t device, ps2_device_cmd_t command)
 Send a command to a PS/2 device and read response.
 
uint64_t ps2_device_sub_cmd (ps2_device_t device, ps2_device_cmd_t command, uint8_t subCommand)
 Send a command and a subcommand to a PS/2 device.
 

Macro Definition Documentation

◆ PS2_WAIT_TIMEOUT

#define PS2_WAIT_TIMEOUT   (CLOCKS_PER_SEC / 2)

Wait timeout for PS/2 controller.

Definition at line 22 of file ps2.h.

◆ PS2_SMALL_DELAY

#define PS2_SMALL_DELAY   (CLOCKS_PER_SEC / 100)

Small delay for various operations.

Definition at line 27 of file ps2.h.

◆ PS2_LARGE_DELAY

#define PS2_LARGE_DELAY   (CLOCKS_PER_SEC / 5)

Large delay for various operations.

Definition at line 32 of file ps2.h.

◆ PS2_COMMAND_RETRIES

#define PS2_COMMAND_RETRIES   10

Number of retries for commands.

Definition at line 37 of file ps2.h.

◆ PS2_KEYBOARD_PNP_IDS

#define PS2_KEYBOARD_PNP_IDS
Value:
"PNP0300;PNP0301;PNP0302;PNP0303;PNP0304;PNP0305;PNP0306;PNP0307;PNP0308;PNP0309;PNP030A;PNP030B;PNP0320;PNP0321;" \
"PNP0322;PNP0323;PNP0324;PNP0325;PNP0326;PNP0327;PNP0340;PNP0341;PNP0342;PNP0343;PNP0344"

All known PS/2 keyboard PNP IDs.

See also
https://uefi.org/sites/default/files/resources/devids%20%285%29.txt

Definition at line 44 of file ps2.h.

◆ PS2_MOUSE_PNP_IDS

#define PS2_MOUSE_PNP_IDS
Value:
"PNP0F00;PNP0F01;PNP0F02;PNP0F03;PNP0F04;PNP0F05;PNP0F06;PNP0F07;PNP0F08;PNP0F09;PNP0F0A;PNP0F0B;PNP0F0C;PNP0F0D;" \
"PNP0F0E;PNP0F0F;PNP0F10;PNP0F11;PNP0F12;PNP0F13;PNP0F14;PNP0F15;PNP0F16;PNP0F17;PNP0F18;PNP0F19;PNP0F1A;PNP0F1B;" \
"PNP0F1C;PNP0F1D;PNP0F1E;PNP0F1F;PNP0F20;PNP0F21;PNP0F22;PNP0F23;PNP0FFC;PNP0FFF"

All known PS/2 mouse PNP IDs.

See also
https://uefi.org/sites/default/files/resources/devids%20%285%29.txt

Definition at line 53 of file ps2.h.

Enumeration Type Documentation

◆ 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 62 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 81 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 96 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 112 of file ps2.h.

◆ ps2_device_t

PS/2 device identifiers.

Enumerator
PS2_DEV_NONE 
PS2_DEV_FIRST 
PS2_DEV_SECOND 
PS2_DEV_COUNT 

Definition at line 130 of file ps2.h.

◆ ps2_device_type_t

PS/2 device types.

Enumerator
PS2_DEV_TYPE_NONE 
PS2_DEV_TYPE_KEYBOARD 
PS2_DEV_TYPE_MOUSE_STANDARD 
PS2_DEV_TYPE_MOUSE_SCROLL 
PS2_DEV_TYPE_MOUSE_5BUTTON 

Definition at line 142 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 155 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 165 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 178 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 325 of file ps2.c.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ ps2_wait_until_set()

uint64_t ps2_wait_until_set ( ps2_status_bits_t  status)

Wait until status bit(s) are set.

Parameters
statusStatus bit(s) to wait for.
Returns
On success, 0. On timeout, ERR.

Definition at line 335 of file ps2.c.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ ps2_wait_until_clear()

uint64_t ps2_wait_until_clear ( ps2_status_bits_t  status)

Wait until status bit(s) are clear.

Parameters
statusStatus bit(s) to wait for.
Returns
On success, 0. On timeout, ERR.

Definition at line 350 of file ps2.c.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ ps2_read()

uint64_t ps2_read ( void  )

Read from the PS/2 controllers data port.

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

Returns
On success, the response byte. On failure, ERR and errno is set to:
  • ETIMEOUT: Timeout occurred while waiting for data.

Definition at line 365 of file ps2.c.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ ps2_read_no_wait()

uint64_t ps2_read_no_wait ( void  )

Read from the PS/2 controllers data port without waiting.

Returns
On success, the response byte. On failure, ERR and errno is set to:
  • EAGAIN: No data available to read.

Definition at line 375 of file ps2.c.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ ps2_write()

uint64_t ps2_write ( uint8_t  data)

Write to the PS/2 controllers data port.

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: Timeout occurred while waiting to write.

Definition at line 386 of file ps2.c.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ ps2_cmd()

uint64_t ps2_cmd ( ps2_cmd_t  command)

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: Timeout occurred while waiting to send command.

Definition at line 397 of file ps2.c.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ ps2_cmd_and_read()

uint64_t ps2_cmd_and_read ( ps2_cmd_t  command)

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

Parameters
commandCommand to send.
Returns
On success, the response byte. On failure, ERR and errno is set to:
  • ETIMEOUT: Timeout occurred while waiting to send command or read data.

Definition at line 408 of file ps2.c.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ ps2_cmd_and_write()

uint64_t ps2_cmd_and_write ( ps2_cmd_t  command,
uint8_t  data 
)

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: Timeout occurred while waiting to send command or write data.

Definition at line 417 of file ps2.c.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ ps2_device_cmd()

uint64_t ps2_device_cmd ( ps2_device_t  device,
ps2_device_cmd_t  command 
)

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

Parameters
deviceDevice to send command to.
commandCommand to send.
Returns
On success, 0. On failure, ERR and errno is set to:
  • ETIMEOUT: Timeout occurred while waiting to send command.

Definition at line 488 of file ps2.c.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ ps2_device_cmd_and_read()

uint64_t ps2_device_cmd_and_read ( ps2_device_t  device,
ps2_device_cmd_t  command 
)

Send a command to a PS/2 device and read 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: Timeout occurred while waiting to send command or read data.

Definition at line 530 of file ps2.c.

Here is the call graph for this function:

◆ ps2_device_sub_cmd()

uint64_t ps2_device_sub_cmd ( ps2_device_t  device,
ps2_device_cmd_t  command,
uint8_t  subCommand 
)

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: Timeout occurred while waiting to send command or subcommand.

Definition at line 539 of file ps2.c.

Here is the call graph for this function:
Here is the caller graph for this function: