Store devices as a hashtable
This commit is contained in:
18
kernel/dev/dev.c
Normal file
18
kernel/dev/dev.c
Normal 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();
|
||||
}
|
@ -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_
|
||||
|
@ -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;
|
||||
}
|
||||
|
@ -15,8 +15,6 @@ typedef struct {
|
||||
|
||||
void ps2kbdev_init(void);
|
||||
|
||||
extern Dev PS2KBDEV;
|
||||
|
||||
extern Ps2KbFastBuf PS2KB_BUF;
|
||||
|
||||
#endif // DEV_PS2KBDEV_H_
|
||||
|
@ -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;
|
||||
}
|
||||
|
@ -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_
|
||||
|
@ -29,7 +29,7 @@ enum {
|
||||
#define HSHTB_ALLOC(tb, keyfield, k, out) \
|
||||
do { \
|
||||
size_t __len = sizeof((tb)) / sizeof((tb)[0]); \
|
||||
uint32_t __h = hshtb_fnv32((k), strlen((k))); \
|
||||
uint32_t __h = hshtb_fnv32((k), hal_strlen((k))); \
|
||||
size_t __idx = __h % __len; \
|
||||
size_t __start = __idx; \
|
||||
typeof(&(tb)[0]) __tomb = NULL; \
|
||||
|
@ -6,11 +6,9 @@
|
||||
#include "spinlock/spinlock.h"
|
||||
#include "proc/proc.h"
|
||||
#include "sysdefs/devctl.h"
|
||||
#include "dev/termdev.h"
|
||||
#include "dev/ps2kbdev.h"
|
||||
#include "util/util.h"
|
||||
|
||||
Dev *DEVS = NULL;
|
||||
#include "hshtb.h"
|
||||
#include "dev/dev.h"
|
||||
|
||||
int32_t SYSCALL5(sys_devctl, devh1, cmd1, buffer1, len1, extra1) {
|
||||
uint64_t *devh = (uint64_t *)devh1;
|
||||
@ -23,16 +21,15 @@ int32_t SYSCALL5(sys_devctl, devh1, cmd1, buffer1, len1, extra1) {
|
||||
|
||||
switch (cmd) {
|
||||
case DEVCTL_GET_HANDLE: {
|
||||
Dev *founddev = NULL;
|
||||
size_t i = 0;
|
||||
Dev *dev, *devtmp;
|
||||
LL_FOREACH_SAFE_IDX(DEVS, dev, devtmp, i) {
|
||||
if (i == buffer1) {
|
||||
founddev = dev;
|
||||
break;
|
||||
}
|
||||
char *ident = (char *)buffer1;
|
||||
if (ident == NULL) {
|
||||
ret = E_INVALIDARGUMENT;
|
||||
goto done;
|
||||
}
|
||||
|
||||
Dev *founddev;
|
||||
HSHTB_GET(DEVTABLE.devs, ident, ident, founddev);
|
||||
|
||||
if (founddev == NULL) {
|
||||
ret = E_NOENTRY;
|
||||
goto done;
|
||||
|
@ -5,8 +5,6 @@
|
||||
#include "syscall.h"
|
||||
#include "dev/dev.h"
|
||||
|
||||
extern Dev *DEVS;
|
||||
|
||||
int32_t SYSCALL5(sys_devctl, devh1, cmd1, buffer1, len1, extra1);
|
||||
|
||||
#endif // SYSCALL_DEVCTL_H_
|
||||
|
@ -1,9 +1,6 @@
|
||||
#ifndef ULIB_DEV_H_
|
||||
#define ULIB_DEV_H_
|
||||
|
||||
#define DEV_TERMDEV 0
|
||||
#define DEV_PS2KBDEV 1
|
||||
|
||||
#define DEV_TERMDEV_PUTCH 0
|
||||
|
||||
#define DEV_PS2KBDEV_READCH 0
|
||||
|
@ -6,7 +6,7 @@ Dev_t ps2kbdev;
|
||||
Dev_t termdev;
|
||||
|
||||
void tb_runinitscript(void) {
|
||||
devctl(&termdev, DEVCTL_GET_HANDLE, (uint8_t *)DEV_TERMDEV, 0, 0);
|
||||
devctl(&termdev, DEVCTL_GET_HANDLE, (uint8_t *)"termdev", 0, 0);
|
||||
|
||||
char *tbargs[] = { "-m", "runfile", "-f", "base:/scripts/init.tb", "-logcmds", "yes" };
|
||||
int32_t tb = processctl(-1, PCTL_SPAWN, (uint64_t)"base:/bin/tb", (uint64_t)(char **)tbargs, ARRLEN(tbargs));
|
||||
@ -33,7 +33,7 @@ void tb_runinitscript(void) {
|
||||
|
||||
void main(void) {
|
||||
PID = (uint64_t)processctl(-1, PCTL_GETPID, 0, 0, 0);
|
||||
devctl(&ps2kbdev, DEVCTL_GET_HANDLE, (uint8_t *)DEV_PS2KBDEV, 0, 0);
|
||||
devctl(&ps2kbdev, DEVCTL_GET_HANDLE, (uint8_t *)"ps2kbdev", 0, 0);
|
||||
|
||||
tb_runinitscript();
|
||||
|
||||
|
Reference in New Issue
Block a user