Commit c1d4519e authored by Danilo Krummrich's avatar Danilo Krummrich
Browse files

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: default avatarAlice Ryhl <aliceryhl@google.com>
Acked-by: default avatarIgor Korotin <igor.korotin.linux@gmail.com>
Link: https://patch.msgid.link/20260107103511.570525-5-dakr@kernel.org


Signed-off-by: default avatarDanilo Krummrich <dakr@kernel.org>
parent 0af1a9e4
Loading
Loading
Loading
Loading
+3 −0
Original line number Diff line number Diff line
@@ -25,8 +25,11 @@

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

// SAFETY: A call to `unregister` for a given instance of `DriverType` is guaranteed to be valid if
+7 −1
Original line number Diff line number Diff line
@@ -107,10 +107,16 @@
/// # Safety
///
/// Implementors must guarantee that:
/// - `DriverType` is `repr(C)`.
/// - `DriverType` is `repr(C)`,
/// - `DriverType` embeds a valid `struct device_driver` at byte offset `DEVICE_DRIVER_OFFSET`.
pub unsafe trait DriverLayout {
    /// The specific driver type embedding a `struct device_driver`.
    type DriverType: Default;

    /// Byte offset of the embedded `struct device_driver` within `DriverType`.
    ///
    /// This must correspond exactly to the location of the embedded `struct device_driver` field.
    const DEVICE_DRIVER_OFFSET: usize;
}

/// The [`RegistrationOps`] trait serves as generic interface for subsystems (e.g., PCI, Platform,
+3 −0
Original line number Diff line number Diff line
@@ -94,8 +94,11 @@ macro_rules! i2c_device_table {

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

// SAFETY: A call to `unregister` for a given instance of `DriverType` is guaranteed to be valid if
+3 −0
Original line number Diff line number Diff line
@@ -52,8 +52,11 @@

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

// SAFETY: A call to `unregister` for a given instance of `DriverType` is guaranteed to be valid if
+3 −0
Original line number Diff line number Diff line
@@ -28,8 +28,11 @@

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

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