Minimal device system, implement terminal device and libterminal
All checks were successful
Build documentation / build-and-deploy (push) Successful in 55s
All checks were successful
Build documentation / build-and-deploy (push) Successful in 55s
This commit is contained in:
1
kernel/device/.gitignore
vendored
Normal file
1
kernel/device/.gitignore
vendored
Normal file
@@ -0,0 +1 @@
|
||||
*.o
|
||||
72
kernel/device/device.c
Normal file
72
kernel/device/device.c
Normal file
@@ -0,0 +1,72 @@
|
||||
#include <device/device.h>
|
||||
#include <device/terminal.h>
|
||||
#include <libk/fieldlengthof.h>
|
||||
#include <libk/rbtree.h>
|
||||
#include <libk/std.h>
|
||||
#include <libk/string.h>
|
||||
#include <m/terminal_device.h>
|
||||
#include <mm/liballoc.h>
|
||||
#include <sync/spin_lock.h>
|
||||
|
||||
static struct rb_node_link* device_tree = NULL;
|
||||
static spin_lock_t device_tree_lock;
|
||||
|
||||
struct device* device_create (int id, device_op_func_t* ops, size_t ops_len,
|
||||
bool (*init) (void* arg), void (*fini) (void), void* arg) {
|
||||
if (ops_len >= fieldlengthof (struct device, ops))
|
||||
return NULL;
|
||||
|
||||
struct device* device = malloc (sizeof (*device));
|
||||
|
||||
if (device == NULL)
|
||||
return NULL;
|
||||
|
||||
memset (device, 0, sizeof (*device));
|
||||
|
||||
device->id = id;
|
||||
device->init = init;
|
||||
device->fini = fini;
|
||||
memcpy (device->ops, ops, ops_len * sizeof (device_op_func_t));
|
||||
|
||||
if (!device->init (arg)) {
|
||||
free (device);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
spin_lock (&device_tree_lock);
|
||||
rbtree_insert (struct device, &device_tree, &device->device_tree_link, device_tree_link, id);
|
||||
spin_unlock (&device_tree_lock);
|
||||
|
||||
return device;
|
||||
}
|
||||
|
||||
void device_delete (struct device* device) {
|
||||
spin_lock (&device_tree_lock);
|
||||
spin_lock (&device->lock);
|
||||
|
||||
rbtree_delete (&device_tree, &device->device_tree_link);
|
||||
|
||||
spin_unlock (&device->lock);
|
||||
spin_unlock (&device_tree_lock);
|
||||
|
||||
device->fini ();
|
||||
}
|
||||
|
||||
struct device* device_find (int id) {
|
||||
struct device* device = NULL;
|
||||
|
||||
spin_lock (&device_tree_lock);
|
||||
rbtree_find (struct device, &device_tree, id, device, device_tree_link, id);
|
||||
spin_unlock (&device_tree_lock);
|
||||
|
||||
return device;
|
||||
}
|
||||
|
||||
void devices_init (void) {
|
||||
{
|
||||
device_op_func_t ops[] = {
|
||||
[TERMINAL_PUTSTR] = &terminal_putstr,
|
||||
};
|
||||
device_create (1, ops, lengthof (ops), &terminal_init, &terminal_fini, NULL);
|
||||
}
|
||||
}
|
||||
24
kernel/device/device.h
Normal file
24
kernel/device/device.h
Normal file
@@ -0,0 +1,24 @@
|
||||
#ifndef _KERNEL_DEVICE_DEVICE_H
|
||||
#define _KERNEL_DEVICE_DEVICE_H
|
||||
|
||||
#include <libk/rbtree.h>
|
||||
#include <sync/spin_lock.h>
|
||||
|
||||
typedef int (*device_op_func_t) (void* a1, void* a2, void* a3, void* a4);
|
||||
|
||||
struct device {
|
||||
int id;
|
||||
device_op_func_t ops[32];
|
||||
spin_lock_t lock;
|
||||
struct rb_node_link device_tree_link;
|
||||
bool (*init) (void* arg);
|
||||
void (*fini) (void);
|
||||
};
|
||||
|
||||
struct device* device_create (int id, device_op_func_t* ops, size_t ops_len,
|
||||
bool (*init) (void* arg), void (*fini) (void), void* arg);
|
||||
struct device* device_find (int id);
|
||||
void device_delete (struct device* device);
|
||||
void devices_init (void);
|
||||
|
||||
#endif // _KERNEL_DEVICE_DEVICE_H
|
||||
5
kernel/device/src.mk
Normal file
5
kernel/device/src.mk
Normal file
@@ -0,0 +1,5 @@
|
||||
c += device/device.c \
|
||||
device/terminal.c
|
||||
|
||||
o += device/device.o \
|
||||
device/terminal.o
|
||||
44
kernel/device/terminal.c
Normal file
44
kernel/device/terminal.c
Normal file
@@ -0,0 +1,44 @@
|
||||
#include <Flanterm/src/flanterm.h>
|
||||
#include <Flanterm/src/flanterm_backends/fb.h>
|
||||
#include <device/terminal.h>
|
||||
#include <libk/std.h>
|
||||
#include <limine/requests.h>
|
||||
#include <m/status.h>
|
||||
#include <mm/liballoc.h>
|
||||
#include <sys/debug.h>
|
||||
|
||||
struct flanterm_context* ft_ctx;
|
||||
|
||||
void* ft_malloc (size_t n) { return malloc (n); }
|
||||
|
||||
void ft_free (void* ptr, size_t size) {
|
||||
(void)size;
|
||||
free (ptr);
|
||||
}
|
||||
|
||||
bool terminal_init (void* arg) {
|
||||
(void)arg;
|
||||
|
||||
struct limine_framebuffer_response* fb_r = limine_framebuffer_request.response;
|
||||
struct limine_framebuffer* fb = fb_r->framebuffers[0];
|
||||
|
||||
ft_ctx = flanterm_fb_init (&ft_malloc, &ft_free, fb->address, fb->width, fb->height, fb->pitch,
|
||||
fb->red_mask_size, fb->red_mask_shift, fb->green_mask_size,
|
||||
fb->green_mask_shift, fb->blue_mask_size, fb->blue_mask_shift, NULL,
|
||||
NULL, NULL, NULL, NULL, NULL, NULL, NULL, 0, 0, 1, 0, 0, 0, 0);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void terminal_fini (void) {}
|
||||
|
||||
int terminal_putstr (void* a1, void* a2, void* a3, void* a4) {
|
||||
(void)a2, (void)a3, (void)a4;
|
||||
|
||||
char* string = (char*)a1;
|
||||
size_t* len = (size_t*)a2;
|
||||
|
||||
flanterm_write (ft_ctx, string, *len);
|
||||
|
||||
return ST_OK;
|
||||
}
|
||||
8
kernel/device/terminal.h
Normal file
8
kernel/device/terminal.h
Normal file
@@ -0,0 +1,8 @@
|
||||
#ifndef _KERNEL_DEVICE_TERMINAL_H
|
||||
#define _KERNEL_DEVICE_TERMINAL_H
|
||||
|
||||
bool terminal_init (void* arg);
|
||||
void terminal_fini (void);
|
||||
int terminal_putstr (void* a1, void* a2, void* a3, void* a4);
|
||||
|
||||
#endif // _KERNEL_DEVICE_TERMINAL_H
|
||||
Reference in New Issue
Block a user