xref: /DragonOS/kernel/src/driver/input/ps2_mouse/ps_mouse_driver.rs (revision a381e482cbe742b2e4bbeaacae134a8131bf3f91)
1 use alloc::{
2     string::ToString,
3     sync::{Arc, Weak},
4     vec::Vec,
5 };
6 use system_error::SystemError;
7 use unified_init::macros::unified_init;
8 
9 use crate::{
10     arch::{io::PortIOArch, CurrentPortIOArch},
11     driver::{
12         base::{
13             device::{bus::Bus, driver::Driver, Device, IdTable},
14             kobject::{KObjType, KObject, KObjectState, LockedKObjectState},
15             kset::KSet,
16         },
17         input::serio::{
18             serio_device::SerioDevice,
19             serio_driver::{serio_driver_manager, SerioDriver},
20         },
21     },
22     filesystem::kernfs::KernFSInode,
23     init::initcall::INITCALL_DEVICE,
24     libs::{
25         rwlock::{RwLockReadGuard, RwLockWriteGuard},
26         spinlock::SpinLock,
27     },
28 };
29 
30 use super::ps_mouse_device::{ps2_mouse_device, Ps2MouseDevice};
31 
32 extern "C" {
33     fn c_ps2_mouse_init();
34 }
35 
36 #[no_mangle]
37 unsafe extern "C" fn ps2_mouse_driver_interrupt() {
38     if let Some(psmouse_device) = ps2_mouse_device() {
39         ps2_mouse_driver()
40             .interrupt(&(psmouse_device as Arc<dyn SerioDevice>), 0, 0)
41             .ok();
42     } else {
43         unsafe { CurrentPortIOArch::in8(0x60) };
44     }
45 }
46 
47 static mut PS2_MOUSE_DRIVER: Option<Arc<Ps2MouseDriver>> = None;
48 
49 #[allow(dead_code)]
50 pub fn ps2_mouse_driver() -> Arc<Ps2MouseDriver> {
51     unsafe { PS2_MOUSE_DRIVER.clone().unwrap() }
52 }
53 
54 #[derive(Debug)]
55 #[cast_to([sync] Driver)]
56 #[cast_to([sync] SerioDriver)]
57 pub struct Ps2MouseDriver {
58     inner: SpinLock<InnerPs2MouseDriver>,
59     kobj_state: LockedKObjectState,
60 }
61 
62 impl Ps2MouseDriver {
63     pub const NAME: &'static str = "psmouse";
64     pub fn new() -> Arc<Self> {
65         let r = Arc::new(Ps2MouseDriver {
66             inner: SpinLock::new(InnerPs2MouseDriver {
67                 ktype: None,
68                 kset: None,
69                 parent: None,
70                 kernfs_inode: None,
71                 devices: Vec::new(),
72                 bus: None,
73                 self_ref: Weak::new(),
74             }),
75             kobj_state: LockedKObjectState::new(None),
76         });
77 
78         r.inner.lock().self_ref = Arc::downgrade(&r);
79         return r;
80     }
81 
82     #[allow(dead_code)]
83     pub fn process_packet(&self) {
84         let guard = self.inner.lock();
85         if guard.devices.is_empty() {
86             return;
87         }
88 
89         let device: Option<&Ps2MouseDevice> = guard.devices[0]
90             .as_any_ref()
91             .downcast_ref::<Ps2MouseDevice>();
92         let _ = device.unwrap().process_packet();
93     }
94 }
95 
96 #[derive(Debug)]
97 pub struct InnerPs2MouseDriver {
98     ktype: Option<&'static dyn KObjType>,
99     kset: Option<Arc<KSet>>,
100     parent: Option<Weak<dyn KObject>>,
101     kernfs_inode: Option<Arc<KernFSInode>>,
102     devices: Vec<Arc<dyn Device>>,
103     bus: Option<Weak<dyn Bus>>,
104     self_ref: Weak<Ps2MouseDriver>,
105 }
106 
107 impl Driver for Ps2MouseDriver {
108     fn id_table(&self) -> Option<IdTable> {
109         Some(IdTable::new("psmouse".to_string(), None))
110     }
111 
112     fn devices(&self) -> alloc::vec::Vec<Arc<dyn Device>> {
113         self.inner.lock().devices.clone()
114     }
115 
116     fn add_device(&self, device: Arc<dyn Device>) {
117         let mut guard = self.inner.lock();
118         // check if the device is already in the list
119         if guard.devices.iter().any(|dev| Arc::ptr_eq(dev, &device)) {
120             return;
121         }
122 
123         guard.devices.push(device);
124     }
125 
126     fn delete_device(&self, device: &Arc<dyn Device>) {
127         let mut guard = self.inner.lock();
128         guard.devices.retain(|dev| !Arc::ptr_eq(dev, device));
129     }
130 
131     fn set_bus(&self, bus: Option<alloc::sync::Weak<dyn Bus>>) {
132         self.inner.lock().bus = bus;
133     }
134 
135     fn bus(&self) -> Option<Weak<dyn Bus>> {
136         self.inner.lock().bus.clone()
137     }
138 }
139 
140 impl KObject for Ps2MouseDriver {
141     fn as_any_ref(&self) -> &dyn core::any::Any {
142         self
143     }
144 
145     fn set_inode(&self, inode: Option<Arc<KernFSInode>>) {
146         self.inner.lock().kernfs_inode = inode;
147     }
148 
149     fn inode(&self) -> Option<Arc<KernFSInode>> {
150         self.inner.lock().kernfs_inode.clone()
151     }
152 
153     fn parent(&self) -> Option<alloc::sync::Weak<dyn KObject>> {
154         self.inner.lock().parent.clone()
155     }
156 
157     fn set_parent(&self, parent: Option<alloc::sync::Weak<dyn KObject>>) {
158         self.inner.lock().parent = parent;
159     }
160 
161     fn kset(&self) -> Option<Arc<KSet>> {
162         self.inner.lock().kset.clone()
163     }
164 
165     fn set_kset(&self, kset: Option<Arc<KSet>>) {
166         self.inner.lock().kset = kset;
167     }
168 
169     fn kobj_type(&self) -> Option<&'static dyn KObjType> {
170         self.inner.lock().ktype
171     }
172 
173     fn set_kobj_type(&self, ktype: Option<&'static dyn KObjType>) {
174         self.inner.lock().ktype = ktype;
175     }
176 
177     fn name(&self) -> alloc::string::String {
178         Self::NAME.to_string()
179     }
180 
181     fn set_name(&self, _name: alloc::string::String) {}
182 
183     fn kobj_state(&self) -> RwLockReadGuard<KObjectState> {
184         self.kobj_state.read()
185     }
186 
187     fn kobj_state_mut(&self) -> RwLockWriteGuard<KObjectState> {
188         self.kobj_state.write()
189     }
190 
191     fn set_kobj_state(&self, state: KObjectState) {
192         *self.kobj_state.write() = state;
193     }
194 }
195 
196 impl SerioDriver for Ps2MouseDriver {
197     fn write_wakeup(
198         &self,
199         _device: &Arc<dyn SerioDevice>,
200     ) -> Result<(), system_error::SystemError> {
201         todo!()
202     }
203 
204     fn interrupt(
205         &self,
206         device: &Arc<dyn SerioDevice>,
207         _char: u8,
208         _int: u8,
209     ) -> Result<(), system_error::SystemError> {
210         let device = device
211             .clone()
212             .arc_any()
213             .downcast::<Ps2MouseDevice>()
214             .map_err(|_| SystemError::EINVAL)?;
215         device.process_packet()?;
216         Ok(())
217     }
218 
219     fn connect(&self, device: &Arc<dyn SerioDevice>) -> Result<(), system_error::SystemError> {
220         let device = device
221             .clone()
222             .arc_any()
223             .downcast::<Ps2MouseDevice>()
224             .map_err(|_| SystemError::EINVAL)?;
225 
226         device.set_driver(Some(self.inner.lock_irqsave().self_ref.clone()));
227 
228         device.init()?;
229         unsafe { c_ps2_mouse_init() };
230         return Ok(());
231     }
232 
233     fn reconnect(&self, _device: &Arc<dyn SerioDevice>) -> Result<(), system_error::SystemError> {
234         todo!()
235     }
236 
237     fn fast_reconnect(
238         &self,
239         _device: &Arc<dyn SerioDevice>,
240     ) -> Result<(), system_error::SystemError> {
241         todo!()
242     }
243 
244     fn disconnect(&self, _device: &Arc<dyn SerioDevice>) -> Result<(), system_error::SystemError> {
245         todo!()
246     }
247 
248     fn cleanup(&self, _device: &Arc<dyn SerioDevice>) -> Result<(), system_error::SystemError> {
249         todo!()
250     }
251 }
252 
253 #[unified_init(INITCALL_DEVICE)]
254 fn ps2_mouse_driver_init() -> Result<(), SystemError> {
255     kdebug!("Ps2_mouse_drive initing...");
256     let driver = Ps2MouseDriver::new();
257     serio_driver_manager().register(driver.clone())?;
258     unsafe { PS2_MOUSE_DRIVER = Some(driver) };
259     return Ok(());
260 }
261