mirror of
				https://github.com/torvalds/linux.git
				synced 2025-11-04 02:30:34 +02:00 
			
		
		
		
	As by now, platform::Device is implemented as:
	#[derive(Clone)]
	pub struct Device(ARef<device::Device>);
This may be convenient, but has the implication that drivers can call
device methods that require a mutable reference concurrently at any
point of time.
Instead define platform::Device as
	pub struct Device<Ctx: DeviceContext = Normal>(
		Opaque<bindings::platform_dev>,
		PhantomData<Ctx>,
	);
and manually implement the AlwaysRefCounted trait.
With this we can implement methods that should only be called from
bus callbacks (such as probe()) for platform::Device<Core>. Consequently,
we make this type accessible in bus callbacks only.
Arbitrary references taken by the driver are still of type
ARef<platform::Device> and hence don't provide access to methods that are
reserved for bus callbacks.
Fixes: 683a63befc ("rust: platform: add basic platform device / driver abstractions")
Reviewed-by: Benno Lossin <benno.lossin@proton.me>
Signed-off-by: Danilo Krummrich <dakr@kernel.org>
Acked-by: Boqun Feng <boqun.feng@gmail.com>
Link: https://lore.kernel.org/r/20250314160932.100165-5-dakr@kernel.org
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
		
	
			
		
			
				
	
	
		
			52 lines
		
	
	
	
		
			1.3 KiB
		
	
	
	
		
			Rust
		
	
	
	
	
	
			
		
		
	
	
			52 lines
		
	
	
	
		
			1.3 KiB
		
	
	
	
		
			Rust
		
	
	
	
	
	
// SPDX-License-Identifier: GPL-2.0
 | 
						|
 | 
						|
//! Rust Platform driver sample.
 | 
						|
 | 
						|
use kernel::{c_str, device::Core, of, platform, prelude::*, types::ARef};
 | 
						|
 | 
						|
struct SampleDriver {
 | 
						|
    pdev: ARef<platform::Device>,
 | 
						|
}
 | 
						|
 | 
						|
struct Info(u32);
 | 
						|
 | 
						|
kernel::of_device_table!(
 | 
						|
    OF_TABLE,
 | 
						|
    MODULE_OF_TABLE,
 | 
						|
    <SampleDriver as platform::Driver>::IdInfo,
 | 
						|
    [(of::DeviceId::new(c_str!("test,rust-device")), Info(42))]
 | 
						|
);
 | 
						|
 | 
						|
impl platform::Driver for SampleDriver {
 | 
						|
    type IdInfo = Info;
 | 
						|
    const OF_ID_TABLE: Option<of::IdTable<Self::IdInfo>> = Some(&OF_TABLE);
 | 
						|
 | 
						|
    fn probe(
 | 
						|
        pdev: &platform::Device<Core>,
 | 
						|
        info: Option<&Self::IdInfo>,
 | 
						|
    ) -> Result<Pin<KBox<Self>>> {
 | 
						|
        dev_dbg!(pdev.as_ref(), "Probe Rust Platform driver sample.\n");
 | 
						|
 | 
						|
        if let Some(info) = info {
 | 
						|
            dev_info!(pdev.as_ref(), "Probed with info: '{}'.\n", info.0);
 | 
						|
        }
 | 
						|
 | 
						|
        let drvdata = KBox::new(Self { pdev: pdev.into() }, GFP_KERNEL)?;
 | 
						|
 | 
						|
        Ok(drvdata.into())
 | 
						|
    }
 | 
						|
}
 | 
						|
 | 
						|
impl Drop for SampleDriver {
 | 
						|
    fn drop(&mut self) {
 | 
						|
        dev_dbg!(self.pdev.as_ref(), "Remove Rust Platform driver sample.\n");
 | 
						|
    }
 | 
						|
}
 | 
						|
 | 
						|
kernel::module_platform_driver! {
 | 
						|
    type: SampleDriver,
 | 
						|
    name: "rust_driver_platform",
 | 
						|
    author: "Danilo Krummrich",
 | 
						|
    description: "Rust Platform driver",
 | 
						|
    license: "GPL v2",
 | 
						|
}
 |