summaryrefslogtreecommitdiff
path: root/src/kernel/arch/amd64/driver/video.c
blob: 390e8dd12020d86390507eab7eba717085c3c21e (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
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
#include <camellia/errno.h>
#include <camellia/fsutil.h>
#include <kernel/arch/amd64/driver/driver.h>
#include <kernel/arch/amd64/driver/util.h>
#include <kernel/panic.h>
#include <kernel/proc.h>
#include <kernel/vfs/request.h>
#include <shared/mem.h>
#include <shared/printf.h>

static GfxInfo fb;
static char namebuf[64];
static size_t namelen;

enum {
	H_ROOT,
	H_FB,
};

static int handle(VfsReq *req) {
	switch (req->type) {
		case VFSOP_OPEN:
			if (!req->input.kern) panic_invalid_state();
			if (req->input.len == 0) {
				return H_ROOT;
			} else if (req->input.len == namelen && !memcmp(req->input.buf_kern, namebuf, namelen)) {
				return H_FB;
			} else {
				return -1;
			}

		case VFSOP_READ:
			if (req->id == H_ROOT) {
				return req_readcopy(req, namebuf, namelen + 1);
			} else {
				return req_readcopy(req, fb.b, fb.size);
			}

		case VFSOP_WRITE:
			if ((long __force)req->id != H_FB) {
				return -1;
			}
			fs_normslice(&req->offset, &req->input.len, fb.size, false);
			/* parial writes ignored */
			pcpy_from(req->caller, fb.b + req->offset, req->input.buf, req->input.len);
			return req->input.len;

		case VFSOP_GETSIZE:
			return fb.size;

		default:
			return -1;
	}
}

static void accept(VfsReq *req) {
	if (req->caller) {
		vfsreq_finish_short(req, handle(req));
	} else {
		vfsreq_finish_short(req, -1);
	}
}

void video_init(GfxInfo fb_) {
	fb = fb_;
	snprintf(namebuf, sizeof namebuf, "%ux%ux%u", fb.width, fb.height, fb.bpp);
	namelen = strlen(namebuf);
	vfs_root_register("/kdev/video/", accept);
}