| 1 | |
| 2 |
|
| 3 | |
| 4 |
|
| 5 | |
| 6 | |
| 7 | |
| 8 | |
| 9 | |
| 10 | |
| 11 | |
| 12 | |
| 13 | |
| 14 | |
| 15 | |
| 16 | |
| 17 | |
| 18 | |
| 19 | |
| 20 | |
| 21 | |
| 22 | |
| 23 | |
| 24 | |
| 25 | |
| 26 | |
| 27 | |
| 28 | |
| 29 | |
| 30 | |
| 31 | |
| 32 | |
| 33 | |
| 34 | |
| 35 | |
| 36 | |
| 37 | |
| 38 | |
| 39 | |
| 40 | |
| 41 | |
| 42 | |
| 43 | |
| 44 | |
| 45 | |
| 46 | |
| 47 | |
| 48 | |
| 49 | |
| 50 | |
| 51 | |
| 52 | |
| 53 | |
| 54 | |
| 55 | |
| 56 | |
| 57 | |
| 58 | |
| 59 | |
| 60 | |
| 61 | |
| 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 | |
| 151 | |
| 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 | }
|