From 4932cb4d6252b9dd49394a8ca2433999ecce0c3e Mon Sep 17 00:00:00 2001 From: Red Bear OS Date: Sun, 12 Jul 2026 02:39:59 +0300 Subject: [PATCH] fix: add try_port() to PciBar and try_irq_handle() to LegacyInterruptLine Non-panicking variants needed by ac97d, vboxd, and other drivers that prefer error handling over panics. --- drivers/pcid/src/driver_interface/bar.rs | 10 ++++++++++ drivers/pcid/src/driver_interface/mod.rs | 23 +++++++++++++++++++++++ 2 files changed, 33 insertions(+) diff --git a/drivers/pcid/src/driver_interface/bar.rs b/drivers/pcid/src/driver_interface/bar.rs index 03bce8474a..d398ca4281 100644 --- a/drivers/pcid/src/driver_interface/bar.rs +++ b/drivers/pcid/src/driver_interface/bar.rs @@ -65,4 +65,14 @@ impl PciBar { _ => None, } } + + pub fn try_port(&self) -> Result { + match *self { + PciBar::Port(port) => Ok(port), + PciBar::Memory32 { .. } | PciBar::Memory64 { .. } => { + Err("expected port BAR, found memory BAR") + } + PciBar::None => Err("expected BAR to exist"), + } + } } diff --git a/drivers/pcid/src/driver_interface/mod.rs b/drivers/pcid/src/driver_interface/mod.rs index 8ef55a7580..fa5adf65b2 100644 --- a/drivers/pcid/src/driver_interface/mod.rs +++ b/drivers/pcid/src/driver_interface/mod.rs @@ -50,6 +50,29 @@ impl LegacyInterruptLine { .unwrap_or_else(|err| panic!("{driver}: failed to open IRQ file: {err}")) } } + + /// Non-panicking version of `irq_handle`. + pub fn try_irq_handle(self, _driver: &str) -> io::Result { + if let Some((phandle, addr, cells)) = self.phandled { + let path = match cells { + 1 => format!("/scheme/irq/phandle-{}/{}", phandle, addr[0]), + 2 => format!("/scheme/irq/phandle-{}/{},{}", phandle, addr[0], addr[1]), + 3 => format!( + "/scheme/irq/phandle-{}/{},{},{}", + phandle, addr[0], addr[1], addr[2] + ), + _ => { + return Err(io::Error::new( + io::ErrorKind::InvalidInput, + format!("unexpected IRQ cells count: {cells}"), + )) + } + }; + File::create(path) + } else { + File::open(format!("/scheme/irq/{}", self.irq)) + } + } } impl fmt::Display for LegacyInterruptLine {