dev: add functions to create device objects

This commit is contained in:
2023-04-03 16:59:14 +01:00
parent 7c6f619b96
commit 5b53168c5f
8 changed files with 105 additions and 1 deletions

View File

@@ -20,7 +20,7 @@ include arch/$(SOCKS_ARCH)/config.mk
# Platform-independent kernel source files
####################################
KERNEL_SRC_DIRS := init kernel vm ds util obj sched test
KERNEL_SRC_DIRS := init kernel vm ds util obj sched dev test
KERNEL_C_FILES := $(foreach dir,$(KERNEL_SRC_DIRS),$(wildcard $(dir)/*.c))
KERNEL_CXX_FILES := $(foreach dir,$(KERNEL_SRC_DIRS),$(wildcard $(dir)/*.cpp))
KERNEL_OBJ := $(addprefix $(BUILD_DIR)/,$(KERNEL_C_FILES:.c=.o) $(KERNEL_CXX_FILES:.cpp=.o))

12
dev/block.c Normal file
View File

@@ -0,0 +1,12 @@
#include <socks/device.h>
struct block_device *block_device_create(void)
{
struct device *dev = device_alloc();
if (!dev) {
return NULL;
}
dev->dev_type = DEV_TYPE_BLOCK;
return BLOCK_DEVICE(dev);
}

12
dev/bus.c Normal file
View File

@@ -0,0 +1,12 @@
#include <socks/device.h>
struct bus_device *bus_device_create(void)
{
struct device *dev = device_alloc();
if (!dev) {
return NULL;
}
dev->dev_type = DEV_TYPE_BUS;
return BUS_DEVICE(dev);
}

12
dev/char.c Normal file
View File

@@ -0,0 +1,12 @@
#include <socks/device.h>
struct char_device *char_device_create(void)
{
struct device *dev = device_alloc();
if (!dev) {
return NULL;
}
dev->dev_type = DEV_TYPE_CHAR;
return CHAR_DEVICE(dev);
}

40
dev/core.c Normal file
View File

@@ -0,0 +1,40 @@
#include <socks/status.h>
#include <socks/object.h>
#include <socks/device.h>
static struct device *root_device = NULL;
static object_type_t device_type = {
.ob_name = "device",
.ob_size = sizeof(struct device),
.ob_ops = {
.destroy = NULL,
}
};
kern_status_t device_init(void)
{
object_type_register(&device_type);
return KERN_OK;
}
kern_status_t set_root_device(struct device *dev)
{
if (root_device) {
object_deref(object_header(root_device));
}
object_ref(object_header(dev));
root_device = dev;
return KERN_OK;
}
struct device *device_alloc(void)
{
object_t *dev_object = object_create(&device_type);
if (!dev_object) {
return NULL;
}
return object_data(dev_object);
}

12
dev/input.c Normal file
View File

@@ -0,0 +1,12 @@
#include <socks/device.h>
struct input_device *input_device_create(void)
{
struct device *dev = device_alloc();
if (!dev) {
return NULL;
}
dev->dev_type = DEV_TYPE_INPUT;
return INPUT_DEVICE(dev);
}

12
dev/net.c Normal file
View File

@@ -0,0 +1,12 @@
#include <socks/device.h>
struct net_device *net_device_create(void)
{
struct device *dev = device_alloc();
if (!dev) {
return NULL;
}
dev->dev_type = DEV_TYPE_NET;
return NET_DEVICE(dev);
}

View File

@@ -10,12 +10,14 @@ struct device;
#define CHAR_DEVICE(dev) ((dev)->dev_type == DEV_TYPE_CHAR ? &(dev)->chr : NULL);
#define NET_DEVICE(dev) ((dev)->dev_type == DEV_TYPE_NET ? &(dev)->net : NULL);
#define INPUT_DEVICE(dev) ((dev)->dev_type == DEV_TYPE_INPUT ? &(dev)->input : NULL);
#define BUS_DEVICE(dev) ((dev)->dev_type == DEV_TYPE_BUS ? &(dev)->bus : NULL);
enum device_type {
DEV_TYPE_BLOCK,
DEV_TYPE_CHAR,
DEV_TYPE_NET,
DEV_TYPE_INPUT,
DEV_TYPE_BUS,
};
struct block_device_ops {
@@ -84,6 +86,8 @@ struct device {
extern kern_status_t device_init(void);
extern kern_status_t set_root_device(struct device *dev);
extern struct device *device_alloc(void);
extern struct char_device *char_device_create(void);
extern struct block_device *block_device_create(void);
extern struct net_device *net_device_create(void);