Clean up PS/2 keyboard driver, new IPC mechanism MBus (message bus)
This commit is contained in:
@ -7,6 +7,7 @@
|
||||
#include "sysdefs/dev.h"
|
||||
#include "proc/proc.h"
|
||||
#include "io/io.h"
|
||||
#include "ipc/mbus/mbus.h"
|
||||
#include "errors.h"
|
||||
#include "hshtb.h"
|
||||
#include "kprintf.h"
|
||||
@ -152,100 +153,31 @@ int32_t ps2kb_intr(void) {
|
||||
return c;
|
||||
}
|
||||
|
||||
typedef struct Ps2kbEvConsumer {
|
||||
struct Ps2kbEvConsumer *next;
|
||||
Proc *proc;
|
||||
RBuf rbuf;
|
||||
} Ps2kbEvConsumer;
|
||||
|
||||
struct {
|
||||
SpinLock spinlock;
|
||||
Ps2kbEvConsumer *list;
|
||||
} PS2KB_CONSUMERS = {0};
|
||||
IpcMBus *PS2KB_MBUS;
|
||||
|
||||
int32_t ps2kbdev_readch(struct Dev *dev, uint8_t *buffer, size_t len, uint64_t pid) {
|
||||
(void)dev; (void)buffer; (void)len;
|
||||
|
||||
Proc *consproc = NULL;
|
||||
spinlock_acquire(&PROCS.spinlock);
|
||||
Proc *proc, *proctmp;
|
||||
LL_FOREACH_SAFE(PROCS.procs, proc, proctmp) {
|
||||
if (proc->pid == pid) {
|
||||
consproc = proc;
|
||||
}
|
||||
}
|
||||
spinlock_release(&PROCS.spinlock);
|
||||
|
||||
if (consproc == NULL) {
|
||||
return E_INVALIDOPER;
|
||||
}
|
||||
|
||||
uint8_t b;
|
||||
int32_t r = -1;
|
||||
|
||||
spinlock_acquire(&PS2KB_CONSUMERS.spinlock);
|
||||
Ps2kbEvConsumer *cons, *constmp;
|
||||
LL_FOREACH_SAFE(PS2KB_CONSUMERS.list, cons, constmp) {
|
||||
if (cons->proc == consproc) {
|
||||
r = rbuf_pop(&cons->rbuf, &b);
|
||||
break;
|
||||
}
|
||||
}
|
||||
spinlock_release(&PS2KB_CONSUMERS.spinlock);
|
||||
|
||||
if (r == 0) {
|
||||
int32_t r = ipc_mbusconsume("ps2kb", &b, pid);
|
||||
if (r > 0) {
|
||||
return b;
|
||||
} else {
|
||||
return E_NOTYET;
|
||||
}
|
||||
}
|
||||
|
||||
#define CONSUMER_RBUF_MAX 0x400
|
||||
|
||||
int32_t ps2kbdev_attchcons(struct Dev *dev, uint8_t *buffer, size_t len, uint64_t pid) {
|
||||
(void)dev; (void)buffer; (void)len;
|
||||
|
||||
spinlock_acquire(&PROCS.spinlock);
|
||||
Proc *proc, *proctmp;
|
||||
LL_FOREACH_SAFE(PROCS.procs, proc, proctmp) {
|
||||
if (proc->pid == pid) {
|
||||
Ps2kbEvConsumer *cons = dlmalloc(sizeof(*cons));
|
||||
cons->proc = proc;
|
||||
uint8_t *buf = dlmalloc(CONSUMER_RBUF_MAX);
|
||||
rbuf_init(&cons->rbuf, buf, CONSUMER_RBUF_MAX);
|
||||
spinlock_acquire(&PS2KB_CONSUMERS.spinlock);
|
||||
LL_APPEND(PS2KB_CONSUMERS.list, cons);
|
||||
spinlock_release(&PS2KB_CONSUMERS.spinlock);
|
||||
break;
|
||||
}
|
||||
}
|
||||
spinlock_release(&PROCS.spinlock);
|
||||
return E_OK;
|
||||
return ipc_mbusattchcons("ps2kb", pid);
|
||||
}
|
||||
|
||||
void ps2kbdev_intr(void) {
|
||||
int32_t c = ps2kb_intr();
|
||||
if (c >= 0) {
|
||||
uint8_t b = c;
|
||||
spinlock_acquire(&PS2KB_CONSUMERS.spinlock);
|
||||
Ps2kbEvConsumer *cons, *constmp;
|
||||
LL_FOREACH_SAFE(PS2KB_CONSUMERS.list, cons, constmp) {
|
||||
bool found = false;
|
||||
spinlock_acquire(&PROCS.spinlock);
|
||||
Proc *proc, *proctmp;
|
||||
LL_FOREACH_SAFE(PROCS.procs, proc, proctmp) {
|
||||
if (proc == cons->proc) {
|
||||
found = true;
|
||||
}
|
||||
}
|
||||
if (!found) {
|
||||
LL_REMOVE(PS2KB_CONSUMERS.list, cons);
|
||||
}
|
||||
spinlock_release(&PROCS.spinlock);
|
||||
|
||||
rbuf_push(&cons->rbuf, b);
|
||||
}
|
||||
spinlock_release(&PS2KB_CONSUMERS.spinlock);
|
||||
ipc_mbuspublish("ps2kb", &b);
|
||||
}
|
||||
}
|
||||
|
||||
@ -255,7 +187,7 @@ void ps2kbdev_init(void) {
|
||||
Dev *ps2kbdev;
|
||||
HSHTB_ALLOC(DEVTABLE.devs, ident, "ps2kbdev", ps2kbdev);
|
||||
spinlock_init(&ps2kbdev->spinlock);
|
||||
spinlock_init(&PS2KB_CONSUMERS.spinlock);
|
||||
PS2KB_MBUS = ipc_mbusmake("ps2kb", 1, 0x100);
|
||||
ps2kbdev->fns[DEV_PS2KBDEV_READCH] = &ps2kbdev_readch;
|
||||
ps2kbdev->fns[DEV_PS2KBDEV_ATTCHCONS] = &ps2kbdev_attchcons;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user