AquilaOS documentation
How it works, and its code
Loading...
Searching...
No Matches
i8042.c File Reference

The driver for the i8042, the PC's keyboard controller. More...

#include <core/system.h>
#include <core/string.h>
#include <core/kargs.h>
#include <core/module.h>
#include <cpu/io.h>
#include <dev/irq.h>
#include <dev/platform.h>
#include <dev/i8042.h>
#include <aquila/errno.h>

Go to the source code of this file.

Macros

#define DATA_PORT   0x00
#define STATUS_PORT   0x04
#define COMMAND_PORT   0x04
#define STATUS_OBUF_FULL   0x01
#define STATUS_IBUF_FULL   0x02
#define STATUS_SYSTEM   0x04
#define STATUS_COMMAND   0x08
#define STATUS_TOUT   0x40
#define STATUS_PARITY   0x80
#define CMD_READ_CONFIG   0x20
#define CMD_WRITE_CONFIG   0x60
#define CMD_AUX_ENABLE   0xA8
#define CONFIG_AUX_IRQ   0x02

Functions

static uint8_t read_status (void)
 Returns the controller's status register.
static int has_byte (void)
 Returns whether the controller has a byte for us (its output buffer is full).
static void i8042_irq (void *arg)
 Handles a port's interrupt: its byte goes to the port's handler.
void i8042_handler_register (int port, void(*fun)(int))
 Registers the function that gets each byte a port receives.
static int write_wait (void)
 Waits until the controller can take a byte.
int i8042_aux_write (uint8_t byte)
 Sends a byte to the device on the second port (the mouse).
void i8042_reboot (void)
 Resets the CPU, through the controller's output line: this reboots the PC.
static int read_wait (void)
 Waits for the controller's reply to a command.
static int aux_enable (void)
 Turns the second port on, and its interrupt (a bit of the controller's configuration byte).
static int i8042_init (void)
 Finds the platform's i8042, turns the mouse's port on, and installs the ports' interrupt handlers.

Variables

static struct ioaddr i8042_ioaddr
static int i8042_found
static void(* port_handler [2])(int)

Detailed Description

The driver for the i8042, the PC's keyboard controller.

The controller has two PS/2 ports, the keyboard's (the first) and the mouse's (the second, "aux"), each with its own interrupt. The bytes each port receives go to the handler that the port's driver registered (dev/kbd/ps2kbd.c, dev/mouse/ps2mouse.c). The controller can also reset the CPU, which is how a PC reboots. The platform says where it is (dev/platform.h: "i8042", with its two interrupts in the ports' order).

Definition in file i8042.c.

Macro Definition Documentation

◆ CMD_AUX_ENABLE

#define CMD_AUX_ENABLE   0xA8

Definition at line 142 of file i8042.c.

Referenced by aux_enable().

◆ CMD_READ_CONFIG

#define CMD_READ_CONFIG   0x20

Definition at line 140 of file i8042.c.

Referenced by aux_enable().

◆ CMD_WRITE_CONFIG

#define CMD_WRITE_CONFIG   0x60

Definition at line 141 of file i8042.c.

Referenced by aux_enable().

◆ COMMAND_PORT

#define COMMAND_PORT   0x04

Definition at line 31 of file i8042.c.

Referenced by aux_enable(), i8042_aux_write(), and i8042_reboot().

◆ CONFIG_AUX_IRQ

#define CONFIG_AUX_IRQ   0x02

Definition at line 143 of file i8042.c.

Referenced by aux_enable().

◆ DATA_PORT

#define DATA_PORT   0x00

Definition at line 29 of file i8042.c.

Referenced by aux_enable(), i8042_aux_write(), i8042_init(), i8042_irq(), and read_wait().

◆ STATUS_COMMAND

#define STATUS_COMMAND   0x08

Definition at line 36 of file i8042.c.

◆ STATUS_IBUF_FULL

#define STATUS_IBUF_FULL   0x02

Definition at line 34 of file i8042.c.

Referenced by write_wait().

◆ STATUS_OBUF_FULL

#define STATUS_OBUF_FULL   0x01

Definition at line 33 of file i8042.c.

Referenced by has_byte().

◆ STATUS_PARITY

#define STATUS_PARITY   0x80

Definition at line 38 of file i8042.c.

◆ STATUS_PORT

#define STATUS_PORT   0x04

Definition at line 30 of file i8042.c.

Referenced by read_status().

◆ STATUS_SYSTEM

#define STATUS_SYSTEM   0x04

Definition at line 35 of file i8042.c.

Referenced by i8042_init().

◆ STATUS_TOUT

#define STATUS_TOUT   0x40

Definition at line 37 of file i8042.c.

Function Documentation

◆ aux_enable()

int aux_enable ( void )
static

