xref: /DragonOS/kernel/src/driver/base/device/mod.rs (revision 13776c114b15c406b1e0aaeeb71812ea6e471d2e)
1 use core::{any::Any, fmt::Debug};
2 
3 pub mod bus;
4 pub mod driver;
5 
6 /// @brief: 设备类型
7 #[allow(dead_code)]
8 #[derive(Debug, Eq, PartialEq)]
9 pub enum DeviceType {
10     Bus,
11     Net,
12     Gpu,
13     Input,
14     Block,
15     Rtc,
16     Serial,
17     Intc,
18     PlatformDev,
19 }
20 
21 /// @brief: 设备标识符类型
22 #[derive(Debug, Clone, Hash, PartialOrd, PartialEq, Ord, Eq)]
23 pub struct IdTable(&'static str, u32);
24 
25 /// @brief: 设备标识符操作方法集
26 impl IdTable {
27     /// @brief: 创建一个新的设备标识符
28     /// @parameter name: 设备名
29     /// @parameter id: 设备id
30     /// @return: 设备标识符
31     pub fn new(name: &'static str, id: u32) -> IdTable {
32         Self(name, id)
33     }
34 }
35 
36 /// @brief: 设备当前状态
37 #[derive(Debug, Clone, Copy)]
38 pub enum DeviceState {
39     NotInitialized = 0,
40     Initialized = 1,
41     UnDefined = 2,
42 }
43 
44 /// @brief: 设备错误类型
45 #[derive(Debug, Copy, Clone)]
46 pub enum DeviceError {
47     DriverExists,      // 设备已存在
48     DeviceExists,      // 驱动已存在
49     InitializeFailed,  // 初始化错误
50     NoDeviceForDriver, // 没有合适的设备匹配驱动
51     NoDriverForDevice, // 没有合适的驱动匹配设备
52 }
53 
54 /// @brief: 将u32类型转换为设备状态类型
55 impl From<u32> for DeviceState {
56     fn from(state: u32) -> Self {
57         match state {
58             0 => DeviceState::NotInitialized,
59             1 => DeviceState::Initialized,
60             _ => todo!(),
61         }
62     }
63 }
64 
65 /// @brief: 将设备状态转换为u32类型
66 impl From<DeviceState> for u32 {
67     fn from(state: DeviceState) -> Self {
68         match state {
69             DeviceState::NotInitialized => 0,
70             DeviceState::Initialized => 1,
71             DeviceState::UnDefined => 2,
72         }
73     }
74 }
75 
76 /// @brief: 所有设备都应该实现该trait
77 pub trait Device: Any + Send + Sync + Debug {
78     /// @brief: 获取设备类型
79     /// @parameter: None
80     /// @return: 实现该trait的设备所属类型
81     fn get_type(&self) -> DeviceType;
82 
83     /// @brief: 获取设备标识
84     /// @parameter: None
85     /// @return: 该设备唯一标识
86     fn get_id_table(&self) -> IdTable;
87 }
88