aboutsummaryrefslogtreecommitdiff
path: root/kernel/drivers
diff options
context:
space:
mode:
authorMarvin Borner2021-04-14 14:34:38 +0200
committerMarvin Borner2021-04-14 14:34:38 +0200
commit462eaa9531b9e62916b02ab52759cd070de755d3 (patch)
tree71bb32daa5f8b4389c9d4ce1e42d66600090485f /kernel/drivers
parent212582f69dea4c99c292081b15ea526014b9ad44 (diff)
Implemented some I/O interfaces
Diffstat (limited to 'kernel/drivers')
-rw-r--r--kernel/drivers/fb.c15
-rw-r--r--kernel/drivers/ps2/keyboard.c19
-rw-r--r--kernel/drivers/ps2/mouse.c119
-rw-r--r--kernel/drivers/ps2/ps2.c71
-rw-r--r--kernel/drivers/rtc.c20
-rw-r--r--kernel/drivers/timer.c35
6 files changed, 98 insertions, 181 deletions
diff --git a/kernel/drivers/fb.c b/kernel/drivers/fb.c
index 1d266f7..c74bc28 100644
--- a/kernel/drivers/fb.c
+++ b/kernel/drivers/fb.c
@@ -5,7 +5,7 @@
#include <def.h>
#include <errno.h>
#include <fb.h>
-#include <fs.h>
+#include <io.h>
#include <mem.h>
#include <mm.h>
#include <str.h>
@@ -21,15 +21,13 @@ struct vbe_basic {
u8 stuff3[212];
};
-PROTECTED static u32 dev_id = 0;
PROTECTED static struct vid_info *info = NULL;
static u32 fb_owner = 0;
-static res fb_ioctl(u32 request, void *arg1, void *arg2, void *arg3, struct vfs_dev *dev)
+static res fb_ioctl(u32 request, void *arg1, void *arg2, void *arg3)
{
UNUSED(arg2);
UNUSED(arg3);
- UNUSED(dev);
switch (request) {
case 0: {
@@ -66,10 +64,7 @@ CLEAR void fb_install(struct vid_info *boot)
{
info = boot;
- struct vfs_dev *dev = zalloc(sizeof(*dev));
- dev->name = strdup("fb");
- dev->type = DEV_CHAR;
- dev->ioctl = fb_ioctl;
- /* device_add(dev); */
- dev_id = dev->id;
+ struct io_dev *dev = zalloc(sizeof(*dev));
+ dev->control = fb_ioctl;
+ io_add(IO_FRAMEBUFFER, dev);
}
diff --git a/kernel/drivers/ps2/keyboard.c b/kernel/drivers/ps2/keyboard.c
index 9a19b5c..3c2cdbb 100644
--- a/kernel/drivers/ps2/keyboard.c
+++ b/kernel/drivers/ps2/keyboard.c
@@ -3,8 +3,8 @@
#include <cpu.h>
#include <def.h>
#include <errno.h>
-#include <fs.h>
#include <interrupts.h>
+#include <io.h>
#include <mem.h>
#include <print.h>
#include <proc.h>
@@ -14,7 +14,6 @@
#include <sys.h>
PROTECTED static struct stack *queue = NULL;
-PROTECTED static u32 dev_id = 0;
static struct event_keyboard *event = NULL;
static int state = 0;
@@ -46,9 +45,8 @@ static void keyboard_handler(struct regs *r)
merged = 0;
}
-static res keyboard_read(void *buf, u32 offset, u32 count, struct vfs_dev *dev)
+static res keyboard_read(void *buf, u32 offset, u32 count)
{
- UNUSED(dev);
if (stack_empty(queue))
return -EINVAL;
@@ -68,16 +66,15 @@ CLEAR void ps2_keyboard_reset(void)
stack_clear(queue);
}
-CLEAR void ps2_keyboard_install(void)
+CLEAR void ps2_keyboard_install(u8 device)
{
- //keyboard_rate(); TODO: Fix keyboard rate?
+ UNUSED(device);
+
irq_install_handler(1, keyboard_handler);
queue = stack_new();
- struct vfs_dev *dev = zalloc(sizeof(*dev));
- dev->name = strdup("kbd");
- dev->type = DEV_CHAR;
+ struct io_dev *dev = zalloc(sizeof(*dev));
dev->read = keyboard_read;
- /* device_add(dev); */
- dev_id = dev->id;
+ dev->ready = keyboard_ready;
+ io_add(IO_KEYBOARD, dev);
}
diff --git a/kernel/drivers/ps2/mouse.c b/kernel/drivers/ps2/mouse.c
index 9b3fa2e..1d3c1ae 100644
--- a/kernel/drivers/ps2/mouse.c
+++ b/kernel/drivers/ps2/mouse.c
@@ -3,8 +3,8 @@
#include <boot.h>
#include <cpu.h>
#include <errno.h>
-#include <fs.h>
#include <interrupts.h>
+#include <io.h>
#include <mem.h>
#include <print.h>
#include <proc.h>
@@ -14,7 +14,6 @@
#include <sys.h>
PROTECTED static struct stack *queue = NULL;
-PROTECTED static u32 dev_id = 0;
static struct event_mouse *event = NULL;
static char mouse_cycle = 0;
@@ -52,46 +51,13 @@ static void mouse_handler(struct regs *r)
}
}
-#define MOUSE_WAIT_OUT 0
-#define MOUSE_WAIT_IN 1
-CLEAR static void mouse_serial_wait(u8 in)
-{
- u32 time_out = 100000;
- if (in) {
- while (time_out--)
- if (ps2_read_status().in_full)
- return;
- return;
- } else {
- while (time_out--)
- if (ps2_read_status().out_full)
- return;
- return;
- }
-}
-
-CLEAR static void mouse_serial_write(u8 data)
-{
- mouse_serial_wait(MOUSE_WAIT_IN);
- ps2_write_command(0xd4);
- mouse_serial_wait(MOUSE_WAIT_IN);
- ps2_write_data(data);
-}
-
-CLEAR static u8 mouse_serial_read(void)
-{
- mouse_serial_wait(MOUSE_WAIT_OUT);
- return ps2_read_data();
-}
-
static res mouse_ready(void)
{
return !stack_empty(queue);
}
-static res mouse_read(void *buf, u32 offset, u32 count, struct vfs_dev *dev)
+static res mouse_read(void *buf, u32 offset, u32 count)
{
- (void)dev;
if (stack_empty(queue))
return -EINVAL;
@@ -101,96 +67,33 @@ static res mouse_read(void *buf, u32 offset, u32 count, struct vfs_dev *dev)
return MIN(count, sizeof(*e));
}
-CLEAR void ps2_mouse_install(void)
+CLEAR void ps2_mouse_install(u8 device)
{
u8 status;
// Enable auxiliary mouse device
- mouse_serial_wait(MOUSE_WAIT_IN);
- ps2_write_command(0xa8);
+ ps2_write_device(device, 0xa8);
// Enable interrupts
- mouse_serial_wait(MOUSE_WAIT_IN);
ps2_write_command(0x20);
- mouse_serial_wait(MOUSE_WAIT_OUT);
status = ps2_read_data() | 3;
- mouse_serial_wait(MOUSE_WAIT_IN);
ps2_write_command(0x60);
- mouse_serial_wait(MOUSE_WAIT_IN);
ps2_write_data(status);
// Use default settings
- mouse_serial_write(0xf6);
- mouse_serial_read();
-
- // Enable mousewheel
- mouse_serial_write(0xf2);
- mouse_serial_read();
- mouse_serial_read();
- mouse_serial_write(0xf3);
- mouse_serial_read();
- mouse_serial_write(200);
- mouse_serial_read();
- mouse_serial_write(0xf3);
- mouse_serial_read();
- mouse_serial_write(100);
- mouse_serial_read();
- mouse_serial_write(0xf3);
- mouse_serial_read();
- mouse_serial_write(80);
- mouse_serial_read();
- mouse_serial_write(0xf2);
- mouse_serial_read();
- status = (u8)mouse_serial_read();
- if (status == 3) {
- }
- /* printf("Scrollwheel support!\n"); */
-
- // Activate 4th and 5th mouse buttons
- mouse_serial_write(0xf2);
- mouse_serial_read();
- mouse_serial_read();
- mouse_serial_write(0xf3);
- mouse_serial_read();
- mouse_serial_write(200);
- mouse_serial_read();
- mouse_serial_write(0xf3);
- mouse_serial_read();
- mouse_serial_write(200);
- mouse_serial_read();
- mouse_serial_write(0xf3);
- mouse_serial_read();
- mouse_serial_write(80);
- mouse_serial_read();
- mouse_serial_write(0xf2);
- mouse_serial_read();
- status = (u8)mouse_serial_read();
- if (status == 4) {
- }
- /* printf("4th and 5th mouse button support!\n"); */
-
- /* TODO: Fix mouse laggyness
- mouse_serial_write(0xE8);
- mouse_serial_read();
- mouse_serial_write(0x03);
- mouse_serial_read();
- mouse_serial_write(0xF3);
- mouse_serial_read();
- mouse_serial_write(200);
- mouse_serial_read(); */
+ ps2_write_device(device, 0xf6);
+ ps2_read_data();
// Enable mouse
- mouse_serial_write(0xf4);
- mouse_serial_read();
+ ps2_write_device(device, 0xf4);
+ ps2_read_data();
// Setup the mouse handler
irq_install_handler(12, mouse_handler);
queue = stack_new();
- struct vfs_dev *dev = zalloc(sizeof(*dev));
- dev->name = strdup("mouse");
- dev->type = DEV_CHAR;
+ struct io_dev *dev = zalloc(sizeof(*dev));
dev->read = mouse_read;
- /* device_add(dev); */
- dev_id = dev->id;
+ dev->ready = mouse_ready;
+ io_add(IO_MOUSE, dev);
}
diff --git a/kernel/drivers/ps2/ps2.c b/kernel/drivers/ps2/ps2.c
index 1c73855..7045fbe 100644
--- a/kernel/drivers/ps2/ps2.c
+++ b/kernel/drivers/ps2/ps2.c
@@ -86,6 +86,13 @@ CLEAR static u8 ps2_write_config(struct ps2_config config)
#define PS2_TYPE_TRANSLATION_KEYBOARD2 0xabc1
#define PS2_TYPE_STANDARD_KEYBOARD 0xab83
+#define PS2_KEYBOARD(type) \
+ ((type) == PS2_TYPE_TRANSLATION_KEYBOARD1 || (type) == PS2_TYPE_TRANSLATION_KEYBOARD2 || \
+ (type) == PS2_TYPE_STANDARD_KEYBOARD)
+#define PS2_MOUSE(type) \
+ ((type) == PS2_TYPE_STANDARD_MOUSE || (type) == PS2_TYPE_WHEEL_MOUSE || \
+ (type) == PS2_TYPE_BUTTON_MOUSE)
+
PROTECTED static struct {
u8 detected : 1;
struct {
@@ -98,7 +105,7 @@ PROTECTED static struct {
} second;
} info = { 0 };
-CLEAR static u8 ps2_write_device(u8 device, u8 data)
+CLEAR u8 ps2_write_device(u8 device, u8 data)
{
u8 resp = PS2_RESEND;
for (u8 i = 0; resp == PS2_RESEND && i < 3; i++) {
@@ -114,44 +121,62 @@ CLEAR static u8 ps2_write_device(u8 device, u8 data)
return 1;
}
-CLEAR static u8 ps2_device_keyboard(u16 type)
+CLEAR static u8 ps2_device_keyboard(void)
{
- return type == PS2_TYPE_TRANSLATION_KEYBOARD1 || type == PS2_TYPE_TRANSLATION_KEYBOARD2 ||
- type == PS2_TYPE_STANDARD_KEYBOARD;
+ if (!info.detected)
+ return U8_MAX;
+
+ if (info.first.exists && PS2_KEYBOARD(info.first.type))
+ return 0;
+ else if (info.second.exists && PS2_KEYBOARD(info.second.type))
+ return 1;
+
+ return U8_MAX;
}
-CLEAR static u8 ps2_device_mouse(u16 type)
+CLEAR static u8 ps2_device_mouse(void)
{
- return type == PS2_TYPE_STANDARD_MOUSE || type == PS2_TYPE_WHEEL_MOUSE ||
- type == PS2_TYPE_BUTTON_MOUSE;
+ if (!info.detected)
+ return U8_MAX;
+
+ if (info.first.exists && PS2_MOUSE(info.first.type))
+ return 0;
+ else if (info.second.exists && PS2_MOUSE(info.second.type))
+ return 1;
+
+ return U8_MAX;
}
-CLEAR u8 ps2_keyboard_support(void)
+CLEAR u8 ps2_keyboard_detect(void)
{
if (!info.detected)
- return 0;
+ return U8_MAX;
- // Find, reset and self-test
- if ((info.first.exists && ps2_device_keyboard(info.first.type) &&
- ps2_write_device(0, 0xff)) ||
- (info.second.exists && ps2_device_keyboard(info.second.type) &&
- ps2_write_device(1, 0xff)))
- return ps2_read_data() == 0xaa;
+ u8 device = ps2_device_keyboard();
+ if (device == U8_MAX)
+ return device;
- return 0;
+ ps2_write_device(device, 0xff);
+ if (ps2_read_data() == 0xaa)
+ return device;
+
+ return U8_MAX;
}
-CLEAR u8 ps2_mouse_support(void)
+CLEAR u8 ps2_mouse_detect(void)
{
if (!info.detected)
- return 0;
+ return U8_MAX;
- // Find, reset and self-test
- if ((info.first.exists && ps2_device_mouse(info.first.type) && ps2_write_device(0, 0xff)) ||
- (info.second.exists && ps2_device_mouse(info.second.type) && ps2_write_device(1, 0xff)))
- return ps2_read_data() == 0xaa;
+ u8 device = ps2_device_mouse();
+ if (device == U8_MAX)
+ return device;
- return 0;
+ ps2_write_device(device, 0xff);
+ if (ps2_read_data() == 0xaa)
+ return device;
+
+ return U8_MAX;
}
CLEAR void ps2_detect(void)
diff --git a/kernel/drivers/rtc.c b/kernel/drivers/rtc.c
index a814fa9..01d65f4 100644
--- a/kernel/drivers/rtc.c
+++ b/kernel/drivers/rtc.c
@@ -69,23 +69,3 @@ u32 rtc_stamp(void)
return timer_get() + rtc.second + rtc.minute * 60 + rtc.hour * 360 + rtc.day * 360 * 24 +
rtc.year * 360 * 24 * 365;
}
-
-static res rtc_dev_read(void *buf, u32 offset, u32 count, struct vfs_dev *dev)
-{
- UNUSED(offset);
- UNUSED(dev);
-
- u32 stamp = rtc_stamp();
- memcpy_user(buf, &stamp, MIN(count, sizeof(stamp)));
-
- return MIN(count, sizeof(stamp));
-}
-
-CLEAR void rtc_install(void)
-{
- struct vfs_dev *dev = zalloc(sizeof(*dev));
- dev->name = strdup("rtc");
- dev->type = DEV_CHAR;
- dev->read = rtc_dev_read;
- /* device_add(dev); */
-}
diff --git a/kernel/drivers/timer.c b/kernel/drivers/timer.c
index 2a7ca2a..294df84 100644
--- a/kernel/drivers/timer.c
+++ b/kernel/drivers/timer.c
@@ -3,7 +3,10 @@
#include <cpu.h>
#include <def.h>
#include <interrupts.h>
+#include <io.h>
+#include <mem.h>
#include <proc.h>
+#include <rtc.h>
#include <timer.h>
static u32 timer_ticks = 0;
@@ -42,15 +45,6 @@ void timer_wait(u32 ticks)
}
}
-// Install timer handler into IRQ0
-CLEAR void timer_install(void)
-{
- /* hpet_install(10000); // TODO: Find optimal femtosecond period */
- /* if (!hpet) */
- timer_phase(1000);
- irq_install_handler(0, timer_handler);
-}
-
CLEAR void scheduler_enable(void)
{
call_scheduler = 1;
@@ -62,3 +56,26 @@ CLEAR void scheduler_disable(void)
call_scheduler = 0;
irq_install_handler(0, timer_handler);
}
+
+static res timer_read(void *buf, u32 offset, u32 count)
+{
+ UNUSED(offset);
+
+ u32 stamp = rtc_stamp();
+ memcpy_user(buf, &stamp, MIN(count, sizeof(stamp)));
+
+ return MIN(count, sizeof(stamp));
+}
+
+// Install timer handler into IRQ0
+CLEAR void timer_install(void)
+{
+ /* hpet_install(10000); // TODO: Find optimal femtosecond period */
+ /* if (!hpet) */
+ timer_phase(1000);
+ irq_install_handler(0, timer_handler);
+
+ struct io_dev *dev = zalloc(sizeof(*dev));
+ dev->read = timer_read;
+ io_add(IO_TIMER, dev);
+}