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
18extern crate alloc;
19
20use alloc::boxed::Box;
21
22/// All supported device types.
23#[derive(Debug, Clone, Copy, Eq, PartialEq)]
24pub enum DeviceType {
25 /// Block storage device (e.g., disk).
26 Block,
27 /// Character device (e.g., serial port).
28 Char,
29 /// Network device (e.g., ethernet card).
30 Net,
31 /// Graphic display device (e.g., GPU)
32 Display,
33 /// Input device (e.g., keyboard, mouse).
34 Input,
35 /// Vsock device (e.g., virtio-vsock).
36 Vsock,
37}
38
39/// The error type for device operation failures.
40#[derive(Debug)]
41pub enum DevError {
42 /// An entity already exists.
43 AlreadyExists,
44 /// Try again, for non-blocking APIs.
45 Again,
46 /// Bad internal state.
47 BadState,
48 /// Invalid parameter/argument.
49 InvalidParam,
50 /// Input/output error.
51 Io,
52 /// Not enough space/cannot allocate memory (DMA).
53 NoMemory,
54 /// Device or resource is busy.
55 ResourceBusy,
56 /// This operation is unsupported or unimplemented.
57 Unsupported,
58}
59
60impl core::fmt::Display for DevError {
61 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
62 match self {
63 DevError::AlreadyExists => write!(f, "Entity already exists"),
64 DevError::Again => write!(f, "Try again"),
65 DevError::BadState => write!(f, "Bad state"),
66 DevError::InvalidParam => write!(f, "Invalid parameter"),
67 DevError::Io => write!(f, "Input/output error"),
68 DevError::NoMemory => write!(f, "Not enough memory"),
69 DevError::ResourceBusy => write!(f, "Resource is busy"),
70 DevError::Unsupported => write!(f, "Unsupported operation"),
71 }
72 }
73}
74
75/// A specialized `Result` type for device operations.
76pub type DevResult<T = ()> = Result<T, DevError>;
77
78/// Common operations that require all device drivers to implement.
79pub trait BaseDriverOps: Send + Sync {
80 /// The name of the device.
81 fn device_name(&self) -> &str;
82
83 /// The type of the device.
84 fn device_type(&self) -> DeviceType;
85
86 /// The IRQ number of the device, if applicable.
87 fn irq_num(&self) -> Option<usize> {
88 None
89 }
90}
91
92impl<T: BaseDriverOps + ?Sized> BaseDriverOps for Box<T> {
93 fn device_name(&self) -> &str {
94 (**self).device_name()
95 }
96
97 fn device_type(&self) -> DeviceType {
98 (**self).device_type()
99 }
100
101 fn irq_num(&self) -> Option<usize> {
102 (**self).irq_num()
103 }
104}