1 use alloc::{ 2 string::{String, ToString}, 3 sync::{Arc, Weak}, 4 }; 5 use intertrait::cast::CastArc; 6 use system_error::SystemError; 7 8 use crate::{ 9 driver::{ 10 acpi::acpi_manager, 11 base::{ 12 device::{bus::Bus, driver::Driver, Device}, 13 kobject::KObject, 14 subsys::SubSysPrivate, 15 }, 16 }, 17 filesystem::{ 18 sysfs::{Attribute, AttributeGroup}, 19 vfs::syscall::ModeType, 20 }, 21 }; 22 23 use super::{serio_device::SerioDevice, serio_driver::SerioDriver}; 24 25 #[derive(Debug)] 26 pub struct SerioBus { 27 private: SubSysPrivate, 28 } 29 30 impl SerioBus { new() -> Arc<Self>31 pub fn new() -> Arc<Self> { 32 let w: Weak<Self> = Weak::new(); 33 let private = SubSysPrivate::new("serio".to_string(), Some(w), None, &[]); 34 let bus = Arc::new(Self { private }); 35 bus.subsystem() 36 .set_bus(Some(Arc::downgrade(&(bus.clone() as Arc<dyn Bus>)))); 37 38 return bus; 39 } 40 } 41 42 impl Bus for SerioBus { name(&self) -> String43 fn name(&self) -> String { 44 return "serio".to_string(); 45 } 46 dev_name(&self) -> String47 fn dev_name(&self) -> String { 48 return self.name(); 49 } 50 dev_groups(&self) -> &'static [&'static dyn AttributeGroup]51 fn dev_groups(&self) -> &'static [&'static dyn AttributeGroup] { 52 return &[&SerioDeviceAttrGroup]; 53 } 54 subsystem(&self) -> &SubSysPrivate55 fn subsystem(&self) -> &SubSysPrivate { 56 return &self.private; 57 } 58 probe(&self, device: &Arc<dyn Device>) -> Result<(), SystemError>59 fn probe(&self, device: &Arc<dyn Device>) -> Result<(), SystemError> { 60 let drv = device.driver().ok_or(SystemError::EINVAL)?; 61 let pdrv = drv.cast::<dyn SerioDriver>().map_err(|_| { 62 kerror!( 63 "SerioBus::probe() failed: device.driver() is not a SerioDriver. Device: '{:?}'", 64 device.name() 65 ); 66 SystemError::EINVAL 67 })?; 68 69 let pdev = device.clone().cast::<dyn SerioDevice>().map_err(|_| { 70 kerror!( 71 "SerioBus::probe() failed: device is not a SerioDevice. Device: '{:?}'", 72 device.name() 73 ); 74 SystemError::EINVAL 75 })?; 76 77 return pdrv.connect(&pdev); 78 } 79 remove(&self, _device: &Arc<dyn Device>) -> Result<(), SystemError>80 fn remove(&self, _device: &Arc<dyn Device>) -> Result<(), SystemError> { 81 todo!() 82 } 83 sync_state(&self, _device: &Arc<dyn Device>)84 fn sync_state(&self, _device: &Arc<dyn Device>) { 85 todo!() 86 } 87 shutdown(&self, _device: &Arc<dyn Device>)88 fn shutdown(&self, _device: &Arc<dyn Device>) { 89 todo!() 90 } 91 resume(&self, _device: &Arc<dyn Device>) -> Result<(), SystemError>92 fn resume(&self, _device: &Arc<dyn Device>) -> Result<(), SystemError> { 93 todo!() 94 } 95 match_device( &self, device: &Arc<dyn Device>, driver: &Arc<dyn Driver>, ) -> Result<bool, SystemError>96 fn match_device( 97 &self, 98 device: &Arc<dyn Device>, 99 driver: &Arc<dyn Driver>, 100 ) -> Result<bool, SystemError> { 101 // 尝试从 ACPI 中匹配 102 if let Ok(x) = acpi_manager().driver_match_device(driver, device) { 103 if x { 104 return Ok(true); 105 } 106 } 107 108 // 尝试从 ID table 中匹配 109 if let Some(drv_id_table) = driver.id_table() { 110 let pdev = device 111 .clone() 112 .cast::<dyn SerioDevice>() 113 .map_err(|_| SystemError::EINVAL)?; 114 if drv_id_table.name().eq(&pdev.name()) { 115 return Ok(true); 116 } 117 } 118 119 // 尝试根据设备名称匹配 120 return Ok(device.name().eq(&driver.name())); 121 } 122 } 123 124 #[derive(Debug)] 125 pub struct SerioDeviceAttrGroup; 126 127 impl AttributeGroup for SerioDeviceAttrGroup { name(&self) -> Option<&str>128 fn name(&self) -> Option<&str> { 129 None 130 } 131 132 /// todo: https://code.dragonos.org.cn/xref/linux-6.1.9/drivers/input/serio/serio.c#473 attrs(&self) -> &[&'static dyn Attribute]133 fn attrs(&self) -> &[&'static dyn Attribute] { 134 return &[]; 135 } 136 is_visible(&self, _kobj: Arc<dyn KObject>, _attr: &dyn Attribute) -> Option<ModeType>137 fn is_visible(&self, _kobj: Arc<dyn KObject>, _attr: &dyn Attribute) -> Option<ModeType> { 138 None 139 } 140 } 141