xref: /DragonOS/kernel/src/driver/input/serio/serio_device.rs (revision f2022a8a1cc4a8e2a85e9061e036e9c491a2fa00)
1 use alloc::sync::Arc;
2 use system_error::SystemError;
3 
4 use crate::driver::base::device::{bus::Bus, device_manager, Device};
5 
6 use super::serio_bus;
7 
8 /// 串行设备,实现该trait的设备实例挂载在serio总线上,同时应该实现Device trait
9 ///
10 /// 参考: https://code.dragonos.org.cn/xref/linux-6.1.9/include/linux/serio.h#20
11 pub trait SerioDevice: Device {
12     /// # 函数功能
13     ///
14     /// Serio设备写入数据
15     ///
16     /// ## 参数
17     ///
18     /// - data 写入的数据
19     ///
20     /// ## 返回值
21     ///
22     /// 无
23     fn write(&self, device: &Arc<dyn SerioDevice>, data: u8) -> Result<(), SystemError>;
24     /// Serio设备连接驱动时调用
25     fn open(&self, device: &Arc<dyn SerioDevice>) -> Result<(), SystemError>;
26     /// Serio设备断开驱动时调用
27     fn close(&self, device: &Arc<dyn SerioDevice>) -> Result<(), SystemError>;
28     /// Serio设备初始化时调用
29     fn start(&self, device: &Arc<dyn SerioDevice>) -> Result<(), SystemError>;
30     /// Serio设备销毁时调用
31     fn stop(&self, device: &Arc<dyn SerioDevice>) -> Result<(), SystemError>;
32 }
33 
34 #[inline(always)]
35 pub fn serio_device_manager() -> &'static SerioDeviceManager {
36     &SerioDeviceManager
37 }
38 
39 pub struct SerioDeviceManager;
40 
41 impl SerioDeviceManager {
42     /// # 函数功能
43     /// 注册Serio设备
44     ///
45     /// ## 参数
46     /// - device 待注册的设备
47     ///
48     /// ## 返回值
49     /// 无
50     pub fn register_port(&self, device: Arc<dyn SerioDevice>) -> Result<(), SystemError> {
51         self.init_port(device)
52     }
53 
54     /// # 函数功能
55     /// 初始化Serio设备
56     ///
57     /// ## 参数
58     /// - device 待初始化的Serio设备
59     ///
60     /// ## 返回值
61     /// 无
62     ///
63     /// todo:https://code.dragonos.org.cn/xref/linux-6.1.9/drivers/input/serio/serio.c#494
64     pub fn init_port(&self, device: Arc<dyn SerioDevice>) -> Result<(), SystemError> {
65         device.set_bus(Some(Arc::downgrade(&(serio_bus() as Arc<dyn Bus>))));
66         device_manager().add_device(device.clone() as Arc<dyn Device>)?;
67         Ok(())
68     }
69 }
70