1
0
mirror of https://gitlab.os-k.eu/os-k-team/kvisc.git synced 2023-08-25 14:05:46 +02:00
kvisc/vm/pc/device.c

160 lines
2.8 KiB
C
Raw Normal View History

2019-06-05 19:31:48 +02:00
// The OS/K Team licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
2019-06-21 22:19:55 +02:00
#include <pc/device.h>
2019-06-05 19:31:48 +02:00
2019-06-13 23:12:11 +02:00
//----------------------------------------------------------------------------//
//
// Add new builtin devices here
//
2019-06-26 21:25:59 +02:00
extern dev_t cpudev, memdev, clockdev, keybdev, diskdev;
2019-06-13 23:12:11 +02:00
static dev_t *arch_d[] =
{
2019-06-26 21:25:59 +02:00
&cpudev, // 0
&memdev, // 1
&clockdev, // 2
&keybdev, // 3
&diskdev, // 4
2019-06-13 23:12:11 +02:00
NULL,
};
int devinitall(ctx_t *ctx)
{
size_t it;
for (it = 0; arch_d[it] != NULL; it++) {
2019-06-19 21:41:22 +02:00
// trace("Adding device %s\n", arch_d[it]->name);
2019-06-13 23:12:11 +02:00
if (devinit(ctx, arch_d[it]) < 0)
return -1;
}
return 0;
}
int devfiniall(ctx_t *ctx)
{
int failed = 0;
while (ctx->dh)
if (devfini(ctx, ctx->dh) < 0)
failed = -1;
return failed;
}
//----------------------------------------------------------------------------//
2019-06-05 19:31:48 +02:00
dev_t *devalloc(void)
{
return calloc(sizeof(dev_t)/sizeof(ulong), sizeof(ulong));
}
void devfree(dev_t *dev)
{
assert(dev != NULL);
free(dev);
}
2019-06-13 23:12:11 +02:00
//----------------------------------------------------------------------------//
2019-06-05 19:31:48 +02:00
dev_t *devget(ctx_t *ctx, int idx)
{
if (idx < 0)
return NULL;
int i;
dev_t *dev;
for (i = 0, dev = ctx->dh; dev && i != idx; dev = dev->next, i++);
return dev;
}
void devattach(ctx_t *ctx, dev_t *dev)
{
dev_t *it;
assert(dev != NULL);
if (ctx->dh) {
for (it = ctx->dh; it->next != NULL; it = it->next);
it->next = dev;
}
else ctx->dh = dev;
}
int devinit(ctx_t *ctx, dev_t *dev)
{
if (!dev || dev->state != DEVPWOF)
return -1;
if (dev->fpwon)
dev->fpwon(ctx, dev);
else dev->state = DEVGOOD;
if (!(dev->state == DEVPLUG || dev->state == DEVGOOD))
return -1;
dev->id = random();
devattach(ctx, dev);
return 0;
}
2019-06-13 23:12:11 +02:00
//----------------------------------------------------------------------------//
2019-06-05 19:31:48 +02:00
// NEVER detach while some assembly code
// may still be running on the vm!
void devdetach(ctx_t *ctx, dev_t *dev)
{
dev_t *it;
assert(dev != NULL);
if (dev == ctx->dh) {
ctx->dh = dev->next;
}
for (it = ctx->dh; it; it = it->next) {
if (!it)
break;
if (it->next == dev) {
it->next = dev->next;
break;
}
}
}
int devfini(ctx_t *ctx, dev_t *dev)
{
if (dev->state == DEVPWOF || dev->state == DEVFERR) {
goto err;
}
if (dev->fpwoff)
dev->fpwoff(ctx, dev);
else dev->state = DEVPWOF;
if (dev->state != DEVPWOF)
goto err;
devdetach(ctx, dev);
return 0;
err:
devdetach(ctx, dev);
return -1;
}