summaryrefslogtreecommitdiff
path: root/rust
diff options
context:
space:
mode:
authorLee Jones <lee@kernel.org>2024-12-10 12:39:02 +0300
committerGreg Kroah-Hartman <gregkh@linuxfoundation.org>2024-12-16 18:12:34 +0300
commit284ae0be4dcafff0a154c1e69e7430c144a7ddc2 (patch)
tree9c51223039af8413237737347f3d62ef4837d52b /rust
parent88441d5c6d17211bcbd5b429205b09c25598f756 (diff)
downloadlinux-284ae0be4dcafff0a154c1e69e7430c144a7ddc2.tar.xz
rust: miscdevice: Provide accessor to pull out miscdevice::this_device
There are situations where a pointer to a `struct device` will become necessary (e.g. for calling into dev_*() functions). This accessor allows callers to pull this out from the `struct miscdevice`. Signed-off-by: Lee Jones <lee@kernel.org> Signed-off-by: Alice Ryhl <aliceryhl@google.com> Tested-by: Lee Jones <lee@kernel.org> Reviewed-by: Danilo Krummrich <dakr@kernel.org> Link: https://lore.kernel.org/r/20241210-miscdevice-file-param-v3-3-b2a79b666dc5@google.com Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Diffstat (limited to 'rust')
-rw-r--r--rust/kernel/miscdevice.rs11
1 files changed, 11 insertions, 0 deletions
diff --git a/rust/kernel/miscdevice.rs b/rust/kernel/miscdevice.rs
index 75a9d26c8001..20895e809607 100644
--- a/rust/kernel/miscdevice.rs
+++ b/rust/kernel/miscdevice.rs
@@ -10,6 +10,7 @@
use crate::{
bindings,
+ device::Device,
error::{to_result, Error, Result, VTABLE_DEFAULT_ERROR},
fs::File,
prelude::*,
@@ -85,6 +86,16 @@ impl<T: MiscDevice> MiscDeviceRegistration<T> {
pub fn as_raw(&self) -> *mut bindings::miscdevice {
self.inner.get()
}
+
+ /// Access the `this_device` field.
+ pub fn device(&self) -> &Device {
+ // SAFETY: This can only be called after a successful register(), which always
+ // initialises `this_device` with a valid device. Furthermore, the signature of this
+ // function tells the borrow-checker that the `&Device` reference must not outlive the
+ // `&MiscDeviceRegistration<T>` used to obtain it, so the last use of the reference must be
+ // before the underlying `struct miscdevice` is destroyed.
+ unsafe { Device::as_ref((*self.as_raw()).this_device) }
+ }
}
#[pinned_drop]