Skip to content

Commit 2ad0f49

Browse files
author
Danilo Krummrich
committed
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>
1 parent c1d4519 commit 2ad0f49

6 files changed

Lines changed: 14 additions & 0 deletions

File tree

rust/kernel/auxiliary.rs

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

2626
// SAFETY:
2727
// - `bindings::auxiliary_driver` is a C type declared as `repr(C)`.
28+
// - `T` is the type of the driver's device private data.
2829
// - `struct auxiliary_driver` embeds a `struct device_driver`.
2930
// - `DEVICE_DRIVER_OFFSET` is the correct byte offset to the embedded `struct device_driver`.
3031
unsafe impl<T: Driver + 'static> driver::DriverLayout for Adapter<T> {
3132
type DriverType = bindings::auxiliary_driver;
33+
type DriverData = T;
3234
const DEVICE_DRIVER_OFFSET: usize = core::mem::offset_of!(Self::DriverType, driver);
3335
}
3436

rust/kernel/driver.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,11 +108,15 @@ use pin_init::{pin_data, pinned_drop, PinInit};
108108
///
109109
/// Implementors must guarantee that:
110110
/// - `DriverType` is `repr(C)`,
111+
/// - `DriverData` is the type of the driver's device private data.
111112
/// - `DriverType` embeds a valid `struct device_driver` at byte offset `DEVICE_DRIVER_OFFSET`.
112113
pub unsafe trait DriverLayout {
113114
/// The specific driver type embedding a `struct device_driver`.
114115
type DriverType: Default;
115116

117+
/// The type of the driver's device private data.
118+
type DriverData;
119+
116120
/// Byte offset of the embedded `struct device_driver` within `DriverType`.
117121
///
118122
/// This must correspond exactly to the location of the embedded `struct device_driver` field.

rust/kernel/i2c.rs

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

9595
// SAFETY:
9696
// - `bindings::i2c_driver` is a C type declared as `repr(C)`.
97+
// - `T` is the type of the driver's device private data.
9798
// - `struct i2c_driver` embeds a `struct device_driver`.
9899
// - `DEVICE_DRIVER_OFFSET` is the correct byte offset to the embedded `struct device_driver`.
99100
unsafe impl<T: Driver + 'static> driver::DriverLayout for Adapter<T> {
100101
type DriverType = bindings::i2c_driver;
102+
type DriverData = T;
101103
const DEVICE_DRIVER_OFFSET: usize = core::mem::offset_of!(Self::DriverType, driver);
102104
}
103105

rust/kernel/pci.rs

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

5353
// SAFETY:
5454
// - `bindings::pci_driver` is a C type declared as `repr(C)`.
55+
// - `T` is the type of the driver's device private data.
5556
// - `struct pci_driver` embeds a `struct device_driver`.
5657
// - `DEVICE_DRIVER_OFFSET` is the correct byte offset to the embedded `struct device_driver`.
5758
unsafe impl<T: Driver + 'static> driver::DriverLayout for Adapter<T> {
5859
type DriverType = bindings::pci_driver;
60+
type DriverData = T;
5961
const DEVICE_DRIVER_OFFSET: usize = core::mem::offset_of!(Self::DriverType, driver);
6062
}
6163

rust/kernel/platform.rs

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

2929
// SAFETY:
3030
// - `bindings::platform_driver` is a C type declared as `repr(C)`.
31+
// - `T` is the type of the driver's device private data.
3132
// - `struct platform_driver` embeds a `struct device_driver`.
3233
// - `DEVICE_DRIVER_OFFSET` is the correct byte offset to the embedded `struct device_driver`.
3334
unsafe impl<T: Driver + 'static> driver::DriverLayout for Adapter<T> {
3435
type DriverType = bindings::platform_driver;
36+
type DriverData = T;
3537
const DEVICE_DRIVER_OFFSET: usize = core::mem::offset_of!(Self::DriverType, driver);
3638
}
3739

rust/kernel/usb.rs

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

3030
// SAFETY:
3131
// - `bindings::usb_driver` is a C type declared as `repr(C)`.
32+
// - `T` is the type of the driver's device private data.
3233
// - `struct usb_driver` embeds a `struct device_driver`.
3334
// - `DEVICE_DRIVER_OFFSET` is the correct byte offset to the embedded `struct device_driver`.
3435
unsafe impl<T: Driver + 'static> driver::DriverLayout for Adapter<T> {
3536
type DriverType = bindings::usb_driver;
37+
type DriverData = T;
3638
const DEVICE_DRIVER_OFFSET: usize = core::mem::offset_of!(Self::DriverType, driver);
3739
}
3840

0 commit comments

Comments
 (0)