Skip to content

Commit ce32274

Browse files
wedsonafFabo
authored andcommitted
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 <[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 fe66e7c commit ce32274

File tree

3 files changed

+25
-0
lines changed

3 files changed

+25
-0
lines changed

rust/bindings/bindings_helper.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
*/
88

99
#include <kunit/test.h>
10+
#include <linux/device.h>
1011
#include <linux/errname.h>
1112
#include <linux/slab.h>
1213
#include <linux/refcount.h>

rust/kernel/device.rs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// SPDX-License-Identifier: GPL-2.0
2+
3+
//! Generic devices that are part of the kernel's driver model.
4+
//!
5+
//! C header: [`include/linux/device.h`](../../../../include/linux/device.h)
6+
7+
use crate::bindings;
8+
9+
/// A raw device.
10+
///
11+
/// # Safety
12+
///
13+
/// Implementers must ensure that the `*mut device` returned by [`RawDevice::raw_device`] is
14+
/// related to `self`, that is, actions on it will affect `self`. For example, if one calls
15+
/// `get_device`, then the refcount on the device represented by `self` will be incremented.
16+
///
17+
/// Additionally, implementers must ensure that the device is never renamed. Commit a5462516aa99
18+
/// ("driver-core: document restrictions on device_rename()") has details on why `device_rename`
19+
/// should not be used.
20+
pub unsafe trait RawDevice {
21+
/// Returns the raw `struct device` related to `self`.
22+
fn raw_device(&self) -> *mut bindings::device;
23+
}

rust/kernel/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ extern crate self as kernel;
3333
#[cfg(not(testlib))]
3434
mod allocator;
3535
mod build_assert;
36+
pub mod device;
3637
pub mod error;
3738
pub mod init;
3839
pub mod ioctl;

0 commit comments

Comments
 (0)