Skip to main content

hermit/drivers/
mod.rs

1//! A module containing hermit-rs driver, hermit-rs driver trait and driver specific errors.
2
3#[cfg(feature = "virtio-console")]
4pub mod console;
5#[cfg(feature = "virtio-fs")]
6pub mod fs;
7#[cfg(not(feature = "pci"))]
8pub mod mmio;
9#[cfg(feature = "net")]
10pub mod net;
11#[cfg(feature = "pci")]
12pub mod pci;
13#[cfg(feature = "virtio")]
14pub mod virtio;
15#[cfg(feature = "virtio-vsock")]
16pub mod vsock;
17
18use alloc::collections::VecDeque;
19
20use ahash::RandomState;
21use hashbrown::HashMap;
22#[cfg(feature = "pci")]
23pub(crate) use pci_types::InterruptLine;
24#[cfg(not(feature = "pci"))]
25pub(crate) type InterruptLine = u8;
26
27pub(crate) type InterruptHandlerMap = HashMap<InterruptLine, VecDeque<fn()>, RandomState>;
28
29/// A common error module for drivers.
30/// [DriverError](error::DriverError) values will be
31/// passed on to higher layers.
32pub mod error {
33	#[cfg(any(
34		feature = "virtio",
35		all(target_arch = "riscv64", feature = "gem-net", not(feature = "pci")),
36		feature = "rtl8139",
37	))]
38	use thiserror::Error;
39
40	#[cfg(all(target_arch = "riscv64", feature = "gem-net", not(feature = "pci")))]
41	use crate::drivers::net::gem::GEMError;
42	#[cfg(feature = "rtl8139")]
43	use crate::drivers::net::rtl8139::RTL8139Error;
44	#[cfg(feature = "virtio")]
45	use crate::drivers::virtio::error::VirtioError;
46
47	#[cfg(any(
48		feature = "virtio",
49		all(target_arch = "riscv64", feature = "gem-net", not(feature = "pci")),
50		feature = "rtl8139",
51	))]
52	#[derive(Error, Debug)]
53	pub enum DriverError {
54		#[cfg(feature = "virtio")]
55		#[error("Virtio driver failed: {0:?}")]
56		InitVirtioDevFail(#[from] VirtioError),
57
58		#[cfg(feature = "rtl8139")]
59		#[error("RTL8139 driver failed: {0:?}")]
60		InitRTL8139DevFail(#[from] RTL8139Error),
61
62		#[cfg(all(target_arch = "riscv64", feature = "gem-net", not(feature = "pci")))]
63		#[error("GEM driver failed: {0:?}")]
64		InitGEMDevFail(#[from] GEMError),
65	}
66}
67
68/// A trait to determine general driver information
69#[allow(dead_code)]
70pub(crate) trait Driver {
71	/// Returns the device driver name
72	fn get_name() -> &'static str;
73}
74
75pub(crate) fn init() {
76	#[cfg_attr(
77		all(
78			not(feature = "pci"),
79			not(target_arch = "riscv64"),
80			not(feature = "virtio")
81		),
82		expect(unused_mut)
83	)]
84	let mut handlers = HashMap::with_hasher(RandomState::with_seeds(0, 0, 0, 0));
85
86	// Initialize PCI Drivers
87	#[cfg(feature = "pci")]
88	pci::init(&mut handlers);
89	#[cfg(all(feature = "pci", target_arch = "x86_64"))]
90	crate::arch::kernel::serial::register_handler(&mut handlers);
91
92	#[cfg(all(not(feature = "pci"), feature = "virtio", target_arch = "x86_64"))]
93	crate::arch::kernel::mmio::init_drivers(&mut handlers);
94	#[cfg(all(not(feature = "pci"), feature = "virtio", target_arch = "aarch64"))]
95	crate::arch::kernel::mmio::init_drivers(&mut handlers);
96
97	#[cfg(target_arch = "riscv64")]
98	crate::arch::kernel::init_drivers(&mut handlers);
99
100	crate::arch::kernel::interrupts::install_handlers(handlers);
101}