xref: /DragonOS/kernel/src/driver/input/serio/serio_driver.rs (revision f049d1af01da7b92f312245ed411b22475b76065) !
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 #[inline(always)]
50 pub fn serio_driver_manager() -> &'static SerioDriverManager {
51     &SerioDriverManager
52 }
53 
54 pub struct SerioDriverManager;
55 
56 #[allow(dead_code)]
57 impl SerioDriverManager {
58     /// # 函数功能
59     /// 注册Serio驱动
60     ///
61     /// ## 参数
62     /// - driver 待注册的Serio驱动
63     ///
64     /// ## 返回值
65     /// 无
66     pub fn register(&self, driver: Arc<dyn SerioDriver>) -> Result<(), SystemError> {
67         driver.set_bus(Some(Arc::downgrade(&(serio_bus() as Arc<dyn Bus>))));
68         return driver_manager().register(driver as Arc<dyn Driver>);
69     }
70 
71     /// # 函数功能
72     /// 卸载Serio驱动
73     ///
74     /// ## 参数
75     /// - driver 待卸载的Serio驱动
76     ///
77     /// ## 返回值
78     /// 无
79     #[allow(dead_code)]
80     pub fn unregister(&self, driver: &Arc<dyn SerioDriver>) {
81         driver_manager().unregister(&(driver.clone() as Arc<dyn Driver>));
82     }
83 }
84