Store devices as a hashtable

This commit is contained in:
2025-10-03 23:47:58 +02:00
parent 18d646ff8b
commit 04a4b1395c
11 changed files with 53 additions and 38 deletions

18
kernel/dev/dev.c Normal file
View File

@ -0,0 +1,18 @@
#include <stdint.h>
#include <stddef.h>
#include "spinlock/spinlock.h"
#include "dev.h"
#include "hshtb.h"
#include "hal/hal.h"
#include "termdev.h"
#include "ps2kbdev.h"
DevTable DEVTABLE;
void dev_init(void) {
hal_memset(&DEVTABLE, 0, sizeof(DEVTABLE));
spinlock_init(&DEVTABLE.spinlock);
termdev_init();
ps2kbdev_init();
}

View File

@ -2,14 +2,26 @@
#define DEV_DEV_H_
#include <stdint.h>
#include "spinlock/spinlock.h"
#define DEV_FNS_MAX 32
typedef int32_t (*DevFn)(uint8_t *buffer, size_t len, void *extra);
typedef struct Dev {
struct Dev *next;
typedef struct {
int _hshtbstate;
char ident[0x100];
DevFn fns[DEV_FNS_MAX];
} Dev;
typedef struct {
SpinLock spinlock;
Dev devs[0x100];
} DevTable;
extern DevTable DEVTABLE;
void dev_init(void);
#endif // DEV_DEV_H_

View File

@ -7,9 +7,8 @@
#include "errors.h"
#include "dlmalloc/malloc.h"
#include "util/util.h"
#include "syscall/devctl.h"
#include "hshtb.h"
Dev PS2KBDEV;
Ps2KbFastBuf PS2KB_BUF;
int32_t ps2kbdev_readch(uint8_t *buffer, size_t len, void *extra) {
@ -27,12 +26,12 @@ int32_t ps2kbdev_readch(uint8_t *buffer, size_t len, void *extra) {
}
void ps2kbdev_init(void) {
hal_memset(&PS2KBDEV, 0, sizeof(PS2KBDEV));
PS2KBDEV.fns[0] = &ps2kbdev_readch;
const int bufsz = 0x1000;
uint8_t *buf = dlmalloc(bufsz);
rbuf_init(&PS2KB_BUF.rbuf, buf, bufsz);
PS2KB_BUF.init = true;
LL_APPEND(DEVS, &PS2KBDEV);
Dev *ps2kbdev;
HSHTB_ALLOC(DEVTABLE.devs, ident, "ps2kbdev", ps2kbdev);
ps2kbdev->fns[0] = &ps2kbdev_readch;
}

View File

@ -15,8 +15,6 @@ typedef struct {
void ps2kbdev_init(void);
extern Dev PS2KBDEV;
extern Ps2KbFastBuf PS2KB_BUF;
#endif // DEV_PS2KBDEV_H_

View File

@ -6,9 +6,7 @@
#include "dev.h"
#include "errors.h"
#include "util/util.h"
#include "syscall/devctl.h"
Dev TERMDEV;
#include "hshtb.h"
int32_t termdev_putch(uint8_t *buffer, size_t len, void *extra) {
kprintf("%.*s", (int)len, (char *)buffer);
@ -16,7 +14,7 @@ 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);
Dev *termdev = NULL;
HSHTB_ALLOC(DEVTABLE.devs, ident, "termdev", termdev);
termdev->fns[0] = &termdev_putch;
}

View File

@ -8,6 +8,4 @@
int32_t termdev_putch(uint8_t *buffer, size_t len, void *extra);
void termdev_init(void);
extern Dev TERMDEV;
#endif // DEV_TERMDEV_H_