diff options
author | Wedson Almeida Filho <wedsonaf@gmail.com> | 2023-02-24 19:53:14 +0900 |
---|---|---|
committer | Danilo Krummrich <dakr@redhat.com> | 2023-11-22 22:00:53 +0100 |
commit | d8394c6526f1fb709166c95c22490a24a02a0b7e (patch) | |
tree | f91061b29d193ddea0766685be32010632beef7a /rust | |
parent | 5027ca462ef96ecc4e19c4aca0dd7813dc6bae17 (diff) |
rust: device: Add a minimal RawDevice trait
Add a RawDevice trait which can be implemented by any type representing
a device class (such as a PlatformDevice). This is the minimum amount of
Device support code required to unblock abstractions that need to take
device pointers.
Lina: Rewrote commit message, and dropped everything except RawDevice.
Co-developed-by: Miguel Ojeda <ojeda@kernel.org>
Signed-off-by: Miguel Ojeda <ojeda@kernel.org>
Signed-off-by: Wedson Almeida Filho <wedsonaf@gmail.com>
Signed-off-by: Asahi Lina <lina@asahilina.net>
Diffstat (limited to 'rust')
-rw-r--r-- | rust/bindings/bindings_helper.h | 1 | ||||
-rw-r--r-- | rust/kernel/device.rs | 23 | ||||
-rw-r--r-- | rust/kernel/lib.rs | 1 |
3 files changed, 25 insertions, 0 deletions
diff --git a/rust/bindings/bindings_helper.h b/rust/bindings/bindings_helper.h index c91a3c24f607..076a6af14395 100644 --- a/rust/bindings/bindings_helper.h +++ b/rust/bindings/bindings_helper.h @@ -7,6 +7,7 @@ */ #include <kunit/test.h> +#include <linux/device.h> #include <linux/errname.h> #include <linux/slab.h> #include <linux/refcount.h> diff --git a/rust/kernel/device.rs b/rust/kernel/device.rs new file mode 100644 index 000000000000..9be021e393ca --- /dev/null +++ b/rust/kernel/device.rs @@ -0,0 +1,23 @@ +// SPDX-License-Identifier: GPL-2.0 + +//! Generic devices that are part of the kernel's driver model. +//! +//! C header: [`include/linux/device.h`](../../../../include/linux/device.h) + +use crate::bindings; + +/// A raw device. +/// +/// # Safety +/// +/// Implementers must ensure that the `*mut device` returned by [`RawDevice::raw_device`] is +/// related to `self`, that is, actions on it will affect `self`. For example, if one calls +/// `get_device`, then the refcount on the device represented by `self` will be incremented. +/// +/// Additionally, implementers must ensure that the device is never renamed. Commit a5462516aa99 +/// ("driver-core: document restrictions on device_rename()") has details on why `device_rename` +/// should not be used. +pub unsafe trait RawDevice { + /// Returns the raw `struct device` related to `self`. + fn raw_device(&self) -> *mut bindings::device; +} diff --git a/rust/kernel/lib.rs b/rust/kernel/lib.rs index 82a841e02e6d..fe4216b89b69 100644 --- a/rust/kernel/lib.rs +++ b/rust/kernel/lib.rs @@ -31,6 +31,7 @@ extern crate self as kernel; #[cfg(not(testlib))] mod allocator; mod build_assert; +pub mod device; pub mod error; pub mod init; pub mod ioctl; |