mirror of
https://github.com/Mathewnd/Astral.git
synced 2026-10-30 22:18:21 +00:00
kernel: drop old keyboard and mouse subsystems
This commit is contained in:
@@ -1,257 +0,0 @@
|
||||
#include <kernel/keyboard.h>
|
||||
#include <kernel/alloc.h>
|
||||
#include <logging.h>
|
||||
#include <errno.h>
|
||||
#include <kernel/devfs.h>
|
||||
#include <hashtable.h>
|
||||
#include <kernel/poll.h>
|
||||
#include <kernel/usercopy.h>
|
||||
#include <kernel/init.h>
|
||||
|
||||
#define BUFFER_PACKET_CAPACITY 100
|
||||
#define BUFFER_SIZE (BUFFER_PACKET_CAPACITY * sizeof(kbpacket_t))
|
||||
|
||||
static char asciitable[] = {
|
||||
0, '\033', '1', '2', '3', '4', '5', '6', '7', '8', '9', '0', '-', '=', '\b', '\t', 'q', 'w', 'e', 'r', 't', 'y', 'u', 'i', 'o', 'p', '[', ']', '\r', 0, 'a', 's', 'd', 'f', 'g', 'h', 'j', 'k', 'l', ';', '\'', '`', 0, '\\', 'z', 'x', 'c', 'v', 'b', 'n', 'm', ',', '.', '/', 0, '*', 0, ' ', 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, '7', '8', '9', '-', '4', '5', '6', '+', '1', '2', '3', '0', '.', 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, '\r', 0, '/', 0, 0, '\r', 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
|
||||
};
|
||||
|
||||
static char asciitableupper[] = {
|
||||
0, '\033', '!', '@', '#', '$', '%', '^', '&', '*', '(', ')', '_', '+', '\b', '\t', 'Q', 'W', 'E', 'R', 'T', 'Y', 'U', 'I', 'O', 'P', '{', '}', '\r', 0, 'A', 'S', 'D', 'F', 'G', 'H', 'J', 'K', 'L', ':', '\"', '~', 0, '|', 'Z', 'X', 'C', 'V', 'B', 'N', 'M', '<', '>', '?', 0, '*', 0, ' ', 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, '7', '8', '9', '-', '4', '5', '6', '+', '1', '2', '3', '0', '.', 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, '\r', 0, '/', 0, 0, '\r', 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
|
||||
};
|
||||
|
||||
static int currentkbnum;
|
||||
keyboard_t *keyboard_console;
|
||||
|
||||
static keyboard_t *newkb() {
|
||||
keyboard_t *kb = alloc(sizeof(keyboard_t));
|
||||
if (kb == NULL)
|
||||
return NULL;
|
||||
|
||||
if (ringbuffer_init(&kb->packetbuffer, BUFFER_SIZE)) {
|
||||
free(kb);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
SEMAPHORE_INIT(&kb->semaphore, 0);
|
||||
return kb;
|
||||
}
|
||||
|
||||
static void destroykb(keyboard_t *kb) {
|
||||
ringbuffer_destroy(&kb->packetbuffer);
|
||||
free(kb);
|
||||
}
|
||||
|
||||
void keyboard_sendpacket(keyboard_t *kb, kbpacket_t *packet) {
|
||||
long ipl = interrupt_raiseipl(IPL_KEYBOARD);
|
||||
switch (packet->keycode) {
|
||||
case KEYCODE_LEFTCTRL:
|
||||
if (packet->flags & KBPACKET_FLAGS_RELEASED)
|
||||
kb->flags &= ~KBPACKET_FLAGS_LEFTCTRL;
|
||||
else
|
||||
kb->flags |= KBPACKET_FLAGS_LEFTCTRL;
|
||||
break;
|
||||
case KEYCODE_RIGHTCTRL:
|
||||
if (packet->flags & KBPACKET_FLAGS_RELEASED)
|
||||
kb->flags &= ~KBPACKET_FLAGS_RIGHTCTRL;
|
||||
else
|
||||
kb->flags |= KBPACKET_FLAGS_RIGHTCTRL;
|
||||
break;
|
||||
case KEYCODE_CAPSLOCK:
|
||||
if (packet->flags & KBPACKET_FLAGS_RELEASED)
|
||||
kb->flags &= ~KBPACKET_FLAGS_CAPSLOCK;
|
||||
else
|
||||
kb->flags |= KBPACKET_FLAGS_CAPSLOCK;
|
||||
break;
|
||||
case KEYCODE_LEFTALT:
|
||||
if (packet->flags & KBPACKET_FLAGS_RELEASED)
|
||||
kb->flags &= ~KBPACKET_FLAGS_LEFTALT;
|
||||
else
|
||||
kb->flags |= KBPACKET_FLAGS_LEFTALT;
|
||||
break;
|
||||
case KEYCODE_RIGHTALT:
|
||||
if (packet->flags & KBPACKET_FLAGS_RELEASED)
|
||||
kb->flags &= ~KBPACKET_FLAGS_RIGHTALT;
|
||||
else
|
||||
kb->flags |= KBPACKET_FLAGS_RIGHTALT;
|
||||
break;
|
||||
case KEYCODE_LEFTSHIFT:
|
||||
if (packet->flags & KBPACKET_FLAGS_RELEASED)
|
||||
kb->flags &= ~KBPACKET_FLAGS_LEFTSHIFT;
|
||||
else
|
||||
kb->flags |= KBPACKET_FLAGS_LEFTSHIFT;
|
||||
break;
|
||||
case KEYCODE_RIGHTSHIFT:
|
||||
if (packet->flags & KBPACKET_FLAGS_RELEASED)
|
||||
kb->flags &= ~KBPACKET_FLAGS_RIGHTSHIFT;
|
||||
else
|
||||
kb->flags |= KBPACKET_FLAGS_RIGHTSHIFT;
|
||||
break;
|
||||
}
|
||||
|
||||
packet->flags |= kb->flags;
|
||||
char* table = asciitable;
|
||||
|
||||
if ((packet->flags & KBPACKET_FLAGS_LEFTSHIFT) || (packet->flags & KBPACKET_FLAGS_RIGHTSHIFT))
|
||||
table = asciitableupper;
|
||||
|
||||
packet->ascii = table[packet->keycode];
|
||||
|
||||
spinlock_acquire(&kb->lock);
|
||||
|
||||
if (ringbuffer_write(&kb->packetbuffer, packet, sizeof(kbpacket_t)) != 0) {
|
||||
semaphore_signal(&kb->semaphore);
|
||||
poll_event(&kb->pollheader, POLLIN);
|
||||
}
|
||||
spinlock_release(&kb->lock);
|
||||
|
||||
spinlock_acquire(&keyboard_console->lock);
|
||||
|
||||
if (ringbuffer_write(&keyboard_console->packetbuffer, packet, sizeof(kbpacket_t)) != 0) {
|
||||
semaphore_signal(&keyboard_console->semaphore);
|
||||
poll_event(&keyboard_console->pollheader, POLLIN);
|
||||
}
|
||||
|
||||
spinlock_release(&keyboard_console->lock);
|
||||
|
||||
interrupt_loweripl(ipl);
|
||||
}
|
||||
|
||||
int keyboard_wait(keyboard_t *kb, kbpacket_t *packet) {
|
||||
int e = semaphore_wait(&kb->semaphore, true);
|
||||
if (e == SCHED_WAKEUP_REASON_INTERRUPTED)
|
||||
return e;
|
||||
long ipl = interrupt_raiseipl(IPL_KEYBOARD);
|
||||
|
||||
spinlock_acquire(&kb->lock);
|
||||
__assert(ringbuffer_read(&kb->packetbuffer, packet, sizeof(kbpacket_t)) == sizeof(kbpacket_t));
|
||||
spinlock_release(&kb->lock);
|
||||
|
||||
interrupt_loweripl(ipl);
|
||||
return 0;
|
||||
}
|
||||
|
||||
bool keyboard_get(keyboard_t *kb, kbpacket_t *packet) {
|
||||
if (semaphore_test(&kb->semaphore) == false)
|
||||
return false;
|
||||
|
||||
long ipl = interrupt_raiseipl(IPL_KEYBOARD);
|
||||
|
||||
spinlock_acquire(&kb->lock);
|
||||
bool ok = ringbuffer_read(&kb->packetbuffer, packet, sizeof(kbpacket_t)) == sizeof(kbpacket_t);
|
||||
spinlock_release(&kb->lock);
|
||||
|
||||
interrupt_loweripl(ipl);
|
||||
return ok;
|
||||
}
|
||||
|
||||
static hashtable_t kbtable;
|
||||
static mutex_t tablelock;
|
||||
|
||||
static keyboard_t* getkb(int kb) {
|
||||
void *kbp = NULL;
|
||||
MUTEX_ACQUIRE(&tablelock);
|
||||
hashtable_get(&kbtable, &kbp, &kb, sizeof(kb));
|
||||
MUTEX_RELEASE(&tablelock);
|
||||
return kbp;
|
||||
}
|
||||
|
||||
static int read(int minor, iovec_iterator_t *iovec_iterator, size_t size, uintmax_t offset, int flags, size_t *readc) {
|
||||
keyboard_t *kb = getkb(minor);
|
||||
if (kb == NULL)
|
||||
return ENODEV;
|
||||
|
||||
*readc = 0;
|
||||
size_t count = size / sizeof(kbpacket_t);
|
||||
if (count == 0)
|
||||
return 0;
|
||||
|
||||
for (int i = 0; i < count; ++i) {
|
||||
kbpacket_t packet;
|
||||
|
||||
bool ok = keyboard_get(kb, &packet);
|
||||
if (ok == false) {
|
||||
if (i == 0 && (flags & V_FFLAGS_NONBLOCKING) == 0) {
|
||||
int error = keyboard_wait(kb, &packet);
|
||||
if (error)
|
||||
return error;
|
||||
|
||||
error = iovec_iterator_copy_from_buffer(iovec_iterator, &packet, sizeof(kbpacket_t));
|
||||
|
||||
*readc += error ? 0 : sizeof(kbpacket_t);
|
||||
return error;
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
int error = iovec_iterator_copy_from_buffer(iovec_iterator, &packet, sizeof(kbpacket_t));
|
||||
if (error)
|
||||
return error;
|
||||
|
||||
*readc += sizeof(kbpacket_t);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int poll(int minor, polldata_t *data, int events) {
|
||||
int revents = 0;
|
||||
keyboard_t *kb = getkb(minor);
|
||||
if (kb == NULL)
|
||||
return POLLERR;
|
||||
|
||||
long ipl = interrupt_raiseipl(IPL_KEYBOARD);
|
||||
spinlock_acquire(&kb->lock);
|
||||
if (events & POLLIN) {
|
||||
size_t dataleft = RINGBUFFER_DATACOUNT(&kb->packetbuffer);
|
||||
if (dataleft)
|
||||
revents |= POLLIN;
|
||||
}
|
||||
|
||||
if (revents == 0 && data)
|
||||
poll_add(&kb->pollheader, data, events);
|
||||
|
||||
spinlock_release(&kb->lock);
|
||||
interrupt_loweripl(ipl);
|
||||
|
||||
return revents;
|
||||
}
|
||||
|
||||
static devops_t devops = {
|
||||
.read = read,
|
||||
.poll = poll
|
||||
};
|
||||
|
||||
keyboard_t *keyboard_new() {
|
||||
keyboard_t *kb = newkb();
|
||||
if (kb == NULL)
|
||||
return NULL;
|
||||
|
||||
MUTEX_ACQUIRE(&tablelock);
|
||||
|
||||
if (hashtable_set(&kbtable, kb, ¤tkbnum, sizeof(currentkbnum), true)) {
|
||||
MUTEX_RELEASE(&tablelock);
|
||||
destroykb(kb);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
MUTEX_RELEASE(&tablelock);
|
||||
|
||||
char name[12];
|
||||
snprintf(name, 12, "keyboard%d", currentkbnum);
|
||||
|
||||
__assert(devfs_register(&devops, name, V_TYPE_CHDEV, DEV_MAJOR_KEYBOARD, currentkbnum, 0666, NULL) == 0);
|
||||
|
||||
POLL_INITHEADER(&kb->pollheader);
|
||||
++currentkbnum;
|
||||
return kb;
|
||||
}
|
||||
|
||||
void keyboard_init() {
|
||||
MUTEX_INIT(&tablelock);
|
||||
keyboard_console = newkb();
|
||||
__assert(keyboard_console);
|
||||
__assert(hashtable_init(&kbtable, 20) == 0);
|
||||
}
|
||||
|
||||
INIT_ROUTINE_DEFINE(keyboard, INIT_ROUTINE_FLAGS_NONE, keyboard_init, bsp_early);
|
||||
@@ -1,183 +0,0 @@
|
||||
#include <kernel/mouse.h>
|
||||
#include <kernel/alloc.h>
|
||||
#include <logging.h>
|
||||
#include <errno.h>
|
||||
#include <kernel/devfs.h>
|
||||
#include <hashtable.h>
|
||||
#include <kernel/poll.h>
|
||||
|
||||
#define BUFFER_PACKET_CAPACITY 100
|
||||
#define BUFFER_SIZE (BUFFER_PACKET_CAPACITY * sizeof(mousepacket_t))
|
||||
|
||||
static int currentnum;
|
||||
|
||||
static mouse_t *newmouse() {
|
||||
mouse_t *mouse = alloc(sizeof(mouse_t));
|
||||
if (mouse == NULL)
|
||||
return NULL;
|
||||
|
||||
if (ringbuffer_init(&mouse->packetbuffer, BUFFER_SIZE)) {
|
||||
free(mouse);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
POLL_INITHEADER(&mouse->pollheader);
|
||||
SPINLOCK_INIT(mouse->lock);
|
||||
MUTEX_INIT(&mouse->readmutex);
|
||||
|
||||
return mouse;
|
||||
}
|
||||
|
||||
static void destroymouse(mouse_t *mouse) {
|
||||
ringbuffer_destroy(&mouse->packetbuffer);
|
||||
free(mouse);
|
||||
}
|
||||
|
||||
void mouse_packet(mouse_t *mouse, mousepacket_t *packet) {
|
||||
int oldipl = interrupt_raiseipl(IPL_MOUSE);
|
||||
spinlock_acquire(&mouse->lock);
|
||||
|
||||
ringbuffer_write(&mouse->packetbuffer, packet, sizeof(mousepacket_t));
|
||||
poll_event(&mouse->pollheader, POLLIN);
|
||||
|
||||
spinlock_release(&mouse->lock);
|
||||
interrupt_loweripl(oldipl);
|
||||
}
|
||||
|
||||
HASHTABLE_DEFINE_STATIC(mousetable, 10);
|
||||
static MUTEX_DEFINE(tablelock);
|
||||
|
||||
static mouse_t* getmouse(int mouse) {
|
||||
void *mousep = NULL;
|
||||
MUTEX_ACQUIRE(&tablelock);
|
||||
hashtable_get(&mousetable, &mousep, &mouse, sizeof(mouse));
|
||||
MUTEX_RELEASE(&tablelock);
|
||||
return mousep;
|
||||
}
|
||||
|
||||
static int internalpoll(mouse_t *mouse, polldata_t *data, int events) {
|
||||
int revents = 0;
|
||||
|
||||
if ((events & POLLIN) && RINGBUFFER_DATACOUNT(&mouse->packetbuffer))
|
||||
revents |= POLLIN;
|
||||
|
||||
if (revents == 0 && data)
|
||||
poll_add(&mouse->pollheader, data, events);
|
||||
|
||||
return revents;
|
||||
}
|
||||
|
||||
static int read(int minor, iovec_iterator_t *iovec_iterator, size_t size, uintmax_t offset, int flags, size_t *readc) {
|
||||
mouse_t *mouse = getmouse(minor);
|
||||
if (mouse == NULL)
|
||||
return ENODEV;
|
||||
|
||||
int error = 0;
|
||||
*readc = 0;
|
||||
size_t packetcount = size / sizeof(mousepacket_t);
|
||||
if (packetcount == 0)
|
||||
return error;
|
||||
|
||||
MUTEX_ACQUIRE(&mouse->readmutex);
|
||||
// wait until there is data to read or return if nonblock
|
||||
for (;;) {
|
||||
polldesc_t desc = {0};
|
||||
error = poll_initdesc(&desc, 1);
|
||||
if (error)
|
||||
goto leave;
|
||||
|
||||
// disable interrupts here as to not lose any interrupts
|
||||
// and because we cannot sleep with a raised ipl
|
||||
bool oldintstatus = interrupt_set(false);
|
||||
spinlock_acquire(&mouse->lock);
|
||||
|
||||
int revents = internalpoll(mouse, &desc.data[0], POLLIN);
|
||||
|
||||
if (revents) {
|
||||
spinlock_release(&mouse->lock);
|
||||
interrupt_set(oldintstatus);
|
||||
poll_leave(&desc);
|
||||
poll_destroydesc(&desc);
|
||||
break;
|
||||
}
|
||||
|
||||
if (revents == 0 && flags & V_FFLAGS_NONBLOCKING) {
|
||||
spinlock_release(&mouse->lock);
|
||||
interrupt_set(oldintstatus);
|
||||
poll_leave(&desc);
|
||||
poll_destroydesc(&desc);
|
||||
error = EAGAIN;
|
||||
goto leave;
|
||||
}
|
||||
|
||||
spinlock_release(&mouse->lock);
|
||||
error = poll_dowait(&desc, 0);
|
||||
interrupt_set(oldintstatus);
|
||||
|
||||
poll_leave(&desc);
|
||||
poll_destroydesc(&desc);
|
||||
|
||||
if (error)
|
||||
goto leave;
|
||||
}
|
||||
|
||||
*readc = iovec_iterator_read_from_ringbuffer(iovec_iterator, &mouse->packetbuffer, sizeof(mousepacket_t) * packetcount);
|
||||
if (*readc == RINGBUFFER_USER_COPY_FAILED) {
|
||||
error = EFAULT;
|
||||
goto leave;
|
||||
}
|
||||
|
||||
__assert(*readc);
|
||||
|
||||
leave:
|
||||
MUTEX_RELEASE(&mouse->readmutex);
|
||||
|
||||
return error;
|
||||
}
|
||||
|
||||
static int poll(int minor, polldata_t *data, int events) {
|
||||
mouse_t *mouse = getmouse(minor);
|
||||
if (mouse == NULL)
|
||||
return POLLERR;
|
||||
|
||||
MUTEX_ACQUIRE(&mouse->readmutex);
|
||||
|
||||
long ipl = interrupt_raiseipl(IPL_MOUSE);
|
||||
spinlock_acquire(&mouse->lock);
|
||||
int revents = internalpoll(mouse, data, events);
|
||||
spinlock_release(&mouse->lock);
|
||||
interrupt_loweripl(ipl);
|
||||
|
||||
MUTEX_RELEASE(&mouse->readmutex);
|
||||
|
||||
return revents;
|
||||
}
|
||||
|
||||
static devops_t devops = {
|
||||
.read = read,
|
||||
.poll = poll
|
||||
};
|
||||
|
||||
mouse_t *mouse_new() {
|
||||
mouse_t *mouse = newmouse();
|
||||
if (mouse == NULL)
|
||||
return NULL;
|
||||
|
||||
MUTEX_ACQUIRE(&tablelock);
|
||||
|
||||
if (hashtable_set(&mousetable, mouse, ¤tnum, sizeof(currentnum), true)) {
|
||||
MUTEX_RELEASE(&tablelock);
|
||||
destroymouse(mouse);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
MUTEX_RELEASE(&tablelock);
|
||||
|
||||
char name[12];
|
||||
|
||||
snprintf(name, 12, "mouse%d", currentnum);
|
||||
__assert(devfs_register(&devops, name, V_TYPE_CHDEV, DEV_MAJOR_MOUSE, currentnum, 0666, NULL) == 0);
|
||||
|
||||
++currentnum;
|
||||
return mouse;
|
||||
}
|
||||
Reference in New Issue
Block a user