xref: /DragonOS/kernel/src/driver/input/serio/i8042/i8042_ports.rs (revision 52dc4c3ee87b31fee4f567f776fd8ad58fbfd9a5)
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, DeviceCommonData, DeviceType, IdTable},
12             kobject::{KObjType, KObject, KObjectCommonData, 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, SpinLockGuard},
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     device_common: DeviceCommonData,
36     kobject_common: KObjectCommonData,
37 }
38 
39 impl I8042AuxPort {
40     pub const NAME: &'static str = "serio1";
41     pub fn new() -> Self {
42         return Self {
43             inner: SpinLock::new(InnerI8042AuxPort {
44                 device_common: DeviceCommonData::default(),
45                 kobject_common: KObjectCommonData::default(),
46             }),
47             kobj_state: LockedKObjectState::new(None),
48         };
49     }
50 
51     fn inner(&self) -> SpinLockGuard<InnerI8042AuxPort> {
52         self.inner.lock()
53     }
54 }
55 
56 impl Device for I8042AuxPort {
57     fn dev_type(&self) -> DeviceType {
58         DeviceType::Char
59     }
60 
61     fn id_table(&self) -> IdTable {
62         IdTable::new(self.name(), None)
63     }
64 
65     fn bus(&self) -> Option<Weak<dyn Bus>> {
66         self.inner().device_common.bus.clone()
67     }
68 
69     fn set_bus(&self, bus: Option<Weak<dyn Bus>>) {
70         self.inner().device_common.bus = bus;
71     }
72 
73     fn set_class(&self, class: Option<Weak<dyn Class>>) {
74         self.inner().device_common.class = class;
75     }
76 
77     fn class(&self) -> Option<Arc<dyn Class>> {
78         let mut guard = self.inner();
79         let r = guard.device_common.class.clone()?.upgrade();
80         if r.is_none() {
81             guard.device_common.class = None;
82         }
83         return r;
84     }
85 
86     fn driver(&self) -> Option<Arc<dyn Driver>> {
87         self.inner().device_common.driver.clone()?.upgrade()
88     }
89 
90     fn set_driver(&self, driver: Option<Weak<dyn Driver>>) {
91         self.inner().device_common.driver = driver;
92     }
93 
94     fn is_dead(&self) -> bool {
95         false
96     }
97 
98     fn can_match(&self) -> bool {
99         true
100     }
101 
102     fn set_can_match(&self, _can_match: bool) {}
103 
104     fn state_synced(&self) -> bool {
105         true
106     }
107 
108     fn dev_parent(&self) -> Option<Weak<dyn Device>> {
109         self.inner().device_common.get_parent_weak_or_clear()
110     }
111 
112     fn set_dev_parent(&self, parent: Option<Weak<dyn Device>>) {
113         self.inner().device_common.parent = parent;
114     }
115 }
116 
117 impl KObject for I8042AuxPort {
118     fn as_any_ref(&self) -> &dyn core::any::Any {
119         self
120     }
121 
122     fn set_inode(&self, inode: Option<Arc<KernFSInode>>) {
123         self.inner().kobject_common.kern_inode = inode;
124     }
125 
126     fn inode(&self) -> Option<Arc<KernFSInode>> {
127         self.inner().kobject_common.kern_inode.clone()
128     }
129 
130     fn parent(&self) -> Option<Weak<dyn KObject>> {
131         self.inner().kobject_common.parent.clone()
132     }
133 
134     fn set_parent(&self, parent: Option<Weak<dyn KObject>>) {
135         self.inner().kobject_common.parent = parent;
136     }
137 
138     fn kset(&self) -> Option<Arc<KSet>> {
139         self.inner().kobject_common.kset.clone()
140     }
141 
142     fn set_kset(&self, kset: Option<Arc<KSet>>) {
143         self.inner().kobject_common.kset = kset;
144     }
145 
146     fn kobj_type(&self) -> Option<&'static dyn KObjType> {
147         self.inner().kobject_common.kobj_type
148     }
149 
150     fn set_kobj_type(&self, ktype: Option<&'static dyn KObjType>) {
151         self.inner().kobject_common.kobj_type = ktype;
152     }
153 
154     fn name(&self) -> String {
155         Self::NAME.to_string()
156     }
157 
158     fn set_name(&self, _name: String) {
159         // do nothing
160     }
161 
162     fn kobj_state(&self) -> RwLockReadGuard<KObjectState> {
163         self.kobj_state.read()
164     }
165 
166     fn kobj_state_mut(&self) -> RwLockWriteGuard<KObjectState> {
167         self.kobj_state.write()
168     }
169 
170     fn set_kobj_state(&self, state: KObjectState) {
171         *self.kobj_state.write() = state;
172     }
173 }
174 
175 impl SerioDevice for I8042AuxPort {
176     // TODO: https://code.dragonos.org.cn/xref/linux-6.1.9/drivers/input/serio/i8042.c#387
177     fn write(&self, _device: &Arc<dyn SerioDevice>, _data: u8) -> Result<(), SystemError> {
178         todo!()
179     }
180 
181     fn open(&self, _device: &Arc<dyn SerioDevice>) -> Result<(), SystemError> {
182         Ok(())
183     }
184 
185     fn close(&self, _device: &Arc<dyn SerioDevice>) -> Result<(), SystemError> {
186         Ok(())
187     }
188 
189     fn start(&self, device: &Arc<dyn SerioDevice>) -> Result<(), SystemError> {
190         i8042_start(device)
191     }
192 
193     fn stop(&self, device: &Arc<dyn SerioDevice>) -> Result<(), SystemError> {
194         i8042_stop(device)
195     }
196 }
197