xref: /DragonOS/kernel/src/driver/input/serio/i8042/i8042_device.rs (revision 52dc4c3ee87b31fee4f567f776fd8ad58fbfd9a5)
1 use alloc::{
2     string::{String, ToString},
3     sync::{Arc, Weak},
4 };
5 
6 use crate::{
7     driver::base::{
8         class::Class,
9         device::{
10             bus::Bus, driver::Driver, Device, DeviceCommonData, DeviceState, DeviceType, IdTable,
11         },
12         kobject::{KObjType, KObject, KObjectCommonData, KObjectState, LockedKObjectState},
13         kset::KSet,
14         platform::platform_device::PlatformDevice,
15     },
16     filesystem::kernfs::KernFSInode,
17     libs::{
18         rwlock::{RwLockReadGuard, RwLockWriteGuard},
19         spinlock::{SpinLock, SpinLockGuard},
20     },
21 };
22 
23 #[derive(Debug)]
24 #[cast_to([sync] Device)]
25 #[cast_to([sync] PlatformDevice)]
26 pub struct I8042PlatformDevice {
27     inner: SpinLock<InnerI8042PlatformDevice>,
28     kobj_state: LockedKObjectState,
29 }
30 
31 impl I8042PlatformDevice {
32     pub const NAME: &'static str = "i8042";
33     pub fn new() -> Self {
34         return Self {
35             inner: SpinLock::new(InnerI8042PlatformDevice {
36                 kobject_common: KObjectCommonData::default(),
37                 device_common: DeviceCommonData::default(),
38                 device_state: DeviceState::NotInitialized,
39                 pdev_id: 0,
40                 pdev_id_auto: false,
41             }),
42             kobj_state: LockedKObjectState::new(None),
43         };
44     }
45 
46     fn inner(&self) -> SpinLockGuard<InnerI8042PlatformDevice> {
47         self.inner.lock()
48     }
49 }
50 
51 #[derive(Debug)]
52 pub struct InnerI8042PlatformDevice {
53     kobject_common: KObjectCommonData,
54     device_common: DeviceCommonData,
55     device_state: DeviceState,
56     pdev_id: i32,
57     pdev_id_auto: bool,
58 }
59 
60 impl Device for I8042PlatformDevice {
61     fn dev_type(&self) -> DeviceType {
62         DeviceType::Char
63     }
64 
65     fn id_table(&self) -> IdTable {
66         IdTable::new(self.name(), None)
67     }
68 
69     fn bus(&self) -> Option<Weak<dyn Bus>> {
70         self.inner().device_common.bus.clone()
71     }
72 
73     fn set_bus(&self, bus: Option<Weak<dyn Bus>>) {
74         self.inner().device_common.bus = bus;
75     }
76     fn class(&self) -> Option<Arc<dyn Class>> {
77         let mut guard = self.inner();
78         let r = guard.device_common.class.clone()?.upgrade();
79         if r.is_none() {
80             guard.device_common.class = None;
81         }
82 
83         return r;
84     }
85     fn set_class(&self, class: Option<Weak<dyn Class>>) {
86         self.inner().device_common.class = class;
87     }
88 
89     fn driver(&self) -> Option<Arc<dyn Driver>> {
90         self.inner().device_common.driver.clone()?.upgrade()
91     }
92 
93     fn set_driver(&self, driver: Option<Weak<dyn Driver>>) {
94         self.inner().device_common.driver = driver;
95     }
96 
97     fn is_dead(&self) -> bool {
98         false
99     }
100 
101     fn can_match(&self) -> bool {
102         true
103     }
104 
105     fn set_can_match(&self, _can_match: bool) {}
106 
107     fn state_synced(&self) -> bool {
108         true
109     }
110 
111     fn dev_parent(&self) -> Option<Weak<dyn Device>> {
112         self.inner().device_common.get_parent_weak_or_clear()
113     }
114 
115     fn set_dev_parent(&self, dev_parent: Option<Weak<dyn Device>>) {
116         self.inner().device_common.parent = dev_parent;
117     }
118 }
119 
120 impl KObject for I8042PlatformDevice {
121     fn as_any_ref(&self) -> &dyn core::any::Any {
122         self
123     }
124 
125     fn set_inode(&self, inode: Option<Arc<KernFSInode>>) {
126         self.inner().kobject_common.kern_inode = inode;
127     }
128 
129     fn inode(&self) -> Option<Arc<KernFSInode>> {
130         self.inner().kobject_common.kern_inode.clone()
131     }
132 
133     fn parent(&self) -> Option<Weak<dyn KObject>> {
134         self.inner().kobject_common.parent.clone()
135     }
136 
137     fn set_parent(&self, parent: Option<Weak<dyn KObject>>) {
138         self.inner().kobject_common.parent = parent;
139     }
140 
141     fn kset(&self) -> Option<Arc<KSet>> {
142         self.inner().kobject_common.kset.clone()
143     }
144 
145     fn set_kset(&self, kset: Option<Arc<KSet>>) {
146         self.inner().kobject_common.kset = kset;
147     }
148 
149     fn kobj_type(&self) -> Option<&'static dyn KObjType> {
150         self.inner().kobject_common.kobj_type
151     }
152 
153     fn set_kobj_type(&self, ktype: Option<&'static dyn KObjType>) {
154         self.inner().kobject_common.kobj_type = ktype;
155     }
156 
157     fn name(&self) -> String {
158         Self::NAME.to_string()
159     }
160 
161     fn set_name(&self, _name: String) {
162         // do nothing
163     }
164 
165     fn kobj_state(&self) -> RwLockReadGuard<KObjectState> {
166         self.kobj_state.read()
167     }
168 
169     fn kobj_state_mut(&self) -> RwLockWriteGuard<KObjectState> {
170         self.kobj_state.write()
171     }
172 
173     fn set_kobj_state(&self, state: KObjectState) {
174         *self.kobj_state.write() = state;
175     }
176 }
177 
178 impl PlatformDevice for I8042PlatformDevice {
179     fn pdev_name(&self) -> &str {
180         Self::NAME
181     }
182 
183     fn set_pdev_id(&self, id: i32) {
184         self.inner.lock().pdev_id = id;
185     }
186 
187     fn set_pdev_id_auto(&self, id_auto: bool) {
188         self.inner.lock().pdev_id_auto = id_auto;
189     }
190 
191     fn is_initialized(&self) -> bool {
192         self.inner.lock().device_state == DeviceState::Initialized
193     }
194 
195     fn set_state(&self, set_state: DeviceState) {
196         self.inner.lock().device_state = set_state;
197     }
198 }
199