xref: /DragonOS/kernel/src/driver/input/serio/i8042/i8042_driver.rs (revision 01c18c64b14b4ebabd98fa92c587c26874275eb1)
1 use alloc::{
2     string::{String, ToString},
3     sync::{Arc, Weak},
4     vec::Vec,
5 };
6 use system_error::SystemError;
7 
8 use crate::{
9     driver::base::{
10         device::{bus::Bus, driver::Driver, Device, IdTable},
11         kobject::{KObjType, KObject, KObjectState, LockedKObjectState},
12         kset::KSet,
13         platform::{platform_device::PlatformDevice, platform_driver::PlatformDriver},
14     },
15     filesystem::kernfs::KernFSInode,
16     libs::{
17         rwlock::{RwLockReadGuard, RwLockWriteGuard},
18         spinlock::SpinLock,
19     },
20 };
21 
22 use super::{i8042_device::I8042PlatformDevice, i8042_setup_aux};
23 
24 #[derive(Debug)]
25 #[cast_to([sync] PlatformDriver)]
26 pub struct I8042Driver {
27     inner: SpinLock<InnerI8042Driver>,
28     kobj_state: LockedKObjectState,
29 }
30 
31 impl I8042Driver {
32     pub const NAME: &'static str = "i8042";
33     pub fn new() -> Arc<I8042Driver> {
34         let r = Arc::new(Self {
35             inner: SpinLock::new(InnerI8042Driver {
36                 ktype: None,
37                 kset: None,
38                 parent: None,
39                 kernfs_inode: None,
40                 devices: Vec::new(),
41                 bus: None,
42                 self_ref: Weak::new(),
43             }),
44             kobj_state: LockedKObjectState::new(None),
45         });
46 
47         r.inner.lock().self_ref = Arc::downgrade(&r);
48 
49         return r;
50     }
51 }
52 
53 #[derive(Debug)]
54 pub struct InnerI8042Driver {
55     ktype: Option<&'static dyn KObjType>,
56     kset: Option<Arc<KSet>>,
57     parent: Option<Weak<dyn KObject>>,
58     kernfs_inode: Option<Arc<KernFSInode>>,
59     devices: Vec<Arc<dyn Device>>,
60     bus: Option<Weak<dyn Bus>>,
61 
62     self_ref: Weak<I8042Driver>,
63 }
64 
65 impl PlatformDriver for I8042Driver {
66     // TODO: https://code.dragonos.org.cn/xref/linux-6.1.9/drivers/input/serio/i8042.c#1542
67     fn probe(&self, device: &Arc<dyn PlatformDevice>) -> Result<(), SystemError> {
68         let device = device
69             .clone()
70             .arc_any()
71             .downcast::<I8042PlatformDevice>()
72             .map_err(|_| SystemError::EINVAL)?;
73 
74         device.set_driver(Some(self.inner.lock().self_ref.clone()));
75 
76         i8042_setup_aux()?;
77         return Ok(());
78     }
79 
80     // TODO: https://code.dragonos.org.cn/xref/linux-6.1.9/drivers/input/serio/i8042.c#1587
81     fn remove(&self, _device: &Arc<dyn PlatformDevice>) -> Result<(), SystemError> {
82         todo!()
83     }
84 
85     // TODO: https://code.dragonos.org.cn/xref/linux-6.1.9/drivers/input/serio/i8042.c#1322
86     fn shutdown(&self, _device: &Arc<dyn PlatformDevice>) -> Result<(), SystemError> {
87         todo!()
88     }
89 
90     fn suspend(&self, _device: &Arc<dyn PlatformDevice>) -> Result<(), SystemError> {
91         // do nothing
92         return Ok(());
93     }
94 
95     fn resume(&self, _device: &Arc<dyn PlatformDevice>) -> Result<(), SystemError> {
96         // do nothing
97         return Ok(());
98     }
99 }
100 
101 impl Driver for I8042Driver {
102     fn id_table(&self) -> Option<IdTable> {
103         Some(IdTable::new(I8042PlatformDevice::NAME.to_string(), None))
104     }
105 
106     fn devices(&self) -> Vec<Arc<dyn Device>> {
107         self.inner.lock().devices.clone()
108     }
109 
110     fn add_device(&self, device: Arc<dyn Device>) {
111         let mut guard = self.inner.lock();
112         // check if the device is already in the list
113         if guard.devices.iter().any(|dev| Arc::ptr_eq(dev, &device)) {
114             return;
115         }
116 
117         guard.devices.push(device);
118     }
119 
120     fn delete_device(&self, device: &Arc<dyn Device>) {
121         let mut guard = self.inner.lock();
122         guard.devices.retain(|dev| !Arc::ptr_eq(dev, device));
123     }
124 
125     fn set_bus(&self, bus: Option<Weak<dyn Bus>>) {
126         self.inner.lock().bus = bus;
127     }
128 
129     fn bus(&self) -> Option<Weak<dyn Bus>> {
130         self.inner.lock().bus.clone()
131     }
132 }
133 
134 impl KObject for I8042Driver {
135     fn as_any_ref(&self) -> &dyn core::any::Any {
136         self
137     }
138 
139     fn set_inode(&self, inode: Option<Arc<KernFSInode>>) {
140         self.inner.lock().kernfs_inode = inode;
141     }
142 
143     fn inode(&self) -> Option<Arc<KernFSInode>> {
144         self.inner.lock().kernfs_inode.clone()
145     }
146 
147     fn parent(&self) -> Option<Weak<dyn KObject>> {
148         self.inner.lock().parent.clone()
149     }
150 
151     fn set_parent(&self, parent: Option<Weak<dyn KObject>>) {
152         self.inner.lock().parent = parent;
153     }
154 
155     fn kset(&self) -> Option<Arc<KSet>> {
156         self.inner.lock().kset.clone()
157     }
158 
159     fn set_kset(&self, kset: Option<Arc<KSet>>) {
160         self.inner.lock().kset = kset;
161     }
162 
163     fn kobj_type(&self) -> Option<&'static dyn KObjType> {
164         self.inner.lock().ktype
165     }
166 
167     fn set_kobj_type(&self, ktype: Option<&'static dyn KObjType>) {
168         self.inner.lock().ktype = ktype;
169     }
170 
171     fn name(&self) -> String {
172         Self::NAME.to_string()
173     }
174 
175     fn set_name(&self, _name: String) {
176         // do nothing
177     }
178 
179     fn kobj_state(&self) -> RwLockReadGuard<KObjectState> {
180         self.kobj_state.read()
181     }
182 
183     fn kobj_state_mut(&self) -> RwLockWriteGuard<KObjectState> {
184         self.kobj_state.write()
185     }
186 
187     fn set_kobj_state(&self, state: KObjectState) {
188         *self.kobj_state.write() = state;
189     }
190 }
191