summaryrefslogtreecommitdiffhomepage
path: root/src/loader/device.c
blob: eb2bdd0b053625334ef7e39e340f8ac79f5e13cf (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
// MIT License, Copyright (c) 2021 Marvin Borner
// Device manager

#include <device.h>
#include <library.h>
#include <panic.h>

static struct dev devices[32] = { 0 };

static const char *device_resolve_type(enum device_type type)
{
	switch (type) {
	case DEVICE_DISK:
		return "Disk";
	case DEVICE_FB:
		return "Framebuffer";
	case DEVICE_NONE:
	default:
		return "Unknown";
	}
}

struct dev *device_get_by_id(u8 id)
{
	assert(id < COUNT(devices));
	return &devices[id];
}

struct dev *device_get_by_name(const char *name, u32 len)
{
	if (!name || !name[0])
		return NULL;

	for (u8 i = 0; i < COUNT(devices); i++) {
		struct dev *dev = &devices[i];
		if (strncmp(dev->name, name, MIN(sizeof(dev->name), len)) == 0)
			return dev;
	}

	return NULL;
}

void device_foreach(enum device_type type, u8 (*cb)(struct dev *))
{
	for (u8 i = 0; i < COUNT(devices); i++) {
		struct dev *dev = &devices[i];
		if (dev->type == type)
			if (cb(dev))
				break;
	}
}

u8 device_register(enum device_type type, char *name, u32 data,
		   s32 (*read)(void *, u32, u32, struct dev *),
		   s32 (*write)(const void *, u32, u32, struct dev *))
{
	static u8 id = 0;
	assert(++id < 0xff);

	struct dev *dev = &devices[id];
	dev->id = id;
	dev->type = type;
	dev->read = read;
	dev->write = write;
	dev->data = data;

	assert(strlen(name) < sizeof(dev->name));
	memcpy(dev->name, name, sizeof(dev->name));

	if (type == DEVICE_DISK)
		disk_detect(dev);

	return id;
}

void device_print(void)
{
	for (u8 i = 0; i < COUNT(devices); i++) {
		struct dev *dev = &devices[i];
		if (!dev->id)
			continue;
		log("[DEV] %d: %s device: %s\n", dev->id, device_resolve_type(dev->type),
		    dev->name);
	}
}