this also fixes up a bunch of formatting, adding the type vaddr_t Signed-off-by: Michael Shaw <spencershawiscool@gmail.com>
68 lines
1.5 KiB
C
68 lines
1.5 KiB
C
/* SPDX-License-Identifier: GPL-2.0-only */
|
|
#include <kernel/slab.h>
|
|
#include <kernel/device.h>
|
|
#include <lib/rbtree.h>
|
|
#include <lib/string.h>
|
|
#include <lib/math.h>
|
|
|
|
struct dev_info {
|
|
dev_t dev;
|
|
dev_class_t class;
|
|
void *private;
|
|
char name[256];
|
|
};
|
|
|
|
static mem_cache_t *dev_info_cache = NULL;
|
|
|
|
static rbtree device_tree = { 0 };
|
|
|
|
dev_t device_create(void *private, dev_class_t class, const char *name)
|
|
{
|
|
dev_t key = rbt_next_key(&device_tree);
|
|
rbnode *node = rbt_insert(&device_tree, key);
|
|
|
|
size_t name_len = strlen(name);
|
|
|
|
struct dev_info *info = slab_alloc(dev_info_cache);
|
|
info->dev = key;
|
|
info->private = private;
|
|
info->class = class;
|
|
memcpy(info->name, name, MIN(name_len, sizeof(info->name) - 1));
|
|
|
|
node->value = (vaddr_t)info;
|
|
|
|
return key;
|
|
}
|
|
|
|
void device_destroy(dev_t dev)
|
|
{
|
|
rbnode *dev_node = rbt_search(&device_tree, dev);
|
|
struct dev_info *devinfo = (void *)dev_node->value;
|
|
|
|
slab_free(dev_info_cache, devinfo);
|
|
rbt_delete(&device_tree, dev_node);
|
|
}
|
|
|
|
dev_class_t device_class(dev_t dev)
|
|
{
|
|
rbnode *dev_node = rbt_search(&device_tree, dev);
|
|
struct dev_info *devinfo = (void *)dev_node->value;
|
|
|
|
return devinfo->class;
|
|
}
|
|
|
|
void *device_private_info(dev_t dev)
|
|
{
|
|
rbnode *dev_node = rbt_search(&device_tree, dev);
|
|
struct dev_info *devinfo = (void *)dev_node->value;
|
|
|
|
return devinfo->private;
|
|
}
|
|
|
|
void device_layer_init()
|
|
{
|
|
dev_info_cache = slab_create(sizeof(struct dev_info), 16 * KB, 8);
|
|
|
|
if (!dev_info_cache)
|
|
panic("dev_info_cache allocation failure!\n");
|
|
}
|