Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Keyboard driver #20

Open
wants to merge 12 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
build/
toolchain/
.toolchains/
*.err
bx_enh_dbg.ini
.vscode/*.log
Expand Down
36 changes: 35 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,40 @@
{
"files.associations": {
"x86.h": "c",
"stdio.h": "c"
"stdio.h": "c",
"hal.h": "c",
"stdint.h": "c",
"stdint-gcc.h": "c",
"cstdint": "c",
"ctype.h": "c",
"stdbool.h": "c",
"cstddef": "c",
"list": "c",
"string": "c",
"bit": "c",
"limits": "c",
"memory": "c",
"functional": "c",
"compare": "c",
"new": "c",
"*.in": "c",
"*.tcc": "c",
"optional": "c",
"ratio": "c",
"system_error": "c",
"type_traits": "c",
"variant": "c",
"array": "c",
"regex": "c",
"tuple": "c",
"utility": "c",
"memory.h": "c",
"irq.h": "c",
"bootparams.h": "c",
"io.h": "c",
"chrono": "c",
"i8042.h": "c",
"i8259.h": "c",
"keyboard.h": "c"
}
}
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
# basic_os

My work in progress. First step is getting the keyboard controller fully operational, then I can start working on parsing user input into something interesting.

---

# nanobyte_os
This repository contains the code from the ["Building an OS"](https://www.youtube.com/watch?v=9t-SPC7Tczc&list=PLFjM7v6KGMpiH2G-kT781ByCNC_0pKpPN) series on the ["Nanobyte"](https://www.youtube.com/channel/UCSPIuWADJIMIf9Erf--XAsA) YouTube channel.

Expand All @@ -19,6 +25,11 @@ paru -S gcc make bison flex libgmp-static libmpc mpfr texinfo nasm mtools qemu-s
```
NOTE: to install all the required packages on Arch, you need an [AUR helper](https://wiki.archlinux.org/title/AUR_helpers).

I had to run this to get guestmount to work without sudo:
```bash
sudo dpkg-statoverride --update --add root root 0644 /boot/vmlinuz-`uname -r`
```

Then you must run `python3 -m pip install -r requirements.txt`

After that, run `scons toolchain`, this should download and build the required tools (binutils and GCC). If you encounter errors during this step, you might have to modify `build_scripts/config.mk` and try a different version of **binutils** and **gcc**. Using the same version as the one bundled with your distribution is your best bet.
Expand All @@ -29,3 +40,4 @@ Finally, you should be able to run `scons`. Use `scons run` to test your OS usin

* [Discord channel](https://discord.gg/RgHc5XrCEw)
* [Patreon](https://www.patreon.com/nanobyte)

4 changes: 3 additions & 1 deletion build_scripts/config.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import os

#config = 'release'
#arch = 'i686'
imageType = 'disk'
imageFS = 'fat32'
imageSize = '250m'
toolchain='../.toolchains'
toolchain = os.path.abspath(os.path.join(os.getcwd(), '.toolchains'))
158 changes: 158 additions & 0 deletions src/kernel/arch/i686/i8042.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
#include "i8042.h"

// For i686_inb/outb.
#include "arch/i686/io.h"

static PS2Driver g_PS2Driver = {
.Name = "8042 PS/2",
.KeyboardPortNumber = -1,
.MousePortNumber = -1
};

/**
* Wait until the 8042 is done processing the current command.
*/
void i8042_waitForBufferEmpty() {
uint16_t count = 0xFFFF;
do {
uint8_t status = i686_inb(PS2_REGISTER);
if ((status & PS2_STATUS_INPUT_BUFFER) != 0) {
break;
}
} while (count--);
}

/*
* Send the given command to the 8042 PS/2 controller.
*
* command: The command byte to send.
*/
void i8042_setCommand(uint8_t command) {
i686_DisableInterrupts();

i8042_waitForBufferEmpty();

// Send the command.
i686_outb(PS2_REGISTER, command);

i686_EnableInterrupts();
}

/**
* portNumber: [0..1] to specify the first or second PS/2 port.
*/
void i8042_sendCommand(uint8_t command, int portNumber) {
// Disable the port.
i8042_setCommand((portNumber == 0) ? PS2_COMMAND_DISABLE_FIRST_PORT : PS2_COMMAND_DISABLE_SECOND_PORT);

i686_DisableInterrupts();

// Wait until the 8042 is done processing the current command.
i8042_waitForBufferEmpty();

if (portNumber != 0) {
i8042_setCommand(PS2_COMMAND_SELECT_PORT2); // Address the second PS/2 port.
}

// Enable the keyboard.
i8042_setCommand((portNumber == 0) ? PS2_COMMAND_ENABLE_FIRST_PORT : PS2_COMMAND_ENABLE_SECOND_PORT);

// Send the data to port 0x60.
i686_outb(PS2_DATA_PORT, command);

i686_EnableInterrupts();
}

