ax_driver_base/lib.rs
1//! Device driver interfaces used by [ArceOS][1]. It provides common traits and
2//! types for implementing a device driver.
3//!
4//! You have to use this crate with the following crates for corresponding
5//! device types:
6//!
7//! - [`axdriver_block`][2]: Common traits for block storage drivers.
8//! - [`axdriver_display`][3]: Common traits and types for graphics display drivers.
9//! - [`axdriver_net`][4]: Common traits and types for network (NIC) drivers.
10//!
11//! [1]: https://github.com/arceos-org/arceos
12//! [2]: ../ax-driver-block/index.html
13//! [3]: ../ax-driver-display/index.html
14//! [4]: ../ax-driver-net/index.html
15
16#![no_std]
17
18/// All supported device types.
19#[derive(Debug, Clone, Copy, Eq, PartialEq)]
20pub enum DeviceType {
21 /// Block storage device (e.g., disk).
22 Block,
23 /// Character device (e.g., serial port).
24 Char,
25 /// Network device (e.g., ethernet card).
26 Net,
27 /// Graphic display device (e.g., GPU)
28 Display,
29 /// Input device (e.g., keyboard, mouse).
30 Input,
31 /// Vsock device (e.g., virtio-vsock).
32 Vsock,
33}
34
35/// The error type for device operation failures.
36#[derive(Debug)]
37pub enum DevError {
38 /// An entity already exists.
39 AlreadyExists,
40 /// Try again, for non-blocking APIs.
41 Again,
42 /// Bad internal state.
43 BadState,
44 /// Invalid parameter/argument.
45 InvalidParam,
46 /// Input/output error.
47 Io,
48 /// Not enough space/cannot allocate memory (DMA).
49 NoMemory,
50 /// Device or resource is busy.
51 ResourceBusy,
52 /// This operation is unsupported or unimplemented.
53 Unsupported,
54}
55
56impl core::fmt::Display for DevError {
57 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
58 match self {
59 DevError::AlreadyExists => write!(f, "Entity already exists"),
60 DevError::Again => write!(f, "Try again"),
61 DevError::BadState => write!(f, "Bad state"),
62 DevError::InvalidParam => write!(f, "Invalid parameter"),
63 DevError::Io => write!(f, "Input/output error"),
64 DevError::NoMemory => write!(f, "Not enough memory"),
65 DevError::ResourceBusy => write!(f, "Resource is busy"),
66 DevError::Unsupported => write!(f, "Unsupported operation"),
67 }
68 }
69}
70
71/// A specialized `Result` type for device operations.
72pub type DevResult<T = ()> = Result<T, DevError>;
73
74/// Common operations that require all device drivers to implement.
75pub trait BaseDriverOps: Send + Sync {
76 /// The name of the device.
77 fn device_name(&self) -> &str;
78
79 /// The type of the device.
80 fn device_type(&self) -> DeviceType;
81
82 /// The IRQ number of the device, if applicable.
83 fn irq_num(&self) -> Option<usize> {
84 None
85 }
86}