1 use alloc::{ 2 string::{String, ToString}, 3 sync::Arc, 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 bus = Arc::new(Self { 57 private: SubSysPrivate::new("acpi".to_string(), None, None, &[]), 58 }); 59 bus.subsystem() 60 .set_bus(Some(Arc::downgrade(&(bus.clone() as Arc<dyn Bus>)))); 61 return bus; 62 } 63 } 64 65 impl Bus for AcpiBus { 66 fn name(&self) -> String { 67 return self.private.subsys().as_kobject().name(); 68 } 69 70 fn dev_name(&self) -> String { 71 self.name() 72 } 73 74 /// 参考 https://opengrok.ringotek.cn/xref/linux-6.1.9/drivers/acpi/bus.c#1056 75 fn remove(&self, _device: &Arc<dyn Device>) -> Result<(), SystemError> { 76 todo!("acpi_bus: remove") 77 } 78 79 fn shutdown(&self, _device: &Arc<dyn Device>) { 80 return; 81 } 82 83 fn resume(&self, _device: &Arc<dyn Device>) -> Result<(), SystemError> { 84 return Ok(()); 85 } 86 87 /// 通过acpi来匹配驱动 88 /// 89 /// 参考 https://opengrok.ringotek.cn/xref/linux-6.1.9/drivers/acpi/bus.c#1005 90 fn match_device( 91 &self, 92 _device: &Arc<dyn Device>, 93 _driver: &Arc<dyn Driver>, 94 ) -> Result<bool, SystemError> { 95 // todo: 通过acpi来匹配驱动 96 return Ok(false); 97 } 98 99 /// 参考 https://opengrok.ringotek.cn/xref/linux-6.1.9/drivers/acpi/bus.c#1019 100 fn probe(&self, _device: &Arc<dyn Device>) -> Result<(), SystemError> { 101 todo!("acpi_bus: probe") 102 } 103 104 fn subsystem(&self) -> &SubSysPrivate { 105 return &self.private; 106 } 107 } 108 109 /// Acpi设备应当实现的trait 110 /// 111 /// 所有的实现了 AcpiDevice trait的结构体,都应该在结构体上方标注`#[cast_to([sync] AcpiDevice)] 112 /// 113 /// todo: 仿照linux的acpi_device去设计这个trait 114 /// 115 /// 116 /// 参考 https://opengrok.ringotek.cn/xref/linux-6.1.9/include/acpi/acpi_bus.h#364 117 pub trait AcpiDevice: Device {} 118 119 /// Acpi驱动应当实现的trait 120 /// 121 /// 所有的实现了 AcpiDriver trait的结构体,都应该在结构体上方标注`#[cast_to([sync] AcpiDriver)] 122 /// 123 /// todo: 仿照linux的acpi_driver去设计这个trait 124 /// 125 /// 参考 https://opengrok.ringotek.cn/xref/linux-6.1.9/include/acpi/acpi_bus.h#163 126 pub trait AcpiDriver: Driver {} 127