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