xref: /DragonOS/kernel/src/driver/input/serio/serio_driver.rs (revision bd70d2d1f490aabd570a5301b858bd5eb04149fa)
1 use alloc::sync::Arc;
2 use system_error::SystemError;
3 
4 use crate::driver::base::device::{
5     bus::Bus,
6     driver::{driver_manager, Driver},
7 };
8 
9 use super::{serio_bus, serio_device::SerioDevice};
10 
11 /// 实现该trait的设备驱动实例应挂载在serio总线上,同时应该实现Driver trait
12 ///
13 /// 参考:  https://code.dragonos.org.cn/xref/linux-6.1.9/include/linux/serio.h#67
14 #[allow(dead_code)]
15 pub trait SerioDriver: Driver {
16     // 写入时唤醒设备
write_wakeup(&self, device: &Arc<dyn SerioDevice>) -> Result<(), SystemError>17     fn write_wakeup(&self, device: &Arc<dyn SerioDevice>) -> Result<(), SystemError>;
18     /// # 函数功能
19     /// 中断函数
20     ///
21     /// ## 参数
22     /// - device: Serio设备
23     /// - data: 端口数据
24     /// - flag: 状态掩码
25     ///
26     /// ## 返回值
27     /// 无
28     ///
29     /// todo:https://code.dragonos.org.cn/xref/linux-6.1.9/drivers/input/serio/serio.c?fi=__serio_register_driver#989
interrupt( &self, device: &Arc<dyn SerioDevice>, data: u8, flag: u8, ) -> Result<(), SystemError>30     fn interrupt(
31         &self,
32         device: &Arc<dyn SerioDevice>,
33         data: u8,
34         flag: u8,
35     ) -> Result<(), SystemError>;
36     /// Serio驱动连接设备
connect(&self, device: &Arc<dyn SerioDevice>) -> Result<(), SystemError>37     fn connect(&self, device: &Arc<dyn SerioDevice>) -> Result<(), SystemError>;
38     /// 重新连接设备
reconnect(&self, device: &Arc<dyn SerioDevice>) -> Result<(), SystemError>39     fn reconnect(&self, device: &Arc<dyn SerioDevice>) -> Result<(), SystemError>;
40     /// 快速重连设备
fast_reconnect(&self, device: &Arc<dyn SerioDevice>) -> Result<(), SystemError>41     fn fast_reconnect(&self, device: &Arc<dyn SerioDevice>) -> Result<(), SystemError>;
42     /// 驱动断开设备
disconnect(&self, device: &Arc<dyn SerioDevice>) -> Result<(), SystemError>43     fn disconnect(&self, device: &Arc<dyn SerioDevice>) -> Result<(), SystemError>;
44     /// 清除设备状态
cleanup(&self, device: &Arc<dyn SerioDevice>) -> Result<(), SystemError>45     fn cleanup(&self, device: &Arc<dyn SerioDevice>) -> Result<(), SystemError>;
46 }
47 
48 ///todo: https://code.dragonos.org.cn/xref/linux-6.1.9/drivers/input/serio/serio.c#810
49 
50 #[allow(dead_code)]
51 #[inline(always)]
serio_driver_manager() -> &'static SerioDriverManager52 pub fn serio_driver_manager() -> &'static SerioDriverManager {
53     &SerioDriverManager
54 }
55 
56 pub struct SerioDriverManager;
57 
58 #[allow(dead_code)]
59 impl SerioDriverManager {
60     /// # 函数功能
61     /// 注册Serio驱动
62     ///
63     /// ## 参数
64     /// - driver 待注册的Serio驱动
65     ///
66     /// ## 返回值
67     /// 无
register(&self, driver: Arc<dyn SerioDriver>) -> Result<(), SystemError>68     pub fn register(&self, driver: Arc<dyn SerioDriver>) -> Result<(), SystemError> {
69         driver.set_bus(Some(Arc::downgrade(&(serio_bus() as Arc<dyn Bus>))));
70         return driver_manager().register(driver as Arc<dyn Driver>);
71     }
72 
73     /// # 函数功能
74     /// 卸载Serio驱动
75     ///
76     /// ## 参数
77     /// - driver 待卸载的Serio驱动
78     ///
79     /// ## 返回值
80     /// 无
81     #[allow(dead_code)]
unregister(&self, driver: &Arc<dyn SerioDriver>)82     pub fn unregister(&self, driver: &Arc<dyn SerioDriver>) {
83         driver_manager().unregister(&(driver.clone() as Arc<dyn Driver>));
84     }
85 }
86