mirror of
https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
synced 2026-08-28 04:03:23 -04:00
The common practice in C drivers is to store pointers into `driver_data` field of device IDs. The Rust code is however currently storing indices into the fields and then carry a side table that maps the index to pointers. It is much simpler to just have `DeviceId` carry the pointer like C code does. However, just doing so naively would cause a "pointers cannot be cast to integers during const eval" error, as kernel_ulong_t does not have provenance while pointers do, and Rust forbids `expose_provenance` during consteval. Work around this limitation by wrapping raw IDs in `MaybeUninit`. `MaybeUninit` is allowed to host arbitrary bytes with or without provenance, so we can just then use `unsafe` to store a pointer with provenance there. This has the same effect as changing the C-side definition to use `void*` instead of `kernel_ulong_t`, but without actually changing the C side. Signed-off-by: Gary Guo <gary@garyguo.net> Reviewed-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org> Link: https://patch.msgid.link/20260629-id_info-v2-8-56fccbe9c5ef@garyguo.net Signed-off-by: Danilo Krummrich <dakr@kernel.org>
56 lines
1.6 KiB
Rust
56 lines
1.6 KiB
Rust
// SPDX-License-Identifier: GPL-2.0
|
|
|
|
//! Advanced Configuration and Power Interface abstractions.
|
|
|
|
use crate::{
|
|
bindings,
|
|
device_id::{RawDeviceId, RawDeviceIdIndex},
|
|
prelude::*,
|
|
};
|
|
|
|
/// IdTable type for ACPI drivers.
|
|
pub type IdTable<T> = &'static dyn kernel::device_id::IdTable<DeviceId, T>;
|
|
|
|
/// An ACPI device id.
|
|
#[repr(transparent)]
|
|
#[derive(Clone, Copy)]
|
|
pub struct DeviceId(bindings::acpi_device_id);
|
|
|
|
// SAFETY: `DeviceId` is a `#[repr(transparent)]` wrapper of `acpi_device_id` and does not add
|
|
// additional invariants, so it's safe to transmute to `RawType`.
|
|
unsafe impl RawDeviceId for DeviceId {
|
|
type RawType = bindings::acpi_device_id;
|
|
}
|
|
|
|
// SAFETY: `DRIVER_DATA_OFFSET` is the offset to the `driver_data` field.
|
|
unsafe impl RawDeviceIdIndex for DeviceId {
|
|
const DRIVER_DATA_OFFSET: usize = core::mem::offset_of!(bindings::acpi_device_id, driver_data);
|
|
}
|
|
|
|
impl DeviceId {
|
|
const ACPI_ID_LEN: usize = 16;
|
|
|
|
/// Create a new device id from an ACPI 'id' string.
|
|
#[inline(always)]
|
|
pub const fn new(id: &'static CStr) -> Self {
|
|
let src = id.to_bytes_with_nul();
|
|
build_assert!(src.len() <= Self::ACPI_ID_LEN, "ID exceeds 16 bytes");
|
|
let mut acpi: bindings::acpi_device_id = pin_init::zeroed();
|
|
let mut i = 0;
|
|
while i < src.len() {
|
|
acpi.id[i] = src[i];
|
|
i += 1;
|
|
}
|
|
|
|
Self(acpi)
|
|
}
|
|
}
|
|
|
|
/// Create an ACPI `IdTable` with an "alias" for modpost.
|
|
#[macro_export]
|
|
macro_rules! acpi_device_table {
|
|
($($tt:tt)*) => {
|
|
$crate::module_device_table!("acpi", $crate::acpi::DeviceId, $($tt)*);
|
|
};
|
|
}
|