Skip to content

Commit 0861303

Browse files
committed
spi: Remove option for spi_driver
spi: Remove option for spi_driver field spi: Fix mutability of SpiDevice parameter Co-developed-by: Martin Schmidt <[email protected]> Signed-off-by: Martin Schmidt <[email protected]> Co-developed-by: Arthur Cohen <[email protected]> Signed-off-by: Arthur Cohen <[email protected]> Signed-off-by: Esteban Blanc <[email protected]>
1 parent f650715 commit 0861303

File tree

1 file changed

+17
-18
lines changed

1 file changed

+17
-18
lines changed

rust/kernel/spi.rs

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ pub struct DriverRegistration {
2424
this_module: &'static crate::ThisModule,
2525
registered: bool,
2626
name: CStr<'static>,
27-
spi_driver: Option<bindings::spi_driver>,
27+
spi_driver: bindings::spi_driver,
2828
}
2929

3030
pub struct ToUse {
@@ -42,15 +42,15 @@ pub const USE_NONE: ToUse = ToUse {
4242
pub trait SpiMethods {
4343
const TO_USE: ToUse;
4444

45-
fn probe(_spi_dev: SpiDevice) -> Result {
45+
fn probe(mut _spi_dev: SpiDevice) -> Result {
4646
Ok(())
4747
}
4848

49-
fn remove(_spi_dev: SpiDevice) -> Result {
49+
fn remove(mut _spi_dev: SpiDevice) -> Result {
5050
Ok(())
5151
}
5252

53-
fn shutdown(_spi_dev: SpiDevice) {}
53+
fn shutdown(mut _spi_dev: SpiDevice) {}
5454
}
5555

5656
/// Populate the TO_USE field in the `SpiMethods` implementer
@@ -97,7 +97,7 @@ impl DriverRegistration {
9797
this_module,
9898
name,
9999
registered: false,
100-
spi_driver: None,
100+
spi_driver: bindings::spi_driver::default(),
101101
}
102102
}
103103

@@ -147,23 +147,23 @@ impl DriverRegistration {
147147
}
148148
}
149149

150-
let mut spi_driver = bindings::spi_driver::default();
151-
spi_driver.driver.name = self.name.as_ptr() as *const c_types::c_char;
152-
153-
spi_driver.probe = maybe_get_wrapper(T::TO_USE.probe, DriverRegistration::probe_wrapper::<T>);
154-
spi_driver.remove = maybe_get_wrapper(T::TO_USE.remove, DriverRegistration::remove_wrapper::<T>);
155-
spi_driver.shutdown = maybe_get_wrapper(T::TO_USE.shutdown, DriverRegistration::shutdown_wrapper::<T>);
156-
157150
let this = unsafe { self.get_unchecked_mut() };
158151
if this.registered {
159152
return Err(Error::EINVAL);
160153
}
161154

162-
this.spi_driver = Some(spi_driver);
155+
this.spi_driver.driver.name = this.name.as_ptr() as *const c_types::c_char;
156+
this.spi_driver.probe =
157+
maybe_get_wrapper(T::TO_USE.probe, DriverRegistration::probe_wrapper::<T>);
158+
this.spi_driver.remove =
159+
maybe_get_wrapper(T::TO_USE.remove, DriverRegistration::remove_wrapper::<T>);
160+
this.spi_driver.shutdown = maybe_get_wrapper(
161+
T::TO_USE.shutdown,
162+
DriverRegistration::shutdown_wrapper::<T>,
163+
);
163164

164-
let res = unsafe {
165-
bindings::__spi_register_driver(this.this_module.0, this.spi_driver.as_mut().unwrap())
166-
};
165+
let res =
166+
unsafe { bindings::__spi_register_driver(this.this_module.0, &mut this.spi_driver) };
167167

168168
match res {
169169
0 => {
@@ -177,8 +177,7 @@ impl DriverRegistration {
177177

178178
impl Drop for DriverRegistration {
179179
fn drop(&mut self) {
180-
unsafe { bindings::driver_unregister(&mut self.spi_driver.as_mut().unwrap().driver) }
181-
// FIXME: No unwrap? But it's safe?
180+
unsafe { bindings::driver_unregister(&mut self.spi_driver.driver) }
182181
}
183182
}
184183

0 commit comments

Comments
 (0)