xref: /DragonOS/kernel/src/driver/input/serio/i8042/i8042_ports.rs (revision 338f6903262c5031abad3c8e361813355a27fcdb)
1 use alloc::{
2     string::{String, ToString},
3     sync::{Arc, Weak},
4 };
5 use system_error::SystemError;
6 
7 use crate::{
8     driver::{
9         base::{
10             class::Class,
11             device::{bus::Bus, driver::Driver, Device, DeviceType, IdTable},
12             kobject::{KObjType, KObject, KObjectState, LockedKObjectState},
13             kset::KSet,
14         },
15         input::serio::serio_device::SerioDevice,
16     },
17     filesystem::kernfs::KernFSInode,
18     libs::{
19         rwlock::{RwLockReadGuard, RwLockWriteGuard},
20         spinlock::SpinLock,
21     },
22 };
23 
24 use super::{i8042_start, i8042_stop};
25 
26 #[derive(Debug)]
27 #[cast_to([sync] Device)]
28 pub struct I8042AuxPort {
29     inner: SpinLock<InnerI8042AuxPort>,
30     kobj_state: LockedKObjectState,
31 }
32 
33 #[derive(Debug)]
34 pub struct InnerI8042AuxPort {
35     bus: Option<Weak<dyn Bus>>,
36     class: Option<Arc<dyn Class>>,
37     driver: Option<Weak<dyn Driver>>,
38     kern_inode: Option<Arc<KernFSInode>>,
39     parent: Option<Weak<dyn KObject>>,
40     kset: Option<Arc<KSet>>,
41     kobj_type: Option<&'static dyn KObjType>,
42 }
43 
44 impl I8042AuxPort {
45     pub const NAME: &'static str = "serio1";
46     pub fn new() -> Self {
47         return Self {
48             inner: SpinLock::new(InnerI8042AuxPort {
49                 bus: None,
50                 class: None,
51                 driver: None,
52                 kern_inode: None,
53                 parent: None,
54                 kset: None,
55                 kobj_type: None,
56             }),
57             kobj_state: LockedKObjectState::new(None),
58         };
59     }
60 }
61 
62 impl Device for I8042AuxPort {
63     fn dev_type(&self) -> DeviceType {
64         DeviceType::Char
65     }
66 
67     fn id_table(&self) -> IdTable {
68         IdTable::new(self.name(), None)
69     }
70 
71     fn bus(&self) -> Option<Weak<dyn Bus>> {
72         self.inner.lock().bus.clone()
73     }
74 
75     fn set_bus(&self, bus: Option<Weak<dyn Bus>>) {
76         self.inner.lock().bus = bus;
77     }
78 
79     fn set_class(&self, class: Option<Arc<dyn Class>>) {
80         self.inner.lock().class = class;
81     }
82 
83     fn driver(&self) -> Option<Arc<dyn Driver>> {
84         self.inner.lock().driver.clone()?.upgrade()
85     }
86 
87     fn set_driver(&self, driver: Option<Weak<dyn Driver>>) {
88         self.inner.lock().driver = driver;
89     }
90 
91     fn is_dead(&self) -> bool {
92         false
93     }
94 
95     fn can_match(&self) -> bool {
96         true
97     }
98 
99     fn set_can_match(&self, _can_match: bool) {}
100 
101     fn state_synced(&self) -> bool {
102         true
103     }
104 }
105 
106 impl KObject for I8042AuxPort {
107     fn as_any_ref(&self) -> &dyn core::any::Any {
108         self
109     }
110 
111     fn set_inode(&self, inode: Option<Arc<KernFSInode>>) {
112         self.inner.lock().kern_inode = inode;
113     }
114 
115     fn inode(&self) -> Option<Arc<KernFSInode>> {
116         self.inner.lock().kern_inode.clone()
117     }
118 
119     fn parent(&self) -> Option<Weak<dyn KObject>> {
120         self.inner.lock().parent.clone()
121     }
122 
123     fn set_parent(&self, parent: Option<Weak<dyn KObject>>) {
124         self.inner.lock().parent = parent;
125     }
126 
127     fn kset(&self) -> Option<Arc<KSet>> {
128         self.inner.lock().kset.clone()
129     }
130 
131     fn set_kset(&self, kset: Option<Arc<KSet>>) {
132         self.inner.lock().kset = kset;
133     }
134 
135     fn kobj_type(&self) -> Option<&'static dyn KObjType> {
136         self.inner.lock().kobj_type
137     }
138 
139     fn set_kobj_type(&self, ktype: Option<&'static dyn KObjType>) {
140         self.inner.lock().kobj_type = ktype;
141     }
142 
143     fn name(&self) -> String {
144         Self::NAME.to_string()
145     }
146 
147     fn set_name(&self, _name: String) {
148         // do nothing
149     }
150 
151     fn kobj_state(&self) -> RwLockReadGuard<KObjectState> {
152         self.kobj_state.read()
153     }
154 
155     fn kobj_state_mut(&self) -> RwLockWriteGuard<KObjectState> {
156         self.kobj_state.write()
157     }
158 
159     fn set_kobj_state(&self, state: KObjectState) {
160         *self.kobj_state.write() = state;
161     }
162 }
163 
164 impl SerioDevice for I8042AuxPort {
165     // TODO: https://code.dragonos.org.cn/xref/linux-6.1.9/drivers/input/serio/i8042.c#387
166     fn write(&self, _device: &Arc<dyn SerioDevice>, _data: u8) -> Result<(), SystemError> {
167         todo!()
168     }
169 
170     fn open(&self, _device: &Arc<dyn SerioDevice>) -> Result<(), SystemError> {
171         Ok(())
172     }
173 
174     fn close(&self, _device: &Arc<dyn SerioDevice>) -> Result<(), SystemError> {
175         Ok(())
176     }
177 
178     fn start(&self, device: &Arc<dyn SerioDevice>) -> Result<(), SystemError> {
179         i8042_start(device)
180     }
181 
182     fn stop(&self, device: &Arc<dyn SerioDevice>) -> Result<(), SystemError> {
183         i8042_stop(device)
184     }
185 }
186