blob: 697bd342923d351dd8f40fbf61e477b041435372 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
|
#include <kernel/arch/i386/driver/ps2.h>
#include <kernel/arch/i386/interrupts/irq.h>
#include <shared/mem.h>
#define BACKLOG_CAPACITY 64
static volatile uint8_t backlog[BACKLOG_CAPACITY] = {};
static volatile size_t backlog_size = 0;
void ps2_recv(uint8_t s) {
if (backlog_size >= BACKLOG_CAPACITY) return;
backlog[backlog_size++] = s;
}
size_t ps2_read(uint8_t *buf, size_t len) {
irq_interrupt_flag(true);
while (backlog_size == 0)
asm("hlt" ::: "memory");
irq_interrupt_flag(false);
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;
}
|