Anonymous user
UACPI: Difference between revisions
→Code examples
[unchecked revision] | [unchecked revision] |
Line 126:
== Code examples ==
=== Namespace Enumeration & Finding Devices ===
There's multiple ways to implement device discovery for an ACPI namespace, below we will discuss the most common ways and their pros and cons.
==== Let Devices Discover Themselves ====
In this approach, we don't use a centralized bus system, but instead write ad-hoc find/discover() function for each supported device, then register it somewhere
so that it's called by kernel code during initialization.
<source lang="c">
#include <uacpi/utilities.h>
#include <uacpi/resources.h>
#define PS2K_PNP_ID "PNP0303"
static uacpi_ns_iteration_decision match_ps2k(void *user, uacpi_namespace_node *node)
{
// Found a PS2 keyboard! Do initialization below.
uacpi_resources *kb_res;
uacpi_status ret = uacpi_get_current_resources(node, &kb_res);
if (uacpi_unlikely_error(ret)) {
log_error("unable to get PS2K resources: %s", uacpi_status_to_string(ret));
return UACPI_NS_ITERATION_DECISION_NEXT_PEER;
}
// Parse the resources to find the IRQ and IO ports the keyboard is connected to
// ...uacpi_for_each_resource()
ps2k_create_device(...);
uacpi_free_resources(kb_res);
return UACPI_NS_ITERATION_DECISION_CONTINUE;
}
void find_ps2_keyboard()
{
uacpi_find_devices(PS2K_PNP_ID, match_ps2k, NULL);
}
// somewhere in kernel init
find_ps2_keyboard();
find_ps2_mouse();
find_i2c();
find_power_button();
// ...and more
</source>
As you can see it's a very simple approach, but it has lots of drawbacks:
* Very slow: we have to enumerate the entire namespace every time
* Binary bloat: more devices, more ad-hoc find methods
* Error-prone: more code duplication, more space for errors
==== Treat ACPI Namespace as a Bus ====
In this approach, we treat the ACPI namespace as a bus in our kernel, and let devices provide a way to identify them.
<source lang="c">
// acpi_bus.h
#include <uacpi/uacpi.h>
#include <uacpi/namespace.h>
struct acpi_driver {
const char *device_name;
const char *const *pnp_ids;
int (*device_probe)(uacpi_namespace_node *node);
struct acpi_driver *next;
};
void acpi_register_driver(struct acpi_driver *driver);
extern struct acpi_driver *acpi_drivers_head;
// ps2k.c
#include <acpi_bus.h>
#include <uacpi/resources.h>
#define PS2K_PNP_ID "PNP0303"
static const char *const ps2k_pnp_ids[] = {
PS2K_PNP_ID,
NULL,
};
static int ps2k_probe(uacpi_namespace_node_info *info, uacpi_namespace_node *node)
{
uacpi_resources *kb_res;
/* Parse the resources to find the IRQ and IO ports the keyboard is connected to
*
* Note that for a centralized system like that the resources could be passed
* to the device probe callback from common enumeration code at this point as
* well!
*/
uacpi_status ret = uacpi_get_current_resources(node, &kb_res);
if (uacpi_unlikely_error(ret)) {
log_error("unable to get PS2K resources: %s", uacpi_status_to_string(ret));
return -ENODEV;
}
// Actually instantiate the device
int ret = ps2k_create_device(...);
uacpi_free_resources(kb_res);
return ret;
}
static acpi_driver ps2k_driver = {
.device_name = "PS2 Keyboard",
.pnp_ids = ps2k_pnp_ids,
.device_probe = ps2k_probe,
};
int ps2k_init(void)
{
acpi_register_driver(&ps2k_driver);
}
// acpi_bus.c
#include <acpi_bus.h>
#include <uacpi/utilities.h>
#include <uacpi/resources.h>
static uacpi_ns_iteration_decision acpi_init_one_device(void *ctx, uacpi_namespace_node *node)
{
uacpi_namespace_node_info *info;
uacpi_status ret = uacpi_get_namespace_node_info(node, &info);
if (uacpi_unlikely_error(ret)) {
log_error("unable to retrieve node %.4s information: %s",
uacpi_namespace_node_name(node).text, uacpi_status_to_string(ret));
return UACPI_NS_ITERATION_DECISION_CONTINUE;
}
if (info->type != UACPI_OBJECT_DEVICE) {
// We probably don't care about anything but devices at this point
uacpi_free_namespace_node_info(info);
return UACPI_NS_ITERATION_DECISION_CONTINUE;
}
struct acpi_driver *drv = NULL;
if (info->has_hid) {
// Match the HID against every existing acpi_driver pnp id list
}
if (drv == NULL && info->has_cid) {
// Match the CID list against every existing acpi_driver pnp id list
}
if (drv != NULL) {
// Probe the driver and do something with the error code if desired
drv->probe(node, info);
}
uacpi_free_namespace_node_info(info);
return UACPI_NS_ITERATION_DECISION_CONTINUE;
}
void acpi_bus_enumerate()
{
uacpi_namespace_for_each_node_depth_first(
uacpi_namespace_root(), acpi_init_one_device
);
}
</source>
As you can see above, this approach is more scalable, faster, and involves way less code duplication.
It does require a lot mode code and design to get going initially though.
=== Shutting Down the System ===
|