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