Skip to content

Commit dbbd1dc

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 4bfaa80 commit dbbd1dc

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
pdrv: bindings::platform_driver,
2524
_pin: PhantomPinned,
2625
}
@@ -79,18 +78,15 @@ extern "C" fn remove_callback<P: PlatformDriver>(
7978
}
8079

8180
impl Registration {
82-
fn register<P: PlatformDriver>(
83-
self: Pin<&mut Self>,
81+
/// Registers a platform device.
82+
///
83+
/// Returns a pinned heap-allocated representation of the registration.
84+
pub fn new_pinned<P: PlatformDriver>(
8485
name: &'static CStr,
8586
of_match_table: Option<&'static OfMatchTable>,
8687
module: &'static crate::ThisModule,
87-
) -> Result {
88-
// SAFETY: We must ensure that we never move out of `this`.
89-
let this = unsafe { self.get_unchecked_mut() };
90-
if this.registered {
91-
// Already registered.
92-
return Err(Error::EINVAL);
93-
}
88+
) -> Result<Pin<Box<Self>>> {
89+
let mut this = Box::try_new(Self::default())?;
9490
this.pdrv.driver.name = name.as_char_ptr();
9591
if let Some(tbl) = of_match_table {
9692
this.pdrv.driver.of_match_table = tbl.as_ptr();
@@ -109,32 +105,14 @@ impl Registration {
109105
if ret < 0 {
110106
return Err(Error::from_kernel_errno(ret));
111107
}
112-
this.registered = true;
113-
Ok(())
114-
}
115-
116-
/// Registers a platform device.
117-
///
118-
/// Returns a pinned heap-allocated representation of the registration.
119-
pub fn new_pinned<P: PlatformDriver>(
120-
name: &'static CStr,
121-
of_match_tbl: Option<&'static OfMatchTable>,
122-
module: &'static crate::ThisModule,
123-
) -> Result<Pin<Box<Self>>> {
124-
let mut r = Pin::from(Box::try_new(Self::default())?);
125-
r.as_mut().register::<P>(name, of_match_tbl, module)?;
126-
Ok(r)
108+
Ok(Pin::from(this))
127109
}
128110
}
129111

130112
impl Drop for Registration {
131113
fn drop(&mut self) {
132-
if self.registered {
133-
// SAFETY: if `registered` is true, then `self.pdev` was registered
134-
// previously, which means `platform_driver_unregister` is always
135-
// safe to call.
136-
unsafe { bindings::platform_driver_unregister(&mut self.pdrv) }
137-
}
114+
// SAFETY: `self.pdev` was registered previously.
115+
unsafe { bindings::platform_driver_unregister(&mut self.pdrv) }
138116
}
139117
}
140118

0 commit comments

Comments
 (0)