summaryrefslogtreecommitdiff
path: root/src/user/driver
diff options
context:
space:
mode:
authordzwdz2022-07-11 21:54:51 +0200
committerdzwdz2022-07-11 21:54:51 +0200
commit2e79a2e5af3affa7a6a3becdffb1c91d89af90af (patch)
treec253761c4ec010ad14b08f9ee8dfc1a50411b30f /src/user/driver
parent6c01d9a7e34e1fccc2775b0e2187ac5e50dd4392 (diff)
user: reorganize the userland sources
Diffstat (limited to 'src/user/driver')
-rw-r--r--src/user/driver/ansiterm.c103
-rw-r--r--src/user/driver/driver.h5
-rw-r--r--src/user/driver/ps2.c90
-rw-r--r--src/user/driver/tmpfs.c95
4 files changed, 293 insertions, 0 deletions
diff --git a/src/user/driver/ansiterm.c b/src/user/driver/ansiterm.c
new file mode 100644
index 0000000..e332ca2
--- /dev/null
+++ b/src/user/driver/ansiterm.c
@@ -0,0 +1,103 @@
+#include <user/driver/driver.h>
+#include <shared/syscalls.h>
+#include <stdbool.h>
+
+struct vga_cell {
+ unsigned char c;
+ unsigned char style;
+} __attribute__((__packed__));
+static struct vga_cell vga[80 * 25];
+
+static handle_t vga_fd;
+
+static struct {int x, y;} cursor = {0};
+static bool dirty = false;
+static bool pendingFlush = false;
+
+static void flush(void) {
+ size_t off = 0;
+ /* we have to do multiple write() calls if we're behind a shitty passthrough fs
+ * i don't like this either */
+ while (off < sizeof(vga))
+ off += _syscall_write(vga_fd, (void*)vga + off, sizeof(vga) - off, off);
+ dirty = false;
+ pendingFlush = false;
+}
+
+static void scroll(void) {
+ for (size_t i = 0; i < 80 * 24; i++)
+ vga[i] = vga[i + 80];
+ for (size_t i = 80 * 24; i < 80 * 25; i++)
+ vga[i].c = ' ';
+ cursor.y--;
+ pendingFlush = true;
+}
+
+static void in_char(char c) {
+ switch (c) {
+ case '\n':
+ cursor.x = 0;
+ cursor.y++;
+ pendingFlush = true;
+ break;
+ case '\b':
+ if (--cursor.x < 0) cursor.x = 0;
+ break;
+ default:
+ vga[cursor.y * 80 + cursor.x++].c = c;
+ }
+
+ if (cursor.x >= 80) {
+ cursor.x = 0;
+ cursor.y++;
+ }
+ while (cursor.y >= 25) scroll();
+ dirty = true;
+}
+
+void ansiterm_drv(void) {
+ vga_fd = _syscall_open("/kdev/vga", 9, 0);
+ _syscall_read(vga_fd, vga, sizeof vga, 0);
+
+ // find first empty line
+ for (cursor.y = 0; cursor.y < 25; cursor.y++) {
+ for (cursor.x = 0; cursor.x < 80; cursor.x++) {
+ char c = vga[cursor.y * 80 + cursor.x].c;
+ if (c != ' ' && c != '\0') break;
+ }
+ if (cursor.x == 80) break;
+ }
+ cursor.x = 0;
+
+ for (int i = 0; i < 80 * 25; i++)
+ vga[i].style = 0x70;
+ flush();
+
+ static char buf[512];
+ struct fs_wait_response res;
+ while (!_syscall_fs_wait(buf, sizeof buf, &res)) {
+ switch (res.op) {
+ case VFSOP_OPEN:
+ if (res.flags & OPEN_CREATE) {
+ _syscall_fs_respond(NULL, -1, 0);
+ break;
+ }
+ // TODO check path
+ _syscall_fs_respond(NULL, 0, 0);
+ break;
+
+ case VFSOP_WRITE:
+ for (size_t i = 0; i < res.len; i++)
+ in_char(buf[i]);
+ /* if (pendingFlush) */ flush();
+ _syscall_fs_respond(NULL, res.len, 0);
+ break;
+
+ default:
+ _syscall_fs_respond(NULL, -1, 0);
+ break;
+ }
+ }
+
+ _syscall_exit(1);
+}
diff --git a/src/user/driver/driver.h b/src/user/driver/driver.h
new file mode 100644
index 0000000..69c4529
--- /dev/null
+++ b/src/user/driver/driver.h
@@ -0,0 +1,5 @@
+#pragma once
+
+void ansiterm_drv(void);
+void ps2_drv(void);
+void tmpfs_drv(void);
diff --git a/src/user/driver/ps2.c b/src/user/driver/ps2.c
new file mode 100644
index 0000000..b2811c3
--- /dev/null
+++ b/src/user/driver/ps2.c
@@ -0,0 +1,90 @@
+#include <user/driver/driver.h>
+#include <shared/container/ring.h>
+#include <shared/syscalls.h>
+#include <stdbool.h>
+
+
+static const char keymap_lower[] = {
+ '\0', '\0', '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', '\'', '`', '\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',
+ '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0',
+ '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0',
+};
+
+static const char keymap_upper[] = {
+ '\0', '\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',
+ '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0',
+ '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0',
+};
+
+
+static volatile uint8_t backlog_buf[16];
+static volatile ring_t backlog = {(void*)backlog_buf, sizeof backlog_buf, 0, 0};
+
+static handle_t fd;
+
+static bool keys[0x80] = {0};
+
+static void parse_scancode(uint8_t s) {
+ bool ctrl = keys[0x1D];
+ bool shift = keys[0x2A] || keys[0x36];
+ bool down = !(s & 0x80);
+ char c;
+ s &= 0x7f;
+ keys[s] = down;
+
+ c = shift ? keymap_upper[s] : keymap_lower[s];
+ if (ctrl && keymap_upper[s] >= 'A' && keymap_upper[s] <= 'Z')
+ c = keymap_upper[s] - 'A' + 1;
+ if (down && c) ring_put1b((void*)&backlog, c);
+}
+
+static void main_loop(void) {
+ static char buf[512];
+ struct fs_wait_response res;
+ int ret;
+ while (!_syscall_fs_wait(buf, sizeof buf, &res)) {
+ switch (res.op) {
+ case VFSOP_OPEN:
+ if (res.flags & OPEN_CREATE) {
+ _syscall_fs_respond(NULL, -1, 0);
+ break;
+ }
+ _syscall_fs_respond(NULL, 1, 0);
+ break;
+
+ case VFSOP_READ:
+ while (ring_size((void*)&backlog) == 0) {
+ /* read raw input until we have something to output */
+ int len = _syscall_read(fd, buf, sizeof buf, 0);
+ if (len == 0) break;
+ for (int i = 0; i < len; i++)
+ parse_scancode(buf[i]);
+ }
+ ret = ring_get((void*)&backlog, buf, res.capacity);
+ _syscall_fs_respond(buf, ret, 0);
+ break;
+
+ default:
+ _syscall_fs_respond(NULL, -1, 0);
+ break;
+ }
+ }
+}
+
+void ps2_drv(void) {
+ fd = _syscall_open("/kdev/ps2", 9, 0);
+ if (fd < 0) _syscall_exit(1);
+
+ main_loop();
+ _syscall_exit(0);
+}
diff --git a/src/user/driver/tmpfs.c b/src/user/driver/tmpfs.c
new file mode 100644
index 0000000..225204b
--- /dev/null
+++ b/src/user/driver/tmpfs.c
@@ -0,0 +1,95 @@
+#include <user/lib/malloc.h>
+#include <shared/mem.h>
+#include <shared/syscalls.h>
+#include <stddef.h>
+
+struct node {
+ const char *name;
+ size_t len;
+ struct node *next;
+};
+
+struct node *root = NULL;
+static struct node special_root;
+
+static struct node *lookup(const char *path, size_t len) {
+ for (struct node *iter = root; iter; iter = iter->next) {
+ if (iter->len == len && !memcmp(path, iter->name, len))
+ return iter;
+ }
+ return NULL;
+}
+
+static struct node *tmpfs_open(const char *path, struct fs_wait_response *res) {
+ if (res->len == 0) return NULL;
+ path++;
+ res->len--;
+
+ if (res->len == 0) return &special_root;
+
+ // no directory support (yet)
+ if (memchr(path, '/', res->len)) return NULL;
+
+ if (res->flags & OPEN_CREATE) {
+ if (lookup(path, res->len)) return NULL; /* already exists */
+ struct node *new = malloc(sizeof *new);
+ char *namebuf = malloc(res->len);
+ memcpy(namebuf, path, res->len);
+ new->name = namebuf;
+ new->len = res->len;
+ new->next = root;
+ root = new;
+ return new;
+ }
+
+ return lookup(path, res->len);
+}
+
+void tmpfs_drv(void) {
+ // TODO replace all the static allocations in drivers with mallocs
+ static char buf[512];
+ struct fs_wait_response res;
+ struct node *ptr;
+ while (!_syscall_fs_wait(buf, sizeof buf, &res)) {
+ switch (res.op) {
+ case VFSOP_OPEN:
+ ptr = tmpfs_open(buf, &res);
+ _syscall_fs_respond(ptr, ptr ? 0 : -1, 0);
+ break;
+
+ case VFSOP_READ:
+ if (res.id != &special_root) {
+ // rw unimplemented
+ _syscall_fs_respond(NULL, -1, 0);
+ break;
+ }
+ size_t buf_pos = 0;
+ size_t to_skip = res.offset;
+
+ for (struct node *iter = root; iter; iter = iter->next) {
+ if (iter->len <= to_skip) {
+ to_skip -= iter->len;
+ continue;
+ }
+
+ if (iter->len + buf_pos - to_skip >= sizeof(buf)) {
+ memcpy(buf + buf_pos, iter->name + to_skip, sizeof(buf) - buf_pos - to_skip);
+ buf_pos = sizeof(buf);
+ break;
+ }
+ memcpy(buf + buf_pos, iter->name + to_skip, iter->len - to_skip);
+ buf_pos += iter->len - to_skip;
+ buf[buf_pos++] = '\0';
+ to_skip = 0;
+ }
+ _syscall_fs_respond(buf, buf_pos, 0);
+ break;
+
+ default:
+ _syscall_fs_respond(NULL, -1, 0);
+ break;
+ }
+ }
+
+ _syscall_exit(1);
+}