| 1 | |
| 2 |
|
| 3 | |
| 4 | |
| 5 | |
| 6 |
|
| 7 | use core::{mem::MaybeUninit, ptr};
|
| 8 |
|
| 9 | use super::private::Sealed;
|
| 10 | use crate::{
|
| 11 | alloc::KVec,
|
| 12 | bindings,
|
| 13 | error::{to_result, Result},
|
| 14 | fmt,
|
| 15 | prelude::*,
|
| 16 | str::{CStr, CString},
|
| 17 | sync::aref::ARef,
|
| 18 | types::Opaque,
|
| 19 | };
|
| 20 |
|
| 21 | |
| 22 | |
| 23 | |
| 24 | |
| 25 | |
| 26 | |
| 27 | |
| 28 | |
| 29 | |
| 30 | |
| 31 | |
| 32 | |
| 33 | |
| 34 | |
| 35 | |
| 36 | #[repr(transparent)]
|
| 37 | pub struct FwNode(Opaque<bindings::fwnode_handle>);
|
| 38 |
|
| 39 | impl FwNode {
|
| 40 | |
| 41 | |
| 42 | |
| 43 | |
| 44 | |
| 45 | |
| 46 | |
| 47 | unsafe fn from_raw(raw: *mut bindings::fwnode_handle) -> ARef<Self> {
|
| 48 | |
| 49 | |
| 50 | |
| 51 | |
| 52 | |
| 53 | |
| 54 | |
| 55 | |
| 56 | |
| 57 | |
| 58 | unsafe { ARef::from_raw(ptr::NonNull::new_unchecked(raw.cast())) }
|
| 59 | }
|
| 60 |
|
| 61 | |
| 62 | pub(crate) fn as_raw(&self) -> *mut bindings::fwnode_handle {
|
| 63 | self.0.get()
|
| 64 | }
|
| 65 |
|
| 66 | |
| 67 | pub fn is_of_node(&self) -> bool {
|
| 68 | |
| 69 | |
| 70 | unsafe { bindings::is_of_node(self.as_raw()) }
|
| 71 | }
|
| 72 |
|
| 73 | |
| 74 | |
| 75 | |
| 76 | |
| 77 | |
| 78 | pub fn display_name(&self) -> impl fmt::Display + '_ {
|
| 79 | struct FwNodeDisplayName<'a>(&'a FwNode);
|
| 80 |
|
| 81 | impl fmt::Display for FwNodeDisplayName<'_> {
|
| 82 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
| 83 | |
| 84 | let name = unsafe { bindings::fwnode_get_name(self.0.as_raw()) };
|
| 85 | if name.is_null() {
|
| 86 | return Ok(());
|
| 87 | }
|
| 88 | |
| 89 | |
| 90 | |
| 91 | let name = unsafe { CStr::from_char_ptr(name) };
|
| 92 | fmt::Display::fmt(name, f)
|
| 93 | }
|
| 94 | }
|
| 95 |
|
| 96 | FwNodeDisplayName(self)
|
| 97 | }
|
| 98 |
|
| 99 | |
| 100 | pub fn property_present(&self, name: &CStr) -> bool {
|
| 101 | |
| 102 | unsafe { bindings::fwnode_property_present(self.as_raw().cast_const(), name.as_char_ptr()) }
|
| 103 | }
|
| 104 |
|
| 105 | |
| 106 | pub fn property_read_bool(&self, name: &CStr) -> bool {
|
| 107 | |
| 108 | |
| 109 | |
| 110 | unsafe { bindings::fwnode_property_read_bool(self.as_raw(), name.as_char_ptr()) }
|
| 111 | }
|
| 112 |
|
| 113 | |
| 114 | |
| 115 | pub fn property_match_string(&self, name: &CStr, match_str: &CStr) -> Result<usize> {
|
| 116 | |
| 117 | |
| 118 | |
| 119 | let ret = unsafe {
|
| 120 | bindings::fwnode_property_match_string(
|
| 121 | self.as_raw(),
|
| 122 | name.as_char_ptr(),
|
| 123 | match_str.as_char_ptr(),
|
| 124 | )
|
| 125 | };
|
| 126 | to_result(ret)?;
|
| 127 | Ok(ret as usize)
|
| 128 | }
|
| 129 |
|
| 130 | |
| 131 | pub fn property_read_array_vec<'fwnode, 'name, T: PropertyInt>(
|
| 132 | &'fwnode self,
|
| 133 | name: &'name CStr,
|
| 134 | len: usize,
|
| 135 | ) -> Result<PropertyGuard<'fwnode, 'name, KVec<T>>> {
|
| 136 | let mut val: KVec<T> = KVec::with_capacity(len, GFP_KERNEL)?;
|
| 137 |
|
| 138 | let res = T::read_array_from_fwnode_property(self, name, val.spare_capacity_mut());
|
| 139 | let res = match res {
|
| 140 | Ok(_) => {
|
| 141 | |
| 142 | |
| 143 | |
| 144 | |
| 145 | |
| 146 | unsafe { val.inc_len(len) }
|
| 147 | Ok(val)
|
| 148 | }
|
| 149 | Err(e) => Err(e),
|
| 150 | };
|
| 151 | Ok(PropertyGuard {
|
| 152 | inner: res,
|
| 153 | fwnode: self,
|
| 154 | name,
|
| 155 | })
|
| 156 | }
|
| 157 |
|
| 158 | |
| 159 | pub fn property_count_elem<T: PropertyInt>(&self, name: &CStr) -> Result<usize> {
|
| 160 | T::read_array_len_from_fwnode_property(self, name)
|
| 161 | }
|
| 162 |
|
| 163 | |
| 164 | |
| 165 | |
| 166 | |
| 167 | |
| 168 | |
| 169 | |
| 170 | |
| 171 | |
| 172 | |
| 173 | |
| 174 | |
| 175 | |
| 176 | |
| 177 | |
| 178 | |
| 179 | |
| 180 | |
| 181 | |
| 182 | |
| 183 | |
| 184 | |
| 185 | |
| 186 | |
| 187 | |
| 188 | |
| 189 | |
| 190 | |
| 191 | |
| 192 | pub fn property_read<'fwnode, 'name, T: Property>(
|
| 193 | &'fwnode self,
|
| 194 | name: &'name CStr,
|
| 195 | ) -> PropertyGuard<'fwnode, 'name, T> {
|
| 196 | PropertyGuard {
|
| 197 | inner: T::read_from_fwnode_property(self, name),
|
| 198 | fwnode: self,
|
| 199 | name,
|
| 200 | }
|
| 201 | }
|
| 202 |
|
| 203 | |
| 204 | pub fn get_child_by_name(&self, name: &CStr) -> Option<ARef<Self>> {
|
| 205 | |
| 206 | let child =
|
| 207 | unsafe { bindings::fwnode_get_named_child_node(self.as_raw(), name.as_char_ptr()) };
|
| 208 | if child.is_null() {
|
| 209 | return None;
|
| 210 | }
|
| 211 | |
| 212 | |
| 213 | |
| 214 | |
| 215 | |
| 216 | Some(unsafe { Self::from_raw(child) })
|
| 217 | }
|
| 218 |
|
| 219 | |
| 220 | pub fn children<'a>(&'a self) -> impl Iterator<Item = ARef<FwNode>> + 'a {
|
| 221 | let mut prev: Option<ARef<FwNode>> = None;
|
| 222 |
|
| 223 | core::iter::from_fn(move || {
|
| 224 | let prev_ptr = match prev.take() {
|
| 225 | None => ptr::null_mut(),
|
| 226 | Some(prev) => {
|
| 227 | |
| 228 | |
| 229 | |
| 230 | |
| 231 | let prev = ARef::into_raw(prev);
|
| 232 | prev.as_ptr().cast()
|
| 233 | }
|
| 234 | };
|
| 235 | |
| 236 | |
| 237 | |
| 238 | |
| 239 | |
| 240 | |
| 241 | |
| 242 | |
| 243 | let next = unsafe { bindings::fwnode_get_next_child_node(self.as_raw(), prev_ptr) };
|
| 244 | if next.is_null() {
|
| 245 | return None;
|
| 246 | }
|
| 247 | |
| 248 | |
| 249 | |
| 250 | |
| 251 | |
| 252 | |
| 253 | let next = unsafe { FwNode::from_raw(next) };
|
| 254 | prev = Some(next.clone());
|
| 255 | Some(next)
|
| 256 | })
|
| 257 | }
|
| 258 |
|
| 259 | |
| 260 | pub fn property_get_reference_args(
|
| 261 | &self,
|
| 262 | prop: &CStr,
|
| 263 | nargs: NArgs<'_>,
|
| 264 | index: u32,
|
| 265 | ) -> Result<FwNodeReferenceArgs> {
|
| 266 | let mut out_args = FwNodeReferenceArgs::default();
|
| 267 |
|
| 268 | let (nargs_prop, nargs) = match nargs {
|
| 269 | NArgs::Prop(nargs_prop) => (nargs_prop.as_char_ptr(), 0),
|
| 270 | NArgs::N(nargs) => (ptr::null(), nargs),
|
| 271 | };
|
| 272 |
|
| 273 | |
| 274 | |
| 275 | |
| 276 | |
| 277 | |
| 278 | |
| 279 | |
| 280 | |
| 281 | |
| 282 | |
| 283 | |
| 284 | let ret = unsafe {
|
| 285 | bindings::fwnode_property_get_reference_args(
|
| 286 | self.0.get(),
|
| 287 | prop.as_char_ptr(),
|
| 288 | nargs_prop,
|
| 289 | nargs,
|
| 290 | index,
|
| 291 | &mut out_args.0,
|
| 292 | )
|
| 293 | };
|
| 294 | to_result(ret)?;
|
| 295 |
|
| 296 | Ok(out_args)
|
| 297 | }
|
| 298 | }
|
| 299 |
|
| 300 | |
| 301 | pub enum NArgs<'a> {
|
| 302 | |
| 303 | |
| 304 | Prop(&'a CStr),
|
| 305 | |
| 306 | N(u32),
|
| 307 | }
|
| 308 |
|
| 309 | |
| 310 | |
| 311 | |
| 312 | |
| 313 | |
| 314 | |
| 315 | |
| 316 | |
| 317 | |
| 318 | |
| 319 | |
| 320 | #[repr(transparent)]
|
| 321 | #[derive(Default)]
|
| 322 | pub struct FwNodeReferenceArgs(bindings::fwnode_reference_args);
|
| 323 |
|
| 324 | impl Drop for FwNodeReferenceArgs {
|
| 325 | fn drop(&mut self) {
|
| 326 | if !self.0.fwnode.is_null() {
|
| 327 | |
| 328 | |
| 329 | |
| 330 | |
| 331 | |
| 332 | let _ = unsafe { FwNode::from_raw(self.0.fwnode) };
|
| 333 | }
|
| 334 | }
|
| 335 | }
|
| 336 |
|
| 337 | impl FwNodeReferenceArgs {
|
| 338 | |
| 339 | pub fn as_slice(&self) -> &[u64] {
|
| 340 | |
| 341 | |
| 342 | unsafe { core::slice::from_raw_parts(self.0.args.as_ptr(), self.0.nargs as usize) }
|
| 343 | }
|
| 344 |
|
| 345 | |
| 346 | pub fn len(&self) -> usize {
|
| 347 | self.0.nargs as usize
|
| 348 | }
|
| 349 |
|
| 350 | |
| 351 | pub fn is_empty(&self) -> bool {
|
| 352 | self.0.nargs == 0
|
| 353 | }
|
| 354 | }
|
| 355 |
|
| 356 | impl fmt::Debug for FwNodeReferenceArgs {
|
| 357 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
| 358 | write!(f, "{:?}", self.as_slice())
|
| 359 | }
|
| 360 | }
|
| 361 |
|
| 362 | |
| 363 | unsafe impl crate::sync::aref::AlwaysRefCounted for FwNode {
|
| 364 | fn inc_ref(&self) {
|
| 365 | |
| 366 | |
| 367 | unsafe { bindings::fwnode_handle_get(self.as_raw()) };
|
| 368 | }
|
| 369 |
|
| 370 | unsafe fn dec_ref(obj: ptr::NonNull<Self>) {
|
| 371 | |
| 372 | |
| 373 | unsafe { bindings::fwnode_handle_put(obj.cast().as_ptr()) }
|
| 374 | }
|
| 375 | }
|
| 376 |
|
| 377 | enum Node<'a> {
|
| 378 | Borrowed(&'a FwNode),
|
| 379 | Owned(ARef<FwNode>),
|
| 380 | }
|
| 381 |
|
| 382 | impl fmt::Display for FwNode {
|
| 383 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
| 384 | |
| 385 | |
| 386 |
|
| 387 | |
| 388 | let num_parents = unsafe { bindings::fwnode_count_parents(self.as_raw()) };
|
| 389 |
|
| 390 | for depth in (0..=num_parents).rev() {
|
| 391 | let fwnode = if depth == 0 {
|
| 392 | Node::Borrowed(self)
|
| 393 | } else {
|
| 394 | |
| 395 | let ptr = unsafe { bindings::fwnode_get_nth_parent(self.as_raw(), depth) };
|
| 396 | |
| 397 | |
| 398 | |
| 399 | |
| 400 | |
| 401 | |
| 402 | |
| 403 | |
| 404 | Node::Owned(unsafe { FwNode::from_raw(ptr) })
|
| 405 | };
|
| 406 | |
| 407 | let fwnode: &FwNode = match &fwnode {
|
| 408 | Node::Borrowed(f) => f,
|
| 409 | Node::Owned(f) => f,
|
| 410 | };
|
| 411 |
|
| 412 | |
| 413 | let prefix = unsafe { bindings::fwnode_get_name_prefix(fwnode.as_raw()) };
|
| 414 | if !prefix.is_null() {
|
| 415 | |
| 416 | |
| 417 | let prefix = unsafe { CStr::from_char_ptr(prefix) };
|
| 418 | fmt::Display::fmt(prefix, f)?;
|
| 419 | }
|
| 420 | fmt::Display::fmt(&fwnode.display_name(), f)?;
|
| 421 | }
|
| 422 |
|
| 423 | Ok(())
|
| 424 | }
|
| 425 | }
|
| 426 |
|
| 427 | |
| 428 | |
| 429 | |
| 430 | |
| 431 | |
| 432 | |
| 433 | |
| 434 | |
| 435 | |
| 436 | |
| 437 | |
| 438 | pub trait Property: Sized + Sealed {
|
| 439 | |
| 440 | fn read_from_fwnode_property(fwnode: &FwNode, name: &CStr) -> Result<Self>;
|
| 441 | }
|
| 442 |
|
| 443 | impl Sealed for CString {}
|
| 444 |
|
| 445 | impl Property for CString {
|
| 446 | fn read_from_fwnode_property(fwnode: &FwNode, name: &CStr) -> Result<Self> {
|
| 447 | let mut str: *mut u8 = ptr::null_mut();
|
| 448 | let pstr: *mut _ = &mut str;
|
| 449 |
|
| 450 | |
| 451 | |
| 452 | |
| 453 | let ret = unsafe {
|
| 454 | bindings::fwnode_property_read_string(fwnode.as_raw(), name.as_char_ptr(), pstr.cast())
|
| 455 | };
|
| 456 | to_result(ret)?;
|
| 457 |
|
| 458 | |
| 459 | |
| 460 | |
| 461 | |
| 462 | |
| 463 | let str = unsafe { CStr::from_char_ptr(*pstr) };
|
| 464 | Ok(str.try_into()?)
|
| 465 | }
|
| 466 | }
|
| 467 |
|
| 468 | |
| 469 | |
| 470 | |
| 471 | |
| 472 | |
| 473 | |
| 474 | |
| 475 | |
| 476 | pub trait PropertyInt: Copy + Sealed {
|
| 477 | |
| 478 | fn read_array_from_fwnode_property<'a>(
|
| 479 | fwnode: &FwNode,
|
| 480 | name: &CStr,
|
| 481 | out: &'a mut [MaybeUninit<Self>],
|
| 482 | ) -> Result<&'a mut [Self]>;
|
| 483 |
|
| 484 | |
| 485 | fn read_array_len_from_fwnode_property(fwnode: &FwNode, name: &CStr) -> Result<usize>;
|
| 486 | }
|
| 487 | |
| 488 | |
| 489 | |
| 490 | |
| 491 | |
| 492 | macro_rules! impl_property_for_int {
|
| 493 | ($($int:ty: $f:ident),* $(,)?) => { $(
|
| 494 | impl Sealed for $int {}
|
| 495 | impl<const N: usize> Sealed for [$int; N] {}
|
| 496 |
|
| 497 | impl PropertyInt for $int {
|
| 498 | fn read_array_from_fwnode_property<'a>(
|
| 499 | fwnode: &FwNode,
|
| 500 | name: &CStr,
|
| 501 | out: &'a mut [MaybeUninit<Self>],
|
| 502 | ) -> Result<&'a mut [Self]> {
|
| 503 | // SAFETY:
|
| 504 | // - `fwnode`, `name` and `out` are all valid by their type
|
| 505 | // invariants.
|
| 506 | // - `out.len()` is a valid bound for the memory pointed to by
|
| 507 | // `out.as_mut_ptr()`.
|
| 508 | // CAST: It's ok to cast from `*mut MaybeUninit<$int>` to a
|
| 509 | // `*mut $int` because they have the same memory layout.
|
| 510 | let ret = unsafe {
|
| 511 | bindings::$f(
|
| 512 | fwnode.as_raw(),
|
| 513 | name.as_char_ptr(),
|
| 514 | out.as_mut_ptr().cast(),
|
| 515 | out.len(),
|
| 516 | )
|
| 517 | };
|
| 518 | to_result(ret)?;
|
| 519 | // SAFETY: Transmuting from `&'a mut [MaybeUninit<Self>]` to
|
| 520 | // `&'a mut [Self]` is sound, because the previous call to a
|
| 521 | // `fwnode_property_read_*_array` function (which didn't fail)
|
| 522 | // fully initialized the slice.
|
| 523 | Ok(unsafe { core::mem::transmute::<&mut [MaybeUninit<Self>], &mut [Self]>(out) })
|
| 524 | }
|
| 525 |
|
| 526 | fn read_array_len_from_fwnode_property(fwnode: &FwNode, name: &CStr) -> Result<usize> {
|
| 527 | // SAFETY:
|
| 528 | // - `fwnode` and `name` are valid by their type invariants.
|
| 529 | // - It's ok to pass a null pointer to the
|
| 530 | // `fwnode_property_read_*_array` functions if `nval` is zero.
|
| 531 | // This will return the length of the array.
|
| 532 | let ret = unsafe {
|
| 533 | bindings::$f(
|
| 534 | fwnode.as_raw(),
|
| 535 | name.as_char_ptr(),
|
| 536 | ptr::null_mut(),
|
| 537 | 0,
|
| 538 | )
|
| 539 | };
|
| 540 | to_result(ret)?;
|
| 541 | Ok(ret as usize)
|
| 542 | }
|
| 543 | }
|
| 544 |
|
| 545 | impl Property for $int {
|
| 546 | fn read_from_fwnode_property(fwnode: &FwNode, name: &CStr) -> Result<Self> {
|
| 547 | let val: [_; 1] = <[$int; 1]>::read_from_fwnode_property(fwnode, name)?;
|
| 548 | Ok(val[0])
|
| 549 | }
|
| 550 | }
|
| 551 |
|
| 552 | impl<const N: usize> Property for [$int; N] {
|
| 553 | fn read_from_fwnode_property(fwnode: &FwNode, name: &CStr) -> Result<Self> {
|
| 554 | let mut val: [MaybeUninit<$int>; N] = [const { MaybeUninit::uninit() }; N];
|
| 555 |
|
| 556 | <$int>::read_array_from_fwnode_property(fwnode, name, &mut val)?;
|
| 557 |
|
| 558 | // SAFETY: `val` is always initialized when
|
| 559 | // `fwnode_property_read_*_array` is successful.
|
| 560 | Ok(val.map(|v| unsafe { v.assume_init() }))
|
| 561 | }
|
| 562 | }
|
| 563 | )* };
|
| 564 | }
|
| 565 | impl_property_for_int! {
|
| 566 | u8: fwnode_property_read_u8_array,
|
| 567 | u16: fwnode_property_read_u16_array,
|
| 568 | u32: fwnode_property_read_u32_array,
|
| 569 | u64: fwnode_property_read_u64_array,
|
| 570 | i8: fwnode_property_read_u8_array,
|
| 571 | i16: fwnode_property_read_u16_array,
|
| 572 | i32: fwnode_property_read_u32_array,
|
| 573 | i64: fwnode_property_read_u64_array,
|
| 574 | }
|
| 575 |
|
| 576 | /// A helper for reading device properties.
|
| 577 | ///
|
| 578 | /// Use [`Self::required_by`] if a missing property is considered a bug and
|
| 579 | /// [`Self::optional`] otherwise.
|
| 580 | ///
|
| 581 | /// For convenience, [`Self::or`] and [`Self::or_default`] are provided.
|
| 582 | pub struct PropertyGuard<'fwnode, 'name, T> {
|
| 583 | /// The result of reading the property.
|
| 584 | inner: Result<T>,
|
| 585 | /// The fwnode of the property, used for logging in the "required" case.
|
| 586 | fwnode: &'fwnode FwNode,
|
| 587 | /// The name of the property, used for logging in the "required" case.
|
| 588 | name: &'name CStr,
|
| 589 | }
|
| 590 |
|
| 591 | impl<T> PropertyGuard<'_, '_, T> {
|
| 592 | /// Access the property, indicating it is required.
|
| 593 | ///
|
| 594 | /// If the property is not present, the error is automatically logged. If a
|
| 595 | /// missing property is not an error, use [`Self::optional`] instead. The
|
| 596 | /// device is required to associate the log with it.
|
| 597 | pub fn required_by(self, dev: &super::Device) -> Result<T> {
|
| 598 | if self.inner.is_err() {
|
| 599 | dev_err!(
|
| 600 | dev,
|
| 601 | "{}: property '{}' is missing\n",
|
| 602 | self.fwnode,
|
| 603 | self.name
|
| 604 | );
|
| 605 | }
|
| 606 | self.inner
|
| 607 | }
|
| 608 |
|
| 609 | |
| 610 | |
| 611 | |
| 612 | |
| 613 | pub fn optional(self) -> Option<T> {
|
| 614 | self.inner.ok()
|
| 615 | }
|
| 616 |
|
| 617 | |
| 618 | |
| 619 | |
| 620 | |
| 621 | pub fn or(self, default: T) -> T {
|
| 622 | self.inner.unwrap_or(default)
|
| 623 | }
|
| 624 | }
|
| 625 |
|
| 626 | impl<T: Default> PropertyGuard<'_, '_, T> {
|
| 627 | |
| 628 | |
| 629 | |
| 630 | pub fn or_default(self) -> T {
|
| 631 | self.inner.unwrap_or_default()
|
| 632 | }
|
| 633 | }
|