void i8042_disableScanning(int portNumber) {
do {
i8042_sendCommand(PS2_COMMAND_DISABLE_SCANNING, portNumber); // Disable Scanning
} while (i686_inb(PS2_DATA_PORT) == PS2_RESPONSE_RESEND);
}

void i8042_enableScanning(int portNumber) {
do {
i8042_sendCommand(PS2_COMMAND_ENABLE_SCANNING, portNumber); // Enable Scanning
} while (i686_inb(PS2_DATA_PORT) == PS2_RESPONSE_RESEND);
}

/*
* Inputs:
* portNumber: [0, 1] for the first or second PS/2 ports respectively.
*/
PS2_DEVICE_TYPE i8042_getDeviceType(uint8_t portNumber) {
i8042_disableScanning(portNumber);
i8042_waitForBufferEmpty();
do {
i8042_sendCommand(PS2_COMMAND_IDENTIFY_HARDWARE, portNumber); // Identify
} while (i686_inb(PS2_DATA_PORT) == PS2_RESPONSE_RESEND);

PS2_DEVICE_TYPE type = PS2_DEVICE_TYPE_KEYBOARD_AT;
for (int n = 0; n < 16; n++) {
uint8_t response = i686_inb(PS2_DATA_PORT);
switch (response) {
case 0x00:
type = PS2_DEVICE_TYPE_MOUSE_STANDARD;
break;
case 0x03:
type = PS2_DEVICE_TYPE_MOUSE_WHEEL;
break;
case 0x04:
type = PS2_DEVICE_TYPE_MOUSE_5BUTTON;
break;
case 0x41:
case 0xC1:
type = PS2_DEVICE_TYPE_KEYBOARD_MF2_TRANSLATED;
break;
case 0x83:
type = PS2_DEVICE_TYPE_KEYBOARD_MF2;
break;
}
if (type != PS2_DEVICE_TYPE_KEYBOARD_AT) {
break;
}
}

i8042_enableScanning(portNumber);
return type;
}

PS2_DEVICE_CATEGORY i8042_getDeviceCategory(uint8_t portNumber) {
switch (i8042_getDeviceType(portNumber)) {
case PS2_DEVICE_TYPE_MOUSE_STANDARD:
case PS2_DEVICE_TYPE_MOUSE_WHEEL:
case PS2_DEVICE_TYPE_MOUSE_5BUTTON:
return PS2_DEVICE_CATEGORY_MOUSE;
default:
case PS2_DEVICE_TYPE_KEYBOARD_AT:
case PS2_DEVICE_TYPE_KEYBOARD_MF2:
case PS2_DEVICE_TYPE_KEYBOARD_MF2_TRANSLATED:
return PS2_DEVICE_CATEGORY_KEYBOARD;
}
}

void i8042_Initialize() {
g_PS2Driver.KeyboardPortNumber = -1;
g_PS2Driver.MousePortNumber = -1;

switch (i8042_getDeviceCategory(0)) {
case PS2_DEVICE_CATEGORY_KEYBOARD:
g_PS2Driver.KeyboardPortNumber = 0;
break;
case PS2_DEVICE_CATEGORY_MOUSE:
g_PS2Driver.MousePortNumber = 0;
break;
}

switch (i8042_getDeviceCategory(1)) {
case PS2_DEVICE_CATEGORY_KEYBOARD:
g_PS2Driver.KeyboardPortNumber = 1;
break;
case PS2_DEVICE_CATEGORY_MOUSE:
g_PS2Driver.MousePortNumber = 1;
break;
}
}

const PS2Driver* i8042_getDriver() {
return &g_PS2Driver;
}
99 changes: 99 additions & 0 deletions src/kernel/arch/i686/i8042.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
#pragma once

#include <stdint.h>
#include "ps2.h"

#define PS2_DATA_PORT 0x60
#define PS2_REGISTER 0x64 // Read for status, write for command.

/*
* Byte/s Device Type
* None Ancient AT keyboard with translation enabled in the PS/Controller (not possible for the second PS/2 port)
* 0x00 Standard PS/2 mouse
* 0x03 Mouse with scroll wheel
* 0x04 5-button mouse
* 0xAB, 0x41 or 0xAB, 0xC1 MF2 keyboard with translation enabled in the PS/Controller (not possible for the second PS/2 port)
* 0xAB, 0x83 MF2 keyboard
*/
typedef enum {
PS2_DEVICE_TYPE_KEYBOARD_AT,
PS2_DEVICE_TYPE_MOUSE_STANDARD,
PS2_DEVICE_TYPE_MOUSE_WHEEL,
PS2_DEVICE_TYPE_MOUSE_5BUTTON,
PS2_DEVICE_TYPE_KEYBOARD_MF2_TRANSLATED,
PS2_DEVICE_TYPE_KEYBOARD_MF2
} PS2_DEVICE_TYPE;

