at commit 32d0771e
| 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 | <SampleDriver as platform::Driver>::IdInfo, |
| 91 | [(of::DeviceId::new(c"test,rust-device"), Info(42))] |
| 92 | ); |
| 93 | |
| 94 | kernel::acpi_device_table!( |
| 95 | ACPI_TABLE, |
| 96 | <SampleDriver as platform::Driver>::IdInfo, |
| 97 | [(acpi::DeviceId::new(c"LNUXBEEF"), Info(0))] |
| 98 | ); |
| 99 | |
| 100 | impl platform::Driver for SampleDriver { |
| 101 | type IdInfo = Info; |
| 102 | type Data<'bound> = Self; |
| 103 | const OF_ID_TABLE: Option<of::IdTable<Self::IdInfo>> = Some(&OF_TABLE); |
| 104 | const ACPI_ID_TABLE: Option<acpi::IdTable<Self::IdInfo>> = Some(&ACPI_TABLE); |
| 105 | |
| 106 | fn probe<'bound>( |
| 107 | pdev: &'bound platform::Device<Core<'_>>, |
| 108 | info: Option<&'bound Self::IdInfo>, |
| 109 | ) -> impl PinInit<Self, Error> + 'bound { |
| 110 | let dev = pdev.as_ref(); |
| 111 | |
| 112 | dev_dbg!(dev, "Probe Rust Platform driver sample.\n"); |
| 113 | |
| 114 | if let Some(info) = info { |
| 115 | dev_info!(dev, "Probed with info: '{}'.\n", info.0); |
| 116 | } |
| 117 | |
| 118 | if dev.fwnode().is_some_and(|node| node.is_of_node()) { |
| 119 | Self::properties_parse(dev)?; |
| 120 | } |
| 121 | |
| 122 | Ok(Self { pdev: pdev.into() }) |
| 123 | } |
| 124 | } |
| 125 | |
| 126 | impl SampleDriver { |
| 127 | fn properties_parse(dev: &device::Device) -> Result { |
| 128 | let fwnode = dev.fwnode().ok_or(ENOENT)?; |
| 129 | |
| 130 | if let Ok(idx) = fwnode.property_match_string(c"compatible", c"test,rust-device") { |
| 131 | dev_info!(dev, "matched compatible string idx = {}\n", idx); |
| 132 | } |
| 133 | |
| 134 | let name = c"compatible"; |
| 135 | let prop = fwnode.property_read::<CString>(name).required_by(dev)?; |
| 136 | dev_info!(dev, "'{name}'='{prop:?}'\n"); |
| 137 | |
| 138 | let name = c"test,bool-prop"; |
| 139 | let prop = fwnode.property_read_bool(c"test,bool-prop"); |
| 140 | dev_info!(dev, "'{name}'='{prop}'\n"); |
| 141 | |
| 142 | if fwnode.property_present(c"test,u32-prop") { |
| 143 | dev_info!(dev, "'test,u32-prop' is present\n"); |
| 144 | } |
| 145 | |
| 146 | let name = c"test,u32-optional-prop"; |
| 147 | let prop = fwnode.property_read::<u32>(name).or(0x12); |
| 148 | dev_info!(dev, "'{name}'='{prop:#x}' (default = 0x12)\n"); |
| 149 | |
| 150 | // A missing required property will print an error. Discard the error to |
| 151 | // prevent properties_parse from failing in that case. |
| 152 | let name = c"test,u32-required-prop"; |
| 153 | let _ = fwnode.property_read::<u32>(name).required_by(dev); |
| 154 | |
| 155 | let name = c"test,u32-prop"; |
| 156 | let prop: u32 = fwnode.property_read(name).required_by(dev)?; |
| 157 | dev_info!(dev, "'{name}'='{prop:#x}'\n"); |
| 158 | |
| 159 | let name = c"test,i16-array"; |
| 160 | let prop: [i16; 4] = fwnode.property_read(name).required_by(dev)?; |
| 161 | dev_info!(dev, "'{name}'='{prop:?}'\n"); |
| 162 | let len = fwnode.property_count_elem::<u16>(name)?; |
| 163 | dev_info!(dev, "'{name}' length is {len}\n"); |
| 164 | |
| 165 | let name = c"test,i16-array"; |
| 166 | let prop: KVec<i16> = fwnode.property_read_array_vec(name, 4)?.required_by(dev)?; |
| 167 | dev_info!(dev, "'{name}'='{prop:?}' (KVec)\n"); |
| 168 | |
| 169 | for child in fwnode.children() { |
| 170 | let name = c"test,ref-arg"; |
| 171 | let nargs = NArgs::N(2); |
| 172 | let prop: FwNodeReferenceArgs = child.property_get_reference_args(name, nargs, 0)?; |
| 173 | dev_info!(dev, "'{name}'='{prop:?}'\n"); |
| 174 | } |
| 175 | |
| 176 | Ok(()) |
| 177 | } |
| 178 | } |
| 179 | |
| 180 | impl Drop for SampleDriver { |
| 181 | fn drop(&mut self) { |
| 182 | dev_dbg!(self.pdev, "Remove Rust Platform driver sample.\n"); |
| 183 | } |
| 184 | } |
| 185 | |
| 186 | kernel::module_platform_driver! { |
| 187 | type: SampleDriver, |
| 188 | name: "rust_driver_platform", |
| 189 | authors: ["Danilo Krummrich"], |
| 190 | description: "Rust Platform driver", |
| 191 | license: "GPL v2", |
| 192 | } |