Skip to content

Commit 7a718a1

Browse files
Danilo Krummrichgregkh
authored andcommitted
rust: driver: implement Adapter
In order to not duplicate code in bus specific implementations (e.g. platform), implement a generic `driver::Adapter` to represent the connection of matched drivers and devices. Bus specific `Adapter` implementations can simply implement this trait to inherit generic functionality, such as matching OF or ACPI device IDs and ID table entries. Suggested-by: Rob Herring (Arm) <[email protected]> Signed-off-by: Danilo Krummrich <[email protected]> Tested-by: Dirk Behme <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Greg Kroah-Hartman <[email protected]>
1 parent bbe3b4d commit 7a718a1

File tree

2 files changed

+58
-1
lines changed

2 files changed

+58
-1
lines changed

rust/bindings/bindings_helper.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include <linux/jump_label.h>
2121
#include <linux/mdio.h>
2222
#include <linux/miscdevice.h>
23+
#include <linux/of_device.h>
2324
#include <linux/pci.h>
2425
#include <linux/phy.h>
2526
#include <linux/pid_namespace.h>

rust/kernel/driver.rs

Lines changed: 57 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
//! register using the [`Registration`] class.
77
88
use crate::error::{Error, Result};
9-
use crate::{init::PinInit, str::CStr, try_pin_init, types::Opaque, ThisModule};
9+
use crate::{device, init::PinInit, of, str::CStr, try_pin_init, types::Opaque, ThisModule};
1010
use core::pin::Pin;
1111
use macros::{pin_data, pinned_drop};
1212

@@ -115,3 +115,59 @@ macro_rules! module_driver {
115115
}
116116
}
117117
}
118+
119+
/// The bus independent adapter to match a drivers and a devices.
120+
///
121+
/// This trait should be implemented by the bus specific adapter, which represents the connection
122+
/// of a device and a driver.
123+
///
124+
/// It provides bus independent functions for device / driver interactions.
125+
pub trait Adapter {
126+
/// The type holding driver private data about each device id supported by the driver.
127+
type IdInfo: 'static;
128+
129+
/// The [`of::IdTable`] of the corresponding driver.
130+
fn of_id_table() -> Option<of::IdTable<Self::IdInfo>>;
131+
132+
/// Returns the driver's private data from the matching entry in the [`of::IdTable`], if any.
133+
///
134+
/// If this returns `None`, it means there is no match with an entry in the [`of::IdTable`].
135+
#[cfg(CONFIG_OF)]
136+
fn of_id_info(dev: &device::Device) -> Option<&'static Self::IdInfo> {
137+
let table = Self::of_id_table()?;
138+
139+
// SAFETY:
140+
// - `table` has static lifetime, hence it's valid for read,
141+
// - `dev` is guaranteed to be valid while it's alive, and so is `pdev.as_ref().as_raw()`.
142+
let raw_id = unsafe { bindings::of_match_device(table.as_ptr(), dev.as_raw()) };
143+
144+
if raw_id.is_null() {
145+
None
146+
} else {
147+
// SAFETY: `DeviceId` is a `#[repr(transparent)` wrapper of `struct of_device_id` and
148+
// does not add additional invariants, so it's safe to transmute.
149+
let id = unsafe { &*raw_id.cast::<of::DeviceId>() };
150+
151+
Some(table.info(<of::DeviceId as crate::device_id::RawDeviceId>::index(id)))
152+
}
153+
}
154+
155+
#[cfg(not(CONFIG_OF))]
156+
#[allow(missing_docs)]
157+
fn of_id_info(_dev: &device::Device) -> Option<&'static Self::IdInfo> {
158+
None
159+
}
160+
161+
/// Returns the driver's private data from the matching entry of any of the ID tables, if any.
162+
///
163+
/// If this returns `None`, it means that there is no match in any of the ID tables directly
164+
/// associated with a [`device::Device`].
165+
fn id_info(dev: &device::Device) -> Option<&'static Self::IdInfo> {
166+
let id = Self::of_id_info(dev);
167+
if id.is_some() {
168+
return id;
169+
}
170+
171+
None
172+
}
173+
}

0 commit comments

Comments
 (0)