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