Skip to content

Commit 35f852a

Browse files
wedsonafmetaspace
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]> --- Modified to apply to this tree.
1 parent ae13f48 commit 35f852a

File tree

3 files changed

+91
-1
lines changed

3 files changed

+91
-1
lines changed

rust/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ RUST_HELPERS := \
1010
bug \
1111
build_assert \
1212
build_bug \
13+
device \
1314
err \
1415
kunit \
1516
mutex \

rust/helpers/device.c

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// SPDX-License-Identifier: GPL-2.0
2+
3+
#include <linux/device.h>
4+
5+
void *rust_helper_dev_get_drvdata(struct device *dev)
6+
{
7+
return dev_get_drvdata(dev);
8+
}
9+
EXPORT_SYMBOL_GPL(rust_helper_dev_get_drvdata);
10+
11+
const char *rust_helper_dev_name(const struct device *dev)
12+
{
13+
return dev_name(dev);
14+
}
15+
EXPORT_SYMBOL_GPL(rust_helper_dev_name);

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+
Self::from_dev(self)
96+
}
2397
}

0 commit comments

Comments
 (0)