xref: /DragonOS/kernel/src/driver/input/serio/i8042/i8042_device.rs (revision da152319797436368304cbc3f85a3b9ec049134b)
19993c0fcSR0ronoa use alloc::{
29993c0fcSR0ronoa     string::{String, ToString},
39993c0fcSR0ronoa     sync::{Arc, Weak},
49993c0fcSR0ronoa };
59993c0fcSR0ronoa 
69993c0fcSR0ronoa use crate::{
79993c0fcSR0ronoa     driver::base::{
89993c0fcSR0ronoa         class::Class,
99993c0fcSR0ronoa         device::{bus::Bus, driver::Driver, Device, DeviceState, DeviceType, IdTable},
109993c0fcSR0ronoa         kobject::{KObjType, KObject, KObjectState, LockedKObjectState},
119993c0fcSR0ronoa         kset::KSet,
12*da152319SLoGin         platform::platform_device::PlatformDevice,
139993c0fcSR0ronoa     },
149993c0fcSR0ronoa     filesystem::kernfs::KernFSInode,
159993c0fcSR0ronoa     libs::{
169993c0fcSR0ronoa         rwlock::{RwLockReadGuard, RwLockWriteGuard},
179993c0fcSR0ronoa         spinlock::SpinLock,
189993c0fcSR0ronoa     },
199993c0fcSR0ronoa };
209993c0fcSR0ronoa 
219993c0fcSR0ronoa #[derive(Debug)]
229993c0fcSR0ronoa #[cast_to([sync] Device)]
239993c0fcSR0ronoa #[cast_to([sync] PlatformDevice)]
249993c0fcSR0ronoa pub struct I8042PlatformDevice {
259993c0fcSR0ronoa     inner: SpinLock<InnerI8042PlatformDevice>,
269993c0fcSR0ronoa     kobj_state: LockedKObjectState,
279993c0fcSR0ronoa }
289993c0fcSR0ronoa 
299993c0fcSR0ronoa impl I8042PlatformDevice {
309993c0fcSR0ronoa     pub const NAME: &'static str = "i8042";
319993c0fcSR0ronoa     pub fn new() -> Self {
329993c0fcSR0ronoa         return Self {
339993c0fcSR0ronoa             inner: SpinLock::new(InnerI8042PlatformDevice {
349993c0fcSR0ronoa                 bus: None,
359993c0fcSR0ronoa                 class: None,
369993c0fcSR0ronoa                 driver: None,
379993c0fcSR0ronoa                 kern_inode: None,
389993c0fcSR0ronoa                 parent: None,
399993c0fcSR0ronoa                 kset: None,
409993c0fcSR0ronoa                 kobj_type: None,
419993c0fcSR0ronoa                 device_state: DeviceState::NotInitialized,
429993c0fcSR0ronoa                 pdev_id: 0,
439993c0fcSR0ronoa                 pdev_id_auto: false,
449993c0fcSR0ronoa             }),
459993c0fcSR0ronoa             kobj_state: LockedKObjectState::new(None),
469993c0fcSR0ronoa         };
479993c0fcSR0ronoa     }
489993c0fcSR0ronoa }
499993c0fcSR0ronoa 
509993c0fcSR0ronoa #[derive(Debug)]
519993c0fcSR0ronoa pub struct InnerI8042PlatformDevice {
529993c0fcSR0ronoa     bus: Option<Weak<dyn Bus>>,
534256da7fSLoGin     class: Option<Weak<dyn Class>>,
549993c0fcSR0ronoa     driver: Option<Weak<dyn Driver>>,
559993c0fcSR0ronoa     kern_inode: Option<Arc<KernFSInode>>,
569993c0fcSR0ronoa     parent: Option<Weak<dyn KObject>>,
579993c0fcSR0ronoa     kset: Option<Arc<KSet>>,
589993c0fcSR0ronoa     kobj_type: Option<&'static dyn KObjType>,
599993c0fcSR0ronoa     device_state: DeviceState,
609993c0fcSR0ronoa     pdev_id: i32,
619993c0fcSR0ronoa     pdev_id_auto: bool,
629993c0fcSR0ronoa }
639993c0fcSR0ronoa 
649993c0fcSR0ronoa impl Device for I8042PlatformDevice {
659993c0fcSR0ronoa     fn dev_type(&self) -> DeviceType {
669993c0fcSR0ronoa         DeviceType::Char
679993c0fcSR0ronoa     }
689993c0fcSR0ronoa 
699993c0fcSR0ronoa     fn id_table(&self) -> IdTable {
709993c0fcSR0ronoa         IdTable::new(self.name(), None)
719993c0fcSR0ronoa     }
729993c0fcSR0ronoa 
739993c0fcSR0ronoa     fn bus(&self) -> Option<Weak<dyn Bus>> {
749993c0fcSR0ronoa         self.inner.lock().bus.clone()
759993c0fcSR0ronoa     }
769993c0fcSR0ronoa 
779993c0fcSR0ronoa     fn set_bus(&self, bus: Option<Weak<dyn Bus>>) {
789993c0fcSR0ronoa         self.inner.lock().bus = bus;
799993c0fcSR0ronoa     }
804256da7fSLoGin     fn class(&self) -> Option<Arc<dyn Class>> {
814256da7fSLoGin         let mut guard = self.inner.lock();
824256da7fSLoGin         let r = guard.class.clone()?.upgrade();
834256da7fSLoGin         if r.is_none() {
844256da7fSLoGin             guard.class = None;
854256da7fSLoGin         }
869993c0fcSR0ronoa 
874256da7fSLoGin         return r;
884256da7fSLoGin     }
894256da7fSLoGin     fn set_class(&self, class: Option<Weak<dyn Class>>) {
909993c0fcSR0ronoa         self.inner.lock().class = class;
919993c0fcSR0ronoa     }
929993c0fcSR0ronoa 
939993c0fcSR0ronoa     fn driver(&self) -> Option<Arc<dyn Driver>> {
949993c0fcSR0ronoa         self.inner.lock().driver.clone()?.upgrade()
959993c0fcSR0ronoa     }
969993c0fcSR0ronoa 
979993c0fcSR0ronoa     fn set_driver(&self, driver: Option<Weak<dyn Driver>>) {
989993c0fcSR0ronoa         self.inner.lock().driver = driver;
999993c0fcSR0ronoa     }
1009993c0fcSR0ronoa 
1019993c0fcSR0ronoa     fn is_dead(&self) -> bool {
1029993c0fcSR0ronoa         false
1039993c0fcSR0ronoa     }
1049993c0fcSR0ronoa 
1059993c0fcSR0ronoa     fn can_match(&self) -> bool {
1069993c0fcSR0ronoa         true
1079993c0fcSR0ronoa     }
1089993c0fcSR0ronoa 
1099993c0fcSR0ronoa     fn set_can_match(&self, _can_match: bool) {}
1109993c0fcSR0ronoa 
1119993c0fcSR0ronoa     fn state_synced(&self) -> bool {
1129993c0fcSR0ronoa         true
1139993c0fcSR0ronoa     }
1149993c0fcSR0ronoa }
1159993c0fcSR0ronoa 
1169993c0fcSR0ronoa impl KObject for I8042PlatformDevice {
1179993c0fcSR0ronoa     fn as_any_ref(&self) -> &dyn core::any::Any {
1189993c0fcSR0ronoa         self
1199993c0fcSR0ronoa     }
1209993c0fcSR0ronoa 
1219993c0fcSR0ronoa     fn set_inode(&self, inode: Option<Arc<KernFSInode>>) {
1229993c0fcSR0ronoa         self.inner.lock().kern_inode = inode;
1239993c0fcSR0ronoa     }
1249993c0fcSR0ronoa 
1259993c0fcSR0ronoa     fn inode(&self) -> Option<Arc<KernFSInode>> {
1269993c0fcSR0ronoa         self.inner.lock().kern_inode.clone()
1279993c0fcSR0ronoa     }
1289993c0fcSR0ronoa 
1299993c0fcSR0ronoa     fn parent(&self) -> Option<Weak<dyn KObject>> {
1309993c0fcSR0ronoa         self.inner.lock().parent.clone()
1319993c0fcSR0ronoa     }
1329993c0fcSR0ronoa 
1339993c0fcSR0ronoa     fn set_parent(&self, parent: Option<Weak<dyn KObject>>) {
1349993c0fcSR0ronoa         self.inner.lock().parent = parent;
1359993c0fcSR0ronoa     }
1369993c0fcSR0ronoa 
1379993c0fcSR0ronoa     fn kset(&self) -> Option<Arc<KSet>> {
1389993c0fcSR0ronoa         self.inner.lock().kset.clone()
1399993c0fcSR0ronoa     }
1409993c0fcSR0ronoa 
1419993c0fcSR0ronoa     fn set_kset(&self, kset: Option<Arc<KSet>>) {
1429993c0fcSR0ronoa         self.inner.lock().kset = kset;
1439993c0fcSR0ronoa     }
1449993c0fcSR0ronoa 
1459993c0fcSR0ronoa     fn kobj_type(&self) -> Option<&'static dyn KObjType> {
1469993c0fcSR0ronoa         self.inner.lock().kobj_type
1479993c0fcSR0ronoa     }
1489993c0fcSR0ronoa 
1499993c0fcSR0ronoa     fn set_kobj_type(&self, ktype: Option<&'static dyn KObjType>) {
1509993c0fcSR0ronoa         self.inner.lock().kobj_type = ktype;
1519993c0fcSR0ronoa     }
1529993c0fcSR0ronoa 
1539993c0fcSR0ronoa     fn name(&self) -> String {
1549993c0fcSR0ronoa         Self::NAME.to_string()
1559993c0fcSR0ronoa     }
1569993c0fcSR0ronoa 
1579993c0fcSR0ronoa     fn set_name(&self, _name: String) {
1589993c0fcSR0ronoa         // do nothing
1599993c0fcSR0ronoa     }
1609993c0fcSR0ronoa 
1619993c0fcSR0ronoa     fn kobj_state(&self) -> RwLockReadGuard<KObjectState> {
1629993c0fcSR0ronoa         self.kobj_state.read()
1639993c0fcSR0ronoa     }
1649993c0fcSR0ronoa 
1659993c0fcSR0ronoa     fn kobj_state_mut(&self) -> RwLockWriteGuard<KObjectState> {
1669993c0fcSR0ronoa         self.kobj_state.write()
1679993c0fcSR0ronoa     }
1689993c0fcSR0ronoa 
1699993c0fcSR0ronoa     fn set_kobj_state(&self, state: KObjectState) {
1709993c0fcSR0ronoa         *self.kobj_state.write() = state;
1719993c0fcSR0ronoa     }
1729993c0fcSR0ronoa }
1739993c0fcSR0ronoa 
1749993c0fcSR0ronoa impl PlatformDevice for I8042PlatformDevice {
1759993c0fcSR0ronoa     fn pdev_name(&self) -> &str {
1769993c0fcSR0ronoa         Self::NAME
1779993c0fcSR0ronoa     }
1789993c0fcSR0ronoa 
1799993c0fcSR0ronoa     fn set_pdev_id(&self, id: i32) {
1809993c0fcSR0ronoa         self.inner.lock().pdev_id = id;
1819993c0fcSR0ronoa     }
1829993c0fcSR0ronoa 
1839993c0fcSR0ronoa     fn set_pdev_id_auto(&self, id_auto: bool) {
1849993c0fcSR0ronoa         self.inner.lock().pdev_id_auto = id_auto;
1859993c0fcSR0ronoa     }
1869993c0fcSR0ronoa 
1879993c0fcSR0ronoa     fn is_initialized(&self) -> bool {
1889993c0fcSR0ronoa         self.inner.lock().device_state == DeviceState::Initialized
1899993c0fcSR0ronoa     }
1909993c0fcSR0ronoa 
1919993c0fcSR0ronoa     fn set_state(&self, set_state: DeviceState) {
1929993c0fcSR0ronoa         self.inner.lock().device_state = set_state;
1939993c0fcSR0ronoa     }
1949993c0fcSR0ronoa }
195