1 use super::{LockedSysFSInode, SYS_BUS_INODE};
2 use crate::{filesystem::vfs::IndexNode, kdebug, syscall::SystemError};
3 use alloc::sync::Arc;
4
5 /// @brief: 注册bus,在sys/bus下生成文件夹
6 /// @parameter bus_name: 总线文件夹名
7 /// @return: 操作成功,返回inode,操作失败,返回错误码
8 #[inline]
9 #[allow(dead_code)]
sys_bus_register(bus_name: &str) -> Result<Arc<dyn IndexNode>, SystemError>10 pub fn sys_bus_register(bus_name: &str) -> Result<Arc<dyn IndexNode>, SystemError> {
11 let binding: Arc<dyn IndexNode> = SYS_BUS_INODE();
12 kdebug!("Before bus_register: ls /sys/bus/: {:?}", binding.list());
13 binding
14 .as_any_ref()
15 .downcast_ref::<LockedSysFSInode>()
16 .ok_or(SystemError::E2BIG)
17 .unwrap()
18 .add_dir(bus_name)
19 }
20
21 /// @brief: 注销bus,在sys/bus删除文件夹
22 /// @parameter bus_name: 总线文件夹名
23 /// @return: 操作成功,返回(),操作失败,返回错误码
24 #[allow(dead_code)]
sys_bus_unregister(bus_name: &str) -> Result<(), SystemError>25 pub fn sys_bus_unregister(bus_name: &str) -> Result<(), SystemError> {
26 let binding: Arc<dyn IndexNode> = SYS_BUS_INODE();
27 binding
28 .as_any_ref()
29 .downcast_ref::<LockedSysFSInode>()
30 .ok_or(SystemError::E2BIG)
31 .unwrap()
32 .remove(bus_name)
33 }
34
35 /// @brief: 在相应总线文件夹下生成devices和drivers文件夹
36 /// @parameter inode: 总线文件夹inode
37 /// @return: 操作成功,返回devices inode和drivers inode,操作失败,返回错误码
sys_bus_init( inode: &Arc<dyn IndexNode>, ) -> Result<(Arc<dyn IndexNode>, Arc<dyn IndexNode>), SystemError>38 pub fn sys_bus_init(
39 inode: &Arc<dyn IndexNode>,
40 ) -> Result<(Arc<dyn IndexNode>, Arc<dyn IndexNode>), SystemError> {
41 match inode.as_any_ref().downcast_ref::<LockedSysFSInode>() {
42 Some(lock_bus) => match lock_bus.add_dir("devices") {
43 Ok(devices) => match lock_bus.add_dir("drivers") {
44 Ok(drivers) => Ok((devices, drivers)),
45 Err(err) => Err(err),
46 },
47 Err(err) => Err(err),
48 },
49 None => Err(SystemError::E2BIG),
50 }
51 }
52
53 /// @brief: 在相应总线的device下生成设备文件夹
54 /// @parameter bus_name: 总线名
55 /// name: 设备名
56 /// @return: 操作成功,返回device inode,操作失败,返回错误码
bus_driver_register(bus_name: &str, name: &str) -> Result<Arc<dyn IndexNode>, SystemError>57 pub fn bus_driver_register(bus_name: &str, name: &str) -> Result<Arc<dyn IndexNode>, SystemError> {
58 match SYS_BUS_INODE().find(bus_name) {
59 Ok(platform) => match platform.find("drivers") {
60 Ok(device) => device
61 .as_any_ref()
62 .downcast_ref::<LockedSysFSInode>()
63 .ok_or(SystemError::E2BIG)
64 .unwrap()
65 .add_dir(name),
66 Err(_) => return Err(SystemError::EXDEV),
67 },
68 Err(_) => return Err(SystemError::EXDEV),
69 }
70 }
71
72 /// @brief: 在相应总线的driver下生成驱动文件夹
73 /// @parameter bus_name: 总线名
74 /// name: 驱动名
75 /// @return: 操作成功,返回drivers inode,操作失败,返回错误码
bus_device_register(bus_name: &str, name: &str) -> Result<Arc<dyn IndexNode>, SystemError>76 pub fn bus_device_register(bus_name: &str, name: &str) -> Result<Arc<dyn IndexNode>, SystemError> {
77 match SYS_BUS_INODE().find(bus_name) {
78 Ok(platform) => match platform.find("devices") {
79 Ok(device) => device
80 .as_any_ref()
81 .downcast_ref::<LockedSysFSInode>()
82 .ok_or(SystemError::E2BIG)
83 .unwrap()
84 .add_dir(name),
85 Err(_) => return Err(SystemError::EXDEV),
86 },
87 Err(_) => return Err(SystemError::EXDEV),
88 }
89 }
90