1*91e9d4abSLoGin use crate::driver::base::{ 27eda31b2SLoGin device::{ 37eda31b2SLoGin bus::{bus_manager, Bus}, 47eda31b2SLoGin driver::Driver, 57eda31b2SLoGin Device, 67eda31b2SLoGin }, 77eda31b2SLoGin subsys::SubSysPrivate, 8a03c4f9dSLoGin }; 9*91e9d4abSLoGin use alloc::{ 10*91e9d4abSLoGin string::{String, ToString}, 11*91e9d4abSLoGin sync::Arc, 12*91e9d4abSLoGin }; 13*91e9d4abSLoGin use system_error::SystemError; 14a03c4f9dSLoGin 15a03c4f9dSLoGin use super::AcpiManager; 16a03c4f9dSLoGin 17a03c4f9dSLoGin impl AcpiManager { 18a03c4f9dSLoGin /// 通过acpi来匹配驱动 19a03c4f9dSLoGin /// 20a03c4f9dSLoGin /// 参考 https://opengrok.ringotek.cn/xref/linux-6.1.9/drivers/acpi/bus.c#949 21a03c4f9dSLoGin pub fn driver_match_device( 22a03c4f9dSLoGin &self, 23a03c4f9dSLoGin _driver: &Arc<dyn Driver>, 24a03c4f9dSLoGin _device: &Arc<dyn Device>, 25a03c4f9dSLoGin ) -> Result<bool, SystemError> { 26a03c4f9dSLoGin // todo: 27a03c4f9dSLoGin 28a03c4f9dSLoGin return Ok(false); 29a03c4f9dSLoGin } 307eda31b2SLoGin 317eda31b2SLoGin /// 参考 https://opengrok.ringotek.cn/xref/linux-6.1.9/drivers/acpi/bus.c#1286 327eda31b2SLoGin pub(super) fn bus_init(&self) -> Result<(), SystemError> { 337eda31b2SLoGin self.acpi_sysfs_init()?; 347eda31b2SLoGin 357eda31b2SLoGin let acpi_bus = AcpiBus::new(); 367eda31b2SLoGin bus_manager() 377eda31b2SLoGin .register(acpi_bus as Arc<dyn Bus>) 387eda31b2SLoGin .expect("acpi_bus register failed"); 397eda31b2SLoGin return Ok(()); 40a03c4f9dSLoGin } 417eda31b2SLoGin } 427eda31b2SLoGin 437eda31b2SLoGin /// ACPI总线 447eda31b2SLoGin /// 457eda31b2SLoGin /// 参考 https://opengrok.ringotek.cn/xref/linux-6.1.9/drivers/acpi/bus.c#1072 467eda31b2SLoGin #[derive(Debug)] 477eda31b2SLoGin pub(super) struct AcpiBus { 487eda31b2SLoGin private: SubSysPrivate, 497eda31b2SLoGin } 507eda31b2SLoGin 517eda31b2SLoGin impl AcpiBus { 527eda31b2SLoGin pub fn new() -> Arc<Self> { 537eda31b2SLoGin let bus = Arc::new(Self { 5408a2ee40SLoGin private: SubSysPrivate::new("acpi".to_string(), None, None, &[]), 557eda31b2SLoGin }); 567eda31b2SLoGin bus.subsystem() 5708a2ee40SLoGin .set_bus(Some(Arc::downgrade(&(bus.clone() as Arc<dyn Bus>)))); 587eda31b2SLoGin return bus; 597eda31b2SLoGin } 607eda31b2SLoGin } 617eda31b2SLoGin 627eda31b2SLoGin impl Bus for AcpiBus { 637eda31b2SLoGin fn name(&self) -> String { 647eda31b2SLoGin return self.private.subsys().as_kobject().name(); 657eda31b2SLoGin } 667eda31b2SLoGin 677eda31b2SLoGin fn dev_name(&self) -> String { 687eda31b2SLoGin self.name() 697eda31b2SLoGin } 707eda31b2SLoGin 717eda31b2SLoGin /// 参考 https://opengrok.ringotek.cn/xref/linux-6.1.9/drivers/acpi/bus.c#1056 727eda31b2SLoGin fn remove(&self, _device: &Arc<dyn Device>) -> Result<(), SystemError> { 737eda31b2SLoGin todo!("acpi_bus: remove") 747eda31b2SLoGin } 757eda31b2SLoGin 767eda31b2SLoGin fn shutdown(&self, _device: &Arc<dyn Device>) { 777eda31b2SLoGin return; 787eda31b2SLoGin } 797eda31b2SLoGin 807eda31b2SLoGin fn resume(&self, _device: &Arc<dyn Device>) -> Result<(), SystemError> { 817eda31b2SLoGin return Ok(()); 827eda31b2SLoGin } 837eda31b2SLoGin 847eda31b2SLoGin /// 通过acpi来匹配驱动 857eda31b2SLoGin /// 867eda31b2SLoGin /// 参考 https://opengrok.ringotek.cn/xref/linux-6.1.9/drivers/acpi/bus.c#1005 877eda31b2SLoGin fn match_device( 887eda31b2SLoGin &self, 897eda31b2SLoGin _device: &Arc<dyn Device>, 907eda31b2SLoGin _driver: &Arc<dyn Driver>, 917eda31b2SLoGin ) -> Result<bool, SystemError> { 927eda31b2SLoGin // todo: 通过acpi来匹配驱动 937eda31b2SLoGin return Ok(false); 947eda31b2SLoGin } 957eda31b2SLoGin 967eda31b2SLoGin /// 参考 https://opengrok.ringotek.cn/xref/linux-6.1.9/drivers/acpi/bus.c#1019 977eda31b2SLoGin fn probe(&self, _device: &Arc<dyn Device>) -> Result<(), SystemError> { 987eda31b2SLoGin todo!("acpi_bus: probe") 997eda31b2SLoGin } 1007eda31b2SLoGin 1017eda31b2SLoGin fn subsystem(&self) -> &SubSysPrivate { 1027eda31b2SLoGin return &self.private; 1037eda31b2SLoGin } 1047eda31b2SLoGin } 1057eda31b2SLoGin 1067eda31b2SLoGin /// Acpi设备应当实现的trait 1077eda31b2SLoGin /// 1087eda31b2SLoGin /// 所有的实现了 AcpiDevice trait的结构体,都应该在结构体上方标注`#[cast_to([sync] AcpiDevice)] 1097eda31b2SLoGin /// 1107eda31b2SLoGin /// todo: 仿照linux的acpi_device去设计这个trait 1117eda31b2SLoGin /// 1127eda31b2SLoGin /// 1137eda31b2SLoGin /// 参考 https://opengrok.ringotek.cn/xref/linux-6.1.9/include/acpi/acpi_bus.h#364 1147eda31b2SLoGin pub trait AcpiDevice: Device {} 1157eda31b2SLoGin 1167eda31b2SLoGin /// Acpi驱动应当实现的trait 1177eda31b2SLoGin /// 1187eda31b2SLoGin /// 所有的实现了 AcpiDriver trait的结构体,都应该在结构体上方标注`#[cast_to([sync] AcpiDriver)] 1197eda31b2SLoGin /// 1207eda31b2SLoGin /// todo: 仿照linux的acpi_driver去设计这个trait 1217eda31b2SLoGin /// 1227eda31b2SLoGin /// 参考 https://opengrok.ringotek.cn/xref/linux-6.1.9/include/acpi/acpi_bus.h#163 1237eda31b2SLoGin pub trait AcpiDriver: Driver {} 124