summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDave Airlie <airlied@redhat.com>2023-11-29 18:01:08 +1000
committerDave Airlie <airlied@redhat.com>2023-11-30 08:42:17 +1000
commit430460341cc22bc4b6a37ebbf031edbf6595f8c8 (patch)
treec855a5b4a75031a05cb393d5d2c1e56415987cc2
parent9e81179bfa0ce972cc1f9b147ec784195ca2c0ad (diff)
rust: add initial firmware loading bindings.
-rw-r--r--rust/kernel/firmware.rs60
-rw-r--r--rust/kernel/lib.rs1
2 files changed, 61 insertions, 0 deletions
diff --git a/rust/kernel/firmware.rs b/rust/kernel/firmware.rs
new file mode 100644
index 000000000000..8c54096a6737
--- /dev/null
+++ b/rust/kernel/firmware.rs
@@ -0,0 +1,60 @@
+// SPDX-License-Identifier: GPL-2.0
+
+//! Firmware load
+//!
+//! C header: [`include/linux/firmware.h`](../../../../include/linux/firmware.h")
+
+use crate::{
+ bindings,
+ error::Error,
+ error::Result,
+ device::{RawDevice},
+ str::CStr,
+ types::Opaque,
+};
+
+/// Pointer to a C firmware struct
+pub struct Firmware {
+ pub fw: Opaque<*const bindings::firmware>,
+ fw_size: usize,
+}
+
+impl Firmware {
+ /// Create a new firmware instance.
+ pub const fn new() -> Self {
+ Self {
+ fw: Opaque::uninit(),
+ fw_size: 0,
+ }
+ }
+
+ pub fn request(&mut self, name: &CStr, dev: &dyn RawDevice) -> Result {
+ let ret = unsafe { bindings::request_firmware(self.fw.get(),
+ name.as_char_ptr(), dev.raw_device()) };
+ if ret != 0 {
+ return Err(Error::from_errno(ret));
+ }
+ self.fw_size = unsafe { (*(*self.fw.get())).size };
+ Ok(())
+ }
+
+ pub fn request_nowarn(&mut self, name: &CStr, dev: &dyn RawDevice) -> Result {
+ let ret = unsafe { bindings::firmware_request_nowarn(self.fw.get(),
+ name.as_char_ptr(), dev.raw_device()) };
+ if ret != 0 {
+ return Err(Error::from_errno(ret));
+ }
+ self.fw_size = unsafe { (*(*self.fw.get())).size };
+ Ok(())
+ }
+
+ pub fn size(&self) -> usize {
+ self.fw_size
+ }
+}
+
+impl Drop for Firmware {
+ fn drop(&mut self) {
+ unsafe {bindings::release_firmware(*self.fw.get())};
+ }
+}
diff --git a/rust/kernel/lib.rs b/rust/kernel/lib.rs
index 0cbb01bfce8e..82188b6d3639 100644
--- a/rust/kernel/lib.rs
+++ b/rust/kernel/lib.rs
@@ -40,6 +40,7 @@ pub mod device;
pub mod dma;
pub mod driver;
pub mod error;
+pub mod firmware;
pub mod init;
pub mod io_mem;
pub mod ioctl;