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, of, platform, prelude::*};
6
7struct SampleDriver {
8 pdev: 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(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
37impl Drop for SampleDriver {
38 fn drop(&mut self) {
39 dev_dbg!(self.pdev.as_ref(), "Remove Rust Platform driver sample.\n");
40 }
41}
42
43kernel::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}