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_
|
#define DEV_DEV_H_
|
||||||
|
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
|
#include "spinlock/spinlock.h"
|
||||||
|
|
||||||
#define DEV_FNS_MAX 32
|
#define DEV_FNS_MAX 32
|
||||||
|
|
||||||
typedef int32_t (*DevFn)(uint8_t *buffer, size_t len, void *extra);
|
typedef int32_t (*DevFn)(uint8_t *buffer, size_t len, void *extra);
|
||||||
|
|
||||||
typedef struct Dev {
|
typedef struct {
|
||||||
struct Dev *next;
|
int _hshtbstate;
|
||||||
|
char ident[0x100];
|
||||||
DevFn fns[DEV_FNS_MAX];
|
DevFn fns[DEV_FNS_MAX];
|
||||||
} Dev;
|
} Dev;
|
||||||
|
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
SpinLock spinlock;
|
||||||
|
Dev devs[0x100];
|
||||||
|
} DevTable;
|
||||||
|
|
||||||
|
extern DevTable DEVTABLE;
|
||||||
|
|
||||||
|
void dev_init(void);
|
||||||
|
|
||||||
#endif // DEV_DEV_H_
|
#endif // DEV_DEV_H_
|
||||||
|
@ -7,9 +7,8 @@
|
|||||||
#include "errors.h"
|
#include "errors.h"
|
||||||
#include "dlmalloc/malloc.h"
|
#include "dlmalloc/malloc.h"
|
||||||
#include "util/util.h"
|
#include "util/util.h"
|
||||||
#include "syscall/devctl.h"
|
#include "hshtb.h"
|
||||||
|
|
||||||
Dev PS2KBDEV;
|
|
||||||
Ps2KbFastBuf PS2KB_BUF;
|
Ps2KbFastBuf PS2KB_BUF;
|
||||||
|
|
||||||
int32_t ps2kbdev_readch(uint8_t *buffer, size_t len, void *extra) {
|
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) {
|
void ps2kbdev_init(void) {
|
||||||
hal_memset(&PS2KBDEV, 0, sizeof(PS2KBDEV));
|
|
||||||
PS2KBDEV.fns[0] = &ps2kbdev_readch;
|
|
||||||
|
|
||||||
const int bufsz = 0x1000;
|
const int bufsz = 0x1000;
|
||||||
uint8_t *buf = dlmalloc(bufsz);
|
uint8_t *buf = dlmalloc(bufsz);
|
||||||
rbuf_init(&PS2KB_BUF.rbuf, buf, bufsz);
|
rbuf_init(&PS2KB_BUF.rbuf, buf, bufsz);
|
||||||
PS2KB_BUF.init = true;
|
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);
|
void ps2kbdev_init(void);
|
||||||
|
|
||||||
extern Dev PS2KBDEV;
|
|
||||||
|
|
||||||
extern Ps2KbFastBuf PS2KB_BUF;
|
extern Ps2KbFastBuf PS2KB_BUF;
|
||||||
|
|
||||||
#endif // DEV_PS2KBDEV_H_
|
#endif // DEV_PS2KBDEV_H_
|
||||||
|
@ -6,9 +6,7 @@
|
|||||||
#include "dev.h"
|
#include "dev.h"
|
||||||
#include "errors.h"
|
#include "errors.h"
|
||||||
#include "util/util.h"
|
#include "util/util.h"
|
||||||
#include "syscall/devctl.h"
|
#include "hshtb.h"
|
||||||
|
|
||||||
Dev TERMDEV;
|
|
||||||
|
|
||||||
int32_t termdev_putch(uint8_t *buffer, size_t len, void *extra) {
|
int32_t termdev_putch(uint8_t *buffer, size_t len, void *extra) {
|
||||||
kprintf("%.*s", (int)len, (char *)buffer);
|
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) {
|
void termdev_init(void) {
|
||||||
hal_memset(&TERMDEV, 0, sizeof(TERMDEV));
|
Dev *termdev = NULL;
|
||||||
TERMDEV.fns[0] = &termdev_putch;
|
HSHTB_ALLOC(DEVTABLE.devs, ident, "termdev", termdev);
|
||||||
LL_APPEND(DEVS, &TERMDEV);
|
termdev->fns[0] = &termdev_putch;
|
||||||
}
|
}
|
||||||
|
@ -8,6 +8,4 @@
|
|||||||
int32_t termdev_putch(uint8_t *buffer, size_t len, void *extra);
|
int32_t termdev_putch(uint8_t *buffer, size_t len, void *extra);
|
||||||
void termdev_init(void);
|
void termdev_init(void);
|
||||||
|
|
||||||
extern Dev TERMDEV;
|
|
||||||
|
|
||||||
#endif // DEV_TERMDEV_H_
|
#endif // DEV_TERMDEV_H_
|
||||||
|
@ -29,7 +29,7 @@ enum {
|
|||||||
#define HSHTB_ALLOC(tb, keyfield, k, out) \
|
#define HSHTB_ALLOC(tb, keyfield, k, out) \
|
||||||
do { \
|
do { \
|
||||||
size_t __len = sizeof((tb)) / sizeof((tb)[0]); \
|
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 __idx = __h % __len; \
|
||||||
size_t __start = __idx; \
|
size_t __start = __idx; \
|
||||||
typeof(&(tb)[0]) __tomb = NULL; \
|
typeof(&(tb)[0]) __tomb = NULL; \
|
||||||
|
@ -6,11 +6,9 @@
|
|||||||
#include "spinlock/spinlock.h"
|
#include "spinlock/spinlock.h"
|
||||||
#include "proc/proc.h"
|
#include "proc/proc.h"
|
||||||
#include "sysdefs/devctl.h"
|
#include "sysdefs/devctl.h"
|
||||||
#include "dev/termdev.h"
|
|
||||||
#include "dev/ps2kbdev.h"
|
|
||||||
#include "util/util.h"
|
#include "util/util.h"
|
||||||
|
#include "hshtb.h"
|
||||||
Dev *DEVS = NULL;
|
#include "dev/dev.h"
|
||||||
|
|
||||||
int32_t SYSCALL5(sys_devctl, devh1, cmd1, buffer1, len1, extra1) {
|
int32_t SYSCALL5(sys_devctl, devh1, cmd1, buffer1, len1, extra1) {
|
||||||
uint64_t *devh = (uint64_t *)devh1;
|
uint64_t *devh = (uint64_t *)devh1;
|
||||||
@ -23,16 +21,15 @@ int32_t SYSCALL5(sys_devctl, devh1, cmd1, buffer1, len1, extra1) {
|
|||||||
|
|
||||||
switch (cmd) {
|
switch (cmd) {
|
||||||
case DEVCTL_GET_HANDLE: {
|
case DEVCTL_GET_HANDLE: {
|
||||||
Dev *founddev = NULL;
|
char *ident = (char *)buffer1;
|
||||||
size_t i = 0;
|
if (ident == NULL) {
|
||||||
Dev *dev, *devtmp;
|
ret = E_INVALIDARGUMENT;
|
||||||
LL_FOREACH_SAFE_IDX(DEVS, dev, devtmp, i) {
|
goto done;
|
||||||
if (i == buffer1) {
|
|
||||||
founddev = dev;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Dev *founddev;
|
||||||
|
HSHTB_GET(DEVTABLE.devs, ident, ident, founddev);
|
||||||
|
|
||||||
if (founddev == NULL) {
|
if (founddev == NULL) {
|
||||||
ret = E_NOENTRY;
|
ret = E_NOENTRY;
|
||||||
goto done;
|
goto done;
|
||||||
|
@ -5,8 +5,6 @@
|
|||||||
#include "syscall.h"
|
#include "syscall.h"
|
||||||
#include "dev/dev.h"
|
#include "dev/dev.h"
|
||||||
|
|
||||||
extern Dev *DEVS;
|
|
||||||
|
|
||||||
int32_t SYSCALL5(sys_devctl, devh1, cmd1, buffer1, len1, extra1);
|
int32_t SYSCALL5(sys_devctl, devh1, cmd1, buffer1, len1, extra1);
|
||||||
|
|
||||||
#endif // SYSCALL_DEVCTL_H_
|
#endif // SYSCALL_DEVCTL_H_
|
||||||
|
@ -1,9 +1,6 @@
|
|||||||
#ifndef ULIB_DEV_H_
|
#ifndef ULIB_DEV_H_
|
||||||
#define ULIB_DEV_H_
|
#define ULIB_DEV_H_
|
||||||
|
|
||||||
#define DEV_TERMDEV 0
|
|
||||||
#define DEV_PS2KBDEV 1
|
|
||||||
|
|
||||||
#define DEV_TERMDEV_PUTCH 0
|
#define DEV_TERMDEV_PUTCH 0
|
||||||
|
|
||||||
#define DEV_PS2KBDEV_READCH 0
|
#define DEV_PS2KBDEV_READCH 0
|
||||||
|
@ -6,7 +6,7 @@ Dev_t ps2kbdev;
|
|||||||
Dev_t termdev;
|
Dev_t termdev;
|
||||||
|
|
||||||
void tb_runinitscript(void) {
|
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" };
|
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));
|
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) {
|
void main(void) {
|
||||||
PID = (uint64_t)processctl(-1, PCTL_GETPID, 0, 0, 0);
|
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();
|
tb_runinitscript();
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user