about summary refs log tree commit diff
diff options
context:
space:
mode:
authorYour Name <you@example.com>2020-06-22 22:35:13 +0200
committerYour Name <you@example.com>2020-06-23 02:15:58 +0200
commit00ab91d631444d315b55fb8658a72213604b3df6 (patch)
treeea3a1a3c995cf6b7a6b7199ff451bce4f3766224
parentGit commit clean (diff)
downloadpOS-00ab91d631444d315b55fb8658a72213604b3df6.tar.gz
pOS-00ab91d631444d315b55fb8658a72213604b3df6.tar.bz2
pOS-00ab91d631444d315b55fb8658a72213604b3df6.zip
Base: Added very basic driver support
To fully support drivers we need to implement memory management, as we
need virtual functions and heap allocation.
-rw-r--r--src/pOS/arch/x86/kernel/drivers.cpp51
-rw-r--r--src/pOS/arch/x86/kernel/drivers/exampledriver.cpp27
-rw-r--r--src/pOS/include/kernel/drivers.h46
-rw-r--r--src/pOS/include/kernel/pOS.h2
-rw-r--r--src/pOS/kernel/kernel.cpp2
5 files changed, 128 insertions, 0 deletions
diff --git a/src/pOS/arch/x86/kernel/drivers.cpp b/src/pOS/arch/x86/kernel/drivers.cpp
new file mode 100644
index 0000000..e2c6b63
--- /dev/null
+++ b/src/pOS/arch/x86/kernel/drivers.cpp
@@ -0,0 +1,51 @@
+#include <kernel/drivers.h>
+
+Driver* Drivers::toload_drivers[1]; //vec
+Driver* Drivers::loaded_drivers[1];
+
+void Drivers::add_drivers()
+{
+    #ifdef DRIVER_EXAMPLE
+    ADD_DRIVER(ExampleDriver);
+    #endif
+}
+
+int Drivers::add_driver(Driver* driver)
+{
+    dbgprintf("adding driver\n");
+    if(!driver->isloaded())
+        toload_drivers[0] = driver;
+
+    return 0;
+}
+
+int Drivers::load_drivers()
+{
+    dbgprintf("loading drivers\n");
+
+    add_drivers();
+
+    int result = 0;
+    for(Driver* driver : toload_drivers)
+    {
+        result = driver->load();
+        if(result) break;
+        //add to loaded
+    }
+
+    return result;
+}
+
+int Drivers::unload_drivers()
+{
+    dbgprintf("unloading drivers\n");
+    int result = 0;
+    for(Driver* driver : loaded_drivers)
+    {
+        result = driver->unload();
+        if(result) break;
+        //remove from loaded
+    }
+
+    return result;
+}
diff --git a/src/pOS/arch/x86/kernel/drivers/exampledriver.cpp b/src/pOS/arch/x86/kernel/drivers/exampledriver.cpp
new file mode 100644
index 0000000..0164f0d
--- /dev/null
+++ b/src/pOS/arch/x86/kernel/drivers/exampledriver.cpp
@@ -0,0 +1,27 @@
+#include <kernel/drivers.h>
+
+static bool loaded = false;
+
+int ExampleDriver::load()
+{
+    dbgprintf("loaded example\n");
+    loaded = true;
+    return 0;
+}
+
+int ExampleDriver::unload()
+{
+    dbgprintf("unloaded example\n");
+    loaded = false;
+    return 0;
+}
+
+bool ExampleDriver::isloaded()
+{
+    return loaded;
+}
+
+const char* ExampleDriver::get_name()
+{
+    return "Example";
+}
diff --git a/src/pOS/include/kernel/drivers.h b/src/pOS/include/kernel/drivers.h
new file mode 100644
index 0000000..acba524
--- /dev/null
+++ b/src/pOS/include/kernel/drivers.h
@@ -0,0 +1,46 @@
+#ifndef _DRIVER_H_
+#define _DRIVER_H_
+
+#include <kernel/debug.h>
+#include <kernel/pOS.h>
+
+class Driver
+{
+public:
+     virtual int load() = NULL;
+     virtual int unload() = NULL;
+     virtual bool isloaded() = NULL;
+     virtual const char* get_name() = NULL;
+};
+
+class Drivers
+{
+public:
+    static void add_drivers();
+    static int add_driver(Driver* driver);
+    static int load_drivers();
+    static int unload_drivers();
+
+private:
+    static Driver* toload_drivers[1]; //need vectors - mem managing
+    static Driver* loaded_drivers[1];
+};
+
+/* ALL DRIVERS */
+
+class ExampleDriver : public Driver
+{
+public:
+    int load();
+    int unload();
+    bool isloaded();
+    const char* get_name();
+};
+
+#define ADD_DRIVER(driver)          \
+    do {                            \
+    static driver drv = driver();   \
+    add_driver(&drv);               \
+    } while (0)
+
+#endif
diff --git a/src/pOS/include/kernel/pOS.h b/src/pOS/include/kernel/pOS.h
index 278b0a1..f466658 100644
--- a/src/pOS/include/kernel/pOS.h
+++ b/src/pOS/include/kernel/pOS.h
@@ -4,4 +4,6 @@
 #define DEBUG
 #define VERSION "meme"
 
+#define DRIVER_EXAMPLE
+
 #endif
diff --git a/src/pOS/kernel/kernel.cpp b/src/pOS/kernel/kernel.cpp
index 9e2db30..fd0340c 100644
--- a/src/pOS/kernel/kernel.cpp
+++ b/src/pOS/kernel/kernel.cpp
@@ -2,6 +2,7 @@
 #include <kernel/pOS.h>
 #include <kernel/time.h>
 #include <kernel/debug.h>
+#include <kernel/drivers.h>
 #include <math.h>
 #include <assert.h>
 
@@ -11,6 +12,7 @@ extern "C" int kmain(void)
     Debug::set_serial_debug(true);
     #endif
     TTY::tty_initialize();
+    Drivers::load_drivers();
 
     printf("Welcome to pOS v%s\n", VERSION);
     dbgprintf("Hi host %d!\n", pow(3, 2));