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