Linux kernel mirror (for testing)
git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel
os
linux
1// SPDX-License-Identifier: GPL-2.0
2
3//! Rust Platform driver sample.
4
5use kernel::{c_str, device::Core, of, platform, prelude::*, types::ARef};
6
7struct SampleDriver {
8 pdev: ARef<platform::Device>,
9}
10
11struct Info(u32);
12
13kernel::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
20impl platform::Driver for SampleDriver {
21 type IdInfo = Info;
22 const OF_ID_TABLE: Option<of::IdTable<Self::IdInfo>> = Some(&OF_TABLE);
23
24 fn probe(
25 pdev: &platform::Device<Core>,
26 info: Option<&Self::IdInfo>,
27 ) -> Result<Pin<KBox<Self>>> {
28 dev_dbg!(pdev.as_ref(), "Probe Rust Platform driver sample.\n");
29
30 if let Some(info) = info {
31 dev_info!(pdev.as_ref(), "Probed with info: '{}'.\n", info.0);
32 }
33
34 let drvdata = KBox::new(Self { pdev: pdev.into() }, GFP_KERNEL)?;
35
36 Ok(drvdata.into())
37 }
38}
39
40impl Drop for SampleDriver {
41 fn drop(&mut self) {
42 dev_dbg!(self.pdev.as_ref(), "Remove Rust Platform driver sample.\n");
43 }
44}
45
46kernel::module_platform_driver! {
47 type: SampleDriver,
48 name: "rust_driver_platform",
49 authors: ["Danilo Krummrich"],
50 description: "Rust Platform driver",
51 license: "GPL v2",
52}