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.
This commit is contained in:
Red Bear OS
2026-07-12 02:39:59 +03:00
parent bf9989b7c0
commit 4932cb4d62
2 changed files with 33 additions and 0 deletions
+10
View File
@@ -65,4 +65,14 @@ impl PciBar {
_ => None,
}
}
pub fn try_port(&self) -> Result<u16, &'static str> {
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"),
}
}
}
+23
View File
@@ -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<File> {
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 {