From eb03ae91e0a79f13dcea5f7afcd3608d87b381fc Mon Sep 17 00:00:00 2001 From: apio Date: Wed, 12 Oct 2022 10:44:30 +0200 Subject: [PATCH] Devices: Add a new Serial device This device permits userspace to interface with the serial port. --- apps/src/init.c | 17 +++++++++++++++++ kernel/include/fs/devices/Serial.h | 9 +++++++++ kernel/src/fs/devices/DeviceFS.cpp | 2 ++ kernel/src/fs/devices/Serial.cpp | 25 +++++++++++++++++++++++++ 4 files changed, 53 insertions(+) create mode 100644 kernel/include/fs/devices/Serial.h create mode 100644 kernel/src/fs/devices/Serial.cpp diff --git a/apps/src/init.c b/apps/src/init.c index 007547da..732110a1 100644 --- a/apps/src/init.c +++ b/apps/src/init.c @@ -47,6 +47,23 @@ int main() return 1; } + FILE* serial = fopen("/dev/serial", "r"); + if (!serial) + { + perror("fopen"); + return 1; + } + if (fputs("Hello from init!\n", serial) < 0) + { + perror("fputs"); + return 1; + } + if (fclose(serial) < 0) + { + perror("fclose"); + return 1; + } + printf("Welcome to Luna!\n"); printf("Running as tid %ld\n\n", gettid()); diff --git a/kernel/include/fs/devices/Serial.h b/kernel/include/fs/devices/Serial.h new file mode 100644 index 00000000..79963e61 --- /dev/null +++ b/kernel/include/fs/devices/Serial.h @@ -0,0 +1,9 @@ +#pragma once +#include "fs/VFS.h" + +namespace SerialDevice +{ + VFS::Node* create_new(); + + ssize_t write(VFS::Node* node, size_t offset, size_t size, const char* buffer); +} \ No newline at end of file diff --git a/kernel/src/fs/devices/DeviceFS.cpp b/kernel/src/fs/devices/DeviceFS.cpp index 4bc698a4..e0fe053b 100644 --- a/kernel/src/fs/devices/DeviceFS.cpp +++ b/kernel/src/fs/devices/DeviceFS.cpp @@ -1,5 +1,6 @@ #include "fs/devices/DeviceFS.h" #include "fs/devices/Console.h" +#include "fs/devices/Serial.h" #include "fs/devices/Version.h" #include "std/stdlib.h" #include "std/string.h" @@ -23,6 +24,7 @@ VFS::Node* DeviceFS::get() devfs_files[devfs_file_count++] = VersionDevice::create_new(); devfs_files[devfs_file_count++] = ConsoleDevice::create_new(); + devfs_files[devfs_file_count++] = SerialDevice::create_new(); return devfs_root; } diff --git a/kernel/src/fs/devices/Serial.cpp b/kernel/src/fs/devices/Serial.cpp new file mode 100644 index 00000000..b3b5f729 --- /dev/null +++ b/kernel/src/fs/devices/Serial.cpp @@ -0,0 +1,25 @@ +#include "io/Serial.h" +#include "config.h" +#include "fs/devices/Serial.h" +#include "std/stdio.h" +#include "std/stdlib.h" +#include "std/string.h" + +VFS::Node* SerialDevice::create_new() +{ + VFS::Node* dev = new VFS::Node; + dev->write_func = SerialDevice::write; + dev->inode = 0; + dev->length = 0; + dev->type = VFS_FILE; + dev->flags = 0; + strncpy(dev->name, "serial", sizeof(dev->name)); + return dev; +} + +ssize_t SerialDevice::write(VFS::Node* node, size_t, size_t size, const char* buffer) +{ + if (!node) return -1; + Serial::write(buffer, size); + return (ssize_t)size; +} \ No newline at end of file