summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authordzwdz2022-04-10 17:37:33 +0200
committerdzwdz2022-04-10 17:37:33 +0200
commit0ed2c976cc0bf569a3901be686a467150c089896 (patch)
treefc9389db2dc2950957fcd7c59ab9565c503f81d6
parent7022f39e10551d7ab325d196884de301ddb3087e (diff)
kernel/driver: modify the ps2/serial drivers to use ring_t
-rw-r--r--src/kernel/arch/i386/driver/ps2.c23
-rw-r--r--src/kernel/arch/i386/driver/serial.c24
-rw-r--r--src/shared/container/ring.c2
3 files changed, 13 insertions, 36 deletions
diff --git a/src/kernel/arch/i386/driver/ps2.c b/src/kernel/arch/i386/driver/ps2.c
index 5121b9e..4b7eae4 100644
--- a/src/kernel/arch/i386/driver/ps2.c
+++ b/src/kernel/arch/i386/driver/ps2.c
@@ -1,31 +1,20 @@
#include <kernel/arch/i386/driver/ps2.h>
#include <kernel/arch/i386/interrupts/irq.h>
+#include <shared/container/ring.h>
#include <shared/mem.h>
#define BACKLOG_CAPACITY 64
-
-static volatile uint8_t backlog[BACKLOG_CAPACITY] = {};
-static volatile size_t backlog_size = 0;
+static volatile uint8_t backlog_buf[BACKLOG_CAPACITY];
+static volatile ring_t backlog = {(void*)backlog_buf, BACKLOG_CAPACITY, 0, 0};
bool ps2_ready(void) {
- return backlog_size > 0;
+ return ring_size((void*)&backlog) > 0;
}
void ps2_recv(uint8_t s) {
- if (backlog_size >= BACKLOG_CAPACITY) return;
- backlog[backlog_size++] = s;
+ ring_put1b((void*)&backlog, s);
}
size_t ps2_read(uint8_t *buf, size_t len) {
- if (backlog_size <= len)
- len = backlog_size;
- backlog_size -= len; /* guaranteed to never be < 0 */
- memcpy(buf, (void*)backlog, len);
-
- /* move rest of buffer back on partial reads */
- // TODO assumes that memcpy()ing into an overlapping buffer is fine, outside spec
- if (backlog_size > 0)
- memcpy((void*)backlog, (void*)backlog + len, backlog_size);
-
- return len;
+ return ring_get((void*)&backlog, buf, len);
}
diff --git a/src/kernel/arch/i386/driver/serial.c b/src/kernel/arch/i386/driver/serial.c
index 56a2f62..40d4a77 100644
--- a/src/kernel/arch/i386/driver/serial.c
+++ b/src/kernel/arch/i386/driver/serial.c
@@ -2,13 +2,13 @@
#include <kernel/arch/i386/interrupts/irq.h>
#include <kernel/arch/i386/port_io.h>
#include <kernel/panic.h>
+#include <shared/container/ring.h>
#include <shared/mem.h>
#include <stdint.h>
-
#define BACKLOG_CAPACITY 64
-static volatile uint8_t backlog[BACKLOG_CAPACITY] = {};
-static volatile size_t backlog_size = 0;
+static volatile uint8_t backlog_buf[BACKLOG_CAPACITY];
+static volatile ring_t backlog = {(void*)backlog_buf, BACKLOG_CAPACITY, 0, 0};
static const int COM1 = 0x3f8;
@@ -38,27 +38,15 @@ void serial_init(void) {
bool serial_ready(void) {
- return backlog_size > 0;
+ return ring_size((void*)&backlog) > 0;
}
void serial_irq(void) {
- if (backlog_size >= BACKLOG_CAPACITY) return;
- backlog[backlog_size++] = port_in8(COM1);
+ ring_put1b((void*)&backlog, port_in8(COM1));
}
size_t serial_read(char *buf, size_t len) {
- // copied from ps2, maybe could be made into a shared function? TODO FIFO lib
- if (backlog_size <= len)
- len = backlog_size;
- backlog_size -= len; /* guaranteed to never be < 0 */
- memcpy(buf, (void*)backlog, len);
-
- /* move rest of buffer back on partial reads */
- // TODO assumes that memcpy()ing into an overlapping buffer is fine, outside spec
- if (backlog_size > 0)
- memcpy((void*)backlog, (void*)backlog + len, backlog_size);
-
- return len;
+ return ring_get((void*)&backlog, buf, len);
}
diff --git a/src/shared/container/ring.c b/src/shared/container/ring.c
index 525fade..8c56acd 100644
--- a/src/shared/container/ring.c
+++ b/src/shared/container/ring.c
@@ -27,7 +27,7 @@ void ring_put1b(ring_t *r, uint8_t byte) {
size_t ring_get(ring_t *r, void *buf, size_t len) {
for (size_t i = 0; i < len; i++) {
- if (at_end(r)) return i;
+ if (r->_head == r->_tail || at_end(r)) return i;
((uint8_t*)buf)[i] = ((uint8_t*)r->buf)[r->_tail++];
if (r->_tail >= r->capacity) r->_tail = 0;
}