1#[macro_use]
4pub mod bus;
5pub mod block;
6pub mod console;
7pub mod gpu;
8pub mod input;
9pub mod irq;
10pub mod net;
11pub mod rtc;
12pub mod serial;
13pub mod virtio_hal;
14
15pub mod device_tree;
16
17use alloc::sync::Arc;
18pub use block::ram_disk::RamDisk;
19use spin::RwLock;
20
21use crate::device::rtc::RtcDriver;
22
23use crate::device::serial::SerialDriver;
24use crate::device::{block::BlockDriver, net::net_device::NetDevice};
25
26use alloc::{string::String, vec::Vec};
27use lazy_static::lazy_static;
28
29#[derive(Debug, Eq, PartialEq)]
31pub enum DeviceType {
32 Net,
34 Gpu,
36 Input,
38 Block,
40 Rtc,
42 Serial,
44 Intc,
46}
47
48pub trait Driver: Send + Sync {
50 fn try_handle_interrupt(&self, irq: Option<usize>) -> bool;
55
56 fn device_type(&self) -> DeviceType;
58
59 fn get_id(&self) -> String;
62
63 fn as_net(&self) -> Option<&dyn NetDevice> {
65 None
66 }
67
68 fn as_net_arc(self: Arc<Self>) -> Option<Arc<dyn NetDevice>> {
70 None
71 }
72
73 fn as_block(&self) -> Option<&dyn BlockDriver> {
75 None
76 }
77
78 fn as_block_arc(self: Arc<Self>) -> Option<Arc<dyn BlockDriver>> {
80 None
81 }
82
83 fn as_rtc(&self) -> Option<&dyn RtcDriver> {
85 None
86 }
87
88 fn as_rtc_arc(self: Arc<Self>) -> Option<Arc<dyn RtcDriver>> {
90 None
91 }
92
93 fn as_serial(&self) -> Option<&dyn serial::SerialDriver> {
95 None
96 }
97}
98
99lazy_static! {
100 pub static ref DRIVERS: RwLock<Vec<Arc<dyn Driver>>> = RwLock::new(Vec::new());
102 pub static ref BLK_DRIVERS: RwLock<Vec<Arc<dyn BlockDriver>>> = RwLock::new(Vec::new());
103 pub static ref RTC_DRIVERS: RwLock<Vec<Arc<dyn RtcDriver>>> = RwLock::new(Vec::new());
104 pub static ref SERIAL_DRIVERS: RwLock<Vec<Arc<dyn SerialDriver>>> = RwLock::new(Vec::new());
105 pub static ref IRQ_MANAGER: RwLock<irq::IrqManager> = RwLock::new(irq::IrqManager::new(true));
106}
107
108lazy_static! {
109 pub static ref CMDLINE: RwLock<String> = RwLock::new(String::new());
112}
113
114pub fn register_driver(driver: Arc<dyn Driver>) {
116 DRIVERS.write().push(driver);
117}