driver: load driver file by alias only

Signed-off-by: Michael Shaw <m@michaelsshaw.com>
This commit is contained in:
Michael Shaw 2026-08-12 20:54:27 -04:00
commit 20905ec95f
Signed by: michaelsshaw
GPG key ID: 5A5E0FB20708A9E7
6 changed files with 69 additions and 39 deletions

View file

@ -19,6 +19,7 @@
#include <lib/endian.h> #include <lib/endian.h>
#define SPIN_MAX 2000 #define SPIN_MAX 2000
#define AHCI_ALIAS "pci:010601"
static uint8_t sata_device_count = 0; static uint8_t sata_device_count = 0;
static uint8_t cd_device_count = 0; static uint8_t cd_device_count = 0;
@ -392,8 +393,8 @@ static int ahci_init_device(struct pci_device *device)
void ahci_init() void ahci_init()
{ {
driver_register("ahci", DRIVER_PCI, ahci_init_device, NULL, pci_driver_class(1, 6, 1)); driver_register("ahci", DRIVER_PCI, ahci_init_device, NULL, AHCI_ALIAS);
krdy("AHCI ready\n"); krdy("AHCI ready\n");
} }
DRIVER_ENTRY_PRIORITY(ahci_init, 2); DRIVER_ENTRY_PRIORITY(ahci_init, 2);
DRIVER_ALIAS("pci:010601"); DRIVER_ALIAS(AHCI_ALIAS);

View file