Turns the second port on, and its interrupt (a bit of the controller's configuration byte).

It polls: the interrupt handlers aren't installed yet. Returns 0 or a negative errno.

Definition at line 148 of file i8042.c.

References aux_enable(), CMD_AUX_ENABLE, CMD_READ_CONFIG, CMD_WRITE_CONFIG, COMMAND_PORT, CONFIG_AUX_IRQ, DATA_PORT, i8042_ioaddr, io_out8(), read_wait(), and write_wait().

Referenced by aux_enable(), and i8042_init().

◆ has_byte()

int has_byte ( void )
static

Returns whether the controller has a byte for us (its output buffer is full).

The interrupt only comes once it has one, so there is nothing to wait for; an interrupt with nothing to read is spurious.

Definition at line 49 of file i8042.c.

References has_byte(), read_status(), and STATUS_OBUF_FULL.

Referenced by has_byte(), i8042_irq(), and read_wait().

◆ i8042_aux_write()

int i8042_aux_write ( uint8_t byte)

Sends a byte to the device on the second port (the mouse).

Its replies come as that port's interrupts.

Parameters
bytethe byte
Returns
0, -ENODEV if there is no controller, or -ETIMEDOUT if it didn't take the byte in time

Definition at line 100 of file i8042.c.

References COMMAND_PORT, DATA_PORT, i8042_aux_write(), i8042_found, i8042_ioaddr, io_out8(), and write_wait().

Referenced by i8042_aux_write(), ps2mouse_file_open(), ps2mouse_probe(), and ps2mouse_write().

◆ i8042_handler_register()

void i8042_handler_register ( int port,
void(* fun )(int) )

Registers the function that gets each byte a port receives.

Parameters
portthe port: 1 (the keyboard) or 2 (the mouse)
funthe function, called with each byte, from the interrupt

Definition at line 75 of file i8042.c.

References i8042_handler_register(), and port_handler.

Referenced by i8042_handler_register(), ps2kbd_probe(), and ps2mouse_register().

◆ i8042_init()

int i8042_init ( void )
static

Finds the platform's i8042, turns the mouse's port on, and installs the ports' interrupt handlers.

Definition at line 179 of file i8042.c.

References ioaddr::addr, aux_enable(), DATA_PORT, i8042_found, i8042_init(), i8042_ioaddr, i8042_irq(), platform_device::io, io_in8(), ioaddr_type_str(), platform_device::irq, irq_install(), kargs_get(), platform_device::nirq, NULL, platform_device_get(), platform_device_io(), port_handler, printk(), read_status(), and STATUS_SYSTEM.

Referenced by i8042_init().

◆ i8042_irq()

void i8042_irq ( void * arg)
static

Handles a port's interrupt: its byte goes to the port's handler.

arg is the port's slot in port_handler.

Definition at line 56 of file i8042.c.

References DATA_PORT, has_byte(), i8042_ioaddr, i8042_irq(), and io_in8().

Referenced by i8042_init(), and i8042_irq().

◆ i8042_reboot()

void i8042_reboot ( void )

Resets the CPU, through the controller's output line: this reboots the PC.

Without a controller, it does nothing.

Definition at line 123 of file i8042.c.

References COMMAND_PORT, i8042_found, i8042_ioaddr, i8042_reboot(), and io_out8().

Referenced by i8042_reboot(), and platform_reboot().

◆ read_status()

uint8_t read_status ( void )
static

Returns the controller's status register.

Definition at line 41 of file i8042.c.

References i8042_ioaddr, io_in8(), read_status(), and STATUS_PORT.

Referenced by has_byte(), i8042_init(), read_status(), and write_wait().

◆ read_wait()

int read_wait ( void )
static

Waits for the controller's reply to a command.

Returns the reply, or -ETIMEDOUT if none comes in time.

Definition at line 131 of file i8042.c.

References DATA_PORT, has_byte(), i8042_ioaddr, io_in8(), and read_wait().

Referenced by aux_enable(), and read_wait().

◆ write_wait()

int write_wait ( void )
static

Waits until the controller can take a byte.

Returns 0, or -ETIMEDOUT if it doesn't in time.

Definition at line 83 of file i8042.c.

References read_status(), STATUS_IBUF_FULL, and write_wait().

Referenced by aux_enable(), i8042_aux_write(), and write_wait().

Variable Documentation

◆ i8042_found

int i8042_found
static

Definition at line 24 of file i8042.c.

Referenced by i8042_aux_write(), i8042_init(), and i8042_reboot().

◆ i8042_ioaddr

struct ioaddr i8042_ioaddr
static

◆ port_handler

void(* port_handler[2])(int) ( int )
static

Definition at line 27 of file i8042.c.

Referenced by i8042_handler_register(), and i8042_init().