1 // Copyright 2024, Linaro Limited 2 // Author(s): Manos Pitsidianakis <manos.pitsidianakis@linaro.org> 3 // SPDX-License-Identifier: GPL-2.0-or-later 4 5 use std::{ffi::CStr, os::raw::c_void}; 6 7 use crate::{ 8 bindings::{self, DeviceClass, DeviceState, Error, ObjectClass, Property, VMStateDescription}, 9 zeroable::Zeroable, 10 }; 11 12 /// Trait providing the contents of [`DeviceClass`]. 13 pub trait DeviceImpl { 14 /// _Realization_ is the second stage of device creation. It contains 15 /// all operations that depend on device properties and can fail (note: 16 /// this is not yet supported for Rust devices). 17 /// 18 /// If not `None`, the parent class's `realize` method is overridden 19 /// with the function pointed to by `REALIZE`. 20 const REALIZE: Option<unsafe extern "C" fn(*mut DeviceState, *mut *mut Error)> = None; 21 22 /// If not `None`, the parent class's `reset` method is overridden 23 /// with the function pointed to by `RESET`. 24 /// 25 /// Rust does not yet support the three-phase reset protocol; this is 26 /// usually okay for leaf classes. 27 const RESET: Option<unsafe extern "C" fn(dev: *mut DeviceState)> = None; 28 29 /// An array providing the properties that the user can set on the 30 /// device. Not a `const` because referencing statics in constants 31 /// is unstable until Rust 1.83.0. 32 fn properties() -> &'static [Property] { 33 &[Zeroable::ZERO; 1] 34 } 35 36 /// A `VMStateDescription` providing the migration format for the device 37 /// Not a `const` because referencing statics in constants is unstable 38 /// until Rust 1.83.0. 39 fn vmsd() -> Option<&'static VMStateDescription> { 40 None 41 } 42 } 43 44 /// # Safety 45 /// 46 /// We expect the FFI user of this function to pass a valid pointer that 47 /// can be downcasted to type `DeviceClass`, because `T` implements 48 /// `DeviceImpl`. 49 pub unsafe extern "C" fn rust_device_class_init<T: DeviceImpl>( 50 klass: *mut ObjectClass, 51 _: *mut c_void, 52 ) { 53 let mut dc = ::core::ptr::NonNull::new(klass.cast::<DeviceClass>()).unwrap(); 54 unsafe { 55 let dc = dc.as_mut(); 56 if let Some(realize_fn) = <T as DeviceImpl>::REALIZE { 57 dc.realize = Some(realize_fn); 58 } 59 if let Some(reset_fn) = <T as DeviceImpl>::RESET { 60 bindings::device_class_set_legacy_reset(dc, Some(reset_fn)); 61 } 62 if let Some(vmsd) = <T as DeviceImpl>::vmsd() { 63 dc.vmsd = vmsd; 64 } 65 bindings::device_class_set_props(dc, <T as DeviceImpl>::properties().as_ptr()); 66 } 67 } 68 69 #[macro_export] 70 macro_rules! impl_device_class { 71 ($type:ty) => { 72 impl $crate::definitions::ClassInitImpl for $type { 73 const CLASS_INIT: Option< 74 unsafe extern "C" fn(klass: *mut ObjectClass, data: *mut ::std::os::raw::c_void), 75 > = Some($crate::device_class::rust_device_class_init::<$type>); 76 const CLASS_BASE_INIT: Option< 77 unsafe extern "C" fn(klass: *mut ObjectClass, data: *mut ::std::os::raw::c_void), 78 > = None; 79 } 80 }; 81 } 82 83 #[macro_export] 84 macro_rules! define_property { 85 ($name:expr, $state:ty, $field:ident, $prop:expr, $type:ty, default = $defval:expr$(,)*) => { 86 $crate::bindings::Property { 87 // use associated function syntax for type checking 88 name: ::std::ffi::CStr::as_ptr($name), 89 info: $prop, 90 offset: $crate::offset_of!($state, $field) as isize, 91 set_default: true, 92 defval: $crate::bindings::Property__bindgen_ty_1 { u: $defval as u64 }, 93 ..$crate::zeroable::Zeroable::ZERO 94 } 95 }; 96 ($name:expr, $state:ty, $field:ident, $prop:expr, $type:ty$(,)*) => { 97 $crate::bindings::Property { 98 // use associated function syntax for type checking 99 name: ::std::ffi::CStr::as_ptr($name), 100 info: $prop, 101 offset: $crate::offset_of!($state, $field) as isize, 102 set_default: false, 103 ..$crate::zeroable::Zeroable::ZERO 104 } 105 }; 106 } 107 108 #[macro_export] 109 macro_rules! declare_properties { 110 ($ident:ident, $($prop:expr),*$(,)*) => { 111 pub static $ident: [$crate::bindings::Property; { 112 let mut len = 1; 113 $({ 114 _ = stringify!($prop); 115 len += 1; 116 })* 117 len 118 }] = [ 119 $($prop),*, 120 $crate::zeroable::Zeroable::ZERO, 121 ]; 122 }; 123 } 124 125 // workaround until we can use --generate-cstr in bindgen. 126 pub const TYPE_DEVICE: &CStr = 127 unsafe { CStr::from_bytes_with_nul_unchecked(bindings::TYPE_DEVICE) }; 128 pub const TYPE_SYS_BUS_DEVICE: &CStr = 129 unsafe { CStr::from_bytes_with_nul_unchecked(bindings::TYPE_SYS_BUS_DEVICE) }; 130