| 1 | |
| 2 |
|
| 3 | |
| 4 | |
| 5 | |
| 6 | |
| 7 |
|
| 8 | use crate::error::{Error, Result};
|
| 9 | use crate::{device, of, str::CStr, try_pin_init, types::Opaque, ThisModule};
|
| 10 | use core::pin::Pin;
|
| 11 | use pin_init::{pin_data, pinned_drop, PinInit};
|
| 12 |
|
| 13 | |
| 14 | |
| 15 | |
| 16 | |
| 17 | |
| 18 | |
| 19 | |
| 20 | |
| 21 | |
| 22 | |
| 23 | |
| 24 | |
| 25 | pub unsafe trait RegistrationOps {
|
| 26 | |
| 27 | |
| 28 | type RegType: Default;
|
| 29 |
|
| 30 | |
| 31 | |
| 32 | |
| 33 | |
| 34 | |
| 35 | |
| 36 | unsafe fn register(
|
| 37 | reg: &Opaque<Self::RegType>,
|
| 38 | name: &'static CStr,
|
| 39 | module: &'static ThisModule,
|
| 40 | ) -> Result;
|
| 41 |
|
| 42 | |
| 43 | |
| 44 | |
| 45 | |
| 46 | |
| 47 | |
| 48 | unsafe fn unregister(reg: &Opaque<Self::RegType>);
|
| 49 | }
|
| 50 |
|
| 51 | |
| 52 | |
| 53 | |
| 54 | |
| 55 | |
| 56 | |
| 57 | #[pin_data(PinnedDrop)]
|
| 58 | pub struct Registration<T: RegistrationOps> {
|
| 59 | #[pin]
|
| 60 | reg: Opaque<T::RegType>,
|
| 61 | }
|
| 62 |
|
| 63 | |
| 64 | |
| 65 | unsafe impl<T: RegistrationOps> Sync for Registration<T> {}
|
| 66 |
|
| 67 | |
| 68 | |
| 69 | unsafe impl<T: RegistrationOps> Send for Registration<T> {}
|
| 70 |
|
| 71 | impl<T: RegistrationOps> Registration<T> {
|
| 72 | |
| 73 | pub fn new(name: &'static CStr, module: &'static ThisModule) -> impl PinInit<Self, Error> {
|
| 74 | try_pin_init!(Self {
|
| 75 | reg <- Opaque::try_ffi_init(|ptr: *mut T::RegType| {
|
| 76 | |
| 77 | unsafe { ptr.write(T::RegType::default()) };
|
| 78 |
|
| 79 | |
| 80 | |
| 81 | let drv = unsafe { &*(ptr as *const Opaque<T::RegType>) };
|
| 82 |
|
| 83 | |
| 84 | unsafe { T::register(drv, name, module) }
|
| 85 | }),
|
| 86 | })
|
| 87 | }
|
| 88 | }
|
| 89 |
|
| 90 | #[pinned_drop]
|
| 91 | impl<T: RegistrationOps> PinnedDrop for Registration<T> {
|
| 92 | fn drop(self: Pin<&mut Self>) {
|
| 93 | |
| 94 | |
| 95 | unsafe { T::unregister(&self.reg) };
|
| 96 | }
|
| 97 | }
|
| 98 |
|
| 99 | |
| 100 | |
| 101 | |
| 102 | |
| 103 | #[macro_export]
|
| 104 | macro_rules! module_driver {
|
| 105 | (<$gen_type:ident>, $driver_ops:ty, { type: $type:ty, $($f:tt)* }) => {
|
| 106 | type Ops<$gen_type> = $driver_ops;
|
| 107 |
|
| 108 | #[$crate::prelude::pin_data]
|
| 109 | struct DriverModule {
|
| 110 | #[pin]
|
| 111 | _driver: $crate::driver::Registration<Ops<$type>>,
|
| 112 | }
|
| 113 |
|
| 114 | impl $crate::InPlaceModule for DriverModule {
|
| 115 | fn init(
|
| 116 | module: &'static $crate::ThisModule
|
| 117 | ) -> impl ::pin_init::PinInit<Self, $crate::error::Error> {
|
| 118 | $crate::try_pin_init!(Self {
|
| 119 | _driver <- $crate::driver::Registration::new(
|
| 120 | <Self as $crate::ModuleMetadata>::NAME,
|
| 121 | module,
|
| 122 | ),
|
| 123 | })
|
| 124 | }
|
| 125 | }
|
| 126 |
|
| 127 | $crate::prelude::module! {
|
| 128 | type: DriverModule,
|
| 129 | $($f)*
|
| 130 | }
|
| 131 | }
|
| 132 | }
|
| 133 |
|
| 134 | |
| 135 | |
| 136 | |
| 137 | |
| 138 | |
| 139 | |
| 140 | pub trait Adapter {
|
| 141 | |
| 142 | type IdInfo: 'static;
|
| 143 |
|
| 144 | |
| 145 | fn of_id_table() -> Option<of::IdTable<Self::IdInfo>>;
|
| 146 |
|
| 147 | |
| 148 | |
| 149 | |
| 150 | #[cfg(CONFIG_OF)]
|
| 151 | fn of_id_info(dev: &device::Device) -> Option<&'static Self::IdInfo> {
|
| 152 | let table = Self::of_id_table()?;
|
| 153 |
|
| 154 | |
| 155 | |
| 156 | |
| 157 | let raw_id = unsafe { bindings::of_match_device(table.as_ptr(), dev.as_raw()) };
|
| 158 |
|
| 159 | if raw_id.is_null() {
|
| 160 | None
|
| 161 | } else {
|
| 162 | |
| 163 | |
| 164 | let id = unsafe { &*raw_id.cast::<of::DeviceId>() };
|
| 165 |
|
| 166 | Some(table.info(<of::DeviceId as crate::device_id::RawDeviceId>::index(id)))
|
| 167 | }
|
| 168 | }
|
| 169 |
|
| 170 | #[cfg(not(CONFIG_OF))]
|
| 171 | #[allow(missing_docs)]
|
| 172 | fn of_id_info(_dev: &device::Device) -> Option<&'static Self::IdInfo> {
|
| 173 | None
|
| 174 | }
|
| 175 |
|
| 176 | |
| 177 | |
| 178 | |
| 179 | |
| 180 | fn id_info(dev: &device::Device) -> Option<&'static Self::IdInfo> {
|
| 181 | let id = Self::of_id_info(dev);
|
| 182 | if id.is_some() {
|
| 183 | return id;
|
| 184 | }
|
| 185 |
|
| 186 | None
|
| 187 | }
|
| 188 | }
|