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