summaryrefslogtreecommitdiff
path: root/kernel/src/drivers
diff options
context:
space:
mode:
authorTyler Murphy <=>2023-07-17 19:34:52 -0400
committerTyler Murphy <=>2023-07-17 19:34:52 -0400
commit7a912d1b668ab86ffe088eca3ac7e6f78a04a0c5 (patch)
tree4e86ff20e73171285156631db043e12aaf63bf04 /kernel/src/drivers
parentpaging (diff)
downloadfinix-7a912d1b668ab86ffe088eca3ac7e6f78a04a0c5.tar.gz
finix-7a912d1b668ab86ffe088eca3ac7e6f78a04a0c5.tar.bz2
finix-7a912d1b668ab86ffe088eca3ac7e6f78a04a0c5.zip
refactoring
Diffstat (limited to 'kernel/src/drivers')
-rw-r--r--kernel/src/drivers/cmos.c138
-rw-r--r--kernel/src/drivers/ps2ctrl.c93
-rw-r--r--kernel/src/drivers/ps2ctrl.h14
-rw-r--r--kernel/src/drivers/ps2kb.c126
-rw-r--r--kernel/src/drivers/ps2kb.h15
-rw-r--r--kernel/src/drivers/ps2mouse.c85
-rw-r--r--kernel/src/drivers/ps2mouse.h18
7 files changed, 0 insertions, 489 deletions
diff --git a/kernel/src/drivers/cmos.c b/kernel/src/drivers/cmos.c
deleted file mode 100644
index 5ac12c3..0000000
--- a/kernel/src/drivers/cmos.c
+++ /dev/null
@@ -1,138 +0,0 @@
-#include "print.h"
-#include <stdint.h>
-#include <sys.h>
-#include <time.h>
-
-#define CMOS_WRITE_PORT 0x70
-#define CMOS_READ_PORT 0x71
-
-#define CMOS_REG_SEC 0x00
-#define CMOS_REG_MIN 0x02
-#define CMOS_REG_HOUR 0x04
-#define CMOS_REG_WDAY 0x06
-#define CMOS_REG_MDAY 0x07
-#define CMOS_REG_MON 0x08
-#define CMOS_REG_YEAR 0x09
-#define CMOS_REG_CEN 0x32
-
-static struct Time time;
-static struct Time localtime;
-static int cur_offset = 0;
-
-static uint8_t cmos_read(uint8_t reg) {
- uint8_t hex, ret;
-
- outb(CMOS_WRITE_PORT, reg);
- hex = inb(CMOS_READ_PORT);
-
- ret = hex & 0x0F;
- ret += (hex & 0xF0) / 16 * 10;
-
- return ret;
-}
-
-static int mday_offset[12] = {
- 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334
-};
-
-static int month_days[12] = {
- 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
-};
-
-static void update_localtime(void) {
-
- int change, max;
-
- // set localtime
- localtime = time;
-
- // do we acutally need to do anything
- if (cur_offset == 0) return;
- localtime.hour += cur_offset;
-
- // check if day rolled over
- change = localtime.hour < 0 ? -1 : localtime.hour >= 24 ? 1 : 0;
- if (!change) return;
-
- // roll over day
- localtime.hour = (localtime.hour + 24) % 24;
- localtime.wday = (localtime.wday + change + 7) % 7;
- localtime.mday += change;
- localtime.yday += change;
-
- // check if month rolled over
- max = month_days[localtime.mon];
- if (localtime.leap && localtime.mon == 1) max++;
- change = localtime.mday < 0 ? -1 : localtime.mday >= max ? 1 : 0;
- if (!change) return;
-
- // roll over month
- localtime.mon = (localtime.mon + change + 12) % 12;
-
- // check if year rolled over
- max = localtime.leap ? 366 : 365;
- change = localtime.yday < 0 ? -1 : localtime.yday >= max ? 1 : 0;
- if (!change) return;
-
- // roll over year
- localtime.yn += change;
-
- // check if cen rolled over
- change = localtime.yn < 0 ? -1 : localtime.yn >= 100 ? 1 : 0;
- if (!change) goto year;
-
- // roll over cen
- localtime.cen += change;
-
-
-year:
-
- localtime.year = localtime.yn + localtime.cen * 100;
- localtime.leap = localtime.year % 4 == 0 && localtime.year % 100 != 0;
-
- if (localtime.leap && localtime.yday == -1)
- localtime.yday = 365;
- else if (localtime.yday == -1)
- localtime.yday = 364;
- else
- localtime.yday = 0;
-
- localtime.year -= 1900;
-
-}
-
-void rtc_set_timezone(int offset) {
- cur_offset = offset;
-}
-
-void rtc_update(void) {
- time.sec = cmos_read(CMOS_REG_SEC);
- time.min = cmos_read(CMOS_REG_MIN);
- time.hour = cmos_read(CMOS_REG_HOUR);
- time.wday = cmos_read(CMOS_REG_WDAY) - 1;
- time.mday = cmos_read(CMOS_REG_MDAY);
- time.mon = cmos_read(CMOS_REG_MON) - 1;
- time.yn = cmos_read(CMOS_REG_YEAR);
- time.cen = 20;
-
- time.year = time.yn + time.cen * 100;
-
- time.leap = time.year % 4 == 0 && time.year % 100 != 0;
-
- time.yday = mday_offset[time.mon] + time.mday;
-
- if (time.leap && time.mon > 2)
- time.yday++;
-
- time.year -= 1900;
-
- update_localtime();
-}
-
-struct Time *rtc_utctime(void) {
- return &time;
-}
-
-struct Time *rtc_localtime(void) {
- return &localtime;
-}
diff --git a/kernel/src/drivers/ps2ctrl.c b/kernel/src/drivers/ps2ctrl.c
deleted file mode 100644
index 05ef25d..0000000
--- a/kernel/src/drivers/ps2ctrl.c
+++ /dev/null
@@ -1,93 +0,0 @@
-#include <panic.h>
-#include <sys.h>
-
-#include "print.h"
-#include "ps2ctrl.h"
-#include "interrupt/pic.h"
-
-#define STATUS_OUT_BUF ((uint8_t)0x01)
-#define STATUS_IN_BUF ((uint8_t)0x02)
-
-#define CONFIG_INT_0 ((uint8_t)0x01)
-#define CONFIG_INT_1 ((uint8_t)0x02)
-#define CONFIG_SYS ((uint8_t)0x04)
-#define CONFIG_CLOCK_0 ((uint8_t)0x10)
-#define CONFIG_CLOCK_1 ((uint8_t)0x20)
-#define CONFIG_TRANS ((uint8_t)0x40)
-
-static bool is_init = false;
-
-uint8_t ps2ctrl_in(void) {
- while((ps2ctrl_in_status() & STATUS_OUT_BUF) == 0) {
- io_wait();
- }
- return inb(0x60);
-}
-
-uint8_t ps2ctrl_in_status(void) {
- return inb(0x64);
-}
-
-void ps2ctrl_out_cmd(uint8_t cmd) {
- while((ps2ctrl_in_status() & STATUS_IN_BUF) != 0) {
- io_wait();
- }
- outb(0x64, cmd);
-}
-
-void ps2ctrl_out_data(uint8_t data) {
- while((ps2ctrl_in_status() & STATUS_IN_BUF) != 0) {
- io_wait();
- }
- outb(0x60, data);
-}
-
-void ps2ctrl_set_port2(void) {
- outb(0x64, 0xD4);
-}
-
-void ps2ctrl_init(void) {
-
- is_init = false;
-
- pic_mask(1); // keyboard
- pic_mask(12); // mouse
-
- inb(0x60);
-
- // self-test
- ps2ctrl_out_cmd(0xAA);
- uint8_t response = ps2ctrl_in();
- if(response != 0x55) {
- panic("PS/2 controller failed to initialize");
- }
-
- // set config
- ps2ctrl_out_cmd(0x20);
- uint8_t config = ps2ctrl_in();
- config = (config | CONFIG_INT_0 | CONFIG_INT_1) & ~CONFIG_TRANS;
- // config = 0xFF;
- ps2ctrl_out_cmd(0x60);
- ps2ctrl_out_data(config);
-
- // enable port 0
- ps2ctrl_out_cmd(0xAE);
-
- // enable port 2
- ps2ctrl_out_cmd(0xA9);
- response = ps2ctrl_in();
- if (response == 0x01) {
- panic("PS/2 port 2 not supported");
- }
-
- ps2ctrl_out_cmd(0xA8);
-
- is_init = true;
-
- pic_unmask(1);
- pic_unmask(12);
-}
-
-bool ps2ctrl_is_init(void) {
- return is_init;
-}
diff --git a/kernel/src/drivers/ps2ctrl.h b/kernel/src/drivers/ps2ctrl.h
deleted file mode 100644
index a674c57..0000000
--- a/kernel/src/drivers/ps2ctrl.h
+++ /dev/null
@@ -1,14 +0,0 @@
-#pragma once
-
-#include <stdbool.h>
-#include <stdint.h>
-
-uint8_t ps2ctrl_in(void);
-uint8_t ps2ctrl_in_status(void);
-void ps2ctrl_out_cmd(uint8_t cmd);
-void ps2ctrl_out_data(uint8_t data);
-void ps2ctrl_set_port2(void);
-
-void ps2ctrl_init(void);
-
-bool ps2ctrl_is_init(void);
diff --git a/kernel/src/drivers/ps2kb.c b/kernel/src/drivers/ps2kb.c
deleted file mode 100644
index f6fc514..0000000
--- a/kernel/src/drivers/ps2kb.c
+++ /dev/null
@@ -1,126 +0,0 @@
-#include <panic.h>
-#include <sys.h>
-
-#include "print.h"
-#include "ps2kb.h"
-#include "ps2ctrl.h"
-#include "interrupt/pic.h"
-
-#define BUFFER_LEN 16
-
-#define KEYCODE_ARRAY_LEN 0x84
-
-static uint8_t scancodes[] = {
-// 00/08 01/09 02/0A 03/0B 04/0C 05/0D 06/0E 07/0F
-/*00*/ KEY_NONE, KEY_F9, KEY_NONE, KEY_F5, KEY_F3, KEY_F1, KEY_F2, KEY_F12,
-/*08*/ KEY_NONE, KEY_F10, KEY_F8, KEY_F6, KEY_F4, KEY_TAB, KEY_BACKTICK, KEY_NONE,
-/*10*/ KEY_NONE, KEY_L_ALT, KEY_L_SHIFT, KEY_NONE, KEY_L_CTRL, KEY_Q, KEY_1, KEY_NONE,
-/*18*/ KEY_NONE, KEY_NONE, KEY_Z, KEY_S, KEY_A, KEY_W, KEY_2, KEY_NONE,
-/*20*/ KEY_NONE, KEY_C, KEY_X, KEY_D, KEY_E, KEY_4, KEY_3, KEY_NONE,
-/*28*/ KEY_NONE, KEY_SPACE, KEY_V, KEY_F, KEY_T, KEY_R, KEY_5, KEY_NONE,
-/*30*/ KEY_NONE, KEY_N, KEY_B, KEY_H, KEY_G, KEY_Y, KEY_6, KEY_NONE,
-/*38*/ KEY_NONE, KEY_NONE, KEY_M, KEY_J, KEY_U, KEY_7, KEY_8, KEY_NONE,
-/*40*/ KEY_NONE, KEY_COMMA, KEY_K, KEY_I, KEY_O, KEY_0, KEY_9, KEY_NONE,
-/*48*/ KEY_NONE, KEY_PERIOD, KEY_SLASH, KEY_L, KEY_SEMICOLON, KEY_P, KEY_MINUS, KEY_NONE,
-/*50*/ KEY_NONE, KEY_NONE, KEY_QUOTE, KEY_NONE, KEY_L_BRACE, KEY_EQUAL, KEY_NONE, KEY_NONE,
-/*58*/ KEY_CAPS_LOCK, KEY_R_SHIFT, KEY_ENTER, KEY_R_BRACE, KEY_NONE, KEY_BACKSLASH, KEY_NONE, KEY_NONE,
-/*60*/ KEY_NONE, KEY_NONE, KEY_NONE, KEY_NONE, KEY_NONE, KEY_NONE, KEY_BACKSPACE, KEY_NONE,
-/*68*/ KEY_NONE, KEY_NP_1, KEY_NONE, KEY_NP_4, KEY_NP_7, KEY_NONE, KEY_NONE, KEY_NONE,
-/*70*/ KEY_NP_0, KEY_NP_PERIOD, KEY_NP_2, KEY_NP_5, KEY_NP_6, KEY_NP_8, KEY_ESCAPE, KEY_NUM_LOCK,
-/*78*/ KEY_F11, KEY_NP_PLUS, KEY_NP_3, KEY_NP_MINUS, KEY_NP_ASTERISK, KEY_NP_9, KEY_SCROLL_LOCK, KEY_NONE,
-/*80*/ KEY_NONE, KEY_NONE, KEY_NONE, KEY_F7,
-};
-static uint8_t scancodes_ext[] = {
-// 00/08 01/09 02/0A 03/0B 04/0C 05/0D 06/0E 07/0F
-/*00*/ KEY_NONE, KEY_NONE, KEY_NONE, KEY_NONE, KEY_NONE, KEY_NONE, KEY_NONE, KEY_NONE,
-/*08*/ KEY_NONE, KEY_NONE, KEY_NONE, KEY_NONE, KEY_NONE, KEY_NONE, KEY_NONE, KEY_NONE,
-/*10*/ KEY_UNKNOWN, KEY_R_ALT, KEY_PRINT_SCREEN, KEY_NONE, KEY_R_CTRL, KEY_UNKNOWN, KEY_NONE, KEY_NONE,
-/*18*/ KEY_UNKNOWN, KEY_NONE, KEY_NONE, KEY_NONE, KEY_NONE, KEY_NONE, KEY_NONE, KEY_L_META,
-/*20*/ KEY_UNKNOWN, KEY_UNKNOWN, KEY_NONE, KEY_UNKNOWN, KEY_NONE, KEY_NONE, KEY_NONE, KEY_R_META,
-/*28*/ KEY_UNKNOWN, KEY_NONE, KEY_NONE, KEY_UNKNOWN, KEY_NONE, KEY_NONE, KEY_NONE, KEY_MENU,
-/*30*/ KEY_UNKNOWN, KEY_NONE, KEY_UNKNOWN, KEY_NONE, KEY_UNKNOWN, KEY_NONE, KEY_NONE, KEY_UNKNOWN,
-/*38*/ KEY_UNKNOWN, KEY_NONE, KEY_UNKNOWN, KEY_UNKNOWN, KEY_NONE, KEY_NONE, KEY_NONE, KEY_UNKNOWN,
-/*40*/ KEY_UNKNOWN, KEY_NONE, KEY_NONE, KEY_NONE, KEY_NONE, KEY_NONE, KEY_NONE, KEY_NONE,
-/*48*/ KEY_UNKNOWN, KEY_NONE, KEY_NP_SLASH, KEY_NONE, KEY_NONE, KEY_UNKNOWN, KEY_NONE, KEY_NONE,
-/*50*/ KEY_UNKNOWN, KEY_NONE, KEY_NONE, KEY_NONE, KEY_NONE, KEY_NONE, KEY_NONE, KEY_NONE,
-/*58*/ KEY_NONE, KEY_NONE, KEY_NP_ENTER, KEY_NONE, KEY_NONE, KEY_NONE, KEY_UNKNOWN, KEY_NONE,
-/*60*/ KEY_NONE, KEY_NONE, KEY_NONE, KEY_NONE, KEY_NONE, KEY_NONE, KEY_NONE, KEY_NONE,
-/*68*/ KEY_NONE, KEY_END, KEY_NONE, KEY_LEFT, KEY_HOME, KEY_NONE, KEY_NONE, KEY_NONE,
-/*70*/ KEY_INSERT, KEY_DELETE, KEY_DOWN, KEY_NONE, KEY_RIGHT, KEY_UP, KEY_NONE, KEY_NONE,
-/*78*/ KEY_NONE, KEY_NONE, KEY_PAGE_DOWN, KEY_NONE, KEY_NONE, KEY_PAGE_UP, KEY_NONE, KEY_NONE,
-/*80*/ KEY_NONE, KEY_NONE, KEY_NONE, KEY_NONE, KEY_NONE, KEY_NONE, KEY_NONE, KEY_NONE,
-};
-
-static struct Keycode last_keycode;
-
-static bool is_init = false;
-static bool state_keyup = false;
-static bool state_ext = false;
-
-void ps2kb_init(void) {
-
- is_init = false;
- pic_mask(1);
-
- uint8_t result;
-
- ps2ctrl_out_data(0xFF);
- result = ps2ctrl_in();
- if(result != 0xFA) {
- panic("Failed to reset PS/2 keyboard: expected 0xFA, got 0x%X\n", result);
- return;
- }
- result = ps2ctrl_in();
- if(result != 0xAA) {
- panic("Failed to reset PS/2 keyboard: expected 0xAA, got 0x%X\n", result);
- return;
- }
-
- ps2ctrl_out_data(0xF4);
- result = ps2ctrl_in();
- if(result != 0xFA) {
- panic("Failed to enable PS/2 keyboard: expected 0xFA, got 0x%X\n", result);
- return;
- }
-
- pic_unmask(1);
- is_init = true;
-}
-
-void ps2kb_recv(void) {
- if(!ps2ctrl_is_init() || !is_init) {
- inb(0x60);
- return;
- }
- uint8_t code = ps2ctrl_in();
- if (code == 0x00 || code == 0x0F) {
- last_keycode.key = KEY_NONE;
- last_keycode.flags = KC_FLAG_ERROR;
- } else if(code == 0xF0) {
- state_keyup = true;
- } else if(code == 0xE0) {
- state_ext = true;
- } else if(code <= KEYCODE_ARRAY_LEN) {
- uint8_t *scancode_table = state_ext ? scancodes_ext : scancodes;
- uint8_t keycode = scancode_table[code];
- if(keycode != KEY_NONE) {
- last_keycode.key = keycode;
- last_keycode.flags = state_keyup ? KC_FLAG_KEY_UP : KC_FLAG_KEY_DOWN;
- }
- state_keyup = false;
- state_ext = false;
- }
-}
-
-struct Keycode ps2kb_get(void) {
- struct Keycode code;
- if(is_init) {
- code = last_keycode;
- } else {
- code.key = KEY_NONE;
- code.flags = KC_FLAG_ERROR;
- }
- last_keycode.key = KEY_NONE;
- last_keycode.flags = 0;
- return code;
-}
diff --git a/kernel/src/drivers/ps2kb.h b/kernel/src/drivers/ps2kb.h
deleted file mode 100644
index 1aaefb2..0000000
--- a/kernel/src/drivers/ps2kb.h
+++ /dev/null
@@ -1,15 +0,0 @@
-#pragma once
-
-#include <stdbool.h>
-#include <stdint.h>
-#include <keycodes.h>
-
-struct Keycode {
- uint8_t key;
- uint8_t flags;
-};
-
-void ps2kb_init(void);
-
-void ps2kb_recv(void);
-struct Keycode ps2kb_get(void);
diff --git a/kernel/src/drivers/ps2mouse.c b/kernel/src/drivers/ps2mouse.c
deleted file mode 100644
index 1616cd6..0000000
--- a/kernel/src/drivers/ps2mouse.c
+++ /dev/null
@@ -1,85 +0,0 @@
-#include <panic.h>
-#include <stdbool.h>
-#include <string.h>
-#include <sys.h>
-
-#include "print.h"
-#include "ps2mouse.h"
-#include "drivers/ps2ctrl.h"
-#include "interrupt/pic.h"
-
-static bool is_init = false;
-
-static struct MouseEvent last_event;
-static uint8_t first_b, second_b, third_b;
-
-void ps2mouse_init(void) {
-
- is_init = false;
- pic_mask(12);
-
- uint8_t result;
-
- ps2ctrl_set_port2();
- ps2ctrl_out_data(0xFF);
- result = ps2ctrl_in();
- if (result != 0xFA) {
- panic("Failed to reset PS/2 mouse: expected 0xFA, got 0x%X", result);
- return;
- }
- result = ps2ctrl_in();
- if (result != 0xAA) {
- panic("Failed to reset PS/2 mouse: expected 0xAA, got 0x%X", result);
- return;
- }
-
- ps2ctrl_set_port2();
- ps2ctrl_out_data(0xF4);
-
- pic_unmask(12);
- is_init = true;
-}
-
-static uint8_t packet_num = 0;
-void ps2mouse_recv(void) {
- if (!ps2ctrl_is_init() || !is_init) {
- inb(0x60);
- return;
- }
-
- uint8_t packet = ps2ctrl_in();
- switch (packet_num) {
- case 0:
- first_b = packet;
- break;
- case 1:
- second_b = packet;
- break;
- case 2: {
- third_b = packet;
-
- int state, d;
- state = first_b;
- d = second_b;
- last_event.relx = d - ((state << 4) & 0x100);
- d = third_b;
- last_event.rely = d - ((state << 3) & 0x100);
-
- last_event.lmb = first_b & 0x01;
- last_event.rmb = first_b & 0x02;
- last_event.mmb = first_b & 0x04;
- last_event.updated = true;
- break;
- }
- }
-
- packet_num += 1;
- packet_num %= 3;
-
-}
-
-struct MouseEvent ps2mouse_get(void) {
- struct MouseEvent event = last_event;
- last_event.updated = false;
- return event;
-}
diff --git a/kernel/src/drivers/ps2mouse.h b/kernel/src/drivers/ps2mouse.h
deleted file mode 100644
index 9cd4818..0000000
--- a/kernel/src/drivers/ps2mouse.h
+++ /dev/null
@@ -1,18 +0,0 @@
-#pragma once
-
-#include <stdbool.h>
-#include <stdint.h>
-
-struct MouseEvent {
- bool updated;
- bool lmb;
- bool rmb;
- bool mmb;
- int relx;
- int rely;
-};
-
-void ps2mouse_init(void);
-
-void ps2mouse_recv(void);
-struct MouseEvent ps2mouse_get(void);