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:
@@ -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"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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 {
|
||||
|
||||
Reference in New Issue
Block a user