1 // SPDX-License-Identifier: GPL-2.0 2 3 //! Rust Platform driver sample. 4 5 use kernel::{c_str, of, platform, prelude::*}; 6 7 struct SampleDriver { 8 pdev: platform::Device, 9 } 10 11 struct Info(u32); 12 13 kernel::of_device_table!( 14 OF_TABLE, 15 MODULE_OF_TABLE, 16 <SampleDriver as platform::Driver>::IdInfo, 17 [(of::DeviceId::new(c_str!("test,rust-device")), Info(42))] 18 ); 19 20 impl platform::Driver for SampleDriver { 21 type IdInfo = Info; 22 const OF_ID_TABLE: Option<of::IdTable<Self::IdInfo>> = Some(&OF_TABLE); 23 probe(pdev: &mut platform::Device, info: Option<&Self::IdInfo>) -> Result<Pin<KBox<Self>>>24 fn probe(pdev: &mut platform::Device, info: Option<&Self::IdInfo>) -> Result<Pin<KBox<Self>>> { 25 dev_dbg!(pdev.as_ref(), "Probe Rust Platform driver sample.\n"); 26 27 if let Some(info) = info { 28 dev_info!(pdev.as_ref(), "Probed with info: '{}'.\n", info.0); 29 } 30 31 let drvdata = KBox::new(Self { pdev: pdev.clone() }, GFP_KERNEL)?; 32 33 Ok(drvdata.into()) 34 } 35 } 36 37 impl Drop for SampleDriver { drop(&mut self)38 fn drop(&mut self) { 39 dev_dbg!(self.pdev.as_ref(), "Remove Rust Platform driver sample.\n"); 40 } 41 } 42 43 kernel::module_platform_driver! { 44 type: SampleDriver, 45 name: "rust_driver_platform", 46 author: "Danilo Krummrich", 47 description: "Rust Platform driver", 48 license: "GPL v2", 49 } 50