Skip to content

Commit

Permalink
rust/kernel/platdev: get rid of register() private function
Browse files Browse the repository at this point in the history
`register()` serves no purpose, except to complicate `Registration`
construction. It's a copy-and-paste leftover from miscdev.

Eliminate this function to get the following benefits:
- `bool registered` disappears, which simplifies logic flow
- an `unsafe` block disappears

Signed-off-by: Sven Van Asbroeck <thesven73@gmail.com>
  • Loading branch information
Sven Van Asbroeck committed Jun 29, 2021
1 parent 4bfaa80 commit dbbd1dc
Showing 1 changed file with 9 additions and 31 deletions.
40 changes: 9 additions & 31 deletions rust/kernel/platdev.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ use core::{marker::PhantomPinned, pin::Pin};
/// A registration of a platform device.
#[derive(Default)]
pub struct Registration {
registered: bool,
pdrv: bindings::platform_driver,
_pin: PhantomPinned,
}
Expand Down Expand Up @@ -79,18 +78,15 @@ extern "C" fn remove_callback<P: PlatformDriver>(
}

impl Registration {
fn register<P: PlatformDriver>(
self: Pin<&mut Self>,
/// Registers a platform device.
///
/// Returns a pinned heap-allocated representation of the registration.
pub fn new_pinned<P: PlatformDriver>(
name: &'static CStr,
of_match_table: Option<&'static OfMatchTable>,
module: &'static crate::ThisModule,
) -> Result {
// SAFETY: We must ensure that we never move out of `this`.
let this = unsafe { self.get_unchecked_mut() };
if this.registered {
// Already registered.
return Err(Error::EINVAL);
}
) -> Result<Pin<Box<Self>>> {
let mut this = Box::try_new(Self::default())?;
this.pdrv.driver.name = name.as_char_ptr();
if let Some(tbl) = of_match_table {
this.pdrv.driver.of_match_table = tbl.as_ptr();
Expand All @@ -109,32 +105,14 @@ impl Registration {
if ret < 0 {
return Err(Error::from_kernel_errno(ret));
}
this.registered = true;
Ok(())
}

/// Registers a platform device.
///
/// Returns a pinned heap-allocated representation of the registration.
pub fn new_pinned<P: PlatformDriver>(
name: &'static CStr,
of_match_tbl: Option<&'static OfMatchTable>,
module: &'static crate::ThisModule,
) -> Result<Pin<Box<Self>>> {
let mut r = Pin::from(Box::try_new(Self::default())?);
r.as_mut().register::<P>(name, of_match_tbl, module)?;
Ok(r)
Ok(Pin::from(this))
}
}

impl Drop for Registration {
fn drop(&mut self) {
if self.registered {
// SAFETY: if `registered` is true, then `self.pdev` was registered
// previously, which means `platform_driver_unregister` is always
// safe to call.
unsafe { bindings::platform_driver_unregister(&mut self.pdrv) }
}
// SAFETY: `self.pdev` was registered previously.
unsafe { bindings::platform_driver_unregister(&mut self.pdrv) }
}
}

Expand Down

0 comments on commit dbbd1dc

Please sign in to comment.