106d5e247SLoGin use alloc::{ 206d5e247SLoGin string::{String, ToString}, 306d5e247SLoGin sync::{Arc, Weak}, 406d5e247SLoGin }; 5a03c4f9dSLoGin use intertrait::cast::CastArc; 62eab6dd7S曾俊 use log::error; 706d5e247SLoGin 8*28fe4ad2S黄铭涛 use super::{ 9*28fe4ad2S黄铭涛 platform_bus_device, platform_device::PlatformDevice, platform_driver::PlatformDriver, 10*28fe4ad2S黄铭涛 }; 1106d5e247SLoGin use crate::{ 12a03c4f9dSLoGin driver::{ 13a03c4f9dSLoGin acpi::acpi_manager, 14a03c4f9dSLoGin base::{ 15a03c4f9dSLoGin device::{bus::Bus, driver::Driver, Device}, 16a03c4f9dSLoGin kobject::KObject, 17a03c4f9dSLoGin subsys::SubSysPrivate, 18a03c4f9dSLoGin }, 19a03c4f9dSLoGin }, 2006d5e247SLoGin filesystem::{ 2106d5e247SLoGin sysfs::{Attribute, AttributeGroup}, 2206d5e247SLoGin vfs::syscall::ModeType, 2306d5e247SLoGin }, 2406d5e247SLoGin }; 2591e9d4abSLoGin use system_error::SystemError; 26a03c4f9dSLoGin 2706d5e247SLoGin #[derive(Debug)] 2806d5e247SLoGin pub struct PlatformBus { 2906d5e247SLoGin private: SubSysPrivate, 3006d5e247SLoGin } 3106d5e247SLoGin 3206d5e247SLoGin impl PlatformBus { new() -> Arc<Self>3306d5e247SLoGin pub fn new() -> Arc<Self> { 3406d5e247SLoGin let w: Weak<Self> = Weak::new(); 3508a2ee40SLoGin let private = SubSysPrivate::new("platform".to_string(), Some(w), None, &[]); 3606d5e247SLoGin let bus = Arc::new(Self { private }); 3706d5e247SLoGin bus.subsystem() 3808a2ee40SLoGin .set_bus(Some(Arc::downgrade(&(bus.clone() as Arc<dyn Bus>)))); 3906d5e247SLoGin 4006d5e247SLoGin return bus; 4106d5e247SLoGin } 4206d5e247SLoGin } 4306d5e247SLoGin 4406d5e247SLoGin impl Bus for PlatformBus { name(&self) -> String4506d5e247SLoGin fn name(&self) -> String { 4606d5e247SLoGin return "platform".to_string(); 4706d5e247SLoGin } 4806d5e247SLoGin dev_name(&self) -> String4906d5e247SLoGin fn dev_name(&self) -> String { 5006d5e247SLoGin return self.name(); 5106d5e247SLoGin } 5206d5e247SLoGin dev_groups(&self) -> &'static [&'static dyn AttributeGroup]5306d5e247SLoGin fn dev_groups(&self) -> &'static [&'static dyn AttributeGroup] { 5406d5e247SLoGin return &[&PlatformDeviceAttrGroup]; 5506d5e247SLoGin } 5606d5e247SLoGin subsystem(&self) -> &SubSysPrivate5706d5e247SLoGin fn subsystem(&self) -> &SubSysPrivate { 5806d5e247SLoGin return &self.private; 5906d5e247SLoGin } 60a03c4f9dSLoGin probe(&self, device: &Arc<dyn Device>) -> Result<(), SystemError>61a03c4f9dSLoGin fn probe(&self, device: &Arc<dyn Device>) -> Result<(), SystemError> { 62a03c4f9dSLoGin let drv = device.driver().ok_or(SystemError::EINVAL)?; 63a03c4f9dSLoGin let pdrv = drv.cast::<dyn PlatformDriver>().map_err(|_|{ 642eab6dd7S曾俊 error!("PlatformBus::probe() failed: device.driver() is not a PlatformDriver. Device: '{:?}'", device.name()); 65a03c4f9dSLoGin SystemError::EINVAL 66a03c4f9dSLoGin })?; 67a03c4f9dSLoGin 68a03c4f9dSLoGin let pdev = device.clone().cast::<dyn PlatformDevice>().map_err(|_| { 692eab6dd7S曾俊 error!( 70a03c4f9dSLoGin "PlatformBus::probe() failed: device is not a PlatformDevice. Device: '{:?}'", 71a03c4f9dSLoGin device.name() 72a03c4f9dSLoGin ); 73a03c4f9dSLoGin SystemError::EINVAL 74a03c4f9dSLoGin })?; 75a03c4f9dSLoGin 76a03c4f9dSLoGin return pdrv.probe(&pdev); 77a03c4f9dSLoGin } 78a03c4f9dSLoGin remove(&self, _device: &Arc<dyn Device>) -> Result<(), SystemError>79a03c4f9dSLoGin fn remove(&self, _device: &Arc<dyn Device>) -> Result<(), SystemError> { 80a03c4f9dSLoGin todo!() 81a03c4f9dSLoGin } 82a03c4f9dSLoGin sync_state(&self, _device: &Arc<dyn Device>)83a03c4f9dSLoGin fn sync_state(&self, _device: &Arc<dyn Device>) { 84a03c4f9dSLoGin todo!() 85a03c4f9dSLoGin } 86a03c4f9dSLoGin shutdown(&self, _device: &Arc<dyn Device>)87a03c4f9dSLoGin fn shutdown(&self, _device: &Arc<dyn Device>) { 88a03c4f9dSLoGin todo!() 89a03c4f9dSLoGin } 90a03c4f9dSLoGin resume(&self, _device: &Arc<dyn Device>) -> Result<(), SystemError>91a03c4f9dSLoGin fn resume(&self, _device: &Arc<dyn Device>) -> Result<(), SystemError> { 92a03c4f9dSLoGin todo!() 93a03c4f9dSLoGin } 94a03c4f9dSLoGin 95a03c4f9dSLoGin /// 96a03c4f9dSLoGin /// match platform device to platform driver. 97a03c4f9dSLoGin /// 98a03c4f9dSLoGin /// ## 参数 99a03c4f9dSLoGin /// 100a03c4f9dSLoGin /// * `device` - platform device 101a03c4f9dSLoGin /// * `driver` - platform driver 102a03c4f9dSLoGin /// 103a03c4f9dSLoGin /// ## 返回 104a03c4f9dSLoGin /// 105a03c4f9dSLoGin /// - `Ok(true)` - 匹配成功 106a03c4f9dSLoGin /// - `Ok(false)` - 匹配失败 107a03c4f9dSLoGin /// - `Err(_)` - 由于内部错误导致匹配失败 108a03c4f9dSLoGin /// 109a03c4f9dSLoGin /// Platform device IDs are assumed to be encoded like this: 110a03c4f9dSLoGin /// "<name><instance>", where <name> is a short description of the type of 111a03c4f9dSLoGin /// device, like "pci" or "floppy", and <instance> is the enumerated 112a03c4f9dSLoGin /// instance of the device, like '0' or '42'. Driver IDs are simply 113a03c4f9dSLoGin /// "<name>". So, extract the <name> from the platform_device structure, 114a03c4f9dSLoGin /// and compare it against the name of the driver. Return whether they match 115a03c4f9dSLoGin /// or not. 116a03c4f9dSLoGin /// 117e7071df6SLoGin /// 参考 https://code.dragonos.org.cn/xref/linux-6.1.9/drivers/base/platform.c#1331 118a03c4f9dSLoGin /// 119a03c4f9dSLoGin /// match_device( &self, device: &Arc<dyn Device>, driver: &Arc<dyn Driver>, ) -> Result<bool, SystemError>120a03c4f9dSLoGin fn match_device( 121a03c4f9dSLoGin &self, 122a03c4f9dSLoGin device: &Arc<dyn Device>, 123a03c4f9dSLoGin driver: &Arc<dyn Driver>, 124a03c4f9dSLoGin ) -> Result<bool, SystemError> { 125a03c4f9dSLoGin // 尝试从 ACPI 中匹配 126a03c4f9dSLoGin if let Ok(x) = acpi_manager().driver_match_device(driver, device) { 127a03c4f9dSLoGin if x { 128a03c4f9dSLoGin return Ok(true); 129a03c4f9dSLoGin } 130a03c4f9dSLoGin } 131a03c4f9dSLoGin 132a03c4f9dSLoGin // 尝试从 ID table 中匹配 133a03c4f9dSLoGin if let Some(drv_id_table) = driver.id_table() { 134a03c4f9dSLoGin let pdev = device 135a03c4f9dSLoGin .clone() 136a03c4f9dSLoGin .cast::<dyn PlatformDevice>() 137a03c4f9dSLoGin .map_err(|_| SystemError::EINVAL)?; 138a03c4f9dSLoGin if drv_id_table.name().eq(&pdev.name()) { 139a03c4f9dSLoGin return Ok(true); 140a03c4f9dSLoGin } 141a03c4f9dSLoGin } 142a03c4f9dSLoGin 143a03c4f9dSLoGin // 尝试根据设备名称匹配 144a03c4f9dSLoGin return Ok(device.name().eq(&driver.name())); 145a03c4f9dSLoGin } 146*28fe4ad2S黄铭涛 root_device(&self) -> Option<Weak<dyn Device>>147*28fe4ad2S黄铭涛 fn root_device(&self) -> Option<Weak<dyn Device>> { 148*28fe4ad2S黄铭涛 let root_device = platform_bus_device() as Arc<dyn Device>; 149*28fe4ad2S黄铭涛 return Some(Arc::downgrade(&root_device)); 150*28fe4ad2S黄铭涛 } 15106d5e247SLoGin } 15206d5e247SLoGin 15306d5e247SLoGin #[derive(Debug)] 15406d5e247SLoGin pub struct PlatformDeviceAttrGroup; 15506d5e247SLoGin 15606d5e247SLoGin impl AttributeGroup for PlatformDeviceAttrGroup { name(&self) -> Option<&str>15706d5e247SLoGin fn name(&self) -> Option<&str> { 15806d5e247SLoGin None 15906d5e247SLoGin } 16006d5e247SLoGin attrs(&self) -> &[&'static dyn Attribute]16106d5e247SLoGin fn attrs(&self) -> &[&'static dyn Attribute] { 162e7071df6SLoGin // todo: https://code.dragonos.org.cn/xref/linux-6.1.9/drivers/base/platform.c?r=&mo=38425&fi=1511#1311 16306d5e247SLoGin return &[]; 16406d5e247SLoGin } 16506d5e247SLoGin is_visible(&self, _kobj: Arc<dyn KObject>, attr: &dyn Attribute) -> Option<ModeType>16606d5e247SLoGin fn is_visible(&self, _kobj: Arc<dyn KObject>, attr: &dyn Attribute) -> Option<ModeType> { 16706d5e247SLoGin return Some(attr.mode()); 16806d5e247SLoGin } 16906d5e247SLoGin } 170