1 use alloc::{string::ToString, sync::Arc}; 2 use log::info; 3 4 use crate::driver::base::{ 5 device::{ 6 set_sys_dev_block_kset, set_sys_dev_char_kset, set_sys_devices_virtual_kset, sys_dev_kset, 7 sys_devices_kset, DeviceManager, DEVICES_KSET_INSTANCE, DEVICE_MANAGER, DEV_KSET_INSTANCE, 8 }, 9 kobject::KObject, 10 kset::KSet, 11 }; 12 13 use system_error::SystemError; 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/devices/virtual` 目录 31 { 32 let devices_kset = sys_devices_kset(); 33 let virtual_kset = KSet::new("virtual".to_string()); 34 let parent = devices_kset.clone() as Arc<dyn KObject>; 35 virtual_kset.set_parent(Some(Arc::downgrade(&parent))); 36 37 virtual_kset 38 .register(Some(devices_kset)) 39 .expect("register virtual kset failed"); 40 unsafe { set_sys_devices_virtual_kset(virtual_kset) }; 41 } 42 43 // 创建 `/sys/dev` 目录 44 { 45 let dev_kset = KSet::new("dev".to_string()); 46 dev_kset.register(None).expect("register dev kset failed"); 47 unsafe { 48 DEV_KSET_INSTANCE = Some(dev_kset); 49 } 50 } 51 52 // 创建 `/sys/dev/block` 目录 53 { 54 // debug!("create /sys/dev/block"); 55 let dev_kset = sys_dev_kset(); 56 let dev_block_kset = KSet::new("block".to_string()); 57 let parent = dev_kset.clone() as Arc<dyn KObject>; 58 dev_block_kset.set_parent(Some(Arc::downgrade(&parent))); 59 60 dev_block_kset 61 .register(Some(dev_kset)) 62 .expect("register dev block kset failed"); 63 64 unsafe { set_sys_dev_block_kset(dev_block_kset) }; 65 } 66 67 // 创建 `/sys/dev/char` 目录 68 { 69 // debug!("create /sys/dev/char"); 70 let dev_kset = sys_dev_kset(); 71 let dev_char_kset = KSet::new("char".to_string()); 72 let parent = dev_kset.clone() as Arc<dyn KObject>; 73 dev_char_kset.set_parent(Some(Arc::downgrade(&parent))); 74 75 dev_char_kset 76 .register(Some(dev_kset)) 77 .expect("register dev char kset failed"); 78 79 unsafe { set_sys_dev_char_kset(dev_char_kset) }; 80 } 81 82 info!("devices init success"); 83 84 return Ok(()); 85 } 86