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