Devices: Add a new Serial device

This device permits userspace to interface with the serial port.
This commit is contained in:
apio 2022-10-12 10:44:30 +02:00
parent 89cbe5957b
commit eb03ae91e0
4 changed files with 53 additions and 0 deletions

View File

@ -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());

View File

@ -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);
}

View File

@ -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;
}

View File

@ -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;
}