xref: /DragonOS/kernel/src/driver/acpi/bus.rs (revision bd70d2d1f490aabd570a5301b858bd5eb04149fa)
1 use crate::driver::base::{
2     device::{
3         bus::{bus_manager, Bus},
4         driver::Driver,
5         Device,
6     },
7     subsys::SubSysPrivate,
8 };
9 use alloc::{
10     string::{String, ToString},
11     sync::Arc,
12 };
13 use system_error::SystemError;
14 
15 use super::AcpiManager;
16 
17 impl AcpiManager {
18     /// 通过acpi来匹配驱动
19     ///
20     /// 参考 https://code.dragonos.org.cn/xref/linux-6.1.9/drivers/acpi/bus.c#949
driver_match_device( &self, _driver: &Arc<dyn Driver>, _device: &Arc<dyn Device>, ) -> Result<bool, SystemError>21     pub fn driver_match_device(
22         &self,
23         _driver: &Arc<dyn Driver>,
24         _device: &Arc<dyn Device>,
25     ) -> Result<bool, SystemError> {
26         // todo:
27 
28         return Ok(false);
29     }
30 
31     /// 参考 https://code.dragonos.org.cn/xref/linux-6.1.9/drivers/acpi/bus.c#1286
bus_init(&self) -> Result<(), SystemError>32     pub(super) fn bus_init(&self) -> Result<(), SystemError> {
33         self.acpi_sysfs_init()?;
34 
35         let acpi_bus = AcpiBus::new();
36         bus_manager()
37             .register(acpi_bus as Arc<dyn Bus>)
38             .expect("acpi_bus register failed");
39         return Ok(());
40     }
41 }
42 
43 /// ACPI总线
44 ///
45 /// 参考 https://code.dragonos.org.cn/xref/linux-6.1.9/drivers/acpi/bus.c#1072
46 #[derive(Debug)]
47 pub(super) struct AcpiBus {
48     private: SubSysPrivate,
49 }
50 
51 impl AcpiBus {
new() -> Arc<Self>52     pub fn new() -> Arc<Self> {
53         let bus = Arc::new(Self {
54             private: SubSysPrivate::new("acpi".to_string(), None, None, &[]),
55         });
56         bus.subsystem()
57             .set_bus(Some(Arc::downgrade(&(bus.clone() as Arc<dyn Bus>))));
58         return bus;
59     }
60 }
61 
62 impl Bus for AcpiBus {
name(&self) -> String63     fn name(&self) -> String {
64         return self.private.subsys().as_kobject().name();
65     }
66 
dev_name(&self) -> String67     fn dev_name(&self) -> String {
68         self.name()
69     }
70 
71     /// 参考 https://code.dragonos.org.cn/xref/linux-6.1.9/drivers/acpi/bus.c#1056
remove(&self, _device: &Arc<dyn Device>) -> Result<(), SystemError>72     fn remove(&self, _device: &Arc<dyn Device>) -> Result<(), SystemError> {
73         todo!("acpi_bus: remove")
74     }
75 
shutdown(&self, _device: &Arc<dyn Device>)76     fn shutdown(&self, _device: &Arc<dyn Device>) {
77         return;
78     }
79 
resume(&self, _device: &Arc<dyn Device>) -> Result<(), SystemError>80     fn resume(&self, _device: &Arc<dyn Device>) -> Result<(), SystemError> {
81         return Ok(());
82     }
83 
84     /// 通过acpi来匹配驱动
85     ///
86     /// 参考 https://code.dragonos.org.cn/xref/linux-6.1.9/drivers/acpi/bus.c#1005
match_device( &self, _device: &Arc<dyn Device>, _driver: &Arc<dyn Driver>, ) -> Result<bool, SystemError>87     fn match_device(
88         &self,
89         _device: &Arc<dyn Device>,
90         _driver: &Arc<dyn Driver>,
91     ) -> Result<bool, SystemError> {
92         // todo: 通过acpi来匹配驱动
93         return Ok(false);
94     }
95 
96     /// 参考 https://code.dragonos.org.cn/xref/linux-6.1.9/drivers/acpi/bus.c#1019
probe(&self, _device: &Arc<dyn Device>) -> Result<(), SystemError>97     fn probe(&self, _device: &Arc<dyn Device>) -> Result<(), SystemError> {
98         todo!("acpi_bus: probe")
99     }
100 
subsystem(&self) -> &SubSysPrivate101     fn subsystem(&self) -> &SubSysPrivate {
102         return &self.private;
103     }
104 }
105 
106 /// Acpi设备应当实现的trait
107 ///
108 /// 所有的实现了 AcpiDevice trait的结构体,都应该在结构体上方标注`#[cast_to([sync] AcpiDevice)]
109 ///
110 /// todo: 仿照linux的acpi_device去设计这个trait
111 ///
112 ///
113 /// 参考 https://code.dragonos.org.cn/xref/linux-6.1.9/include/acpi/acpi_bus.h#364
114 #[allow(unused)]
115 pub trait AcpiDevice: Device {}
116 
117 /// Acpi驱动应当实现的trait
118 ///
119 /// 所有的实现了 AcpiDriver trait的结构体,都应该在结构体上方标注`#[cast_to([sync] AcpiDriver)]
120 ///
121 /// todo: 仿照linux的acpi_driver去设计这个trait
122 ///
123 /// 参考 https://code.dragonos.org.cn/xref/linux-6.1.9/include/acpi/acpi_bus.h#163
124 #[allow(unused)]
125 pub trait AcpiDriver: Driver {}
126