mirror of https://github.com/torvalds/linux.git
rust: platform: use generic device drvdata accessors
Take advantage of the generic drvdata accessors of the generic Device type. While at it, use from_result() instead of match. Link: https://lore.kernel.org/r/20250621195118.124245-4-dakr@kernel.org Signed-off-by: Danilo Krummrich <dakr@kernel.org>
This commit is contained in:
parent
880dec12a2
commit
f0a68a912c
|
|
@ -2,16 +2,6 @@
|
||||||
|
|
||||||
#include <linux/platform_device.h>
|
#include <linux/platform_device.h>
|
||||||
|
|
||||||
void *rust_helper_platform_get_drvdata(const struct platform_device *pdev)
|
|
||||||
{
|
|
||||||
return platform_get_drvdata(pdev);
|
|
||||||
}
|
|
||||||
|
|
||||||
void rust_helper_platform_set_drvdata(struct platform_device *pdev, void *data)
|
|
||||||
{
|
|
||||||
platform_set_drvdata(pdev, data);
|
|
||||||
}
|
|
||||||
|
|
||||||
bool rust_helper_dev_is_platform(const struct device *dev)
|
bool rust_helper_dev_is_platform(const struct device *dev)
|
||||||
{
|
{
|
||||||
return dev_is_platform(dev);
|
return dev_is_platform(dev);
|
||||||
|
|
|
||||||
|
|
@ -6,10 +6,10 @@
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
acpi, bindings, container_of, device, driver,
|
acpi, bindings, container_of, device, driver,
|
||||||
error::{to_result, Result},
|
error::{from_result, to_result, Result},
|
||||||
of,
|
of,
|
||||||
prelude::*,
|
prelude::*,
|
||||||
types::{ForeignOwnable, Opaque},
|
types::Opaque,
|
||||||
ThisModule,
|
ThisModule,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
@ -66,30 +66,28 @@ extern "C" fn probe_callback(pdev: *mut bindings::platform_device) -> kernel::ff
|
||||||
// `struct platform_device`.
|
// `struct platform_device`.
|
||||||
//
|
//
|
||||||
// INVARIANT: `pdev` is valid for the duration of `probe_callback()`.
|
// INVARIANT: `pdev` is valid for the duration of `probe_callback()`.
|
||||||
let pdev = unsafe { &*pdev.cast::<Device<device::Core>>() };
|
let pdev = unsafe { &*pdev.cast::<Device<device::CoreInternal>>() };
|
||||||
|
|
||||||
let info = <Self as driver::Adapter>::id_info(pdev.as_ref());
|
let info = <Self as driver::Adapter>::id_info(pdev.as_ref());
|
||||||
match T::probe(pdev, info) {
|
|
||||||
Ok(data) => {
|
|
||||||
// Let the `struct platform_device` own a reference of the driver's private data.
|
|
||||||
// SAFETY: By the type invariant `pdev.as_raw` returns a valid pointer to a
|
|
||||||
// `struct platform_device`.
|
|
||||||
unsafe { bindings::platform_set_drvdata(pdev.as_raw(), data.into_foreign() as _) };
|
|
||||||
}
|
|
||||||
Err(err) => return Error::to_errno(err),
|
|
||||||
}
|
|
||||||
|
|
||||||
0
|
from_result(|| {
|
||||||
|
let data = T::probe(pdev, info)?;
|
||||||
|
|
||||||
|
pdev.as_ref().set_drvdata(data);
|
||||||
|
Ok(0)
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
extern "C" fn remove_callback(pdev: *mut bindings::platform_device) {
|
extern "C" fn remove_callback(pdev: *mut bindings::platform_device) {
|
||||||
// SAFETY: `pdev` is a valid pointer to a `struct platform_device`.
|
// SAFETY: The platform bus only ever calls the remove callback with a valid pointer to a
|
||||||
let ptr = unsafe { bindings::platform_get_drvdata(pdev) }.cast();
|
// `struct platform_device`.
|
||||||
|
//
|
||||||
|
// INVARIANT: `pdev` is valid for the duration of `probe_callback()`.
|
||||||
|
let pdev = unsafe { &*pdev.cast::<Device<device::CoreInternal>>() };
|
||||||
|
|
||||||
// SAFETY: `remove_callback` is only ever called after a successful call to
|
// SAFETY: `remove_callback` is only ever called after a successful call to
|
||||||
// `probe_callback`, hence it's guaranteed that `ptr` points to a valid and initialized
|
// `probe_callback`, hence it's guaranteed that `Device::set_drvdata()` has been called
|
||||||
// `KBox<T>` pointer created through `KBox::into_foreign`.
|
// and stored a `Pin<KBox<T>>`.
|
||||||
let _ = unsafe { KBox::<T>::from_foreign(ptr) };
|
let _ = unsafe { pdev.as_ref().drvdata_obtain::<Pin<KBox<T>>>() };
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue