xref: /linux/rust/kernel/acpi.rs (revision 59e6295fac26b8e85c1ea859cdd89fa1e47519d7)
1 // SPDX-License-Identifier: GPL-2.0
2 
3 //! Advanced Configuration and Power Interface abstractions.
4 
5 use crate::{
6     bindings,
7     device_id::{RawDeviceId, RawDeviceIdIndex},
8     prelude::*,
9 };
10 
11 /// IdTable type for ACPI drivers.
12 pub type IdTable<T> = &'static dyn kernel::device_id::IdTable<DeviceId, T>;
13 
14 /// An ACPI device id.
15 #[repr(transparent)]
16 #[derive(Clone, Copy)]
17 pub struct DeviceId(bindings::acpi_device_id);
18 
19 // SAFETY: `DeviceId` is a `#[repr(transparent)]` wrapper of `acpi_device_id` and does not add
20 // additional invariants, so it's safe to transmute to `RawType`.
21 unsafe impl RawDeviceId for DeviceId {
22     type RawType = bindings::acpi_device_id;
23 }
24 
25 // SAFETY: `DRIVER_DATA_OFFSET` is the offset to the `driver_data` field.
26 unsafe impl RawDeviceIdIndex for DeviceId {
27     const DRIVER_DATA_OFFSET: usize = core::mem::offset_of!(bindings::acpi_device_id, driver_data);
28 }
29 
30 impl DeviceId {
31     const ACPI_ID_LEN: usize = 16;
32 
33     /// Create a new device id from an ACPI 'id' string.
34     #[inline(always)]
35     pub const fn new(id: &'static CStr) -> Self {
36         let src = id.to_bytes_with_nul();
37         build_assert!(src.len() <= Self::ACPI_ID_LEN, "ID exceeds 16 bytes");
38         let mut acpi: bindings::acpi_device_id = pin_init::zeroed();
39         let mut i = 0;
40         while i < src.len() {
41             acpi.id[i] = src[i];
42             i += 1;
43         }
44 
45         Self(acpi)
46     }
47 }
48 
49 /// Create an ACPI `IdTable` with an "alias" for modpost.
50 #[macro_export]
51 macro_rules! acpi_device_table {
52     ($($tt:tt)*) => {
53         $crate::module_device_table!("acpi", $crate::acpi::DeviceId, $($tt)*);
54     };
55 }
56