| 1 | |
| 2 |
|
| 3 | |
| 4 |
|
| 5 | use crate::{
|
| 6 | bindings,
|
| 7 | device_id::{RawDeviceId, RawDeviceIdIndex},
|
| 8 | prelude::*,
|
| 9 | };
|
| 10 |
|
| 11 | |
| 12 | pub type IdTable<T> = &'static dyn kernel::device_id::IdTable<DeviceId, T>;
|
| 13 |
|
| 14 | |
| 15 | #[repr(transparent)]
|
| 16 | #[derive(Clone, Copy)]
|
| 17 | pub struct DeviceId(bindings::of_device_id);
|
| 18 |
|
| 19 | |
| 20 | |
| 21 | unsafe impl RawDeviceId for DeviceId {
|
| 22 | type RawType = bindings::of_device_id;
|
| 23 | }
|
| 24 |
|
| 25 | |
| 26 | unsafe impl RawDeviceIdIndex for DeviceId {
|
| 27 | const DRIVER_DATA_OFFSET: usize = core::mem::offset_of!(bindings::of_device_id, data);
|
| 28 |
|
| 29 | fn index(&self) -> usize {
|
| 30 | self.0.data as usize
|
| 31 | }
|
| 32 | }
|
| 33 |
|
| 34 | impl DeviceId {
|
| 35 | |
| 36 | pub const fn new(compatible: &'static CStr) -> Self {
|
| 37 | let src = compatible.to_bytes_with_nul();
|
| 38 | |
| 39 | |
| 40 | let mut of: bindings::of_device_id = unsafe { core::mem::zeroed() };
|
| 41 |
|
| 42 | |
| 43 | let mut i = 0;
|
| 44 | while i < src.len() {
|
| 45 | of.compatible[i] = src[i];
|
| 46 | i += 1;
|
| 47 | }
|
| 48 |
|
| 49 | Self(of)
|
| 50 | }
|
| 51 | }
|
| 52 |
|
| 53 | |
| 54 | #[macro_export]
|
| 55 | macro_rules! of_device_table {
|
| 56 | ($table_name:ident, $module_table_name:ident, $id_info_type: ty, $table_data: expr) => {
|
| 57 | const $table_name: $crate::device_id::IdArray<
|
| 58 | $crate::of::DeviceId,
|
| 59 | $id_info_type,
|
| 60 | { $table_data.len() },
|
| 61 | > = $crate::device_id::IdArray::new($table_data);
|
| 62 |
|
| 63 | $crate::module_device_table!("of", $module_table_name, $table_name);
|
| 64 | };
|
| 65 | }
|