Skip to content

Commit

Permalink
Merge pull request torvalds#613 from wedsonaf/this-module
Browse files Browse the repository at this point in the history
rust: add `ThisModule` to the arguments of driver registration
  • Loading branch information
wedsonaf authored Jan 10, 2022
2 parents 7739ad0 + 269b6bd commit 4bb4f38
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 6 deletions.
4 changes: 3 additions & 1 deletion rust/kernel/amba.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
use crate::{
bindings, c_types, device, driver, error::from_kernel_result, io_mem::Resource, power,
str::CStr, to_result, types::PointerWrapper, Error, Result,
str::CStr, to_result, types::PointerWrapper, Error, Result, ThisModule,
};
use core::{marker::PhantomData, ops::Deref};

Expand Down Expand Up @@ -72,12 +72,14 @@ where
unsafe fn register(
reg: *mut bindings::amba_driver,
name: &'static CStr,
module: &'static ThisModule,
id_table: *const bindings::amba_id,
) -> Result {
// SAFETY: By the safety requirements of this function (defined in the trait defintion),
// `reg` is non-null and valid.
let amba = unsafe { &mut *reg };
amba.drv.name = name.as_char_ptr();
amba.drv.owner = module.0;
amba.id_table = id_table;
amba.probe = Some(probe_callback::<T>);
amba.remove = Some(remove_callback::<T>);
Expand Down
16 changes: 11 additions & 5 deletions rust/kernel/driver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ pub trait DriverOps {
unsafe fn register(
reg: *mut Self::RegType,
name: &'static CStr,
module: &'static ThisModule,
id_table: *const Self::RawIdType,
) -> Result;

Expand Down Expand Up @@ -85,17 +86,21 @@ impl<T: DriverOps> Registration<T> {
/// Allocates a pinned registration object and registers it.
///
/// Returns a pinned heap-allocated representation of the registration.
pub fn new_pinned(name: &'static CStr) -> Result<Pin<Box<Self>>> {
pub fn new_pinned(name: &'static CStr, module: &'static ThisModule) -> Result<Pin<Box<Self>>> {
let mut reg = Pin::from(Box::try_new(Self::new())?);
reg.as_mut().register(name)?;
reg.as_mut().register(name, module)?;
Ok(reg)
}

/// Registers a driver with its subsystem.
///
/// It must be pinned because the memory block that represents the registration is potentially
/// self-referential.
pub fn register(self: Pin<&mut Self>, name: &'static CStr) -> Result {
pub fn register(
self: Pin<&mut Self>,
name: &'static CStr,
module: &'static ThisModule,
) -> Result {
// SAFETY: We never move out of `this`.
let this = unsafe { self.get_unchecked_mut() };
if this.is_registered {
Expand All @@ -114,6 +119,7 @@ impl<T: DriverOps> Registration<T> {
T::register(
this.concrete_reg.get(),
name,
module,
&this.id_table[0] as *const _ as *const _,
)
}?;
Expand Down Expand Up @@ -174,9 +180,9 @@ pub struct Module<T: DriverOps> {
}

impl<T: DriverOps> KernelModule for Module<T> {
fn init(name: &'static CStr, _module: &'static ThisModule) -> Result<Self> {
fn init(name: &'static CStr, module: &'static ThisModule) -> Result<Self> {
Ok(Self {
_driver: Registration::new_pinned(name)?,
_driver: Registration::new_pinned(name, module)?,
})
}
}
Expand Down

0 comments on commit 4bb4f38

Please sign in to comment.