| 1 |
// SPDX-License-Identifier: GPL-2.0 |
| 2 |
|
| 3 |
//! Rust Platform driver sample. |
| 4 |
|
| 5 |
//! ACPI match table test |
| 6 |
//! |
| 7 |
//! This demonstrates how to test an ACPI-based Rust platform driver using QEMU |
| 8 |
//! with a custom SSDT. |
| 9 |
//! |
| 10 |
//! Steps: |
| 11 |
//! |
| 12 |
//! 1. **Create an SSDT source file** (`ssdt.dsl`) with the following content: |
| 13 |
//! |
| 14 |
//! ```asl |
| 15 |
//! DefinitionBlock ("", "SSDT", 2, "TEST", "VIRTACPI", 0x00000001) |
| 16 |
//! { |
| 17 |
//! Scope (\_SB) |
| 18 |
//! { |
| 19 |
//! Device (T432) |
| 20 |
//! { |
| 21 |
//! Name (_HID, "LNUXBEEF") // ACPI hardware ID to match |
| 22 |
//! Name (_UID, 1) |
| 23 |
//! Name (_STA, 0x0F) // Device present, enabled |
| 24 |
//! Name (_CRS, ResourceTemplate () |
| 25 |
//! { |
| 26 |
//! Memory32Fixed (ReadWrite, 0xFED00000, 0x1000) |
| 27 |
//! }) |
| 28 |
//! } |
| 29 |
//! } |
| 30 |
//! } |
| 31 |
//! ``` |
| 32 |
//! |
| 33 |
//! 2. **Compile the table**: |
| 34 |
//! |
| 35 |
//! ```sh |
| 36 |
//! iasl -tc ssdt.dsl |
| 37 |
//! ``` |
| 38 |
//! |
| 39 |
//! This generates `ssdt.aml` |
| 40 |
//! |
| 41 |
//! 3. **Run QEMU** with the compiled AML file: |
| 42 |
//! |
| 43 |
//! ```sh |
| 44 |
//! qemu-system-x86_64 -m 512M \ |
| 45 |
//! -enable-kvm \ |
| 46 |
//! -kernel path/to/bzImage \ |
| 47 |
//! -append "root=/dev/sda console=ttyS0" \ |
| 48 |
//! -hda rootfs.img \ |
| 49 |
//! -serial stdio \ |
| 50 |
//! -acpitable file=ssdt.aml |
| 51 |
//! ``` |
| 52 |
//! |
| 53 |
//! Requirements: |
| 54 |
//! - The `rust_driver_platform` must be present either: |
| 55 |
//! - built directly into the kernel (`bzImage`), or |
| 56 |
//! - available as a `.ko` file and loadable from `rootfs.img` |
| 57 |
//! |
| 58 |
//! 4. **Verify it worked** by checking `dmesg`: |
| 59 |
//! |
| 60 |
//! ``` |
| 61 |
//! rust_driver_platform LNUXBEEF:00: Probed with info: '0'. |
| 62 |
//! ``` |
| 63 |
//! |
| 64 |
|
| 65 |
use kernel::{ |
| 66 |
acpi, |
| 67 |
device::{ |
| 68 |
self, |
| 69 |
property::{ |
| 70 |
FwNodeReferenceArgs, |
| 71 |
NArgs, // |
| 72 |
}, |
| 73 |
Core, |
| 74 |
}, |
| 75 |
of, |
| 76 |
platform, |
| 77 |
prelude::*, |
| 78 |
str::CString, |
| 79 |
sync::aref::ARef, // |
| 80 |
}; |
| 81 |
|
| 82 |
struct SampleDriver { |
| 83 |
pdev: ARef<platform::Device>, |
| 84 |
} |
| 85 |
|
| 86 |
struct Info(u32); |
| 87 |
|
| 88 |
kernel::of_device_table!( |
| 89 |
OF_TABLE, |
| 90 |
MODULE_OF_TABLE, |
| 91 |
<SampleDriver as platform::Driver>::IdInfo, |
| 92 |
[(of::DeviceId::new(c"test,rust-device"), Info(42))] |
| 93 |
); |
| 94 |
|
| 95 |
kernel::acpi_device_table!( |
| 96 |
ACPI_TABLE, |
| 97 |
MODULE_ACPI_TABLE, |
| 98 |
<SampleDriver as platform::Driver>::IdInfo, |
| 99 |
[(acpi::DeviceId::new(c"LNUXBEEF"), Info(0))] |
| 100 |
); |
| 101 |
|
| 102 |
impl platform::Driver for SampleDriver { |
| 103 |
type IdInfo = Info; |
| 104 |
const OF_ID_TABLE: Option<of::IdTable<Self::IdInfo>> = Some(&OF_TABLE); |
| 105 |
const ACPI_ID_TABLE: Option<acpi::IdTable<Self::IdInfo>> = Some(&ACPI_TABLE); |
| 106 |
|
| 107 |
fn probe( |
| 108 |
pdev: &platform::Device<Core>, |
| 109 |
info: Option<&Self::IdInfo>, |
| 110 |
) -> impl PinInit<Self, Error> { |
| 111 |
let dev = pdev.as_ref(); |
| 112 |
|
| 113 |
dev_dbg!(dev, "Probe Rust Platform driver sample.\n"); |
| 114 |
|
| 115 |
if let Some(info) = info { |
| 116 |
dev_info!(dev, "Probed with info: '{}'.\n", info.0); |
| 117 |
} |
| 118 |
|
| 119 |
if dev.fwnode().is_some_and(|node| node.is_of_node()) { |
| 120 |
Self::properties_parse(dev)?; |
| 121 |
} |
| 122 |
|
| 123 |
Ok(Self { pdev: pdev.into() }) |
| 124 |
} |
| 125 |
} |
| 126 |
|
| 127 |
impl SampleDriver { |
| 128 |
fn properties_parse(dev: &device::Device) -> Result { |
| 129 |
let fwnode = dev.fwnode().ok_or(ENOENT)?; |
| 130 |
|
| 131 |
if let Ok(idx) = fwnode.property_match_string(c"compatible", c"test,rust-device") { |
| 132 |
dev_info!(dev, "matched compatible string idx = {}\n", idx); |
| 133 |
} |
| 134 |
|
| 135 |
let name = c"compatible"; |
| 136 |
let prop = fwnode.property_read::<CString>(name).required_by(dev)?; |
| 137 |
dev_info!(dev, "'{name}'='{prop:?}'\n"); |
| 138 |
|
| 139 |
let name = c"test,bool-prop"; |
| 140 |
let prop = fwnode.property_read_bool(c"test,bool-prop"); |
| 141 |
dev_info!(dev, "'{name}'='{prop}'\n"); |
| 142 |
|
| 143 |
if fwnode.property_present(c"test,u32-prop") { |
| 144 |
dev_info!(dev, "'test,u32-prop' is present\n"); |
| 145 |
} |
| 146 |
|
| 147 |
let name = c"test,u32-optional-prop"; |
| 148 |
let prop = fwnode.property_read::<u32>(name).or(0x12); |
| 149 |
dev_info!(dev, "'{name}'='{prop:#x}' (default = 0x12)\n"); |
| 150 |
|
| 151 |
// A missing required property will print an error. Discard the error to |
| 152 |
// prevent properties_parse from failing in that case. |
| 153 |
let name = c"test,u32-required-prop"; |
| 154 |
let _ = fwnode.property_read::<u32>(name).required_by(dev); |
| 155 |
|
| 156 |
let name = c"test,u32-prop"; |
| 157 |
let prop: u32 = fwnode.property_read(name).required_by(dev)?; |
| 158 |
dev_info!(dev, "'{name}'='{prop:#x}'\n"); |
| 159 |
|
| 160 |
let name = c"test,i16-array"; |
| 161 |
let prop: [i16; 4] = fwnode.property_read(name).required_by(dev)?; |
| 162 |
dev_info!(dev, "'{name}'='{prop:?}'\n"); |
| 163 |
let len = fwnode.property_count_elem::<u16>(name)?; |
| 164 |
dev_info!(dev, "'{name}' length is {len}\n"); |
| 165 |
|
| 166 |
let name = c"test,i16-array"; |
| 167 |
let prop: KVec<i16> = fwnode.property_read_array_vec(name, 4)?.required_by(dev)?; |
| 168 |
dev_info!(dev, "'{name}'='{prop:?}' (KVec)\n"); |
| 169 |
|
| 170 |
for child in fwnode.children() { |
| 171 |
let name = c"test,ref-arg"; |
| 172 |
let nargs = NArgs::N(2); |
| 173 |
let prop: FwNodeReferenceArgs = child.property_get_reference_args(name, nargs, 0)?; |
| 174 |
dev_info!(dev, "'{name}'='{prop:?}'\n"); |
| 175 |
} |
| 176 |
|
| 177 |
Ok(()) |
| 178 |
} |
| 179 |
} |
| 180 |
|
| 181 |
impl Drop for SampleDriver { |
| 182 |
fn drop(&mut self) { |
| 183 |
dev_dbg!(self.pdev, "Remove Rust Platform driver sample.\n"); |
| 184 |
} |
| 185 |
} |
| 186 |
|
| 187 |
kernel::module_platform_driver! { |
| 188 |
type: SampleDriver, |
| 189 |
name: "rust_driver_platform", |
| 190 |
authors: ["Danilo Krummrich"], |
| 191 |
description: "Rust Platform driver", |
| 192 |
license: "GPL v2", |
| 193 |
} |