Skip to content

Commit 621dcf9

Browse files
author
Sven Van Asbroeck
committed
rust/kernel/platdev: get rid of register() private function
`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 <[email protected]>
1 parent cbd2a83 commit 621dcf9

File tree

1 file changed

+9
-31
lines changed

1 file changed

+9
-31
lines changed

rust/kernel/platdev.rs

Lines changed: 9 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ use core::{marker::PhantomPinned, pin::Pin};
2020
/// A registration of a platform device.
2121
#[derive(Default)]
2222
pub struct Registration {
23-
registered: bool,
2423
of_table: Option<*const c_types::c_void>,
2524
pdrv: bindings::platform_driver,
2625
_pin: PhantomPinned,
@@ -80,18 +79,15 @@ extern "C" fn remove_callback<P: PlatformDriver>(
8079
}
8180

8281
impl Registration {
83-
fn register<P: PlatformDriver>(
84-
self: Pin<&mut Self>,
82+
/// Registers a platform device.
83+
///
84+
/// Returns a pinned heap-allocated representation of the registration.
85+
pub fn new_pinned<P: PlatformDriver>(
8586
name: &'static CStr,
8687
of_match_table: Option<OfMatchTable>,
8788
module: &'static crate::ThisModule,
88-
) -> Result {
89-
// SAFETY: We must ensure that we never move out of `this`.
90-
let this = unsafe { self.get_unchecked_mut() };
91-
if this.registered {
92-
// Already registered.
93-
return Err(Error::EINVAL);
94-
}
89+
) -> Result<Pin<Box<Self>>> {
90+
let mut this = Box::try_new(Self::default())?;
9591
this.pdrv.driver.name = name.as_char_ptr();
9692
if let Some(tbl) = of_match_table {
9793
let ptr = tbl.into_pointer();
@@ -113,32 +109,14 @@ impl Registration {
113109
if ret < 0 {
114110
return Err(Error::from_kernel_errno(ret));
115111
}
116-
this.registered = true;
117-
Ok(())
118-
}
119-
120-
/// Registers a platform device.
121-
///
122-
/// Returns a pinned heap-allocated representation of the registration.
123-
pub fn new_pinned<P: PlatformDriver>(
124-
name: &'static CStr,
125-
of_match_tbl: Option<OfMatchTable>,
126-
module: &'static crate::ThisModule,
127-
) -> Result<Pin<Box<Self>>> {
128-
let mut r = Pin::from(Box::try_new(Self::default())?);
129-
r.as_mut().register::<P>(name, of_match_tbl, module)?;
130-
Ok(r)
112+
Ok(Pin::from(this))
131113
}
132114
}
133115

134116
impl Drop for Registration {
135117
fn drop(&mut self) {
136-
if self.registered {
137-
// SAFETY: if `registered` is true, then `self.pdev` was registered
138-
// previously, which means `platform_driver_unregister` is always
139-
// safe to call.
140-
unsafe { bindings::platform_driver_unregister(&mut self.pdrv) }
141-
}
118+
// SAFETY: `self.pdev` was registered previously.
119+
unsafe { bindings::platform_driver_unregister(&mut self.pdrv) }
142120
if let Some(ptr) = self.of_table {
143121
// SAFETY: `ptr` came from an `OfMatchTable`.
144122
let tbl = unsafe { OfMatchTable::from_pointer(ptr) };

0 commit comments

Comments
 (0)