aboutsummaryrefslogtreecommitdiff
path: root/kernel/features/io.c
blob: 6042818649f5bd4870b7dc1d22ea61b3852293f2 (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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
// MIT License, Copyright (c) 2021 Marvin Borner

#include <assert.h>
#include <def.h>
#include <fb.h>
#include <interrupts.h>
#include <io.h>
#include <list.h>
#include <mm.h>
#include <proc.h>
#include <ps2.h>
#include <rand.h>
#include <str.h>
#include <timer.h>

PROTECTED static struct io_dev *io_mappings[IO_MAX] = { 0 };
PROTECTED static struct list *io_listeners[IO_MAX] = { 0 };

static u8 io_type_valid(enum io_type io)
{
	return io > IO_MIN && io < IO_MAX;
}

static struct io_dev *io_get(enum io_type io)
{
	if (!io_type_valid(io))
		return NULL;

	return io_mappings[io];
}

CLEAR void io_add(enum io_type io, struct io_dev *dev)
{
	assert(io_type_valid(io) && !io_mappings[io]);
	io_mappings[io] = dev;
}

res io_poll(u32 *devs)
{
	if (!memory_readable(devs))
		return -EFAULT;

	for (u32 *p = devs; p && memory_readable(p) && *p; p++) {
		if (!io_get(*p))
			return -ENOENT;
	}

	return -EFAULT;
}

res io_control(enum io_type io, u32 request, void *arg1, void *arg2, void *arg3)
{
	struct io_dev *dev;
	if (!(dev = io_get(io)) || !dev->control)
		return -ENOENT;

	return dev->control(request, arg1, arg2, arg3);
}

res io_write(enum io_type io, void *buf, u32 offset, u32 count)
{
	if (!memory_readable(buf))
		return -EFAULT;

	struct io_dev *dev;
	if (!(dev = io_get(io)) || !dev->write)
		return -ENOENT;

	return dev->write(buf, offset, count);
}

res io_read(enum io_type io, void *buf, u32 offset, u32 count)
{
	if (!memory_readable(buf))
		return -EFAULT;

	struct io_dev *dev;
	if (!(dev = io_get(io)) || !dev->read)
		return -ENOENT;

	if (dev->ready && !dev->ready())
		return -EAGAIN;

	return dev->read(buf, offset, count);
}

void io_block(enum io_type io, struct proc *proc, struct regs *r)
{
	assert(r->eax == SYS_IOREAD);
	assert(io_type_valid(io));
	list_add(io_listeners[io], proc);
	proc_state(proc_current(), PROC_BLOCKED);
	proc_yield(r);
}

void io_unblock(enum io_type io)
{
	struct page_dir *dir_bak;
	memory_backup_dir(&dir_bak);

	assert(io_type_valid(io));
	struct node *iterator = io_listeners[io]->head;
	while (iterator) {
		struct proc *proc = iterator->data;
		struct regs *r = &proc->regs;

		memory_switch_dir(proc->page_dir);
		r->eax = io_read(r->ebx, (void *)r->ecx, r->edx, r->esi);
		memory_switch_dir(dir_bak);

		proc_state(proc, PROC_RUNNING);
		struct node *next = iterator->next;
		list_remove(io_listeners[io], iterator);
		iterator = next;
	}
}

CLEAR void io_install(struct boot_info *boot)
{
	for (u32 i = 0; i < IO_MAX; i++)
		io_listeners[i] = list_new();

	ps2_detect();

	u8 ps2_keyboard = ps2_keyboard_detect();
	if (ps2_keyboard != U8_MAX) {
		ps2_keyboard_install(ps2_keyboard);
	}

	u8 ps2_mouse = ps2_mouse_detect();
	if (ps2_mouse != U8_MAX) {
		ps2_mouse_install(ps2_mouse);
	}

	timer_install();
	fb_install(boot->vid);
}