dt: add /model and /compatible to sysfs

This commit is contained in:
Mark Poliakov 2024-12-17 19:45:44 +02:00
parent 49b918e2ac
commit e309fdab81

View File

@ -1,10 +1,11 @@
//! Utility functions for device tree handling
use alloc::string::String;
use fdt_rs::index::iters::DevTreeIndexNodeSiblingIter;
use libk::{
error::Error,
fs::sysfs::{
self,
attribute::{BytesAttribute, BytesAttributeOps},
attribute::{BytesAttribute, BytesAttributeOps, StringAttribute, StringAttributeOps},
object::KObject,
},
};
@ -56,6 +57,13 @@ impl Iterator for DeviceTreeMemoryRegionIter<'_> {
/// Registers sysfs objects related to the device tree
pub fn create_sysfs_nodes(dt: &'static DeviceTree) {
struct Raw;
struct Compatible;
struct Model;
struct MachineNodeInfo {
compatible: Option<&'static str>,
model: Option<&'static str>,
}
impl BytesAttributeOps for Raw {
type Data = &'static DeviceTree<'static>;
@ -76,11 +84,41 @@ pub fn create_sysfs_nodes(dt: &'static DeviceTree) {
}
}
impl StringAttributeOps for Compatible {
type Data = MachineNodeInfo;
const NAME: &'static str = "compatible";
fn read(state: &Self::Data) -> Result<String, Error> {
Ok(state.compatible.unwrap_or("").into())
}
}
impl StringAttributeOps for Model {
type Data = MachineNodeInfo;
const NAME: &'static str = "model";
fn read(state: &Self::Data) -> Result<String, Error> {
Ok(state.model.unwrap_or("").into())
}
}
if let Some(device) = sysfs::device() {
let object = KObject::new(dt);
object.add_attribute(BytesAttribute::from(Raw)).ok();
let compatible = dt.root().prop_string("compatible");
let model = dt.root().prop_string("model");
let machine = KObject::new(MachineNodeInfo { compatible, model });
machine
.add_attribute(StringAttribute::from(Compatible))
.ok();
machine.add_attribute(StringAttribute::from(Model)).ok();
object.add_object("machine", machine).ok();
device.add_object("device-tree", object).ok();
}
}