| 1 | |
| 2 |
|
| 3 | |
| 4 | |
| 5 | |
| 6 | |
| 7 |
|
| 8 | use core::{
|
| 9 | marker::PhantomData,
|
| 10 | mem::MaybeUninit, |
| 11 | };
|
| 12 |
|
| 13 | |
| 14 | |
| 15 | |
| 16 | |
| 17 | |
| 18 | |
| 19 | |
| 20 | |
| 21 | |
| 22 | |
| 23 | |
| 24 | |
| 25 | |
| 26 | |
| 27 | |
| 28 | |
| 29 | pub unsafe trait RawDeviceId {
|
| 30 | |
| 31 | |
| 32 | |
| 33 | type RawType: Copy;
|
| 34 | }
|
| 35 |
|
| 36 | |
| 37 | |
| 38 | |
| 39 | |
| 40 | |
| 41 | |
| 42 | |
| 43 | |
| 44 | |
| 45 | |
| 46 | |
| 47 | |
| 48 | |
| 49 | pub unsafe trait RawDeviceIdIndex: RawDeviceId {
|
| 50 | |
| 51 | const DRIVER_DATA_OFFSET: usize;
|
| 52 |
|
| 53 | |
| 54 | |
| 55 | |
| 56 | |
| 57 | |
| 58 | unsafe fn info_unchecked<U>(&self) -> &'static U {
|
| 59 | |
| 60 | |
| 61 | |
| 62 | unsafe {
|
| 63 | core::ptr::from_ref(self)
|
| 64 | .byte_add(Self::DRIVER_DATA_OFFSET)
|
| 65 | .cast::<&U>()
|
| 66 | .read()
|
| 67 | }
|
| 68 | }
|
| 69 |
|
| 70 | |
| 71 | |
| 72 | |
| 73 | |
| 74 | |
| 75 | unsafe fn info_unchecked_opt<U>(&self) -> Option<&'static U> {
|
| 76 | |
| 77 | |
| 78 | |
| 79 | |
| 80 | unsafe {
|
| 81 | core::ptr::from_ref(self)
|
| 82 | .byte_add(Self::DRIVER_DATA_OFFSET)
|
| 83 | .cast::<Option<&U>>()
|
| 84 | .read()
|
| 85 | }
|
| 86 | }
|
| 87 | }
|
| 88 |
|
| 89 | |
| 90 | #[repr(C)]
|
| 91 | pub struct IdArray<T: RawDeviceId, U: 'static, const N: usize> {
|
| 92 | |
| 93 | |
| 94 | ids: [MaybeUninit<T::RawType>; N],
|
| 95 | sentinel: MaybeUninit<T::RawType>,
|
| 96 | phantom: PhantomData<&'static U>,
|
| 97 | }
|
| 98 |
|
| 99 | |
| 100 | |
| 101 | unsafe impl<T: RawDeviceId, U: Sync + 'static, const N: usize> Send for IdArray<T, U, N> {}
|
| 102 |
|
| 103 | |
| 104 | |
| 105 | unsafe impl<T: RawDeviceId, U: Sync + 'static, const N: usize> Sync for IdArray<T, U, N> {}
|
| 106 |
|
| 107 | impl<T: RawDeviceId + RawDeviceIdIndex, U: 'static, const N: usize> IdArray<T, U, N> {
|
| 108 | |
| 109 | |
| 110 | |
| 111 | pub const fn new(ids: [(T, &'static U); N]) -> Self {
|
| 112 | let mut raw_ids = [const { MaybeUninit::<T::RawType>::uninit() }; N];
|
| 113 |
|
| 114 | let mut i = 0usize;
|
| 115 | while i < N {
|
| 116 | |
| 117 | |
| 118 | raw_ids[i] = unsafe { core::mem::transmute_copy(&ids[i].0) };
|
| 119 | |
| 120 | |
| 121 | unsafe {
|
| 122 | raw_ids[i]
|
| 123 | .as_mut_ptr()
|
| 124 | .byte_add(T::DRIVER_DATA_OFFSET)
|
| 125 | .cast::<&U>()
|
| 126 | .write(ids[i].1);
|
| 127 | }
|
| 128 |
|
| 129 | i += 1;
|
| 130 | }
|
| 131 |
|
| 132 | core::mem::forget(ids);
|
| 133 |
|
| 134 | Self {
|
| 135 | ids: raw_ids,
|
| 136 | sentinel: MaybeUninit::zeroed(),
|
| 137 | phantom: PhantomData,
|
| 138 | }
|
| 139 | }
|
| 140 | }
|
| 141 |
|
| 142 | impl<T: RawDeviceId, const N: usize> IdArray<T, (), N> {
|
| 143 | |
| 144 | |
| 145 | |
| 146 | |
| 147 | pub const fn new_without_index(ids: [T; N]) -> Self {
|
| 148 | |
| 149 | let raw_ids: [MaybeUninit<T::RawType>; N] = unsafe { core::mem::transmute_copy(&ids) };
|
| 150 | core::mem::forget(ids);
|
| 151 |
|
| 152 | Self {
|
| 153 | ids: raw_ids,
|
| 154 | sentinel: MaybeUninit::zeroed(),
|
| 155 | phantom: PhantomData,
|
| 156 | }
|
| 157 | }
|
| 158 | }
|
| 159 |
|
| 160 | |
| 161 | |
| 162 | |
| 163 | |
| 164 | |
| 165 | |
| 166 | pub trait IdTable<T: RawDeviceId, U> {
|
| 167 | |
| 168 | fn as_ptr(&self) -> *const T::RawType;
|
| 169 | }
|
| 170 |
|
| 171 | impl<T: RawDeviceId, U, const N: usize> IdTable<T, U> for IdArray<T, U, N> {
|
| 172 | fn as_ptr(&self) -> *const T::RawType {
|
| 173 | |
| 174 | |
| 175 | core::ptr::from_ref(self).cast()
|
| 176 | }
|
| 177 | }
|
| 178 |
|
| 179 | |
| 180 | #[macro_export]
|
| 181 | macro_rules! module_device_table {
|
| 182 | (
|
| 183 | $table_type: literal, $device_id_ty: ty,
|
| 184 | $table_name: ident, $id_info_type: ty,
|
| 185 | [$(($id: expr, $info:expr $(,)?)),* $(,)?]
|
| 186 | ) => {
|
| 187 | #[export_name =
|
| 188 | concat!("__mod_device_table__", ::core::line!(),
|
| 189 | "__kmod_", module_path!(),
|
| 190 | "__", $table_type,
|
| 191 | "__", stringify!($table_name))
|
| 192 | ]
|
| 193 | static $table_name: $crate::device_id::IdArray<
|
| 194 | $device_id_ty,
|
| 195 | $id_info_type,
|
| 196 | { <[$device_id_ty]>::len(&[$($id,)*]) },
|
| 197 | > = $crate::device_id::IdArray::new([$(($id, &$info),)*]);
|
| 198 | };
|
| 199 |
|
| 200 | |
| 201 | (
|
| 202 | $table_type: literal, $device_id_ty: ty,
|
| 203 | $table_name: ident, @none,
|
| 204 | [$($id: expr),* $(,)?]
|
| 205 | ) => {
|
| 206 | #[export_name =
|
| 207 | concat!("__mod_device_table__", ::core::line!(),
|
| 208 | "__kmod_", module_path!(),
|
| 209 | "__", $table_type,
|
| 210 | "__", stringify!($table_name))
|
| 211 | ]
|
| 212 | static $table_name: $crate::device_id::IdArray<
|
| 213 | $device_id_ty,
|
| 214 | (),
|
| 215 | { <[$device_id_ty]>::len(&[$($id,)*]) },
|
| 216 | > = $crate::device_id::IdArray::new_without_index([$($id),*]);
|
| 217 | };
|
| 218 | }
|