dev: implement block device registration
This commit is contained in:
46
dev/block.c
46
dev/block.c
@@ -1,4 +1,10 @@
|
||||
#include <socks/device.h>
|
||||
#include <socks/util.h>
|
||||
#include <socks/printk.h>
|
||||
#include <socks/libc/stdio.h>
|
||||
|
||||
static DECLARE_BITMAP(block_device_ids, BLOCK_DEVICE_MAX);
|
||||
static spin_lock_t block_device_ids_lock = SPIN_LOCK_INIT;
|
||||
|
||||
struct block_device *block_device_create(void)
|
||||
{
|
||||
@@ -17,3 +23,43 @@ struct block_device *block_device_from_generic(struct device *dev)
|
||||
dev->dev_type = DEV_TYPE_BLOCK;
|
||||
return BLOCK_DEVICE(dev);
|
||||
}
|
||||
|
||||
kern_status_t block_device_read(struct device *dev, void *buf, size_t size, size_t *bytes_read, socks_flags_t flags)
|
||||
{
|
||||
return KERN_UNIMPLEMENTED;
|
||||
}
|
||||
|
||||
static kern_status_t generate_name(struct block_device *dev, char out[DEV_NAME_MAX])
|
||||
{
|
||||
snprintf(out, DEV_NAME_MAX, "disk%u", dev->b_id);
|
||||
return KERN_OK;
|
||||
}
|
||||
|
||||
kern_status_t block_device_register(struct device *dev)
|
||||
{
|
||||
unsigned long flags;
|
||||
spin_lock_irqsave(&block_device_ids_lock, &flags);
|
||||
unsigned int id = bitmap_lowest_clear(block_device_ids, BLOCK_DEVICE_MAX);
|
||||
bitmap_set(block_device_ids, id);
|
||||
spin_unlock_irqrestore(&block_device_ids_lock, flags);
|
||||
|
||||
struct block_device *blockdev = &dev->blk;
|
||||
blockdev->b_id = id;
|
||||
|
||||
char name[DEV_NAME_MAX];
|
||||
generate_name(blockdev, name);
|
||||
char path[OBJECT_PATH_MAX];
|
||||
snprintf(path, sizeof path, "/dev/block/%s", name);
|
||||
|
||||
char size_string[32];
|
||||
data_size_to_string(blockdev->sector_size * blockdev->capacity, size_string, sizeof size_string);
|
||||
|
||||
printk("dev: found %s %s block device '%s'", size_string, dev->dev_owner->drv_name, dev->dev_model_name);
|
||||
|
||||
return object_namespace_create_link(global_namespace(), path, &dev->dev_base);
|
||||
}
|
||||
|
||||
struct device_type_ops block_type_ops = {
|
||||
.register_device = block_device_register,
|
||||
.read = block_device_read,
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user