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