Implement driver architecture with linker-section registration (AI)
- Created driver framework with probe/init lifecycle. Drivers register via REGISTER_DRIVER macro which places pointers in a .drivers linker section. - During boot, init_drivers() iterates the section, probes each driver (checking if hardware is present), and initializes those that respond OK. - Added .drivers section to linker.ld with __drivers_start/__drivers_end symbols for iteration. - Also added .rodata.* pattern to the .rodata section for string literals placed in sub-sections by the compiler. - No drivers are registered yet; the VGA driver will be the first. Tested: boots cleanly with driver scan completing (0 registered, 0 loaded).
This commit is contained in:
56
src/driver.h
Normal file
56
src/driver.h
Normal file
@@ -0,0 +1,56 @@
|
||||
/**
|
||||
* @file driver.h
|
||||
* @brief Kernel driver architecture.
|
||||
*
|
||||
* Provides a simple framework for registering and initializing kernel drivers.
|
||||
* Each driver provides a probe function that returns whether it should load,
|
||||
* and an init function that performs the actual initialization.
|
||||
*
|
||||
* Drivers are registered at compile time using the REGISTER_DRIVER macro,
|
||||
* which places driver descriptors in a special linker section. The kernel
|
||||
* iterates over all registered drivers during boot.
|
||||
*/
|
||||
|
||||
#ifndef DRIVER_H
|
||||
#define DRIVER_H
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
/** Driver probe result codes. */
|
||||
typedef enum {
|
||||
DRIVER_PROBE_OK = 0, /**< Driver should be loaded. */
|
||||
DRIVER_PROBE_NOT_FOUND = 1, /**< Hardware not found, skip this driver. */
|
||||
DRIVER_PROBE_ERROR = 2 /**< Error during probing. */
|
||||
} driver_probe_result_t;
|
||||
|
||||
/**
|
||||
* Driver descriptor structure.
|
||||
*
|
||||
* Each driver provides a name, a probe function (to test if hardware is
|
||||
* present), and an init function (to set up the driver).
|
||||
*/
|
||||
typedef struct driver {
|
||||
const char *name; /**< Human-readable driver name. */
|
||||
driver_probe_result_t (*probe)(void); /**< Probe function. Returns DRIVER_PROBE_OK if driver should load. */
|
||||
int (*init)(void); /**< Init function. Returns 0 on success, non-zero on failure. */
|
||||
} driver_t;
|
||||
|
||||
/**
|
||||
* Register a driver.
|
||||
*
|
||||
* Places the driver descriptor in the .drivers linker section so it
|
||||
* is automatically discovered during boot.
|
||||
*/
|
||||
#define REGISTER_DRIVER(drv) \
|
||||
static const driver_t * __attribute__((used, section(".drivers"))) \
|
||||
_driver_##drv = &(drv)
|
||||
|
||||
/**
|
||||
* Initialize all registered drivers.
|
||||
*
|
||||
* Iterates over the .drivers section, probes each driver, and initializes
|
||||
* those that respond positively to probing.
|
||||
*/
|
||||
void init_drivers(void);
|
||||
|
||||
#endif /* DRIVER_H */
|
||||
Reference in New Issue
Block a user