driver: load driver file by alias only
Signed-off-by: Michael Shaw <m@michaelsshaw.com>
This commit is contained in:
parent
0aec18db63
commit
20905ec95f
6 changed files with 69 additions and 39 deletions
|
|
@ -2,17 +2,33 @@
|
|||
#include <kernel/driver.h>
|
||||
#include <kernel/slab.h>
|
||||
#include <lib/string.h>
|
||||
#include <lib/rbtree.h>
|
||||
#include <lib/hashchain.h>
|
||||
#include <fs/vfs.h>
|
||||
|
||||
static rbtree_t driver_registry;
|
||||
#define DRIVER_HT_SIZE 128
|
||||
|
||||
static ht_t *driver_registry;
|
||||
static char *modules_alias_data;
|
||||
static size_t modules_alias_len;
|
||||
|
||||
list_t pending_drivers = {0};
|
||||
|
||||
struct driver *driver_register(const char *name, u32 type, void *init, void *deinit, u32 class)
|
||||
static void driver_registry_init()
|
||||
{
|
||||
driver_registry = ht_create(DRIVER_HT_SIZE);
|
||||
}
|
||||
|
||||
struct driver *driver_register(const char *name, u32 type, void *init, void *deinit, const char *alias)
|
||||
{
|
||||
if (!alias)
|
||||
return NULL;
|
||||
|
||||
if (!driver_registry) {
|
||||
driver_registry_init();
|
||||
if (!driver_registry)
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if (!pending_drivers.head && !pending_drivers.tail)
|
||||
list_create(&pending_drivers);
|
||||
|
||||
|
|
@ -46,20 +62,16 @@ struct driver *driver_register(const char *name, u32 type, void *init, void *dei
|
|||
driver->init_device = init;
|
||||
driver->deinit_device = deinit;
|
||||
|
||||
u64 key = (u64)type << 32;
|
||||
key |= class;
|
||||
strncpy(driver->alias, alias, sizeof(driver->alias) - 1);
|
||||
|
||||
rbnode_t *node = rbt_insert(&driver_registry, key);
|
||||
if (!node) {
|
||||
kfree(driver);
|
||||
return NULL;
|
||||
}
|
||||
node->ptr = driver;
|
||||
ht_lock(driver_registry);
|
||||
ht_insert(driver_registry, driver->alias, driver);
|
||||
ht_unlock(driver_registry);
|
||||
|
||||
return driver;
|
||||
}
|
||||
|
||||
static const char *alias_lookup(u32 type, u32 class)
|
||||
static const char *alias_lookup(const char *alias)
|
||||
{
|
||||
if (!modules_alias_data) {
|
||||
dirent_t *dent;
|
||||
|
|
@ -76,10 +88,7 @@ static const char *alias_lookup(u32 type, u32 class)
|
|||
}
|
||||
|
||||
char want[64];
|
||||
if (type == DRIVER_PCI)
|
||||
snprintf(want, sizeof(want), "pci:%06x ", class);
|
||||
else
|
||||
return NULL;
|
||||
snprintf(want, sizeof(want), "%s ", alias);
|
||||
|
||||
size_t want_len = strlen(want);
|
||||
char *p = modules_alias_data;
|
||||
|
|
@ -103,21 +112,21 @@ static const char *alias_lookup(u32 type, u32 class)
|
|||
return NULL;
|
||||
}
|
||||
|
||||
struct driver *driver_find(u32 type, u32 class)
|
||||
struct driver *driver_find(const char *alias)
|
||||
{
|
||||
u64 key = (u64)type << 32;
|
||||
key |= class;
|
||||
if (!driver_registry) {
|
||||
driver_registry_init();
|
||||
if (!driver_registry)
|
||||
return NULL;
|
||||
}
|
||||
|
||||
rbnode_t *node = rbt_search(&driver_registry, key);
|
||||
if (!node) {
|
||||
const char *modname = alias_lookup(type, class);
|
||||
struct driver *ret = ht_lookup(driver_registry, alias);
|
||||
if (!ret) {
|
||||
const char *modname = alias_lookup(alias);
|
||||
if (modname && driver_load(modname) == 0) {
|
||||
node = rbt_search(&driver_registry, key);
|
||||
ret = ht_lookup(driver_registry, alias);
|
||||
}
|
||||
}
|
||||
|
||||
if (!node)
|
||||
return NULL;
|
||||
|
||||
return node->ptr;
|
||||
return ret;
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue