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