| 1 | |
| 2 |
|
| 3 | |
| 4 |
|
| 5 | |
| 6 | |
| 7 | |
| 8 |
|
| 9 | use crate::{
|
| 10 | bindings,
|
| 11 | error,
|
| 12 | prelude::*,
|
| 13 | str::CString,
|
| 14 | types::Opaque, |
| 15 | };
|
| 16 | use core::ptr::NonNull;
|
| 17 |
|
| 18 | |
| 19 | |
| 20 | |
| 21 | |
| 22 | |
| 23 | |
| 24 | |
| 25 | |
| 26 | |
| 27 | pub struct Attributes {
|
| 28 | |
| 29 | |
| 30 | |
| 31 | |
| 32 | |
| 33 | pub machine: Option<CString>,
|
| 34 | |
| 35 | |
| 36 | |
| 37 | |
| 38 | |
| 39 | pub family: Option<CString>,
|
| 40 | |
| 41 | pub revision: Option<CString>,
|
| 42 | |
| 43 | |
| 44 | |
| 45 | pub serial_number: Option<CString>,
|
| 46 | |
| 47 | |
| 48 | |
| 49 | |
| 50 | |
| 51 | |
| 52 | pub soc_id: Option<CString>,
|
| 53 | }
|
| 54 |
|
| 55 | struct BuiltAttributes {
|
| 56 | |
| 57 | |
| 58 | _backing: Attributes,
|
| 59 | |
| 60 | inner: Opaque<bindings::soc_device_attribute>,
|
| 61 | }
|
| 62 |
|
| 63 | fn cstring_to_c(mcs: &Option<CString>) -> *const kernel::ffi::c_char {
|
| 64 | mcs.as_ref()
|
| 65 | .map(|cs| cs.as_char_ptr())
|
| 66 | .unwrap_or(core::ptr::null())
|
| 67 | }
|
| 68 |
|
| 69 | impl BuiltAttributes {
|
| 70 | fn as_mut_ptr(&self) -> *mut bindings::soc_device_attribute {
|
| 71 | self.inner.get()
|
| 72 | }
|
| 73 | }
|
| 74 |
|
| 75 | impl Attributes {
|
| 76 | fn build(self) -> BuiltAttributes {
|
| 77 | BuiltAttributes {
|
| 78 | inner: Opaque::new(bindings::soc_device_attribute {
|
| 79 | machine: cstring_to_c(&self.machine),
|
| 80 | family: cstring_to_c(&self.family),
|
| 81 | revision: cstring_to_c(&self.revision),
|
| 82 | serial_number: cstring_to_c(&self.serial_number),
|
| 83 | soc_id: cstring_to_c(&self.soc_id),
|
| 84 | data: core::ptr::null(),
|
| 85 | custom_attr_group: core::ptr::null(),
|
| 86 | }),
|
| 87 | _backing: self,
|
| 88 | }
|
| 89 | }
|
| 90 | }
|
| 91 |
|
| 92 | #[pin_data(PinnedDrop)]
|
| 93 | |
| 94 | |
| 95 | pub struct Registration {
|
| 96 | #[pin]
|
| 97 | attr: BuiltAttributes,
|
| 98 | soc_dev: NonNull<bindings::soc_device>,
|
| 99 | }
|
| 100 |
|
| 101 | |
| 102 | unsafe impl Sync for Registration {}
|
| 103 |
|
| 104 | |
| 105 | unsafe impl Send for Registration {}
|
| 106 |
|
| 107 | #[pinned_drop]
|
| 108 | impl PinnedDrop for Registration {
|
| 109 | fn drop(self: Pin<&mut Self>) {
|
| 110 | |
| 111 | unsafe { bindings::soc_device_unregister(self.soc_dev.as_ptr()) }
|
| 112 | }
|
| 113 | }
|
| 114 |
|
| 115 | impl Registration {
|
| 116 | |
| 117 | pub fn new(attr: Attributes) -> impl PinInit<Self, Error> {
|
| 118 | try_pin_init!(Self {
|
| 119 | attr: attr.build(),
|
| 120 | soc_dev: {
|
| 121 | |
| 122 | |
| 123 | |
| 124 | |
| 125 | |
| 126 | |
| 127 | let raw_soc = error::from_err_ptr(unsafe {
|
| 128 | bindings::soc_device_register(attr.as_mut_ptr())
|
| 129 | })?;
|
| 130 |
|
| 131 | NonNull::new(raw_soc).ok_or(EINVAL)?
|
| 132 | },
|
| 133 | }? Error)
|
| 134 | }
|
| 135 | }
|