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