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