Kernel improvements (#468)

* **New Features**
  * Plugin-style modules for platforms and devices with start/stop lifecycle and a runtime-consumable device tree array.

* **Refactor**
  * Consolidated initialization into a kernel/module startup flow.
  * Generated devicetree artifacts moved to the build Generated directory.

* **Changes**
  * Removed legacy no-op registration hooks and I2C lock/unlock wrappers.
  * Driver ownership and private state moved to explicit module/owner model.
  * Added ERROR_NOT_ALLOWED error code.
This commit is contained in:
Ken Van Hoeylandt
2026-01-31 09:18:02 +01:00
committed by GitHub
parent d62314f41f
commit c9185740d7
119 changed files with 1639 additions and 440 deletions
+10 -10
View File
@@ -13,7 +13,7 @@
#define TAG LOG_TAG(device)
struct DeviceData {
struct DevicePrivate {
std::vector<Device*> children;
};
@@ -42,11 +42,11 @@ extern "C" {
#define ledger_lock() mutex_lock(&ledger.mutex)
#define ledger_unlock() mutex_unlock(&ledger.mutex)
#define get_device_data(device) static_cast<DeviceData*>(device->internal.data)
#define get_device_private(device) static_cast<DevicePrivate*>(device->internal.device_private)
error_t device_construct(Device* device) {
device->internal.data = new(std::nothrow) DeviceData;
if (device->internal.data == nullptr) {
device->internal.device_private = new(std::nothrow) DevicePrivate;
if (device->internal.device_private == nullptr) {
return ERROR_OUT_OF_MEMORY;
}
LOG_I(TAG, "construct %s", device->name);
@@ -58,13 +58,13 @@ error_t device_destruct(Device* device) {
if (device->internal.state.started || device->internal.state.added) {
return ERROR_INVALID_STATE;
}
if (!get_device_data(device)->children.empty()) {
if (!get_device_private(device)->children.empty()) {
return ERROR_INVALID_STATE;
}
LOG_I(TAG, "destruct %s", device->name);
mutex_destruct(&device->internal.mutex);
delete get_device_data(device);
device->internal.data = nullptr;
delete get_device_private(device);
device->internal.device_private = nullptr;
return ERROR_NONE;
}
@@ -72,14 +72,14 @@ error_t device_destruct(Device* device) {
static void device_add_child(struct Device* device, struct Device* child) {
device_lock(device);
assert(device->internal.state.added);
get_device_data(device)->children.push_back(child);
get_device_private(device)->children.push_back(child);
device_unlock(device);
}
/** Remove a child from the list of children */
static void device_remove_child(struct Device* device, struct Device* child) {
device_lock(device);
auto* parent_data = get_device_data(device);
auto* parent_data = get_device_private(device);
const auto iterator = std::ranges::find(parent_data->children, child);
if (iterator != parent_data->children.end()) {
parent_data->children.erase(iterator);
@@ -251,7 +251,7 @@ void for_each_device(void* callback_context, bool(*on_device)(Device* device, vo
}
void for_each_device_child(Device* device, void* callbackContext, bool(*on_device)(struct Device* device, void* context)) {
auto* data = get_device_data(device);
auto* data = get_device_private(device);
for (auto* child_device : data->children) {
if (!on_device(child_device, callbackContext)) {
break;
+23 -17
View File
@@ -12,16 +12,16 @@
#define TAG LOG_TAG(driver)
struct DriverInternalData {
struct DriverPrivate {
Mutex mutex { 0 };
int use_count = 0;
bool destroying = false;
DriverInternalData() {
DriverPrivate() {
mutex_construct(&mutex);
}
~DriverInternalData() {
~DriverPrivate() {
mutex_destruct(&mutex);
}
};
@@ -54,9 +54,9 @@ static DriverLedger& get_ledger() {
#define ledger get_ledger()
#define driver_internal_data(driver) static_cast<DriverInternalData*>(driver->internal.data)
#define driver_lock(driver) mutex_lock(&driver_internal_data(driver)->mutex);
#define driver_unlock(driver) mutex_unlock(&driver_internal_data(driver)->mutex);
#define get_driver_private(driver) static_cast<DriverPrivate*>(driver->driver_private)
#define driver_lock(driver) mutex_lock(&get_driver_private(driver)->mutex);
#define driver_unlock(driver) mutex_unlock(&get_driver_private(driver)->mutex);
static void driver_add(Driver* driver) {
LOG_I(TAG, "add %s", driver->name);
@@ -83,8 +83,8 @@ static error_t driver_remove(Driver* driver) {
extern "C" {
error_t driver_construct(Driver* driver) {
driver->internal.data = new(std::nothrow) DriverInternalData;
if (driver->internal.data == nullptr) {
driver->driver_private = new(std::nothrow) DriverPrivate;
if (driver->driver_private == nullptr) {
return ERROR_OUT_OF_MEMORY;
}
driver_add(driver);
@@ -93,19 +93,25 @@ error_t driver_construct(Driver* driver) {
error_t driver_destruct(Driver* driver) {
driver_lock(driver);
if (driver_internal_data(driver)->use_count != 0 || driver_internal_data(driver)->destroying) {
// No module means the system owns it and it cannot be destroyed
if (driver->owner == nullptr) {
driver_unlock(driver);
return ERROR_NOT_ALLOWED;
}
if (get_driver_private(driver)->use_count != 0 || get_driver_private(driver)->destroying) {
driver_unlock(driver);
return ERROR_INVALID_STATE;
}
driver_internal_data(driver)->destroying = true;
driver_unlock(driver);
get_driver_private(driver)->destroying = true;
if (driver_remove(driver) != ERROR_NONE) {
LOG_W(TAG, "Failed to remove driver from ledger: %s", driver->name);
}
delete driver_internal_data(driver);
driver->internal.data = nullptr;
driver_unlock(driver);
delete get_driver_private(driver);
driver->driver_private = nullptr;
return ERROR_NONE;
}
@@ -140,7 +146,7 @@ error_t driver_bind(Driver* driver, Device* device) {
driver_lock(driver);
error_t error = ERROR_NONE;
if (driver_internal_data(driver)->destroying || !device_is_added(device)) {
if (get_driver_private(driver)->destroying || !device_is_added(device)) {
error = ERROR_INVALID_STATE;
goto error;
}
@@ -152,7 +158,7 @@ error_t driver_bind(Driver* driver, Device* device) {
}
}
driver_internal_data(driver)->use_count++;
get_driver_private(driver)->use_count++;
driver_unlock(driver);
LOG_I(TAG, "bound %s to %s", driver->name, device->name);
@@ -168,7 +174,7 @@ error_t driver_unbind(Driver* driver, Device* device) {
driver_lock(driver);
error_t error = ERROR_NONE;
if (driver_internal_data(driver)->destroying || !device_is_added(device)) {
if (get_driver_private(driver)->destroying || !device_is_added(device)) {
error = ERROR_INVALID_STATE;
goto error;
}
@@ -180,7 +186,7 @@ error_t driver_unbind(Driver* driver, Device* device) {
}
}
driver_internal_data(driver)->use_count--;
get_driver_private(driver)->use_count--;
driver_unlock(driver);
LOG_I(TAG, "unbound %s from %s", driver->name, device->name);
@@ -1,12 +0,0 @@
// SPDX-License-Identifier: Apache-2.0
#include <tactility/driver.h>
extern "C" {
extern void register_kernel_drivers() {
extern Driver root_driver;
driver_construct(&root_driver);
}
}
+2 -1
View File
@@ -12,7 +12,8 @@ Driver root_driver = {
.stopDevice = nullptr,
.api = nullptr,
.deviceType = nullptr,
.internal = { 0 }
.owner = nullptr,
.driver_private = nullptr
};
}
+2
View File
@@ -27,6 +27,8 @@ const char* error_to_string(error_t error) {
return "out of memory";
case ERROR_NOT_SUPPORTED:
return "not supported";
case ERROR_NOT_ALLOWED:
return "not allowed";
default:
return "unknown";
}
+61
View File
@@ -0,0 +1,61 @@
#include <tactility/kernel_init.h>
#include <tactility/log.h>
#ifdef __cplusplus
extern "C" {
#endif
#define TAG LOG_TAG(kernel)
struct ModuleParent kernel_module_parent = {
"kernel",
nullptr
};
static error_t init_kernel_drivers() {
extern Driver root_driver;
if (driver_construct(&root_driver) != ERROR_NONE) return ERROR_RESOURCE;
return ERROR_NONE;
}
error_t kernel_init(struct Module* platform_module, struct Module* device_module, struct CompatibleDevice devicetree_devices[]) {
LOG_I(TAG, "init");
if (module_parent_construct(&kernel_module_parent) != ERROR_NONE) {
LOG_E(TAG, "init failed to create kernel module parent");
return ERROR_RESOURCE;
}
if (init_kernel_drivers() != ERROR_NONE) {
LOG_E(TAG, "init failed to init kernel drivers");
return ERROR_RESOURCE;
}
module_set_parent(platform_module, &kernel_module_parent);
if (module_start(platform_module) != ERROR_NONE) {
LOG_E(TAG, "init failed to start platform module");
return ERROR_RESOURCE;
}
module_set_parent(device_module, &kernel_module_parent);
if (module_start(device_module) != ERROR_NONE) {
LOG_E(TAG, "init failed to start device module");
return ERROR_RESOURCE;
}
CompatibleDevice* compatible_device = devicetree_devices;
while (compatible_device->device != nullptr) {
if (device_construct_add_start(compatible_device->device, compatible_device->compatible) != ERROR_NONE) {
LOG_E(TAG, "kernel_init failed to construct device: %s (%s)", compatible_device->device->name, compatible_device->compatible);
return ERROR_RESOURCE;
}
compatible_device++;
}
LOG_I(TAG, "init done");
return ERROR_NONE;
};
#ifdef __cplusplus
}
#endif
+102
View File
@@ -0,0 +1,102 @@
#include <vector>
#include <algorithm>
#include <tactility/concurrent/mutex.h>
#include <tactility/module.h>
struct ModuleParentPrivate {
std::vector<struct Module*> modules;
struct Mutex mutex = { 0 };
};
extern "C" {
#pragma region module_parent
error_t module_parent_construct(struct ModuleParent* parent) {
parent->module_parent_private = new (std::nothrow) ModuleParentPrivate();
if (!parent->module_parent_private) return ERROR_OUT_OF_MEMORY;
auto* data = static_cast<ModuleParentPrivate*>(parent->module_parent_private);
mutex_construct(&data->mutex);
return ERROR_NONE;
}
error_t module_parent_destruct(struct ModuleParent* parent) {
auto* data = static_cast<ModuleParentPrivate*>(parent->module_parent_private);
if (data == nullptr) return ERROR_NONE;
mutex_lock(&data->mutex);
if (!data->modules.empty()) {
mutex_unlock(&data->mutex);
return ERROR_INVALID_STATE;
}
mutex_unlock(&data->mutex);
mutex_destruct(&data->mutex);
delete data;
parent->module_parent_private = nullptr;
return ERROR_NONE;
}
#pragma endregion
#pragma region module
error_t module_set_parent(struct Module* module, struct ModuleParent* parent) {
if (module->internal.started) return ERROR_INVALID_STATE;
if (module->internal.parent == parent) return ERROR_NONE;
// Remove from old parent
if (module->internal.parent && module->internal.parent->module_parent_private) {
auto* old_data = static_cast<ModuleParentPrivate*>(module->internal.parent->module_parent_private);
mutex_lock(&old_data->mutex);
auto it = std::find(old_data->modules.begin(), old_data->modules.end(), module);
if (it != old_data->modules.end()) {
old_data->modules.erase(it);
}
mutex_unlock(&old_data->mutex);
}
module->internal.parent = parent;
// Add to new parent
if (parent && parent->module_parent_private) {
auto* new_data = static_cast<ModuleParentPrivate*>(parent->module_parent_private);
mutex_lock(&new_data->mutex);
new_data->modules.push_back(module);
mutex_unlock(&new_data->mutex);
}
return ERROR_NONE;
}
error_t module_start(struct Module* module) {
if (module->internal.started) return ERROR_NONE;
if (!module->internal.parent) return ERROR_INVALID_STATE;
error_t error = module->start();
module->internal.started = (error == ERROR_NONE);
return error;
}
bool module_is_started(struct Module* module) {
return module->internal.started;
}
error_t module_stop(struct Module* module) {
if (!module->internal.started) return ERROR_NONE;
error_t error = module->stop();
if (error != ERROR_NONE) {
return error;
}
module->internal.started = false;
return error;
}
#pragma endregion
}