aboutsummaryrefslogtreecommitdiff
path: root/kernel/drivers/keyboard.c
blob: 7f3247f7a315cda7bae3e005af3ac56a02d9ce9a (plain) (blame)
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
// MIT License, Copyright (c) 2020 Marvin Borner

#include <cpu.h>
#include <def.h>
#include <interrupts.h>
#include <mem.h>
#include <print.h>
#include <sys.h>

static struct event_keyboard *event = NULL;

static int state = 0;
static int merged = 0;
void keyboard_handler()
{
	int scancode = inb(0x60);

	// TODO: Support more than two-byte scancodes
	if (scancode == 0xe0) {
		merged = 0xe0;
		state = 1;
		return;
	} else {
		merged = scancode << 8 | merged;
	}

	// TODO: "Merge" scancode to linux keycode?
	/* printf("%x %x = %x\n", scancode, state ? 0xe0 : 0, merged); */

	if (event)
		free(event);
	event = malloc(sizeof(*event));
	event->magic = KEYBOARD_MAGIC;
	event->press = (scancode & 0x80) == 0;
	event->scancode = event->press ? scancode : scancode & ~0x80;
	//event_trigger(EVENT_KEYBOARD, event);

	state = 0;
	merged = 0;
}

void keyboard_acknowledge(void)
{
	while (inb(0x60) != 0xfa)
		;
}

void keyboard_rate(void)
{
	outb(0x60, 0xF3);
	keyboard_acknowledge();
	outb(0x60, 0x0); // Rate{00000} Delay{00} 0
}

void keyboard_install(void)
{
	//keyboard_rate(); TODO: Fix keyboard rate?
	irq_install_handler(1, keyboard_handler);
}