@ -79,7 +79,11 @@ size_t pci_find_device(u16 major, u16 minor, list_t *out)
static void device_init(struct pci_device *pdev) static void device_init(struct pci_device *pdev)
{ {
u32 driverclass = pci_driver_class(pdev->class_major, pdev->class_minor, pdev->prog_if); u32 driverclass = pci_driver_class(pdev->class_major, pdev->class_minor, pdev->prog_if);
struct driver *driver = driver_find(DRIVER_PCI, driverclass); char alias[11];
snprintf(alias, 11, "pci:%06x", driverclass);
struct driver *driver = driver_find(alias);
if (driver && driver->init_device) { if (driver && driver->init_device) {
pci_init_t f = driver->init_device; pci_init_t f = driver->init_device;

View file

@ -12,8 +12,7 @@
#define DRIVER_EXIT(fn) static void (*__exitcall_##fn)(void) __attribute((used, section(".exitcall"))) = fn #define DRIVER_EXIT(fn) static void (*__exitcall_##fn)(void) __attribute((used, section(".exitcall"))) = fn
#define DRIVER_ALIAS(_alias) \ #define DRIVER_ALIAS(_alias) \
static const char __driver_alias_##__LINE__[] \ static const char __driver_alias_##__LINE__[] __attribute__((used, section(".modinfo"), aligned(1))) = "alias=" _alias
__attribute__((used, section(".modinfo"), aligned(1))) = "alias=" _alias
#define DRIVER_PCI 1 #define DRIVER_PCI 1
@ -33,11 +32,13 @@ struct driver {
void *base; void *base;
size_t size; size_t size;
char alias[64];
u32 n_users; u32 n_users;
}; };
struct driver *driver_register(const char *name, u32 type, void *init, void *deinit, u32 class); struct driver *driver_register(const char *name, u32 type, void *init, void *deinit, const char *alias);
struct driver *driver_find(u32 type, u32 class); struct driver *driver_find(const char *alias);
int driver_load(const char *name); int driver_load(const char *name);
int driver_unload(const char *name); int driver_unload(const char *name);

View file

@ -3,6 +3,7 @@
#define _ht_H_ #define _ht_H_
#include <kernel/common.h> #include <kernel/common.h>
#include <kernel/mtx.h>
typedef struct ht_entry { typedef struct ht_entry {
char *key; char *key;
@ -17,6 +18,8 @@ typedef struct {
ht_entry_t **buckets; ht_entry_t **buckets;
size_t capacity; size_t capacity;
size_t size; size_t size;
mtx_t lock;
} ht_t; } ht_t;
ht_t *ht_create(size_t capacity); ht_t *ht_create(size_t capacity);
@ -24,5 +27,7 @@ void ht_destroy(ht_t *ht);
int ht_insert(ht_t *ht, const char *key, void *value); int ht_insert(ht_t *ht, const char *key, void *value);
void *ht_lookup(ht_t *ht, const char *key); void *ht_lookup(ht_t *ht, const char *key);
int ht_delete(ht_t *ht, const char *key); int ht_delete(ht_t *ht, const char *key);
void ht_lock(ht_t *ht);
void ht_unlock(ht_t *ht);
#endif /* _ht_H_ */ #endif /* _ht_H_ */

View file

@ -2,17 +2,33 @@
#include <kernel/driver.h> #include <kernel/driver.h>
#include <kernel/slab.h> #include <kernel/slab.h>
#include <lib/string.h> #include <lib/string.h>
#include <lib/rbtree.h> #include <lib/hashchain.h>
#include <fs/vfs.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 char *modules_alias_data;
static size_t modules_alias_len; static size_t modules_alias_len;
list_t pending_drivers = {0}; 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) if (!pending_drivers.head && !pending_drivers.tail)
list_create(&pending_drivers); 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->init_device = init;
driver->deinit_device = deinit; driver->deinit_device = deinit;
u64 key = (u64)type << 32; strncpy(driver->alias, alias, sizeof(driver->alias) - 1);
key |= class;
rbnode_t *node = rbt_insert(&driver_registry, key); ht_lock(driver_registry);
if (!node) { ht_insert(driver_registry, driver->alias, driver);
kfree(driver); ht_unlock(driver_registry);
return NULL;
}
node->ptr = driver;
return driver; return driver;
} }
static const char *alias_lookup(u32 type, u32 class) static const char *alias_lookup(const char *alias)
{ {
if (!modules_alias_data) { if (!modules_alias_data) {
dirent_t *dent; dirent_t *dent;
@ -76,10 +88,7 @@ static const char *alias_lookup(u32 type, u32 class)
} }
char want[64]; char want[64];
if (type == DRIVER_PCI) snprintf(want, sizeof(want), "%s ", alias);
snprintf(want, sizeof(want), "pci:%06x ", class);
else
return NULL;
size_t want_len = strlen(want); size_t want_len = strlen(want);
char *p = modules_alias_data; char *p = modules_alias_data;
@ -103,21 +112,21 @@ static const char *alias_lookup(u32 type, u32 class)
return NULL; return NULL;
} }
struct driver *driver_find(u32 type, u32 class) struct driver *driver_find(const char *alias)
{ {
u64 key = (u64)type << 32; if (!driver_registry) {
key |= class; driver_registry_init();
if (!driver_registry)
return NULL;
}
rbnode_t *node = rbt_search(&driver_registry, key); struct driver *ret = ht_lookup(driver_registry, alias);
if (!node) { if (!ret) {
const char *modname = alias_lookup(type, class); const char *modname = alias_lookup(alias);
if (modname && driver_load(modname) == 0) { if (modname && driver_load(modname) == 0) {
node = rbt_search(&driver_registry, key); ret = ht_lookup(driver_registry, alias);
} }
} }
if (!node) return ret;
return NULL;
return node->ptr;
} }

View file

@ -1,6 +1,7 @@
/* SPDX-License-Identifier: GPL-2.0-only */ /* SPDX-License-Identifier: GPL-2.0-only */
#include <kernel/common.h> #include <kernel/common.h>
#include <kernel/slab.h> #include <kernel/slab.h>
#include <kernel/mtx.h>
#include <lib/hashchain.h> #include <lib/hashchain.h>
#include <lib/math.h> #include <lib/math.h>
#include <lib/string.h> #include <lib/string.h>
@ -61,21 +62,20 @@ int ht_insert(ht_t *ht, const char *key, void *value)
ht_entry_t *entry = ht->buckets[idx]; ht_entry_t *entry = ht->buckets[idx];
while (entry) { while (entry) {
if (strcmp(entry->key, key) == 0) { if (!strcmp(entry->key, key)) {
entry->ptr = value; return -EEXIST;
return 0;
} }
entry = entry->next; entry = entry->next;
} }
entry = kzalloc(sizeof(ht_entry_t), ALLOC_KERN); entry = kzalloc(sizeof(ht_entry_t), ALLOC_KERN);
if (!entry) if (!entry)
return -1; return -ENOMEM;
entry->key = strdup(key); entry->key = strdup(key);
if (!entry->key) { if (!entry->key) {
kfree(entry); kfree(entry);
return -1; return -ENOMEM;
} }
entry->ptr = value; entry->ptr = value;
@ -126,3 +126,13 @@ int ht_delete(ht_t *ht, const char *key)
return -1; return -1;
} }
void ht_lock(ht_t *ht)
{
mtx_lock(&ht->lock);
}
void ht_unlock(ht_t *ht)
{
mtx_unlock(&ht->lock);
}