Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion rust/kernel/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@
const_panic,
const_raw_ptr_deref,
const_unreachable_unchecked,
try_reserve
try_reserve,
bool_to_option
)]
#![deny(clippy::complexity)]
#![deny(clippy::correctness)]
Expand Down
35 changes: 14 additions & 21 deletions rust/kernel/spi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,38 +140,31 @@ impl DriverRegistration {

// FIXME: Add documentation
pub fn register<T: SpiMethods>(self: Pin<&mut Self>) -> Result {
fn maybe_get_wrapper<F>(vtable_value: bool, func: F) -> Option<F> {
match vtable_value {
false => None,
true => Some(func),
}
}

let this = unsafe { self.get_unchecked_mut() };
if this.registered {
return Err(Error::EINVAL);
}

this.spi_driver.driver.name = this.name.as_ptr() as *const c_types::c_char;
this.spi_driver.probe =
maybe_get_wrapper(T::TO_USE.probe, DriverRegistration::probe_wrapper::<T>);
this.spi_driver.remove =
maybe_get_wrapper(T::TO_USE.remove, DriverRegistration::remove_wrapper::<T>);
this.spi_driver.shutdown = maybe_get_wrapper(
T::TO_USE.shutdown,
DriverRegistration::shutdown_wrapper::<T>,
);
this.spi_driver.probe = T::TO_USE
.probe
.then_some(DriverRegistration::probe_wrapper::<T>);
this.spi_driver.remove = T::TO_USE
.remove
.then_some(DriverRegistration::remove_wrapper::<T>);
this.spi_driver.shutdown = T::TO_USE
.shutdown
.then_some(DriverRegistration::shutdown_wrapper::<T>);

let res =
unsafe { bindings::__spi_register_driver(this.this_module.0, &mut this.spi_driver) };

match res {
0 => {
this.registered = true;
Ok(())
}
_ => Err(Error::from_kernel_errno(res)),
if res != 0 {
return Err(Error::from_kernel_errno(res));
}

this.registered = true;
Ok(())
}
}

Expand Down