xref: /DragonOS/kernel/src/driver/input/serio/serio_device.rs (revision fae6e9ade46a52976ad5d099643d51cc20876448) !
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 #[allow(dead_code)]
12 pub trait SerioDevice: Device {
13     /// # 函数功能
14     ///
15     /// Serio设备写入数据
16     ///
17     /// ## 参数
18     ///
19     /// - data 写入的数据
20     ///
21     /// ## 返回值
22     ///
23     /// 无
24     fn write(&self, device: &Arc<dyn SerioDevice>, data: u8) -> Result<(), SystemError>;
25     /// Serio设备连接驱动时调用
26     fn open(&self, device: &Arc<dyn SerioDevice>) -> Result<(), SystemError>;
27     /// Serio设备断开驱动时调用
28     fn close(&self, device: &Arc<dyn SerioDevice>) -> Result<(), SystemError>;
29     /// Serio设备初始化时调用
30     fn start(&self, device: &Arc<dyn SerioDevice>) -> Result<(), SystemError>;
31     /// Serio设备销毁时调用
32     fn stop(&self, device: &Arc<dyn SerioDevice>) -> Result<(), SystemError>;
33 }
34 
35 #[inline(always)]
36 pub fn serio_device_manager() -> &'static SerioDeviceManager {
37     &SerioDeviceManager
38 }
39 
40 pub struct SerioDeviceManager;
41 
42 impl SerioDeviceManager {
43     /// # 函数功能
44     /// 注册Serio设备
45     ///
46     /// ## 参数
47     /// - device 待注册的设备
48     ///
49     /// ## 返回值
50     /// 无
51     pub fn register_port(&self, device: Arc<dyn SerioDevice>) -> Result<(), SystemError> {
52         self.init_port(device)
53     }
54 
55     /// # 函数功能
56     /// 初始化Serio设备
57     ///
58     /// ## 参数
59     /// - device 待初始化的Serio设备
60     ///
61     /// ## 返回值
62     /// 无
63     ///
64     /// todo:https://code.dragonos.org.cn/xref/linux-6.1.9/drivers/input/serio/serio.c#494
65     pub fn init_port(&self, device: Arc<dyn SerioDevice>) -> Result<(), SystemError> {
66         device.set_bus(Some(Arc::downgrade(&(serio_bus() as Arc<dyn Bus>))));
67         device_manager().add_device(device.clone() as Arc<dyn Device>)?;
68         Ok(())
69     }
70 }
71