xref: /DragonOS/kernel/src/driver/input/serio/i8042/i8042_device.rs (revision 9fab312ea9921618629924ab15c28c2d255b21c6)
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,
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<Weak<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     fn class(&self) -> Option<Arc<dyn Class>> {
81         let mut guard = self.inner.lock();
82         let r = guard.class.clone()?.upgrade();
83         if r.is_none() {
84             guard.class = None;
85         }
86 
87         return r;
88     }
89     fn set_class(&self, class: Option<Weak<dyn Class>>) {
90         self.inner.lock().class = class;
91     }
92 
93     fn driver(&self) -> Option<Arc<dyn Driver>> {
94         self.inner.lock().driver.clone()?.upgrade()
95     }
96 
97     fn set_driver(&self, driver: Option<Weak<dyn Driver>>) {
98         self.inner.lock().driver = driver;
99     }
100 
101     fn is_dead(&self) -> bool {
102         false
103     }
104 
105     fn can_match(&self) -> bool {
106         true
107     }
108 
109     fn set_can_match(&self, _can_match: bool) {}
110 
111     fn state_synced(&self) -> bool {
112         true
113     }
114 }
115 
116 impl KObject for I8042PlatformDevice {
117     fn as_any_ref(&self) -> &dyn core::any::Any {
118         self
119     }
120 
121     fn set_inode(&self, inode: Option<Arc<KernFSInode>>) {
122         self.inner.lock().kern_inode = inode;
123     }
124 
125     fn inode(&self) -> Option<Arc<KernFSInode>> {
126         self.inner.lock().kern_inode.clone()
127     }
128 
129     fn parent(&self) -> Option<Weak<dyn KObject>> {
130         self.inner.lock().parent.clone()
131     }
132 
133     fn set_parent(&self, parent: Option<Weak<dyn KObject>>) {
134         self.inner.lock().parent = parent;
135     }
136 
137     fn kset(&self) -> Option<Arc<KSet>> {
138         self.inner.lock().kset.clone()
139     }
140 
141     fn set_kset(&self, kset: Option<Arc<KSet>>) {
142         self.inner.lock().kset = kset;
143     }
144 
145     fn kobj_type(&self) -> Option<&'static dyn KObjType> {
146         self.inner.lock().kobj_type
147     }
148 
149     fn set_kobj_type(&self, ktype: Option<&'static dyn KObjType>) {
150         self.inner.lock().kobj_type = ktype;
151     }
152 
153     fn name(&self) -> String {
154         Self::NAME.to_string()
155     }
156 
157     fn set_name(&self, _name: String) {
158         // do nothing
159     }
160 
161     fn kobj_state(&self) -> RwLockReadGuard<KObjectState> {
162         self.kobj_state.read()
163     }
164 
165     fn kobj_state_mut(&self) -> RwLockWriteGuard<KObjectState> {
166         self.kobj_state.write()
167     }
168 
169     fn set_kobj_state(&self, state: KObjectState) {
170         *self.kobj_state.write() = state;
171     }
172 }
173 
174 impl PlatformDevice for I8042PlatformDevice {
175     fn pdev_name(&self) -> &str {
176         Self::NAME
177     }
178 
179     fn set_pdev_id(&self, id: i32) {
180         self.inner.lock().pdev_id = id;
181     }
182 
183     fn set_pdev_id_auto(&self, id_auto: bool) {
184         self.inner.lock().pdev_id_auto = id_auto;
185     }
186 
187     fn is_initialized(&self) -> bool {
188         self.inner.lock().device_state == DeviceState::Initialized
189     }
190 
191     fn set_state(&self, set_state: DeviceState) {
192         self.inner.lock().device_state = set_state;
193     }
194 }
195