@@ -31,6 +31,7 @@
#include <linux/pid_namespace.h>
#include <linux/platform_device.h>
#include <linux/poll.h>
+#include <linux/property.h>
#include <linux/refcount.h>
#include <linux/sched.h>
#include <linux/security.h>
@@ -6,6 +6,9 @@
use crate::{
bindings,
+ error::Result,
+ prelude::ENODEV,
+ str::CString,
types::{ARef, Opaque},
};
use core::{fmt, ptr};
@@ -59,6 +62,18 @@ pub unsafe fn get_device(ptr: *mut bindings::device) -> ARef<Self> {
unsafe { Self::as_ref(ptr) }.into()
}
+ /// Creates a new ref-counted instance of device of a CPU.
+ pub fn from_cpu(cpu: u32) -> Result<ARef<Self>> {
+ // SAFETY: It is safe to call `get_cpu_device()` for any CPU number.
+ let ptr = unsafe { bindings::get_cpu_device(cpu) };
+ if ptr.is_null() {
+ return Err(ENODEV);
+ }
+
+ // SAFETY: By the safety requirements, ptr is valid.
+ Ok(unsafe { Device::get_device(ptr) })
+ }
+
/// Obtain the raw `struct device *`.
pub(crate) fn as_raw(&self) -> *mut bindings::device {
self.0.get()
@@ -180,6 +195,12 @@ unsafe fn printk(&self, klevel: &[u8], msg: fmt::Arguments<'_>) {
)
};
}
+
+ /// Checks if property is present or not.
+ pub fn property_present(&self, name: &CString) -> bool {
+ // SAFETY: `name` is null-terminated. `self.as_raw` is valid because `self` is valid.
+ unsafe { bindings::device_property_present(self.as_raw(), name.as_ptr() as *const _) }
+ }
}
// SAFETY: Instances of `Device` are always reference-counted.
Add from_cpu() and property_present() helpers to the device bindings. Signed-off-by: Viresh Kumar <viresh.kumar@linaro.org> --- rust/bindings/bindings_helper.h | 1 + rust/kernel/device.rs | 21 +++++++++++++++++++++ 2 files changed, 22 insertions(+)