xref: /DragonOS/kernel/src/driver/video/fbdev/base/fbsysfs.rs (revision bc6f0a967c8cb1e9379ced184b25a7722fbda2a4)
1 use alloc::sync::Arc;
2 use system_error::SystemError;
3 
4 use crate::{
5     driver::base::kobject::KObject,
6     filesystem::{
7         sysfs::{file::sysfs_emit_str, Attribute, AttributeGroup, SysFSOpsSupport},
8         vfs::syscall::ModeType,
9     },
10 };
11 
12 use super::fbmem::FbDevice;
13 
14 /// 为FbDevice实现的sysfs属性组
15 #[derive(Debug)]
16 pub struct FbDeviceAttrGroup;
17 
18 impl AttributeGroup for FbDeviceAttrGroup {
19     fn name(&self) -> Option<&str> {
20         None
21     }
22 
23     fn attrs(&self) -> &[&'static dyn Attribute] {
24         &[
25             &AttrBitsPerPixel,
26             &AttrBlank,
27             &AttrMode,
28             &AttrModes,
29             &AttrName,
30             &AttrPan,
31             &AttrRotate,
32             &AttrState,
33             &AttrStride,
34             &AttrVirtualSize,
35         ]
36     }
37 
38     fn is_visible(
39         &self,
40         _kobj: alloc::sync::Arc<dyn KObject>,
41         _attr: &'static dyn Attribute,
42     ) -> Option<ModeType> {
43         None
44     }
45 }
46 
47 #[derive(Debug)]
48 struct AttrName;
49 
50 impl Attribute for AttrName {
51     fn name(&self) -> &str {
52         "name"
53     }
54 
55     fn mode(&self) -> ModeType {
56         ModeType::S_IRUGO
57     }
58 
59     fn support(&self) -> SysFSOpsSupport {
60         SysFSOpsSupport::ATTR_SHOW
61     }
62 
63     fn show(&self, kobj: Arc<dyn KObject>, buf: &mut [u8]) -> Result<usize, SystemError> {
64         let fb_dev = kobj.arc_any().downcast::<FbDevice>().unwrap();
65         let fb = fb_dev.framebuffer().ok_or(SystemError::ENODEV)?;
66         let name = fb.name();
67         return sysfs_emit_str(buf, &format!("{}\n", name));
68     }
69 }
70 
71 #[derive(Debug)]
72 struct AttrBitsPerPixel;
73 
74 impl Attribute for AttrBitsPerPixel {
75     fn name(&self) -> &str {
76         "bits_per_pixel"
77     }
78 
79     fn mode(&self) -> ModeType {
80         ModeType::S_IRUGO | ModeType::S_IWUSR
81     }
82 
83     fn support(&self) -> SysFSOpsSupport {
84         SysFSOpsSupport::ATTR_SHOW | SysFSOpsSupport::ATTR_STORE
85     }
86 
87     fn store(&self, _kobj: Arc<dyn KObject>, _buf: &[u8]) -> Result<usize, SystemError> {
88         kwarn!("attr bits_per_pixel store not implemented");
89         return Err(SystemError::ENOSYS);
90     }
91 
92     fn show(&self, kobj: Arc<dyn KObject>, buf: &mut [u8]) -> Result<usize, SystemError> {
93         let fb_dev = kobj.arc_any().downcast::<FbDevice>().unwrap();
94         let fb = fb_dev.framebuffer().ok_or(SystemError::ENODEV)?;
95         let bits_per_pixel = fb.current_fb_var().bits_per_pixel;
96         return sysfs_emit_str(buf, &format!("{}\n", bits_per_pixel));
97     }
98 }
99 
100 /// 用于清空屏幕的属性
101 #[derive(Debug)]
102 struct AttrBlank;
103 
104 impl Attribute for AttrBlank {
105     fn name(&self) -> &str {
106         "blank"
107     }
108 
109     fn mode(&self) -> ModeType {
110         ModeType::S_IWUSR
111     }
112 
113     fn support(&self) -> SysFSOpsSupport {
114         SysFSOpsSupport::ATTR_STORE
115     }
116 
117     // todo:  https://code.dragonos.org.cn/xref/linux-6.1.9/drivers/video/fbdev/core/fbsysfs.c#309
118     fn store(&self, _kobj: Arc<dyn KObject>, _buf: &[u8]) -> Result<usize, SystemError> {
119         kwarn!("attr blank store not implemented");
120         return Err(SystemError::ENOSYS);
121     }
122 }
123 
124 #[derive(Debug)]
125 struct AttrMode;
126 
127 impl Attribute for AttrMode {
128     fn name(&self) -> &str {
129         "mode"
130     }
131 
132     fn mode(&self) -> ModeType {
133         ModeType::S_IRUGO | ModeType::S_IWUSR
134     }
135 
136     fn support(&self) -> SysFSOpsSupport {
137         SysFSOpsSupport::ATTR_SHOW | SysFSOpsSupport::ATTR_STORE
138     }
139 
140     /// https://code.dragonos.org.cn/xref/linux-6.1.9/drivers/video/fbdev/core/fbsysfs.c#166
141     fn show(&self, _kobj: Arc<dyn KObject>, _buf: &mut [u8]) -> Result<usize, SystemError> {
142         todo!("AttrMode::show")
143     }
144 
145     /// https://code.dragonos.org.cn/xref/linux-6.1.9/drivers/video/fbdev/core/fbsysfs.c#135
146     fn store(&self, _kobj: Arc<dyn KObject>, _buf: &[u8]) -> Result<usize, SystemError> {
147         todo!("AttrMode::store")
148     }
149 }
150 
151 #[derive(Debug)]
152 struct AttrModes;
153 
154 impl Attribute for AttrModes {
155     fn name(&self) -> &str {
156         "modes"
157     }
158 
159     fn mode(&self) -> ModeType {
160         ModeType::S_IRUGO | ModeType::S_IWUSR
161     }
162 
163     fn support(&self) -> SysFSOpsSupport {
164         SysFSOpsSupport::ATTR_SHOW | SysFSOpsSupport::ATTR_STORE
165     }
166 
167     /// https://code.dragonos.org.cn/xref/linux-6.1.9/drivers/video/fbdev/core/fbsysfs.c#206
168     fn show(&self, _kobj: Arc<dyn KObject>, _buf: &mut [u8]) -> Result<usize, SystemError> {
169         todo!("AttrMode::show")
170     }
171 
172     /// https://code.dragonos.org.cn/xref/linux-6.1.9/drivers/video/fbdev/core/fbsysfs.c#177
173     fn store(&self, _kobj: Arc<dyn KObject>, _buf: &[u8]) -> Result<usize, SystemError> {
174         todo!("AttrMode::store")
175     }
176 }
177 
178 #[derive(Debug)]
179 struct AttrPan;
180 
181 impl Attribute for AttrPan {
182     fn name(&self) -> &str {
183         "pan"
184     }
185 
186     fn mode(&self) -> ModeType {
187         ModeType::S_IRUGO | ModeType::S_IWUSR
188     }
189 
190     fn support(&self) -> SysFSOpsSupport {
191         SysFSOpsSupport::ATTR_SHOW | SysFSOpsSupport::ATTR_STORE
192     }
193 
194     fn show(&self, kobj: Arc<dyn KObject>, buf: &mut [u8]) -> Result<usize, SystemError> {
195         let fb_dev = kobj.arc_any().downcast::<FbDevice>().unwrap();
196         let fb = fb_dev.framebuffer().ok_or(SystemError::ENODEV)?;
197         let var_info = fb.current_fb_var();
198         return sysfs_emit_str(buf, &format!("{},{}\n", var_info.xoffset, var_info.yoffset));
199     }
200 
201     /// https://code.dragonos.org.cn/xref/linux-6.1.9/drivers/video/fbdev/core/fbsysfs.c#365
202     fn store(&self, _kobj: Arc<dyn KObject>, _buf: &[u8]) -> Result<usize, SystemError> {
203         todo!("AttrPan::store")
204     }
205 }
206 
207 #[derive(Debug)]
208 struct AttrVirtualSize;
209 
210 impl Attribute for AttrVirtualSize {
211     fn name(&self) -> &str {
212         "virtual_size"
213     }
214 
215     fn mode(&self) -> ModeType {
216         ModeType::S_IRUGO | ModeType::S_IWUSR
217     }
218 
219     fn support(&self) -> SysFSOpsSupport {
220         SysFSOpsSupport::ATTR_SHOW | SysFSOpsSupport::ATTR_STORE
221     }
222 
223     fn show(&self, kobj: Arc<dyn KObject>, buf: &mut [u8]) -> Result<usize, SystemError> {
224         let fb_dev = kobj.arc_any().downcast::<FbDevice>().unwrap();
225         let fb = fb_dev.framebuffer().ok_or(SystemError::ENODEV)?;
226         let var_info = fb.current_fb_var();
227         return sysfs_emit_str(
228             buf,
229             &format!("{},{}\n", var_info.xres_virtual, var_info.yres_virtual),
230         );
231     }
232 
233     /// https://code.dragonos.org.cn/xref/linux-6.1.9/drivers/video/fbdev/core/fbsysfs.c#273
234     fn store(&self, _kobj: Arc<dyn KObject>, _buf: &[u8]) -> Result<usize, SystemError> {
235         todo!("AttrVirtualSize::store")
236     }
237 }
238 
239 #[derive(Debug)]
240 struct AttrStride;
241 
242 impl Attribute for AttrStride {
243     fn name(&self) -> &str {
244         "stride"
245     }
246 
247     fn mode(&self) -> ModeType {
248         ModeType::S_IRUGO
249     }
250 
251     fn support(&self) -> SysFSOpsSupport {
252         SysFSOpsSupport::ATTR_SHOW
253     }
254 
255     fn show(&self, kobj: Arc<dyn KObject>, buf: &mut [u8]) -> Result<usize, SystemError> {
256         let fb_dev = kobj.arc_any().downcast::<FbDevice>().unwrap();
257         let fb = fb_dev.framebuffer().ok_or(SystemError::ENODEV)?;
258         let fix_info = fb.current_fb_fix();
259         return sysfs_emit_str(buf, &format!("{}\n", fix_info.line_length));
260     }
261 }
262 
263 #[derive(Debug)]
264 struct AttrRotate;
265 
266 impl Attribute for AttrRotate {
267     fn name(&self) -> &str {
268         "rotate"
269     }
270 
271     fn mode(&self) -> ModeType {
272         ModeType::S_IRUGO | ModeType::S_IWUSR
273     }
274 
275     fn support(&self) -> SysFSOpsSupport {
276         SysFSOpsSupport::ATTR_SHOW | SysFSOpsSupport::ATTR_STORE
277     }
278 
279     fn show(&self, kobj: Arc<dyn KObject>, buf: &mut [u8]) -> Result<usize, SystemError> {
280         let fb_dev = kobj.arc_any().downcast::<FbDevice>().unwrap();
281         let fb = fb_dev.framebuffer().ok_or(SystemError::ENODEV)?;
282         let var_info = fb.current_fb_var();
283 
284         return sysfs_emit_str(buf, &format!("{}\n", var_info.rotate_angle));
285     }
286 
287     /// todo https://code.dragonos.org.cn/xref/linux-6.1.9/drivers/video/fbdev/core/fbsysfs.c#246
288     fn store(&self, _kobj: Arc<dyn KObject>, _buf: &[u8]) -> Result<usize, SystemError> {
289         todo!("AttrRotate::store")
290     }
291 }
292 
293 #[derive(Debug)]
294 struct AttrState;
295 
296 impl Attribute for AttrState {
297     fn name(&self) -> &str {
298         "state"
299     }
300 
301     fn mode(&self) -> ModeType {
302         ModeType::S_IRUGO | ModeType::S_IWUSR
303     }
304 
305     fn support(&self) -> SysFSOpsSupport {
306         SysFSOpsSupport::ATTR_SHOW | SysFSOpsSupport::ATTR_STORE
307     }
308 
309     fn show(&self, kobj: Arc<dyn KObject>, buf: &mut [u8]) -> Result<usize, SystemError> {
310         let fb_dev = kobj.arc_any().downcast::<FbDevice>().unwrap();
311         let fb = fb_dev.framebuffer().ok_or(SystemError::ENODEV)?;
312 
313         return sysfs_emit_str(buf, &format!("{}\n", fb.state() as u8));
314     }
315 
316     /// todo https://code.dragonos.org.cn/xref/linux-6.1.9/drivers/video/fbdev/core/fbsysfs.c#406
317     fn store(&self, _kobj: Arc<dyn KObject>, _buf: &[u8]) -> Result<usize, SystemError> {
318         todo!("AttrState::store")
319     }
320 }
321