dev: start implementing framebuffer devices

This commit is contained in:
2023-06-06 22:01:17 +01:00
parent 81533a1cff
commit cb220452db
7 changed files with 177 additions and 13 deletions

View File

@@ -19,6 +19,19 @@ static kern_status_t device_object_get_child_named(struct object *, const char *
extern kern_status_t init_driver_tree(void);
extern struct device_type_ops input_type_ops;
extern struct device_type_ops framebuffer_type_ops;
static struct device_type_ops *type_ops[] = {
[DEV_TYPE_UNKNOWN] = NULL,
[DEV_TYPE_BLOCK] = NULL,
[DEV_TYPE_CHAR] = NULL,
[DEV_TYPE_NET] = NULL,
[DEV_TYPE_INPUT] = &input_type_ops,
[DEV_TYPE_BUS] = NULL,
[DEV_TYPE_FRAMEBUFFER] = &framebuffer_type_ops,
};
static struct object_type device_type = {
.ob_name = "device",
.ob_size = sizeof(struct device),
@@ -60,7 +73,7 @@ kern_status_t device_init(void)
struct bus_device *system_dev = bus_device_create();
struct device *system_dev_base = bus_device_base(system_dev);
snprintf(system_dev_base->dev_name, sizeof system_dev_base->dev_name, "system");
__root_device = bus_device_base(system_dev);
set_root_device(bus_device_base(system_dev));
struct bus_device *misc_dev = bus_device_create();
struct device *misc_dev_base = bus_device_base(misc_dev);
@@ -236,14 +249,12 @@ kern_status_t device_register(struct device *dev, struct driver *owner, struct d
driver_add_device(owner, dev);
switch (dev->dev_type) {
case DEV_TYPE_INPUT:
status = input_device_register(INPUT_DEVICE(dev));
break;
default:
break;
if (type_ops[dev->dev_type] && type_ops[dev->dev_type]->register_device) {
status = type_ops[dev->dev_type]->register_device(dev);
}
/* TODO remove device if registration failed */
driver_unlock(owner);
device_unlock_irqrestore(dev, flags);

47
dev/fb.c Normal file
View File

@@ -0,0 +1,47 @@
#include <socks/device.h>
#include <socks/bitmap.h>
#include <socks/libc/stdio.h>
static DECLARE_BITMAP(fb_device_ids, FRAMEBUFFER_DEVICE_MAX);
static spin_lock_t fb_device_ids_lock = SPIN_LOCK_INIT;
struct framebuffer_device *framebuffer_device_create(void)
{
struct device *dev = device_alloc();
if (!dev) {
return NULL;
}
dev->dev_type = DEV_TYPE_FRAMEBUFFER;
return FRAMEBUFFER_DEVICE(dev);
}
static kern_status_t generate_name(struct framebuffer_device *dev, char out[DEV_NAME_MAX])
{
snprintf(out, DEV_NAME_MAX, "fb%u", dev->fb_id);
return KERN_OK;
}
kern_status_t framebuffer_device_register(struct device *dev)
{
unsigned long flags;
spin_lock_irqsave(&fb_device_ids_lock, &flags);
unsigned int id = bitmap_lowest_clear(fb_device_ids, INPUT_DEVICE_MAX);
bitmap_set(fb_device_ids, id);
spin_unlock_irqrestore(&fb_device_ids_lock, flags);
struct framebuffer_device *fbdev = &dev->fb;
fbdev->fb_id = id;
char name[DEV_NAME_MAX];
generate_name(fbdev, name);
char path[OBJECT_PATH_MAX];
snprintf(path, sizeof path, "/dev/video/%s", name);
return object_namespace_create_link(global_namespace(), path, &dev->dev_base);
}
struct device_type_ops framebuffer_type_ops = {
.register_device = framebuffer_device_register,
};

View File

@@ -61,7 +61,7 @@ static kern_status_t generate_name(struct input_device *dev, char out[DEV_NAME_M
return KERN_OK;
}
kern_status_t input_device_register(struct input_device *dev)
kern_status_t input_device_register(struct device *dev)
{
unsigned long flags;
spin_lock_irqsave(&input_device_ids_lock, &flags);
@@ -69,13 +69,17 @@ kern_status_t input_device_register(struct input_device *dev)
bitmap_set(input_device_ids, id);
spin_unlock_irqrestore(&input_device_ids_lock, flags);
dev->i_id = id;
struct input_device *inputdev = &dev->input;
inputdev->i_id = id;
char name[DEV_NAME_MAX];
generate_name(dev, name);
generate_name(inputdev, name);
char path[OBJECT_PATH_MAX];
snprintf(path, sizeof path, "/dev/input/%s", name);
struct device *base = input_device_base(dev);
return object_namespace_create_link(global_namespace(), path, &base->dev_base);
return object_namespace_create_link(global_namespace(), path, &dev->dev_base);
}
struct device_type_ops input_type_ops = {
.register_device = input_device_register,
};