From e309fdab81ecb38cd89e07cdbe586fc53a391c3e Mon Sep 17 00:00:00 2001 From: Mark Poliakov Date: Tue, 17 Dec 2024 19:45:44 +0200 Subject: [PATCH] dt: add /model and /compatible to sysfs --- kernel/lib/device-tree/src/util.rs | 40 +++++++++++++++++++++++++++++- 1 file changed, 39 insertions(+), 1 deletion(-) diff --git a/kernel/lib/device-tree/src/util.rs b/kernel/lib/device-tree/src/util.rs index 6b11fa1e..61fa2051 100644 --- a/kernel/lib/device-tree/src/util.rs +++ b/kernel/lib/device-tree/src/util.rs @@ -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 { + Ok(state.compatible.unwrap_or("").into()) + } + } + + impl StringAttributeOps for Model { + type Data = MachineNodeInfo; + const NAME: &'static str = "model"; + + fn read(state: &Self::Data) -> Result { + 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(); } }