|
4 | 4 | //!
|
5 | 5 | //! C header: [`include/linux/of_*.h`](../../../../include/linux/of_*.h)
|
6 | 6 |
|
7 |
| -use alloc::boxed::Box; |
| 7 | +use crate::{bindings, c_types, str::CStr}; |
8 | 8 |
|
9 |
| -use crate::{ |
10 |
| - bindings, c_types, |
11 |
| - error::{Error, Result}, |
12 |
| - str::CStr, |
13 |
| - types::PointerWrapper, |
14 |
| -}; |
15 |
| - |
16 |
| -use core::mem::transmute; |
17 |
| - |
18 |
| -type InnerTable = Box<[bindings::of_device_id; 2]>; |
| 9 | +use core::ptr; |
19 | 10 |
|
20 | 11 | /// Wraps a kernel Open Firmware / devicetree match table.
|
21 | 12 | ///
|
22 |
| -/// Rust drivers may create this structure to match against devices |
| 13 | +/// Rust drivers may use this trait to match against devices |
23 | 14 | /// described in the devicetree.
|
24 |
| -/// |
25 |
| -/// The ['PointerWrapper'] trait provides conversion to/from a raw pointer, |
26 |
| -/// suitable to be assigned to a `bindings::device_driver::of_match_table`. |
| 15 | +pub trait OfMatchTable { |
| 16 | + /// Return the table as a static lifetime, sentinel-terminated C array. |
| 17 | + /// |
| 18 | + /// This is suitable to be assigned to the kernel's `of_match_table` field. |
| 19 | + fn as_ptr(&'static self) -> *const bindings::of_device_id; |
| 20 | +} |
| 21 | + |
| 22 | +/// An Open Firmware Match Table that can be constructed at build time. |
27 | 23 | ///
|
28 | 24 | /// # Invariants
|
29 | 25 | ///
|
30 |
| -/// The final array element is always filled with zeros (the default). |
31 |
| -pub struct OfMatchTable(InnerTable); |
| 26 | +/// `sentinel` always contains zeroes. |
| 27 | +#[repr(C)] |
| 28 | +pub struct ConstOfMatchTable<const N: usize> { |
| 29 | + table: [bindings::of_device_id; N], |
| 30 | + sentinel: bindings::of_device_id, |
| 31 | +} |
32 | 32 |
|
33 |
| -impl OfMatchTable { |
34 |
| - /// Creates a [`OfMatchTable`] from a single `compatible` string. |
35 |
| - pub fn new(compatible: &'static CStr) -> Result<Self> { |
36 |
| - let tbl = Box::try_new([ |
37 |
| - Self::new_of_device_id(compatible)?, |
38 |
| - bindings::of_device_id::default(), |
39 |
| - ])?; |
40 |
| - // INVARIANTS: we allocated an array with `default()` as its final |
41 |
| - // element, therefore that final element will be filled with zeros, |
42 |
| - // and the invariant above will hold. |
43 |
| - Ok(Self(tbl)) |
| 33 | +impl<const N: usize> ConstOfMatchTable<N> { |
| 34 | + /// Create a new Open Firmware Match Table from a list of compatible strings. |
| 35 | + pub const fn new_const(compatibles: [&'static CStr; N]) -> Self { |
| 36 | + let mut table = [Self::zeroed_of_device_id(); N]; |
| 37 | + let mut i = 0; |
| 38 | + loop { |
| 39 | + if i == N { |
| 40 | + break; |
| 41 | + } |
| 42 | + table[i] = Self::new_of_device_id(compatibles[i]); |
| 43 | + i += 1; |
| 44 | + } |
| 45 | + Self { |
| 46 | + table, |
| 47 | + // INVARIANT: we zero the sentinel here, and never change it |
| 48 | + // anwhere. Therefore it always contains zeroes. |
| 49 | + sentinel: Self::zeroed_of_device_id(), |
| 50 | + } |
44 | 51 | }
|
45 | 52 |
|
46 |
| - fn new_of_device_id(compatible: &'static CStr) -> Result<bindings::of_device_id> { |
47 |
| - let mut buf = [0_u8; 128]; |
48 |
| - if compatible.len() > buf.len() { |
49 |
| - return Err(Error::EINVAL); |
| 53 | + const fn zeroed_of_device_id() -> bindings::of_device_id { |
| 54 | + bindings::of_device_id { |
| 55 | + name: [0; 32usize], |
| 56 | + type_: [0; 32usize], |
| 57 | + compatible: [0; 128usize], |
| 58 | + data: ptr::null(), |
50 | 59 | }
|
51 |
| - buf.get_mut(..compatible.len()) |
52 |
| - .ok_or(Error::EINVAL)? |
53 |
| - .copy_from_slice(compatible.as_bytes()); |
54 |
| - Ok(bindings::of_device_id { |
55 |
| - // SAFETY: re-interpretation from [u8] to [c_types::c_char] of same length is always safe. |
56 |
| - compatible: unsafe { transmute::<[u8; 128], [c_types::c_char; 128]>(buf) }, |
57 |
| - ..Default::default() |
58 |
| - }) |
59 | 60 | }
|
60 |
| -} |
61 | 61 |
|
62 |
| -impl PointerWrapper for OfMatchTable { |
63 |
| - fn into_pointer(self) -> *const c_types::c_void { |
64 |
| - // Per the invariant above, the generated pointer points to an |
65 |
| - // array of `bindings::of_device_id`, where the final element is |
66 |
| - // filled with zeros (the sentinel). Therefore, it's suitable to |
67 |
| - // be assigned to `bindings::device_driver::of_match_table`. |
68 |
| - self.0.into_pointer() |
| 62 | + const fn new_of_device_id(compatible: &'static CStr) -> bindings::of_device_id { |
| 63 | + let mut id = Self::zeroed_of_device_id(); |
| 64 | + let compatible = compatible.as_bytes_with_nul(); |
| 65 | + let mut i = 0; |
| 66 | + loop { |
| 67 | + if i == compatible.len() { |
| 68 | + break; |
| 69 | + } |
| 70 | + // if `compatible` does not fit in `id.compatible`, an |
| 71 | + // "index out of bounds" build time exception will be triggered. |
| 72 | + id.compatible[i] = compatible[i] as c_types::c_char; |
| 73 | + i += 1; |
| 74 | + } |
| 75 | + id |
69 | 76 | }
|
| 77 | +} |
70 | 78 |
|
71 |
| - unsafe fn from_pointer(p: *const c_types::c_void) -> Self { |
72 |
| - // SAFETY: The passed pointer comes from a previous call to [`InnerTable::into_pointer()`]. |
73 |
| - Self(unsafe { InnerTable::from_pointer(p) }) |
| 79 | +impl<const N: usize> OfMatchTable for ConstOfMatchTable<N> { |
| 80 | + fn as_ptr(&'static self) -> *const bindings::of_device_id { |
| 81 | + // The array is sentinel-terminated, by the invariant above. |
| 82 | + // The returned pointer is created by dereferencing |
| 83 | + // a structure with static lifetime, therefore the pointer itself |
| 84 | + // has static lifetime. |
| 85 | + &self.table[0] |
74 | 86 | }
|
75 | 87 | }
|
0 commit comments