dev: auto-generate device name in device_register if name is unspecified

This commit is contained in:
2023-05-11 21:19:00 +01:00
parent 802e610eed
commit 0238008986
5 changed files with 45 additions and 3 deletions

View File

@@ -37,6 +37,18 @@ static kern_status_t set_root_device(struct device *dev)
return KERN_OK;
}
static kern_status_t device_generate_name(struct device *dev)
{
switch (dev->dev_type) {
case DEV_TYPE_INPUT:
return input_device_generate_name(&dev->input);
default:
break;
}
return KERN_UNSUPPORTED;
}
kern_status_t device_init(void)
{
object_type_register(&device_type);
@@ -157,6 +169,15 @@ kern_status_t device_register(struct device *dev, struct device *parent)
kern_status_t status = KERN_OK;
if (dev->dev_name[0] == 0) {
status = device_generate_name(dev);
}
if (status != KERN_OK) {
device_unlock_irqrestore(dev, flags);
return status;
}
queue_foreach (struct device, child, &dev->dev_children, dev_childent) {
if (!strcmp(dev->dev_name, child->dev_name)) {
status = KERN_NAME_EXISTS;

View File

@@ -1,5 +1,10 @@
#include <socks/device.h>
#include <socks/input.h>
#include <socks/bitmap.h>
#include <socks/libc/stdio.h>
static DECLARE_BITMAP(input_device_ids, INPUT_DEVICE_MAX);
static spin_lock_t input_device_ids_lock = SPIN_LOCK_INIT;
struct input_device *input_device_create(void)
{
@@ -16,6 +21,14 @@ struct input_device *input_device_create(void)
return NULL;
}
unsigned long flags;
spin_lock_irqsave(&input_device_ids_lock, &flags);
unsigned int id = bitmap_lowest_clear(input_device_ids, INPUT_DEVICE_MAX);
bitmap_set(input_device_ids, id);
spin_unlock_irqrestore(&input_device_ids_lock, flags);
input_dev->i_id = id;
return INPUT_DEVICE(dev);
}
@@ -49,3 +62,10 @@ kern_status_t input_device_read(struct device *dev, void *buf, size_t size, size
return KERN_OK;
}
kern_status_t input_device_generate_name(struct input_device *dev)
{
struct device *d = input_device_base(dev);
snprintf(d->dev_name, sizeof d->dev_name, "input%u", dev->i_id);
return KERN_OK;
}

View File

@@ -12,6 +12,7 @@ struct input_event;
#define DEV_NAME_MAX OBJECT_NAME_MAX
#define INPUT_DEVICE_EVENT_QUEUE_SIZE 128
#define INPUT_DEVICE_MAX 4096
#define BLOCK_DEVICE(dev) ((dev)->dev_type == DEV_TYPE_BLOCK ? &(dev)->blk : NULL);
#define CHAR_DEVICE(dev) ((dev)->dev_type == DEV_TYPE_CHAR ? &(dev)->chr : NULL);
@@ -67,6 +68,7 @@ struct net_device {
struct input_device {
struct input_device_ops *i_ops;
unsigned int i_id;
struct ringbuffer i_events;
};
@@ -148,5 +150,6 @@ extern kern_status_t device_register(struct device *dev, struct device *parent);
extern kern_status_t input_device_report_event(struct input_device *dev, const struct input_event *ev, bool noblock);
extern kern_status_t input_device_read(struct device *dev, void *buf, size_t size, size_t *bytes_read);
extern kern_status_t input_device_generate_name(struct input_device *dev);
#endif

View File

@@ -187,7 +187,7 @@ void kernel_init(uintptr_t arg)
create_kernel_thread(background_thread);
struct object *kbd_obj;
status = object_namespace_get_object(global_namespace(), "/dev/system/misc/ps2kbd", &kbd_obj);
status = object_namespace_get_object(global_namespace(), "/dev/system/misc/input0", &kbd_obj);
if (status != KERN_OK) {
printk("no keyboard available");
hang();

View File

@@ -205,11 +205,9 @@ static kern_status_t create_devices(void)
{
struct input_device *kbd = input_device_create();
struct device *kbd_base = input_device_base(kbd);
snprintf(kbd_base->dev_name, sizeof kbd_base->dev_name, "ps2kbd");
struct input_device *ms = input_device_create();
struct device *ms_base = input_device_base(ms);
snprintf(ms_base->dev_name, sizeof ms_base->dev_name, "ps2ms");
kern_status_t status = device_register(kbd_base, misc_device());
if (status != KERN_OK) {