typedef enum {
PS2_DEVICE_CATEGORY_MOUSE,
PS2_DEVICE_CATEGORY_KEYBOARD
} PS2_DEVICE_CATEGORY;

typedef enum {
PS2_COMMAND_DISABLE_FIRST_PORT = 0xAD,
PS2_COMMAND_ENABLE_FIRST_PORT = 0xAE,
PS2_COMMAND_DISABLE_SECOND_PORT = 0xA7,
PS2_COMMAND_ENABLE_SECOND_PORT = 0xA8,
PS2_COMMAND_SELECT_PORT2 = 0xD4,
PS2_COMMAND_SET_LED = 0xED,
PS2_COMMAND_SCANCODE_SET = 0xF0, // get/set
PS2_COMMAND_IDENTIFY_HARDWARE = 0xF2,
PS2_COMMAND_SET_TYPEMATIC_RATE_AND_DELAY = 0xF3,
PS2_COMMAND_ENABLE_SCANNING = 0xF4,
PS2_COMMAND_DISABLE_SCANNING = 0xF5
} PS2_COMMAND;

typedef enum {
PS2_SCANCODE_SET_1 = 0x43,
PS2_SCANCODE_SET_2 = 0x41,
PS2_SCANCODE_SET_3 = 0x3f
} PS2_SCANCODE_SET;

/*
* The Status Register contains various flags that show the state of the PS/2 controller. The meanings for each bit are:
*
* Bit Meaning
* 0 Output buffer status (0 = empty, 1 = full)
* (must be set before attempting to read data from IO port 0x60)
*
* 1 Input buffer status (0 = empty, 1 = full)
* (must be clear before attempting to write data to IO port 0x60 or IO port 0x64)
*
* 2 System Flag
* Meant to be cleared on reset and set by firmware (via. PS/2 Controller Configuration Byte) if the system passes self tests (POST)
*
* 3 Command/data (0 = data written to input buffer is data for PS/2 device, 1 = data written to input buffer is data for PS/2 controller command)
* 4 Unknown (chipset specific)
* May be "keyboard lock" (more likely unused on modern systems)
*
* 5 Unknown (chipset specific)
* May be "receive time-out" or "second PS/2 port output buffer full"
*
* 6 Time-out error (0 = no error, 1 = time-out error)
* 7 Parity error (0 = no error, 1 = parity error)
*/
typedef enum {
PS2_STATUS_OUTPUT_BUFFER = 0x01,
PS2_STATUS_INPUT_BUFFER = 0x02,
PS2_STATUS_SYSTEM_FLAG = 0x04,
PS2_STATUS_COMMAND = 0x08,
PS2_STATUS_UNKNOWN0 = 0x10,
PS2_STATUS_UNKNOWN1 = 0x20,
PS2_STATUS_ERROR_TIMEOUT = 0x40,
PS2_STATUS_ERROR_PARITY = 0x80
} PS2_STATUS;

typedef enum {
PS2_RESPONSE_ACK = 0xFA,
PS2_RESPONSE_RESEND = 0xF3
} PS2_RESPONSE;

void i8042_waitForBufferEmpty();
void i8042_setCommand(uint8_t command);
void i8042_sendCommand(uint8_t command, int portNumber);
void i8042_disableScanning(int portNumber);
void i8042_enableScanning(int portNumber);
PS2_DEVICE_TYPE i8042_getDeviceType(uint8_t portNumber);
PS2_DEVICE_CATEGORY i8042_getDeviceCategory(uint8_t portNumber);
void i8042_Initialize();
const PS2Driver* i8042_getDriver();
6 changes: 2 additions & 4 deletions src/kernel/arch/i686/irq.c
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ void i686_IRQ_Initialize()
return;
}

log_info(MODULE, "Found %s PIC.", g_Driver->Name);
log_info(MODULE, "Found %s.", g_Driver->Name);
g_Driver->Initialize(PIC_REMAP_OFFSET, PIC_REMAP_OFFSET + 8, false);

// register ISR handlers for each of the 16 irq lines
Expand All @@ -57,12 +57,10 @@ void i686_IRQ_Initialize()

// enable interrupts
i686_EnableInterrupts();

// g_Driver->Unmask(0);
// g_Driver->Unmask(1);
}

void i686_IRQ_RegisterHandler(int irq, IRQHandler handler)
{
g_IRQHandlers[irq] = handler;
g_Driver->Unmask(irq);
}
10 changes: 10 additions & 0 deletions src/kernel/arch/i686/ps2.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#pragma once

#include <stdint.h>
#include <stdbool.h>

typedef struct {
const char* Name;
int KeyboardPortNumber;
int MousePortNumber;
} PS2Driver;
Loading