Skip to content

Commit ef16ba0

Browse files
wedsonafFabo
authored andcommitted
rust: device: Add a stub abstraction for devices
Add a Device type which represents an owned reference to a generic struct device. This minimal implementation just handles reference counting and allows the user to get the device name. Lina: Rewrote commit message, dropped the Amba bits, and squashed in simple changes to the core Device code from latter commits in rust-for-linux/rust. Also include the rust_helper_dev_get_drvdata helper which will be needed by consumers later on anyway. Co-developed-by: Miguel Ojeda <[email protected]> Signed-off-by: Miguel Ojeda <[email protected]> Signed-off-by: Wedson Almeida Filho <[email protected]> Signed-off-by: Asahi Lina <[email protected]>
1 parent 5590128 commit ef16ba0

File tree

2 files changed

+88
-1
lines changed

2 files changed

+88
-1
lines changed

rust/helpers.c

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#include <kunit/test-bug.h>
2424
#include <linux/bug.h>
2525
#include <linux/build_bug.h>
26+
#include <linux/device.h>
2627
#include <linux/err.h>
2728
#include <linux/errname.h>
2829
#include <linux/mutex.h>
@@ -157,6 +158,18 @@ void rust_helper_init_work_with_key(struct work_struct *work, work_func_t func,
157158
}
158159
EXPORT_SYMBOL_GPL(rust_helper_init_work_with_key);
159160

161+
void *rust_helper_dev_get_drvdata(struct device *dev)
162+
{
163+
return dev_get_drvdata(dev);
164+
}
165+
EXPORT_SYMBOL_GPL(rust_helper_dev_get_drvdata);
166+
167+
const char *rust_helper_dev_name(const struct device *dev)
168+
{
169+
return dev_name(dev);
170+
}
171+
EXPORT_SYMBOL_GPL(rust_helper_dev_name);
172+
160173
/*
161174
* `bindgen` binds the C `size_t` type as the Rust `usize` type, so we can
162175
* use it in contexts where Rust expects a `usize` like slice (array) indices.

rust/kernel/device.rs

Lines changed: 75 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
//!
55
//! C header: [`include/linux/device.h`](../../../../include/linux/device.h)
66
7-
use crate::bindings;
7+
use crate::{bindings, str::CStr};
88

99
/// A raw device.
1010
///
@@ -20,4 +20,78 @@ use crate::bindings;
2020
pub unsafe trait RawDevice {
2121
/// Returns the raw `struct device` related to `self`.
2222
fn raw_device(&self) -> *mut bindings::device;
23+
24+
/// Returns the name of the device.
25+
fn name(&self) -> &CStr {
26+
let ptr = self.raw_device();
27+
28+
// SAFETY: `ptr` is valid because `self` keeps it alive.
29+
let name = unsafe { bindings::dev_name(ptr) };
30+
31+
// SAFETY: The name of the device remains valid while it is alive (because the device is
32+
// never renamed, per the safety requirement of this trait). This is guaranteed to be the
33+
// case because the reference to `self` outlives the one of the returned `CStr` (enforced
34+
// by the compiler because of their lifetimes).
35+
unsafe { CStr::from_char_ptr(name) }
36+
}
37+
}
38+
39+
/// A ref-counted device.
40+
///
41+
/// # Invariants
42+
///
43+
/// `ptr` is valid, non-null, and has a non-zero reference count. One of the references is owned by
44+
/// `self`, and will be decremented when `self` is dropped.
45+
pub struct Device {
46+
pub(crate) ptr: *mut bindings::device,
47+
}
48+
49+
// SAFETY: `Device` only holds a pointer to a C device, which is safe to be used from any thread.
50+
unsafe impl Send for Device {}
51+
52+
// SAFETY: `Device` only holds a pointer to a C device, references to which are safe to be used
53+
// from any thread.
54+
unsafe impl Sync for Device {}
55+
56+
impl Device {
57+
/// Creates a new device instance.
58+
///
59+
/// # Safety
60+
///
61+
/// Callers must ensure that `ptr` is valid, non-null, and has a non-zero reference count.
62+
pub unsafe fn new(ptr: *mut bindings::device) -> Self {
63+
// SAFETY: By the safety requirements, ptr is valid and its refcounted will be incremented.
64+
unsafe { bindings::get_device(ptr) };
65+
// INVARIANT: The safety requirements satisfy all but one invariant, which is that `self`
66+
// owns a reference. This is satisfied by the call to `get_device` above.
67+
Self { ptr }
68+
}
69+
70+
/// Creates a new device instance from an existing [`RawDevice`] instance.
71+
pub fn from_dev(dev: &dyn RawDevice) -> Self {
72+
// SAFETY: The requirements are satisfied by the existence of `RawDevice` and its safety
73+
// requirements.
74+
unsafe { Self::new(dev.raw_device()) }
75+
}
76+
}
77+
78+
// SAFETY: The device returned by `raw_device` is the one for which we hold a reference.
79+
unsafe impl RawDevice for Device {
80+
fn raw_device(&self) -> *mut bindings::device {
81+
self.ptr
82+
}
83+
}
84+
85+
impl Drop for Device {
86+
fn drop(&mut self) {
87+
// SAFETY: By the type invariants, we know that `self` owns a reference, so it is safe to
88+
// relinquish it now.
89+
unsafe { bindings::put_device(self.ptr) };
90+
}
91+
}
92+
93+
impl Clone for Device {
94+
fn clone(&self) -> Self {
95+
Device::from_dev(self)
96+
}
2397
}

0 commit comments

Comments
 (0)