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