2023-04-03 16:59:14 +01:00
|
|
|
#include <socks/device.h>
|
2023-07-08 15:55:43 +01:00
|
|
|
#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;
|
2023-04-03 16:59:14 +01:00
|
|
|
|
|
|
|
|
struct block_device *block_device_create(void)
|
|
|
|
|
{
|
|
|
|
|
struct device *dev = device_alloc();
|
|
|
|
|
if (!dev) {
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
dev->dev_type = DEV_TYPE_BLOCK;
|
2023-06-02 19:35:07 +01:00
|
|
|
|
2023-04-03 16:59:14 +01:00
|
|
|
return BLOCK_DEVICE(dev);
|
|
|
|
|
}
|
2023-06-10 13:21:42 +01:00
|
|
|
|
|
|
|
|
struct block_device *block_device_from_generic(struct device *dev)
|
|
|
|
|
{
|
|
|
|
|
dev->dev_type = DEV_TYPE_BLOCK;
|
|
|
|
|
return BLOCK_DEVICE(dev);
|
|
|
|
|
}
|
2023-07-08 15:55:43 +01:00
|
|
|
|
|
|
|
|
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,
|
|
|
|
|
};
|