mirror of
https://github.com/torvalds/linux.git
synced 2026-01-25 07:47:50 +00:00
rust: driver: add DriverData type to the DriverLayout trait
Add an associated type DriverData to the DriverLayout trait indicating the type of the driver's device private data. Acked-by: Alice Ryhl <aliceryhl@google.com> Acked-by: Igor Korotin <igor.korotin.linux@gmail.com> Link: https://patch.msgid.link/20260107103511.570525-6-dakr@kernel.org Signed-off-by: Danilo Krummrich <dakr@kernel.org>
This commit is contained in:
@@ -25,10 +25,12 @@ pub struct Adapter<T: Driver>(T);
|
||||
|
||||
// SAFETY:
|
||||
// - `bindings::auxiliary_driver` is a C type declared as `repr(C)`.
|
||||
// - `T` is the type of the driver's device private data.
|
||||
// - `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;
|
||||
type DriverData = T;
|
||||
const DEVICE_DRIVER_OFFSET: usize = core::mem::offset_of!(Self::DriverType, driver);
|
||||
}
|
||||
|
||||
|
||||
@@ -108,11 +108,15 @@ use pin_init::{pin_data, pinned_drop, PinInit};
|
||||
///
|
||||
/// Implementors must guarantee that:
|
||||
/// - `DriverType` is `repr(C)`,
|
||||
/// - `DriverData` is the type of the driver's device private data.
|
||||
/// - `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;
|
||||
|
||||
/// The type of the driver's device private data.
|
||||
type DriverData;
|
||||
|
||||
/// Byte offset of the embedded `struct device_driver` within `DriverType`.
|
||||
///
|
||||
/// This must correspond exactly to the location of the embedded `struct device_driver` field.
|
||||
|
||||
@@ -94,10 +94,12 @@ pub struct Adapter<T: Driver>(T);
|
||||
|
||||
// SAFETY:
|
||||
// - `bindings::i2c_driver` is a C type declared as `repr(C)`.
|
||||
// - `T` is the type of the driver's device private data.
|
||||
// - `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;
|
||||
type DriverData = T;
|
||||
const DEVICE_DRIVER_OFFSET: usize = core::mem::offset_of!(Self::DriverType, driver);
|
||||
}
|
||||
|
||||
|
||||
@@ -52,10 +52,12 @@ pub struct Adapter<T: Driver>(T);
|
||||
|
||||
// SAFETY:
|
||||
// - `bindings::pci_driver` is a C type declared as `repr(C)`.
|
||||
// - `T` is the type of the driver's device private data.
|
||||
// - `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;
|
||||
type DriverData = T;
|
||||
const DEVICE_DRIVER_OFFSET: usize = core::mem::offset_of!(Self::DriverType, driver);
|
||||
}
|
||||
|
||||
|
||||
@@ -28,10 +28,12 @@ pub struct Adapter<T: Driver>(T);
|
||||
|
||||
// SAFETY:
|
||||
// - `bindings::platform_driver` is a C type declared as `repr(C)`.
|
||||
// - `T` is the type of the driver's device private data.
|
||||
// - `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;
|
||||
type DriverData = T;
|
||||
const DEVICE_DRIVER_OFFSET: usize = core::mem::offset_of!(Self::DriverType, driver);
|
||||
}
|
||||
|
||||
|
||||
@@ -29,10 +29,12 @@ pub struct Adapter<T: Driver>(T);
|
||||
|
||||
// SAFETY:
|
||||
// - `bindings::usb_driver` is a C type declared as `repr(C)`.
|
||||
// - `T` is the type of the driver's device private data.
|
||||
// - `struct usb_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::usb_driver;
|
||||
type DriverData = T;
|
||||
const DEVICE_DRIVER_OFFSET: usize = core::mem::offset_of!(Self::DriverType, driver);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user