axdriver/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`]: axdriver_virtio::VirtIoNetDev
53//! [`Box<dyn NetDriverOps>`]: axdriver_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#![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
69mod bus;
70mod drivers;
71mod dummy;
72mod structs;
73
74#[cfg(feature = "virtio")]
75mod virtio;
76
77#[cfg(feature = "ixgbe")]
78mod ixgbe;
79
80pub mod prelude;
81
82#[allow(unused_imports)]
83use self::prelude::*;
84pub use self::structs::{AxDeviceContainer, AxDeviceEnum};
85
86#[cfg(feature = "block")]
87pub use self::structs::AxBlockDevice;
88#[cfg(feature = "display")]
89pub use self::structs::AxDisplayDevice;
90#[cfg(feature = "net")]
91pub use self::structs::AxNetDevice;
92
93/// A structure that contains all device drivers, organized by their category.
94#[derive(Default)]
95pub struct AllDevices {
96 /// All network device drivers.
97 #[cfg(feature = "net")]
98 pub net: AxDeviceContainer<AxNetDevice>,
99 /// All block device drivers.
100 #[cfg(feature = "block")]
101 pub block: AxDeviceContainer<AxBlockDevice>,
102 /// All graphics device drivers.
103 #[cfg(feature = "display")]
104 pub display: AxDeviceContainer<AxDisplayDevice>,
105}
106
107impl AllDevices {
108 /// Returns the device model used, either `dyn` or `static`.
109 ///
110 /// See the [crate-level documentation](crate) for more details.
111 pub const fn device_model() -> &'static str {
112 if cfg!(feature = "dyn") {
113 "dyn"
114 } else {
115 "static"
116 }
117 }
118
119 /// Probes all supported devices.
120 fn probe(&mut self) {
121 for_each_drivers!(type Driver, {
122 if let Some(dev) = Driver::probe_global() {
123 info!(
124 "registered a new {:?} device: {:?}",
125 dev.device_type(),
126 dev.device_name(),
127 );
128 self.add_device(dev);
129 }
130 });
131
132 self.probe_bus_devices();
133 }
134
135 /// Adds one device into the corresponding container, according to its device category.
136 #[allow(dead_code)]
137 fn add_device(&mut self, dev: AxDeviceEnum) {
138 match dev {
139 #[cfg(feature = "net")]
140 AxDeviceEnum::Net(dev) => self.net.push(dev),
141 #[cfg(feature = "block")]
142 AxDeviceEnum::Block(dev) => self.block.push(dev),
143 #[cfg(feature = "display")]
144 AxDeviceEnum::Display(dev) => self.display.push(dev),
145 }
146 }
147}
148
149/// Probes and initializes all device drivers, returns the [`AllDevices`] struct.
150pub fn init_drivers() -> AllDevices {
151 info!("Initialize device drivers...");
152 info!(" device model: {}", AllDevices::device_model());
153
154 let mut all_devs = AllDevices::default();
155 all_devs.probe();
156
157 #[cfg(feature = "net")]
158 {
159 debug!("number of NICs: {}", all_devs.net.len());
160 for (i, dev) in all_devs.net.iter().enumerate() {
161 assert_eq!(dev.device_type(), DeviceType::Net);
162 debug!(" NIC {}: {:?}", i, dev.device_name());
163 }
164 }
165 #[cfg(feature = "block")]
166 {
167 debug!("number of block devices: {}", all_devs.block.len());
168 for (i, dev) in all_devs.block.iter().enumerate() {
169 assert_eq!(dev.device_type(), DeviceType::Block);
170 debug!(" block device {}: {:?}", i, dev.device_name());
171 }
172 }
173 #[cfg(feature = "display")]
174 {
175 debug!("number of graphics devices: {}", all_devs.display.len());
176 for (i, dev) in all_devs.display.iter().enumerate() {
177 assert_eq!(dev.device_type(), DeviceType::Display);
178 debug!(" graphics device {}: {:?}", i, dev.device_name());
179 }
180 }
181
182 all_devs
183}