ax_driver/lib.rs
1//! [ArceOS](https://github.com/arceos-org/arceos) device drivers.
2//!
3//! # Usage
4//!
5//! All detected devices are composed into a large struct [`AllDevices`]
6//! and returned by the [`init_drivers`] function. The upperlayer subsystems
7//! (e.g., the network stack) may unpack the struct to get the specified device
8//! driver they want.
9//!
10//! For each device category (i.e., net, block, display, etc.), an unified type
11//! is used to represent all devices in that category. Currently, there are 3
12//! categories: [`AxNetDevice`], [`AxBlockDevice`], and [`AxDisplayDevice`].
13//!
14//! # Concepts
15//!
16//! This crate supports two device models depending on the `dyn` feature:
17//!
18//! - **Static**: The type of all devices is static, it is determined at compile
19//! time by corresponding cargo features. For example, [`AxNetDevice`] will be
20//! an alias of [`VirtioNetDev`] if the `virtio-net` feature is enabled. This
21//! model provides the best performance as it avoids dynamic dispatch. But on
22//! limitation, only one device instance is supported for each device category.
23//! - **Dynamic**: All device instance is using [trait objects] and wrapped in a
24//! `Box<dyn Trait>`. For example, [`AxNetDevice`] will be [`Box<dyn NetDriverOps>`].
25//! When call a method provided by the device, it uses [dynamic dispatch][dyn]
26//! that may introduce a little overhead. But on the other hand, it is more
27//! flexible, multiple instances of each device category are supported.
28//!
29//! # Supported Devices
30//!
31//! | Device Category | Cargo Feature | Description |
32//! |-|-|-|
33//! | Block | `ramdisk` | A RAM disk that stores data in a vector |
34//! | Block | `virtio-blk` | VirtIO block device |
35//! | Network | `virtio-net` | VirtIO network device |
36//! | Display | `virtio-gpu` | VirtIO graphics device |
37//!
38//! # Other Cargo Features
39//!
40//! - `dyn`: use the dynamic device model (see above).
41//! - `bus-mmio`: use device tree to probe all MMIO devices.
42//! - `bus-pci`: use PCI bus to probe all PCI devices. This feature is
43//! enabled by default.
44//! - `virtio`: use VirtIO devices. This is enabled if any of `virtio-blk`,
45//! `virtio-net` or `virtio-gpu` is enabled.
46//! - `net`: use network devices. This is enabled if any feature of network
47//! devices is selected. If this feature is enabled without any network device
48//! features, a dummy struct is used for [`AxNetDevice`].
49//! - `block`: use block storage devices. Similar to the `net` feature.
50//! - `display`: use graphics display devices. Similar to the `net` feature.
51//!
52//! [`VirtioNetDev`]: ax_driver_virtio::VirtIoNetDev
53//! [`Box<dyn NetDriverOps>`]: ax_driver_net::NetDriverOps
54//! [trait objects]: https://doc.rust-lang.org/book/ch17-02-trait-objects.html
55//! [dyn]: https://doc.rust-lang.org/std/keyword.dyn.html
56
57#![no_std]
58#![cfg_attr(feature = "virtio", feature(associated_type_defaults))]
59
60#[macro_use]
61extern crate log;
62
63#[cfg(feature = "dyn")]
64extern crate alloc;
65
66#[macro_use]
67mod macros;
68
69#[cfg(not(feature = "dyn"))]
70mod bus;
71mod drivers;
72mod dummy;
73mod structs;
74
75#[cfg(feature = "virtio")]
76mod virtio;
77
78#[cfg(feature = "ixgbe")]
79mod ixgbe;
80
81#[cfg(feature = "dyn")]
82mod dyn_drivers;
83
84pub mod prelude;
85
86#[allow(unused_imports)]
87use self::prelude::*;
88#[cfg(feature = "block")]
89pub use self::structs::AxBlockDevice;
90#[cfg(feature = "display")]
91pub use self::structs::AxDisplayDevice;
92#[cfg(feature = "net")]
93pub use self::structs::AxNetDevice;
94pub use self::structs::{AxDeviceContainer, AxDeviceEnum};
95
96/// A structure that contains all device drivers, organized by their category.
97#[derive(Default)]
98pub struct AllDevices {
99 /// All network device drivers.
100 #[cfg(feature = "net")]
101 pub net: AxDeviceContainer<AxNetDevice>,
102 /// All block device drivers.
103 #[cfg(feature = "block")]
104 pub block: AxDeviceContainer<AxBlockDevice>,
105 /// All graphics device drivers.
106 #[cfg(feature = "display")]
107 pub display: AxDeviceContainer<AxDisplayDevice>,
108 /// All input device drivers.
109 #[cfg(feature = "input")]
110 pub input: AxDeviceContainer<AxInputDevice>,
111 /// All vsock device drivers.
112 #[cfg(feature = "vsock")]
113 pub vsock: AxDeviceContainer<AxVsockDevice>,
114}
115
116impl AllDevices {
117 /// Returns the device model used, either `dyn` or `static`.
118 ///
119 /// See the [crate-level documentation](crate) for more details.
120 pub const fn device_model() -> &'static str {
121 if cfg!(feature = "dyn") {
122 "dyn"
123 } else {
124 "static"
125 }
126 }
127
128 /// Probes all supported devices.
129 fn probe(&mut self) {
130 #[cfg(feature = "dyn")]
131 for dev in dyn_drivers::probe_all_devices() {
132 self.add_device(dev);
133 }
134 #[cfg(not(feature = "dyn"))]
135 {
136 for_each_drivers!(type Driver, {
137 if let Some(dev) = Driver::probe_global() {
138 info!(
139 "registered a new {:?} device: {:?}",
140 dev.device_type(),
141 dev.device_name(),
142 );
143 self.add_device(dev);
144 }
145 });
146
147 self.probe_bus_devices();
148 }
149 }
150
151 /// Adds one device into the corresponding container, according to its device category.
152 #[allow(dead_code)]
153 fn add_device(&mut self, dev: AxDeviceEnum) {
154 match dev {
155 #[cfg(feature = "net")]
156 AxDeviceEnum::Net(dev) => self.net.push(dev),
157 #[cfg(feature = "block")]
158 AxDeviceEnum::Block(dev) => self.block.push(dev),
159 #[cfg(feature = "display")]
160 AxDeviceEnum::Display(dev) => self.display.push(dev),
161 #[cfg(feature = "input")]
162 AxDeviceEnum::Input(dev) => self.input.push(dev),
163 #[cfg(feature = "vsock")]
164 AxDeviceEnum::Vsock(dev) => self.vsock.push(dev),
165 }
166 }
167}
168
169/// Probes and initializes all device drivers, returns the [`AllDevices`] struct.
170pub fn init_drivers() -> AllDevices {
171 info!("Initialize device drivers...");
172 info!(" device model: {}", AllDevices::device_model());
173
174 let mut all_devs = AllDevices::default();
175 all_devs.probe();
176
177 #[cfg(feature = "net")]
178 {
179 debug!("number of NICs: {}", all_devs.net.len());
180 for (i, dev) in all_devs.net.iter().enumerate() {
181 assert_eq!(dev.device_type(), DeviceType::Net);
182 debug!(" NIC {}: {:?}", i, dev.device_name());
183 }
184 }
185 #[cfg(feature = "block")]
186 {
187 debug!("number of block devices: {}", all_devs.block.len());
188 for (i, dev) in all_devs.block.iter().enumerate() {
189 assert_eq!(dev.device_type(), DeviceType::Block);
190 debug!(" block device {}: {:?}", i, dev.device_name());
191 }
192 }
193 #[cfg(feature = "display")]
194 {
195 debug!("number of graphics devices: {}", all_devs.display.len());
196 for (i, dev) in all_devs.display.iter().enumerate() {
197 assert_eq!(dev.device_type(), DeviceType::Display);
198 debug!(" graphics device {}: {:?}", i, dev.device_name());
199 }
200 }
201 #[cfg(feature = "input")]
202 {
203 debug!("number of input devices: {}", all_devs.input.len());
204 for (i, dev) in all_devs.input.iter().enumerate() {
205 assert_eq!(dev.device_type(), DeviceType::Input);
206 debug!(" input device {}: {:?}", i, dev.device_name());
207 }
208 }
209 #[cfg(feature = "vsock")]
210 {
211 debug!("number of vsock devices: {}", all_devs.vsock.len());
212 for (i, dev) in all_devs.vsock.iter().enumerate() {
213 assert_eq!(dev.device_type(), DeviceType::Vsock);
214 debug!(" vsock device {}: {:?}", i, dev.device_name());
215 }
216 }
217
218 all_devs
219}