Store device structs in a dynamic list

This commit is contained in:
2025-10-03 22:25:16 +02:00
parent c0178a1405
commit 57ba9ff126
10 changed files with 44 additions and 24 deletions

View File

@ -7,7 +7,8 @@
typedef int32_t (*DevFn)(uint8_t *buffer, size_t len, void *extra);
typedef struct {
typedef struct Dev {
struct Dev *next;
DevFn fns[DEV_FNS_MAX];
} Dev;

View File

@ -6,6 +6,8 @@
#include "dev.h"
#include "errors.h"
#include "dlmalloc/malloc.h"
#include "util/util.h"
#include "syscall/devctl.h"
Dev PS2KBDEV;
Ps2KbFastBuf PS2KB_BUF;
@ -32,4 +34,5 @@ void ps2kbdev_init(void) {
uint8_t *buf = dlmalloc(bufsz);
rbuf_init(&PS2KB_BUF.rbuf, buf, bufsz);
PS2KB_BUF.init = true;
LL_APPEND(DEVS, &PS2KBDEV);
}

View File

@ -5,6 +5,8 @@
#include "termdev.h"
#include "dev.h"
#include "errors.h"
#include "util/util.h"
#include "syscall/devctl.h"
Dev TERMDEV;
@ -16,4 +18,5 @@ int32_t termdev_putch(uint8_t *buffer, size_t len, void *extra) {
void termdev_init(void) {
hal_memset(&TERMDEV, 0, sizeof(TERMDEV));
TERMDEV.fns[0] = &termdev_putch;
LL_APPEND(DEVS, &TERMDEV);
}

View File

@ -10,10 +10,7 @@
#include "dev/ps2kbdev.h"
#include "util/util.h"
Dev *DEVS[] = {
[0x10] = &TERMDEV,
[0x11] = &PS2KBDEV,
};
Dev *DEVS = NULL;
int32_t SYSCALL5(sys_devctl, devh1, cmd1, buffer1, len1, extra1) {
uint64_t *devh = (uint64_t *)devh1;
@ -26,9 +23,18 @@ int32_t SYSCALL5(sys_devctl, devh1, cmd1, buffer1, len1, extra1) {
switch (cmd) {
case DEVCTL_GET_HANDLE: {
uint64_t devid = buffer1;
if (devid >= LEN(DEVS)) {
ret = E_INVALIDARGUMENT;
Dev *founddev = NULL;
size_t i = 0;
Dev *dev, *devtmp;
LL_FOREACH_SAFE_IDX(DEVS, dev, devtmp, i) {
if (i == buffer1) {
founddev = dev;
break;
}
}
if (founddev == NULL) {
ret = E_NOENTRY;
goto done;
}
@ -36,14 +42,14 @@ int32_t SYSCALL5(sys_devctl, devh1, cmd1, buffer1, len1, extra1) {
for (size_t i = 0; i < PROC_DEVHANDLES_MAX; i++) {
if (proc->devs[i] == NULL) {
found = true;
proc->devs[i] = DEVS[devid];
proc->devs[i] = founddev;
*devh = i;
break;
}
}
if (!found) {
ret = E_NOENTRY;
ret = E_NOMEMORY;
goto done;
}
} break;
@ -59,6 +65,10 @@ int32_t SYSCALL5(sys_devctl, devh1, cmd1, buffer1, len1, extra1) {
}
Dev *dev = proc->devs[*devh];
if (dev == NULL) {
ret = E_NOENTRY;
goto done;
}
ret = dev->fns[cmd]((uint8_t *)buffer1, (size_t)len1, (void *)extra1);
} break;
}

View File

@ -3,6 +3,9 @@
#include <stdint.h>
#include "syscall.h"
#include "dev/dev.h"
extern Dev *DEVS;
int32_t SYSCALL5(sys_devctl, devh1, cmd1, buffer1, len1, extra1);