Skip to content

Commit c1d4519

Browse files
author
Danilo Krummrich
committed
rust: driver: add DEVICE_DRIVER_OFFSET to the DriverLayout trait
Add an associated const DEVICE_DRIVER_OFFSET to the DriverLayout trait indicating the offset of the embedded struct device_driver within Self::DriverType, i.e. the specific driver structs, such as struct pci_driver or struct platform_driver. Acked-by: Alice Ryhl <aliceryhl@google.com> Acked-by: Igor Korotin <igor.korotin.linux@gmail.com> Link: https://patch.msgid.link/20260107103511.570525-5-dakr@kernel.org Signed-off-by: Danilo Krummrich <dakr@kernel.org>
1 parent 0af1a9e commit c1d4519

6 files changed

Lines changed: 22 additions & 1 deletion

File tree

rust/kernel/auxiliary.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,11 @@ pub struct Adapter<T: Driver>(T);
2525

2626
// SAFETY:
2727
// - `bindings::auxiliary_driver` is a C type declared as `repr(C)`.
28+
// - `struct auxiliary_driver` embeds a `struct device_driver`.
29+
// - `DEVICE_DRIVER_OFFSET` is the correct byte offset to the embedded `struct device_driver`.
2830
unsafe impl<T: Driver + 'static> driver::DriverLayout for Adapter<T> {
2931
type DriverType = bindings::auxiliary_driver;
32+
const DEVICE_DRIVER_OFFSET: usize = core::mem::offset_of!(Self::DriverType, driver);
3033
}
3134

3235
// SAFETY: A call to `unregister` for a given instance of `DriverType` is guaranteed to be valid if

rust/kernel/driver.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,10 +107,16 @@ use pin_init::{pin_data, pinned_drop, PinInit};
107107
/// # Safety
108108
///
109109
/// Implementors must guarantee that:
110-
/// - `DriverType` is `repr(C)`.
110+
/// - `DriverType` is `repr(C)`,
111+
/// - `DriverType` embeds a valid `struct device_driver` at byte offset `DEVICE_DRIVER_OFFSET`.
111112
pub unsafe trait DriverLayout {
112113
/// The specific driver type embedding a `struct device_driver`.
113114
type DriverType: Default;
115+
116+
/// Byte offset of the embedded `struct device_driver` within `DriverType`.
117+
///
118+
/// This must correspond exactly to the location of the embedded `struct device_driver` field.
119+
const DEVICE_DRIVER_OFFSET: usize;
114120
}
115121

116122
/// The [`RegistrationOps`] trait serves as generic interface for subsystems (e.g., PCI, Platform,

rust/kernel/i2c.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,8 +94,11 @@ pub struct Adapter<T: Driver>(T);
9494

9595
// SAFETY:
9696
// - `bindings::i2c_driver` is a C type declared as `repr(C)`.
97+
// - `struct i2c_driver` embeds a `struct device_driver`.
98+
// - `DEVICE_DRIVER_OFFSET` is the correct byte offset to the embedded `struct device_driver`.
9799
unsafe impl<T: Driver + 'static> driver::DriverLayout for Adapter<T> {
98100
type DriverType = bindings::i2c_driver;
101+
const DEVICE_DRIVER_OFFSET: usize = core::mem::offset_of!(Self::DriverType, driver);
99102
}
100103

101104
// SAFETY: A call to `unregister` for a given instance of `DriverType` is guaranteed to be valid if

rust/kernel/pci.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,11 @@ pub struct Adapter<T: Driver>(T);
5252

5353
// SAFETY:
5454
// - `bindings::pci_driver` is a C type declared as `repr(C)`.
55+
// - `struct pci_driver` embeds a `struct device_driver`.
56+
// - `DEVICE_DRIVER_OFFSET` is the correct byte offset to the embedded `struct device_driver`.
5557
unsafe impl<T: Driver + 'static> driver::DriverLayout for Adapter<T> {
5658
type DriverType = bindings::pci_driver;
59+
const DEVICE_DRIVER_OFFSET: usize = core::mem::offset_of!(Self::DriverType, driver);
5760
}
5861

5962
// SAFETY: A call to `unregister` for a given instance of `DriverType` is guaranteed to be valid if

rust/kernel/platform.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,11 @@ pub struct Adapter<T: Driver>(T);
2828

2929
// SAFETY:
3030
// - `bindings::platform_driver` is a C type declared as `repr(C)`.
31+
// - `struct platform_driver` embeds a `struct device_driver`.
32+
// - `DEVICE_DRIVER_OFFSET` is the correct byte offset to the embedded `struct device_driver`.
3133
unsafe impl<T: Driver + 'static> driver::DriverLayout for Adapter<T> {
3234
type DriverType = bindings::platform_driver;
35+
const DEVICE_DRIVER_OFFSET: usize = core::mem::offset_of!(Self::DriverType, driver);
3336
}
3437

3538
// SAFETY: A call to `unregister` for a given instance of `DriverType` is guaranteed to be valid if

rust/kernel/usb.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,11 @@ pub struct Adapter<T: Driver>(T);
2929

3030
// SAFETY:
3131
// - `bindings::usb_driver` is a C type declared as `repr(C)`.
32+
// - `struct usb_driver` embeds a `struct device_driver`.
33+
// - `DEVICE_DRIVER_OFFSET` is the correct byte offset to the embedded `struct device_driver`.
3234
unsafe impl<T: Driver + 'static> driver::DriverLayout for Adapter<T> {
3335
type DriverType = bindings::usb_driver;
36+
const DEVICE_DRIVER_OFFSET: usize = core::mem::offset_of!(Self::DriverType, driver);
3437
}
3538

3639
// SAFETY: A call to `unregister` for a given instance of `DriverType` is guaranteed to be valid if

0 commit comments

Comments
 (0)