dev: auto-generate device name in device_register if name is unspecified
This commit is contained in:
21
dev/core.c
21
dev/core.c
@@ -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;
|
||||
|
||||
20
dev/input.c
20
dev/input.c
@@ -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;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user