xref: /DragonOS/kernel/src/driver/base/device/init.rs (revision a03c4f9dee5705207325c56629c0ccd219168f10)
1 use alloc::{string::ToString, sync::Arc};
2 
3 use crate::{
4     driver::base::{
5         device::{
6             sys_dev_kset, DeviceManager, DEVICES_KSET_INSTANCE, DEVICE_MANAGER, DEV_KSET_INSTANCE,
7         },
8         kobject::KObject,
9         kset::KSet,
10     },
11     kdebug, kinfo,
12     syscall::SystemError,
13 };
14 
15 pub fn devices_init() -> Result<(), SystemError> {
16     // 创建 `/sys/devices` 目录
17     {
18         let devices_kset = KSet::new("devices".to_string());
19         devices_kset
20             .register(None)
21             .expect("register devices kset failed");
22 
23         unsafe {
24             DEVICES_KSET_INSTANCE = Some(devices_kset);
25             // 初始化全局设备管理器
26             DEVICE_MANAGER = Some(DeviceManager::new());
27         }
28     }
29 
30     // 创建 `/sys/dev` 目录
31     {
32         let dev_kset = KSet::new("dev".to_string());
33         dev_kset.register(None).expect("register dev kset failed");
34         unsafe {
35             DEV_KSET_INSTANCE = Some(dev_kset);
36         }
37     }
38 
39     // 创建 `/sys/dev/block` 目录
40     {
41         kdebug!("create /sys/dev/block");
42         let dev_kset = sys_dev_kset();
43         let dev_block_kset = KSet::new("block".to_string());
44         let parent = dev_kset.clone() as Arc<dyn KObject>;
45         dev_block_kset.set_parent(Some(Arc::downgrade(&parent)));
46 
47         dev_block_kset
48             .register(Some(dev_kset))
49             .expect("register dev block kset failed");
50     }
51 
52     // 创建 `/sys/dev/char` 目录
53     {
54         kdebug!("create /sys/dev/char");
55         let dev_kset = sys_dev_kset();
56         let dev_char_kset = KSet::new("char".to_string());
57         let parent = dev_kset.clone() as Arc<dyn KObject>;
58         dev_char_kset.set_parent(Some(Arc::downgrade(&parent)));
59 
60         dev_char_kset
61             .register(Some(dev_kset))
62             .expect("register dev char kset failed");
63     }
64 
65     kinfo!("devices init success");
66 
67     return Ok(());
68 }
69